xref: /freebsd/release/tools/vmimage.subr (revision 7d97075f02527bcf29318143c2fc3650be79ee07)
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
14	case "${TARGET}:${TARGET_ARCH}" in
15		amd64:amd64 | i386:i386)
16			mkimg -s gpt -b /boot/pmbr \
17				-p freebsd-boot/bootfs:=/boot/gptboot \
18				-p freebsd-swap/swapfs::1G \
19				-p freebsd-ufs/rootfs:=${VMBASE} \
20				-o ${VMIMAGE}
21			;;
22		powerpc:powerpc*)
23			mkimg -s apm \
24				-p apple-boot/bootfs:=/boot/boot1.hfs \
25				-p freebsd-swap/swapfs::1G \
26				-p freebsd-ufs/rootfs:=${VMBASE} \
27				-o ${VMIMAGE}
28			;;
29		*)
30			# ENOTSUPP
31			return 1
32			;;
33	esac
34
35	return 0
36}
37
38err() {
39	printf "${@}\n"
40	cleanup
41	return 1
42}
43
44cleanup() {
45	umount ${DESTDIR}/dev 2>/dev/null
46	umount ${DESTDIR}
47	if [ ! -z "${mddev}" ]; then
48		mdconfig -d -u ${mddev}
49	fi
50
51	return 0
52}
53
54vm_create_base() {
55	# Creates the UFS root filesystem for the virtual machine disk,
56	# written to the formatted disk image with mkimg(1).
57
58	mkdir -p ${DESTDIR}
59	truncate -s ${VMSIZE} ${VMBASE}
60	mddev=$(mdconfig -f ${VMBASE})
61	newfs -j /dev/${mddev}
62	mount /dev/${mddev} ${DESTDIR}
63
64	return 0
65}
66
67vm_install_base() {
68	# Installs the FreeBSD userland/kernel to the virtual machine disk.
69
70	cd ${WORLDDIR} && \
71		make DESTDIR=${DESTDIR} \
72		installworld installkernel distribution || \
73		err "\n\nCannot install the base system to ${DESTDIR}."
74
75	echo '# Custom /etc/fstab for FreeBSD VM images' \
76		> ${DESTDIR}/etc/fstab
77	echo '/dev/gpt/rootfs   /       ufs     rw      1       1' \
78		>> ${DESTDIR}/etc/fstab
79	echo '/dev/gpt/swapfs  none    swap    sw      0       0' \
80		>> ${DESTDIR}/etc/fstab
81
82	mkdir -p ${DESTDIR}/dev
83	mount -t devfs devfs ${DESTDIR}/dev
84	chroot ${DESTDIR} /usr/bin/newaliases
85	chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
86	umount ${DESTDIR}/dev
87
88	cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
89
90	return 0
91}
92
93vm_extra_install_base() {
94	# Prototype.  When overridden, runs extra post-installworld commands
95	# as needed, based on the target virtual machine image or cloud
96	# provider image target.
97
98	return 0
99}
100
101vm_extra_enable_services() {
102	if [ ! -z "${VM_RC_LIST}" ]; then
103		for _rcvar in ${VM_RC_LIST}; do
104			echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
105		done
106	fi
107
108	return 0
109}
110
111vm_extra_install_packages() {
112	mkdir -p ${DESTDIR}/dev
113	mount -t devfs devfs ${DESTDIR}/dev
114	chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
115		/usr/sbin/pkg bootstrap -y
116	if [ ! -z "${VM_EXTRA_PACKAGES}" ]; then
117		chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
118			/usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
119	fi
120	umount ${DESTDIR}/dev
121
122	return 0
123}
124
125vm_extra_install_ports() {
126	# Prototype.  When overridden, installs additional ports within the
127	# virtual machine environment.
128
129	return 0
130}
131
132vm_extra_pre_umount() {
133	# Prototype.  When overridden, installs additional ports within the
134	# virtual machine environment.
135
136	rm -f ${DESTDIR}/etc/resolv.conf
137
138	return 0
139}
140
141vm_umount_base() {
142	i=0
143	sync
144	while ! umount ${DESTDIR}/dev ${DESTDIR}; do
145		i=$(( $i + 1 ))
146		if [ $i -ge 10 ]; then
147			# This should never happen.  But, it has happened.
148			msg="Cannot umount(8) ${DESTDIR}\n"
149			msg="${msg}Something has gone horribly wrong."
150			err "${msg}"
151		fi
152		sleep 1
153	done
154
155	return 0
156}
157
158vm_create_disk() {
159	echo "Creating image...  Please wait."
160	echo
161
162	write_partition_layout || return 1
163
164	return 0
165}
166
167vm_extra_create_disk() {
168
169	return 0
170}
171
172