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 151ca8842fSNathan Whitehorn# Platform-specific large-scale setup 161ca8842fSNathan Whitehorn# Most platforms use GPT, so put that as default, then special cases 171ca8842fSNathan WhitehornPARTSCHEME=gpt 181ca8842fSNathan WhitehornROOTLABEL="gpt" 19efeb11a7SGlen Barbercase "${TARGET}:${TARGET_ARCH}" in 20efeb11a7SGlen Barber powerpc:powerpc*) 211ca8842fSNathan Whitehorn PARTSCHEME=mbr 221ca8842fSNathan Whitehorn ROOTLABEL="ufs" 231ca8842fSNathan Whitehorn NOSWAP=yes # Can't label swap partition with MBR, so no swap 24efeb11a7SGlen Barber ;; 25efeb11a7SGlen Barberesac 26efeb11a7SGlen Barber 271e7c1f17SGlen Barbererr() { 281e7c1f17SGlen Barber printf "${@}\n" 291e7c1f17SGlen Barber cleanup 301e7c1f17SGlen Barber return 1 311e7c1f17SGlen Barber} 321e7c1f17SGlen Barber 331e7c1f17SGlen Barbercleanup() { 34dcdebabdSColin Percival if [ -c "${DESTDIR}/dev/null" ]; then 3596341228SColin Percival umount_loop ${DESTDIR}/dev 2>/dev/null 3696341228SColin Percival fi 371e7c1f17SGlen Barber 381e7c1f17SGlen Barber return 0 391e7c1f17SGlen Barber} 401e7c1f17SGlen Barber 411e7c1f17SGlen Barbervm_create_base() { 421e7c1f17SGlen Barber # Creates the UFS root filesystem for the virtual machine disk, 431e7c1f17SGlen Barber # written to the formatted disk image with mkimg(1). 441e7c1f17SGlen Barber 451e7c1f17SGlen Barber mkdir -p ${DESTDIR} 461e7c1f17SGlen Barber 471e7c1f17SGlen Barber return 0 481e7c1f17SGlen Barber} 491e7c1f17SGlen Barber 5025c11557SColin Percivalvm_copy_base() { 511ca8842fSNathan Whitehorn # Defunct 5225c11557SColin Percival} 5325c11557SColin Percival 541e7c1f17SGlen Barbervm_install_base() { 551e7c1f17SGlen Barber # Installs the FreeBSD userland/kernel to the virtual machine disk. 561e7c1f17SGlen Barber 571e7c1f17SGlen Barber cd ${WORLDDIR} && \ 581e7c1f17SGlen Barber make DESTDIR=${DESTDIR} \ 591e7c1f17SGlen Barber installworld installkernel distribution || \ 601e7c1f17SGlen Barber err "\n\nCannot install the base system to ${DESTDIR}." 611e7c1f17SGlen Barber 62c52962c6SGlen Barber # Bootstrap etcupdate(8) and mergemaster(8) databases. 63c52962c6SGlen Barber mkdir -p ${DESTDIR}/var/db/etcupdate 64c52962c6SGlen Barber etcupdate extract -B \ 65c52962c6SGlen Barber -M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \ 66c52962c6SGlen Barber -s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate 67c52962c6SGlen Barber sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \ 68c52962c6SGlen Barber -F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \ 69c52962c6SGlen Barber -D ${DESTDIR} 70c52962c6SGlen Barber 711e7c1f17SGlen Barber echo '# Custom /etc/fstab for FreeBSD VM images' \ 721e7c1f17SGlen Barber > ${DESTDIR}/etc/fstab 7308a75d1fSGlen Barber echo "/dev/${ROOTLABEL}/rootfs / ufs rw 1 1" \ 741e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 754e7bc9f0SColin Percival if [ -z "${NOSWAP}" ]; then 761e7c1f17SGlen Barber echo '/dev/gpt/swapfs none swap sw 0 0' \ 771e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 784e7bc9f0SColin Percival fi 791e7c1f17SGlen Barber 802170fff8SGlen Barber local hostname 812170fff8SGlen Barber hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')" 822170fff8SGlen Barber echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf 832170fff8SGlen Barber 841ecaa3f7SColin Percival if ! [ -z "${QEMUSTATIC}" ]; then 851ecaa3f7SColin Percival export EMULATOR=/qemu 861ecaa3f7SColin Percival cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR} 871ecaa3f7SColin Percival fi 881ecaa3f7SColin Percival 89e6f3489cSGlen Barber mkdir -p ${DESTDIR}/dev 90e6f3489cSGlen Barber mount -t devfs devfs ${DESTDIR}/dev 911ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} /usr/bin/newaliases 921ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart 9396341228SColin Percival umount_loop ${DESTDIR}/dev 941e7c1f17SGlen Barber 9530a20c89SGlen Barber cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf 9630a20c89SGlen Barber 971e7c1f17SGlen Barber return 0 981e7c1f17SGlen Barber} 991e7c1f17SGlen Barber 1001e7c1f17SGlen Barbervm_extra_install_base() { 1011e7c1f17SGlen Barber # Prototype. When overridden, runs extra post-installworld commands 1021e7c1f17SGlen Barber # as needed, based on the target virtual machine image or cloud 1031e7c1f17SGlen Barber # provider image target. 1041e7c1f17SGlen Barber 1051e7c1f17SGlen Barber return 0 1061e7c1f17SGlen Barber} 1071e7c1f17SGlen Barber 1081e7c1f17SGlen Barbervm_extra_enable_services() { 1091e7c1f17SGlen Barber if [ ! -z "${VM_RC_LIST}" ]; then 1101e7c1f17SGlen Barber for _rcvar in ${VM_RC_LIST}; do 1111e7c1f17SGlen Barber echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf 1121e7c1f17SGlen Barber done 1131e7c1f17SGlen Barber fi 1141e7c1f17SGlen Barber 115f8e26d73SGlen Barber if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then 116f8e26d73SGlen Barber echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \ 117f8e26d73SGlen Barber ${DESTDIR}/etc/rc.conf 118cbc52907SGlen Barber # Expand the filesystem to fill the disk. 119cbc52907SGlen Barber echo 'growfs_enable="YES"' >> ${DESTDIR}/etc/rc.conf 120cbc52907SGlen Barber touch ${DESTDIR}/firstboot 121f8e26d73SGlen Barber fi 122f8e26d73SGlen Barber 1231e7c1f17SGlen Barber return 0 1241e7c1f17SGlen Barber} 1251e7c1f17SGlen Barber 1261e7c1f17SGlen Barbervm_extra_install_packages() { 127a4900ff5SGlen Barber if [ -z "${VM_EXTRA_PACKAGES}" ]; then 128a4900ff5SGlen Barber return 0 129a4900ff5SGlen Barber fi 13030a20c89SGlen Barber mkdir -p ${DESTDIR}/dev 13130a20c89SGlen Barber mount -t devfs devfs ${DESTDIR}/dev 1321ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 13330a20c89SGlen Barber /usr/sbin/pkg bootstrap -y 134*be2bc82fSBrad Davis for p in ${VM_EXTRA_PACKAGES}; do 1351ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 136fd17ea8cSBrad Davis /usr/sbin/pkg install -y ${p} 137fd17ea8cSBrad Davis done 13896341228SColin Percival umount_loop ${DESTDIR}/dev 1391e7c1f17SGlen Barber 1401e7c1f17SGlen Barber return 0 1411e7c1f17SGlen Barber} 1421e7c1f17SGlen Barber 1431e7c1f17SGlen Barbervm_extra_install_ports() { 1441e7c1f17SGlen Barber # Prototype. When overridden, installs additional ports within the 1451e7c1f17SGlen Barber # virtual machine environment. 1461e7c1f17SGlen Barber 1471e7c1f17SGlen Barber return 0 1481e7c1f17SGlen Barber} 1491e7c1f17SGlen Barber 150a02bfc83SGlen Barbervm_extra_pre_umount() { 15126e4122aSGlen Barber # Prototype. When overridden, performs additional tasks within the 15226e4122aSGlen Barber # virtual machine environment prior to unmounting the filesystem. 15326e4122aSGlen Barber # Note: When overriding this function, removing resolv.conf in the 15426e4122aSGlen Barber # disk image must be included. 155a02bfc83SGlen Barber 1561ecaa3f7SColin Percival if ! [ -z "${QEMUSTATIC}" ]; then 1571ecaa3f7SColin Percival rm -f ${DESTDIR}/${EMULATOR} 1581ecaa3f7SColin Percival fi 1592d4ff62fSGlen Barber rm -f ${DESTDIR}/etc/resolv.conf 1602d4ff62fSGlen Barber return 0 1612d4ff62fSGlen Barber} 1622d4ff62fSGlen Barber 1632d4ff62fSGlen Barbervm_extra_pkg_rmcache() { 164a4900ff5SGlen Barber if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then 1651ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 166a4900ff5SGlen Barber /usr/local/sbin/pkg clean -y -a 167a4900ff5SGlen Barber fi 16830a20c89SGlen Barber 169a02bfc83SGlen Barber return 0 170a02bfc83SGlen Barber} 171a02bfc83SGlen Barber 17296341228SColin Percivalumount_loop() { 17396341228SColin Percival DIR=$1 1741e7c1f17SGlen Barber i=0 1751e7c1f17SGlen Barber sync 17696341228SColin Percival while ! umount ${DIR}; do 1771e7c1f17SGlen Barber i=$(( $i + 1 )) 1781e7c1f17SGlen Barber if [ $i -ge 10 ]; then 1791e7c1f17SGlen Barber # This should never happen. But, it has happened. 18096341228SColin Percival echo "Cannot umount(8) ${DIR}" 18196341228SColin Percival echo "Something has gone horribly wrong." 18296341228SColin Percival return 1 1831e7c1f17SGlen Barber fi 1841e7c1f17SGlen Barber sleep 1 1851e7c1f17SGlen Barber done 1861e7c1f17SGlen Barber 1871e7c1f17SGlen Barber return 0 1881e7c1f17SGlen Barber} 1891e7c1f17SGlen Barber 1901e7c1f17SGlen Barbervm_create_disk() { 1911e7c1f17SGlen Barber echo "Creating image... Please wait." 1921e7c1f17SGlen Barber echo 1931e7c1f17SGlen Barber 1941ca8842fSNathan Whitehorn if [ -z "${NOSWAP}" ]; then 1951ca8842fSNathan Whitehorn SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}" 1961ca8842fSNathan Whitehorn fi 1971ca8842fSNathan Whitehorn 1981ca8842fSNathan Whitehorn BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \ 1991ca8842fSNathan Whitehorn WITH_UNIFIED_OBJDIR=yes \ 2001ca8842fSNathan Whitehorn make -C ${WORLDDIR}/stand -V .OBJDIR)" 2011ca8842fSNathan Whitehorn BOOTFILES="$(realpath ${BOOTFILES})" 2021ca8842fSNathan Whitehorn 2031ca8842fSNathan Whitehorn case "${TARGET}:${TARGET_ARCH}" in 2041ca8842fSNathan Whitehorn amd64:amd64 | i386:i386) 2051ca8842fSNathan Whitehorn ESP=yes 2061ca8842fSNathan Whitehorn BOOTPARTS="-b ${BOOTFILES}/i386/pmbr/pmbr \ 2071ca8842fSNathan Whitehorn -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot" 2081ca8842fSNathan Whitehorn ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}" 2091ca8842fSNathan Whitehorn MAKEFSARGS="-B little" 2101ca8842fSNathan Whitehorn ;; 2111ca8842fSNathan Whitehorn arm64:aarch64 | riscv:riscv64*) 2121ca8842fSNathan Whitehorn ESP=yes 2131ca8842fSNathan Whitehorn BOOTPARTS= 2141ca8842fSNathan Whitehorn ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}" 2151ca8842fSNathan Whitehorn MAKEFSARGS="-B little" 2161ca8842fSNathan Whitehorn ;; 2171ca8842fSNathan Whitehorn powerpc:powerpc*) 2181ca8842fSNathan Whitehorn ESP=no 2191ca8842fSNathan Whitehorn BOOTPARTS="-p prepboot:=${BOOTFILES}/powerpc/boot1.chrp/boot1.elf -a 1" 2201ca8842fSNathan Whitehorn ROOTFSPART="-p freebsd:=${VMBASE}" 2211ca8842fSNathan Whitehorn if [ ${TARGET_ARCH} = powerpc64le ]; then 2221ca8842fSNathan Whitehorn MAKEFSARGS="-B little" 2231ca8842fSNathan Whitehorn else 2241ca8842fSNathan Whitehorn MAKEFSARGS="-B big" 2251ca8842fSNathan Whitehorn fi 2261ca8842fSNathan Whitehorn ;; 2271ca8842fSNathan Whitehorn *) 2281ca8842fSNathan Whitehorn echo "vmimage.subr: unsupported target '${TARGET}:${TARGET_ARCH}'" >&2 2291ca8842fSNathan Whitehorn exit 1 2301ca8842fSNathan Whitehorn ;; 2311ca8842fSNathan Whitehorn esac 2321ca8842fSNathan Whitehorn 2331ca8842fSNathan Whitehorn if [ ${ESP} = "yes" ]; then 2341ca8842fSNathan Whitehorn # Create an ESP 2351ca8842fSNathan Whitehorn espfilename=$(mktemp /tmp/efiboot.XXXXXX) 2361ca8842fSNathan Whitehorn make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi 2371ca8842fSNathan Whitehorn BOOTPARTS="${BOOTPARTS} -p efi/efiesp:=${espfilename}" 2381ca8842fSNathan Whitehorn 2391ca8842fSNathan Whitehorn # Add this to fstab 2401ca8842fSNathan Whitehorn mkdir -p ${DESTDIR}/boot/efi 2411ca8842fSNathan Whitehorn echo "/dev/${ROOTLABEL}/efiesp /boot/efi msdosfs rw 2 2" \ 2421ca8842fSNathan Whitehorn >> ${DESTDIR}/etc/fstab 2431ca8842fSNathan Whitehorn fi 2441ca8842fSNathan Whitehorn 2451ca8842fSNathan Whitehorn echo "Building filesystem... Please wait." 2461ca8842fSNathan Whitehorn makefs ${MAKEFSARGS} -o label=rootfs -o version=2 -o softupdates=1 \ 2471ca8842fSNathan Whitehorn -s ${VMSIZE} ${VMBASE} ${DESTDIR} 2481ca8842fSNathan Whitehorn 2491ca8842fSNathan Whitehorn echo "Building final disk image... Please wait." 2501ca8842fSNathan Whitehorn mkimg -s ${PARTSCHEME} -f ${VMFORMAT} \ 2511ca8842fSNathan Whitehorn ${BOOTPARTS} \ 2521ca8842fSNathan Whitehorn ${SWAPOPT} \ 2531ca8842fSNathan Whitehorn ${ROOTFSPART} \ 2541ca8842fSNathan Whitehorn -o ${VMIMAGE} 2551ca8842fSNathan Whitehorn 2561ca8842fSNathan Whitehorn if [ ${ESP} = "yes" ]; then 2571ca8842fSNathan Whitehorn rm ${espfilename} 2581ca8842fSNathan Whitehorn fi 2591e7c1f17SGlen Barber 2601e7c1f17SGlen Barber return 0 2611e7c1f17SGlen Barber} 2621e7c1f17SGlen Barber 2631e7c1f17SGlen Barbervm_extra_create_disk() { 2641e7c1f17SGlen Barber 2651e7c1f17SGlen Barber return 0 2661e7c1f17SGlen Barber} 2671e7c1f17SGlen Barber 268