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)
27 mkimg -s gpt -f ${VMFORMAT} \
28 -b ${BOOTFILES}/i386/pmbr/pmbr \
29 -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot \
31 -p freebsd-ufs/rootfs:=${VMBASE} \
36 espfilename=$(mktemp /tmp/efiboot.XXXXXX)
37 make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi
38 mkimg -s mbr -f ${VMFORMAT} \
39 -p efi:=${espfilename} \
40 -p freebsd:=${VMBASE} \
45 mkimg -s apm -f ${VMFORMAT} \
46 -p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \
48 -p freebsd-ufs/rootfs:=${VMBASE} \
67 if [ -c "${DESTDIR}/dev/null" ]; then
68 umount_loop ${DESTDIR}/dev 2>/dev/null
70 umount_loop ${DESTDIR}
71 if [ ! -z "${mddev}" ]; then
72 mdconfig -d -u ${mddev}
79 # Creates the UFS root filesystem for the virtual machine disk,
80 # written to the formatted disk image with mkimg(1).
83 truncate -s ${VMSIZE} ${VMBASE}
84 mddev=$(mdconfig -f ${VMBASE})
85 newfs -L rootfs /dev/${mddev}
86 mount /dev/${mddev} ${DESTDIR}
92 # Creates a new UFS root filesystem and copies the contents of the
93 # current root filesystem into it. This produces a "clean" disk
94 # image without any remnants of files which were created temporarily
95 # during image-creation and have since been deleted (e.g., downloaded
98 mkdir -p ${DESTDIR}/old
99 mdold=$(mdconfig -f ${VMBASE})
100 mount /dev/${mdold} ${DESTDIR}/old
102 truncate -s ${VMSIZE} ${VMBASE}.tmp
103 mkdir -p ${DESTDIR}/new
104 mdnew=$(mdconfig -f ${VMBASE}.tmp)
105 newfs -L rootfs /dev/${mdnew}
106 mount /dev/${mdnew} ${DESTDIR}/new
108 tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new
110 umount_loop /dev/${mdold}
112 mdconfig -d -u ${mdold}
114 umount_loop /dev/${mdnew}
116 tunefs -n enable /dev/${mdnew}
117 mdconfig -d -u ${mdnew}
118 mv ${VMBASE}.tmp ${VMBASE}
122 # Installs the FreeBSD userland/kernel to the virtual machine disk.
125 make DESTDIR=${DESTDIR} \
126 installworld installkernel distribution || \
127 err "\n\nCannot install the base system to ${DESTDIR}."
129 # Bootstrap etcupdate(8) and mergemaster(8) databases.
130 mkdir -p ${DESTDIR}/var/db/etcupdate
131 etcupdate extract -B \
132 -M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
133 -s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate
134 sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \
135 -F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
138 echo '# Custom /etc/fstab for FreeBSD VM images' \
139 > ${DESTDIR}/etc/fstab
140 echo "/dev/${ROOTLABEL}/rootfs / ufs rw 1 1" \
141 >> ${DESTDIR}/etc/fstab
142 if [ -z "${NOSWAP}" ]; then
143 echo '/dev/gpt/swapfs none swap sw 0 0' \
144 >> ${DESTDIR}/etc/fstab
148 hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')"
149 echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf
151 if ! [ -z "${QEMUSTATIC}" ]; then
152 export EMULATOR=/qemu
153 cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR}
156 mkdir -p ${DESTDIR}/dev
157 mount -t devfs devfs ${DESTDIR}/dev
158 chroot ${DESTDIR} ${EMULATOR} /usr/bin/newaliases
159 chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart
160 umount_loop ${DESTDIR}/dev
162 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
167 vm_extra_install_base() {
168 # Prototype. When overridden, runs extra post-installworld commands
169 # as needed, based on the target virtual machine image or cloud
170 # provider image target.
175 vm_extra_enable_services() {
176 if [ ! -z "${VM_RC_LIST}" ]; then
177 for _rcvar in ${VM_RC_LIST}; do
178 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
182 if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then
183 echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \
184 ${DESTDIR}/etc/rc.conf
185 # Expand the filesystem to fill the disk.
186 echo 'growfs_enable="YES"' >> ${DESTDIR}/etc/rc.conf
187 touch ${DESTDIR}/firstboot
193 vm_extra_install_packages() {
194 if [ -z "${VM_EXTRA_PACKAGES}" ]; then
197 mkdir -p ${DESTDIR}/dev
198 mount -t devfs devfs ${DESTDIR}/dev
199 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
200 /usr/sbin/pkg bootstrap -y
201 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
202 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
203 umount_loop ${DESTDIR}/dev
208 vm_extra_install_ports() {
209 # Prototype. When overridden, installs additional ports within the
210 # virtual machine environment.
215 vm_extra_pre_umount() {
216 # Prototype. When overridden, performs additional tasks within the
217 # virtual machine environment prior to unmounting the filesystem.
218 # Note: When overriding this function, removing resolv.conf in the
219 # disk image must be included.
221 if ! [ -z "${QEMUSTATIC}" ]; then
222 rm -f ${DESTDIR}/${EMULATOR}
224 rm -f ${DESTDIR}/etc/resolv.conf
228 vm_extra_pkg_rmcache() {
229 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
230 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
231 /usr/local/sbin/pkg clean -y -a
241 while ! umount ${DIR}; do
243 if [ $i -ge 10 ]; then
244 # This should never happen. But, it has happened.
245 echo "Cannot umount(8) ${DIR}"
246 echo "Something has gone horribly wrong."
256 echo "Creating image... Please wait."
259 write_partition_layout || return 1
264 vm_extra_create_disk() {