xref: /freebsd/release/tools/vmimage.subr (revision e5d34ca9fddcae66465f01e5a7725a31f8df8493)
1#!/bin/sh
2#
3# $FreeBSD$
4#
5#
6# Common functions for virtual machine image build scripts.
7#
8
9export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
10trap "cleanup" INT QUIT TRAP ABRT TERM
11
12write_partition_layout() {
13	if [ -z "${NOSWAP}" ]; then
14		SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}"
15	fi
16
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/sys/boot"
21	else
22		BOOTFILES="/${_OBJDIR}/sys/boot"
23	fi
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			mkimg -s mbr -f ${VMFORMAT} \
36				-p efi:=${BOOTFILES}/efi/boot1/boot1.efifat \
37				-p freebsd:=${VMBASE} \
38				-o ${VMIMAGE}
39			;;
40		powerpc:powerpc*)
41			mkimg -s apm -f ${VMFORMAT} \
42				-p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \
43				${SWAPOPT} \
44				-p freebsd-ufs/rootfs:=${VMBASE} \
45				-o ${VMIMAGE}
46			;;
47		*)
48			# ENOTSUPP
49			return 1
50			;;
51	esac
52
53	return 0
54}
55
56err() {
57	printf "${@}\n"
58	cleanup
59	return 1
60}
61
62cleanup() {
63	if [ -c "${DESTDIR}/dev/null" ]; then
64		umount_loop ${DESTDIR}/dev 2>/dev/null
65	fi
66	umount_loop ${DESTDIR}
67	if [ ! -z "${mddev}" ]; then
68		mdconfig -d -u ${mddev}
69	fi
70
71	return 0
72}
73
74vm_create_base() {
75	# Creates the UFS root filesystem for the virtual machine disk,
76	# written to the formatted disk image with mkimg(1).
77
78	mkdir -p ${DESTDIR}
79	truncate -s ${VMSIZE} ${VMBASE}
80	mddev=$(mdconfig -f ${VMBASE})
81	newfs -L rootfs /dev/${mddev}
82	mount /dev/${mddev} ${DESTDIR}
83
84	return 0
85}
86
87vm_copy_base() {
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
92	# package archives).
93
94	mkdir -p ${DESTDIR}/old
95	mdold=$(mdconfig -f ${VMBASE})
96	mount /dev/${mdold} ${DESTDIR}/old
97
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
103
104	tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new
105
106	umount_loop /dev/${mdold}
107	rmdir ${DESTDIR}/old
108	mdconfig -d -u ${mdold}
109
110	umount_loop /dev/${mdnew}
111	rmdir ${DESTDIR}/new
112	tunefs -n enable /dev/${mdnew}
113	mdconfig -d -u ${mdnew}
114	mv ${VMBASE}.tmp ${VMBASE}
115}
116
117vm_install_base() {
118	# Installs the FreeBSD userland/kernel to the virtual machine disk.
119
120	cd ${WORLDDIR} && \
121		make DESTDIR=${DESTDIR} \
122		installworld installkernel distribution || \
123		err "\n\nCannot install the base system to ${DESTDIR}."
124
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}" \
132		-D ${DESTDIR}
133
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
141	fi
142
143	mkdir -p ${DESTDIR}/dev
144	mount -t devfs devfs ${DESTDIR}/dev
145	chroot ${DESTDIR} /usr/bin/newaliases
146	chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
147	umount_loop ${DESTDIR}/dev
148
149	cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
150
151	return 0
152}
153
154vm_extra_install_base() {
155	# Prototype.  When overridden, runs extra post-installworld commands
156	# as needed, based on the target virtual machine image or cloud
157	# provider image target.
158
159	return 0
160}
161
162vm_extra_enable_services() {
163	if [ ! -z "${VM_RC_LIST}" ]; then
164		for _rcvar in ${VM_RC_LIST}; do
165			echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
166		done
167	fi
168
169	if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then
170		echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \
171			${DESTDIR}/etc/rc.conf
172	fi
173
174	return 0
175}
176
177vm_extra_install_packages() {
178	if [ -z "${VM_EXTRA_PACKAGES}" ]; then
179		return 0
180	fi
181	mkdir -p ${DESTDIR}/dev
182	mount -t devfs devfs ${DESTDIR}/dev
183	chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
184		/usr/sbin/pkg bootstrap -y
185	chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
186		/usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
187	umount_loop ${DESTDIR}/dev
188
189	return 0
190}
191
192vm_extra_install_ports() {
193	# Prototype.  When overridden, installs additional ports within the
194	# virtual machine environment.
195
196	return 0
197}
198
199vm_extra_pre_umount() {
200	# Prototype.  When overridden, performs additional tasks within the
201	# virtual machine environment prior to unmounting the filesystem.
202	# Note: When overriding this function, removing resolv.conf in the
203	# disk image must be included.
204
205	rm -f ${DESTDIR}/etc/resolv.conf
206	return 0
207}
208
209vm_extra_pkg_rmcache() {
210	if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
211		chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
212			/usr/local/sbin/pkg clean -y -a
213	fi
214
215	return 0
216}
217
218umount_loop() {
219	DIR=$1
220	i=0
221	sync
222	while ! umount ${DIR}; do
223		i=$(( $i + 1 ))
224		if [ $i -ge 10 ]; then
225			# This should never happen.  But, it has happened.
226			echo "Cannot umount(8) ${DIR}"
227			echo "Something has gone horribly wrong."
228			return 1
229		fi
230		sleep 1
231	done
232
233	return 0
234}
235
236vm_create_disk() {
237	echo "Creating image...  Please wait."
238	echo
239
240	write_partition_layout || return 1
241
242	return 0
243}
244
245vm_extra_create_disk() {
246
247	return 0
248}
249
250