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