6 # Common functions for virtual machine image build scripts.
9 export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
10 trap "cleanup" INT QUIT TRAP ABRT TERM
12 write_partition_layout() {
13 if [ -z "${NOSWAP}" ]; then
14 SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}"
17 BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
18 WITH_UNIFIED_OBJDIR=yes \
19 make -C ${WORLDDIR}/stand -V .OBJDIR)"
20 BOOTFILES="$(realpath ${BOOTFILES})"
22 case "${TARGET}:${TARGET_ARCH}" in
23 amd64:amd64 | i386:i386)
26 BOOTPARTS="-b ${BOOTFILES}/i386/pmbr/pmbr \
27 -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot"
28 ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
34 ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
39 BOOTPARTS="-p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs"
40 ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
43 echo "vmimage.subr: unsupported target '${TARGET}:${TARGET_ARCH}'" >&2
48 if [ ${ESP} = "yes" ]; then
49 BOOTPARTS="${BOOTPARTS} -p efi:=${BOOTFILES}/efi/boot1/boot1.efifat"
52 mkimg -s ${SCHEME} -f ${VMFORMAT} \
68 if [ -c "${DESTDIR}/dev/null" ]; then
69 umount_loop ${DESTDIR}/dev 2>/dev/null
71 umount_loop ${DESTDIR}
72 if [ ! -z "${mddev}" ]; then
73 mdconfig -d -u ${mddev}
80 # Creates the UFS root filesystem for the virtual machine disk,
81 # written to the formatted disk image with mkimg(1).
84 truncate -s ${VMSIZE} ${VMBASE}
85 mddev=$(mdconfig -f ${VMBASE})
86 newfs -L rootfs /dev/${mddev}
87 mount /dev/${mddev} ${DESTDIR}
93 # Creates a new UFS root filesystem and copies the contents of the
94 # current root filesystem into it. This produces a "clean" disk
95 # image without any remnants of files which were created temporarily
96 # during image-creation and have since been deleted (e.g., downloaded
99 mkdir -p ${DESTDIR}/old
100 mdold=$(mdconfig -f ${VMBASE})
101 mount /dev/${mdold} ${DESTDIR}/old
103 truncate -s ${VMSIZE} ${VMBASE}.tmp
104 mkdir -p ${DESTDIR}/new
105 mdnew=$(mdconfig -f ${VMBASE}.tmp)
106 newfs -L rootfs /dev/${mdnew}
107 mount /dev/${mdnew} ${DESTDIR}/new
109 tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new
111 umount_loop /dev/${mdold}
113 mdconfig -d -u ${mdold}
115 umount_loop /dev/${mdnew}
117 tunefs -n enable /dev/${mdnew}
118 mdconfig -d -u ${mdnew}
119 mv ${VMBASE}.tmp ${VMBASE}
123 # Installs the FreeBSD userland/kernel to the virtual machine disk.
126 make DESTDIR=${DESTDIR} \
127 installworld installkernel distribution || \
128 err "\n\nCannot install the base system to ${DESTDIR}."
130 # Bootstrap etcupdate(8) and mergemaster(8) databases.
131 mkdir -p ${DESTDIR}/var/db/etcupdate
132 etcupdate extract -B \
133 -M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
134 -s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate
135 sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \
136 -F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
139 echo '# Custom /etc/fstab for FreeBSD VM images' \
140 > ${DESTDIR}/etc/fstab
141 echo "/dev/${ROOTLABEL}/rootfs / ufs rw 1 1" \
142 >> ${DESTDIR}/etc/fstab
143 if [ -z "${NOSWAP}" ]; then
144 echo '/dev/gpt/swapfs none swap sw 0 0' \
145 >> ${DESTDIR}/etc/fstab
149 hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')"
150 echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf
152 if ! [ -z "${QEMUSTATIC}" ]; then
153 export EMULATOR=/qemu
154 cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR}
157 mkdir -p ${DESTDIR}/dev
158 mount -t devfs devfs ${DESTDIR}/dev
159 chroot ${DESTDIR} ${EMULATOR} /usr/bin/newaliases
160 chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart
161 umount_loop ${DESTDIR}/dev
163 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
168 vm_extra_install_base() {
169 # Prototype. When overridden, runs extra post-installworld commands
170 # as needed, based on the target virtual machine image or cloud
171 # provider image target.
176 vm_extra_enable_services() {
177 if [ ! -z "${VM_RC_LIST}" ]; then
178 for _rcvar in ${VM_RC_LIST}; do
179 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
183 if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then
184 echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \
185 ${DESTDIR}/etc/rc.conf
186 # Expand the filesystem to fill the disk.
187 echo 'growfs_enable="YES"' >> ${DESTDIR}/etc/rc.conf
188 touch ${DESTDIR}/firstboot
194 vm_extra_install_packages() {
195 if [ -z "${VM_EXTRA_PACKAGES}" ]; then
198 mkdir -p ${DESTDIR}/dev
199 mount -t devfs devfs ${DESTDIR}/dev
200 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
201 /usr/sbin/pkg bootstrap -y
202 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
203 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
204 umount_loop ${DESTDIR}/dev
209 vm_extra_install_ports() {
210 # Prototype. When overridden, installs additional ports within the
211 # virtual machine environment.
216 vm_extra_pre_umount() {
217 # Prototype. When overridden, performs additional tasks within the
218 # virtual machine environment prior to unmounting the filesystem.
219 # Note: When overriding this function, removing resolv.conf in the
220 # disk image must be included.
222 if ! [ -z "${QEMUSTATIC}" ]; then
223 rm -f ${DESTDIR}/${EMULATOR}
225 rm -f ${DESTDIR}/etc/resolv.conf
229 vm_extra_pkg_rmcache() {
230 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
231 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
232 /usr/local/sbin/pkg clean -y -a
242 while ! umount ${DIR}; do
244 if [ $i -ge 10 ]; then
245 # This should never happen. But, it has happened.
246 echo "Cannot umount(8) ${DIR}"
247 echo "Something has gone horribly wrong."
257 echo "Creating image... Please wait."
260 write_partition_layout || return 1
265 vm_extra_create_disk() {