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)
28 espfilename=$(mktemp /tmp/efiboot.XXXXXX)
29 make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi
30 mkimg -s gpt -f ${VMFORMAT} \
31 -b ${BOOTFILES}/i386/pmbr/pmbr \
32 -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot \
33 -p efi:=${espfilename} \
35 -p freebsd-ufs/rootfs:=${VMBASE} \
41 espfilename=$(mktemp /tmp/efiboot.XXXXXX)
42 make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi
43 mkimg -s mbr -f ${VMFORMAT} \
44 -p efi:=${espfilename} \
45 -p freebsd:=${VMBASE} \
50 mkimg -s apm -f ${VMFORMAT} \
51 -p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \
53 -p freebsd-ufs/rootfs:=${VMBASE} \
72 if [ -c "${DESTDIR}/dev/null" ]; then
73 umount_loop ${DESTDIR}/dev 2>/dev/null
75 umount_loop ${DESTDIR}
76 if [ ! -z "${mddev}" ]; then
77 mdconfig -d -u ${mddev}
84 # Creates the UFS root filesystem for the virtual machine disk,
85 # written to the formatted disk image with mkimg(1).
88 truncate -s ${VMSIZE} ${VMBASE}
89 mddev=$(mdconfig -f ${VMBASE})
90 newfs -L rootfs /dev/${mddev}
91 mount /dev/${mddev} ${DESTDIR}
97 # Creates a new UFS root filesystem and copies the contents of the
98 # current root filesystem into it. This produces a "clean" disk
99 # image without any remnants of files which were created temporarily
100 # during image-creation and have since been deleted (e.g., downloaded
103 mkdir -p ${DESTDIR}/old
104 mdold=$(mdconfig -f ${VMBASE})
105 mount /dev/${mdold} ${DESTDIR}/old
107 truncate -s ${VMSIZE} ${VMBASE}.tmp
108 mkdir -p ${DESTDIR}/new
109 mdnew=$(mdconfig -f ${VMBASE}.tmp)
110 newfs -L rootfs /dev/${mdnew}
111 mount /dev/${mdnew} ${DESTDIR}/new
113 tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new
115 umount_loop /dev/${mdold}
117 mdconfig -d -u ${mdold}
119 umount_loop /dev/${mdnew}
121 tunefs -n enable /dev/${mdnew}
122 mdconfig -d -u ${mdnew}
123 mv ${VMBASE}.tmp ${VMBASE}
127 # Installs the FreeBSD userland/kernel to the virtual machine disk.
130 make DESTDIR=${DESTDIR} \
131 installworld installkernel distribution || \
132 err "\n\nCannot install the base system to ${DESTDIR}."
134 # Bootstrap etcupdate(8) and mergemaster(8) databases.
135 mkdir -p ${DESTDIR}/var/db/etcupdate
136 etcupdate extract -B \
137 -M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
138 -s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate
139 sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \
140 -F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
143 echo '# Custom /etc/fstab for FreeBSD VM images' \
144 > ${DESTDIR}/etc/fstab
145 echo "/dev/${ROOTLABEL}/rootfs / ufs rw 1 1" \
146 >> ${DESTDIR}/etc/fstab
147 if [ -z "${NOSWAP}" ]; then
148 echo '/dev/gpt/swapfs none swap sw 0 0' \
149 >> ${DESTDIR}/etc/fstab
153 hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')"
154 echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf
156 if ! [ -z "${QEMUSTATIC}" ]; then
157 export EMULATOR=/qemu
158 cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR}
161 mkdir -p ${DESTDIR}/dev
162 mount -t devfs devfs ${DESTDIR}/dev
163 chroot ${DESTDIR} ${EMULATOR} /usr/bin/newaliases
164 chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart
165 umount_loop ${DESTDIR}/dev
167 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
172 vm_extra_install_base() {
173 # Prototype. When overridden, runs extra post-installworld commands
174 # as needed, based on the target virtual machine image or cloud
175 # provider image target.
180 vm_extra_enable_services() {
181 if [ ! -z "${VM_RC_LIST}" ]; then
182 for _rcvar in ${VM_RC_LIST}; do
183 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
187 if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then
188 echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \
189 ${DESTDIR}/etc/rc.conf
190 # Expand the filesystem to fill the disk.
191 echo 'growfs_enable="YES"' >> ${DESTDIR}/etc/rc.conf
192 touch ${DESTDIR}/firstboot
198 vm_extra_install_packages() {
199 if [ -z "${VM_EXTRA_PACKAGES}" ]; then
202 mkdir -p ${DESTDIR}/dev
203 mount -t devfs devfs ${DESTDIR}/dev
204 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
205 /usr/sbin/pkg bootstrap -y
206 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
207 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
208 umount_loop ${DESTDIR}/dev
213 vm_extra_install_ports() {
214 # Prototype. When overridden, installs additional ports within the
215 # virtual machine environment.
220 vm_extra_pre_umount() {
221 # Prototype. When overridden, performs additional tasks within the
222 # virtual machine environment prior to unmounting the filesystem.
223 # Note: When overriding this function, removing resolv.conf in the
224 # disk image must be included.
226 if ! [ -z "${QEMUSTATIC}" ]; then
227 rm -f ${DESTDIR}/${EMULATOR}
229 rm -f ${DESTDIR}/etc/resolv.conf
233 vm_extra_pkg_rmcache() {
234 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
235 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
236 /usr/local/sbin/pkg clean -y -a
246 while ! umount ${DIR}; do
248 if [ $i -ge 10 ]; then
249 # This should never happen. But, it has happened.
250 echo "Cannot umount(8) ${DIR}"
251 echo "Something has gone horribly wrong."
261 echo "Creating image... Please wait."
264 write_partition_layout || return 1
269 vm_extra_create_disk() {