1#!/bin/sh 2# 3# 4# 5# Common functions for virtual machine image build scripts. 6# 7 8scriptdir=$(dirname $(realpath $0)) 9. ${scriptdir}/../../tools/boot/install-boot.sh 10 11export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin" 12trap "cleanup" INT QUIT TRAP ABRT TERM 13 14# Platform-specific large-scale setup 15# Most platforms use GPT, so put that as default, then special cases 16PARTSCHEME=gpt 17ROOTLABEL="gpt" 18case "${TARGET}:${TARGET_ARCH}" in 19 powerpc:powerpc*) 20 PARTSCHEME=mbr 21 ROOTLABEL="ufs" 22 NOSWAP=yes # Can't label swap partition with MBR, so no swap 23 ;; 24esac 25 26err() { 27 printf "${@}\n" 28 cleanup 29 return 1 30} 31 32cleanup() { 33 if [ -c "${DESTDIR}/dev/null" ]; then 34 umount_loop ${DESTDIR}/dev 2>/dev/null 35 fi 36 37 return 0 38} 39 40vm_create_base() { 41 42 mkdir -p ${DESTDIR} 43 44 return 0 45} 46 47vm_copy_base() { 48 # Defunct 49} 50 51vm_install_base() { 52 # Installs the FreeBSD userland/kernel to the virtual machine disk. 53 54 cd ${WORLDDIR} && \ 55 make DESTDIR=${DESTDIR} \ 56 installworld installkernel distribution || \ 57 err "\n\nCannot install the base system to ${DESTDIR}." 58 59 # Bootstrap etcupdate(8) database. 60 mkdir -p ${DESTDIR}/var/db/etcupdate 61 etcupdate extract -B \ 62 -M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \ 63 -s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate 64 65 echo '# Custom /etc/fstab for FreeBSD VM images' \ 66 > ${DESTDIR}/etc/fstab 67 if [ "${VMFS}" != zfs ]; then 68 echo "/dev/${ROOTLABEL}/rootfs / ${VMFS} rw 1 1" \ 69 >> ${DESTDIR}/etc/fstab 70 fi 71 if [ -z "${NOSWAP}" ]; then 72 echo '/dev/gpt/swapfs none swap sw 0 0' \ 73 >> ${DESTDIR}/etc/fstab 74 fi 75 76 local hostname 77 hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')" 78 echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf 79 if [ "${VMFS}" = zfs ]; then 80 echo "zfs_enable=\"YES\"" >> ${DESTDIR}/etc/rc.conf 81 echo "zpool_reguid=\"zroot\"" >> ${DESTDIR}/etc/rc.conf 82 echo "zpool_upgrade=\"zroot\"" >> ${DESTDIR}/etc/rc.conf 83 echo "kern.geom.label.disk_ident.enable=0" >> ${DESTDIR}/boot/loader.conf 84 echo "zfs_load=YES" >> ${DESTDIR}/boot/loader.conf 85 fi 86 87 return 0 88} 89 90vm_emulation_setup() { 91 if ! [ -z "${QEMUSTATIC}" ]; then 92 export EMULATOR=/qemu 93 cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR} 94 fi 95 96 mkdir -p ${DESTDIR}/dev 97 mount -t devfs devfs ${DESTDIR}/dev 98 chroot ${DESTDIR} ${EMULATOR} /usr/bin/newaliases 99 chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart 100 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf 101 102 return 0 103} 104 105vm_extra_install_base() { 106 # Prototype. When overridden, runs extra post-installworld commands 107 # as needed, based on the target virtual machine image or cloud 108 # provider image target. 109 110 return 0 111} 112 113vm_extra_enable_services() { 114 if [ ! -z "${VM_RC_LIST}" ]; then 115 for _rcvar in ${VM_RC_LIST}; do 116 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf 117 done 118 fi 119 120 if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then 121 echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \ 122 ${DESTDIR}/etc/rc.conf 123 # Expand the filesystem to fill the disk. 124 echo 'growfs_enable="YES"' >> ${DESTDIR}/etc/rc.conf 125 touch ${DESTDIR}/firstboot 126 fi 127 128 return 0 129} 130 131vm_extra_install_packages() { 132 if [ -z "${VM_EXTRA_PACKAGES}" ]; then 133 return 0 134 fi 135 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 136 /usr/sbin/pkg bootstrap -y 137 for p in ${VM_EXTRA_PACKAGES}; do 138 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 139 /usr/sbin/pkg install -y ${p} 140 done 141 142 return 0 143} 144 145vm_extra_install_ports() { 146 # Prototype. When overridden, installs additional ports within the 147 # virtual machine environment. 148 149 return 0 150} 151 152vm_extra_pre_umount() { 153 # Prototype. When overridden, performs additional tasks within the 154 # virtual machine environment prior to unmounting the filesystem. 155 156 return 0 157} 158 159vm_emulation_cleanup() { 160 if ! [ -z "${QEMUSTATIC}" ]; then 161 rm -f ${DESTDIR}/${EMULATOR} 162 fi 163 rm -f ${DESTDIR}/etc/resolv.conf 164 umount_loop ${DESTDIR}/dev 165 return 0 166} 167 168vm_extra_pkg_rmcache() { 169 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then 170 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 171 /usr/local/sbin/pkg clean -y -a 172 fi 173 174 return 0 175} 176 177buildfs() { 178 local md tmppool 179 180 case "${VMFS}" in 181 ufs) 182 makefs ${MAKEFSARGS} -o label=rootfs -o version=2 -o softupdates=1 \ 183 ${VMBASE} ${DESTDIR} 184 ;; 185 zfs) 186 makefs -t zfs ${MAKEFSARGS} \ 187 -o poolname=zroot -o bootfs=zroot/ROOT/default -o rootpath=/ \ 188 -o fs=zroot\;mountpoint=none \ 189 -o fs=zroot/ROOT\;mountpoint=none \ 190 -o fs=zroot/ROOT/default\;mountpoint=/ \ 191 -o fs=zroot/home\;mountpoint=/home \ 192 -o fs=zroot/tmp\;mountpoint=/tmp\;exec=on\;setuid=off \ 193 -o fs=zroot/usr\;mountpoint=/usr\;canmount=off \ 194 -o fs=zroot/usr/ports\;setuid=off \ 195 -o fs=zroot/usr/src \ 196 -o fs=zroot/usr/obj \ 197 -o fs=zroot/var\;mountpoint=/var\;canmount=off \ 198 -o fs=zroot/var/audit\;setuid=off\;exec=off \ 199 -o fs=zroot/var/crash\;setuid=off\;exec=off \ 200 -o fs=zroot/var/log\;setuid=off\;exec=off \ 201 -o fs=zroot/var/mail\;atime=on \ 202 -o fs=zroot/var/tmp\;setuid=off \ 203 ${VMBASE} ${DESTDIR} 204 ;; 205 *) 206 echo "Unexpected VMFS value '${VMFS}'" 207 exit 1 208 ;; 209 esac 210} 211 212umount_loop() { 213 DIR=$1 214 i=0 215 sync 216 while ! umount ${DIR}; do 217 i=$(( $i + 1 )) 218 if [ $i -ge 10 ]; then 219 # This should never happen. But, it has happened. 220 echo "Cannot umount(8) ${DIR}" 221 echo "Something has gone horribly wrong." 222 return 1 223 fi 224 sleep 1 225 done 226 227 return 0 228} 229 230vm_create_disk() { 231 local BOOTFILES BOOTPARTSOFFSET FSPARTTYPE X86GPTBOOTFILE 232 233 if [ -z "${NOSWAP}" ]; then 234 SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}" 235 fi 236 237 if [ -n "${VM_BOOTPARTSOFFSET}" ]; then 238 BOOTPARTSOFFSET=":${VM_BOOTPARTSOFFSET}" 239 fi 240 241 if [ -n "${CONFIG_DRIVE}" ]; then 242 CONFIG_DRIVE="-p freebsd/config-drive::${CONFIG_DRIVE_SIZE}" 243 fi 244 245 case "${VMFS}" in 246 ufs) 247 FSPARTTYPE=freebsd-ufs 248 X86GPTBOOTFILE=i386/gptboot/gptboot 249 ;; 250 zfs) 251 FSPARTTYPE=freebsd-zfs 252 X86GPTBOOTFILE=i386/gptzfsboot/gptzfsboot 253 ;; 254 *) 255 echo "Unexpected VMFS value '${VMFS}'" 256 return 1 257 ;; 258 esac 259 260 echo "Creating image... Please wait." 261 echo 262 263 BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \ 264 WITH_UNIFIED_OBJDIR=yes \ 265 make -C ${WORLDDIR}/stand -V .OBJDIR)" 266 BOOTFILES="$(realpath ${BOOTFILES})" 267 MAKEFSARGS="-s ${VMSIZE}" 268 269 case "${TARGET}:${TARGET_ARCH}" in 270 amd64:amd64 | i386:i386) 271 ESP=yes 272 BOOTPARTS="-b ${BOOTFILES}/i386/pmbr/pmbr \ 273 -p freebsd-boot/bootfs:=${BOOTFILES}/${X86GPTBOOTFILE}${BOOTPARTSOFFSET}" 274 ROOTFSPART="-p ${FSPARTTYPE}/rootfs:=${VMBASE}" 275 MAKEFSARGS="$MAKEFSARGS -B little" 276 ;; 277 arm:armv7 | arm64:aarch64 | riscv:riscv64*) 278 ESP=yes 279 BOOTPARTS= 280 ROOTFSPART="-p ${FSPARTTYPE}/rootfs:=${VMBASE}" 281 MAKEFSARGS="$MAKEFSARGS -B little" 282 ;; 283 powerpc:powerpc*) 284 ESP=no 285 BOOTPARTS="-p prepboot:=${BOOTFILES}/powerpc/boot1.chrp/boot1.elf -a 1" 286 ROOTFSPART="-p freebsd:=${VMBASE}" 287 if [ ${TARGET_ARCH} = powerpc64le ]; then 288 MAKEFSARGS="$MAKEFSARGS -B little" 289 else 290 MAKEFSARGS="$MAKEFSARGS -B big" 291 fi 292 ;; 293 *) 294 echo "vmimage.subr: unsupported target '${TARGET}:${TARGET_ARCH}'" >&2 295 exit 1 296 ;; 297 esac 298 299 if [ ${ESP} = "yes" ]; then 300 # Create an ESP 301 espfilename=$(mktemp /tmp/efiboot.XXXXXX) 302 make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi 303 BOOTPARTS="${BOOTPARTS} -p efi/efiboot0:=${espfilename}" 304 305 # Add this to fstab 306 mkdir -p ${DESTDIR}/boot/efi 307 echo "/dev/${ROOTLABEL}/efiboot0 /boot/efi msdosfs rw 2 2" \ 308 >> ${DESTDIR}/etc/fstab 309 fi 310 311 echo "Building filesystem... Please wait." 312 buildfs 313 314 echo "Building final disk image... Please wait." 315 mkimg -s ${PARTSCHEME} -f ${VMFORMAT} \ 316 ${BOOTPARTS} \ 317 ${SWAPOPT} \ 318 ${CONFIG_DRIVE} \ 319 ${ROOTFSPART} \ 320 -o ${VMIMAGE} 321 322 echo "Disk image ${VMIMAGE} created." 323 324 if [ ${ESP} = "yes" ]; then 325 rm ${espfilename} 326 fi 327 328 return 0 329} 330 331vm_extra_create_disk() { 332 333 return 0 334} 335 336