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 -b ${BOOTFILES}/i386/pmbr/pmbr \
27 -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot \
29 -p freebsd-ufs/rootfs:=${VMBASE} \
34 -p efi:=${BOOTFILES}/efi/boot1/boot1.efifat \
35 -p freebsd:=${VMBASE} \
40 -p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \
42 -p freebsd-ufs/rootfs:=${VMBASE} \
61 if [ -c "${DESTDIR}/dev/null" ]; then
62 umount_loop ${DESTDIR}/dev 2>/dev/null
64 umount_loop ${DESTDIR}
65 if [ ! -z "${mddev}" ]; then
66 mdconfig -d -u ${mddev}
73 # Creates the UFS root filesystem for the virtual machine disk,
74 # written to the formatted disk image with mkimg(1).
77 truncate -s ${VMSIZE} ${VMBASE}
78 mddev=$(mdconfig -f ${VMBASE})
79 newfs -L rootfs /dev/${mddev}
80 mount /dev/${mddev} ${DESTDIR}
86 # Creates a new UFS root filesystem and copies the contents of the
87 # current root filesystem into it. This produces a "clean" disk
88 # image without any remnants of files which were created temporarily
89 # during image-creation and have since been deleted (e.g., downloaded
92 mkdir -p ${DESTDIR}/old
93 mdold=$(mdconfig -f ${VMBASE})
94 mount /dev/${mdold} ${DESTDIR}/old
96 truncate -s ${VMSIZE} ${VMBASE}.tmp
97 mkdir -p ${DESTDIR}/new
98 mdnew=$(mdconfig -f ${VMBASE}.tmp)
99 newfs -L rootfs /dev/${mdnew}
100 mount /dev/${mdnew} ${DESTDIR}/new
102 tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new
104 umount_loop /dev/${mdold}
106 mdconfig -d -u ${mdold}
108 umount_loop /dev/${mdnew}
110 tunefs -j enable /dev/${mdnew}
111 mdconfig -d -u ${mdnew}
112 mv ${VMBASE}.tmp ${VMBASE}
116 # Installs the FreeBSD userland/kernel to the virtual machine disk.
119 make DESTDIR=${DESTDIR} \
120 installworld installkernel distribution || \
121 err "\n\nCannot install the base system to ${DESTDIR}."
123 echo '# Custom /etc/fstab for FreeBSD VM images' \
124 > ${DESTDIR}/etc/fstab
125 echo "/dev/${ROOTLABEL}/rootfs / ufs rw 1 1" \
126 >> ${DESTDIR}/etc/fstab
127 if [ -z "${NOSWAP}" ]; then
128 echo '/dev/gpt/swapfs none swap sw 0 0' \
129 >> ${DESTDIR}/etc/fstab
132 mkdir -p ${DESTDIR}/dev
133 mount -t devfs devfs ${DESTDIR}/dev
134 chroot ${DESTDIR} /usr/bin/newaliases
135 chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
136 umount_loop ${DESTDIR}/dev
138 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
143 vm_extra_install_base() {
144 # Prototype. When overridden, runs extra post-installworld commands
145 # as needed, based on the target virtual machine image or cloud
146 # provider image target.
151 vm_extra_enable_services() {
152 if [ ! -z "${VM_RC_LIST}" ]; then
153 for _rcvar in ${VM_RC_LIST}; do
154 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
161 vm_extra_install_packages() {
162 if [ -z "${VM_EXTRA_PACKAGES}" ]; then
165 mkdir -p ${DESTDIR}/dev
166 mount -t devfs devfs ${DESTDIR}/dev
167 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
168 /usr/sbin/pkg bootstrap -y
169 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
170 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
171 umount_loop ${DESTDIR}/dev
176 vm_extra_install_ports() {
177 # Prototype. When overridden, installs additional ports within the
178 # virtual machine environment.
183 vm_extra_pre_umount() {
184 # Prototype. When overridden, installs additional ports within the
185 # virtual machine environment.
187 rm -f ${DESTDIR}/etc/resolv.conf
191 vm_extra_pkg_rmcache() {
192 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
193 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
194 /usr/local/sbin/pkg clean -y -a
204 while ! umount ${DIR}; do
206 if [ $i -ge 10 ]; then
207 # This should never happen. But, it has happened.
208 echo "Cannot umount(8) ${DIR}"
209 echo "Something has gone horribly wrong."
219 echo "Creating image... Please wait."
222 write_partition_layout || return 1
227 vm_extra_create_disk() {