1#!/bin/sh 2# 3# $FreeBSD$ 4# 5# 6# Common functions for virtual machine image build scripts. 7# 8 9scriptdir=$(dirname $(realpath $0)) 10. ${scriptdir}/../../tools/boot/install-boot.sh 11 12export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin" 13trap "cleanup" INT QUIT TRAP ABRT TERM 14 15write_partition_layout() { 16 if [ -z "${NOSWAP}" ]; then 17 SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}" 18 fi 19 20 BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \ 21 WITH_UNIFIED_OBJDIR=yes \ 22 make -C ${WORLDDIR}/stand -V .OBJDIR)" 23 BOOTFILES="$(realpath ${BOOTFILES})" 24 25 case "${TARGET}:${TARGET_ARCH}" in 26 amd64:amd64 | i386:i386) 27 mkimg -s gpt -f ${VMFORMAT} \ 28 -b ${BOOTFILES}/i386/pmbr/pmbr \ 29 -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot \ 30 ${SWAPOPT} \ 31 -p freebsd-ufs/rootfs:=${VMBASE} \ 32 -o ${VMIMAGE} 33 ;; 34 arm64:aarch64) 35 # Create an ESP 36 espfilename=$(mktemp /tmp/efiboot.XXXXXX) 37 make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi 38 mkimg -s mbr -f ${VMFORMAT} \ 39 -p efi:=${espfilename} \ 40 -p freebsd:=${VMBASE} \ 41 -o ${VMIMAGE} 42 rm ${espfilename} 43 ;; 44 powerpc:powerpc*) 45 mkimg -s apm -f ${VMFORMAT} \ 46 -p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \ 47 ${SWAPOPT} \ 48 -p freebsd-ufs/rootfs:=${VMBASE} \ 49 -o ${VMIMAGE} 50 ;; 51 *) 52 # ENOTSUPP 53 return 1 54 ;; 55 esac 56 57 return 0 58} 59 60err() { 61 printf "${@}\n" 62 cleanup 63 return 1 64} 65 66cleanup() { 67 if [ -c "${DESTDIR}/dev/null" ]; then 68 umount_loop ${DESTDIR}/dev 2>/dev/null 69 fi 70 umount_loop ${DESTDIR} 71 if [ ! -z "${mddev}" ]; then 72 mdconfig -d -u ${mddev} 73 fi 74 75 return 0 76} 77 78vm_create_base() { 79 # Creates the UFS root filesystem for the virtual machine disk, 80 # written to the formatted disk image with mkimg(1). 81 82 mkdir -p ${DESTDIR} 83 truncate -s ${VMSIZE} ${VMBASE} 84 mddev=$(mdconfig -f ${VMBASE}) 85 newfs -L rootfs /dev/${mddev} 86 mount /dev/${mddev} ${DESTDIR} 87 88 return 0 89} 90 91vm_copy_base() { 92 # Creates a new UFS root filesystem and copies the contents of the 93 # current root filesystem into it. This produces a "clean" disk 94 # image without any remnants of files which were created temporarily 95 # during image-creation and have since been deleted (e.g., downloaded 96 # package archives). 97 98 mkdir -p ${DESTDIR}/old 99 mdold=$(mdconfig -f ${VMBASE}) 100 mount /dev/${mdold} ${DESTDIR}/old 101 102 truncate -s ${VMSIZE} ${VMBASE}.tmp 103 mkdir -p ${DESTDIR}/new 104 mdnew=$(mdconfig -f ${VMBASE}.tmp) 105 newfs -L rootfs /dev/${mdnew} 106 mount /dev/${mdnew} ${DESTDIR}/new 107 108 tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new 109 110 umount_loop /dev/${mdold} 111 rmdir ${DESTDIR}/old 112 mdconfig -d -u ${mdold} 113 114 umount_loop /dev/${mdnew} 115 rmdir ${DESTDIR}/new 116 tunefs -n enable /dev/${mdnew} 117 mdconfig -d -u ${mdnew} 118 mv ${VMBASE}.tmp ${VMBASE} 119} 120 121vm_install_base() { 122 # Installs the FreeBSD userland/kernel to the virtual machine disk. 123 124 cd ${WORLDDIR} && \ 125 make DESTDIR=${DESTDIR} \ 126 installworld installkernel distribution || \ 127 err "\n\nCannot install the base system to ${DESTDIR}." 128 129 # Bootstrap etcupdate(8) and mergemaster(8) databases. 130 mkdir -p ${DESTDIR}/var/db/etcupdate 131 etcupdate extract -B \ 132 -M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \ 133 -s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate 134 sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \ 135 -F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \ 136 -D ${DESTDIR} 137 138 echo '# Custom /etc/fstab for FreeBSD VM images' \ 139 > ${DESTDIR}/etc/fstab 140 echo "/dev/${ROOTLABEL}/rootfs / ufs rw 1 1" \ 141 >> ${DESTDIR}/etc/fstab 142 if [ -z "${NOSWAP}" ]; then 143 echo '/dev/gpt/swapfs none swap sw 0 0' \ 144 >> ${DESTDIR}/etc/fstab 145 fi 146 147 local hostname 148 hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')" 149 echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf 150 151 if ! [ -z "${QEMUSTATIC}" ]; then 152 export EMULATOR=/qemu 153 cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR} 154 fi 155 156 mkdir -p ${DESTDIR}/dev 157 mount -t devfs devfs ${DESTDIR}/dev 158 chroot ${DESTDIR} ${EMULATOR} /usr/bin/newaliases 159 chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart 160 umount_loop ${DESTDIR}/dev 161 162 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf 163 164 return 0 165} 166 167vm_extra_install_base() { 168 # Prototype. When overridden, runs extra post-installworld commands 169 # as needed, based on the target virtual machine image or cloud 170 # provider image target. 171 172 return 0 173} 174 175vm_extra_enable_services() { 176 if [ ! -z "${VM_RC_LIST}" ]; then 177 for _rcvar in ${VM_RC_LIST}; do 178 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf 179 done 180 fi 181 182 if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then 183 echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \ 184 ${DESTDIR}/etc/rc.conf 185 fi 186 187 return 0 188} 189 190vm_extra_install_packages() { 191 if [ -z "${VM_EXTRA_PACKAGES}" ]; then 192 return 0 193 fi 194 mkdir -p ${DESTDIR}/dev 195 mount -t devfs devfs ${DESTDIR}/dev 196 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 197 /usr/sbin/pkg bootstrap -y 198 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 199 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES} 200 umount_loop ${DESTDIR}/dev 201 202 return 0 203} 204 205vm_extra_install_ports() { 206 # Prototype. When overridden, installs additional ports within the 207 # virtual machine environment. 208 209 return 0 210} 211 212vm_extra_pre_umount() { 213 # Prototype. When overridden, performs additional tasks within the 214 # virtual machine environment prior to unmounting the filesystem. 215 # Note: When overriding this function, removing resolv.conf in the 216 # disk image must be included. 217 218 if ! [ -z "${QEMUSTATIC}" ]; then 219 rm -f ${DESTDIR}/${EMULATOR} 220 fi 221 rm -f ${DESTDIR}/etc/resolv.conf 222 return 0 223} 224 225vm_extra_pkg_rmcache() { 226 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then 227 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 228 /usr/local/sbin/pkg clean -y -a 229 fi 230 231 return 0 232} 233 234umount_loop() { 235 DIR=$1 236 i=0 237 sync 238 while ! umount ${DIR}; do 239 i=$(( $i + 1 )) 240 if [ $i -ge 10 ]; then 241 # This should never happen. But, it has happened. 242 echo "Cannot umount(8) ${DIR}" 243 echo "Something has gone horribly wrong." 244 return 1 245 fi 246 sleep 1 247 done 248 249 return 0 250} 251 252vm_create_disk() { 253 echo "Creating image... Please wait." 254 echo 255 256 write_partition_layout || return 1 257 258 return 0 259} 260 261vm_extra_create_disk() { 262 263 return 0 264} 265 266