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 write_partition_layout() {
16 if [ -z "${NOSWAP}" ]; then
17 SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}"
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})"
25 case "${TARGET}:${TARGET_ARCH}" in
26 amd64:amd64 | i386:i386)
29 BOOTPARTS="-b ${BOOTFILES}/i386/pmbr/pmbr \
30 -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot"
31 ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
33 arm64:aarch64 | riscv:riscv64*)
37 ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
42 BOOTPARTS="-p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs"
43 ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
46 echo "vmimage.subr: unsupported target '${TARGET}:${TARGET_ARCH}'" >&2
51 if [ ${ESP} = "yes" ]; then
53 espfilename=$(mktemp /tmp/efiboot.XXXXXX)
54 make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi
55 BOOTPARTS="${BOOTPARTS} -p efi/efiesp:=${espfilename}"
57 # Add this to fstab, requires temporarily remounting the fs
58 mddev=$(mdconfig -f ${VMBASE})
59 mount /dev/${mddev} ${DESTDIR}
60 mkdir -p ${DESTDIR}/boot/efi
61 echo "/dev/${ROOTLABEL}/efiesp /boot/efi msdosfs rw 2 2" \
62 >> ${DESTDIR}/etc/fstab
64 mdconfig -d -u ${mddev}
67 mkimg -s ${SCHEME} -f ${VMFORMAT} \
73 if [ ${ESP} = "yes" ]; then
87 if [ -c "${DESTDIR}/dev/null" ]; then
88 umount_loop ${DESTDIR}/dev 2>/dev/null
90 umount_loop ${DESTDIR}
91 if [ ! -z "${mddev}" ]; then
92 mdconfig -d -u ${mddev}
99 # Creates the UFS root filesystem for the virtual machine disk,
100 # written to the formatted disk image with mkimg(1).
103 truncate -s ${VMSIZE} ${VMBASE}
104 mddev=$(mdconfig -f ${VMBASE})
105 newfs -L rootfs /dev/${mddev}
106 mount /dev/${mddev} ${DESTDIR}
112 # Creates a new UFS root filesystem and copies the contents of the
113 # current root filesystem into it. This produces a "clean" disk
114 # image without any remnants of files which were created temporarily
115 # during image-creation and have since been deleted (e.g., downloaded
118 mkdir -p ${DESTDIR}/old
119 mdold=$(mdconfig -f ${VMBASE})
120 mount /dev/${mdold} ${DESTDIR}/old
122 truncate -s ${VMSIZE} ${VMBASE}.tmp
123 mkdir -p ${DESTDIR}/new
124 mdnew=$(mdconfig -f ${VMBASE}.tmp)
125 newfs -L rootfs /dev/${mdnew}
126 mount /dev/${mdnew} ${DESTDIR}/new
128 tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new
130 umount_loop /dev/${mdold}
132 mdconfig -d -u ${mdold}
134 umount_loop /dev/${mdnew}
136 tunefs -n enable /dev/${mdnew}
137 mdconfig -d -u ${mdnew}
138 mv ${VMBASE}.tmp ${VMBASE}
142 # Installs the FreeBSD userland/kernel to the virtual machine disk.
145 make DESTDIR=${DESTDIR} \
146 installworld installkernel distribution || \
147 err "\n\nCannot install the base system to ${DESTDIR}."
149 # Bootstrap etcupdate(8) and mergemaster(8) databases.
150 mkdir -p ${DESTDIR}/var/db/etcupdate
151 etcupdate extract -B \
152 -M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
153 -s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate
154 sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \
155 -F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
158 echo '# Custom /etc/fstab for FreeBSD VM images' \
159 > ${DESTDIR}/etc/fstab
160 echo "/dev/${ROOTLABEL}/rootfs / ufs rw 1 1" \
161 >> ${DESTDIR}/etc/fstab
162 if [ -z "${NOSWAP}" ]; then
163 echo '/dev/gpt/swapfs none swap sw 0 0' \
164 >> ${DESTDIR}/etc/fstab
168 hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')"
169 echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf
171 if ! [ -z "${QEMUSTATIC}" ]; then
172 export EMULATOR=/qemu
173 cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR}
176 mkdir -p ${DESTDIR}/dev
177 mount -t devfs devfs ${DESTDIR}/dev
178 chroot ${DESTDIR} ${EMULATOR} /usr/bin/newaliases
179 chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart
180 umount_loop ${DESTDIR}/dev
182 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
187 vm_extra_install_base() {
188 # Prototype. When overridden, runs extra post-installworld commands
189 # as needed, based on the target virtual machine image or cloud
190 # provider image target.
195 vm_extra_enable_services() {
196 if [ ! -z "${VM_RC_LIST}" ]; then
197 for _rcvar in ${VM_RC_LIST}; do
198 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
202 if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then
203 echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \
204 ${DESTDIR}/etc/rc.conf
205 # Expand the filesystem to fill the disk.
206 echo 'growfs_enable="YES"' >> ${DESTDIR}/etc/rc.conf
207 touch ${DESTDIR}/firstboot
213 vm_extra_install_packages() {
214 if [ -z "${VM_EXTRA_PACKAGES}" ]; then
217 mkdir -p ${DESTDIR}/dev
218 mount -t devfs devfs ${DESTDIR}/dev
219 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
220 /usr/sbin/pkg bootstrap -y
221 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
222 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
223 umount_loop ${DESTDIR}/dev
228 vm_extra_install_ports() {
229 # Prototype. When overridden, installs additional ports within the
230 # virtual machine environment.
235 vm_extra_pre_umount() {
236 # Prototype. When overridden, performs additional tasks within the
237 # virtual machine environment prior to unmounting the filesystem.
238 # Note: When overriding this function, removing resolv.conf in the
239 # disk image must be included.
241 if ! [ -z "${QEMUSTATIC}" ]; then
242 rm -f ${DESTDIR}/${EMULATOR}
244 rm -f ${DESTDIR}/etc/resolv.conf
248 vm_extra_pkg_rmcache() {
249 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
250 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
251 /usr/local/sbin/pkg clean -y -a
261 while ! umount ${DIR}; do
263 if [ $i -ge 10 ]; then
264 # This should never happen. But, it has happened.
265 echo "Cannot umount(8) ${DIR}"
266 echo "Something has gone horribly wrong."
276 echo "Creating image... Please wait."
279 write_partition_layout || return 1
284 vm_extra_create_disk() {