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 case "${TARGET}:${TARGET_ARCH}" in
18 amd64:amd64 | i386:i386)
19 mkimg -s gpt -b /boot/pmbr \
20 -p freebsd-boot/bootfs:=/boot/gptboot \
22 -p freebsd-ufs/rootfs:=${VMBASE} \
27 -p apple-boot/bootfs:=/boot/boot1.hfs \
29 -p freebsd-ufs/rootfs:=${VMBASE} \
48 umount ${DESTDIR}/dev 2>/dev/null
50 if [ ! -z "${mddev}" ]; then
51 mdconfig -d -u ${mddev}
58 # Creates the UFS root filesystem for the virtual machine disk,
59 # written to the formatted disk image with mkimg(1).
62 truncate -s ${VMSIZE} ${VMBASE}
63 mddev=$(mdconfig -f ${VMBASE})
64 newfs -j /dev/${mddev}
65 mount /dev/${mddev} ${DESTDIR}
71 # Installs the FreeBSD userland/kernel to the virtual machine disk.
74 make DESTDIR=${DESTDIR} \
75 installworld installkernel distribution || \
76 err "\n\nCannot install the base system to ${DESTDIR}."
78 echo '# Custom /etc/fstab for FreeBSD VM images' \
79 > ${DESTDIR}/etc/fstab
80 echo '/dev/gpt/rootfs / ufs rw 1 1' \
81 >> ${DESTDIR}/etc/fstab
82 if [ -z "${NOSWAP}" ]; then
83 echo '/dev/gpt/swapfs none swap sw 0 0' \
84 >> ${DESTDIR}/etc/fstab
87 # Set hw.vga.textmode=1, with the assumption that the hypervisor
88 # will not be capable of using the graphics console mode.
89 echo '# Comment the next line to enable graphical console mode' \
90 >> ${DESTDIR}/boot/loader.conf
91 echo 'hw.vga.textmode=1' >> ${DESTDIR}/boot/loader.conf
93 mkdir -p ${DESTDIR}/dev
94 mount -t devfs devfs ${DESTDIR}/dev
95 chroot ${DESTDIR} /usr/bin/newaliases
96 chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
99 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
104 vm_extra_install_base() {
105 # Prototype. When overridden, runs extra post-installworld commands
106 # as needed, based on the target virtual machine image or cloud
107 # provider image target.
112 vm_extra_enable_services() {
113 if [ ! -z "${VM_RC_LIST}" ]; then
114 for _rcvar in ${VM_RC_LIST}; do
115 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
122 vm_extra_install_packages() {
123 if [ -z "${VM_EXTRA_PACKAGES}" ]; then
126 mkdir -p ${DESTDIR}/dev
127 mount -t devfs devfs ${DESTDIR}/dev
128 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
129 /usr/sbin/pkg bootstrap -y
130 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
131 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
132 umount ${DESTDIR}/dev
137 vm_extra_install_ports() {
138 # Prototype. When overridden, installs additional ports within the
139 # virtual machine environment.
144 vm_extra_pre_umount() {
145 # Prototype. When overridden, installs additional ports within the
146 # virtual machine environment.
148 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
149 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
150 /usr/local/sbin/pkg clean -y -a
152 rm -f ${DESTDIR}/etc/resolv.conf
160 while ! umount ${DESTDIR}/dev ${DESTDIR}; do
162 if [ $i -ge 10 ]; then
163 # This should never happen. But, it has happened.
164 msg="Cannot umount(8) ${DESTDIR}\n"
165 msg="${msg}Something has gone horribly wrong."
175 echo "Creating image... Please wait."
178 write_partition_layout || return 1
183 vm_extra_create_disk() {