-
Notifications
You must be signed in to change notification settings - Fork 2
/
vmmake
executable file
·260 lines (219 loc) · 5.77 KB
/
vmmake
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/sh
#
# Copyright (c) 2015-2018 Mark Johnston <[email protected]>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#
# todo:
# - support for incremental rebuilds of images
# - ssh keys
# - i386 images
#
usage()
{
cat >&2 <<__EOF__
usage: $(basename $0) [-tkx] [-a <arch>] [-c <kernconf>] [-f <numfree>] [-p <pkgs>] [-s <size>] <img>
__EOF__
exit 1
}
# Bootstrap packages.
bootstrap()
{
local md mdir pfile pkgs
pfile=$1
pkgs=$2
md=$(mdconfig -S $SECTORSIZE -f $pfile)
mdir=$(mktemp -d)
mount /dev/$md $mdir
# Set up to install packages.
cp -f /etc/resolv.conf ${mdir}/etc/resolv.conf
chroot ${mdir} sh -c "mount -t devfs none /dev && ASSUME_ALWAYS_YES=yes /usr/sbin/pkg bootstrap -y"
# Do the thing.
echo "$pkgs" | tr ',' ' ' | xargs chroot $mdir env ASSUME_ALWAYS_YES=yes \
/usr/local/sbin/pkg install
# Clean up, clean up.
umount ${mdir}/dev
umount $mdir
rmdir $mdir
mdconfig -d -u ${md#md}
}
cleanup()
{
if [ $TMPFS ]; then
umount $DESTDIR
else
rm -rf $DESTDIR || :
chflags -R 0 $DESTDIR || :
rm -rf $DESTDIR
fi
rm -f $PARTFILE
}
# Manually add a file to the image.
logfile()
{
local file root size
file=$1
root=$2
size=$(stat -f '%z' ${root}/${file})
echo "./$file type=file uname=root gname=wheel mode=0644 size=$size" >> ${root}/METALOG
}
# Create custom system configuration files.
install_config()
{
local destdir fstab localtime rcconf srcconf
destdir=$1
kernconfig=$2
fstab=etc/fstab
cat > ${destdir}/$fstab <<__EOF__
/dev/gpt/rootfs / ufs rw 1 1
/dev/gpt/swapfs none swap sw 0 0
none /proc procfs rw 0 0
none /dev/fd fdescfs rw 0 0
__EOF__
if [ $NONET -eq 0 ]; then
cat >> ${destdir}/$fstab <<__EOF__
${IPADDR}:$(pwd) /usr/src nfs ro 0 0
__EOF__
fi
localtime=etc/localtime
cp -f /$localtime ${destdir}/$localtime
rcconf=etc/rc.conf
cat > ${destdir}/$rcconf <<__EOF__
ifconfig_vtnet0="DHCP"
ipv6_activate_all_interfaces="YES"
ipv6_cpe_wanif="vtnet0"
sendmail_enable="NONE"
sshd_enable="YES"
__EOF__
srcconf=etc/src.conf
cat > ${destdir}/$srcconf <<__EOF__
KERNCONF?= $kernconfig
__EOF__
wallcmosclock=etc/wall_cmos_clock
touch ${destdir}/$wallcmosclock
logfile $fstab $destdir
logfile $localtime $destdir
logfile $rcconf $destdir
logfile $srcconf $destdir
logfile $wallcmosclock $destdir
}
#
# Execution begins here.
#
set -e
ARCH=$(uname -m)
IPADDR=
KERNCONFIG=
MKSRC=0
NONET=0
NUMFILES=
PARTSIZE=10g
PACKAGES=
SECTORSIZE=512
TMPFS=
while getopts a:c:f:i:kp:S:s:tx o; do
case "$o" in
a)
ARCH=$OPTARG
;;
c)
KERNCONFIG=$OPTARG
;;
f)
NUMFILES=$OPTARG
;;
i)
IPADDR=$OPTARG
;;
k)
MKSRC=1
;;
p)
PACKAGES=$OPTARG
;;
s)
PARTSIZE=$OPTARG
;;
S)
SECTORSIZE=$OPTARG
;;
t)
TMPFS=1
;;
x)
NONET=1
;;
?)
usage
;;
esac
shift $((OPTIND-1))
done
if [ -n "$PACKAGES" -a $(id -u) -ne 0 ]; then
echo "$(basename $0): must be root to install packages" >&2
exit 1
elif [ "$TMPFS" -a $(id -u) -ne 0 ]; then
echo "$(basename $0): must be root to use tmpfs" >&2
exit 1
fi
if [ -z "$KERNCONFIG" ]; then
KERNCONFIG=BHYVE
fi
if [ $NONET -eq 0 ]; then
if [ -z "$IPADDR" ]; then
ifconfig bridge0 >/dev/null || exit 1
IPADDR=$(ifconfig bridge0 | grep -E '^[[:space:]]*inet' | head -n 1 | \
awk '{print $2}')
fi
fi
IMAGE=${1:-/tmp/vm.raw}
PARTFILE=$(mktemp)
DESTDIR=$(mktemp -d)
if [ $TMPFS ]; then
mount -t tmpfs tmpfs $DESTDIR
fi
trap "cleanup; exit 1" EXIT SIGINT SIGHUP SIGTERM
make -j $(sysctl -n hw.ncpu) -s -DNO_ROOT DESTDIR=$DESTDIR KERNCONF=$KERNCONFIG \
MACHINE=$ARCH TARGET_ARCH=$ARCH \
DISTDIR= installworld installkernel distribution
install_config $DESTDIR $KERNCONFIG
if [ -z "$NUMFILES" ]; then
NUMFILES=$(cat ${DESTDIR}/METALOG | wc -l)
fi
if [ $MKSRC -eq 1 ]; then
echo "Installing source..."
cp -vR * $DESTDIR/usr/src
fi
makefs -B little -f $NUMFILES -M $PARTSIZE -S $SECTORSIZE -Z \
-o label=VM -o softupdates=1 -o version=2 -o bsize=$((4 * 8192)) -o fsize=8192 \
-F ${DESTDIR}/METALOG $PARTFILE $DESTDIR
if [ -n "$PACKAGES" ]; then
bootstrap $PARTFILE $PACKAGES
fi
mkimg -s gpt -f raw -S $SECTORSIZE -b ${DESTDIR}/boot/pmbr \
-p freebsd-boot/bootfs:=${DESTDIR}/boot/gptboot \
-p freebsd-swap/swapfs::2G \
-p freebsd-ufs/rootfs:=${PARTFILE} \
-o $IMAGE
rm -f $PARTFILE