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 if [ -d "${_OBJDIR%%/usr/src}/${TARGET}.${TARGET_ARCH}" ]; then
19 BOOTFILES="/${_OBJDIR%%/usr/src}/${TARGET}.${TARGET_ARCH}/usr/src/sys/boot"
21 BOOTFILES="/${_OBJDIR}/sys/boot"
24 case "${TARGET}:${TARGET_ARCH}" in
25 amd64:amd64 | i386:i386)
26 mkimg -s gpt -f ${VMFORMAT} \
27 -b ${BOOTFILES}/i386/pmbr/pmbr \
28 -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot \
30 -p freebsd-ufs/rootfs:=${VMBASE} \
34 mkimg -s apm -f ${VMFORMAT} \
35 -p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \
37 -p freebsd-ufs/rootfs:=${VMBASE} \
56 if [ -c "${DESTDIR}/dev/null" ]; then
57 umount_loop ${DESTDIR}/dev 2>/dev/null
59 umount_loop ${DESTDIR}
60 if [ ! -z "${mddev}" ]; then
61 mdconfig -d -u ${mddev}
68 # Creates the UFS root filesystem for the virtual machine disk,
69 # written to the formatted disk image with mkimg(1).
72 truncate -s ${VMSIZE} ${VMBASE}
73 mddev=$(mdconfig -f ${VMBASE})
75 mount /dev/${mddev} ${DESTDIR}
81 # Creates a new UFS root filesystem and copies the contents of the
82 # current root filesystem into it. This produces a "clean" disk
83 # image without any remnants of files which were created temporarily
84 # during image-creation and have since been deleted (e.g., downloaded
87 mkdir -p ${DESTDIR}/old
88 mdold=$(mdconfig -f ${VMBASE})
89 mount /dev/${mdold} ${DESTDIR}/old
91 truncate -s ${VMSIZE} ${VMBASE}.tmp
92 mkdir -p ${DESTDIR}/new
93 mdnew=$(mdconfig -f ${VMBASE}.tmp)
95 mount /dev/${mdnew} ${DESTDIR}/new
97 tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new
99 umount_loop /dev/${mdold}
101 mdconfig -d -u ${mdold}
103 umount_loop /dev/${mdnew}
105 tunefs -j enable /dev/${mdnew}
106 mdconfig -d -u ${mdnew}
107 mv ${VMBASE}.tmp ${VMBASE}
111 # Installs the FreeBSD userland/kernel to the virtual machine disk.
114 make DESTDIR=${DESTDIR} \
115 installworld installkernel distribution || \
116 err "\n\nCannot install the base system to ${DESTDIR}."
118 echo '# Custom /etc/fstab for FreeBSD VM images' \
119 > ${DESTDIR}/etc/fstab
120 echo '/dev/gpt/rootfs / ufs rw 1 1' \
121 >> ${DESTDIR}/etc/fstab
122 if [ -z "${NOSWAP}" ]; then
123 echo '/dev/gpt/swapfs none swap sw 0 0' \
124 >> ${DESTDIR}/etc/fstab
127 mkdir -p ${DESTDIR}/dev
128 mount -t devfs devfs ${DESTDIR}/dev
129 chroot ${DESTDIR} /usr/bin/newaliases
130 chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
131 umount_loop ${DESTDIR}/dev
133 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
138 vm_extra_install_base() {
139 # Prototype. When overridden, runs extra post-installworld commands
140 # as needed, based on the target virtual machine image or cloud
141 # provider image target.
146 vm_extra_enable_services() {
147 if [ ! -z "${VM_RC_LIST}" ]; then
148 for _rcvar in ${VM_RC_LIST}; do
149 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
156 vm_extra_install_packages() {
157 if [ -z "${VM_EXTRA_PACKAGES}" ]; then
160 mkdir -p ${DESTDIR}/dev
161 mount -t devfs devfs ${DESTDIR}/dev
162 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
163 /usr/sbin/pkg bootstrap -y
164 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
165 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
166 umount_loop ${DESTDIR}/dev
171 vm_extra_install_ports() {
172 # Prototype. When overridden, installs additional ports within the
173 # virtual machine environment.
178 vm_extra_pre_umount() {
179 # Prototype. When overridden, performs additional tasks within the
180 # virtual machine environment prior to unmounting the filesystem.
181 # Note: When overriding this function, removing resolv.conf in the
182 # disk image must be included.
184 rm -f ${DESTDIR}/etc/resolv.conf
188 vm_extra_pkg_rmcache() {
189 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
190 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
191 /usr/local/sbin/pkg clean -y -a
201 while ! umount ${DIR}; do
203 if [ $i -ge 10 ]; then
204 # This should never happen. But, it has happened.
205 echo "Cannot umount(8) ${DIR}"
206 echo "Something has gone horribly wrong."
216 echo "Creating image... Please wait."
219 write_partition_layout || return 1
224 vm_extra_create_disk() {