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 mkdir -p ${DESTDIR}/dev
152 mount -t devfs devfs ${DESTDIR}/dev
153 chroot ${DESTDIR} /usr/bin/newaliases
154 chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
155 umount_loop ${DESTDIR}/dev
157 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
162 vm_extra_install_base() {
163 # Prototype. When overridden, runs extra post-installworld commands
164 # as needed, based on the target virtual machine image or cloud
165 # provider image target.
170 vm_extra_enable_services() {
171 if [ ! -z "${VM_RC_LIST}" ]; then
172 for _rcvar in ${VM_RC_LIST}; do
173 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
177 if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then
178 echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \
179 ${DESTDIR}/etc/rc.conf
185 vm_extra_install_packages() {
186 if [ -z "${VM_EXTRA_PACKAGES}" ]; then
189 mkdir -p ${DESTDIR}/dev
190 mount -t devfs devfs ${DESTDIR}/dev
191 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
192 /usr/sbin/pkg bootstrap -y
193 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
194 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
195 umount_loop ${DESTDIR}/dev
200 vm_extra_install_ports() {
201 # Prototype. When overridden, installs additional ports within the
202 # virtual machine environment.
207 vm_extra_pre_umount() {
208 # Prototype. When overridden, performs additional tasks within the
209 # virtual machine environment prior to unmounting the filesystem.
210 # Note: When overriding this function, removing resolv.conf in the
211 # disk image must be included.
213 rm -f ${DESTDIR}/etc/resolv.conf
217 vm_extra_pkg_rmcache() {
218 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
219 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
220 /usr/local/sbin/pkg clean -y -a
230 while ! umount ${DIR}; do
232 if [ $i -ge 10 ]; then
233 # This should never happen. But, it has happened.
234 echo "Cannot umount(8) ${DIR}"
235 echo "Something has gone horribly wrong."
245 echo "Creating image... Please wait."
248 write_partition_layout || return 1
253 vm_extra_create_disk() {