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 15*1ca8842fSNathan Whitehorn# Platform-specific large-scale setup 16*1ca8842fSNathan Whitehorn# Most platforms use GPT, so put that as default, then special cases 17*1ca8842fSNathan WhitehornPARTSCHEME=gpt 18*1ca8842fSNathan WhitehornROOTLABEL="gpt" 19efeb11a7SGlen Barbercase "${TARGET}:${TARGET_ARCH}" in 20efeb11a7SGlen Barber powerpc:powerpc*) 21*1ca8842fSNathan Whitehorn PARTSCHEME=mbr 22*1ca8842fSNathan Whitehorn ROOTLABEL="ufs" 23*1ca8842fSNathan 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() { 51*1ca8842fSNathan 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 1341ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 13530a20c89SGlen Barber /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES} 13696341228SColin Percival umount_loop ${DESTDIR}/dev 1371e7c1f17SGlen Barber 1381e7c1f17SGlen Barber return 0 1391e7c1f17SGlen Barber} 1401e7c1f17SGlen Barber 1411e7c1f17SGlen Barbervm_extra_install_ports() { 1421e7c1f17SGlen Barber # Prototype. When overridden, installs additional ports within the 1431e7c1f17SGlen Barber # virtual machine environment. 1441e7c1f17SGlen Barber 1451e7c1f17SGlen Barber return 0 1461e7c1f17SGlen Barber} 1471e7c1f17SGlen Barber 148a02bfc83SGlen Barbervm_extra_pre_umount() { 14926e4122aSGlen Barber # Prototype. When overridden, performs additional tasks within the 15026e4122aSGlen Barber # virtual machine environment prior to unmounting the filesystem. 15126e4122aSGlen Barber # Note: When overriding this function, removing resolv.conf in the 15226e4122aSGlen Barber # disk image must be included. 153a02bfc83SGlen Barber 1541ecaa3f7SColin Percival if ! [ -z "${QEMUSTATIC}" ]; then 1551ecaa3f7SColin Percival rm -f ${DESTDIR}/${EMULATOR} 1561ecaa3f7SColin Percival fi 1572d4ff62fSGlen Barber rm -f ${DESTDIR}/etc/resolv.conf 1582d4ff62fSGlen Barber return 0 1592d4ff62fSGlen Barber} 1602d4ff62fSGlen Barber 1612d4ff62fSGlen Barbervm_extra_pkg_rmcache() { 162a4900ff5SGlen Barber if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then 1631ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 164a4900ff5SGlen Barber /usr/local/sbin/pkg clean -y -a 165a4900ff5SGlen Barber fi 16630a20c89SGlen Barber 167a02bfc83SGlen Barber return 0 168a02bfc83SGlen Barber} 169a02bfc83SGlen Barber 17096341228SColin Percivalumount_loop() { 17196341228SColin Percival DIR=$1 1721e7c1f17SGlen Barber i=0 1731e7c1f17SGlen Barber sync 17496341228SColin Percival while ! umount ${DIR}; do 1751e7c1f17SGlen Barber i=$(( $i + 1 )) 1761e7c1f17SGlen Barber if [ $i -ge 10 ]; then 1771e7c1f17SGlen Barber # This should never happen. But, it has happened. 17896341228SColin Percival echo "Cannot umount(8) ${DIR}" 17996341228SColin Percival echo "Something has gone horribly wrong." 18096341228SColin Percival return 1 1811e7c1f17SGlen Barber fi 1821e7c1f17SGlen Barber sleep 1 1831e7c1f17SGlen Barber done 1841e7c1f17SGlen Barber 1851e7c1f17SGlen Barber return 0 1861e7c1f17SGlen Barber} 1871e7c1f17SGlen Barber 1881e7c1f17SGlen Barbervm_create_disk() { 1891e7c1f17SGlen Barber echo "Creating image... Please wait." 1901e7c1f17SGlen Barber echo 1911e7c1f17SGlen Barber 192*1ca8842fSNathan Whitehorn if [ -z "${NOSWAP}" ]; then 193*1ca8842fSNathan Whitehorn SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}" 194*1ca8842fSNathan Whitehorn fi 195*1ca8842fSNathan Whitehorn 196*1ca8842fSNathan Whitehorn BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \ 197*1ca8842fSNathan Whitehorn WITH_UNIFIED_OBJDIR=yes \ 198*1ca8842fSNathan Whitehorn make -C ${WORLDDIR}/stand -V .OBJDIR)" 199*1ca8842fSNathan Whitehorn BOOTFILES="$(realpath ${BOOTFILES})" 200*1ca8842fSNathan Whitehorn 201*1ca8842fSNathan Whitehorn case "${TARGET}:${TARGET_ARCH}" in 202*1ca8842fSNathan Whitehorn amd64:amd64 | i386:i386) 203*1ca8842fSNathan Whitehorn ESP=yes 204*1ca8842fSNathan Whitehorn BOOTPARTS="-b ${BOOTFILES}/i386/pmbr/pmbr \ 205*1ca8842fSNathan Whitehorn -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot" 206*1ca8842fSNathan Whitehorn ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}" 207*1ca8842fSNathan Whitehorn MAKEFSARGS="-B little" 208*1ca8842fSNathan Whitehorn ;; 209*1ca8842fSNathan Whitehorn arm64:aarch64 | riscv:riscv64*) 210*1ca8842fSNathan Whitehorn ESP=yes 211*1ca8842fSNathan Whitehorn BOOTPARTS= 212*1ca8842fSNathan Whitehorn ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}" 213*1ca8842fSNathan Whitehorn MAKEFSARGS="-B little" 214*1ca8842fSNathan Whitehorn ;; 215*1ca8842fSNathan Whitehorn powerpc:powerpc*) 216*1ca8842fSNathan Whitehorn ESP=no 217*1ca8842fSNathan Whitehorn BOOTPARTS="-p prepboot:=${BOOTFILES}/powerpc/boot1.chrp/boot1.elf -a 1" 218*1ca8842fSNathan Whitehorn ROOTFSPART="-p freebsd:=${VMBASE}" 219*1ca8842fSNathan Whitehorn if [ ${TARGET_ARCH} = powerpc64le ]; then 220*1ca8842fSNathan Whitehorn MAKEFSARGS="-B little" 221*1ca8842fSNathan Whitehorn else 222*1ca8842fSNathan Whitehorn MAKEFSARGS="-B big" 223*1ca8842fSNathan Whitehorn fi 224*1ca8842fSNathan Whitehorn ;; 225*1ca8842fSNathan Whitehorn *) 226*1ca8842fSNathan Whitehorn echo "vmimage.subr: unsupported target '${TARGET}:${TARGET_ARCH}'" >&2 227*1ca8842fSNathan Whitehorn exit 1 228*1ca8842fSNathan Whitehorn ;; 229*1ca8842fSNathan Whitehorn esac 230*1ca8842fSNathan Whitehorn 231*1ca8842fSNathan Whitehorn if [ ${ESP} = "yes" ]; then 232*1ca8842fSNathan Whitehorn # Create an ESP 233*1ca8842fSNathan Whitehorn espfilename=$(mktemp /tmp/efiboot.XXXXXX) 234*1ca8842fSNathan Whitehorn make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi 235*1ca8842fSNathan Whitehorn BOOTPARTS="${BOOTPARTS} -p efi/efiesp:=${espfilename}" 236*1ca8842fSNathan Whitehorn 237*1ca8842fSNathan Whitehorn # Add this to fstab 238*1ca8842fSNathan Whitehorn mkdir -p ${DESTDIR}/boot/efi 239*1ca8842fSNathan Whitehorn echo "/dev/${ROOTLABEL}/efiesp /boot/efi msdosfs rw 2 2" \ 240*1ca8842fSNathan Whitehorn >> ${DESTDIR}/etc/fstab 241*1ca8842fSNathan Whitehorn fi 242*1ca8842fSNathan Whitehorn 243*1ca8842fSNathan Whitehorn echo "Building filesystem... Please wait." 244*1ca8842fSNathan Whitehorn makefs ${MAKEFSARGS} -o label=rootfs -o version=2 -o softupdates=1 \ 245*1ca8842fSNathan Whitehorn -s ${VMSIZE} ${VMBASE} ${DESTDIR} 246*1ca8842fSNathan Whitehorn 247*1ca8842fSNathan Whitehorn echo "Building final disk image... Please wait." 248*1ca8842fSNathan Whitehorn mkimg -s ${PARTSCHEME} -f ${VMFORMAT} \ 249*1ca8842fSNathan Whitehorn ${BOOTPARTS} \ 250*1ca8842fSNathan Whitehorn ${SWAPOPT} \ 251*1ca8842fSNathan Whitehorn ${ROOTFSPART} \ 252*1ca8842fSNathan Whitehorn -o ${VMIMAGE} 253*1ca8842fSNathan Whitehorn 254*1ca8842fSNathan Whitehorn if [ ${ESP} = "yes" ]; then 255*1ca8842fSNathan Whitehorn rm ${espfilename} 256*1ca8842fSNathan Whitehorn fi 2571e7c1f17SGlen Barber 2581e7c1f17SGlen Barber return 0 2591e7c1f17SGlen Barber} 2601e7c1f17SGlen Barber 2611e7c1f17SGlen Barbervm_extra_create_disk() { 2621e7c1f17SGlen Barber 2631e7c1f17SGlen Barber return 0 2641e7c1f17SGlen Barber} 2651e7c1f17SGlen Barber 266