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 91e7c1f17SGlen Barberexport PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin" 101e7c1f17SGlen Barbertrap "cleanup" INT QUIT TRAP ABRT TERM 111e7c1f17SGlen Barber 12efeb11a7SGlen Barberwrite_partition_layout() { 134e7bc9f0SColin Percival if [ -z "${NOSWAP}" ]; then 144e7bc9f0SColin Percival SWAPOPT="-p freebsd-swap/swapfs::1G" 154e7bc9f0SColin Percival fi 16efeb11a7SGlen Barber 17efeb11a7SGlen Barber case "${TARGET}:${TARGET_ARCH}" in 18efeb11a7SGlen Barber amd64:amd64 | i386:i386) 197d565d60SGlen Barber mkimg -s gpt -b /boot/pmbr \ 20efeb11a7SGlen Barber -p freebsd-boot/bootfs:=/boot/gptboot \ 214e7bc9f0SColin Percival ${SWAPOPT} \ 2238d59ac7SGlen Barber -p freebsd-ufs/rootfs:=${VMBASE} \ 23efeb11a7SGlen Barber -o ${VMIMAGE} 24efeb11a7SGlen Barber ;; 25efeb11a7SGlen Barber powerpc:powerpc*) 267d565d60SGlen Barber mkimg -s apm \ 279d3df3bdSGlen Barber -p apple-boot/bootfs:=/boot/boot1.hfs \ 284e7bc9f0SColin Percival ${SWAPOPT} \ 2938d59ac7SGlen Barber -p freebsd-ufs/rootfs:=${VMBASE} \ 30efeb11a7SGlen Barber -o ${VMIMAGE} 31efeb11a7SGlen Barber ;; 32efeb11a7SGlen Barber *) 33efeb11a7SGlen Barber # ENOTSUPP 34efeb11a7SGlen Barber return 1 35efeb11a7SGlen Barber ;; 36efeb11a7SGlen Barber esac 37efeb11a7SGlen Barber 38efeb11a7SGlen Barber return 0 39efeb11a7SGlen Barber} 401e7c1f17SGlen Barber 411e7c1f17SGlen Barbererr() { 421e7c1f17SGlen Barber printf "${@}\n" 431e7c1f17SGlen Barber cleanup 441e7c1f17SGlen Barber return 1 451e7c1f17SGlen Barber} 461e7c1f17SGlen Barber 471e7c1f17SGlen Barbercleanup() { 487d97075fSColin Percival umount ${DESTDIR}/dev 2>/dev/null 497d97075fSColin Percival umount ${DESTDIR} 501e7c1f17SGlen Barber if [ ! -z "${mddev}" ]; then 511e7c1f17SGlen Barber mdconfig -d -u ${mddev} 521e7c1f17SGlen Barber fi 531e7c1f17SGlen Barber 541e7c1f17SGlen Barber return 0 551e7c1f17SGlen Barber} 561e7c1f17SGlen Barber 571e7c1f17SGlen Barbervm_create_base() { 581e7c1f17SGlen Barber # Creates the UFS root filesystem for the virtual machine disk, 591e7c1f17SGlen Barber # written to the formatted disk image with mkimg(1). 601e7c1f17SGlen Barber 611e7c1f17SGlen Barber mkdir -p ${DESTDIR} 621e7c1f17SGlen Barber truncate -s ${VMSIZE} ${VMBASE} 631e7c1f17SGlen Barber mddev=$(mdconfig -f ${VMBASE}) 641e7c1f17SGlen Barber newfs -j /dev/${mddev} 651e7c1f17SGlen Barber mount /dev/${mddev} ${DESTDIR} 661e7c1f17SGlen Barber 671e7c1f17SGlen Barber return 0 681e7c1f17SGlen Barber} 691e7c1f17SGlen Barber 70*25c11557SColin Percivalvm_copy_base() { 71*25c11557SColin Percival # Creates a new UFS root filesystem and copies the contents of the 72*25c11557SColin Percival # current root filesystem into it. This produces a "clean" disk 73*25c11557SColin Percival # image without any remnants of files which were created temporarily 74*25c11557SColin Percival # during image-creation and have since been deleted (e.g., downloaded 75*25c11557SColin Percival # package archives). 76*25c11557SColin Percival 77*25c11557SColin Percival mkdir -p ${DESTDIR}/old 78*25c11557SColin Percival mdold=$(mdconfig -f ${VMBASE}) 79*25c11557SColin Percival mount /dev/${mdold} ${DESTDIR}/old 80*25c11557SColin Percival 81*25c11557SColin Percival truncate -s ${VMSIZE} ${VMBASE}.tmp 82*25c11557SColin Percival mkdir -p ${DESTDIR}/new 83*25c11557SColin Percival mdnew=$(mdconfig -f ${VMBASE}.tmp) 84*25c11557SColin Percival newfs -j /dev/${mdnew} 85*25c11557SColin Percival mount /dev/${mdnew} ${DESTDIR}/new 86*25c11557SColin Percival 87*25c11557SColin Percival tar -cf- -C ${DESTDIR}/old . | tar -xf- -C ${DESTDIR}/new 88*25c11557SColin Percival 89*25c11557SColin Percival umount /dev/${mdold} 90*25c11557SColin Percival rmdir ${DESTDIR}/old 91*25c11557SColin Percival mdconfig -d -u ${mdold} 92*25c11557SColin Percival 93*25c11557SColin Percival umount /dev/${mdnew} 94*25c11557SColin Percival rmdir ${DESTDIR}/new 95*25c11557SColin Percival mdconfig -d -u ${mdnew} 96*25c11557SColin Percival mv ${VMBASE}.tmp ${VMBASE} 97*25c11557SColin Percival} 98*25c11557SColin Percival 991e7c1f17SGlen Barbervm_install_base() { 1001e7c1f17SGlen Barber # Installs the FreeBSD userland/kernel to the virtual machine disk. 1011e7c1f17SGlen Barber 1021e7c1f17SGlen Barber cd ${WORLDDIR} && \ 1031e7c1f17SGlen Barber make DESTDIR=${DESTDIR} \ 1041e7c1f17SGlen Barber installworld installkernel distribution || \ 1051e7c1f17SGlen Barber err "\n\nCannot install the base system to ${DESTDIR}." 1061e7c1f17SGlen Barber 1071e7c1f17SGlen Barber echo '# Custom /etc/fstab for FreeBSD VM images' \ 1081e7c1f17SGlen Barber > ${DESTDIR}/etc/fstab 1091e7c1f17SGlen Barber echo '/dev/gpt/rootfs / ufs rw 1 1' \ 1101e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 1114e7bc9f0SColin Percival if [ -z "${NOSWAP}" ]; then 1121e7c1f17SGlen Barber echo '/dev/gpt/swapfs none swap sw 0 0' \ 1131e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 1144e7bc9f0SColin Percival fi 1151e7c1f17SGlen Barber 116e6f3489cSGlen Barber mkdir -p ${DESTDIR}/dev 117e6f3489cSGlen Barber mount -t devfs devfs ${DESTDIR}/dev 1181e7c1f17SGlen Barber chroot ${DESTDIR} /usr/bin/newaliases 1191e7c1f17SGlen Barber chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart 120e6f3489cSGlen Barber umount ${DESTDIR}/dev 1211e7c1f17SGlen Barber 12230a20c89SGlen Barber cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf 12330a20c89SGlen Barber 1241e7c1f17SGlen Barber return 0 1251e7c1f17SGlen Barber} 1261e7c1f17SGlen Barber 1271e7c1f17SGlen Barbervm_extra_install_base() { 1281e7c1f17SGlen Barber # Prototype. When overridden, runs extra post-installworld commands 1291e7c1f17SGlen Barber # as needed, based on the target virtual machine image or cloud 1301e7c1f17SGlen Barber # provider image target. 1311e7c1f17SGlen Barber 1321e7c1f17SGlen Barber return 0 1331e7c1f17SGlen Barber} 1341e7c1f17SGlen Barber 1351e7c1f17SGlen Barbervm_extra_enable_services() { 1361e7c1f17SGlen Barber if [ ! -z "${VM_RC_LIST}" ]; then 1371e7c1f17SGlen Barber for _rcvar in ${VM_RC_LIST}; do 1381e7c1f17SGlen Barber echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf 1391e7c1f17SGlen Barber done 1401e7c1f17SGlen Barber fi 1411e7c1f17SGlen Barber 1421e7c1f17SGlen Barber return 0 1431e7c1f17SGlen Barber} 1441e7c1f17SGlen Barber 1451e7c1f17SGlen Barbervm_extra_install_packages() { 146a4900ff5SGlen Barber if [ -z "${VM_EXTRA_PACKAGES}" ]; then 147a4900ff5SGlen Barber return 0 148a4900ff5SGlen Barber fi 14930a20c89SGlen Barber mkdir -p ${DESTDIR}/dev 15030a20c89SGlen Barber mount -t devfs devfs ${DESTDIR}/dev 15130a20c89SGlen Barber chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 15230a20c89SGlen Barber /usr/sbin/pkg bootstrap -y 15330a20c89SGlen Barber chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 15430a20c89SGlen Barber /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES} 15530a20c89SGlen Barber umount ${DESTDIR}/dev 1561e7c1f17SGlen Barber 1571e7c1f17SGlen Barber return 0 1581e7c1f17SGlen Barber} 1591e7c1f17SGlen Barber 1601e7c1f17SGlen Barbervm_extra_install_ports() { 1611e7c1f17SGlen Barber # Prototype. When overridden, installs additional ports within the 1621e7c1f17SGlen Barber # virtual machine environment. 1631e7c1f17SGlen Barber 1641e7c1f17SGlen Barber return 0 1651e7c1f17SGlen Barber} 1661e7c1f17SGlen Barber 167a02bfc83SGlen Barbervm_extra_pre_umount() { 168a02bfc83SGlen Barber # Prototype. When overridden, installs additional ports within the 169a02bfc83SGlen Barber # virtual machine environment. 170a02bfc83SGlen Barber 1712d4ff62fSGlen Barber rm -f ${DESTDIR}/etc/resolv.conf 1722d4ff62fSGlen Barber return 0 1732d4ff62fSGlen Barber} 1742d4ff62fSGlen Barber 1752d4ff62fSGlen Barbervm_extra_pkg_rmcache() { 176a4900ff5SGlen Barber if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then 177a4900ff5SGlen Barber chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 178a4900ff5SGlen Barber /usr/local/sbin/pkg clean -y -a 179a4900ff5SGlen Barber fi 18030a20c89SGlen Barber 181a02bfc83SGlen Barber return 0 182a02bfc83SGlen Barber} 183a02bfc83SGlen Barber 1841e7c1f17SGlen Barbervm_umount_base() { 1851e7c1f17SGlen Barber i=0 1861e7c1f17SGlen Barber sync 1871e7c1f17SGlen Barber while ! umount ${DESTDIR}/dev ${DESTDIR}; do 1881e7c1f17SGlen Barber i=$(( $i + 1 )) 1891e7c1f17SGlen Barber if [ $i -ge 10 ]; then 1901e7c1f17SGlen Barber # This should never happen. But, it has happened. 1911e7c1f17SGlen Barber msg="Cannot umount(8) ${DESTDIR}\n" 1921e7c1f17SGlen Barber msg="${msg}Something has gone horribly wrong." 1931e7c1f17SGlen Barber err "${msg}" 1941e7c1f17SGlen Barber fi 1951e7c1f17SGlen Barber sleep 1 1961e7c1f17SGlen Barber done 1971e7c1f17SGlen Barber 1981e7c1f17SGlen Barber return 0 1991e7c1f17SGlen Barber} 2001e7c1f17SGlen Barber 2011e7c1f17SGlen Barbervm_create_disk() { 2021e7c1f17SGlen Barber echo "Creating image... Please wait." 2031e7c1f17SGlen Barber echo 2041e7c1f17SGlen Barber 205efeb11a7SGlen Barber write_partition_layout || return 1 2061e7c1f17SGlen Barber 2071e7c1f17SGlen Barber return 0 2081e7c1f17SGlen Barber} 2091e7c1f17SGlen Barber 2101e7c1f17SGlen Barbervm_extra_create_disk() { 2111e7c1f17SGlen Barber 2121e7c1f17SGlen Barber return 0 2131e7c1f17SGlen Barber} 2141e7c1f17SGlen Barber 215