forked from yellowman/flashrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkrdroot
executable file
·267 lines (207 loc) · 5.72 KB
/
mkrdroot
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
#!/bin/ksh
#
# flashrd mkrdroot
#
# create the kernel ramdisk
#
# this environment (and custom rc) is for setting up vnds
# so that we can mount the system and pass to real /etc/rc
#
# Chris Cappuccio <[email protected]>
#
# Crunchgen links
#
[ -z "$progs" ] && progs="ksh pax ls fsck fsck_ffs reboot init mount mount_ffs mount_vnd umount cp mv"
[ -z "$standlink" ] && standlink="tar pax mount_vnd vnconfig umount"
[ -z "$binlink" ] && binlink="sh ksh ls cp mv"
[ -z "$sbinlink" ] && sbinlink="init mount mount_ffs fsck fsck_ffs reboot"
# Files to copy
#
[ -z "$etccopy" ] && etccopy="etc/login.conf etc/pwd.db"
###
#
# blocks
[ -z "$blocks" ] && blocks=4600
###
#
# rdrootfs, device, distloc
[ -z "$rdroot" ] && rdroot=rd.`uname -m`-`date +%Y%m%d`
if [ -z "$distloc" -a -z "$1" ]; then
echo "% mkrdroot <openbsd base>"
exit 1
fi
[ -z "$distloc" ] && distloc=$1
###
#
# 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]}
if [ ! -f $distloc/dev/MAKEDEV ]; then
echo % $distloc/dev/MAKEDEV not found
exit 1
fi
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 /usr/src/bin/Makefile ]; then
echo OpenBSD source tree not found at /usr/src/bin
echo Giving up...
exit 1
fi
if [ ! -f /usr/src/sbin/Makefile ]; then
echo OpenBSD source tree not found at /usr/src/sbin
echo Giving up...
exit 1
fi
echo Creating ramdisk rootfs
###
#
# dd, vnconfig, disklabel, newfs
c 0 "dd if=/dev/zero of=$rdroot bs=512 count=$blocks > $TMPDIR/last.output 2>&1"
tmplabel=$(c 0 mktemp -t mkrdrootlabel.XXXXXX)
tmpmntdir=$(c 0 mktemp -t -d mkrdroot.XXXXXX)
echo "vnd-${tabname}:\\" > $tmplabel
echo " :dt=SCSI:ty=${tabname}:se#512:nt#1:ns#64:nc#$((blocks / 64)):su#${blocks}:\\" >> $tmplabel
echo " :pa#${blocks}:oa#0:fa#1024:ba#8192:ta=4.2BSD:\\" >> $tmplabel
echo " :pc#${blocks}:oc#0:" >> $tmplabel
# DISKTAB MODIFIED
c 0 mv /etc/disktab /etc/disktab.${tabname}
c 0 cp $tmplabel /etc/disktab
c 0 vnconfig -t vnd-${tabname} $device $rdroot
c 1 "disklabel -w $device vnd-${tabname} > $TMPDIR/last.output 2>&1"
# DISKTAB ORIGINAL
c 1 mv /etc/disktab.${tabname} /etc/disktab
c 1 "newfs -m 0 -o space -i 1024 /dev/r${device}a > $TMPDIR/last.output 2>&1"
c 1 mount /dev/${device}a $tmpmntdir
###
#
# Our ramdisk base
c 2 mkdir $tmpmntdir/bin $tmpmntdir/dev $tmpmntdir/etc $tmpmntdir/etc/firmware $tmpmntdir/home $tmpmntdir/mnt $tmpmntdir/root $tmpmntdir/sbin $tmpmntdir/stand $tmpmntdir/sys $tmpmntdir/tmp $tmpmntdir/usr $tmpmntdir/var $tmpmntdir/flash
# mount_tmpfs preserves permissions from parent mount
c 2 chmod 1777 $tmpmntdir/tmp
c 2 cp $distloc/.cshrc $distloc/.profile $tmpmntdir
c 2 cp $distloc/dev/MAKEDEV $tmpmntdir/dev
###
#
# Devices with an extra touch of bpf (normal distribution has bpf0-bpf9, so create starting at bpf10)
wd=`pwd`
cd $tmpmntdir/dev
c 2 ./MAKEDEV all
i=10
while [ $i -le 511 ]; do
c 2 ./MAKEDEV bpf$i
let i=i+1
done
cd $wd
###
#
# Oppa Crunchgen Style
echo -n "Crunchgen ($progs)"
cgdir=$(c 2 mktemp -t -d mkrdrootcg.XXXXXX)
# Ramdisk binaries aren't cleaned by crunchgen's Makefile, do them manually for NOPIE build
for progname in ${progs}; do
if [ -d /usr/src/bin/${progname} ]; then
c 2 cd /usr/src/bin/${progname}
c 2 make clean > /dev/null
else
c 2 cd /usr/src/sbin/${progname}
c 2 make clean > /dev/null
fi
done
cat <<-_EOF >>$cgdir/kcopy.conf
srcdirs /usr/src/bin /usr/src/sbin
progs $progs
ln ksh -sh # init invokes the shell with "-sh" in argv[0]
ln ksh sh
ln pax tar
ln mount_vnd vnconfig
libs -lutil
_EOF
c 2 cd $cgdir
c 2 "crunchgen -Em Makefile $cgdir/kcopy.conf > $TMPDIR/last.output 2>&1"
# Ramdisk binaries need to be built NOPIE
sed -e "/^.*cd .* && exec .* Makefile/s/exec/exec env NOPIE=/" Makefile > Makefile.tmp
c 2 mv Makefile.tmp Makefile
c 2 "make clean > $TMPDIR/last.output 2>&1"
c 2 "make objs > $TMPDIR/last.output 2>&1"
c 2 "make > $TMPDIR/last.output 2>&1"
# Don't leave non-PIE .o hanging around
for progname in ${progs}; do
if [ -d /usr/src/bin/${progname} ]; then
c 2 cd /usr/src/bin/${progname}
c 2 make clean > /dev/null
else
c 2 cd /usr/src/sbin/${progname}
c 2 make clean > /dev/null
fi
done
c 2 cd $wd
echo
###
#
# Copy specific items to kernel ramdisk
echo -n Linking kcopy
c 2 cp $cgdir/kcopy $tmpmntdir/stand/kcopy
for i in $standlink; do
echo -n " stand/$i"
c 2 ln $tmpmntdir/stand/kcopy $tmpmntdir/stand/$i
done
for i in $binlink; do
echo -n " bin/$i"
c 2 ln $tmpmntdir/stand/kcopy $tmpmntdir/bin/$i
done
for i in $sbinlink; do
echo -n " sbin/$i"
c 2 ln $tmpmntdir/stand/kcopy $tmpmntdir/sbin/$i
done
echo
echo -n Copying
for i in $etccopy; do
echo -n " $i"
c 2 cp $distloc/$i $tmpmntdir/etc
done
echo
echo -n Copying pre-mount firmware to ramdisk rootfs
if [ -f firmware.list ]; then
while read line; do
line=${line%%#*} # strip comments
test -z "$line" && continue
c 2 cp $distloc/etc/firmware/$line $tmpmntdir/etc/firmware
done < firmware.list
else
echo -n " (no firmware.list found for ramdisk firmware copy)"
fi
echo
###
#
# Copy custom rc to kernel ramdisk, cleanup
c 2 cp etc/rc $tmpmntdir/etc/rc
c 2 cp stand/rc $tmpmntdir/stand/rc
umountwait 1 $tmpmntdir
c 0 vnconfig -u $device