forked from yellowman/flashrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfgflashrd
executable file
·524 lines (427 loc) · 10.7 KB
/
cfgflashrd
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#!/bin/ksh
#
# flashrd image configurator
#
# Chris Cappuccio <[email protected]>
typeset -x TMPDIR
# include vnddirs
. ./etc/rc.flashrd.sub
set -A part a d e f g h i j k l m n o p
usage() {
cat <<EOF
Usage: cfgflashrd [-d "dev" | -i "filename"] [options]
Use one of:
-d | -disk "dev" Configure an attached disk
-i | -image "filename" Configure an image
Options:
-r "filename" rdrootfs image (needed only to change ramdisk)
-e "dir" directory where elfrdsetroot.c is located (only with -r)
-c "speed" set com0 as console at speed "speed"
-m set bsd.mp as primary kernel
-o "onetime.tgz" install specified file on the flash as onetime.tgz
-s set bsd.sp as primary kernel
-t "tzfile" set timezone using a timezone file fromfrom
/usr/share/zoneinfo
-k "keyboard" configure alternative keyboard encoding
-dns "dns server" list of DNS servers (separated by spaces)
-ntp "ntp servers" list of NTP servers (separated by spaces)
-hostname "name" hostname of target host
EOF
}
if [[ "$#" -lt 1 ]]; then
usage
exit 1
fi
. ./flashrd.sub
[ `id -u` != 0 ] && echo root required && exit 1
getavailvnd 2
device=${vndevices[0]}
rootvnd=${vndevices[1]}
while :
do
case $1 in
-\?) usage; exit 0;;
-d|-disk) t2 "$2"; disk="$2"; shift 2;;
-i|-image) t2 "$2"; image="$2"; shift 2;;
-r|-rdroot) t2 "$2"; rdfs="$2"; shift 2;;
-c|-com0) t2 "$2"; com0="$2"; shift 2;;
-m|-mp) kernel="bsdmp"; shift;;
-o|-onetime) t2 "$2"; onetime="$2"; shift 2;;
-s|-sp) kernel="bsdsp"; shift;;
-t|-tz) t2 "$2"; tzfile="$2"; shift 2;;
-dns) t2 "$2"; dnsservers="$2"; shift 2;;
-ntp) t2 "$2"; ntpservers="$2"; shift 2;;
-hostname) t2 "$2"; hostname="$2"; shift 2;;
-e) t2 "$2"; elfrdsetrootdir="$2"; shift 2;;
-k) t2 "$2"; keyboardencoding="$2"; shift 2;;
--) shift; break;;
-*) usage; exit 1;;
*) break;;
esac
done
# checking arguments
if [[ -n $elfrdsetrootdir && -z $rdfs ]]; then
echo "Option -e only valid in conjunction with -r"
exit 1
fi
if [[ -n $disk && -n $image ]]; then
echo "Choose either -d or -i, not both"
exit 1
fi
if [[ -z $disk && -z $image ]]; then
echo "Must choose either -d or -i"
exit 1
fi
if [[ -n $disk ]]; then
if [[ ! -b /dev/${disk}a ]]; then
echo "Device /dev/${disk}a not found"
exit 1
fi
vnd=false
rootdevice=/dev/${disk}a
fi
if [[ -n $image ]]; then
if [[ ! -f $image ]]; then
echo "Sorry flashimg $image not found"
exit 1
fi
vnd=true
rootdevice=/dev/${rootvnd}a
fi
if [[ -n $rdfs && ! -f $rdfs ]]; then
echo "Sorry rdroot $rdfs not found"
exit 1
fi
if [[ -n $com0 && ( $com0 -lt 1200 || $com0 -gt 115200 ) ]]; then
echo "Sorry the com0 speed $com0 is unlikely"
exit 1
fi
if [[ -n $tzfile ]]; then
if [[ ! -f $tzfile ]]; then
tzfile=/usr/share/zoneinfo/$tzfile
elif [[ ! -f $tzfile ]]; then
echo "Timezone file not found"
exit 1
fi
fi
if [ -z "$elfrdsetrootdir" ]; then
elfrdsetrootdir=./elfrdsetroot/
fi
if [ ! -d $elfrdsetrootdir ]; then
echo % elfrdsetroodir $elfrdsetrootdir not found
fi
vncfgroot() {
[[ $vnd == true ]] && c 0 vnconfig ${rootvnd} $1
}
vnuncfgroot() {
[[ $vnd == true ]] && c 0 vnconfig -u ${rootvnd}
}
###
#
# Check if provided keyboard encoding seems viable based on what exists on the
# current machine
testkeyboardencoding() {
if [ ! -z $keyboardencoding ]; then
found=`/sbin/kbd -l | grep $keyboardencoding | wc -l`
if [ $found -eq 0 ]; then
echo "WARNING: The keyboard encoding \"$keyboardencoding\" does not exist on your machine."
echo "You'll need to set it manually on the targetted machine via the wsconsctl command if"
echo "this is a new encoding."
return 1
fi
fi
}
###
#
# fall back
4() {
umount -f $tmpmntvnd
}
3() {
vnconfig -u $device
}
2() {
umount -f $tmpmnt
}
1() {
vnuncfgroot
}
0() {
echo % TMPDIR: $TMPDIR
exit 1
}
###
#
# (Re-)Base TMPDIR to a subdiretory of either the current
# TMPDIR value, or to /tmp
TMPDIR=$(c 0 mktemp -t -d cfgflashrd.XXXXXX)
tmpmnt=$TMPDIR/tmpmnt # vnd/disk mount point
c 1 mkdir $tmpmnt
tmpmntvnd=$TMPDIR/tmpmntvnd # openbsd.vnd partition mount point
c 1 mkdir $tmpmntvnd
###
#
# mount vnd or disk
vncfgroot $image
c 1 fsck -p ${rootdevice}
c 1 mount ${rootdevice} $tmpmnt
if [ ! -z "$rdfs" ]; then
###
#
# compile ELFRDSETROOT
c 2 cc -o $elfrdsetrootdir/elfrdsetroot $elfrdsetrootdir/elfrdsetroot.c $elfrdsetrootdir/elf32.c $elfrdsetrootdir/elf64.c
c 2 vnconfig $device $rdfs
c 3 fsck -p /dev/${device}a
c 3 mount /dev/${device}a $tmpmntvnd
cholo=`egrep ^disk= $tmpmntvnd/stand/rc`
if [ -z "$cholo" ]; then
echo % no disk= line found in $tmpmntvnd/stand/rc, aborting
4; 3; 2; 1; 0;
fi
ch1=${cholo#disk=}
if [ ${ch1} != "auto" ]; then
ch1=${ch1%a}
fi
echo Current image device ${ch1}
getphys()
{
read phys?"New physical device name (as seen by destination system)[${ch1}]? "
if [ -z ${phys} ]; then
phys=$ch1
fi
if [ ! -b /dev/${phys}a -a ${phys} != "auto" ]; then
echo /dev/${phys}a appear to be nonsense, try again
getphys
fi
}
getphys
if [ ${phys} != "auto" ]; then
phys=${phys}a
fi
temp=$(c 4 mktemp -t cfgflashrd.XXXXXX)
c 4 "sed -e 's/$cholo/disk=${phys}/' < $tmpmntvnd/stand/rc > $temp"
c 4 mv $temp $tmpmntvnd/stand/rc
c 3 umount $tmpmntvnd
c 2 vnconfig -u $device
echo Setting ramdisk root image
for i in bsd bsd.mp bsd.sp; do
if [ -f $tmpmnt/$i ]; then
c 2 $elfrdsetrootdir/elfrdsetroot $tmpmnt/$i $rdfs
cfgbsd=1
fi
done
c 2 rm $elfrdsetrootdir/elfrdsetroot
if [ -z "$cfgbsd" ]; then
echo % none of bsd bsd.mp bsd.sp kernel found at flash image. how odd?
2; 1; 0;
fi
if [ "$kernel" == "bsdsp" ]; then
if [ -f $tmpmnt/bsd.sp ]; then
mv $tmpmnt/bsd $tmpmnt/bsd.mp
mv $tmpmnt/bsd.sp $tmpmnt/bsd
else
echo "[bsd.sp not found, primary kernel may already be single processor]"
fi
fi
if [ "$kernel" == "bsdmp" ]; then
if [ -f $tmpmnt/bsd.mp ]; then
mv $tmpmnt/bsd $tmpmnt/bsd.sp
mv $tmpmnt/bsd.mp $tmpmnt/bsd
else
echo "[bsd.mp not found, primary kernel may already by multi-processor]"
fi
fi
rm -f $temp
fi
if [ ! -z "$com0" ]; then
echo Installing boot.conf for com0 at $com0 baud
mkdir -p $tmpmnt/etc
cat <<-EOF >$tmpmnt/etc/boot.conf
stty com0 $com0
set tty com0
EOF
fi
###
#
# install the onetime.tgz file
if [ ! -z "$onetime" -a -f "$onetime" ]; then
echo Installing "$onetime" file
cp "$onetime" $tmpmnt/onetime.tgz
fi
###
#
# mount running filesystem for other types of changes
c 2 vnconfig $device $tmpmnt/openbsd.vnd
###
#
# map $vnddirs etc to a partition label, mount
x=0
for i in $vnddirs; do
if [ $i == etc ]; then
etcpart=${part[$x]}
fi
(( x++ ))
done
if [ -z "$etcpart" ]; then
echo "% missing etc in \$vnddirs ($vnddirs) aborting"
3; 2; 1; 0;
fi
c 3 fsck -p /dev/$device$etcpart
c 3 mount /dev/$device$etcpart $tmpmntvnd
if [ ! -f $tmpmntvnd/.flashrd_version ]; then
echo % missing etc/.flashrd_version, not flashrd image or \$vnddirs out of sync? what funky?
else
echo % image flashrd_version $(cat $tmpmntvnd/.flashrd_version)
fi
if [ ! -z "$com0" ]; then
tmpttys=$TMPDIR/tmpttys
echo Modifying /etc/ttys for tty00 at $com0 baud
# Turn off ttyC consoles, turn on com0
# partially gleaned from install.sub
sed -e "/^tty00/s/std.[0-9]*/std.${com0}/" \
-e "/^tty00/s/unknown/vt220 /" \
-e "/^tty00/s/off.*/on secure/" \
-e "/^ttyC/s/on.*secure/off/" $tmpmntvnd/ttys > $tmpttys
c 4 mv $tmpttys $tmpmntvnd/ttys
fi
###
#
# set hostname
getname()
{
read hostname?"Hostname: "
if [ -z "$hostname" ]; then
echo "Nothing entered? Try again."
getname
fi
}
if [ -z "$hostname" ]; then
echo
echo Please assign a system hostname...
getname
fi
echo $hostname > $tmpmntvnd/myname
###
#
# set root password
getpass()
{
stty -echo
read pw1?"Password:"
echo
read pw2?"Verify:"
echo
stty echo
if [ "$pw1" == "$pw2" ]; then
password="$pw1"
fi
if [ -z "$password" ]; then
echo "Passwords don't match or password was empty. Try again."
getpass
fi
}
if [ -z "$encpass" ]; then
if [ -z "$password" ]; then
echo
echo Please assign a root password...
getpass
echo
fi
fi
if [ -z "$encpass" ]; then
encpass=`echo $password | /usr/bin/encrypt -b 8`
fi
tmppass=$TMPDIR/tmppass
awk -v encpass=$encpass -F : '{
if (/^root/){
printf("%s:%s:%s:%s:%s:%s:%s:%s:%s:%s\n",
$1, encpass, $3, $4, $5, $6, $7, $8, $9, $10)
} else {print}}' $tmpmntvnd/master.passwd >> $tmppass
if [[ $? -eq 0 ]]; then
c 4 mv $tmppass $tmpmntvnd/master.passwd
c 4 pwd_mkdb -p -d $tmpmntvnd $tmpmntvnd/master.passwd
fi
###
#
# steal ntp servers from local ntpd.conf!
if [ -f $tmpmntvnd/ntpd.conf ]; then
if [ -z "$ntpservers" ]; then
ntpservers=`awk 'BEGIN {ORS=" "} /^server/{print $2}' /etc/ntpd.conf`
read nntpservers?"NTP Servers: [$ntpservers] "
if [ ! -z "$nntpservers" ]; then
ntpservers="$nntpservers"
fi
fi
if [ ! -z "$ntpservers" ]; then
echo Configuring ntpd.conf for NTP servers $ntpservers
egrep -v ^server $tmpmntvnd/ntpd.conf > $tmppass
for i in $ntpservers; do
c 4 "echo servers $i >> $tmppass"
done
c 4 mv $tmppass $tmpmntvnd/ntpd.conf
fi
fi
###
#
# steal dns servers from local resolv.conf!
if [ -z "$dnsservers" ]; then
dnsservers=`awk 'BEGIN {ORS=" "} /^nameserver/{print $2}' /etc/resolv.conf`
read ndnsservers?"DNS Servers: [$dnsservers] "
if [ ! -z "$ndnsservers" ]; then
dnsservers="$ndnsservers"
fi
fi
if [ ! -z "$dnsservers" ]; then
echo Configuring resolv.conf for DNS servers $dnsservers
resolv=$TMPDIR/resolv.conf
c 4 "echo lookup file bind > ${resolv}"
for i in $dnsservers; do
c 4 "echo nameserver $i >> ${resolv}"
done
if [ -f $tmpmnt/var.tar ]; then
c 4 mkdir $TMPDIR/var-copy
c 4 tar xpf $tmpmnt/var.tar -C $TMPDIR/var-copy
c 4 mkdir -p $TMPDIR/var-copy/db
c 4 mv ${resolv} $TMPDIR/var-copy/db/resolv.conf
c 4 tar cf $tmpmnt/var.tar -C $TMPDIR/var-copy .
else
c 4 mv ${resolv} ${tmpmntvnd}/resolv.conf
fi
else
echo Failed to configure nameservers, you must manually configure /etc/resolv.conf
fi
###
#
# tzfile
if [ ! -z "$tzfile" -a -f "$tzfile" ]; then
c 4 rm $tmpmntvnd/localtime
c 4 cp $tzfile $tmpmntvnd/localtime
fi
###
#
# set keyboard encoding (optional)
if [ -z "$keyboardencoding" ]; then
read keyboardencoding?"Keyboard encoding: [default] "
fi
if [ ! -z "$keyboardencoding" ]; then
testkeyboardencoding
if [ $? == 0 ]; then
sed -e "s:.*keyboard.encoding.*:keyboard.encoding=$keyboardencoding # use different keyboard encoding:" < $tmpmntvnd/examples/wsconsctl.conf >> $TMPDIR/tmpwsconsctl
c 4 mv $TMPDIR/tmpwsconsctl $tmpmntvnd/wsconsctl.conf
fi
fi
###
#
# this is the end
c 3 umount $tmpmntvnd
###
#
# done with the main disk vnd
c 2 vnconfig -u $device
c 1 umount $tmpmnt
###
#
# done with flash img vnd
vnuncfgroot
rm -r $TMPDIR
echo Done