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 apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \
36 -p freebsd-ufs/rootfs:=${VMBASE} \
55 if [ -c "${DESTDIR}/dev/null" ]; then
56 umount_loop ${DESTDIR}/dev 2>/dev/null
58 umount_loop ${DESTDIR}
59 if [ ! -z "${mddev}" ]; then
60 mdconfig -d -u ${mddev}
67 # Creates the UFS root filesystem for the virtual machine disk,
68 # written to the formatted disk image with mkimg(1).
71 truncate -s ${VMSIZE} ${VMBASE}
72 mddev=$(mdconfig -f ${VMBASE})
74 mount /dev/${mddev} ${DESTDIR}
80 # Creates a new UFS root filesystem and copies the contents of the
81 # current root filesystem into it. This produces a "clean" disk
82 # image without any remnants of files which were created temporarily
83 # during image-creation and have since been deleted (e.g., downloaded
86 mkdir -p ${DESTDIR}/old
87 mdold=$(mdconfig -f ${VMBASE})
88 mount /dev/${mdold} ${DESTDIR}/old
90 truncate -s ${VMSIZE} ${VMBASE}.tmp
91 mkdir -p ${DESTDIR}/new
92 mdnew=$(mdconfig -f ${VMBASE}.tmp)
94 mount /dev/${mdnew} ${DESTDIR}/new
96 tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new
98 umount_loop /dev/${mdold}
100 mdconfig -d -u ${mdold}
102 umount_loop /dev/${mdnew}
104 tunefs -j enable /dev/${mdnew}
105 mdconfig -d -u ${mdnew}
106 mv ${VMBASE}.tmp ${VMBASE}
110 # Installs the FreeBSD userland/kernel to the virtual machine disk.
113 make DESTDIR=${DESTDIR} \
114 installworld installkernel distribution || \
115 err "\n\nCannot install the base system to ${DESTDIR}."
117 echo '# Custom /etc/fstab for FreeBSD VM images' \
118 > ${DESTDIR}/etc/fstab
119 echo '/dev/gpt/rootfs / ufs rw 1 1' \
120 >> ${DESTDIR}/etc/fstab
121 if [ -z "${NOSWAP}" ]; then
122 echo '/dev/gpt/swapfs none swap sw 0 0' \
123 >> ${DESTDIR}/etc/fstab
126 mkdir -p ${DESTDIR}/dev
127 mount -t devfs devfs ${DESTDIR}/dev
128 chroot ${DESTDIR} /usr/bin/newaliases
129 chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
130 umount_loop ${DESTDIR}/dev
132 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
137 vm_extra_install_base() {
138 # Prototype. When overridden, runs extra post-installworld commands
139 # as needed, based on the target virtual machine image or cloud
140 # provider image target.
145 vm_extra_enable_services() {
146 if [ ! -z "${VM_RC_LIST}" ]; then
147 for _rcvar in ${VM_RC_LIST}; do
148 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
155 vm_extra_install_packages() {
156 if [ -z "${VM_EXTRA_PACKAGES}" ]; then
159 mkdir -p ${DESTDIR}/dev
160 mount -t devfs devfs ${DESTDIR}/dev
161 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
162 /usr/sbin/pkg bootstrap -y
163 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
164 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
165 umount_loop ${DESTDIR}/dev
170 vm_extra_install_ports() {
171 # Prototype. When overridden, installs additional ports within the
172 # virtual machine environment.
177 vm_extra_pre_umount() {
178 # Prototype. When overridden, installs additional ports within the
179 # virtual machine environment.
181 rm -f ${DESTDIR}/etc/resolv.conf
185 vm_extra_pkg_rmcache() {
186 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
187 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
188 /usr/local/sbin/pkg clean -y -a
198 while ! umount ${DIR}; do
200 if [ $i -ge 10 ]; then
201 # This should never happen. But, it has happened.
202 echo "Cannot umount(8) ${DIR}"
203 echo "Something has gone horribly wrong."
213 echo "Creating image... Please wait."
216 write_partition_layout || return 1
221 vm_extra_create_disk() {