1*1e7c1f17SGlen Barber#!/bin/sh 2*1e7c1f17SGlen Barber# 3*1e7c1f17SGlen Barber# $FreeBSD$ 4*1e7c1f17SGlen Barber# 5*1e7c1f17SGlen Barber# 6*1e7c1f17SGlen Barber# Common functions for virtual machine image build scripts. 7*1e7c1f17SGlen Barber# 8*1e7c1f17SGlen Barber 9*1e7c1f17SGlen Barberexport PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin" 10*1e7c1f17SGlen Barbertrap "cleanup" INT QUIT TRAP ABRT TERM 11*1e7c1f17SGlen Barber 12*1e7c1f17SGlen Barbermkimg_bootcode="/boot/pmbr" 13*1e7c1f17SGlen Barbermkimg_partitions="-p freebsd-boot/bootfs:=/boot/gptboot" 14*1e7c1f17SGlen Barbermkimg_partitions="${mkimg_partitions} -p freebsd-swap/swapfs::1G" 15*1e7c1f17SGlen Barbermkimg_partitions="${mkimg_partitions} freebsd-ufs/rootfs:=${VMBASE}" 16*1e7c1f17SGlen Barber 17*1e7c1f17SGlen Barberusage() { 18*1e7c1f17SGlen Barber echo "${0} usage:" 19*1e7c1f17SGlen Barber echo "${@}" 20*1e7c1f17SGlen Barber return 1 21*1e7c1f17SGlen Barber} 22*1e7c1f17SGlen Barber 23*1e7c1f17SGlen Barbererr() { 24*1e7c1f17SGlen Barber printf "${@}\n" 25*1e7c1f17SGlen Barber cleanup 26*1e7c1f17SGlen Barber return 1 27*1e7c1f17SGlen Barber} 28*1e7c1f17SGlen Barber 29*1e7c1f17SGlen Barbercleanup() { 30*1e7c1f17SGlen Barber if [ ! -z "${mddev}" ]; then 31*1e7c1f17SGlen Barber mdconfig -d -u ${mddev} 32*1e7c1f17SGlen Barber fi 33*1e7c1f17SGlen Barber umount ${DESTDIR}/dev 34*1e7c1f17SGlen Barber umount ${DESTDIR} 35*1e7c1f17SGlen Barber 36*1e7c1f17SGlen Barber return 0 37*1e7c1f17SGlen Barber} 38*1e7c1f17SGlen Barber 39*1e7c1f17SGlen Barbervm_create_base() { 40*1e7c1f17SGlen Barber # Creates the UFS root filesystem for the virtual machine disk, 41*1e7c1f17SGlen Barber # written to the formatted disk image with mkimg(1). 42*1e7c1f17SGlen Barber 43*1e7c1f17SGlen Barber mkdir -p ${DESTDIR} 44*1e7c1f17SGlen Barber truncate -s ${VMSIZE} ${VMBASE} 45*1e7c1f17SGlen Barber mddev=$(mdconfig -f ${VMBASE}) 46*1e7c1f17SGlen Barber newfs -j /dev/${mddev} 47*1e7c1f17SGlen Barber mount /dev/${mddev} ${DESTDIR} 48*1e7c1f17SGlen Barber 49*1e7c1f17SGlen Barber return 0 50*1e7c1f17SGlen Barber} 51*1e7c1f17SGlen Barber 52*1e7c1f17SGlen Barbervm_install_base() { 53*1e7c1f17SGlen Barber # Installs the FreeBSD userland/kernel to the virtual machine disk. 54*1e7c1f17SGlen Barber 55*1e7c1f17SGlen Barber cd ${WORLDDIR} && \ 56*1e7c1f17SGlen Barber make DESTDIR=${DESTDIR} \ 57*1e7c1f17SGlen Barber installworld installkernel distribution || \ 58*1e7c1f17SGlen Barber err "\n\nCannot install the base system to ${DESTDIR}." 59*1e7c1f17SGlen Barber 60*1e7c1f17SGlen Barber echo '# Custom /etc/fstab for FreeBSD VM images' \ 61*1e7c1f17SGlen Barber > ${DESTDIR}/etc/fstab 62*1e7c1f17SGlen Barber echo '/dev/gpt/rootfs / ufs rw 1 1' \ 63*1e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 64*1e7c1f17SGlen Barber echo '/dev/gpt/swapfs none swap sw 0 0' \ 65*1e7c1f17SGlen Barber >> ${DESTDIR}/etc/fstab 66*1e7c1f17SGlen Barber 67*1e7c1f17SGlen Barber chroot ${DESTDIR} /usr/bin/newaliases 68*1e7c1f17SGlen Barber chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart 69*1e7c1f17SGlen Barber 70*1e7c1f17SGlen Barber return 0 71*1e7c1f17SGlen Barber} 72*1e7c1f17SGlen Barber 73*1e7c1f17SGlen Barbervm_extra_install_base() { 74*1e7c1f17SGlen Barber # Prototype. When overridden, runs extra post-installworld commands 75*1e7c1f17SGlen Barber # as needed, based on the target virtual machine image or cloud 76*1e7c1f17SGlen Barber # provider image target. 77*1e7c1f17SGlen Barber 78*1e7c1f17SGlen Barber return 0 79*1e7c1f17SGlen Barber} 80*1e7c1f17SGlen Barber 81*1e7c1f17SGlen Barbervm_extra_enable_services() { 82*1e7c1f17SGlen Barber if [ ! -z "${VM_RC_LIST}" ]; then 83*1e7c1f17SGlen Barber for _rcvar in ${VM_RC_LIST}; do 84*1e7c1f17SGlen Barber echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf 85*1e7c1f17SGlen Barber done 86*1e7c1f17SGlen Barber fi 87*1e7c1f17SGlen Barber 88*1e7c1f17SGlen Barber return 0 89*1e7c1f17SGlen Barber} 90*1e7c1f17SGlen Barber 91*1e7c1f17SGlen Barbervm_extra_install_packages() { 92*1e7c1f17SGlen Barber chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 93*1e7c1f17SGlen Barber /usr/sbin/pkg bootstrap -y 94*1e7c1f17SGlen Barber if [ ! -z "${VM_EXTRA_PACKAGES}" ]; then 95*1e7c1f17SGlen Barber chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 96*1e7c1f17SGlen Barber /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES} 97*1e7c1f17SGlen Barber fi 98*1e7c1f17SGlen Barber 99*1e7c1f17SGlen Barber return 0 100*1e7c1f17SGlen Barber} 101*1e7c1f17SGlen Barber 102*1e7c1f17SGlen Barbervm_extra_install_ports() { 103*1e7c1f17SGlen Barber # Prototype. When overridden, installs additional ports within the 104*1e7c1f17SGlen Barber # virtual machine environment. 105*1e7c1f17SGlen Barber 106*1e7c1f17SGlen Barber return 0 107*1e7c1f17SGlen Barber} 108*1e7c1f17SGlen Barber 109*1e7c1f17SGlen Barbervm_umount_base() { 110*1e7c1f17SGlen Barber i=0 111*1e7c1f17SGlen Barber sync 112*1e7c1f17SGlen Barber while ! umount ${DESTDIR}/dev ${DESTDIR}; do 113*1e7c1f17SGlen Barber i=$(( $i + 1 )) 114*1e7c1f17SGlen Barber if [ $i -ge 10 ]; then 115*1e7c1f17SGlen Barber # This should never happen. But, it has happened. 116*1e7c1f17SGlen Barber msg="Cannot umount(8) ${DESTDIR}\n" 117*1e7c1f17SGlen Barber msg="${msg}Something has gone horribly wrong." 118*1e7c1f17SGlen Barber err "${msg}" 119*1e7c1f17SGlen Barber fi 120*1e7c1f17SGlen Barber sleep 1 121*1e7c1f17SGlen Barber done 122*1e7c1f17SGlen Barber 123*1e7c1f17SGlen Barber return 0 124*1e7c1f17SGlen Barber} 125*1e7c1f17SGlen Barber 126*1e7c1f17SGlen Barbervm_create_disk() { 127*1e7c1f17SGlen Barber if [ -z "${mkimg_paritions}" ]; then 128*1e7c1f17SGlen Barber err "No partition types specified. Skipping." 129*1e7c1f17SGlen Barber return 1 130*1e7c1f17SGlen Barber fi 131*1e7c1f17SGlen Barber echo "Creating image... Please wait." 132*1e7c1f17SGlen Barber echo 133*1e7c1f17SGlen Barber mkimg -f ${mkimg_format} -s ${mkimg_scheme} \ 134*1e7c1f17SGlen Barber ${mkimg_bootcode} \ 135*1e7c1f17SGlen Barber ${mkimg_partitions} \ 136*1e7c1f17SGlen Barber ${mkimg_outfile} 137*1e7c1f17SGlen Barber 138*1e7c1f17SGlen Barber mkimg -b /boot/pmbr -p freebsd-boot/bootfs:=/boot/gptboot \ 139*1e7c1f17SGlen Barber -p freebsd-swap/swapfs::1G \ 140*1e7c1f17SGlen Barber -p freebsd-ufs/rootfs:=${VMBASE} \ 141*1e7c1f17SGlen Barber -o ${VMIMAGE}.raw 142*1e7c1f17SGlen Barber 143*1e7c1f17SGlen Barber return 0 144*1e7c1f17SGlen Barber} 145*1e7c1f17SGlen Barber 146*1e7c1f17SGlen Barbervm_extra_create_disk() { 147*1e7c1f17SGlen Barber 148*1e7c1f17SGlen Barber return 0 149*1e7c1f17SGlen Barber} 150*1e7c1f17SGlen Barber 151