11e7c1f17SGlen Barber#!/bin/sh 21e7c1f17SGlen Barber# 31e7c1f17SGlen Barber# $FreeBSD$ 41e7c1f17SGlen Barber# 51e7c1f17SGlen Barber# 61e7c1f17SGlen Barber# Common functions for virtual machine image build scripts. 71e7c1f17SGlen Barber# 81e7c1f17SGlen Barber 91e7c1f17SGlen Barberexport PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin" 101e7c1f17SGlen Barbertrap "cleanup" INT QUIT TRAP ABRT TERM 111e7c1f17SGlen Barber 12efeb11a7SGlen Barberwrite_partition_layout() { 134e7bc9f0SColin Percival if [ -z "${NOSWAP}" ]; then 144e7bc9f0SColin Percival SWAPOPT="-p freebsd-swap/swapfs::1G" 154e7bc9f0SColin Percival fi 16efeb11a7SGlen Barber 17*4b8175eeSGlen Barber _OBJDIR="$(make -C ${WORLDDIR} -V .OBJDIR)" 18*4b8175eeSGlen Barber if [ -d "${_OBJDIR%%/usr/src}/${TARGET}.${TARGET_ARCH}" ]; then 19*4b8175eeSGlen Barber BOOTFILES="${_OBJDIR%%/usr/src}/${TARGET}.${TARGET_ARCH}/usr/src/sys/boot" 20*4b8175eeSGlen Barber else 21*4b8175eeSGlen Barber BOOTFILES="${_OBJDIR}/usr/src/sys/boot" 22*4b8175eeSGlen Barber fi 23*4b8175eeSGlen Barber 24efeb11a7SGlen Barber case "${TARGET}:${TARGET_ARCH}" in 25efeb11a7SGlen Barber amd64:amd64 | i386:i386) 26*4b8175eeSGlen Barber mkimg -s gpt -b ${BOOTFILES}/i386/pmbr/pmbr \ 27*4b8175eeSGlen Barber -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot \ 284e7bc9f0SColin Percival ${SWAPOPT} \ 2938d59ac7SGlen Barber -p freebsd-ufs/rootfs:=${VMBASE} \ 30efeb11a7SGlen Barber -o ${VMIMAGE} 31efeb11a7SGlen Barber ;; 32efeb11a7SGlen Barber powerpc:powerpc*) 337d565d60SGlen Barber mkimg -s apm \ 34*4b8175eeSGlen Barber -p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \ 354e7bc9f0SColin Percival ${SWAPOPT} \ 3638d59ac7SGlen Barber -p freebsd-ufs/rootfs:=${VMBASE} \ 37efeb11a7SGlen Barber -o ${VMIMAGE} 38efeb11a7SGlen Barber ;; 39efeb11a7SGlen Barber *) 40efeb11a7SGlen Barber # ENOTSUPP 41efeb11a7SGlen Barber return 1 42efeb11a7SGlen Barber ;; 43efeb11a7SGlen Barber esac 44efeb11a7SGlen Barber 45efeb11a7SGlen Barber return 0 46efeb11a7SGlen Barber} 471e7c1f17SGlen Barber 481e7c1f17SGlen Barbererr() { 491e7c1f17SGlen Barber printf "${@}\n" 501e7c1f17SGlen Barber cleanup 511e7c1f17SGlen Barber return 1 521e7c1f17SGlen Barber} 531e7c1f17SGlen Barber 541e7c1f17SGlen Barbercleanup() { 55dcdebabdSColin Percival if [ -c "${DESTDIR}/dev/null" ]; then 5696341228SColin Percival umount_loop ${DESTDIR}/dev 2>/dev/null 5796341228SColin Percival fi 5896341228SColin Percival umount_loop ${DESTDIR} 591e7c1f17SGlen Barber if [ ! -z "${mddev}" ]; then 601e7c1f17SGlen Barber mdconfig -d -u ${mddev} 611e7c1f17SGlen Barber fi 621e7c1f17SGlen Barber 631e7c1f17SGlen Barber return 0 641e7c1f17SGlen Barber} 651e7c1f17SGlen Barber 661e7c1f17SGlen Barbervm_create_base() { 671e7c1f17SGlen Barber # Creates the UFS root filesystem for the virtual machine disk, 681e7c1f17SGlen Barber # written to the formatted disk image with mkimg(1). 691e7c1f17SGlen Barber 701e7c1f17SGlen Barber mkdir -p ${DESTDIR} 711e7c1f17SGlen Barber truncate -s ${VMSIZE} ${VMBASE} 721e7c1f17SGlen Barber mddev=$(mdconfig -f ${VMBASE}) 73*4b8175eeSGlen Barber newfs /dev/${mddev} 741e7c1f17SGlen Barber mount /dev/${mddev} ${DESTDIR} 751e7c1f17SGlen Barber 761e7c1f17SGlen Barber return 0 771e7c1f17SGlen Barber} 781e7c1f17SGlen Barber 7925c11557SColin Percivalvm_copy_base() { 8025c11557SColin Percival # Creates a new UFS root filesystem and copies the contents of the 8125c11557SColin Percival # current root filesystem into it. This produces a "clean" disk 8225c11557SColin Percival # image without any remnants of files which were created temporarily 8325c11557SColin Percival # during image-creation and have since been deleted (e.g., downloaded 8425c11557SColin Percival # package archives). 8525c11557SColin Percival 8625c11557SColin Percival mkdir -p ${DESTDIR}/old 8725c11557SColin Percival mdold=$(mdconfig -f ${VMBASE}) 8825c11557SColin Percival mount /dev/${mdold} ${DESTDIR}/old 8925c11557SColin Percival 9025c11557SColin Percival truncate -s ${VMSIZE} ${VMBASE}.tmp 9125c11557SColin Percival mkdir -p ${DESTDIR}/new 9225c11557SColin Percival mdnew=$(mdconfig -f ${VMBASE}.tmp) 93*4b8175eeSGlen Barber newfs /dev/${mdnew} 9425c11557SColin Percival mount /dev/${mdnew} ${DESTDIR}/new 9525c11557SColin Percival 96*4b8175eeSGlen Barber tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new 9725c11557SColin Percival 9896341228SColin Percival umount_loop /dev/${mdold} 9925c11557SColin Percival rmdir ${DESTDIR}/old 10025c11557SColin Percival mdconfig -d -u ${mdold} 10125c11557SColin Percival 10296341228SColin Percival umount_loop /dev/${mdnew} 10325c11557SColin Percival rmdir ${DESTDIR}/new 104*4b8175eeSGlen Barber tunefs -j enable /dev/${mdnew} 10525c11557SColin Percival mdconfig -d -u ${mdnew} 10625c11557SColin Percival mv ${VMBASE}.tmp ${VMBASE} 10725c11557SColin Percival} 10825c11557SColin Percival 1091e7c1f17SGlen Barbervm_install_base() { 1101e7c1f17SGlen Barber # Installs the FreeBSD userland/kernel to the virtual machine disk. 1111e7c1f17SGlen Barber 1121e7c1f17SGlen Barber cd ${WORLDDIR} && \ 1131e7c1f17SGlen Barber make DESTDIR=${DESTDIR} \ 1141e7c1f17SGlen Barber installworld installkernel distribution || \ 1151e7c1f17SGlen Barber err "\n\nCannot install the base system to ${DESTDIR}." 1161e7c1f17SGlen Barber 1171e7c1f17SGlen Barber echo '# Custom /etc/fstab for FreeBSD VM images' \ 1181e7c1f17SGlen Barber > ${DESTDIR}/etc/fstab 1191e7c1f17SGlen Barber echo '/dev/gpt/rootfs / ufs rw 1 1' \ 1201e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 1214e7bc9f0SColin Percival if [ -z "${NOSWAP}" ]; then 1221e7c1f17SGlen Barber echo '/dev/gpt/swapfs none swap sw 0 0' \ 1231e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 1244e7bc9f0SColin Percival fi 1251e7c1f17SGlen Barber 126e6f3489cSGlen Barber mkdir -p ${DESTDIR}/dev 127e6f3489cSGlen Barber mount -t devfs devfs ${DESTDIR}/dev 1281e7c1f17SGlen Barber chroot ${DESTDIR} /usr/bin/newaliases 1291e7c1f17SGlen Barber chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart 13096341228SColin Percival umount_loop ${DESTDIR}/dev 1311e7c1f17SGlen Barber 13230a20c89SGlen Barber cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf 13330a20c89SGlen Barber 1341e7c1f17SGlen Barber return 0 1351e7c1f17SGlen Barber} 1361e7c1f17SGlen Barber 1371e7c1f17SGlen Barbervm_extra_install_base() { 1381e7c1f17SGlen Barber # Prototype. When overridden, runs extra post-installworld commands 1391e7c1f17SGlen Barber # as needed, based on the target virtual machine image or cloud 1401e7c1f17SGlen Barber # provider image target. 1411e7c1f17SGlen Barber 1421e7c1f17SGlen Barber return 0 1431e7c1f17SGlen Barber} 1441e7c1f17SGlen Barber 1451e7c1f17SGlen Barbervm_extra_enable_services() { 1461e7c1f17SGlen Barber if [ ! -z "${VM_RC_LIST}" ]; then 1471e7c1f17SGlen Barber for _rcvar in ${VM_RC_LIST}; do 1481e7c1f17SGlen Barber echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf 1491e7c1f17SGlen Barber done 1501e7c1f17SGlen Barber fi 1511e7c1f17SGlen Barber 1521e7c1f17SGlen Barber return 0 1531e7c1f17SGlen Barber} 1541e7c1f17SGlen Barber 1551e7c1f17SGlen Barbervm_extra_install_packages() { 156a4900ff5SGlen Barber if [ -z "${VM_EXTRA_PACKAGES}" ]; then 157a4900ff5SGlen Barber return 0 158a4900ff5SGlen Barber fi 15930a20c89SGlen Barber mkdir -p ${DESTDIR}/dev 16030a20c89SGlen Barber mount -t devfs devfs ${DESTDIR}/dev 16130a20c89SGlen Barber chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 16230a20c89SGlen Barber /usr/sbin/pkg bootstrap -y 16330a20c89SGlen Barber chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 16430a20c89SGlen Barber /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES} 16596341228SColin Percival umount_loop ${DESTDIR}/dev 1661e7c1f17SGlen Barber 1671e7c1f17SGlen Barber return 0 1681e7c1f17SGlen Barber} 1691e7c1f17SGlen Barber 1701e7c1f17SGlen Barbervm_extra_install_ports() { 1711e7c1f17SGlen Barber # Prototype. When overridden, installs additional ports within the 1721e7c1f17SGlen Barber # virtual machine environment. 1731e7c1f17SGlen Barber 1741e7c1f17SGlen Barber return 0 1751e7c1f17SGlen Barber} 1761e7c1f17SGlen Barber 177a02bfc83SGlen Barbervm_extra_pre_umount() { 178a02bfc83SGlen Barber # Prototype. When overridden, installs additional ports within the 179a02bfc83SGlen Barber # virtual machine environment. 180a02bfc83SGlen Barber 1812d4ff62fSGlen Barber rm -f ${DESTDIR}/etc/resolv.conf 1822d4ff62fSGlen Barber return 0 1832d4ff62fSGlen Barber} 1842d4ff62fSGlen Barber 1852d4ff62fSGlen Barbervm_extra_pkg_rmcache() { 186a4900ff5SGlen Barber if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then 187a4900ff5SGlen Barber chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 188a4900ff5SGlen Barber /usr/local/sbin/pkg clean -y -a 189a4900ff5SGlen Barber fi 19030a20c89SGlen Barber 191a02bfc83SGlen Barber return 0 192a02bfc83SGlen Barber} 193a02bfc83SGlen Barber 19496341228SColin Percivalumount_loop() { 19596341228SColin Percival DIR=$1 1961e7c1f17SGlen Barber i=0 1971e7c1f17SGlen Barber sync 19896341228SColin Percival while ! umount ${DIR}; do 1991e7c1f17SGlen Barber i=$(( $i + 1 )) 2001e7c1f17SGlen Barber if [ $i -ge 10 ]; then 2011e7c1f17SGlen Barber # This should never happen. But, it has happened. 20296341228SColin Percival echo "Cannot umount(8) ${DIR}" 20396341228SColin Percival echo "Something has gone horribly wrong." 20496341228SColin Percival return 1 2051e7c1f17SGlen Barber fi 2061e7c1f17SGlen Barber sleep 1 2071e7c1f17SGlen Barber done 2081e7c1f17SGlen Barber 2091e7c1f17SGlen Barber return 0 2101e7c1f17SGlen Barber} 2111e7c1f17SGlen Barber 2121e7c1f17SGlen Barbervm_create_disk() { 2131e7c1f17SGlen Barber echo "Creating image... Please wait." 2141e7c1f17SGlen Barber echo 2151e7c1f17SGlen Barber 216efeb11a7SGlen Barber write_partition_layout || return 1 2171e7c1f17SGlen Barber 2181e7c1f17SGlen Barber return 0 2191e7c1f17SGlen Barber} 2201e7c1f17SGlen Barber 2211e7c1f17SGlen Barbervm_extra_create_disk() { 2221e7c1f17SGlen Barber 2231e7c1f17SGlen Barber return 0 2241e7c1f17SGlen Barber} 2251e7c1f17SGlen Barber 226