aboutsummaryrefslogtreecommitdiff
path: root/release/tools/vmimage.subr
blob: 00a0a2e23da329a0feb2a01fea0b6c0df2b23356 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/sh
#
# $FreeBSD$
#
#
# Common functions for virtual machine image build scripts.
#

export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
trap "cleanup" INT QUIT TRAP ABRT TERM

write_partition_layout() {

	case "${TARGET}:${TARGET_ARCH}" in
		amd64:amd64 | i386:i386)
			mkimg -s gpt -b /boot/pmbr \
				-p freebsd-boot/bootfs:=/boot/gptboot \
				-p freebsd-swap/swapfs::1G \
				-p freebsd-ufs/rootfs:=${VMBASE} \
				-o ${VMIMAGE}
			;;
		powerpc:powerpc*)
			mkimg -s apm \
				-p apple-boot/bootfs:=/boot/boot1.hfs \
				-p freebsd-swap/swapfs::1G \
				-p freebsd-ufs/rootfs:=${VMBASE} \
				-o ${VMIMAGE}
			;;
		*)
			# ENOTSUPP
			return 1
			;;
	esac

	return 0
}

err() {
	printf "${@}\n"
	cleanup
	return 1
}

cleanup() {
	if [ ! -z "${mddev}" ]; then
		mdconfig -d -u ${mddev}
	fi
	umount ${DESTDIR}/dev
	umount ${DESTDIR}

	return 0
}

vm_create_base() {
	# Creates the UFS root filesystem for the virtual machine disk,
	# written to the formatted disk image with mkimg(1).

	mkdir -p ${DESTDIR}
	truncate -s ${VMSIZE} ${VMBASE}
	mddev=$(mdconfig -f ${VMBASE})
	newfs -j /dev/${mddev}
	mount /dev/${mddev} ${DESTDIR}

	return 0
}

vm_install_base() {
	# Installs the FreeBSD userland/kernel to the virtual machine disk.

	cd ${WORLDDIR} && \
		make DESTDIR=${DESTDIR} \
		installworld installkernel distribution || \
		err "\n\nCannot install the base system to ${DESTDIR}."

	echo '# Custom /etc/fstab for FreeBSD VM images' \
		> ${DESTDIR}/etc/fstab
	echo '/dev/gpt/rootfs   /       ufs     rw      1       1' \
		>> ${DESTDIR}/etc/fstab
	echo '/dev/gpt/swapfs  none    swap    sw      0       0' \
		>> ${DESTDIR}/etc/fstab

	mkdir -p ${DESTDIR}/dev
	mount -t devfs devfs ${DESTDIR}/dev
	chroot ${DESTDIR} /usr/bin/newaliases
	chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
	umount ${DESTDIR}/dev

	cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf

	return 0
}

vm_extra_install_base() {
	# Prototype.  When overridden, runs extra post-installworld commands
	# as needed, based on the target virtual machine image or cloud
	# provider image target.

	return 0
}

vm_extra_enable_services() {
	if [ ! -z "${VM_RC_LIST}" ]; then
		for _rcvar in ${VM_RC_LIST}; do
			echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
		done
	fi

	return 0
}

vm_extra_install_packages() {
	mkdir -p ${DESTDIR}/dev
	mount -t devfs devfs ${DESTDIR}/dev
	chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
		/usr/sbin/pkg bootstrap -y
	if [ ! -z "${VM_EXTRA_PACKAGES}" ]; then
		chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
			/usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
	fi
	umount ${DESTDIR}/dev

	return 0
}

vm_extra_install_ports() {
	# Prototype.  When overridden, installs additional ports within the
	# virtual machine environment.

	return 0
}

vm_extra_pre_umount() {
	# Prototype.  When overridden, installs additional ports within the
	# virtual machine environment.

	rm -f ${DESTDIR}/etc/resolv.conf

	return 0
}

vm_umount_base() {
	i=0
	sync
	while ! umount ${DESTDIR}/dev ${DESTDIR}; do
		i=$(( $i + 1 ))
		if [ $i -ge 10 ]; then
			# This should never happen.  But, it has happened.
			msg="Cannot umount(8) ${DESTDIR}\n"
			msg="${msg}Something has gone horribly wrong."
			err "${msg}"
		fi
		sleep 1
	done

	return 0
}

vm_create_disk() {
	echo "Creating image...  Please wait."
	echo

	write_partition_layout || return 1

	return 0
}

vm_extra_create_disk() {

	return 0
}