Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 156 lines (122 sloc) 4.84 KB
#!/usr/bin/env bash
if [[ -n "$TRACE" ]]; then
export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -o xtrace
fi
set -euo pipefail
IFS=$'\n\t'
RELEASE="10.3"
MIRROR="ftp.freebsd.org"
MIRROR_PATH="pub/FreeBSD/releases/ISO-IMAGES"
ISO="FreeBSD-${RELEASE}-RELEASE-amd64-disc1.iso"
ISO_DIR="/data/freebsd-iso"
ISO_MOUNT="/mnt/freebsd-iso"
CUSTOM_ISO_LAYOUT="/data/custom-freebsd-iso"
SHA256SUM="CHECKSUM.SHA256-FreeBSD-${RELEASE}-RELEASE-amd64"
CUSTOM_ISO_FILENAME="freebsd-10-custom.iso"
ME_FREEBSD_DIST_NAME="joyent.txz"
INSTALLER_CFG="installerconfig"
# Clean up ISO file if download is interupted
trap "rm -rf $PWD/${ISO_DIR}/${ISO}" SIGHUP SIGINT SIGKILL SIGTERM
# Delete Previous custom layout
trap "rm -rf $CUSTOM_ISO_LAYOUT" SIGHUP SIGINT SIGKILL SIGTERM
function fetch_iso() {
if [[ ! -d $ISO_DIR ]]; then
mkdir -p $ISO_DIR
fi
echo "==> Fetching ${SHA256SUM}"
curl -sS -o ${ISO_DIR}/${SHA256SUM} ftp://${MIRROR}/${MIRROR_PATH}/${RELEASE}/${SHA256SUM}
echo "==> Checking for local copy of $ISO..."
if [[ -e $ISO_DIR/$ISO ]]; then
echo "==> Found local copy of $ISO"
else
echo "==> Local copy not found."
echo "==> Fetching ISO from $MIRROR..."
curl -sS -o ${ISO_DIR}/${ISO} ftp://${MIRROR}/${MIRROR_PATH}/${RELEASE}/${ISO}
echo "==> Done!"
echo "==> ${ISO} saved to $ISO_DIR/"
echo "==> Verifying $ISO with ${SHA256SUM}"
if [[ "$(sha256 $ISO_DIR/$ISO | cut -f2 -d= | tr -d '[[:space:]]')" == "$(grep "($ISO)" $ISO_DIR/${SHA256SUM} | cut -f2 -d= | tr -d '[[:space:]]')" ]]; then
echo "==> Checksums match."
else
echo "==> Checksums don't match!"
exit 1
fi
fi
}
function create_layout() {
echo "==> Creating custom ISO Layout"
if [[ -d $CUSTOM_ISO_LAYOUT ]]; then
echo "==> Previous layout $CUSTOM_ISO_LAYOUT exists...deleting"
rm -rf $CUSTOM_ISO_LAYOUT
fi
echo "==> Creating $CUSTOM_ISO_LAYOUT"
mkdir -p $CUSTOM_ISO_LAYOUT
if [[ ! -d $ISO_MOUNT ]]; then
echo "==> Creating $ISO_MOUNT ..."
mkdir $ISO_MOUNT
fi
if [[ $(df | grep $ISO_MOUNT) ]]; then
echo "==> Unmounting previous $ISO_MOUNT..."
umount $ISO_MOUNT
fi
echo "==> Mounting $ISO to $ISO_MOUNT"
mount -t cd9660 /dev/`mdconfig -f $ISO_DIR/$ISO` $ISO_MOUNT
echo "==> Copying layout from $ISO_MOUNT to $CUSTOM_ISO_LAYOUT"
rsync -aq $ISO_MOUNT/ $CUSTOM_ISO_LAYOUT
echo "==> Copying guest tools to $CUSTOM_ISO_LAYOUT:"
if [[ ! -d me-freebsd ]]; then
echo "===> Getting me-freebsd..."
git clone https://github.com/joyent/me-freebsd.git
else
echo "===> Updating me-freebsd before copying to $CUSTOM_ISO_LAYOUT..."
( cd me-freebsd
git pull
)
fi
integrate_me_freebsd_dstribution
echo "==> Copying $INSTALLER_CFG to $CUSTOM_ISO_LAYOUT"
cp $INSTALLER_CFG $CUSTOM_ISO_LAYOUT/etc/
echo "==> Enable serial and internal consoles"
echo "-Dhv" > $CUSTOM_ISO_LAYOUT/boot.config
echo "cuau0 \"/usr/libexec/getty std.38400\" xterm on secure" >> $CUSTOM_ISO_LAYOUT/etc/ttys
echo "==> Setting autoboot delay to 5 seconds"
echo "autoboot_delay=\"5\"" >> $CUSTOM_ISO_LAYOUT/boot/loader.conf
echo "==> Get network up automatically in the blank VM"
echo "ifconfig_vtnet0=\"DHCP\"" >> $CUSTOM_ISO_LAYOUT/etc/rc.conf
echo "ifconfig_vtnet1=\"DHCP\"" >> $CUSTOM_ISO_LAYOUT/etc/rc.conf
echo "==> Remove resolv.conf symlink"
rm $CUSTOM_ISO_LAYOUT/etc/resolv.conf
echo "==> Set resolvers in the blank VM"
echo "nameserver 8.8.8.8" >> $CUSTOM_ISO_LAYOUT/etc/resolv.conf
echo "nameserver 8.8.4.4" >> $CUSTOM_ISO_LAYOUT/etc/resolv.conf
echo "==> Unmounting $ISO_MOUNT"
umount $ISO_MOUNT
}
function create_newiso() {
echo "==> Getting Volume ID for ${ISO}"
CUSTOM_ISO_TITLE=$(isoinfo -d -i ${ISO_DIR}/${ISO} | grep "Volume id" | awk '{print $3}')
echo "==> Volume ID is $CUSTOM_ISO_TITLE"
echo "==> Preparing NEW ISO"
mkisofs -J -R -no-emul-boot \
-V "$CUSTOM_ISO_TITLE" \
-p "Joyent" -b boot/cdboot \
-o $CUSTOM_ISO_FILENAME $CUSTOM_ISO_LAYOUT
echo "==> Custom ISO now ready: $CUSTOM_ISO_FILENAME"
}
function integrate_me_freebsd_dstribution() {
echo "==> Integrating me-freebsd distribution"
[[ -f $ME_FREEBSD_DIST_NAME ]] && rm $ME_FREEBSD_DIST_NAME
( cd me-freebsd
tar -cvJpf ../${ME_FREEBSD_DIST_NAME} ./etc ./lib ./usr
)
DISTFILE_SHASUM_STDOUT=$(shasum -a 256 $ME_FREEBSD_DIST_NAME)
DISTFILE_SHASUM_PLAIN=${DISTFILE_SHASUM_STDOUT% *}
DISTFILE_FILECOUNT=$(tar -tf $ME_FREEBSD_DIST_NAME | wc -l | tr -d ' ')
echo -e "${ME_FREEBSD_DIST_NAME}\t${DISTFILE_SHASUM_PLAIN}\t${DISTFILE_FILECOUNT}\t${ME_FREEBSD_DIST_NAME%.*}\t\"Joyent smartmachine tools\"\ton" >> ${CUSTOM_ISO_LAYOUT}/usr/freebsd-dist/MANIFEST
cp $ME_FREEBSD_DIST_NAME ${CUSTOM_ISO_LAYOUT}/usr/freebsd-dist/${ME_FREEBSD_DIST_NAME}
}
echo "==> ISO Build Starting!"
fetch_iso
create_layout
create_newiso