1#!/bin/sh 2# 3# 4# 5# Common functions for virtual machine image build scripts. 6# 7 8scriptdir=$(dirname $(realpath $0)) 9. ${scriptdir}/../scripts/tools.subr 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 15# Platform-specific large-scale setup 16# Most platforms use GPT, so put that as default, then special cases 17PARTSCHEME=gpt 18ROOTLABEL="gpt" 19case "${TARGET}:${TARGET_ARCH}" in 20 powerpc:powerpc*) 21 PARTSCHEME=mbr 22 ROOTLABEL="ufs" 23 NOSWAP=yes # Can't label swap partition with MBR, so no swap 24 ;; 25esac 26 27err() { 28 printf "${@}\n" 29 cleanup 30 return 1 31} 32 33cleanup() { 34 if [ -c "${DESTDIR}/dev/null" ]; then 35 umount_loop ${DESTDIR}/dev 2>/dev/null 36 fi 37 38 return 0 39} 40 41metalog_add_data() { 42 local file mode type 43 44 if [ -n "${NO_ROOT}" ]; then 45 file=$1 46 if [ -f ${DESTDIR}/${file} ]; then 47 type=file 48 mode=${2:-0644} 49 elif [ -d ${DESTDIR}/${file} ]; then 50 type=dir 51 mode=${2:-0755} 52 else 53 echo "metalog_add_data: ${file} not found" >&2 54 return 1 55 fi 56 echo "${file} type=${type} uname=root gname=wheel mode=${mode}" >> \ 57 ${DESTDIR}/METALOG 58 fi 59} 60 61vm_create_base() { 62 63 mkdir -p ${DESTDIR} 64 65 return 0 66} 67 68vm_copy_base() { 69 # Defunct 70 return 0 71} 72 73vm_base_packages_list() { 74 # Output a list of package sets equivalent to what we get from 75 # "installworld installkernel distribution", aka. the full base 76 # system. 77 for S in base kernels; do 78 echo FreeBSD-set-$S 79 echo FreeBSD-set-$S-dbg 80 done 81 case ${TARGET_ARCH} in 82 amd64 | aarch64 | powerpc64) 83 echo FreeBSD-set-lib32 84 echo FreeBSD-set-lib32-dbg 85 esac 86 echo FreeBSD-set-tests 87} 88 89vm_extra_filter_base_packages() { 90 # Prototype. When overridden, allows further filtering of base system 91 # packages, reading package names from stdin and writing to stdout. 92 cat 93} 94 95vm_install_base() { 96 # Installs the FreeBSD userland/kernel to the virtual machine disk. 97 98 if [ -z "${NOPKGBASE}" ]; then 99 local pkg_cmd 100 pkg_cmd="${PKG_CMD} --rootdir ${DESTDIR} --repo-conf-dir ${PKGBASE_REPO_DIR} 101 -o ASSUME_ALWAYS_YES=yes -o IGNORE_OSVERSION=yes 102 -o ABI=${PKG_ABI} -o INSTALL_AS_USER=yes " 103 if [ -n "${NO_ROOT}" ]; then 104 pkg_cmd="$pkg_cmd -o METALOG=METALOG" 105 fi 106 $pkg_cmd update 107 selected=$(vm_base_packages_list | vm_extra_filter_base_packages) 108 $pkg_cmd install -U -r FreeBSD-base $selected 109 else 110 cd ${WORLDDIR} && \ 111 make DESTDIR=${DESTDIR} ${INSTALLOPTS} \ 112 installworld installkernel distribution || \ 113 err "\n\nCannot install the base system to ${DESTDIR}." 114 fi 115 116 # Bootstrap etcupdate(8) database. 117 mkdir -p ${DESTDIR}/var/db/etcupdate 118 etcupdate extract -B \ 119 -M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \ 120 -s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate \ 121 -L /dev/stdout ${NO_ROOT:+-N} 122 if [ -n "${NO_ROOT}" ]; then 123 # Reroot etcupdate's internal METALOG to the whole tree 124 sed -n 's,^\.,./var/db/etcupdate/current,p' \ 125 ${DESTDIR}/var/db/etcupdate/current/METALOG | \ 126 env -i LC_COLLATE=C sort >> ${DESTDIR}/METALOG 127 rm ${DESTDIR}/var/db/etcupdate/current/METALOG 128 fi 129 130 echo '# Custom /etc/fstab for FreeBSD VM images' \ 131 > ${DESTDIR}/etc/fstab 132 if [ "${VMFS}" != zfs ]; then 133 echo "/dev/${ROOTLABEL}/rootfs / ${VMFS} rw,noatime 1 1" \ 134 >> ${DESTDIR}/etc/fstab 135 fi 136 if [ -z "${NOSWAP}" ]; then 137 echo '/dev/gpt/swapfs none swap sw 0 0' \ 138 >> ${DESTDIR}/etc/fstab 139 fi 140 metalog_add_data ./etc/fstab 141 142 local hostname 143 hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')" 144 echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf 145 metalog_add_data ./etc/rc.conf 146 if [ "${VMFS}" = zfs ]; then 147 echo "zfs_enable=\"YES\"" >> ${DESTDIR}/etc/rc.conf 148 echo "zpool_reguid=\"zroot\"" >> ${DESTDIR}/etc/rc.conf 149 echo "zpool_upgrade=\"zroot\"" >> ${DESTDIR}/etc/rc.conf 150 echo "kern.geom.label.disk_ident.enable=0" >> ${DESTDIR}/boot/loader.conf 151 echo "zfs_load=YES" >> ${DESTDIR}/boot/loader.conf 152 metalog_add_data ./boot/loader.conf 153 fi 154 155 return 0 156} 157 158vm_emulation_setup() { 159 if [ -n "${WITHOUT_QEMU}" ]; then 160 return 0 161 fi 162 if [ -n "${QEMUSTATIC}" ]; then 163 export EMULATOR=/qemu 164 cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR} 165 fi 166 167 mkdir -p ${DESTDIR}/dev 168 mount -t devfs devfs ${DESTDIR}/dev 169 chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart 170 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf 171 172 return 0 173} 174 175vm_extra_install_base() { 176 # Prototype. When overridden, runs extra post-installworld commands 177 # as needed, based on the target virtual machine image or cloud 178 # provider image target. 179 180 return 0 181} 182 183vm_extra_enable_services() { 184 if [ -n "${VM_RC_LIST}" ]; then 185 for _rcvar in ${VM_RC_LIST}; do 186 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf 187 done 188 fi 189 190 if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then 191 echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \ 192 ${DESTDIR}/etc/rc.conf 193 # Expand the filesystem to fill the disk. 194 echo 'growfs_enable="YES"' >> ${DESTDIR}/etc/rc.conf 195 fi 196 197 return 0 198} 199 200vm_extra_install_packages() { 201 if [ -z "${VM_EXTRA_PACKAGES}" ]; then 202 return 0 203 fi 204 if [ -n "${NO_ROOT}" ]; then 205 for pkg in ${VM_EXTRA_PACKAGES}; do 206 INSTALL_AS_USER=yes \ 207 ${PKG_CMD} \ 208 -o ABI=${PKG_ABI} \ 209 -o METALOG=${DESTDIR}/METALOG.pkg \ 210 -o REPOS_DIR=${PKG_REPOS_DIR} \ 211 -o PKG_DBDIR=${DESTDIR}/var/db/pkg \ 212 -r ${DESTDIR} \ 213 install -y -r ${PKG_REPO_NAME} $pkg 214 done 215 metalog_add_data ./var/db/pkg/local.sqlite 216 else 217 if [ -n "${WITHOUT_QEMU}" ]; then 218 return 0 219 fi 220 221 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 222 /usr/sbin/pkg bootstrap -y 223 for p in ${VM_EXTRA_PACKAGES}; do 224 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 225 /usr/sbin/pkg install -y ${p} 226 done 227 fi 228 229 return 0 230} 231 232vm_extra_install_ports() { 233 # Prototype. When overridden, installs additional ports within the 234 # virtual machine environment. 235 236 return 0 237} 238 239vm_extra_pre_umount() { 240 # Prototype. When overridden, performs additional tasks within the 241 # virtual machine environment prior to unmounting the filesystem. 242 243 return 0 244} 245 246vm_emulation_cleanup() { 247 if [ -n "${WITHOUT_QEMU}" ]; then 248 return 0 249 fi 250 251 if ! [ -z "${QEMUSTATIC}" ]; then 252 rm -f ${DESTDIR}/${EMULATOR} 253 fi 254 rm -f ${DESTDIR}/etc/resolv.conf 255 umount_loop ${DESTDIR}/dev 256 return 0 257} 258 259vm_extra_pkg_rmcache() { 260 if [ -n "${NO_ROOT}" ]; then 261 ${PKG_CMD} \ 262 -o ASSUME_ALWAYS_YES=yes \ 263 -o INSTALL_AS_USER=yes \ 264 -r ${DESTDIR} \ 265 clean -y -a 266 else 267 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then 268 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 269 /usr/local/sbin/pkg clean -y -a 270 fi 271 fi 272 273 return 0 274} 275 276buildfs() { 277 local md tmppool 278 279 if [ -f ${DESTDIR}/METALOG.pkg ]; then 280 cat ${DESTDIR}/METALOG.pkg >> ${DESTDIR}/METALOG 281 fi 282 283 if [ -n "${NO_ROOT}" ]; then 284 # Check for any directories in the staging tree which weren't 285 # recorded in METALOG, and record them now. This is a quick hack 286 # to avoid creating unusable VM images and should go away once 287 # the bugs which produce such unlogged directories are gone. 288 grep type=dir ${DESTDIR}/METALOG | 289 cut -f 1 -d ' ' | 290 sort -u > ${DESTDIR}/METALOG.dirs 291 ( cd ${DESTDIR} && find . -type d ) | 292 sort | 293 comm -23 - ${DESTDIR}/METALOG.dirs > ${DESTDIR}/METALOG.missingdirs 294 if [ -s ${DESTDIR}/METALOG.missingdirs ]; then 295 echo "WARNING: Directories exist but were not in METALOG" 296 cat ${DESTDIR}/METALOG.missingdirs 297 fi 298 while read DIR; do 299 metalog_add_data ${DIR} 300 done < ${DESTDIR}/METALOG.missingdirs 301 302 if [ -z "${NOPKGBASE}" ]; then 303 # Add some database files which are created by pkg triggers; 304 # at some point in the future the tools which create these 305 # files should probably learn how to record them in METALOG 306 # (which would simplify no-root installworld as well). 307 metalog_add_data ./etc/login.conf.db 308 metalog_add_data ./etc/passwd 309 metalog_add_data ./etc/pwd.db 310 metalog_add_data ./etc/spwd.db 600 311 metalog_add_data ./var/db/services.db 312 fi 313 314 # Sort METALOG file; makefs produces directories with 000 permissions 315 # if their contents are seen before the directories themselves. 316 env -i LC_COLLATE=C sort -u ${DESTDIR}/METALOG > ${DESTDIR}/METALOG.sorted 317 mv ${DESTDIR}/METALOG.sorted ${DESTDIR}/METALOG 318 fi 319 320 case "${VMFS}" in 321 ufs) 322 cd ${DESTDIR} && ${MAKEFS} ${MAKEFSARGS} -o label=rootfs -o version=2 -o softupdates=1 \ 323 ${VMBASE} .${NO_ROOT:+/METALOG} 324 ;; 325 zfs) 326 cd ${DESTDIR} && ${MAKEFS} -t zfs ${MAKEFSARGS} \ 327 -o poolname=zroot -o bootfs=zroot/ROOT/default -o rootpath=/ \ 328 -o fs=zroot\;mountpoint=none \ 329 -o fs=zroot/ROOT\;mountpoint=none \ 330 -o fs=zroot/ROOT/default\;mountpoint=/\;canmount=noauto \ 331 -o fs=zroot/home\;mountpoint=/home \ 332 -o fs=zroot/tmp\;mountpoint=/tmp\;exec=on\;setuid=off \ 333 -o fs=zroot/usr\;mountpoint=/usr\;canmount=off \ 334 -o fs=zroot/usr/ports\;setuid=off \ 335 -o fs=zroot/usr/src \ 336 -o fs=zroot/usr/obj \ 337 -o fs=zroot/var\;mountpoint=/var\;canmount=off \ 338 -o fs=zroot/var/audit\;setuid=off\;exec=off \ 339 -o fs=zroot/var/crash\;setuid=off\;exec=off \ 340 -o fs=zroot/var/log\;setuid=off\;exec=off \ 341 -o fs=zroot/var/mail\;atime=on \ 342 -o fs=zroot/var/tmp\;setuid=off \ 343 ${VMBASE} .${NO_ROOT:+/METALOG} 344 ;; 345 *) 346 echo "Unexpected VMFS value '${VMFS}'" 347 exit 1 348 ;; 349 esac 350} 351 352umount_loop() { 353 DIR=$1 354 i=0 355 sync 356 while ! umount ${DIR}; do 357 i=$(( $i + 1 )) 358 if [ $i -ge 10 ]; then 359 # This should never happen. But, it has happened. 360 echo "Cannot umount(8) ${DIR}" 361 echo "Something has gone horribly wrong." 362 return 1 363 fi 364 sleep 1 365 done 366 367 return 0 368} 369 370vm_create_disk() { 371 local BOOTFILES BOOTPARTSOFFSET FSPARTTYPE X86GPTBOOTFILE 372 373 if [ -z "${NOSWAP}" ]; then 374 SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}" 375 fi 376 377 if [ -n "${VM_BOOTPARTSOFFSET}" ]; then 378 BOOTPARTSOFFSET=":${VM_BOOTPARTSOFFSET}" 379 fi 380 381 if [ -n "${CONFIG_DRIVE}" ]; then 382 CONFIG_DRIVE="-p freebsd/config-drive::${CONFIG_DRIVE_SIZE}" 383 fi 384 385 case "${VMFS}" in 386 ufs) 387 FSPARTTYPE=freebsd-ufs 388 X86GPTBOOTFILE=i386/gptboot/gptboot 389 ;; 390 zfs) 391 FSPARTTYPE=freebsd-zfs 392 X86GPTBOOTFILE=i386/gptzfsboot/gptzfsboot 393 ;; 394 *) 395 echo "Unexpected VMFS value '${VMFS}'" 396 return 1 397 ;; 398 esac 399 400 echo "Creating image... Please wait." 401 echo 402 403 BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \ 404 WITH_UNIFIED_OBJDIR=yes \ 405 make -C ${WORLDDIR}/stand -V .OBJDIR)" 406 BOOTFILES="$(realpath ${BOOTFILES})" 407 MAKEFSARGS="-s ${VMSIZE} -D" 408 409 case "${TARGET}:${TARGET_ARCH}" in 410 amd64:amd64 | i386:i386) 411 ESP=yes 412 BOOTPARTS="-b ${BOOTFILES}/i386/pmbr/pmbr \ 413 -p freebsd-boot/bootfs:=${BOOTFILES}/${X86GPTBOOTFILE}${BOOTPARTSOFFSET}" 414 ROOTFSPART="-p ${FSPARTTYPE}/rootfs:=${VMBASE}" 415 MAKEFSARGS="$MAKEFSARGS -B little" 416 ;; 417 arm:armv7 | arm64:aarch64 | riscv:riscv64*) 418 ESP=yes 419 BOOTPARTS= 420 ROOTFSPART="-p ${FSPARTTYPE}/rootfs:=${VMBASE}" 421 MAKEFSARGS="$MAKEFSARGS -B little" 422 ;; 423 powerpc:powerpc*) 424 ESP=no 425 BOOTPARTS="-p prepboot:=${BOOTFILES}/powerpc/boot1.chrp/boot1.elf -a 1" 426 ROOTFSPART="-p freebsd:=${VMBASE}" 427 if [ ${TARGET_ARCH} = powerpc64le ]; then 428 MAKEFSARGS="$MAKEFSARGS -B little" 429 else 430 MAKEFSARGS="$MAKEFSARGS -B big" 431 fi 432 ;; 433 *) 434 echo "vmimage.subr: unsupported target '${TARGET}:${TARGET_ARCH}'" >&2 435 exit 1 436 ;; 437 esac 438 439 if [ ${ESP} = "yes" ]; then 440 # Create an ESP 441 espfilename=$(mktemp /tmp/efiboot.XXXXXX) 442 make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi 443 BOOTPARTS="${BOOTPARTS} -p efi/efiboot0:=${espfilename}" 444 445 # Add this to fstab 446 mkdir -p ${DESTDIR}/boot/efi 447 echo "/dev/${ROOTLABEL}/efiboot0 /boot/efi msdosfs rw 2 2" \ 448 >> ${DESTDIR}/etc/fstab 449 fi 450 451 # Add a marker file which indicates that this image has never 452 # been booted. Some services run only upon the first boot. 453 touch ${DESTDIR}/firstboot 454 metalog_add_data ./firstboot 455 456 echo "Building filesystem... Please wait." 457 buildfs 458 459 echo "Building final disk image... Please wait." 460 ${MKIMG} -s ${PARTSCHEME} -f ${VMFORMAT} \ 461 ${BOOTPARTS} \ 462 ${SWAPOPT} \ 463 ${CONFIG_DRIVE} \ 464 ${ROOTFSPART} \ 465 -o ${VMIMAGE} 466 467 echo "Disk image ${VMIMAGE} created." 468 469 if [ ${ESP} = "yes" ]; then 470 rm ${espfilename} 471 fi 472 473 return 0 474} 475 476vm_extra_create_disk() { 477 478 return 0 479} 480