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 598ee478dfSEd 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 83*0b1c5628SColin Percival echo "kern.geom.label.disk_ident.enable=0" >> ${DESTDIR}/boot/loader.conf 84*0b1c5628SColin Percival echo "zfs_load=YES" >> ${DESTDIR}/boot/loader.conf 8589585511SMark Johnston fi 862170fff8SGlen Barber 87*0b1c5628SColin Percival return 0 88*0b1c5628SColin Percival} 89*0b1c5628SColin Percival 90*0b1c5628SColin Percivalvm_emulation_setup() { 911ecaa3f7SColin Percival if ! [ -z "${QEMUSTATIC}" ]; then 921ecaa3f7SColin Percival export EMULATOR=/qemu 931ecaa3f7SColin Percival cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR} 941ecaa3f7SColin Percival fi 951ecaa3f7SColin Percival 96e6f3489cSGlen Barber mkdir -p ${DESTDIR}/dev 97e6f3489cSGlen Barber mount -t devfs devfs ${DESTDIR}/dev 981ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} /usr/bin/newaliases 991ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart 10030a20c89SGlen Barber cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf 10130a20c89SGlen Barber 1021e7c1f17SGlen Barber return 0 1031e7c1f17SGlen Barber} 1041e7c1f17SGlen Barber 1051e7c1f17SGlen Barbervm_extra_install_base() { 1061e7c1f17SGlen Barber # Prototype. When overridden, runs extra post-installworld commands 1071e7c1f17SGlen Barber # as needed, based on the target virtual machine image or cloud 1081e7c1f17SGlen Barber # provider image target. 1091e7c1f17SGlen Barber 1101e7c1f17SGlen Barber return 0 1111e7c1f17SGlen Barber} 1121e7c1f17SGlen Barber 1131e7c1f17SGlen Barbervm_extra_enable_services() { 1141e7c1f17SGlen Barber if [ ! -z "${VM_RC_LIST}" ]; then 1151e7c1f17SGlen Barber for _rcvar in ${VM_RC_LIST}; do 1161e7c1f17SGlen Barber echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf 1171e7c1f17SGlen Barber done 1181e7c1f17SGlen Barber fi 1191e7c1f17SGlen Barber 120f8e26d73SGlen Barber if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then 121f8e26d73SGlen Barber echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \ 122f8e26d73SGlen Barber ${DESTDIR}/etc/rc.conf 123cbc52907SGlen Barber # Expand the filesystem to fill the disk. 124cbc52907SGlen Barber echo 'growfs_enable="YES"' >> ${DESTDIR}/etc/rc.conf 125cbc52907SGlen Barber touch ${DESTDIR}/firstboot 126f8e26d73SGlen Barber fi 127f8e26d73SGlen Barber 1281e7c1f17SGlen Barber return 0 1291e7c1f17SGlen Barber} 1301e7c1f17SGlen Barber 1311e7c1f17SGlen Barbervm_extra_install_packages() { 132a4900ff5SGlen Barber if [ -z "${VM_EXTRA_PACKAGES}" ]; then 133a4900ff5SGlen Barber return 0 134a4900ff5SGlen Barber fi 1351ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 13630a20c89SGlen Barber /usr/sbin/pkg bootstrap -y 137be2bc82fSBrad Davis for p in ${VM_EXTRA_PACKAGES}; do 1381ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 139fd17ea8cSBrad Davis /usr/sbin/pkg install -y ${p} 140fd17ea8cSBrad Davis done 1411e7c1f17SGlen Barber 1421e7c1f17SGlen Barber return 0 1431e7c1f17SGlen Barber} 1441e7c1f17SGlen Barber 1451e7c1f17SGlen Barbervm_extra_install_ports() { 1461e7c1f17SGlen Barber # Prototype. When overridden, installs additional ports within the 1471e7c1f17SGlen Barber # virtual machine environment. 1481e7c1f17SGlen Barber 1491e7c1f17SGlen Barber return 0 1501e7c1f17SGlen Barber} 1511e7c1f17SGlen Barber 152a02bfc83SGlen Barbervm_extra_pre_umount() { 15326e4122aSGlen Barber # Prototype. When overridden, performs additional tasks within the 15426e4122aSGlen Barber # virtual machine environment prior to unmounting the filesystem. 155a02bfc83SGlen Barber 156*0b1c5628SColin Percival return 0 157*0b1c5628SColin Percival} 158*0b1c5628SColin Percival 159*0b1c5628SColin Percivalvm_emulation_cleanup() { 1601ecaa3f7SColin Percival if ! [ -z "${QEMUSTATIC}" ]; then 1611ecaa3f7SColin Percival rm -f ${DESTDIR}/${EMULATOR} 1621ecaa3f7SColin Percival fi 1632d4ff62fSGlen Barber rm -f ${DESTDIR}/etc/resolv.conf 164*0b1c5628SColin Percival umount_loop ${DESTDIR}/dev 1652d4ff62fSGlen Barber return 0 1662d4ff62fSGlen Barber} 1672d4ff62fSGlen Barber 1682d4ff62fSGlen Barbervm_extra_pkg_rmcache() { 169a4900ff5SGlen Barber if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then 1701ecaa3f7SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 171a4900ff5SGlen Barber /usr/local/sbin/pkg clean -y -a 172a4900ff5SGlen Barber fi 17330a20c89SGlen Barber 174a02bfc83SGlen Barber return 0 175a02bfc83SGlen Barber} 176a02bfc83SGlen Barber 17789585511SMark Johnstonbuildfs() { 17889585511SMark Johnston local md tmppool 17989585511SMark Johnston 18089585511SMark Johnston case "${VMFS}" in 18189585511SMark Johnston ufs) 18289585511SMark Johnston makefs ${MAKEFSARGS} -o label=rootfs -o version=2 -o softupdates=1 \ 18389585511SMark Johnston ${VMBASE} ${DESTDIR} 18489585511SMark Johnston ;; 18589585511SMark Johnston zfs) 18689585511SMark Johnston makefs -t zfs ${MAKEFSARGS} \ 18789585511SMark Johnston -o poolname=zroot -o bootfs=zroot/ROOT/default -o rootpath=/ \ 18889585511SMark Johnston -o fs=zroot\;mountpoint=none \ 18989585511SMark Johnston -o fs=zroot/ROOT\;mountpoint=none \ 19089585511SMark Johnston -o fs=zroot/ROOT/default\;mountpoint=/ \ 191d3c32c80SMike Karels -o fs=zroot/home\;mountpoint=/home \ 19289585511SMark Johnston -o fs=zroot/tmp\;mountpoint=/tmp\;exec=on\;setuid=off \ 19389585511SMark Johnston -o fs=zroot/usr\;mountpoint=/usr\;canmount=off \ 19489585511SMark Johnston -o fs=zroot/usr/ports\;setuid=off \ 19589585511SMark Johnston -o fs=zroot/usr/src \ 19689585511SMark Johnston -o fs=zroot/usr/obj \ 19789585511SMark Johnston -o fs=zroot/var\;mountpoint=/var\;canmount=off \ 19889585511SMark Johnston -o fs=zroot/var/audit\;setuid=off\;exec=off \ 199adc215deSJose Luis Duran -o fs=zroot/var/crash\;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 24178b6117dSBaptiste Daroussin if [ -n "${CONFIG_DRIVE}" ]; then 24278b6117dSBaptiste Daroussin CONFIG_DRIVE="-p freebsd/config-drive::${CONFIG_DRIVE_SIZE}" 24378b6117dSBaptiste Daroussin fi 24478b6117dSBaptiste Daroussin 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 ;; 27765190700SMuhammad Moinur Rahman arm:armv7 | 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 3034f223e0dSJose Luis Duran BOOTPARTS="${BOOTPARTS} -p efi/efiboot0:=${espfilename}" 3041ca8842fSNathan Whitehorn 3051ca8842fSNathan Whitehorn # Add this to fstab 3061ca8842fSNathan Whitehorn mkdir -p ${DESTDIR}/boot/efi 3074f223e0dSJose Luis Duran echo "/dev/${ROOTLABEL}/efiboot0 /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} \ 31878b6117dSBaptiste Daroussin ${CONFIG_DRIVE} \ 3191ca8842fSNathan Whitehorn ${ROOTFSPART} \ 3201ca8842fSNathan Whitehorn -o ${VMIMAGE} 3211ca8842fSNathan Whitehorn 3224194bbb3SEd Maste echo "Disk image ${VMIMAGE} created." 3234194bbb3SEd Maste 3241ca8842fSNathan Whitehorn if [ ${ESP} = "yes" ]; then 3251ca8842fSNathan Whitehorn rm ${espfilename} 3261ca8842fSNathan Whitehorn fi 3271e7c1f17SGlen Barber 3281e7c1f17SGlen Barber return 0 3291e7c1f17SGlen Barber} 3301e7c1f17SGlen Barber 3311e7c1f17SGlen Barbervm_extra_create_disk() { 3321e7c1f17SGlen Barber 3331e7c1f17SGlen Barber return 0 3341e7c1f17SGlen Barber} 3351e7c1f17SGlen Barber 336