-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathretro
executable file
·420 lines (384 loc) · 10.2 KB
/
retro
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
#!/usr/bin/env bash
# Downloads, extracts, and runs retro distros
SCRIPTDIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
RETROHOME=${RETROHOME:-$SCRIPTDIR}
ORIGBASE=$RETROHOME/.downloads
SLACKBASE=$ORIGBASE/mirrors.slackware.com/slackware
AUTOBASE=$SCRIPTDIR/autoinst
# temp directory automatically cleaned up on exit
TEMPDIR=$(mktemp -d)
trap 'rm -rf "$TEMPDIR"' EXIT
# outputs fdisk commands to add a partition
new_partition() {
cat <<EOF
n
$PARTMODE
EOF
# logical partition doesn't ask for a partition number
if [ "$PARTMODE" != "l" ]; then
echo $PARTNUM
fi
cat <<EOF
$PARTBEGIN
$PARTEND
t
$PARTNUM
$PARTTYPE
EOF
}
# adds a final newline to a file so read loop works correctly
cat_newline() {
cat $1
echo
}
# generates fdisk commands for the partition table specified with format:
# mode num begin end type
# mode: p = primary; e = extended; l = logical
# num: partition number: 1-4 (primary/extended); 5+: logical
# begin/end: in "cylinders" (depends on disk size and kernel version)
# type: 82 = swap, 83 = ext2
fdisk_commands() {
cat_newline $1 | while read PARTMODE PARTNUM PARTBEGIN PARTEND PARTTYPE PARTIGNORE; do
case $PARTMODE in
[pel] ) new_partition ;; # primary / extended / logical
* ) ;; # ignore invalid partition modes
esac
done
echo "w" # write partition
echo # extra newline
}
# link the autoinst files listed in $1 to the directory $2
link_autoinst() {
NUM=1
cat_newline $1 | while read FILE; do
ln $AUTOBASE/$FILE $2/$(printf "%02d" $NUM).sh
NUM=$(($NUM+1))
done
}
# patches boot or root disk with autoinst.sh
autoinst_patch() {
retro_extract
if [[ -f $CACHEDIR/root.img ]]; then
PATCHIMG=$CACHEDIR/root.img
elif [ -f $CACHEDIR/boot.img ]; then
PATCHIMG=$CACHEDIR/boot.img
fi
echo "Using sudo to patch $PATCHIMG..."
sudo mount $PATCHIMG /mnt
sudo cp $AUTOBASE/autoinst.sh /mnt
sudo chmod +x /mnt/autoinst.sh
sudo umount /mnt
}
# prepare auto-install/config files for the current distro
# $1 is a partition table file in `autoinst/fdisk`
autoinst_prep() {
AUTOINSTD=$CACHEDIR/install/autoinst.d
mkdir -p $AUTOINSTD/inststep $AUTOINSTD/confstep
ln $AUTOBASE/autoinst.sh $AUTOBASE/autoconf.sh $CONFDIR/config.sh $AUTOINSTD
if [[ -f $CONFDIR/autoinst.txt ]]; then
link_autoinst $CONFDIR/autoinst.txt $AUTOINSTD/inststep
fi
if [[ -f $CONFDIR/autoconf.txt ]]; then
link_autoinst $CONFDIR/autoconf.txt $AUTOINSTD/confstep
fi
fdisk_commands $AUTOBASE/fdisk/$1 > $AUTOINSTD/fdisk.hda
}
# download URLs to the specified filenames contained in file $1
# file format is `filename url`, one per line
download_list() {
if [[ $# -ne 2 ]]; then
exit 1
fi
cat_newline $1 | while read FILE URL; do
DEST=$2/$FILE
if [[ -n "$FILE" && -n "$URL" ]]; then
if [[ ! -f "$DEST" ]]; then
echo "Downloading $FILE"
wget --no-verbose --show-progress -O "$DEST" "$URL"
else
echo "Already downloaded: $FILE"
fi
fi
done
}
# download version $1 of Slackware from the official mirror
download_slackware() {
if [[ $# -ne 1 ]]; then
exit 1
fi
if [[ ! -d "$SLACKBASE/slackware-$1" ]]; then
wget \
--no-verbose \
--show-progress \
--recursive \
--no-parent \
--reject "*.md5*,*.meta4,*.sha*,*mirror*,*index*" \
"http://mirrors.slackware.com/slackware/slackware-$1/"
else
echo "Already downloaded: slackware-$1"
fi
}
# download files for the distro $1 to directory $2
download_all() {
mkdir -p $2
if [[ -f $1/source.txt ]]; then
SOURCE=$(cat $1/source.txt)
SOURCEDIR=$(cd "$SCRIPTDIR/$SOURCE" && pwd)
download_all $SOURCEDIR $ORIGBASE/$SOURCE
ln -sfr $ORIGBASE/$SOURCE/* $2
fi
if [[ -f "$1/urls.txt" ]]; then
download_list "$1/urls.txt" "$2"
fi
if [[ -f "$1/slackver.txt" ]]; then
VER="$(cat "$1/slackver.txt")"
(cd $ORIGBASE; download_slackware $VER)
fi
if [[ -f "$1/download.sh" ]]; then
pushd $2 > /dev/null
source "$1/download.sh"
popd > /dev/null
fi
}
retro_download() {
download_all $CONFDIR $ORIGDIR
}
# fix permissions/ownership for files copied from disk image
fix_perms() {
sudo chown -R $USER:$USER "$1"
chmod -R ugo+r "$1"
chmod -R u+w "$1"
chmod -R go-w "$1"
if [[ -d "$1" ]]; then
find "$1" -type d | xargs chmod ugo+x
fi
}
# mount a disk image and copy files from it
mount_copy() {
IMAGE=$1
DEST=$2
shift; shift
echo "Extract: using sudo to mount $(basename $IMAGE)"
sudo mount -o ro "$IMAGE" /mnt
if [[ $# -gt 0 ]]; then
while [[ $# -gt 0 ]]; do
sudo cp -R --preserve=timestamp "/mnt/$1" "$DEST"
fix_perms $DEST/$(basename $1)
shift
done
else
sudo cp -R --preserve=timestamp "/mnt" "$DEST"
fix_perms $DEST
fi
sudo umount /mnt
}
# extract installation files for the selected distro
retro_extract() {
if [[ ! -d $CACHEDIR ]]; then
retro_download
if [[ -f "$CONFDIR/extract.sh" ]]; then
mkdir -p $CACHEDIR
pushd $CACHEDIR > /dev/null
source "$CONFDIR/extract.sh"
popd > /dev/null
else
echo "Nothing to extract"
fi
else
echo "Using cached files"
fi
}
# run QEMU for the selected distro
retro_run() {
retro_extract
mkdir -p $QEMUDIR
pushd $QEMUDIR > /dev/null
# link installation files
if [[ -f $CACHEDIR/boot.img && ! -f fda.img ]]; then
ln -sr $CACHEDIR/boot.img fda.img
fi
if [[ -f $CACHEDIR/root.img && ! -f fdb.img ]]; then
ln -sr $CACHEDIR/root.img fdb.img
fi
if [[ -f $ORIGDIR/disc1.iso && ! -f hdc.iso ]]; then
ln -sr $ORIGDIR/disc1.iso hdc.iso
fi
if [[ -d $CACHEDIR/install && ! -d hdb ]]; then
ln -sr $CACHEDIR/install hdb
fi
# default configuration
QEMU_SYSTEM="qemu-system-i386"
QEMU_MACHINE="type=isapc"
QEMU_SMP="1"
QEMU_RAM="16M"
QEMU_HD_SIZE="500M"
QEMU_HD_FORMAT="qcow2"
QEMU_NET_DEVICE="ne2k_isa"
QEMU_INTERNET="" # disabled
QEMU_RETRONET="
-netdev socket,id=retronet,connect=:1234
-device $QEMU_NET_DEVICE,netdev=retronet"
QEMU_EXTRA=""
# Allow overriding default configuration
if [[ -f $CONFDIR/qemu.sh ]]; then
source $CONFDIR/qemu.sh
fi
# create hard drive image
if [[ ! -f hda.img ]]; then
if [[ -f fda.img || -f hdc.iso ]]; then
qemu-img create -f $QEMU_HD_FORMAT hda.img $QEMU_HD_SIZE
else
echo "No bootable devices"
if [[ -d $QEMUDIR && -z $(ls -A $QEMUDIR) ]]; then
rmdir $QEMUDIR
fi
exit 1
fi
fi
# Add -drive parameters for each image provided
QEMU_DRIVES=""
for DRIVE in fda fdb hda hdb hdc hdd; do
INDEX=$(echo $DRIVE | cut -c3 | tr abcd 0123)
if [[ $DRIVE = fd* ]]; then
INTERFACE=floppy
FORMAT=raw
else
INTERFACE=ide
if [[ -f $DRIVE.img ]]; then
FORMAT=$QEMU_HD_FORMAT
else
FORMAT=raw
fi
fi
if [[ -f $DRIVE.img ]]; then
QEMU_DRIVES="$QEMU_DRIVES -drive if=$INTERFACE,index=$INDEX,format=$FORMAT,file=$DRIVE.img"
elif [[ -f $DRIVE.iso ]]; then
QEMU_DRIVES="$QEMU_DRIVES -drive if=$INTERFACE,index=$INDEX,format=$FORMAT,media=cdrom,file=$DRIVE.iso"
elif [[ -d $DRIVE ]]; then
QEMU_DRIVES="$QEMU_DRIVES -drive if=$INTERFACE,index=$INDEX,format=raw,file=fat:rw:$DRIVE"
fi
done
# Run QEMU
QEMU_COMMAND="
$QEMU_SYSTEM
-machine $QEMU_MACHINE
-smp $QEMU_SMP
-m $QEMU_RAM
-serial mon:stdio
$QEMU_INTERNET
$QEMU_RETRONET
$QEMU_DRIVES
$QEMU_EXTRA
$@"
QEMU_COMMAND=$(echo $QEMU_COMMAND | tr -d '\n' | tr -s ' ')
echo
echo "QEMU command: $QEMU_COMMAND"
echo
if [[ $COMMAND == "run" ]]; then
$QEMU_COMMAND
fi
popd > /dev/null
}
# generate bat file for running QEMU on windows
retro_package() {
echo $@
if [[ $# -ge 1 && $1 == "--hda" ]]; then
FILES="hda.img retro.bat retro.sh"
shift
else
FILES="*"
fi
retro_run $@
echo
echo "Packaging $CONFNAME..."
echo "@echo off" > "$QEMUDIR/retro.bat"
echo $QEMU_COMMAND >> "$QEMUDIR/retro.bat"
echo "#!/bin/sh" > "$QEMUDIR/retro.sh"
echo $QEMU_COMMAND >> "$QEMUDIR/retro.sh"
chmod +x "$QEMUDIR/retro.sh"
TARNAME=$(echo $CONFNAME | tr / -)
(cd $QEMUDIR; tar cvfz ../$TARNAME.tar.gz $FILES --dereference --transform "s|^|$TARNAME/|")
ls -lh $TARNAME.tar.gz
}
retro_reset() {
read -p "Really remove QEMU images and cached files for $CONFNAME? " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
rm -rf $CACHEDIR
rm -rf $QEMUDIR
echo "Distro reset."
else
echo "Reset aborted."
fi
}
# usage instructions
retro_usage() {
cat <<EOF
Usage: $(basename $0) [COMMAND] [CONFIG] [OPTIONS]
Commands:
run default run the distro in QEMU; calls extract
extract extract files for the distro; calls download
download download source files for the distro or CD-ROM
patch patch the boot/root disk for auto-installation.
package create a tar with the QEMU image files and startup script
reset remove the QEMU images and cached files for the distro
Config:
If specified, should be the name of a config directory.
Defaults to the current directory.
Additional options are passed to QEMU verbatim.
EOF
exit 1
}
# see if the first parameter is a command; default to run otherwise
if [[ $# -ge 1 ]]; then
case $1 in
run | extract | download | reset | patch | package)
COMMAND=$1
shift
;;
*)
COMMAND=run
esac
else
COMMAND=run
fi
# next parameter should be a configuration file; default to pwd
if [[ $# -eq 1 ]]; then
if [[ -d "$1" ]]; then
CONFDIR=$(cd "$1" && pwd)
shift
elif [[ -d "$SCRIPTDIR/$1" ]]; then
CONFDIR=$(cd "$SCRIPTDIR/$1" && pwd)
shift
elif [[ $1 == \-* ]]; then
CONFDIR=$PWD
else
echo "Configuration $1 doesn't exist"
exit 1
fi
else
CONFDIR=$PWD
fi
CONFNAME=$(echo $CONFDIR | sed "s|^$SCRIPTDIR/||")
ORIGDIR=$ORIGBASE/$CONFNAME
CACHEDIR=$CONFDIR/.cache
QEMUDIR=$CONFDIR/.qemu
# output configuration details
cat <<EOF
Retro Distro Playground by J.B. Langston
Command: $COMMAND
Config: $CONFDIR
Download: $ORIGDIR
Cache: $CACHEDIR
QEMU: $QEMUDIR
EOF
# run the selected command
case $COMMAND in
download) retro_download;;
extract) retro_extract;;
run) retro_run $@;;
package) retro_package $@;;
reset) retro_reset;;
patch) autoinst_patch;;
*) retro_usage;;
esac