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 431e7c1f17SGlen Barber mkdir -p ${DESTDIR} 441e7c1f17SGlen Barber 451e7c1f17SGlen Barber return 0 461e7c1f17SGlen Barber} 471e7c1f17SGlen Barber 4825c11557SColin Percivalvm_copy_base() { 491ca8842fSNathan Whitehorn # Defunct 5025c11557SColin Percival} 5125c11557SColin Percival 521e7c1f17SGlen Barbervm_install_base() { 531e7c1f17SGlen Barber # Installs the FreeBSD userland/kernel to the virtual machine disk. 541e7c1f17SGlen Barber 551e7c1f17SGlen Barber cd ${WORLDDIR} && \ 561e7c1f17SGlen Barber make DESTDIR=${DESTDIR} \ 571e7c1f17SGlen Barber installworld installkernel distribution || \ 581e7c1f17SGlen Barber err "\n\nCannot install the base system to ${DESTDIR}." 591e7c1f17SGlen Barber 60c52962c6SGlen Barber # Bootstrap etcupdate(8) and mergemaster(8) databases. 61c52962c6SGlen Barber mkdir -p ${DESTDIR}/var/db/etcupdate 62c52962c6SGlen Barber etcupdate extract -B \ 63c52962c6SGlen Barber -M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \ 64c52962c6SGlen Barber -s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate 65c52962c6SGlen Barber sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \ 66c52962c6SGlen Barber -F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \ 67c52962c6SGlen Barber -D ${DESTDIR} 68c52962c6SGlen Barber 691e7c1f17SGlen Barber echo '# Custom /etc/fstab for FreeBSD VM images' \ 701e7c1f17SGlen Barber > ${DESTDIR}/etc/fstab 7189585511SMark Johnston if [ "${VMFS}" != zfs ]; then 7289585511SMark Johnston echo "/dev/${ROOTLABEL}/rootfs / ${VMFS} rw 1 1" \ 731e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 7489585511SMark Johnston fi 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 8389585511SMark Johnston if [ "${VMFS}" = zfs ]; then 8489585511SMark Johnston echo "zfs_enable=\"YES\"" >> ${DESTDIR}/etc/rc.conf 8589585511SMark Johnston echo "zpool_reguid=\"zroot\"" >> ${DESTDIR}/etc/rc.conf 863bf53c4cSLi-Wen Hsu echo "zpool_upgrade=\"zroot\"" >> ${DESTDIR}/etc/rc.conf 8789585511SMark Johnston fi 882170fff8SGlen Barber 891ecaa3f7SColin Percival if ! [ -z "${QEMUSTATIC}" ]; then 901ecaa3f7SColin Percival export EMULATOR=/qemu 911ecaa3f7SColin Percival cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR} 921ecaa3f7SColin Percival fi 931ecaa3f7SColin Percival 94e6f3489cSGlen Barber mkdir -p ${DESTDIR}/dev 95e6f3489cSGlen Barber mount -t devfs devfs ${DESTDIR}/dev 961ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} /usr/bin/newaliases 971ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart 9896341228SColin Percival umount_loop ${DESTDIR}/dev 991e7c1f17SGlen Barber 10030a20c89SGlen Barber cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf 10130a20c89SGlen Barber 10289585511SMark Johnston if [ "${VMFS}" = zfs ]; then 10389585511SMark Johnston echo "kern.geom.label.disk_ident.enable=0" >> ${DESTDIR}/boot/loader.conf 10489585511SMark Johnston echo "zfs_load=YES" >> ${DESTDIR}/boot/loader.conf 10589585511SMark Johnston fi 10689585511SMark Johnston 1071e7c1f17SGlen Barber return 0 1081e7c1f17SGlen Barber} 1091e7c1f17SGlen Barber 1101e7c1f17SGlen Barbervm_extra_install_base() { 1111e7c1f17SGlen Barber # Prototype. When overridden, runs extra post-installworld commands 1121e7c1f17SGlen Barber # as needed, based on the target virtual machine image or cloud 1131e7c1f17SGlen Barber # provider image target. 1141e7c1f17SGlen Barber 1151e7c1f17SGlen Barber return 0 1161e7c1f17SGlen Barber} 1171e7c1f17SGlen Barber 1181e7c1f17SGlen Barbervm_extra_enable_services() { 1191e7c1f17SGlen Barber if [ ! -z "${VM_RC_LIST}" ]; then 1201e7c1f17SGlen Barber for _rcvar in ${VM_RC_LIST}; do 1211e7c1f17SGlen Barber echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf 1221e7c1f17SGlen Barber done 1231e7c1f17SGlen Barber fi 1241e7c1f17SGlen Barber 125f8e26d73SGlen Barber if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then 126f8e26d73SGlen Barber echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \ 127f8e26d73SGlen Barber ${DESTDIR}/etc/rc.conf 128cbc52907SGlen Barber # Expand the filesystem to fill the disk. 129cbc52907SGlen Barber echo 'growfs_enable="YES"' >> ${DESTDIR}/etc/rc.conf 130cbc52907SGlen Barber touch ${DESTDIR}/firstboot 131f8e26d73SGlen Barber fi 132f8e26d73SGlen Barber 1331e7c1f17SGlen Barber return 0 1341e7c1f17SGlen Barber} 1351e7c1f17SGlen Barber 1361e7c1f17SGlen Barbervm_extra_install_packages() { 137a4900ff5SGlen Barber if [ -z "${VM_EXTRA_PACKAGES}" ]; then 138a4900ff5SGlen Barber return 0 139a4900ff5SGlen Barber fi 14030a20c89SGlen Barber mkdir -p ${DESTDIR}/dev 14130a20c89SGlen Barber mount -t devfs devfs ${DESTDIR}/dev 1421ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 14330a20c89SGlen Barber /usr/sbin/pkg bootstrap -y 144be2bc82fSBrad Davis for p in ${VM_EXTRA_PACKAGES}; do 1451ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 146fd17ea8cSBrad Davis /usr/sbin/pkg install -y ${p} 147fd17ea8cSBrad Davis done 14896341228SColin Percival umount_loop ${DESTDIR}/dev 1491e7c1f17SGlen Barber 1501e7c1f17SGlen Barber return 0 1511e7c1f17SGlen Barber} 1521e7c1f17SGlen Barber 1531e7c1f17SGlen Barbervm_extra_install_ports() { 1541e7c1f17SGlen Barber # Prototype. When overridden, installs additional ports within the 1551e7c1f17SGlen Barber # virtual machine environment. 1561e7c1f17SGlen Barber 1571e7c1f17SGlen Barber return 0 1581e7c1f17SGlen Barber} 1591e7c1f17SGlen Barber 160a02bfc83SGlen Barbervm_extra_pre_umount() { 16126e4122aSGlen Barber # Prototype. When overridden, performs additional tasks within the 16226e4122aSGlen Barber # virtual machine environment prior to unmounting the filesystem. 16326e4122aSGlen Barber # Note: When overriding this function, removing resolv.conf in the 16426e4122aSGlen Barber # disk image must be included. 165a02bfc83SGlen Barber 1661ecaa3f7SColin Percival if ! [ -z "${QEMUSTATIC}" ]; then 1671ecaa3f7SColin Percival rm -f ${DESTDIR}/${EMULATOR} 1681ecaa3f7SColin Percival fi 1692d4ff62fSGlen Barber rm -f ${DESTDIR}/etc/resolv.conf 1702d4ff62fSGlen Barber return 0 1712d4ff62fSGlen Barber} 1722d4ff62fSGlen Barber 1732d4ff62fSGlen Barbervm_extra_pkg_rmcache() { 174a4900ff5SGlen Barber if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then 1751ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 176a4900ff5SGlen Barber /usr/local/sbin/pkg clean -y -a 177a4900ff5SGlen Barber fi 17830a20c89SGlen Barber 179a02bfc83SGlen Barber return 0 180a02bfc83SGlen Barber} 181a02bfc83SGlen Barber 18289585511SMark Johnstonbuildfs() { 18389585511SMark Johnston local md tmppool 18489585511SMark Johnston 18589585511SMark Johnston case "${VMFS}" in 18689585511SMark Johnston ufs) 18789585511SMark Johnston makefs ${MAKEFSARGS} -o label=rootfs -o version=2 -o softupdates=1 \ 18889585511SMark Johnston ${VMBASE} ${DESTDIR} 18989585511SMark Johnston ;; 19089585511SMark Johnston zfs) 19189585511SMark Johnston makefs -t zfs ${MAKEFSARGS} \ 19289585511SMark Johnston -o poolname=zroot -o bootfs=zroot/ROOT/default -o rootpath=/ \ 19389585511SMark Johnston -o fs=zroot\;mountpoint=none \ 19489585511SMark Johnston -o fs=zroot/ROOT\;mountpoint=none \ 19589585511SMark Johnston -o fs=zroot/ROOT/default\;mountpoint=/ \ 19689585511SMark Johnston -o fs=zroot/tmp\;mountpoint=/tmp\;exec=on\;setuid=off \ 19789585511SMark Johnston -o fs=zroot/usr\;mountpoint=/usr\;canmount=off \ 19889585511SMark Johnston -o fs=zroot/usr/home \ 19989585511SMark Johnston -o fs=zroot/usr/ports\;setuid=off \ 20089585511SMark Johnston -o fs=zroot/usr/src \ 20189585511SMark Johnston -o fs=zroot/usr/obj \ 20289585511SMark Johnston -o fs=zroot/var\;mountpoint=/var\;canmount=off \ 20389585511SMark Johnston -o fs=zroot/var/audit\;setuid=off\;exec=off \ 20489585511SMark Johnston -o fs=zroot/var/log\;setuid=off\;exec=off \ 20589585511SMark Johnston -o fs=zroot/var/mail\;atime=on \ 20689585511SMark Johnston -o fs=zroot/var/tmp\;setuid=off \ 20789585511SMark Johnston ${VMBASE} ${DESTDIR} 20889585511SMark Johnston ;; 20989585511SMark Johnston *) 21089585511SMark Johnston echo "Unexpected VMFS value '${VMFS}'" 21189585511SMark Johnston exit 1 21289585511SMark Johnston ;; 21389585511SMark Johnston esac 21489585511SMark Johnston} 21589585511SMark Johnston 21696341228SColin Percivalumount_loop() { 21796341228SColin Percival DIR=$1 2181e7c1f17SGlen Barber i=0 2191e7c1f17SGlen Barber sync 22096341228SColin Percival while ! umount ${DIR}; do 2211e7c1f17SGlen Barber i=$(( $i + 1 )) 2221e7c1f17SGlen Barber if [ $i -ge 10 ]; then 2231e7c1f17SGlen Barber # This should never happen. But, it has happened. 22496341228SColin Percival echo "Cannot umount(8) ${DIR}" 22596341228SColin Percival echo "Something has gone horribly wrong." 22696341228SColin Percival return 1 2271e7c1f17SGlen Barber fi 2281e7c1f17SGlen Barber sleep 1 2291e7c1f17SGlen Barber done 2301e7c1f17SGlen Barber 2311e7c1f17SGlen Barber return 0 2321e7c1f17SGlen Barber} 2331e7c1f17SGlen Barber 2341e7c1f17SGlen Barbervm_create_disk() { 23589585511SMark Johnston local BOOTFILES BOOTPARTSOFFSET FSPARTTYPE X86GPTBOOTFILE 2361e7c1f17SGlen Barber 2371ca8842fSNathan Whitehorn if [ -z "${NOSWAP}" ]; then 2381ca8842fSNathan Whitehorn SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}" 2391ca8842fSNathan Whitehorn fi 2401ca8842fSNathan Whitehorn 241fbece760SLi-Wen Hsu if [ -n "${VM_BOOTPARTSOFFSET}" ]; then 242fbece760SLi-Wen Hsu BOOTPARTSOFFSET=":${VM_BOOTPARTSOFFSET}" 243fbece760SLi-Wen Hsu fi 244fbece760SLi-Wen Hsu 24589585511SMark Johnston case "${VMFS}" in 24689585511SMark Johnston ufs) 24789585511SMark Johnston FSPARTTYPE=freebsd-ufs 24889585511SMark Johnston X86GPTBOOTFILE=i386/gptboot/gptboot 24989585511SMark Johnston ;; 25089585511SMark Johnston zfs) 25189585511SMark Johnston FSPARTTYPE=freebsd-zfs 25289585511SMark Johnston X86GPTBOOTFILE=i386/gptzfsboot/gptzfsboot 25389585511SMark Johnston ;; 25489585511SMark Johnston *) 25589585511SMark Johnston echo "Unexpected VMFS value '${VMFS}'" 25689585511SMark Johnston return 1 25789585511SMark Johnston ;; 25889585511SMark Johnston esac 25989585511SMark Johnston 26089585511SMark Johnston echo "Creating image... Please wait." 26189585511SMark Johnston echo 262fbece760SLi-Wen Hsu 2631ca8842fSNathan Whitehorn BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \ 2641ca8842fSNathan Whitehorn WITH_UNIFIED_OBJDIR=yes \ 2651ca8842fSNathan Whitehorn make -C ${WORLDDIR}/stand -V .OBJDIR)" 2661ca8842fSNathan Whitehorn BOOTFILES="$(realpath ${BOOTFILES})" 26789585511SMark Johnston MAKEFSARGS="-s ${VMSIZE}" 2681ca8842fSNathan Whitehorn 2691ca8842fSNathan Whitehorn case "${TARGET}:${TARGET_ARCH}" in 2701ca8842fSNathan Whitehorn amd64:amd64 | i386:i386) 2711ca8842fSNathan Whitehorn ESP=yes 2721ca8842fSNathan Whitehorn BOOTPARTS="-b ${BOOTFILES}/i386/pmbr/pmbr \ 27389585511SMark Johnston -p freebsd-boot/bootfs:=${BOOTFILES}/${X86GPTBOOTFILE}${BOOTPARTSOFFSET}" 27489585511SMark Johnston ROOTFSPART="-p ${FSPARTTYPE}/rootfs:=${VMBASE}" 27589585511SMark Johnston MAKEFSARGS="$MAKEFSARGS -B little" 2761ca8842fSNathan Whitehorn ;; 2771ca8842fSNathan Whitehorn arm64:aarch64 | riscv:riscv64*) 2781ca8842fSNathan Whitehorn ESP=yes 2791ca8842fSNathan Whitehorn BOOTPARTS= 28089585511SMark Johnston ROOTFSPART="-p ${FSPARTTYPE}/rootfs:=${VMBASE}" 28189585511SMark Johnston MAKEFSARGS="$MAKEFSARGS -B little" 2821ca8842fSNathan Whitehorn ;; 2831ca8842fSNathan Whitehorn powerpc:powerpc*) 2841ca8842fSNathan Whitehorn ESP=no 2851ca8842fSNathan Whitehorn BOOTPARTS="-p prepboot:=${BOOTFILES}/powerpc/boot1.chrp/boot1.elf -a 1" 2861ca8842fSNathan Whitehorn ROOTFSPART="-p freebsd:=${VMBASE}" 2871ca8842fSNathan Whitehorn if [ ${TARGET_ARCH} = powerpc64le ]; then 28889585511SMark Johnston MAKEFSARGS="$MAKEFSARGS -B little" 2891ca8842fSNathan Whitehorn else 29089585511SMark Johnston MAKEFSARGS="$MAKEFSARGS -B big" 2911ca8842fSNathan Whitehorn fi 2921ca8842fSNathan Whitehorn ;; 2931ca8842fSNathan Whitehorn *) 2941ca8842fSNathan Whitehorn echo "vmimage.subr: unsupported target '${TARGET}:${TARGET_ARCH}'" >&2 2951ca8842fSNathan Whitehorn exit 1 2961ca8842fSNathan Whitehorn ;; 2971ca8842fSNathan Whitehorn esac 2981ca8842fSNathan Whitehorn 2991ca8842fSNathan Whitehorn if [ ${ESP} = "yes" ]; then 3001ca8842fSNathan Whitehorn # Create an ESP 3011ca8842fSNathan Whitehorn espfilename=$(mktemp /tmp/efiboot.XXXXXX) 3021ca8842fSNathan Whitehorn make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi 3031ca8842fSNathan Whitehorn BOOTPARTS="${BOOTPARTS} -p efi/efiesp:=${espfilename}" 3041ca8842fSNathan Whitehorn 3051ca8842fSNathan Whitehorn # Add this to fstab 3061ca8842fSNathan Whitehorn mkdir -p ${DESTDIR}/boot/efi 3071ca8842fSNathan Whitehorn echo "/dev/${ROOTLABEL}/efiesp /boot/efi msdosfs rw 2 2" \ 3081ca8842fSNathan Whitehorn >> ${DESTDIR}/etc/fstab 3091ca8842fSNathan Whitehorn fi 3101ca8842fSNathan Whitehorn 3111ca8842fSNathan Whitehorn echo "Building filesystem... Please wait." 31289585511SMark Johnston buildfs 3131ca8842fSNathan Whitehorn 3141ca8842fSNathan Whitehorn echo "Building final disk image... Please wait." 3151ca8842fSNathan Whitehorn mkimg -s ${PARTSCHEME} -f ${VMFORMAT} \ 3161ca8842fSNathan Whitehorn ${BOOTPARTS} \ 3171ca8842fSNathan Whitehorn ${SWAPOPT} \ 3181ca8842fSNathan Whitehorn ${ROOTFSPART} \ 3191ca8842fSNathan Whitehorn -o ${VMIMAGE} 3201ca8842fSNathan Whitehorn 321*4194bbb3SEd Maste echo "Disk image ${VMIMAGE} created." 322*4194bbb3SEd Maste 3231ca8842fSNathan Whitehorn if [ ${ESP} = "yes" ]; then 3241ca8842fSNathan Whitehorn rm ${espfilename} 3251ca8842fSNathan Whitehorn fi 3261e7c1f17SGlen Barber 3271e7c1f17SGlen Barber return 0 3281e7c1f17SGlen Barber} 3291e7c1f17SGlen Barber 3301e7c1f17SGlen Barbervm_extra_create_disk() { 3311e7c1f17SGlen Barber 3321e7c1f17SGlen Barber return 0 3331e7c1f17SGlen Barber} 3341e7c1f17SGlen Barber 335