11e7c1f17SGlen Barber#!/bin/sh 21e7c1f17SGlen Barber# 31e7c1f17SGlen Barber# 41e7c1f17SGlen Barber# 51e7c1f17SGlen Barber# Common functions for virtual machine image build scripts. 61e7c1f17SGlen Barber# 71e7c1f17SGlen Barber 8db8b5613SRebecca Cranscriptdir=$(dirname $(realpath $0)) 9db8b5613SRebecca Cran. ${scriptdir}/../../tools/boot/install-boot.sh 10db8b5613SRebecca Cran 111e7c1f17SGlen Barberexport PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin" 121e7c1f17SGlen Barbertrap "cleanup" INT QUIT TRAP ABRT TERM 131e7c1f17SGlen Barber 141ca8842fSNathan Whitehorn# Platform-specific large-scale setup 151ca8842fSNathan Whitehorn# Most platforms use GPT, so put that as default, then special cases 161ca8842fSNathan WhitehornPARTSCHEME=gpt 171ca8842fSNathan WhitehornROOTLABEL="gpt" 18efeb11a7SGlen Barbercase "${TARGET}:${TARGET_ARCH}" in 19efeb11a7SGlen Barber powerpc:powerpc*) 201ca8842fSNathan Whitehorn PARTSCHEME=mbr 211ca8842fSNathan Whitehorn ROOTLABEL="ufs" 221ca8842fSNathan Whitehorn NOSWAP=yes # Can't label swap partition with MBR, so no swap 23efeb11a7SGlen Barber ;; 24efeb11a7SGlen Barberesac 25efeb11a7SGlen Barber 261e7c1f17SGlen Barbererr() { 271e7c1f17SGlen Barber printf "${@}\n" 281e7c1f17SGlen Barber cleanup 291e7c1f17SGlen Barber return 1 301e7c1f17SGlen Barber} 311e7c1f17SGlen Barber 321e7c1f17SGlen Barbercleanup() { 33dcdebabdSColin Percival if [ -c "${DESTDIR}/dev/null" ]; then 3496341228SColin Percival umount_loop ${DESTDIR}/dev 2>/dev/null 3596341228SColin Percival fi 361e7c1f17SGlen Barber 371e7c1f17SGlen Barber return 0 381e7c1f17SGlen Barber} 391e7c1f17SGlen Barber 401e7c1f17SGlen Barbervm_create_base() { 411e7c1f17SGlen Barber 421e7c1f17SGlen Barber mkdir -p ${DESTDIR} 431e7c1f17SGlen Barber 441e7c1f17SGlen Barber return 0 451e7c1f17SGlen Barber} 461e7c1f17SGlen Barber 4725c11557SColin Percivalvm_copy_base() { 481ca8842fSNathan Whitehorn # Defunct 4925c11557SColin Percival} 5025c11557SColin Percival 511e7c1f17SGlen Barbervm_install_base() { 521e7c1f17SGlen Barber # Installs the FreeBSD userland/kernel to the virtual machine disk. 531e7c1f17SGlen Barber 541e7c1f17SGlen Barber cd ${WORLDDIR} && \ 551e7c1f17SGlen Barber make DESTDIR=${DESTDIR} \ 561e7c1f17SGlen Barber installworld installkernel distribution || \ 571e7c1f17SGlen Barber err "\n\nCannot install the base system to ${DESTDIR}." 581e7c1f17SGlen Barber 59*8ee478dfSEd Maste # Bootstrap etcupdate(8) database. 60c52962c6SGlen Barber mkdir -p ${DESTDIR}/var/db/etcupdate 61c52962c6SGlen Barber etcupdate extract -B \ 62c52962c6SGlen Barber -M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \ 63c52962c6SGlen Barber -s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate 64c52962c6SGlen Barber 651e7c1f17SGlen Barber echo '# Custom /etc/fstab for FreeBSD VM images' \ 661e7c1f17SGlen Barber > ${DESTDIR}/etc/fstab 6789585511SMark Johnston if [ "${VMFS}" != zfs ]; then 6889585511SMark Johnston echo "/dev/${ROOTLABEL}/rootfs / ${VMFS} rw 1 1" \ 691e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 7089585511SMark Johnston fi 714e7bc9f0SColin Percival if [ -z "${NOSWAP}" ]; then 721e7c1f17SGlen Barber echo '/dev/gpt/swapfs none swap sw 0 0' \ 731e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 744e7bc9f0SColin Percival fi 751e7c1f17SGlen Barber 762170fff8SGlen Barber local hostname 772170fff8SGlen Barber hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')" 782170fff8SGlen Barber echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf 7989585511SMark Johnston if [ "${VMFS}" = zfs ]; then 8089585511SMark Johnston echo "zfs_enable=\"YES\"" >> ${DESTDIR}/etc/rc.conf 8189585511SMark Johnston echo "zpool_reguid=\"zroot\"" >> ${DESTDIR}/etc/rc.conf 823bf53c4cSLi-Wen Hsu echo "zpool_upgrade=\"zroot\"" >> ${DESTDIR}/etc/rc.conf 8389585511SMark Johnston fi 842170fff8SGlen Barber 851ecaa3f7SColin Percival if ! [ -z "${QEMUSTATIC}" ]; then 861ecaa3f7SColin Percival export EMULATOR=/qemu 871ecaa3f7SColin Percival cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR} 881ecaa3f7SColin Percival fi 891ecaa3f7SColin Percival 90e6f3489cSGlen Barber mkdir -p ${DESTDIR}/dev 91e6f3489cSGlen Barber mount -t devfs devfs ${DESTDIR}/dev 921ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} /usr/bin/newaliases 931ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart 9496341228SColin Percival umount_loop ${DESTDIR}/dev 951e7c1f17SGlen Barber 9630a20c89SGlen Barber cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf 9730a20c89SGlen Barber 9889585511SMark Johnston if [ "${VMFS}" = zfs ]; then 9989585511SMark Johnston echo "kern.geom.label.disk_ident.enable=0" >> ${DESTDIR}/boot/loader.conf 10089585511SMark Johnston echo "zfs_load=YES" >> ${DESTDIR}/boot/loader.conf 10189585511SMark Johnston fi 10289585511SMark Johnston 1031e7c1f17SGlen Barber return 0 1041e7c1f17SGlen Barber} 1051e7c1f17SGlen Barber 1061e7c1f17SGlen Barbervm_extra_install_base() { 1071e7c1f17SGlen Barber # Prototype. When overridden, runs extra post-installworld commands 1081e7c1f17SGlen Barber # as needed, based on the target virtual machine image or cloud 1091e7c1f17SGlen Barber # provider image target. 1101e7c1f17SGlen Barber 1111e7c1f17SGlen Barber return 0 1121e7c1f17SGlen Barber} 1131e7c1f17SGlen Barber 1141e7c1f17SGlen Barbervm_extra_enable_services() { 1151e7c1f17SGlen Barber if [ ! -z "${VM_RC_LIST}" ]; then 1161e7c1f17SGlen Barber for _rcvar in ${VM_RC_LIST}; do 1171e7c1f17SGlen Barber echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf 1181e7c1f17SGlen Barber done 1191e7c1f17SGlen Barber fi 1201e7c1f17SGlen Barber 121f8e26d73SGlen Barber if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then 122f8e26d73SGlen Barber echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \ 123f8e26d73SGlen Barber ${DESTDIR}/etc/rc.conf 124cbc52907SGlen Barber # Expand the filesystem to fill the disk. 125cbc52907SGlen Barber echo 'growfs_enable="YES"' >> ${DESTDIR}/etc/rc.conf 126cbc52907SGlen Barber touch ${DESTDIR}/firstboot 127f8e26d73SGlen Barber fi 128f8e26d73SGlen Barber 1291e7c1f17SGlen Barber return 0 1301e7c1f17SGlen Barber} 1311e7c1f17SGlen Barber 1321e7c1f17SGlen Barbervm_extra_install_packages() { 133a4900ff5SGlen Barber if [ -z "${VM_EXTRA_PACKAGES}" ]; then 134a4900ff5SGlen Barber return 0 135a4900ff5SGlen Barber fi 13630a20c89SGlen Barber mkdir -p ${DESTDIR}/dev 13730a20c89SGlen Barber mount -t devfs devfs ${DESTDIR}/dev 1381ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 13930a20c89SGlen Barber /usr/sbin/pkg bootstrap -y 140be2bc82fSBrad Davis for p in ${VM_EXTRA_PACKAGES}; do 1411ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 142fd17ea8cSBrad Davis /usr/sbin/pkg install -y ${p} 143fd17ea8cSBrad Davis done 14496341228SColin Percival umount_loop ${DESTDIR}/dev 1451e7c1f17SGlen Barber 1461e7c1f17SGlen Barber return 0 1471e7c1f17SGlen Barber} 1481e7c1f17SGlen Barber 1491e7c1f17SGlen Barbervm_extra_install_ports() { 1501e7c1f17SGlen Barber # Prototype. When overridden, installs additional ports within the 1511e7c1f17SGlen Barber # virtual machine environment. 1521e7c1f17SGlen Barber 1531e7c1f17SGlen Barber return 0 1541e7c1f17SGlen Barber} 1551e7c1f17SGlen Barber 156a02bfc83SGlen Barbervm_extra_pre_umount() { 15726e4122aSGlen Barber # Prototype. When overridden, performs additional tasks within the 15826e4122aSGlen Barber # virtual machine environment prior to unmounting the filesystem. 15926e4122aSGlen Barber # Note: When overriding this function, removing resolv.conf in the 16026e4122aSGlen Barber # disk image must be included. 161a02bfc83SGlen Barber 1621ecaa3f7SColin Percival if ! [ -z "${QEMUSTATIC}" ]; then 1631ecaa3f7SColin Percival rm -f ${DESTDIR}/${EMULATOR} 1641ecaa3f7SColin Percival fi 1652d4ff62fSGlen Barber rm -f ${DESTDIR}/etc/resolv.conf 1662d4ff62fSGlen Barber return 0 1672d4ff62fSGlen Barber} 1682d4ff62fSGlen Barber 1692d4ff62fSGlen Barbervm_extra_pkg_rmcache() { 170a4900ff5SGlen Barber if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then 1711ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 172a4900ff5SGlen Barber /usr/local/sbin/pkg clean -y -a 173a4900ff5SGlen Barber fi 17430a20c89SGlen Barber 175a02bfc83SGlen Barber return 0 176a02bfc83SGlen Barber} 177a02bfc83SGlen Barber 17889585511SMark Johnstonbuildfs() { 17989585511SMark Johnston local md tmppool 18089585511SMark Johnston 18189585511SMark Johnston case "${VMFS}" in 18289585511SMark Johnston ufs) 18389585511SMark Johnston makefs ${MAKEFSARGS} -o label=rootfs -o version=2 -o softupdates=1 \ 18489585511SMark Johnston ${VMBASE} ${DESTDIR} 18589585511SMark Johnston ;; 18689585511SMark Johnston zfs) 18789585511SMark Johnston makefs -t zfs ${MAKEFSARGS} \ 18889585511SMark Johnston -o poolname=zroot -o bootfs=zroot/ROOT/default -o rootpath=/ \ 18989585511SMark Johnston -o fs=zroot\;mountpoint=none \ 19089585511SMark Johnston -o fs=zroot/ROOT\;mountpoint=none \ 19189585511SMark Johnston -o fs=zroot/ROOT/default\;mountpoint=/ \ 192d3c32c80SMike Karels -o fs=zroot/home\;mountpoint=/home \ 19389585511SMark Johnston -o fs=zroot/tmp\;mountpoint=/tmp\;exec=on\;setuid=off \ 19489585511SMark Johnston -o fs=zroot/usr\;mountpoint=/usr\;canmount=off \ 19589585511SMark Johnston -o fs=zroot/usr/ports\;setuid=off \ 19689585511SMark Johnston -o fs=zroot/usr/src \ 19789585511SMark Johnston -o fs=zroot/usr/obj \ 19889585511SMark Johnston -o fs=zroot/var\;mountpoint=/var\;canmount=off \ 19989585511SMark Johnston -o fs=zroot/var/audit\;setuid=off\;exec=off \ 20089585511SMark Johnston -o fs=zroot/var/log\;setuid=off\;exec=off \ 20189585511SMark Johnston -o fs=zroot/var/mail\;atime=on \ 20289585511SMark Johnston -o fs=zroot/var/tmp\;setuid=off \ 20389585511SMark Johnston ${VMBASE} ${DESTDIR} 20489585511SMark Johnston ;; 20589585511SMark Johnston *) 20689585511SMark Johnston echo "Unexpected VMFS value '${VMFS}'" 20789585511SMark Johnston exit 1 20889585511SMark Johnston ;; 20989585511SMark Johnston esac 21089585511SMark Johnston} 21189585511SMark Johnston 21296341228SColin Percivalumount_loop() { 21396341228SColin Percival DIR=$1 2141e7c1f17SGlen Barber i=0 2151e7c1f17SGlen Barber sync 21696341228SColin Percival while ! umount ${DIR}; do 2171e7c1f17SGlen Barber i=$(( $i + 1 )) 2181e7c1f17SGlen Barber if [ $i -ge 10 ]; then 2191e7c1f17SGlen Barber # This should never happen. But, it has happened. 22096341228SColin Percival echo "Cannot umount(8) ${DIR}" 22196341228SColin Percival echo "Something has gone horribly wrong." 22296341228SColin Percival return 1 2231e7c1f17SGlen Barber fi 2241e7c1f17SGlen Barber sleep 1 2251e7c1f17SGlen Barber done 2261e7c1f17SGlen Barber 2271e7c1f17SGlen Barber return 0 2281e7c1f17SGlen Barber} 2291e7c1f17SGlen Barber 2301e7c1f17SGlen Barbervm_create_disk() { 23189585511SMark Johnston local BOOTFILES BOOTPARTSOFFSET FSPARTTYPE X86GPTBOOTFILE 2321e7c1f17SGlen Barber 2331ca8842fSNathan Whitehorn if [ -z "${NOSWAP}" ]; then 2341ca8842fSNathan Whitehorn SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}" 2351ca8842fSNathan Whitehorn fi 2361ca8842fSNathan Whitehorn 237fbece760SLi-Wen Hsu if [ -n "${VM_BOOTPARTSOFFSET}" ]; then 238fbece760SLi-Wen Hsu BOOTPARTSOFFSET=":${VM_BOOTPARTSOFFSET}" 239fbece760SLi-Wen Hsu fi 240fbece760SLi-Wen Hsu 24189585511SMark Johnston case "${VMFS}" in 24289585511SMark Johnston ufs) 24389585511SMark Johnston FSPARTTYPE=freebsd-ufs 24489585511SMark Johnston X86GPTBOOTFILE=i386/gptboot/gptboot 24589585511SMark Johnston ;; 24689585511SMark Johnston zfs) 24789585511SMark Johnston FSPARTTYPE=freebsd-zfs 24889585511SMark Johnston X86GPTBOOTFILE=i386/gptzfsboot/gptzfsboot 24989585511SMark Johnston ;; 25089585511SMark Johnston *) 25189585511SMark Johnston echo "Unexpected VMFS value '${VMFS}'" 25289585511SMark Johnston return 1 25389585511SMark Johnston ;; 25489585511SMark Johnston esac 25589585511SMark Johnston 25689585511SMark Johnston echo "Creating image... Please wait." 25789585511SMark Johnston echo 258fbece760SLi-Wen Hsu 2591ca8842fSNathan Whitehorn BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \ 2601ca8842fSNathan Whitehorn WITH_UNIFIED_OBJDIR=yes \ 2611ca8842fSNathan Whitehorn make -C ${WORLDDIR}/stand -V .OBJDIR)" 2621ca8842fSNathan Whitehorn BOOTFILES="$(realpath ${BOOTFILES})" 26389585511SMark Johnston MAKEFSARGS="-s ${VMSIZE}" 2641ca8842fSNathan Whitehorn 2651ca8842fSNathan Whitehorn case "${TARGET}:${TARGET_ARCH}" in 2661ca8842fSNathan Whitehorn amd64:amd64 | i386:i386) 2671ca8842fSNathan Whitehorn ESP=yes 2681ca8842fSNathan Whitehorn BOOTPARTS="-b ${BOOTFILES}/i386/pmbr/pmbr \ 26989585511SMark Johnston -p freebsd-boot/bootfs:=${BOOTFILES}/${X86GPTBOOTFILE}${BOOTPARTSOFFSET}" 27089585511SMark Johnston ROOTFSPART="-p ${FSPARTTYPE}/rootfs:=${VMBASE}" 27189585511SMark Johnston MAKEFSARGS="$MAKEFSARGS -B little" 2721ca8842fSNathan Whitehorn ;; 2731ca8842fSNathan Whitehorn arm64:aarch64 | riscv:riscv64*) 2741ca8842fSNathan Whitehorn ESP=yes 2751ca8842fSNathan Whitehorn BOOTPARTS= 27689585511SMark Johnston ROOTFSPART="-p ${FSPARTTYPE}/rootfs:=${VMBASE}" 27789585511SMark Johnston MAKEFSARGS="$MAKEFSARGS -B little" 2781ca8842fSNathan Whitehorn ;; 2791ca8842fSNathan Whitehorn powerpc:powerpc*) 2801ca8842fSNathan Whitehorn ESP=no 2811ca8842fSNathan Whitehorn BOOTPARTS="-p prepboot:=${BOOTFILES}/powerpc/boot1.chrp/boot1.elf -a 1" 2821ca8842fSNathan Whitehorn ROOTFSPART="-p freebsd:=${VMBASE}" 2831ca8842fSNathan Whitehorn if [ ${TARGET_ARCH} = powerpc64le ]; then 28489585511SMark Johnston MAKEFSARGS="$MAKEFSARGS -B little" 2851ca8842fSNathan Whitehorn else 28689585511SMark Johnston MAKEFSARGS="$MAKEFSARGS -B big" 2871ca8842fSNathan Whitehorn fi 2881ca8842fSNathan Whitehorn ;; 2891ca8842fSNathan Whitehorn *) 2901ca8842fSNathan Whitehorn echo "vmimage.subr: unsupported target '${TARGET}:${TARGET_ARCH}'" >&2 2911ca8842fSNathan Whitehorn exit 1 2921ca8842fSNathan Whitehorn ;; 2931ca8842fSNathan Whitehorn esac 2941ca8842fSNathan Whitehorn 2951ca8842fSNathan Whitehorn if [ ${ESP} = "yes" ]; then 2961ca8842fSNathan Whitehorn # Create an ESP 2971ca8842fSNathan Whitehorn espfilename=$(mktemp /tmp/efiboot.XXXXXX) 2981ca8842fSNathan Whitehorn make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi 2991ca8842fSNathan Whitehorn BOOTPARTS="${BOOTPARTS} -p efi/efiesp:=${espfilename}" 3001ca8842fSNathan Whitehorn 3011ca8842fSNathan Whitehorn # Add this to fstab 3021ca8842fSNathan Whitehorn mkdir -p ${DESTDIR}/boot/efi 3031ca8842fSNathan Whitehorn echo "/dev/${ROOTLABEL}/efiesp /boot/efi msdosfs rw 2 2" \ 3041ca8842fSNathan Whitehorn >> ${DESTDIR}/etc/fstab 3051ca8842fSNathan Whitehorn fi 3061ca8842fSNathan Whitehorn 3071ca8842fSNathan Whitehorn echo "Building filesystem... Please wait." 30889585511SMark Johnston buildfs 3091ca8842fSNathan Whitehorn 3101ca8842fSNathan Whitehorn echo "Building final disk image... Please wait." 3111ca8842fSNathan Whitehorn mkimg -s ${PARTSCHEME} -f ${VMFORMAT} \ 3121ca8842fSNathan Whitehorn ${BOOTPARTS} \ 3131ca8842fSNathan Whitehorn ${SWAPOPT} \ 3141ca8842fSNathan Whitehorn ${ROOTFSPART} \ 3151ca8842fSNathan Whitehorn -o ${VMIMAGE} 3161ca8842fSNathan Whitehorn 3174194bbb3SEd Maste echo "Disk image ${VMIMAGE} created." 3184194bbb3SEd Maste 3191ca8842fSNathan Whitehorn if [ ${ESP} = "yes" ]; then 3201ca8842fSNathan Whitehorn rm ${espfilename} 3211ca8842fSNathan Whitehorn fi 3221e7c1f17SGlen Barber 3231e7c1f17SGlen Barber return 0 3241e7c1f17SGlen Barber} 3251e7c1f17SGlen Barber 3261e7c1f17SGlen Barbervm_extra_create_disk() { 3271e7c1f17SGlen Barber 3281e7c1f17SGlen Barber return 0 3291e7c1f17SGlen Barber} 3301e7c1f17SGlen Barber 331