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() { 13efeb11a7SGlen Barber 14efeb11a7SGlen Barber case "${TARGET}:${TARGET_ARCH}" in 15efeb11a7SGlen Barber amd64:amd64 | i386:i386) 167d565d60SGlen Barber mkimg -s gpt -b /boot/pmbr \ 17efeb11a7SGlen Barber -p freebsd-boot/bootfs:=/boot/gptboot \ 18efeb11a7SGlen Barber -p freebsd-swap/swapfs::1G \ 1938d59ac7SGlen Barber -p freebsd-ufs/rootfs:=${VMBASE} \ 20efeb11a7SGlen Barber -o ${VMIMAGE} 21efeb11a7SGlen Barber ;; 22efeb11a7SGlen Barber powerpc:powerpc*) 237d565d60SGlen Barber mkimg -s apm \ 24efeb11a7SGlen Barber -p freebsd-boot/bootfs:=/boot/boot1.hfs \ 25efeb11a7SGlen Barber -p freebsd-swap/swapfs::1G \ 2638d59ac7SGlen Barber -p freebsd-ufs/rootfs:=${VMBASE} \ 27efeb11a7SGlen Barber -o ${VMIMAGE} 28efeb11a7SGlen Barber ;; 29efeb11a7SGlen Barber *) 30efeb11a7SGlen Barber # ENOTSUPP 31efeb11a7SGlen Barber return 1 32efeb11a7SGlen Barber ;; 33efeb11a7SGlen Barber esac 34efeb11a7SGlen Barber 35efeb11a7SGlen Barber return 0 36efeb11a7SGlen Barber} 371e7c1f17SGlen Barber 381e7c1f17SGlen Barbererr() { 391e7c1f17SGlen Barber printf "${@}\n" 401e7c1f17SGlen Barber cleanup 411e7c1f17SGlen Barber return 1 421e7c1f17SGlen Barber} 431e7c1f17SGlen Barber 441e7c1f17SGlen Barbercleanup() { 451e7c1f17SGlen Barber if [ ! -z "${mddev}" ]; then 461e7c1f17SGlen Barber mdconfig -d -u ${mddev} 471e7c1f17SGlen Barber fi 481e7c1f17SGlen Barber umount ${DESTDIR}/dev 491e7c1f17SGlen Barber umount ${DESTDIR} 501e7c1f17SGlen Barber 511e7c1f17SGlen Barber return 0 521e7c1f17SGlen Barber} 531e7c1f17SGlen Barber 541e7c1f17SGlen Barbervm_create_base() { 551e7c1f17SGlen Barber # Creates the UFS root filesystem for the virtual machine disk, 561e7c1f17SGlen Barber # written to the formatted disk image with mkimg(1). 571e7c1f17SGlen Barber 581e7c1f17SGlen Barber mkdir -p ${DESTDIR} 591e7c1f17SGlen Barber truncate -s ${VMSIZE} ${VMBASE} 601e7c1f17SGlen Barber mddev=$(mdconfig -f ${VMBASE}) 611e7c1f17SGlen Barber newfs -j /dev/${mddev} 621e7c1f17SGlen Barber mount /dev/${mddev} ${DESTDIR} 631e7c1f17SGlen Barber 641e7c1f17SGlen Barber return 0 651e7c1f17SGlen Barber} 661e7c1f17SGlen Barber 671e7c1f17SGlen Barbervm_install_base() { 681e7c1f17SGlen Barber # Installs the FreeBSD userland/kernel to the virtual machine disk. 691e7c1f17SGlen Barber 701e7c1f17SGlen Barber cd ${WORLDDIR} && \ 711e7c1f17SGlen Barber make DESTDIR=${DESTDIR} \ 721e7c1f17SGlen Barber installworld installkernel distribution || \ 731e7c1f17SGlen Barber err "\n\nCannot install the base system to ${DESTDIR}." 741e7c1f17SGlen Barber 751e7c1f17SGlen Barber echo '# Custom /etc/fstab for FreeBSD VM images' \ 761e7c1f17SGlen Barber > ${DESTDIR}/etc/fstab 771e7c1f17SGlen Barber echo '/dev/gpt/rootfs / ufs rw 1 1' \ 781e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 791e7c1f17SGlen Barber echo '/dev/gpt/swapfs none swap sw 0 0' \ 801e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 811e7c1f17SGlen Barber 82*e6f3489cSGlen Barber mkdir -p ${DESTDIR}/dev 83*e6f3489cSGlen Barber mount -t devfs devfs ${DESTDIR}/dev 841e7c1f17SGlen Barber chroot ${DESTDIR} /usr/bin/newaliases 851e7c1f17SGlen Barber chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart 86*e6f3489cSGlen Barber umount ${DESTDIR}/dev 871e7c1f17SGlen Barber 881e7c1f17SGlen Barber return 0 891e7c1f17SGlen Barber} 901e7c1f17SGlen Barber 911e7c1f17SGlen Barbervm_extra_install_base() { 921e7c1f17SGlen Barber # Prototype. When overridden, runs extra post-installworld commands 931e7c1f17SGlen Barber # as needed, based on the target virtual machine image or cloud 941e7c1f17SGlen Barber # provider image target. 951e7c1f17SGlen Barber 961e7c1f17SGlen Barber return 0 971e7c1f17SGlen Barber} 981e7c1f17SGlen Barber 991e7c1f17SGlen Barbervm_extra_enable_services() { 1001e7c1f17SGlen Barber if [ ! -z "${VM_RC_LIST}" ]; then 1011e7c1f17SGlen Barber for _rcvar in ${VM_RC_LIST}; do 1021e7c1f17SGlen Barber echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf 1031e7c1f17SGlen Barber done 1041e7c1f17SGlen Barber fi 1051e7c1f17SGlen Barber 1061e7c1f17SGlen Barber return 0 1071e7c1f17SGlen Barber} 1081e7c1f17SGlen Barber 1091e7c1f17SGlen Barbervm_extra_install_packages() { 110*e6f3489cSGlen Barber mkdir -p ${DESTDIR}/dev 111*e6f3489cSGlen Barber mount -t devfs devfs ${DESTDIR}/dev 1121e7c1f17SGlen Barber chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 1131e7c1f17SGlen Barber /usr/sbin/pkg bootstrap -y 1141e7c1f17SGlen Barber if [ ! -z "${VM_EXTRA_PACKAGES}" ]; then 1151e7c1f17SGlen Barber chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 1161e7c1f17SGlen Barber /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES} 1171e7c1f17SGlen Barber fi 118*e6f3489cSGlen Barber umount ${DESTDIR}/dev 1191e7c1f17SGlen Barber 1201e7c1f17SGlen Barber return 0 1211e7c1f17SGlen Barber} 1221e7c1f17SGlen Barber 1231e7c1f17SGlen Barbervm_extra_install_ports() { 1241e7c1f17SGlen Barber # Prototype. When overridden, installs additional ports within the 1251e7c1f17SGlen Barber # virtual machine environment. 1261e7c1f17SGlen Barber 1271e7c1f17SGlen Barber return 0 1281e7c1f17SGlen Barber} 1291e7c1f17SGlen Barber 130a02bfc83SGlen Barbervm_extra_pre_umount() { 131a02bfc83SGlen Barber # Prototype. When overridden, installs additional ports within the 132a02bfc83SGlen Barber # virtual machine environment. 133a02bfc83SGlen Barber 134a02bfc83SGlen Barber return 0 135a02bfc83SGlen Barber} 136a02bfc83SGlen Barber 1371e7c1f17SGlen Barbervm_umount_base() { 1381e7c1f17SGlen Barber i=0 1391e7c1f17SGlen Barber sync 1401e7c1f17SGlen Barber while ! umount ${DESTDIR}/dev ${DESTDIR}; do 1411e7c1f17SGlen Barber i=$(( $i + 1 )) 1421e7c1f17SGlen Barber if [ $i -ge 10 ]; then 1431e7c1f17SGlen Barber # This should never happen. But, it has happened. 1441e7c1f17SGlen Barber msg="Cannot umount(8) ${DESTDIR}\n" 1451e7c1f17SGlen Barber msg="${msg}Something has gone horribly wrong." 1461e7c1f17SGlen Barber err "${msg}" 1471e7c1f17SGlen Barber fi 1481e7c1f17SGlen Barber sleep 1 1491e7c1f17SGlen Barber done 1501e7c1f17SGlen Barber 1511e7c1f17SGlen Barber return 0 1521e7c1f17SGlen Barber} 1531e7c1f17SGlen Barber 1541e7c1f17SGlen Barbervm_create_disk() { 1551e7c1f17SGlen Barber echo "Creating image... Please wait." 1561e7c1f17SGlen Barber echo 1571e7c1f17SGlen Barber 158efeb11a7SGlen Barber write_partition_layout || return 1 1591e7c1f17SGlen Barber 1601e7c1f17SGlen Barber return 0 1611e7c1f17SGlen Barber} 1621e7c1f17SGlen Barber 1631e7c1f17SGlen Barbervm_extra_create_disk() { 1641e7c1f17SGlen Barber 1651e7c1f17SGlen Barber return 0 1661e7c1f17SGlen Barber} 1671e7c1f17SGlen Barber 168