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::1G"
17 _OBJDIR="$(make -C ${WORLDDIR} -V .OBJDIR)"
18 _OBJDIR="$(realpath ${_OBJDIR})"
19 if [ -d "${_OBJDIR%%/usr/src}/${TARGET}.${TARGET_ARCH}" ]; then
20 BOOTFILES="/${_OBJDIR%%/usr/src}/${TARGET}.${TARGET_ARCH}/usr/src/sys/boot"
22 BOOTFILES="/${_OBJDIR}/sys/boot"
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} \
35 mkimg -s apm -f ${VMFORMAT} \
36 -p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \
38 -p freebsd-ufs/rootfs:=${VMBASE} \
57 if [ -c "${DESTDIR}/dev/null" ]; then
58 umount_loop ${DESTDIR}/dev 2>/dev/null
60 umount_loop ${DESTDIR}
61 if [ ! -z "${mddev}" ]; then
62 mdconfig -d -u ${mddev}
69 # Creates the UFS root filesystem for the virtual machine disk,
70 # written to the formatted disk image with mkimg(1).
73 truncate -s ${VMSIZE} ${VMBASE}
74 mddev=$(mdconfig -f ${VMBASE})
76 mount /dev/${mddev} ${DESTDIR}
82 # Creates a new UFS root filesystem and copies the contents of the
83 # current root filesystem into it. This produces a "clean" disk
84 # image without any remnants of files which were created temporarily
85 # during image-creation and have since been deleted (e.g., downloaded
88 mkdir -p ${DESTDIR}/old
89 mdold=$(mdconfig -f ${VMBASE})
90 mount /dev/${mdold} ${DESTDIR}/old
92 truncate -s ${VMSIZE} ${VMBASE}.tmp
93 mkdir -p ${DESTDIR}/new
94 mdnew=$(mdconfig -f ${VMBASE}.tmp)
96 mount /dev/${mdnew} ${DESTDIR}/new
98 tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new
100 umount_loop /dev/${mdold}
102 mdconfig -d -u ${mdold}
104 umount_loop /dev/${mdnew}
106 tunefs -n enable /dev/${mdnew}
107 mdconfig -d -u ${mdnew}
108 mv ${VMBASE}.tmp ${VMBASE}
112 # Installs the FreeBSD userland/kernel to the virtual machine disk.
115 make DESTDIR=${DESTDIR} \
116 installworld installkernel distribution || \
117 err "\n\nCannot install the base system to ${DESTDIR}."
119 echo '# Custom /etc/fstab for FreeBSD VM images' \
120 > ${DESTDIR}/etc/fstab
121 echo '/dev/gpt/rootfs / ufs rw 1 1' \
122 >> ${DESTDIR}/etc/fstab
123 if [ -z "${NOSWAP}" ]; then
124 echo '/dev/gpt/swapfs none swap sw 0 0' \
125 >> ${DESTDIR}/etc/fstab
128 mkdir -p ${DESTDIR}/dev
129 mount -t devfs devfs ${DESTDIR}/dev
130 chroot ${DESTDIR} /usr/bin/newaliases
131 chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
132 umount_loop ${DESTDIR}/dev
134 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
139 vm_extra_install_base() {
140 # Prototype. When overridden, runs extra post-installworld commands
141 # as needed, based on the target virtual machine image or cloud
142 # provider image target.
147 vm_extra_enable_services() {
148 if [ ! -z "${VM_RC_LIST}" ]; then
149 for _rcvar in ${VM_RC_LIST}; do
150 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
157 vm_extra_install_packages() {
158 if [ -z "${VM_EXTRA_PACKAGES}" ]; then
161 mkdir -p ${DESTDIR}/dev
162 mount -t devfs devfs ${DESTDIR}/dev
163 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
164 /usr/sbin/pkg bootstrap -y
165 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
166 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
167 umount_loop ${DESTDIR}/dev
172 vm_extra_install_ports() {
173 # Prototype. When overridden, installs additional ports within the
174 # virtual machine environment.
179 vm_extra_pre_umount() {
180 # Prototype. When overridden, performs additional tasks within the
181 # virtual machine environment prior to unmounting the filesystem.
182 # Note: When overriding this function, removing resolv.conf in the
183 # disk image must be included.
185 rm -f ${DESTDIR}/etc/resolv.conf
189 vm_extra_pkg_rmcache() {
190 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
191 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
192 /usr/local/sbin/pkg clean -y -a
202 while ! umount ${DIR}; do
204 if [ $i -ge 10 ]; then
205 # This should never happen. But, it has happened.
206 echo "Cannot umount(8) ${DIR}"
207 echo "Something has gone horribly wrong."
217 echo "Creating image... Please wait."
220 write_partition_layout || return 1
225 vm_extra_create_disk() {