-
Notifications
You must be signed in to change notification settings - Fork 14
/
mkdist
executable file
·415 lines (326 loc) · 8.81 KB
/
mkdist
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/bin/ksh
#
# flashrd mkdist
#
# flip openbsd distribution into vnd and tars for mounting on flash boot
#
# Chris Cappuccio <[email protected]>
#
. ./etc/rc.flashrd.sub
# Args: 1 - etc mnt path, 2 - resolv.conf temp location, 3 - c-level
configetc() {
###
#
# Customize etc
# US/Pacific by default for flashrd!
ln -fs /usr/share/zoneinfo/US/Pacific ${1}/localtime
# random.seed rests at /flash/etc/random.seed
ln -fs /flash/etc/random.seed ${1}/random.seed
if [ ! -f ${1}/myname ]; then
# No name? Let's call it 'flashrd' for now...
c ${3} echo flashrd > ${1}/myname
if [ -f ${1}/hosts ]; then
# Add 'flashrd' to the hosts file
c ${3} sed -e 's/localhost/localhost flashrd/' < ${1}/hosts > ${1}/hosts.new
c ${3} mv ${1}/hosts.new ${1}/hosts
else
# Create a basic hosts file
c ${3} echo "127.0.0.1 localhost flashrd" > ${1}/hosts
c ${3} echo "::1 localhost flashrd" >> ${1}/hosts
fi
fi
# Don't relocate resolv.conf if etc is a tardir
etctardir=0
for etccheck in ${tardirs}; do
if [ ${etccheck} == "etc" ]; then
etctardir=1
fi
done
for zz in $tardirs; do
if [ $zz == var -a ${etctardir} -eq 0 ]; then
# If /var is temporal, we want to move resolv.conf to it and static link
echo -n " (resolv.conf)"
[ -f ${1}/resolv.conf ] && c ${3} mv ${1}/resolv.conf ${2}
c ${3} ln -s /var/db/resolv.conf ${1}
fi
done
###
#
# Build fstab
tmpfstab=$TMPDIR/fstab
cat <<-EOF >$tmpfstab
/dev/rd0a / ffs rw 1 0
EOF
x=0
for i in $vnddirs; do
case $i {
sbin)
opts=noatime,nodev
;;
usr)
opts=noatime,nodev
;;
*)
opts=noatime,nodev,nosuid
;;
}
echo "/dev/vnd0${part[$x]} /$i ffs rw,$opts 1 0" >> $tmpfstab
((x++))
done
x=0
if [ ! -z "$tardirs" ]; then
for i in $tardirs; do
echo "swap /$i tmpfs rw,noatime,nodev,nosuid,-s${tarsize[$x]} 0 0" >> $tmpfstab
((x++))
done
fi
if [ $x -ne ${#tarsize[*]} ]; then
echo "% \$tardirs count ($x) different than tarsize array count (${#tarsize[*]}), aborting"
2; 1; 0;
fi
x=0
if [ ! -z "$tmpfsdirs" ]; then
for i in $tmpfsdirs; do
echo "swap /$i tmpfs rw,noatime,nodev,nosuid,-s${tmpfssize[$x]} 0 0" >> $tmpfstab
((x++))
done
fi
if [ $x -ne ${#tmpfssize[*]} ]; then
echo "% \$tmpfsdirs count ($x) different than tmpfssize array count (${#tmpfssize[*]}), aborting"
2; 1; 0;
fi
###
#
# copy etc files
echo '# Run flashrd final boot routine' >> ${1}/rc.local
c ${3} echo '/etc/rc.flashrd.local' >> ${1}/rc.local
c ${3} echo '/etc/rc.flashrd.shutdown' >> ${1}/rc.shutdown
c ${3} cp etc/rc.flashrd.conf etc/rc.flashrd.onetime etc/rc.flashrd.local etc/rc.flashrd.shutdown etc/rc.flashrd.sub ${1}/
c ${3} cp etc/mtree.flashrd.conf ${1}/mtree/flashrd.conf
c ${3} mv ${tmpfstab} ${1}/fstab
c ${3} "echo ${vers} > ${1}/.flashrd_version"
###
#
# stash calls to rc.flashrd.conf in /etc/rc
TMPF=`c ${3} mktemp /tmp/sed.XXXXXX`
# sed cmd:
# /ifconfig -a carp carpdemote/a\
# /etc/rc.flashrd.conf
#
c ${3} "echo /ifconfig -g carp carpdemote/a\\\\" > $TMPF
c ${3} "echo /etc/rc.flashrd.conf" >> $TMPF
c ${3} sed -f $TMPF ${1}/rc >${1}/rc.new
c ${3} mv ${1}/rc.new ${1}/rc
c ${3} rm $TMPF
}
if [ -z "$vnddirs" ]; then
echo vnddirs not specified, aborting
exit 1
fi
if [ -z "$1" -a -z "$distloc" ]; then
echo "% mkdist <openbsd base>"
exit 1
fi
if [ -z "$distloc" ]; then
distloc=$1
shift
fi
if [ ! -d "$distloc" ]; then
echo % $distloc is not a directory
exit 1
else
if [ ! -f $distloc/.profile ]; then
if [ -f $distloc/usr/share/sysmerge/etc.tgz ]; then
echo % "$distloc/usr/share/sysmerge/etc.tgz must be unpacked under $distloc"
echo % "(such as: tar xzpf $distloc/usr/share/sysmerge/etc.tgz -C $distloc)"
exit 1
else
echo % $distloc does not contain an unpacked etcXX.tgz file
exit 1
fi
fi
if [ ! -f "$distloc"/bin/ksh ]; then
echo % $distloc does not contain an unpacked baseXX.tgz file
exit 1
fi
if [ ! -u "$distloc"/usr/bin/passwd ]; then
echo "% $distloc was not unpacked with tar p flag (to preserve permissions),"
echo "or it was not unpacked as root (to allow set ownership)"
exit 1
fi
fi
y=0
for i in $vnddirs; do
if [ ! -d $distloc/$i ]; then
echo % Directory $distloc/$i not found
exit 1
fi
((y++))
done
if [ $y -ne ${#vndsize[*]} ]; then
echo "% \$vnddirs count ($y) different than \$vndsize count (${#vndsize[*]}), aborting"
exit 1
fi
if [ ! -z "tardirs" ]; then
for i in $tardirs; do
if [ ! -d $distloc/$i ]; then
echo % Directory $distloc/$i not found
exit 1
fi
done
fi
###
#
# disktab dance
if [[ ! -w /etc ]]; then
echo "Can't do it, depends on disktab being replaceable!"
exit 1
fi
random=`dd if=/dev/urandom bs=512 count=1 2>/dev/null | sha512`
tabname=`printf "%.8s" $random`
###
#
# fall back
2() {
umount -f $tmpmntdir
}
1() {
vnconfig -u $device
}
0() {
[[ -f /etc/disktab.${tabname} ]] && mv /etc/disktab.${tabname} /etc/disktab
exit 1
}
. ./flashrd.sub
[ -z "$device" ] && getavailvnd 1 && device=${vndevices[0]}
tmplabel=$(c 0 mktemp -t mkdistlabel.XXXXXX)
tmpmntdir=$(c 0 mktemp -t -d mkdist.XXXXXX)
[ -z "$tmpmnt" ] && tmpmnt=$(c 0 mktemp -t -d vnd.XXXXXX)
###
#
# Determine sizes for partitions
set -A part a d e f g h i j k l m n o p
disktotal=0
x=0
bytessec=512
# In the future, we may create an image with larger blocks.
factor=$((bytessec / 512))
for i in $vnddirs; do
echo -n Analyzing /$i
size=$(c 0 getsize $distloc/$i)
# du outputs 512 byte blocks
[[ $factor -gt 1 ]] && ((size /= factor))
# Leave 1/2th (50%) of used space as free space for >=100MB directories
free=$((size/2))
total[$x]=$((size+free))
# and double time for <100MB directories
if [ ${size} -le `mbytes2sectors 100` ]; then
((total[$x]*=2))
fi
if [ ${vndsize[$x]} != "auto" ]; then
if [ ${total[$x]} -le `mbytes2sectors ${vndsize[$x]}` ]; then
total[$x]=`mbytes2sectors ${vndsize[$x]}`
else
# Auto-calculated size was more than suggested size. Go with auto-calculation.
echo -n " (OVERRIDE ${vndsize[$x]}MB)"
fi
fi
# report results
echo " fs `sectors2mbytes ${total[$x]}`MB (`sectors2mbytes $size`MB files `sectors2mbytes $((total[$x] - size))`MB free)"
disktotal=$((disktotal+total[$x]))
((x++))
done
###
#
# Setup openbsd.vnd with a little bit of zeros
echo -n "Size openbsd.vnd `sectors2mbytes $disktotal`MB "
c 0 "dd if=/dev/zero of=${tmpmnt}/openbsd.vnd bs=512 count=0 seek=${disktotal} > $TMPDIR/last.output 2>&1"
echo
sectorstrack=64
trackscylinder=1
cylinders=$((disktotal / sectorstrack))
###
#
# Generate openbsd.vnd disktab
echo "vnd-${tabname}:\\" > $tmplabel
echo " :dt=SCSI:ty=${tabname}:se#${bytessec}:nt#${trackscylinder}:ns#${sectorstrack}:nc#${cylinders}:su#${disktotal}:\\" >> $tmplabel
x=0
offset=64
((total[0] -= offset))
for i in $vnddirs; do
e="${part[$x]}"
echo " :p${e}#${total[$x]}:o${e}#${offset}:f${e}#$((bytessec * 2)):b${e}#$((bytessec * 16)):t${e}=4.2BSD:\\" >> $tmplabel
offset=$((offset+${total[$x]}))
((x++))
done
echo " :pc#${disktotal}:oc#0:" >> $tmplabel
# DISKTAB MODIFIED
c 0 mv /etc/disktab /etc/disktab.${tabname}
c 0 cp $tmplabel /etc/disktab
c 0 cp $tmplabel $TMPDIR/vnd.disktab
c 0 vnconfig -t vnd-${tabname} $device $tmpmnt/openbsd.vnd
c 1 "disklabel -w $device vnd-${tabname} > $TMPDIR/last.output 2>&1"
# DISKTAB ORIGINAL
c 1 mv /etc/disktab.${tabname} /etc/disktab
###
#
# newfs, copy, customize /etc and /bin
vlc=0
resolv=$TMPDIR/resolv.conf
for vl in $vnddirs; do
echo -n Finalizing /${vl}
echo -n " newfs"
c 1 "newfs -m 1 -o space /dev/r${device}${part[$vlc]} > $TMPDIR/last.output 2>&1"
c 1 mount /dev/${device}${part[$vlc]} $tmpmntdir
echo -n " copy"
c 2 tar cf - -C $distloc/${vl} . | c 2 tar xpf - -C $tmpmntdir
if [ ${vl} == "etc" ]; then
configetc ${tmpmntdir} ${resolv} 2
elif [ ${vl} == "bin" ]; then
c 2 cp bin/ro bin/rw ${tmpmntdir}
fi
umountwait 1 $tmpmntdir
echo
((vlc++))
done
c 0 vnconfig -u $device
###
#
# TAR loop
if [ ! -z "$tardirs" ]; then
for tl in $tardirs; do
echo -n Creating ${tl}.tar
size=$(c 0 getsize $distloc/${tl})
if [ `sectors2mbytes $size` -ge 1 ]; then
echo -n " `sectors2mbytes $size`"MB files
else
echo -n " $((size)) sectors ($bytessec bytes/sector)"
fi
echo -n " copy"
if [ ${tl} == var ]; then
c 0 cp -Rp $distloc/${tl} $TMPDIR/var-copy
c 0 mkdir -p $TMPDIR/var-copy/db
if [ -f $resolv ]; then
echo -n " (resolv.conf)"
c 0 cp -p ${resolv} $TMPDIR/var-copy/db/resolv.conf
fi
echo -n " (host.random)"
c 0 ln -s /flash/etc/host.random $TMPDIR/var-copy/db/host.random
c 0 tar cf $tmpmnt/${tl}.tar -C $TMPDIR/var-copy .
elif [ ${tl} == "etc" ]; then
c 0 cp -Rp ${distloc}/${tl} ${TMPDIR}/etc-copy
configetc ${TMPDIR}/etc-copy ${resolv} 0
c 0 tar cf ${tmpmnt}/${tl}.tar -C $TMPDIR/etc-copy .
else
c 0 tar cf $tmpmnt/${tl}.tar -C $distloc/${tl} .
fi
echo
done
fi
###
#
# Install onetime.tgz file
if [ -f onetime.tgz ]; then
echo 'Installing onetime.tgz'
c 0 cp onetime.tgz $tmpmnt
fi