xref: /freebsd/release/tools/vmimage.subr (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
1#!/bin/sh
2#
3# $FreeBSD$
4#
5#
6# Common functions for virtual machine image build scripts.
7#
8
9scriptdir=$(dirname $(realpath $0))
10. ${scriptdir}/../../tools/boot/install-boot.sh
11
12export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
13trap "cleanup" INT QUIT TRAP ABRT TERM
14
15write_partition_layout() {
16	if [ -z "${NOSWAP}" ]; then
17		SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}"
18	fi
19
20	BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
21		WITH_UNIFIED_OBJDIR=yes \
22		make -C ${WORLDDIR}/stand -V .OBJDIR)"
23	BOOTFILES="$(realpath ${BOOTFILES})"
24
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 \
30				${SWAPOPT} \
31				-p freebsd-ufs/rootfs:=${VMBASE} \
32				-o ${VMIMAGE}
33			;;
34		arm64:aarch64)
35			# Create an ESP
36			espfilename=$(mktemp /tmp/efiboot.XXXXXX)
37			make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi
38			mkimg -s mbr -f ${VMFORMAT} \
39				-p efi:=${espfilename} \
40				-p freebsd:=${VMBASE} \
41				-o ${VMIMAGE}
42			rm ${espfilename}
43			;;
44		powerpc:powerpc*)
45			mkimg -s apm -f ${VMFORMAT} \
46				-p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \
47				${SWAPOPT} \
48				-p freebsd-ufs/rootfs:=${VMBASE} \
49				-o ${VMIMAGE}
50			;;
51		*)
52			# ENOTSUPP
53			return 1
54			;;
55	esac
56
57	return 0
58}
59
60err() {
61	printf "${@}\n"
62	cleanup
63	return 1
64}
65
66cleanup() {
67	if [ -c "${DESTDIR}/dev/null" ]; then
68		umount_loop ${DESTDIR}/dev 2>/dev/null
69	fi
70	umount_loop ${DESTDIR}
71	if [ ! -z "${mddev}" ]; then
72		mdconfig -d -u ${mddev}
73	fi
74
75	return 0
76}
77
78vm_create_base() {
79	# Creates the UFS root filesystem for the virtual machine disk,
80	# written to the formatted disk image with mkimg(1).
81
82	mkdir -p ${DESTDIR}
83	truncate -s ${VMSIZE} ${VMBASE}
84	mddev=$(mdconfig -f ${VMBASE})
85	newfs -L rootfs /dev/${mddev}
86	mount /dev/${mddev} ${DESTDIR}
87
88	return 0
89}
90
91vm_copy_base() {
92	# Creates a new UFS root filesystem and copies the contents of the
93	# current root filesystem into it.  This produces a "clean" disk
94	# image without any remnants of files which were created temporarily
95	# during image-creation and have since been deleted (e.g., downloaded
96	# package archives).
97
98	mkdir -p ${DESTDIR}/old
99	mdold=$(mdconfig -f ${VMBASE})
100	mount /dev/${mdold} ${DESTDIR}/old
101
102	truncate -s ${VMSIZE} ${VMBASE}.tmp
103	mkdir -p ${DESTDIR}/new
104	mdnew=$(mdconfig -f ${VMBASE}.tmp)
105	newfs -L rootfs /dev/${mdnew}
106	mount /dev/${mdnew} ${DESTDIR}/new
107
108	tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new
109
110	umount_loop /dev/${mdold}
111	rmdir ${DESTDIR}/old
112	mdconfig -d -u ${mdold}
113
114	umount_loop /dev/${mdnew}
115	rmdir ${DESTDIR}/new
116	tunefs -n enable /dev/${mdnew}
117	mdconfig -d -u ${mdnew}
118	mv ${VMBASE}.tmp ${VMBASE}
119}
120
121vm_install_base() {
122	# Installs the FreeBSD userland/kernel to the virtual machine disk.
123
124	cd ${WORLDDIR} && \
125		make DESTDIR=${DESTDIR} \
126		installworld installkernel distribution || \
127		err "\n\nCannot install the base system to ${DESTDIR}."
128
129	# Bootstrap etcupdate(8) and mergemaster(8) databases.
130	mkdir -p ${DESTDIR}/var/db/etcupdate
131	etcupdate extract -B \
132		-M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
133		-s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate
134	sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \
135		-F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
136		-D ${DESTDIR}
137
138	echo '# Custom /etc/fstab for FreeBSD VM images' \
139		> ${DESTDIR}/etc/fstab
140	echo "/dev/${ROOTLABEL}/rootfs   /       ufs     rw      1       1" \
141		>> ${DESTDIR}/etc/fstab
142	if [ -z "${NOSWAP}" ]; then
143		echo '/dev/gpt/swapfs  none    swap    sw      0       0' \
144			>> ${DESTDIR}/etc/fstab
145	fi
146
147	local hostname
148	hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')"
149	echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf
150
151	if ! [ -z "${QEMUSTATIC}" ]; then
152		export EMULATOR=/qemu
153		cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR}
154	fi
155
156	mkdir -p ${DESTDIR}/dev
157	mount -t devfs devfs ${DESTDIR}/dev
158	chroot ${DESTDIR} ${EMULATOR} /usr/bin/newaliases
159	chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart
160	umount_loop ${DESTDIR}/dev
161
162	cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
163
164	return 0
165}
166
167vm_extra_install_base() {
168	# Prototype.  When overridden, runs extra post-installworld commands
169	# as needed, based on the target virtual machine image or cloud
170	# provider image target.
171
172	return 0
173}
174
175vm_extra_enable_services() {
176	if [ ! -z "${VM_RC_LIST}" ]; then
177		for _rcvar in ${VM_RC_LIST}; do
178			echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
179		done
180	fi
181
182	if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then
183		echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \
184			${DESTDIR}/etc/rc.conf
185		# Expand the filesystem to fill the disk.
186		echo 'growfs_enable="YES"' >> ${DESTDIR}/etc/rc.conf
187		touch ${DESTDIR}/firstboot
188	fi
189
190	return 0
191}
192
193vm_extra_install_packages() {
194	if [ -z "${VM_EXTRA_PACKAGES}" ]; then
195		return 0
196	fi
197	mkdir -p ${DESTDIR}/dev
198	mount -t devfs devfs ${DESTDIR}/dev
199	chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
200		/usr/sbin/pkg bootstrap -y
201	chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
202		/usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
203	umount_loop ${DESTDIR}/dev
204
205	return 0
206}
207
208vm_extra_install_ports() {
209	# Prototype.  When overridden, installs additional ports within the
210	# virtual machine environment.
211
212	return 0
213}
214
215vm_extra_pre_umount() {
216	# Prototype.  When overridden, performs additional tasks within the
217	# virtual machine environment prior to unmounting the filesystem.
218	# Note: When overriding this function, removing resolv.conf in the
219	# disk image must be included.
220
221	if ! [ -z "${QEMUSTATIC}" ]; then
222		rm -f ${DESTDIR}/${EMULATOR}
223	fi
224	rm -f ${DESTDIR}/etc/resolv.conf
225	return 0
226}
227
228vm_extra_pkg_rmcache() {
229	if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
230		chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
231			/usr/local/sbin/pkg clean -y -a
232	fi
233
234	return 0
235}
236
237umount_loop() {
238	DIR=$1
239	i=0
240	sync
241	while ! umount ${DIR}; do
242		i=$(( $i + 1 ))
243		if [ $i -ge 10 ]; then
244			# This should never happen.  But, it has happened.
245			echo "Cannot umount(8) ${DIR}"
246			echo "Something has gone horribly wrong."
247			return 1
248		fi
249		sleep 1
250	done
251
252	return 0
253}
254
255vm_create_disk() {
256	echo "Creating image...  Please wait."
257	echo
258
259	write_partition_layout || return 1
260
261	return 0
262}
263
264vm_extra_create_disk() {
265
266	return 0
267}
268
269