forked from markjdb/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmmake
More file actions
executable file
·222 lines (182 loc) · 4.17 KB
/
vmmake
File metadata and controls
executable file
·222 lines (182 loc) · 4.17 KB
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
#!/bin/sh
#
# todo:
# - support for incremental rebuilds of images
# - ssh keys
# - i386 images
#
usage()
{
echo "usage: $(basename $0) [-tkx] [-a <arch>] [-c <kernel config>] [-f <free files>] [-p <pkgs>] [-s <size>] <img>" >&2
exit 1
}
# Bootstrap packages.
bootstrap()
{
local md mdir pfile pkgs
pfile=$1
pkgs=$2
md=$(mdconfig -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} env 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
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
${IPADDR}:$(pwd) /usr/src nfs ro 0 0
__EOF__
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=
PARTSIZE=10g
PACKAGES=
TMPFS=
KERNCONFIG=
NUMFILES=
MKSRC=0
NONET=0
while getopts a:c:f:i:kp:s:t:x 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
;;
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 [ -z "$NONET" ]; 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}
OBJDIR=/usr/obj/$(readlink -f $(pwd))
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 ]; then
echo "Installing source..."
cp -vR * $DESTDIR/usr/src
fi
makefs -B little -f $NUMFILES -o label=VM -M $PARTSIZE \
-F ${DESTDIR}/METALOG $PARTFILE $DESTDIR
if [ -n "$PACKAGES" ]; then
bootstrap $PARTFILE $PACKAGES
fi
mkimg -s gpt -f raw -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