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 _OBJDIR="$(make -C ${WORLDDIR} -V .OBJDIR)"
18 _OBJDIR="$(realpath ${_OBJDIR})"
19 _WORLDDIR="$(realpath ${WORLDDIR})"
20 if [ -d "${_OBJDIR%%${_WORLDDIR}}/${TARGET}.${TARGET_ARCH}" ]; then
21 BOOTFILES="/${_OBJDIR%%${_WORLDDIR}}/${TARGET}.${TARGET_ARCH}${_WORLDDIR}/stand"
23 BOOTFILES="/${_OBJDIR}/stand"
26 case "${TARGET}:${TARGET_ARCH}" in
27 amd64:amd64 | i386:i386)
28 mkimg -s gpt -f ${VMFORMAT} \
29 -b ${BOOTFILES}/i386/pmbr/pmbr \
30 -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot \
32 -p freebsd-ufs/rootfs:=${VMBASE} \
36 mkimg -s mbr -f ${VMFORMAT} \
37 -p efi:=${BOOTFILES}/efi/boot1/boot1.efifat \
38 -p freebsd:=${VMBASE} \
42 mkimg -s apm -f ${VMFORMAT} \
43 -p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \
45 -p freebsd-ufs/rootfs:=${VMBASE} \
64 if [ -c "${DESTDIR}/dev/null" ]; then
65 umount_loop ${DESTDIR}/dev 2>/dev/null
67 umount_loop ${DESTDIR}
68 if [ ! -z "${mddev}" ]; then
69 mdconfig -d -u ${mddev}
76 # Creates the UFS root filesystem for the virtual machine disk,
77 # written to the formatted disk image with mkimg(1).
80 truncate -s ${VMSIZE} ${VMBASE}
81 mddev=$(mdconfig -f ${VMBASE})
82 newfs -L rootfs /dev/${mddev}
83 mount /dev/${mddev} ${DESTDIR}
89 # Creates a new UFS root filesystem and copies the contents of the
90 # current root filesystem into it. This produces a "clean" disk
91 # image without any remnants of files which were created temporarily
92 # during image-creation and have since been deleted (e.g., downloaded
95 mkdir -p ${DESTDIR}/old
96 mdold=$(mdconfig -f ${VMBASE})
97 mount /dev/${mdold} ${DESTDIR}/old
99 truncate -s ${VMSIZE} ${VMBASE}.tmp
100 mkdir -p ${DESTDIR}/new
101 mdnew=$(mdconfig -f ${VMBASE}.tmp)
102 newfs -L rootfs /dev/${mdnew}
103 mount /dev/${mdnew} ${DESTDIR}/new
105 tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new
107 umount_loop /dev/${mdold}
109 mdconfig -d -u ${mdold}
111 umount_loop /dev/${mdnew}
113 tunefs -n enable /dev/${mdnew}
114 mdconfig -d -u ${mdnew}
115 mv ${VMBASE}.tmp ${VMBASE}
119 # Installs the FreeBSD userland/kernel to the virtual machine disk.
122 make DESTDIR=${DESTDIR} \
123 installworld installkernel distribution || \
124 err "\n\nCannot install the base system to ${DESTDIR}."
126 # Bootstrap etcupdate(8) and mergemaster(8) databases.
127 mkdir -p ${DESTDIR}/var/db/etcupdate
128 etcupdate extract -B \
129 -M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
130 -s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate
131 sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \
132 -F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
135 echo '# Custom /etc/fstab for FreeBSD VM images' \
136 > ${DESTDIR}/etc/fstab
137 echo "/dev/${ROOTLABEL}/rootfs / ufs rw 1 1" \
138 >> ${DESTDIR}/etc/fstab
139 if [ -z "${NOSWAP}" ]; then
140 echo '/dev/gpt/swapfs none swap sw 0 0' \
141 >> ${DESTDIR}/etc/fstab
145 hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')"
146 echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf
148 if ! [ -z "${QEMUSTATIC}" ]; then
149 export EMULATOR=/qemu
150 cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR}
153 mkdir -p ${DESTDIR}/dev
154 mount -t devfs devfs ${DESTDIR}/dev
155 chroot ${DESTDIR} ${EMULATOR} /usr/bin/newaliases
156 chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart
157 umount_loop ${DESTDIR}/dev
159 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
164 vm_extra_install_base() {
165 # Prototype. When overridden, runs extra post-installworld commands
166 # as needed, based on the target virtual machine image or cloud
167 # provider image target.
172 vm_extra_enable_services() {
173 if [ ! -z "${VM_RC_LIST}" ]; then
174 for _rcvar in ${VM_RC_LIST}; do
175 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
179 if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then
180 echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \
181 ${DESTDIR}/etc/rc.conf
187 vm_extra_install_packages() {
188 if [ -z "${VM_EXTRA_PACKAGES}" ]; then
191 mkdir -p ${DESTDIR}/dev
192 mount -t devfs devfs ${DESTDIR}/dev
193 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
194 /usr/sbin/pkg bootstrap -y
195 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
196 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
197 umount_loop ${DESTDIR}/dev
202 vm_extra_install_ports() {
203 # Prototype. When overridden, installs additional ports within the
204 # virtual machine environment.
209 vm_extra_pre_umount() {
210 # Prototype. When overridden, performs additional tasks within the
211 # virtual machine environment prior to unmounting the filesystem.
212 # Note: When overriding this function, removing resolv.conf in the
213 # disk image must be included.
215 if ! [ -z "${QEMUSTATIC}" ]; then
216 rm -f ${DESTDIR}/${EMULATOR}
218 rm -f ${DESTDIR}/etc/resolv.conf
222 vm_extra_pkg_rmcache() {
223 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
224 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
225 /usr/local/sbin/pkg clean -y -a
235 while ! umount ${DIR}; do
237 if [ $i -ge 10 ]; then
238 # This should never happen. But, it has happened.
239 echo "Cannot umount(8) ${DIR}"
240 echo "Something has gone horribly wrong."
250 echo "Creating image... Please wait."
253 write_partition_layout || return 1
258 vm_extra_create_disk() {