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 17bf93ac78SGlen Barber _OBJDIR="$(make -C ${WORLDDIR} -V .OBJDIR)" 18bf93ac78SGlen Barber if [ -d "${_OBJDIR%%/usr/src}/${TARGET}.${TARGET_ARCH}" ]; then 19bf93ac78SGlen Barber BOOTFILES="/${_OBJDIR%%/usr/src}/${TARGET}.${TARGET_ARCH}/usr/src/sys/boot" 204b8175eeSGlen Barber else 21bf93ac78SGlen Barber BOOTFILES="/${_OBJDIR}/sys/boot" 224b8175eeSGlen Barber fi 234b8175eeSGlen Barber 24efeb11a7SGlen Barber case "${TARGET}:${TARGET_ARCH}" in 25efeb11a7SGlen Barber amd64:amd64 | i386:i386) 26*ccbf2502SGlen Barber mkimg -s gpt -f ${VMFORMAT} \ 27*ccbf2502SGlen Barber -b ${BOOTFILES}/i386/pmbr/pmbr \ 284b8175eeSGlen Barber -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot \ 294e7bc9f0SColin Percival ${SWAPOPT} \ 3038d59ac7SGlen Barber -p freebsd-ufs/rootfs:=${VMBASE} \ 31efeb11a7SGlen Barber -o ${VMIMAGE} 32efeb11a7SGlen Barber ;; 336a3afe69SGlen Barber arm64:aarch64) 34*ccbf2502SGlen Barber mkimg -s mbr -f ${VMFORMAT} \ 3508a75d1fSGlen Barber -p efi:=${BOOTFILES}/efi/boot1/boot1.efifat \ 3608a75d1fSGlen Barber -p freebsd:=${VMBASE} \ 376a3afe69SGlen Barber -o ${VMIMAGE} 386a3afe69SGlen Barber ;; 39efeb11a7SGlen Barber powerpc:powerpc*) 40*ccbf2502SGlen Barber mkimg -s apm -f ${VMFORMAT} \ 414b8175eeSGlen Barber -p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \ 424e7bc9f0SColin Percival ${SWAPOPT} \ 4338d59ac7SGlen Barber -p freebsd-ufs/rootfs:=${VMBASE} \ 44efeb11a7SGlen Barber -o ${VMIMAGE} 45efeb11a7SGlen Barber ;; 46efeb11a7SGlen Barber *) 47efeb11a7SGlen Barber # ENOTSUPP 48efeb11a7SGlen Barber return 1 49efeb11a7SGlen Barber ;; 50efeb11a7SGlen Barber esac 51efeb11a7SGlen Barber 52efeb11a7SGlen Barber return 0 53efeb11a7SGlen Barber} 541e7c1f17SGlen Barber 551e7c1f17SGlen Barbererr() { 561e7c1f17SGlen Barber printf "${@}\n" 571e7c1f17SGlen Barber cleanup 581e7c1f17SGlen Barber return 1 591e7c1f17SGlen Barber} 601e7c1f17SGlen Barber 611e7c1f17SGlen Barbercleanup() { 62dcdebabdSColin Percival if [ -c "${DESTDIR}/dev/null" ]; then 6396341228SColin Percival umount_loop ${DESTDIR}/dev 2>/dev/null 6496341228SColin Percival fi 6596341228SColin Percival umount_loop ${DESTDIR} 661e7c1f17SGlen Barber if [ ! -z "${mddev}" ]; then 671e7c1f17SGlen Barber mdconfig -d -u ${mddev} 681e7c1f17SGlen Barber fi 691e7c1f17SGlen Barber 701e7c1f17SGlen Barber return 0 711e7c1f17SGlen Barber} 721e7c1f17SGlen Barber 731e7c1f17SGlen Barbervm_create_base() { 741e7c1f17SGlen Barber # Creates the UFS root filesystem for the virtual machine disk, 751e7c1f17SGlen Barber # written to the formatted disk image with mkimg(1). 761e7c1f17SGlen Barber 771e7c1f17SGlen Barber mkdir -p ${DESTDIR} 781e7c1f17SGlen Barber truncate -s ${VMSIZE} ${VMBASE} 791e7c1f17SGlen Barber mddev=$(mdconfig -f ${VMBASE}) 8008a75d1fSGlen Barber newfs -L rootfs /dev/${mddev} 811e7c1f17SGlen Barber mount /dev/${mddev} ${DESTDIR} 821e7c1f17SGlen Barber 831e7c1f17SGlen Barber return 0 841e7c1f17SGlen Barber} 851e7c1f17SGlen Barber 8625c11557SColin Percivalvm_copy_base() { 8725c11557SColin Percival # Creates a new UFS root filesystem and copies the contents of the 8825c11557SColin Percival # current root filesystem into it. This produces a "clean" disk 8925c11557SColin Percival # image without any remnants of files which were created temporarily 9025c11557SColin Percival # during image-creation and have since been deleted (e.g., downloaded 9125c11557SColin Percival # package archives). 9225c11557SColin Percival 9325c11557SColin Percival mkdir -p ${DESTDIR}/old 9425c11557SColin Percival mdold=$(mdconfig -f ${VMBASE}) 9525c11557SColin Percival mount /dev/${mdold} ${DESTDIR}/old 9625c11557SColin Percival 9725c11557SColin Percival truncate -s ${VMSIZE} ${VMBASE}.tmp 9825c11557SColin Percival mkdir -p ${DESTDIR}/new 9925c11557SColin Percival mdnew=$(mdconfig -f ${VMBASE}.tmp) 10008a75d1fSGlen Barber newfs -L rootfs /dev/${mdnew} 10125c11557SColin Percival mount /dev/${mdnew} ${DESTDIR}/new 10225c11557SColin Percival 1034b8175eeSGlen Barber tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new 10425c11557SColin Percival 10596341228SColin Percival umount_loop /dev/${mdold} 10625c11557SColin Percival rmdir ${DESTDIR}/old 10725c11557SColin Percival mdconfig -d -u ${mdold} 10825c11557SColin Percival 10996341228SColin Percival umount_loop /dev/${mdnew} 11025c11557SColin Percival rmdir ${DESTDIR}/new 1114b8175eeSGlen Barber tunefs -j enable /dev/${mdnew} 11225c11557SColin Percival mdconfig -d -u ${mdnew} 11325c11557SColin Percival mv ${VMBASE}.tmp ${VMBASE} 11425c11557SColin Percival} 11525c11557SColin Percival 1161e7c1f17SGlen Barbervm_install_base() { 1171e7c1f17SGlen Barber # Installs the FreeBSD userland/kernel to the virtual machine disk. 1181e7c1f17SGlen Barber 1191e7c1f17SGlen Barber cd ${WORLDDIR} && \ 1201e7c1f17SGlen Barber make DESTDIR=${DESTDIR} \ 1211e7c1f17SGlen Barber installworld installkernel distribution || \ 1221e7c1f17SGlen Barber err "\n\nCannot install the base system to ${DESTDIR}." 1231e7c1f17SGlen Barber 1241e7c1f17SGlen Barber echo '# Custom /etc/fstab for FreeBSD VM images' \ 1251e7c1f17SGlen Barber > ${DESTDIR}/etc/fstab 12608a75d1fSGlen Barber echo "/dev/${ROOTLABEL}/rootfs / ufs rw 1 1" \ 1271e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 1284e7bc9f0SColin Percival if [ -z "${NOSWAP}" ]; then 1291e7c1f17SGlen Barber echo '/dev/gpt/swapfs none swap sw 0 0' \ 1301e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 1314e7bc9f0SColin Percival fi 1321e7c1f17SGlen Barber 133e6f3489cSGlen Barber mkdir -p ${DESTDIR}/dev 134e6f3489cSGlen Barber mount -t devfs devfs ${DESTDIR}/dev 1351e7c1f17SGlen Barber chroot ${DESTDIR} /usr/bin/newaliases 1361e7c1f17SGlen Barber chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart 13796341228SColin Percival umount_loop ${DESTDIR}/dev 1381e7c1f17SGlen Barber 13930a20c89SGlen Barber cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf 14030a20c89SGlen Barber 1411e7c1f17SGlen Barber return 0 1421e7c1f17SGlen Barber} 1431e7c1f17SGlen Barber 1441e7c1f17SGlen Barbervm_extra_install_base() { 1451e7c1f17SGlen Barber # Prototype. When overridden, runs extra post-installworld commands 1461e7c1f17SGlen Barber # as needed, based on the target virtual machine image or cloud 1471e7c1f17SGlen Barber # provider image target. 1481e7c1f17SGlen Barber 1491e7c1f17SGlen Barber return 0 1501e7c1f17SGlen Barber} 1511e7c1f17SGlen Barber 1521e7c1f17SGlen Barbervm_extra_enable_services() { 1531e7c1f17SGlen Barber if [ ! -z "${VM_RC_LIST}" ]; then 1541e7c1f17SGlen Barber for _rcvar in ${VM_RC_LIST}; do 1551e7c1f17SGlen Barber echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf 1561e7c1f17SGlen Barber done 1571e7c1f17SGlen Barber fi 1581e7c1f17SGlen Barber 1591e7c1f17SGlen Barber return 0 1601e7c1f17SGlen Barber} 1611e7c1f17SGlen Barber 1621e7c1f17SGlen Barbervm_extra_install_packages() { 163a4900ff5SGlen Barber if [ -z "${VM_EXTRA_PACKAGES}" ]; then 164a4900ff5SGlen Barber return 0 165a4900ff5SGlen Barber fi 16630a20c89SGlen Barber mkdir -p ${DESTDIR}/dev 16730a20c89SGlen Barber mount -t devfs devfs ${DESTDIR}/dev 16830a20c89SGlen Barber chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 16930a20c89SGlen Barber /usr/sbin/pkg bootstrap -y 17030a20c89SGlen Barber chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 17130a20c89SGlen Barber /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES} 17296341228SColin Percival umount_loop ${DESTDIR}/dev 1731e7c1f17SGlen Barber 1741e7c1f17SGlen Barber return 0 1751e7c1f17SGlen Barber} 1761e7c1f17SGlen Barber 1771e7c1f17SGlen Barbervm_extra_install_ports() { 1781e7c1f17SGlen Barber # Prototype. When overridden, installs additional ports within the 1791e7c1f17SGlen Barber # virtual machine environment. 1801e7c1f17SGlen Barber 1811e7c1f17SGlen Barber return 0 1821e7c1f17SGlen Barber} 1831e7c1f17SGlen Barber 184a02bfc83SGlen Barbervm_extra_pre_umount() { 18526e4122aSGlen Barber # Prototype. When overridden, performs additional tasks within the 18626e4122aSGlen Barber # virtual machine environment prior to unmounting the filesystem. 18726e4122aSGlen Barber # Note: When overriding this function, removing resolv.conf in the 18826e4122aSGlen Barber # disk image must be included. 189a02bfc83SGlen Barber 1902d4ff62fSGlen Barber rm -f ${DESTDIR}/etc/resolv.conf 1912d4ff62fSGlen Barber return 0 1922d4ff62fSGlen Barber} 1932d4ff62fSGlen Barber 1942d4ff62fSGlen Barbervm_extra_pkg_rmcache() { 195a4900ff5SGlen Barber if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then 196a4900ff5SGlen Barber chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 197a4900ff5SGlen Barber /usr/local/sbin/pkg clean -y -a 198a4900ff5SGlen Barber fi 19930a20c89SGlen Barber 200a02bfc83SGlen Barber return 0 201a02bfc83SGlen Barber} 202a02bfc83SGlen Barber 20396341228SColin Percivalumount_loop() { 20496341228SColin Percival DIR=$1 2051e7c1f17SGlen Barber i=0 2061e7c1f17SGlen Barber sync 20796341228SColin Percival while ! umount ${DIR}; do 2081e7c1f17SGlen Barber i=$(( $i + 1 )) 2091e7c1f17SGlen Barber if [ $i -ge 10 ]; then 2101e7c1f17SGlen Barber # This should never happen. But, it has happened. 21196341228SColin Percival echo "Cannot umount(8) ${DIR}" 21296341228SColin Percival echo "Something has gone horribly wrong." 21396341228SColin Percival return 1 2141e7c1f17SGlen Barber fi 2151e7c1f17SGlen Barber sleep 1 2161e7c1f17SGlen Barber done 2171e7c1f17SGlen Barber 2181e7c1f17SGlen Barber return 0 2191e7c1f17SGlen Barber} 2201e7c1f17SGlen Barber 2211e7c1f17SGlen Barbervm_create_disk() { 2221e7c1f17SGlen Barber echo "Creating image... Please wait." 2231e7c1f17SGlen Barber echo 2241e7c1f17SGlen Barber 225efeb11a7SGlen Barber write_partition_layout || return 1 2261e7c1f17SGlen Barber 2271e7c1f17SGlen Barber return 0 2281e7c1f17SGlen Barber} 2291e7c1f17SGlen Barber 2301e7c1f17SGlen Barbervm_extra_create_disk() { 2311e7c1f17SGlen Barber 2321e7c1f17SGlen Barber return 0 2331e7c1f17SGlen Barber} 2341e7c1f17SGlen Barber 235