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