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::${SWAPSIZE}"
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/stand"
22 BOOTFILES="/${_OBJDIR}/stand"
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 mbr -f ${VMFORMAT} \
36 -p efi:=${BOOTFILES}/efi/boot1/boot1.efifat \
37 -p freebsd:=${VMBASE} \
41 mkimg -s apm -f ${VMFORMAT} \
42 -p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \
44 -p freebsd-ufs/rootfs:=${VMBASE} \
63 if [ -c "${DESTDIR}/dev/null" ]; then
64 umount_loop ${DESTDIR}/dev 2>/dev/null
66 umount_loop ${DESTDIR}
67 if [ ! -z "${mddev}" ]; then
68 mdconfig -d -u ${mddev}
75 # Creates the UFS root filesystem for the virtual machine disk,
76 # written to the formatted disk image with mkimg(1).
79 truncate -s ${VMSIZE} ${VMBASE}
80 mddev=$(mdconfig -f ${VMBASE})
81 newfs -L rootfs /dev/${mddev}
82 mount /dev/${mddev} ${DESTDIR}
88 # Creates a new UFS root filesystem and copies the contents of the
89 # current root filesystem into it. This produces a "clean" disk
90 # image without any remnants of files which were created temporarily
91 # during image-creation and have since been deleted (e.g., downloaded
94 mkdir -p ${DESTDIR}/old
95 mdold=$(mdconfig -f ${VMBASE})
96 mount /dev/${mdold} ${DESTDIR}/old
98 truncate -s ${VMSIZE} ${VMBASE}.tmp
99 mkdir -p ${DESTDIR}/new
100 mdnew=$(mdconfig -f ${VMBASE}.tmp)
101 newfs -L rootfs /dev/${mdnew}
102 mount /dev/${mdnew} ${DESTDIR}/new
104 tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new
106 umount_loop /dev/${mdold}
108 mdconfig -d -u ${mdold}
110 umount_loop /dev/${mdnew}
112 tunefs -n enable /dev/${mdnew}
113 mdconfig -d -u ${mdnew}
114 mv ${VMBASE}.tmp ${VMBASE}
118 # Installs the FreeBSD userland/kernel to the virtual machine disk.
121 make DESTDIR=${DESTDIR} \
122 installworld installkernel distribution || \
123 err "\n\nCannot install the base system to ${DESTDIR}."
125 # Bootstrap etcupdate(8) and mergemaster(8) databases.
126 mkdir -p ${DESTDIR}/var/db/etcupdate
127 etcupdate extract -B \
128 -M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
129 -s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate
130 sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \
131 -F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
134 echo '# Custom /etc/fstab for FreeBSD VM images' \
135 > ${DESTDIR}/etc/fstab
136 echo "/dev/${ROOTLABEL}/rootfs / ufs rw 1 1" \
137 >> ${DESTDIR}/etc/fstab
138 if [ -z "${NOSWAP}" ]; then
139 echo '/dev/gpt/swapfs none swap sw 0 0' \
140 >> ${DESTDIR}/etc/fstab
144 hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')"
145 echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf
147 mkdir -p ${DESTDIR}/dev
148 mount -t devfs devfs ${DESTDIR}/dev
149 chroot ${DESTDIR} /usr/bin/newaliases
150 chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
151 umount_loop ${DESTDIR}/dev
153 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
158 vm_extra_install_base() {
159 # Prototype. When overridden, runs extra post-installworld commands
160 # as needed, based on the target virtual machine image or cloud
161 # provider image target.
166 vm_extra_enable_services() {
167 if [ ! -z "${VM_RC_LIST}" ]; then
168 for _rcvar in ${VM_RC_LIST}; do
169 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
173 if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then
174 echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \
175 ${DESTDIR}/etc/rc.conf
181 vm_extra_install_packages() {
182 if [ -z "${VM_EXTRA_PACKAGES}" ]; then
185 mkdir -p ${DESTDIR}/dev
186 mount -t devfs devfs ${DESTDIR}/dev
187 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
188 /usr/sbin/pkg bootstrap -y
189 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
190 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
191 umount_loop ${DESTDIR}/dev
196 vm_extra_install_ports() {
197 # Prototype. When overridden, installs additional ports within the
198 # virtual machine environment.
203 vm_extra_pre_umount() {
204 # Prototype. When overridden, performs additional tasks within the
205 # virtual machine environment prior to unmounting the filesystem.
206 # Note: When overriding this function, removing resolv.conf in the
207 # disk image must be included.
209 rm -f ${DESTDIR}/etc/resolv.conf
213 vm_extra_pkg_rmcache() {
214 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
215 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
216 /usr/local/sbin/pkg clean -y -a
226 while ! umount ${DIR}; do
228 if [ $i -ge 10 ]; then
229 # This should never happen. But, it has happened.
230 echo "Cannot umount(8) ${DIR}"
231 echo "Something has gone horribly wrong."
241 echo "Creating image... Please wait."
244 write_partition_layout || return 1
249 vm_extra_create_disk() {