6 # Common functions for virtual machine image build scripts.
9 scriptdir=$(dirname $(realpath $0))
10 . ${scriptdir}/../../tools/boot/install-boot.sh
12 export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
13 trap "cleanup" INT QUIT TRAP ABRT TERM
15 # Platform-specific large-scale setup
16 # Most platforms use GPT, so put that as default, then special cases
19 case "${TARGET}:${TARGET_ARCH}" in
23 NOSWAP=yes # Can't label swap partition with MBR, so no swap
34 if [ -c "${DESTDIR}/dev/null" ]; then
35 umount_loop ${DESTDIR}/dev 2>/dev/null
42 # Creates the UFS root filesystem for the virtual machine disk,
43 # written to the formatted disk image with mkimg(1).
55 # Installs the FreeBSD userland/kernel to the virtual machine disk.
58 make DESTDIR=${DESTDIR} \
59 installworld installkernel distribution || \
60 err "\n\nCannot install the base system to ${DESTDIR}."
62 # Bootstrap etcupdate(8) and mergemaster(8) databases.
63 mkdir -p ${DESTDIR}/var/db/etcupdate
64 etcupdate extract -B \
65 -M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
66 -s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate
67 sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \
68 -F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
71 echo '# Custom /etc/fstab for FreeBSD VM images' \
72 > ${DESTDIR}/etc/fstab
73 echo "/dev/${ROOTLABEL}/rootfs / ufs rw 1 1" \
74 >> ${DESTDIR}/etc/fstab
75 if [ -z "${NOSWAP}" ]; then
76 echo '/dev/gpt/swapfs none swap sw 0 0' \
77 >> ${DESTDIR}/etc/fstab
81 hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')"
82 echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf
84 if ! [ -z "${QEMUSTATIC}" ]; then
86 cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR}
89 mkdir -p ${DESTDIR}/dev
90 mount -t devfs devfs ${DESTDIR}/dev
91 chroot ${DESTDIR} ${EMULATOR} /usr/bin/newaliases
92 chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart
93 umount_loop ${DESTDIR}/dev
95 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
100 vm_extra_install_base() {
101 # Prototype. When overridden, runs extra post-installworld commands
102 # as needed, based on the target virtual machine image or cloud
103 # provider image target.
108 vm_extra_enable_services() {
109 if [ ! -z "${VM_RC_LIST}" ]; then
110 for _rcvar in ${VM_RC_LIST}; do
111 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
115 if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then
116 echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \
117 ${DESTDIR}/etc/rc.conf
118 # Expand the filesystem to fill the disk.
119 echo 'growfs_enable="YES"' >> ${DESTDIR}/etc/rc.conf
120 touch ${DESTDIR}/firstboot
126 vm_extra_install_packages() {
127 if [ -z "${VM_EXTRA_PACKAGES}" ]; then
130 mkdir -p ${DESTDIR}/dev
131 mount -t devfs devfs ${DESTDIR}/dev
132 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
133 /usr/sbin/pkg bootstrap -y
134 for p in ${VM_EXTRA_PACKAGES}; do
135 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
136 /usr/sbin/pkg install -y ${p}
138 umount_loop ${DESTDIR}/dev
143 vm_extra_install_ports() {
144 # Prototype. When overridden, installs additional ports within the
145 # virtual machine environment.
150 vm_extra_pre_umount() {
151 # Prototype. When overridden, performs additional tasks within the
152 # virtual machine environment prior to unmounting the filesystem.
153 # Note: When overriding this function, removing resolv.conf in the
154 # disk image must be included.
156 if ! [ -z "${QEMUSTATIC}" ]; then
157 rm -f ${DESTDIR}/${EMULATOR}
159 rm -f ${DESTDIR}/etc/resolv.conf
163 vm_extra_pkg_rmcache() {
164 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
165 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
166 /usr/local/sbin/pkg clean -y -a
176 while ! umount ${DIR}; do
178 if [ $i -ge 10 ]; then
179 # This should never happen. But, it has happened.
180 echo "Cannot umount(8) ${DIR}"
181 echo "Something has gone horribly wrong."
191 echo "Creating image... Please wait."
194 if [ -z "${NOSWAP}" ]; then
195 SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}"
198 if [ -n "${VM_BOOTPARTSOFFSET}" ]; then
199 BOOTPARTSOFFSET=":${VM_BOOTPARTSOFFSET}"
203 BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
204 WITH_UNIFIED_OBJDIR=yes \
205 make -C ${WORLDDIR}/stand -V .OBJDIR)"
206 BOOTFILES="$(realpath ${BOOTFILES})"
208 case "${TARGET}:${TARGET_ARCH}" in
209 amd64:amd64 | i386:i386)
211 BOOTPARTS="-b ${BOOTFILES}/i386/pmbr/pmbr \
212 -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot${BOOTPARTSOFFSET}"
213 ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
214 MAKEFSARGS="-B little"
216 arm64:aarch64 | riscv:riscv64*)
219 ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
220 MAKEFSARGS="-B little"
224 BOOTPARTS="-p prepboot:=${BOOTFILES}/powerpc/boot1.chrp/boot1.elf -a 1"
225 ROOTFSPART="-p freebsd:=${VMBASE}"
226 if [ ${TARGET_ARCH} = powerpc64le ]; then
227 MAKEFSARGS="-B little"
233 echo "vmimage.subr: unsupported target '${TARGET}:${TARGET_ARCH}'" >&2
238 if [ ${ESP} = "yes" ]; then
240 espfilename=$(mktemp /tmp/efiboot.XXXXXX)
241 make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi
242 BOOTPARTS="${BOOTPARTS} -p efi/efiesp:=${espfilename}"
245 mkdir -p ${DESTDIR}/boot/efi
246 echo "/dev/${ROOTLABEL}/efiesp /boot/efi msdosfs rw 2 2" \
247 >> ${DESTDIR}/etc/fstab
250 echo "Building filesystem... Please wait."
251 makefs ${MAKEFSARGS} -o label=rootfs -o version=2 -o softupdates=1 \
252 -s ${VMSIZE} ${VMBASE} ${DESTDIR}
254 echo "Building final disk image... Please wait."
255 mkimg -s ${PARTSCHEME} -f ${VMFORMAT} \
261 if [ ${ESP} = "yes" ]; then
268 vm_extra_create_disk() {