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 701e7c1f17SGlen Barbervm_install_base() { 711e7c1f17SGlen Barber # Installs the FreeBSD userland/kernel to the virtual machine disk. 721e7c1f17SGlen Barber 731e7c1f17SGlen Barber cd ${WORLDDIR} && \ 741e7c1f17SGlen Barber make DESTDIR=${DESTDIR} \ 751e7c1f17SGlen Barber installworld installkernel distribution || \ 761e7c1f17SGlen Barber err "\n\nCannot install the base system to ${DESTDIR}." 771e7c1f17SGlen Barber 781e7c1f17SGlen Barber echo '# Custom /etc/fstab for FreeBSD VM images' \ 791e7c1f17SGlen Barber > ${DESTDIR}/etc/fstab 801e7c1f17SGlen Barber echo '/dev/gpt/rootfs / ufs rw 1 1' \ 811e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 824e7bc9f0SColin Percival if [ -z "${NOSWAP}" ]; then 831e7c1f17SGlen Barber echo '/dev/gpt/swapfs none swap sw 0 0' \ 841e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 854e7bc9f0SColin Percival fi 861e7c1f17SGlen Barber 87e6f3489cSGlen Barber mkdir -p ${DESTDIR}/dev 88e6f3489cSGlen Barber mount -t devfs devfs ${DESTDIR}/dev 891e7c1f17SGlen Barber chroot ${DESTDIR} /usr/bin/newaliases 901e7c1f17SGlen Barber chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart 91e6f3489cSGlen Barber umount ${DESTDIR}/dev 921e7c1f17SGlen Barber 9330a20c89SGlen Barber cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf 9430a20c89SGlen Barber 951e7c1f17SGlen Barber return 0 961e7c1f17SGlen Barber} 971e7c1f17SGlen Barber 981e7c1f17SGlen Barbervm_extra_install_base() { 991e7c1f17SGlen Barber # Prototype. When overridden, runs extra post-installworld commands 1001e7c1f17SGlen Barber # as needed, based on the target virtual machine image or cloud 1011e7c1f17SGlen Barber # provider image target. 1021e7c1f17SGlen Barber 1031e7c1f17SGlen Barber return 0 1041e7c1f17SGlen Barber} 1051e7c1f17SGlen Barber 1061e7c1f17SGlen Barbervm_extra_enable_services() { 1071e7c1f17SGlen Barber if [ ! -z "${VM_RC_LIST}" ]; then 1081e7c1f17SGlen Barber for _rcvar in ${VM_RC_LIST}; do 1091e7c1f17SGlen Barber echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf 1101e7c1f17SGlen Barber done 1111e7c1f17SGlen Barber fi 1121e7c1f17SGlen Barber 1131e7c1f17SGlen Barber return 0 1141e7c1f17SGlen Barber} 1151e7c1f17SGlen Barber 1161e7c1f17SGlen Barbervm_extra_install_packages() { 117a4900ff5SGlen Barber if [ -z "${VM_EXTRA_PACKAGES}" ]; then 118a4900ff5SGlen Barber return 0 119a4900ff5SGlen Barber fi 12030a20c89SGlen Barber mkdir -p ${DESTDIR}/dev 12130a20c89SGlen Barber mount -t devfs devfs ${DESTDIR}/dev 12230a20c89SGlen Barber chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 12330a20c89SGlen Barber /usr/sbin/pkg bootstrap -y 12430a20c89SGlen Barber chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 12530a20c89SGlen Barber /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES} 12630a20c89SGlen Barber umount ${DESTDIR}/dev 1271e7c1f17SGlen Barber 1281e7c1f17SGlen Barber return 0 1291e7c1f17SGlen Barber} 1301e7c1f17SGlen Barber 1311e7c1f17SGlen Barbervm_extra_install_ports() { 1321e7c1f17SGlen Barber # Prototype. When overridden, installs additional ports within the 1331e7c1f17SGlen Barber # virtual machine environment. 1341e7c1f17SGlen Barber 1351e7c1f17SGlen Barber return 0 1361e7c1f17SGlen Barber} 1371e7c1f17SGlen Barber 138a02bfc83SGlen Barbervm_extra_pre_umount() { 139a02bfc83SGlen Barber # Prototype. When overridden, installs additional ports within the 140a02bfc83SGlen Barber # virtual machine environment. 141a02bfc83SGlen Barber 142*2d4ff62fSGlen Barber rm -f ${DESTDIR}/etc/resolv.conf 143*2d4ff62fSGlen Barber return 0 144*2d4ff62fSGlen Barber} 145*2d4ff62fSGlen Barber 146*2d4ff62fSGlen Barbervm_extra_pkg_rmcache() { 147a4900ff5SGlen Barber if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then 148a4900ff5SGlen Barber chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 149a4900ff5SGlen Barber /usr/local/sbin/pkg clean -y -a 150a4900ff5SGlen Barber fi 15130a20c89SGlen Barber 152a02bfc83SGlen Barber return 0 153a02bfc83SGlen Barber} 154a02bfc83SGlen Barber 1551e7c1f17SGlen Barbervm_umount_base() { 1561e7c1f17SGlen Barber i=0 1571e7c1f17SGlen Barber sync 1581e7c1f17SGlen Barber while ! umount ${DESTDIR}/dev ${DESTDIR}; do 1591e7c1f17SGlen Barber i=$(( $i + 1 )) 1601e7c1f17SGlen Barber if [ $i -ge 10 ]; then 1611e7c1f17SGlen Barber # This should never happen. But, it has happened. 1621e7c1f17SGlen Barber msg="Cannot umount(8) ${DESTDIR}\n" 1631e7c1f17SGlen Barber msg="${msg}Something has gone horribly wrong." 1641e7c1f17SGlen Barber err "${msg}" 1651e7c1f17SGlen Barber fi 1661e7c1f17SGlen Barber sleep 1 1671e7c1f17SGlen Barber done 1681e7c1f17SGlen Barber 1691e7c1f17SGlen Barber return 0 1701e7c1f17SGlen Barber} 1711e7c1f17SGlen Barber 1721e7c1f17SGlen Barbervm_create_disk() { 1731e7c1f17SGlen Barber echo "Creating image... Please wait." 1741e7c1f17SGlen Barber echo 1751e7c1f17SGlen Barber 176efeb11a7SGlen Barber write_partition_layout || return 1 1771e7c1f17SGlen Barber 1781e7c1f17SGlen Barber return 0 1791e7c1f17SGlen Barber} 1801e7c1f17SGlen Barber 1811e7c1f17SGlen Barbervm_extra_create_disk() { 1821e7c1f17SGlen Barber 1831e7c1f17SGlen Barber return 0 1841e7c1f17SGlen Barber} 1851e7c1f17SGlen Barber 186