forked from yellowman/flashrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
growimg
executable file
·326 lines (266 loc) · 7.3 KB
/
growimg
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
#!/bin/sh
#
# Chris Cappuccio <[email protected]>
#
# This is an example of how to create a new image, transfer the contents
# of an old image to it, and keep the user from screwing it up too bad, by
# performing some basic sanity checks.
#
# -t target-disk causes this script to write directly to a disk instead of
# a new VND image. This may be useful if you want to write a small image
# to a large flash without overwriting the unused space (which can be rather
# slow). This is as fast as running flashrd, without requiring
# the source files from flashrd.
#
uname=`uname -s`
[ -z "$distloc" ] && distloc=/
###
#
# 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`
vncfgroot() {
c 0 vnconfig $rootdevice $1
}
vnuncfgroot() {
c 0 vnconfig -u $rootdevice
}
sourcesectors2mbytes() {
echo $(((($1 / 1024) * sourcebytessec) / 1024))
}
###
#
# fall back
4() {
umount -f /dev/"$device"a
}
3() {
[ -z "$alt" ] && vnconfig -u $device
}
2() {
umount -f /dev/"$rootdevice"a
}
1() {
vnuncfgroot
}
0() {
rmdir $OldDir $NewDir
rm -f $NewImg $NewLabel
[[ -f /etc/disktab.${tabname} ]] && mv /etc/disktab.${tabname} /etc/disktab
exit 1
}
. ./flashrd.sub
if [ "$uname" != "OpenBSD" -a "$uname" != "Bitrig" ]; then
echo Sorry, cowboy. This script only runs on OpenBSD and similar systems.
exit 1
fi
[ `id -u` != 0 ] && echo root required && exit 1
syntax()
{
echo "disk target mode:"
echo " growimg <-t target-disk> <source-image-filename>"
echo
echo "image target mode:"
echo " growimg <-l sectors> <source-image-filename>"
}
islezero()
{
if [ "$1" -le "0" ]; then
syntax
echo -n expected a value larger than 0, got \"$1\"
if [ "$2" != "" ]; then
echo " for $2"
else
echo
fi
exit 1
fi
}
if [ "$1" == "-t" ]; then
# target device specified, use its geometry
device="$2"
alt=/dev/"$2"c
if [ ! -b $alt ]; then
echo "${device}(${alt}) is an invalid disk device, not found or not block device"
exit 1
fi
image=$3
else
if [ "$1" != "-l" ]; then
syntax
exit 1
fi
islezero $2 sectors
totalsize=$2
if [ "$3" == "-b" ]; then
islezero $4 bytessec
bytessec=$4
image=$5
else
image=$3
fi
if [ ! -f "$image" ]; then
syntax
exit 1
fi
fi
if [ ! -f $image ]; then
echo "Error: ${image} not found"
exit 1
fi
if [ -z "$device" ]; then
getavailvnd 2
rootdevice=${vndevices[0]}
device=${vndevices[1]}
else
[ -z "$rootdevice" ] && getavailvnd 1 && rootdevice=${vndevices[0]}
fi
if [ -z "$bytessec" ]; then
# destination bytes/sector
bytessec=512
fi
sourcebytessec=512
if [ -z "$alt" ]; then
cylinders=$((totalsize / 64))
trackscylinder=1
sectorstrack=64
else
# Here we use disklabel as a way to grab these values. OpenBSD 4.8+
# always reports actual detected values (not what was stored on disk)
tmplabel=`c 0 mktemp -t growimg.XXXXXX`
c 0 disklabel $device > $tmplabel
totalsize=`awk -F": " ' /^total sectors:/ {print $2} ' $tmplabel`
bytessec=`awk -F": " ' /^bytes\/sector:/ {print $2} ' $tmplabel`
sectorstrack=`awk -F": " ' /^sectors\/track:/ {print $2} ' $tmplabel`
trackscylinder=`awk -F": " ' /^tracks\/cylinder:/ {print $2} ' $tmplabel`
cylinders=`awk -F": " ' /^cylinders:/ {print $2} ' $tmplabel`
c 0 rm $tmplabel
fi
# The source image must be 4GB or less, or this will break.
imgbytes=`ls -l $image | awk ' { print $5 } '`
imgsize=$((imgbytes / sourcebytessec))
if [ $((imgsize * sourcebytessec)) != "$imgbytes" ]; then
cat <<-__FluxCapacitor
Size of image $image is not divisible by $sourcebytessec bytes/sector
without a remainder. Is this a disk image?
__FluxCapacitor
exit 1
fi
dstimgmb=`sectors2mbytes $totalsize`
srcimgmb=`sourcesectors2mbytes $imgsize`
echo Image: $image
echo Image size: $(((imgbytes / 1024) / 1024))MB
echo Requested size: ${dstimgmb}MB
echo Size increase: $((dstimgmb - srcimgmb))MB
echo
if [ $bytessec -gt 512 ]; then
factor=$((bytessec / 512))
if [ $((factor * 512)) != $bytessec ]; then
echo Block size $bytessec not divisible by 512. Are you sure?
exit
fi
else
unset factor
fi
if [ -z "$alt" ]; then
freespace=`df . | tail -1 | awk ' { print $4 } '`
if [ $bytessec -gt 512 ]; then
# df reports in 512 byte sectors, so scale freespace for comparison
# to media with different bytes/sec
freespace=$((freespace / factor))
fi
# The new image will not take more disk space than the old one, in fact
# it may take less (sparse file).
if [ "$imgsize" -gt "$freespace" ]; then
echo imgsize: $imgsize sectors
echo freespace: $freespace sectors
echo
echo there is not enough free disk space for the new image!
exit 1
fi
fi
vncfgroot $image
if [ -z "$alt" ]; then
[ -z "$NewImg" ] && NewImg=$(c 0 mktemp -t growimg.XXXXXX)
fi
[ -z "$NewLabel" ] && NewLabel=$(c 0 mktemp -t growimg.disklabel.XXXXXX)
[ -z "$OldDir" ] && OldDir=$(c 0 mktemp -t -d growimg.XXXXXX)
[ -z "$NewDir" ] && NewDir=$(c 0 mktemp -t -d growimg.XXXXXX)
c 1 mount -o rdonly /dev/"$rootdevice"a $OldDir
offset=$((4*1024*1024/${bytessec}))
asize=$((totalsize - offset))
echo "vnd-${tabname}:\\" > $NewLabel
echo " :dt=SCSI:ty=${tabname}:se#${bytessec}:nt#${trackscylinder}:ns#${sectorstrack}:nc#${cylinders}:su#${totalsize}:\\" >> $NewLabel
echo " :pa#${asize}:oa#${offset}:fa#$((bytessec * 2)):ba#$((bytessec * 16)):ta=4.2BSD:\\" >> $NewLabel
echo " :pc#${totalsize}:oc#0:" >> $NewLabel
# DISKTAB MODIFIED
c 2 mv /etc/disktab /etc/disktab.${tabname}
c 2 cp $NewLabel /etc/disktab
if [ -z "$alt" ]; then
echo Creating new image...
c 2 dd if=/dev/zero bs=512 count=512 of=$NewImg >/dev/null
c 2 vnconfig -t vnd-${tabname} $device $NewImg
c 3 fdisk -l $totalsize -f /usr/mdec/mbr -yi $device
else
c 3 fdisk -f /usr/mdec/mbr -yi $device
fi
echo
echo Writing flashrd disklabel...
c 3 disklabel -w $device vnd-${tabname}
# DISKTAB ORIGINAL
c 3 mv /etc/disktab.${tabname} /etc/disktab
echo
echo Creating new filesystem...
c 3 newfs -S $bytessec -q /dev/r"$device"a
c 3 mount -o async /dev/"$device"a $NewDir
used=`c 4 df $OldDir | tail -1 | awk ' { print $3 } '`
usedmb=`sourcesectors2mbytes $used`
free=`c 4 df $NewDir | tail -1 | awk ' { print $4 } '`
freemb=`sourcesectors2mbytes $free`
if [ "$freemb" -le "$usedmb" ]; then
echo
echo "Image files use ${usedmb}MB, but only ${freemb}MB available. Game over."
4; 3; 2; 1; 0;
fi
echo
echo Copying installation...
c 4 tar cf - -C $OldDir . | c 4 tar xpf - -C $NewDir
echo
echo Installing boot blocks...
c 4 installboot -r $NewDir $device $distloc/usr/mdec/biosboot $distloc/usr/mdec/boot
echo
echo Generate initial random seeds...
c 4 "mkdir -p $NewDir/etc"
c 4 "dd if=/dev/random of=$NewDir/etc/random.seed bs=512 count=1 status=none"
c 4 chmod 600 $NewDir/etc/random.seed
c 4 "dd if=/dev/random of=$NewDir/etc/host.random bs=65536 count=1 status=none"
c 4 chmod 600 $NewDir/etc/host.random
echo
echo Checking filesystem...
c 4 fsck -f /dev/"$device"a
umountwait 4 /dev/"$device"a
umountwait 3 /dev/"$rootdevice"a
if [ -z "$alt" ]; then
c 1 vnconfig -u $device
fi
c 0 vnuncfgroot
echo
echo Grow completed.
if [ -z "$alt" ]; then
echo New `sectors2mbytes $totalsize`MB image is located at $NewImg
if [ $bytessec -gt 512 ]; then
echo
echo disktab for mounting:
cat $NewLabel
fi
else
echo "New image written to ${device}(${alt})"
fi
echo
rmdir $NewDir $OldDir
rm $NewLabel
exit 0