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 INSTALL_AS_USER=yes \ 216 ${PKG_CMD} \ 217 -o ABI=${PKG_ABI} \ 218 -o REPOS_DIR=${PKG_REPOS_DIR} \ 219 -o PKG_DBDIR=${DESTDIR}/var/db/pkg \ 220 -r ${DESTDIR} \ 221 autoremove 222 metalog_add_data ./var/db/pkg/local.sqlite 223 else 224 if [ -n "${WITHOUT_QEMU}" ]; then 225 return 0 226 fi 227 228 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 229 /usr/sbin/pkg bootstrap -y 230 for p in ${VM_EXTRA_PACKAGES}; do 231 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 232 /usr/sbin/pkg install -y ${p} 233 done 234 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 235 /usr/sbin/pkg autoremove 236 fi 237 238 return 0 239} 240 241vm_extra_install_ports() { 242 # Prototype. When overridden, installs additional ports within the 243 # virtual machine environment. 244 245 return 0 246} 247 248vm_extra_pre_umount() { 249 # Prototype. When overridden, performs additional tasks within the 250 # virtual machine environment prior to unmounting the filesystem. 251 252 return 0 253} 254 255vm_emulation_cleanup() { 256 if [ -n "${WITHOUT_QEMU}" ]; then 257 return 0 258 fi 259 260 if ! [ -z "${QEMUSTATIC}" ]; then 261 rm -f ${DESTDIR}/${EMULATOR} 262 fi 263 rm -f ${DESTDIR}/etc/resolv.conf 264 umount_loop ${DESTDIR}/dev 265 return 0 266} 267 268vm_extra_pkg_rmcache() { 269 if [ -n "${NO_ROOT}" ]; then 270 ${PKG_CMD} \ 271 -o ASSUME_ALWAYS_YES=yes \ 272 -o INSTALL_AS_USER=yes \ 273 -r ${DESTDIR} \ 274 clean -y -a 275 else 276 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then 277 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 278 /usr/local/sbin/pkg clean -y -a 279 fi 280 fi 281 282 return 0 283} 284 285buildfs() { 286 local md tmppool 287 288 if [ -f ${DESTDIR}/METALOG.pkg ]; then 289 cat ${DESTDIR}/METALOG.pkg >> ${DESTDIR}/METALOG 290 fi 291 292 if [ -n "${NO_ROOT}" ]; then 293 # Check for any directories in the staging tree which weren't 294 # recorded in METALOG, and record them now. This is a quick hack 295 # to avoid creating unusable VM images and should go away once 296 # the bugs which produce such unlogged directories are gone. 297 grep type=dir ${DESTDIR}/METALOG | 298 cut -f 1 -d ' ' | 299 sort -u > ${DESTDIR}/METALOG.dirs 300 ( cd ${DESTDIR} && find . -type d ) | 301 sort | 302 comm -23 - ${DESTDIR}/METALOG.dirs > ${DESTDIR}/METALOG.missingdirs 303 if [ -s ${DESTDIR}/METALOG.missingdirs ]; then 304 echo "WARNING: Directories exist but were not in METALOG" 305 cat ${DESTDIR}/METALOG.missingdirs 306 fi 307 while read DIR; do 308 metalog_add_data ${DIR} 309 done < ${DESTDIR}/METALOG.missingdirs 310 311 if [ -z "${NOPKGBASE}" ]; then 312 # Add some database files which are created by pkg triggers; 313 # at some point in the future the tools which create these 314 # files should probably learn how to record them in METALOG 315 # (which would simplify no-root installworld as well). 316 metalog_add_data ./etc/login.conf.db 317 metalog_add_data ./etc/passwd 318 metalog_add_data ./etc/pwd.db 319 metalog_add_data ./etc/spwd.db 600 320 metalog_add_data ./var/db/services.db 321 fi 322 323 # Sort METALOG file; makefs produces directories with 000 permissions 324 # if their contents are seen before the directories themselves. 325 env -i LC_COLLATE=C sort -u ${DESTDIR}/METALOG > ${DESTDIR}/METALOG.sorted 326 mv ${DESTDIR}/METALOG.sorted ${DESTDIR}/METALOG 327 fi 328 329 case "${VMFS}" in 330 ufs) 331 cd ${DESTDIR} && ${MAKEFS} ${MAKEFSARGS} -o label=rootfs -o version=2 -o softupdates=1 \ 332 ${VMBASE} .${NO_ROOT:+/METALOG} 333 ;; 334 zfs) 335 cd ${DESTDIR} && ${MAKEFS} -t zfs ${MAKEFSARGS} \ 336 -o poolname=zroot -o bootfs=zroot/ROOT/default -o rootpath=/ \ 337 -o fs=zroot\;mountpoint=none \ 338 -o fs=zroot/ROOT\;mountpoint=none \ 339 -o fs=zroot/ROOT/default\;mountpoint=/\;canmount=noauto \ 340 -o fs=zroot/home\;mountpoint=/home \ 341 -o fs=zroot/tmp\;mountpoint=/tmp\;exec=on\;setuid=off \ 342 -o fs=zroot/usr\;mountpoint=/usr\;canmount=off \ 343 -o fs=zroot/usr/ports\;setuid=off \ 344 -o fs=zroot/usr/src \ 345 -o fs=zroot/usr/obj \ 346 -o fs=zroot/var\;mountpoint=/var\;canmount=off \ 347 -o fs=zroot/var/audit\;setuid=off\;exec=off \ 348 -o fs=zroot/var/crash\;setuid=off\;exec=off \ 349 -o fs=zroot/var/log\;setuid=off\;exec=off \ 350 -o fs=zroot/var/mail\;atime=on \ 351 -o fs=zroot/var/tmp\;setuid=off \ 352 ${VMBASE} .${NO_ROOT:+/METALOG} 353 ;; 354 *) 355 echo "Unexpected VMFS value '${VMFS}'" 356 exit 1 357 ;; 358 esac 359} 360 361umount_loop() { 362 DIR=$1 363 i=0 364 sync 365 while ! umount ${DIR}; do 366 i=$(( $i + 1 )) 367 if [ $i -ge 10 ]; then 368 # This should never happen. But, it has happened. 369 echo "Cannot umount(8) ${DIR}" 370 echo "Something has gone horribly wrong." 371 return 1 372 fi 373 sleep 1 374 done 375 376 return 0 377} 378 379vm_create_disk() { 380 local BOOTFILES BOOTPARTSOFFSET FSPARTTYPE X86GPTBOOTFILE 381 382 if [ -z "${NOSWAP}" ]; then 383 SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}" 384 fi 385 386 if [ -n "${VM_BOOTPARTSOFFSET}" ]; then 387 BOOTPARTSOFFSET=":${VM_BOOTPARTSOFFSET}" 388 fi 389 390 if [ -n "${CONFIG_DRIVE}" ]; then 391 CONFIG_DRIVE="-p freebsd/config-drive::${CONFIG_DRIVE_SIZE}" 392 fi 393 394 case "${VMFS}" in 395 ufs) 396 FSPARTTYPE=freebsd-ufs 397 X86GPTBOOTFILE=i386/gptboot/gptboot 398 ;; 399 zfs) 400 FSPARTTYPE=freebsd-zfs 401 X86GPTBOOTFILE=i386/gptzfsboot/gptzfsboot 402 ;; 403 *) 404 echo "Unexpected VMFS value '${VMFS}'" 405 return 1 406 ;; 407 esac 408 409 echo "Creating image... Please wait." 410 echo 411 412 BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \ 413 WITH_UNIFIED_OBJDIR=yes \ 414 make -C ${WORLDDIR}/stand -V .OBJDIR)" 415 BOOTFILES="$(realpath ${BOOTFILES})" 416 MAKEFSARGS="-s ${VMSIZE} -D" 417 418 case "${TARGET}:${TARGET_ARCH}" in 419 amd64:amd64 | i386:i386) 420 ESP=yes 421 BOOTPARTS="-b ${BOOTFILES}/i386/pmbr/pmbr \ 422 -p freebsd-boot/bootfs:=${BOOTFILES}/${X86GPTBOOTFILE}${BOOTPARTSOFFSET}" 423 ROOTFSPART="-p ${FSPARTTYPE}/rootfs:=${VMBASE}" 424 MAKEFSARGS="$MAKEFSARGS -B little" 425 ;; 426 arm:armv7 | arm64:aarch64 | riscv:riscv64*) 427 ESP=yes 428 BOOTPARTS= 429 ROOTFSPART="-p ${FSPARTTYPE}/rootfs:=${VMBASE}" 430 MAKEFSARGS="$MAKEFSARGS -B little" 431 ;; 432 powerpc:powerpc*) 433 ESP=no 434 BOOTPARTS="-p prepboot:=${BOOTFILES}/powerpc/boot1.chrp/boot1.elf -a 1" 435 ROOTFSPART="-p freebsd:=${VMBASE}" 436 if [ ${TARGET_ARCH} = powerpc64le ]; then 437 MAKEFSARGS="$MAKEFSARGS -B little" 438 else 439 MAKEFSARGS="$MAKEFSARGS -B big" 440 fi 441 ;; 442 *) 443 echo "vmimage.subr: unsupported target '${TARGET}:${TARGET_ARCH}'" >&2 444 exit 1 445 ;; 446 esac 447 448 if [ ${ESP} = "yes" ]; then 449 # Create an ESP 450 espfilename=$(mktemp /tmp/efiboot.XXXXXX) 451 make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi 452 espsuffix="" 453 if [ -z "${BOOTPARTS}" ]; then 454 espsuffix="${BOOTPARTSOFFSET}" 455 fi 456 BOOTPARTS="${BOOTPARTS} -p efi/efiboot0:=${espfilename}${espsuffix}" 457 458 # Add this to fstab 459 mkdir -p ${DESTDIR}/boot/efi 460 echo "/dev/${ROOTLABEL}/efiboot0 /boot/efi msdosfs rw 2 2" \ 461 >> ${DESTDIR}/etc/fstab 462 fi 463 464 # Add a marker file which indicates that this image has never 465 # been booted. Some services run only upon the first boot. 466 touch ${DESTDIR}/firstboot 467 metalog_add_data ./firstboot 468 469 echo "Building filesystem... Please wait." 470 buildfs 471 472 echo "Building final disk image... Please wait." 473 ${MKIMG} -s ${PARTSCHEME} -f ${VMFORMAT} \ 474 ${BOOTPARTS} \ 475 ${SWAPOPT} \ 476 ${CONFIG_DRIVE} \ 477 ${ROOTFSPART} \ 478 -o ${VMIMAGE} 479 480 echo "Disk image ${VMIMAGE} created." 481 482 if [ ${ESP} = "yes" ]; then 483 rm ${espfilename} 484 fi 485 486 return 0 487} 488 489vm_extra_create_disk() { 490 491 return 0 492} 493