forked from yellowman/flashrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkdist
executable file
·314 lines (247 loc) · 6.63 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
#!/bin/ksh
#
# flashrd mkdist
#
# flip openbsd distribution into vnd and tars for mounting on flash boot
#
# Chris Cappuccio <[email protected]>
#
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
if [ ! -z "$vndsize" ]; then
# build vndsizeA array from $vndsize
x=0
for i in $vndsize; do
vndsizeA[$x]=$i
((x++))
done
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 $x ]; then
echo "% \$vnddirs count ($y) different than \$vndsize count ($x), 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
set -- $(c 0 du -s $distloc/$i)
size=$1
# 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 [ ${vndsizeA[$x]} != "auto" ]; then
if [ ${total[$x]} -le `mbytes2sectors ${vndsizeA[$x]}` ]; then
total[$x]=`mbytes2sectors ${vndsizeA[$x]}`
else
# Auto-calculated size was more than suggested size. Go with auto-calculation.
echo -n " (OVERRIDE ${vndsizeA[$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=512 > $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
x=0
resolv=$TMPDIR/resolv.conf
for i in $vnddirs; do
echo -n Finalizing /$i
echo -n " newfs"
c 1 "newfs -m 1 -o space /dev/r${device}${part[$x]} > $TMPDIR/last.output 2>&1"
c 1 mount /dev/${device}${part[$x]} $tmpmntdir
echo -n " copy"
c 2 tar cf - -C $distloc/$i . | c 2 tar xpf - -C $tmpmntdir
if [ $i == etc ]; then
# US/Pacific by default for flashrd!
ln -fs /usr/share/zoneinfo/US/Pacific ${tmpmntdir}/localtime
# random.seed rests at /flash/etc/random.seed
ln -fs /flash/etc/random.seed ${tmpmntdir}/random.seed
if [ ! -f ${tmpmntdir}/myname ]; then
# No name? Let's call it 'flashrd' for now...
c 2 echo flashrd > ${tmpmntdir}/myname
if [ -f ${tmpmntdir}/hosts ]; then
# Add 'flashrd' to the hosts file
c 2 sed -e 's/localhost/localhost flashrd/' < ${tmpmntdir}/hosts > ${tmpmntdir}/hosts.new
c 2 mv ${tmpmntdir}/hosts.new ${tmpmntdir}/hosts
else
# Create a basic hosts file
c 2 echo "127.0.0.1 localhost flashrd" > ${tmpmntdir}/hosts
c 2 echo "::1 localhost flashrd" >> ${tmpmntdir}/hosts
fi
fi
for zz in $tardirs; do
if [ $zz == var ]; then
# If /var is temporal, we want to move resolv.conf to it and static link
echo -n " (resolv.conf)"
[ -f ${tmpmntdir}/resolv.conf ] && c 2 mv ${tmpmntdir}/resolv.conf ${resolv}
c 2 ln -s /var/db/resolv.conf ${tmpmntdir}
fi
done
fi
umountwait 1 $tmpmntdir
echo
((x++))
done
c 0 vnconfig -u $device
###
#
# TAR loop
if [ ! -z "$tardirs" ]; then
for i in $tardirs; do
echo -n Creating $i.tar
set -- $(c 0 du -s $distloc/$i)
size=$1
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 [ $i == var ]; then
c 0 cp -Rp $distloc/$i $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/$i.tar -C $TMPDIR/var-copy .
else
c 0 tar cf $tmpmnt/$i.tar -C $distloc/$i .
fi
echo
done
fi
###
#
# Install onetime.tgz file
if [ -f onetime.tgz ]; then
echo 'Installing onetime.tgz'
c 0 cp onetime.tgz $tmpmnt
fi