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 9db8b5613SRebecca Cranscriptdir=$(dirname $(realpath $0)) 10db8b5613SRebecca Cran. ${scriptdir}/../../tools/boot/install-boot.sh 11db8b5613SRebecca Cran 121e7c1f17SGlen Barberexport PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin" 131e7c1f17SGlen Barbertrap "cleanup" INT QUIT TRAP ABRT TERM 141e7c1f17SGlen Barber 15efeb11a7SGlen Barberwrite_partition_layout() { 164e7bc9f0SColin Percival if [ -z "${NOSWAP}" ]; then 17eb46c987SNavdeep Parhar SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}" 184e7bc9f0SColin Percival fi 19efeb11a7SGlen Barber 20dfa09989SBryan Drewery BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \ 21dfa09989SBryan Drewery WITH_UNIFIED_OBJDIR=yes \ 22ca987d46SWarner Losh make -C ${WORLDDIR}/stand -V .OBJDIR)" 23dfa09989SBryan Drewery BOOTFILES="$(realpath ${BOOTFILES})" 244b8175eeSGlen Barber 25efeb11a7SGlen Barber case "${TARGET}:${TARGET_ARCH}" in 26efeb11a7SGlen Barber amd64:amd64 | i386:i386) 27ccbf2502SGlen Barber mkimg -s gpt -f ${VMFORMAT} \ 28ccbf2502SGlen Barber -b ${BOOTFILES}/i386/pmbr/pmbr \ 294b8175eeSGlen Barber -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot \ 304e7bc9f0SColin Percival ${SWAPOPT} \ 3138d59ac7SGlen Barber -p freebsd-ufs/rootfs:=${VMBASE} \ 32efeb11a7SGlen Barber -o ${VMIMAGE} 33efeb11a7SGlen Barber ;; 346a3afe69SGlen Barber arm64:aarch64) 35db8b5613SRebecca Cran # Create an ESP 36db8b5613SRebecca Cran espfilename=$(mktemp /tmp/efiboot.XXXXXX) 37db8b5613SRebecca Cran make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi 38ccbf2502SGlen Barber mkimg -s mbr -f ${VMFORMAT} \ 39db8b5613SRebecca Cran -p efi:=${espfilename} \ 4008a75d1fSGlen Barber -p freebsd:=${VMBASE} \ 416a3afe69SGlen Barber -o ${VMIMAGE} 42db8b5613SRebecca Cran rm ${espfilename} 436a3afe69SGlen Barber ;; 44efeb11a7SGlen Barber powerpc:powerpc*) 45ccbf2502SGlen Barber mkimg -s apm -f ${VMFORMAT} \ 464b8175eeSGlen Barber -p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \ 474e7bc9f0SColin Percival ${SWAPOPT} \ 4838d59ac7SGlen Barber -p freebsd-ufs/rootfs:=${VMBASE} \ 49efeb11a7SGlen Barber -o ${VMIMAGE} 50efeb11a7SGlen Barber ;; 51efeb11a7SGlen Barber *) 52efeb11a7SGlen Barber # ENOTSUPP 53efeb11a7SGlen Barber return 1 54efeb11a7SGlen Barber ;; 55efeb11a7SGlen Barber esac 56efeb11a7SGlen Barber 57efeb11a7SGlen Barber return 0 58efeb11a7SGlen Barber} 591e7c1f17SGlen Barber 601e7c1f17SGlen Barbererr() { 611e7c1f17SGlen Barber printf "${@}\n" 621e7c1f17SGlen Barber cleanup 631e7c1f17SGlen Barber return 1 641e7c1f17SGlen Barber} 651e7c1f17SGlen Barber 661e7c1f17SGlen Barbercleanup() { 67dcdebabdSColin Percival if [ -c "${DESTDIR}/dev/null" ]; then 6896341228SColin Percival umount_loop ${DESTDIR}/dev 2>/dev/null 6996341228SColin Percival fi 7096341228SColin Percival umount_loop ${DESTDIR} 711e7c1f17SGlen Barber if [ ! -z "${mddev}" ]; then 721e7c1f17SGlen Barber mdconfig -d -u ${mddev} 731e7c1f17SGlen Barber fi 741e7c1f17SGlen Barber 751e7c1f17SGlen Barber return 0 761e7c1f17SGlen Barber} 771e7c1f17SGlen Barber 781e7c1f17SGlen Barbervm_create_base() { 791e7c1f17SGlen Barber # Creates the UFS root filesystem for the virtual machine disk, 801e7c1f17SGlen Barber # written to the formatted disk image with mkimg(1). 811e7c1f17SGlen Barber 821e7c1f17SGlen Barber mkdir -p ${DESTDIR} 831e7c1f17SGlen Barber truncate -s ${VMSIZE} ${VMBASE} 841e7c1f17SGlen Barber mddev=$(mdconfig -f ${VMBASE}) 8508a75d1fSGlen Barber newfs -L rootfs /dev/${mddev} 861e7c1f17SGlen Barber mount /dev/${mddev} ${DESTDIR} 871e7c1f17SGlen Barber 881e7c1f17SGlen Barber return 0 891e7c1f17SGlen Barber} 901e7c1f17SGlen Barber 9125c11557SColin Percivalvm_copy_base() { 9225c11557SColin Percival # Creates a new UFS root filesystem and copies the contents of the 9325c11557SColin Percival # current root filesystem into it. This produces a "clean" disk 9425c11557SColin Percival # image without any remnants of files which were created temporarily 9525c11557SColin Percival # during image-creation and have since been deleted (e.g., downloaded 9625c11557SColin Percival # package archives). 9725c11557SColin Percival 9825c11557SColin Percival mkdir -p ${DESTDIR}/old 9925c11557SColin Percival mdold=$(mdconfig -f ${VMBASE}) 10025c11557SColin Percival mount /dev/${mdold} ${DESTDIR}/old 10125c11557SColin Percival 10225c11557SColin Percival truncate -s ${VMSIZE} ${VMBASE}.tmp 10325c11557SColin Percival mkdir -p ${DESTDIR}/new 10425c11557SColin Percival mdnew=$(mdconfig -f ${VMBASE}.tmp) 10508a75d1fSGlen Barber newfs -L rootfs /dev/${mdnew} 10625c11557SColin Percival mount /dev/${mdnew} ${DESTDIR}/new 10725c11557SColin Percival 1084b8175eeSGlen Barber tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new 10925c11557SColin Percival 11096341228SColin Percival umount_loop /dev/${mdold} 11125c11557SColin Percival rmdir ${DESTDIR}/old 11225c11557SColin Percival mdconfig -d -u ${mdold} 11325c11557SColin Percival 11496341228SColin Percival umount_loop /dev/${mdnew} 11525c11557SColin Percival rmdir ${DESTDIR}/new 11691c42fffSGlen Barber tunefs -n enable /dev/${mdnew} 11725c11557SColin Percival mdconfig -d -u ${mdnew} 11825c11557SColin Percival mv ${VMBASE}.tmp ${VMBASE} 11925c11557SColin Percival} 12025c11557SColin Percival 1211e7c1f17SGlen Barbervm_install_base() { 1221e7c1f17SGlen Barber # Installs the FreeBSD userland/kernel to the virtual machine disk. 1231e7c1f17SGlen Barber 1241e7c1f17SGlen Barber cd ${WORLDDIR} && \ 1251e7c1f17SGlen Barber make DESTDIR=${DESTDIR} \ 1261e7c1f17SGlen Barber installworld installkernel distribution || \ 1271e7c1f17SGlen Barber err "\n\nCannot install the base system to ${DESTDIR}." 1281e7c1f17SGlen Barber 129c52962c6SGlen Barber # Bootstrap etcupdate(8) and mergemaster(8) databases. 130c52962c6SGlen Barber mkdir -p ${DESTDIR}/var/db/etcupdate 131c52962c6SGlen Barber etcupdate extract -B \ 132c52962c6SGlen Barber -M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \ 133c52962c6SGlen Barber -s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate 134c52962c6SGlen Barber sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \ 135c52962c6SGlen Barber -F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \ 136c52962c6SGlen Barber -D ${DESTDIR} 137c52962c6SGlen Barber 1381e7c1f17SGlen Barber echo '# Custom /etc/fstab for FreeBSD VM images' \ 1391e7c1f17SGlen Barber > ${DESTDIR}/etc/fstab 14008a75d1fSGlen Barber echo "/dev/${ROOTLABEL}/rootfs / ufs rw 1 1" \ 1411e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 1424e7bc9f0SColin Percival if [ -z "${NOSWAP}" ]; then 1431e7c1f17SGlen Barber echo '/dev/gpt/swapfs none swap sw 0 0' \ 1441e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 1454e7bc9f0SColin Percival fi 1461e7c1f17SGlen Barber 1472170fff8SGlen Barber local hostname 1482170fff8SGlen Barber hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')" 1492170fff8SGlen Barber echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf 1502170fff8SGlen Barber 1511ecaa3f7SColin Percival if ! [ -z "${QEMUSTATIC}" ]; then 1521ecaa3f7SColin Percival export EMULATOR=/qemu 1531ecaa3f7SColin Percival cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR} 1541ecaa3f7SColin Percival fi 1551ecaa3f7SColin Percival 156e6f3489cSGlen Barber mkdir -p ${DESTDIR}/dev 157e6f3489cSGlen Barber mount -t devfs devfs ${DESTDIR}/dev 1581ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} /usr/bin/newaliases 1591ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart 16096341228SColin Percival umount_loop ${DESTDIR}/dev 1611e7c1f17SGlen Barber 16230a20c89SGlen Barber cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf 16330a20c89SGlen Barber 1641e7c1f17SGlen Barber return 0 1651e7c1f17SGlen Barber} 1661e7c1f17SGlen Barber 1671e7c1f17SGlen Barbervm_extra_install_base() { 1681e7c1f17SGlen Barber # Prototype. When overridden, runs extra post-installworld commands 1691e7c1f17SGlen Barber # as needed, based on the target virtual machine image or cloud 1701e7c1f17SGlen Barber # provider image target. 1711e7c1f17SGlen Barber 1721e7c1f17SGlen Barber return 0 1731e7c1f17SGlen Barber} 1741e7c1f17SGlen Barber 1751e7c1f17SGlen Barbervm_extra_enable_services() { 1761e7c1f17SGlen Barber if [ ! -z "${VM_RC_LIST}" ]; then 1771e7c1f17SGlen Barber for _rcvar in ${VM_RC_LIST}; do 1781e7c1f17SGlen Barber echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf 1791e7c1f17SGlen Barber done 1801e7c1f17SGlen Barber fi 1811e7c1f17SGlen Barber 182f8e26d73SGlen Barber if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then 183f8e26d73SGlen Barber echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \ 184f8e26d73SGlen Barber ${DESTDIR}/etc/rc.conf 185*cbc52907SGlen Barber # Expand the filesystem to fill the disk. 186*cbc52907SGlen Barber echo 'growfs_enable="YES"' >> ${DESTDIR}/etc/rc.conf 187*cbc52907SGlen Barber touch ${DESTDIR}/firstboot 188f8e26d73SGlen Barber fi 189f8e26d73SGlen Barber 1901e7c1f17SGlen Barber return 0 1911e7c1f17SGlen Barber} 1921e7c1f17SGlen Barber 1931e7c1f17SGlen Barbervm_extra_install_packages() { 194a4900ff5SGlen Barber if [ -z "${VM_EXTRA_PACKAGES}" ]; then 195a4900ff5SGlen Barber return 0 196a4900ff5SGlen Barber fi 19730a20c89SGlen Barber mkdir -p ${DESTDIR}/dev 19830a20c89SGlen Barber mount -t devfs devfs ${DESTDIR}/dev 1991ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 20030a20c89SGlen Barber /usr/sbin/pkg bootstrap -y 2011ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 20230a20c89SGlen Barber /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES} 20396341228SColin Percival umount_loop ${DESTDIR}/dev 2041e7c1f17SGlen Barber 2051e7c1f17SGlen Barber return 0 2061e7c1f17SGlen Barber} 2071e7c1f17SGlen Barber 2081e7c1f17SGlen Barbervm_extra_install_ports() { 2091e7c1f17SGlen Barber # Prototype. When overridden, installs additional ports within the 2101e7c1f17SGlen Barber # virtual machine environment. 2111e7c1f17SGlen Barber 2121e7c1f17SGlen Barber return 0 2131e7c1f17SGlen Barber} 2141e7c1f17SGlen Barber 215a02bfc83SGlen Barbervm_extra_pre_umount() { 21626e4122aSGlen Barber # Prototype. When overridden, performs additional tasks within the 21726e4122aSGlen Barber # virtual machine environment prior to unmounting the filesystem. 21826e4122aSGlen Barber # Note: When overriding this function, removing resolv.conf in the 21926e4122aSGlen Barber # disk image must be included. 220a02bfc83SGlen Barber 2211ecaa3f7SColin Percival if ! [ -z "${QEMUSTATIC}" ]; then 2221ecaa3f7SColin Percival rm -f ${DESTDIR}/${EMULATOR} 2231ecaa3f7SColin Percival fi 2242d4ff62fSGlen Barber rm -f ${DESTDIR}/etc/resolv.conf 2252d4ff62fSGlen Barber return 0 2262d4ff62fSGlen Barber} 2272d4ff62fSGlen Barber 2282d4ff62fSGlen Barbervm_extra_pkg_rmcache() { 229a4900ff5SGlen Barber if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then 2301ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 231a4900ff5SGlen Barber /usr/local/sbin/pkg clean -y -a 232a4900ff5SGlen Barber fi 23330a20c89SGlen Barber 234a02bfc83SGlen Barber return 0 235a02bfc83SGlen Barber} 236a02bfc83SGlen Barber 23796341228SColin Percivalumount_loop() { 23896341228SColin Percival DIR=$1 2391e7c1f17SGlen Barber i=0 2401e7c1f17SGlen Barber sync 24196341228SColin Percival while ! umount ${DIR}; do 2421e7c1f17SGlen Barber i=$(( $i + 1 )) 2431e7c1f17SGlen Barber if [ $i -ge 10 ]; then 2441e7c1f17SGlen Barber # This should never happen. But, it has happened. 24596341228SColin Percival echo "Cannot umount(8) ${DIR}" 24696341228SColin Percival echo "Something has gone horribly wrong." 24796341228SColin Percival return 1 2481e7c1f17SGlen Barber fi 2491e7c1f17SGlen Barber sleep 1 2501e7c1f17SGlen Barber done 2511e7c1f17SGlen Barber 2521e7c1f17SGlen Barber return 0 2531e7c1f17SGlen Barber} 2541e7c1f17SGlen Barber 2551e7c1f17SGlen Barbervm_create_disk() { 2561e7c1f17SGlen Barber echo "Creating image... Please wait." 2571e7c1f17SGlen Barber echo 2581e7c1f17SGlen Barber 259efeb11a7SGlen Barber write_partition_layout || return 1 2601e7c1f17SGlen Barber 2611e7c1f17SGlen Barber return 0 2621e7c1f17SGlen Barber} 2631e7c1f17SGlen Barber 2641e7c1f17SGlen Barbervm_extra_create_disk() { 2651e7c1f17SGlen Barber 2661e7c1f17SGlen Barber return 0 2671e7c1f17SGlen Barber} 2681e7c1f17SGlen Barber 269