xref: /freebsd/release/tools/vmimage.subr (revision 0957b409)
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	mkdir -p ${DESTDIR}/dev
152	mount -t devfs devfs ${DESTDIR}/dev
153	chroot ${DESTDIR} /usr/bin/newaliases
154	chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
155	umount_loop ${DESTDIR}/dev
156
157	cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
158
159	return 0
160}
161
162vm_extra_install_base() {
163	# Prototype.  When overridden, runs extra post-installworld commands
164	# as needed, based on the target virtual machine image or cloud
165	# provider image target.
166
167	return 0
168}
169
170vm_extra_enable_services() {
171	if [ ! -z "${VM_RC_LIST}" ]; then
172		for _rcvar in ${VM_RC_LIST}; do
173			echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
174		done
175	fi
176
177	if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then
178		echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \
179			${DESTDIR}/etc/rc.conf
180	fi
181
182	return 0
183}
184
185vm_extra_install_packages() {
186	if [ -z "${VM_EXTRA_PACKAGES}" ]; then
187		return 0
188	fi
189	mkdir -p ${DESTDIR}/dev
190	mount -t devfs devfs ${DESTDIR}/dev
191	chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
192		/usr/sbin/pkg bootstrap -y
193	chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
194		/usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
195	umount_loop ${DESTDIR}/dev
196
197	return 0
198}
199
200vm_extra_install_ports() {
201	# Prototype.  When overridden, installs additional ports within the
202	# virtual machine environment.
203
204	return 0
205}
206
207vm_extra_pre_umount() {
208	# Prototype.  When overridden, performs additional tasks within the
209	# virtual machine environment prior to unmounting the filesystem.
210	# Note: When overriding this function, removing resolv.conf in the
211	# disk image must be included.
212
213	rm -f ${DESTDIR}/etc/resolv.conf
214	return 0
215}
216
217vm_extra_pkg_rmcache() {
218	if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
219		chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
220			/usr/local/sbin/pkg clean -y -a
221	fi
222
223	return 0
224}
225
226umount_loop() {
227	DIR=$1
228	i=0
229	sync
230	while ! umount ${DIR}; do
231		i=$(( $i + 1 ))
232		if [ $i -ge 10 ]; then
233			# This should never happen.  But, it has happened.
234			echo "Cannot umount(8) ${DIR}"
235			echo "Something has gone horribly wrong."
236			return 1
237		fi
238		sleep 1
239	done
240
241	return 0
242}
243
244vm_create_disk() {
245	echo "Creating image...  Please wait."
246	echo
247
248	write_partition_layout || return 1
249
250	return 0
251}
252
253vm_extra_create_disk() {
254
255	return 0
256}
257
258