-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_image.sh
executable file
·77 lines (63 loc) · 2.05 KB
/
create_image.sh
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
#!/bin/sh
usage() { echo "USAGE: sh $0 [OPTIONS]
OPTIONS:
-h display help
-i <imagename> specify a name for the new qemu disk image
-s <size> create disk image with the given size, default 384M (format from qemu-img manpage)
" 1>&2; exit 1; }
SIZE="384M"
IMG="qemu_image.img"
while getopts ":hi:s:" OPT; do
case "$OPT" in
h)
usage
;;
i)
IMG="$OPTARG"
;;
s)
SIZE="$OPTARG"
;;
*)
usage
;;
esac
done
shift $((OPTIND - 1))
TS=$(date +%s%N)
DIR="/tmp/mount_dir-$TS"
[ "$(echo "$IMG" | sed -r 's/.*(.{4})/\1/')" = ".img" ] || { echo "ERROR: image file must have suffix .img"; exit 1; }
echo "Building $IMG..."
command -v debootstrap > /dev/null || {
echo "ERROR: please install debootstrap"
echo " If not on a Debian-based system, a pre-built image is available at"
echo " https://calder.dev/files/qemu_image.img"
exit 1
}
if [ -f "$IMG" ]; then
echo "WARNING: $IMG already exists. Overwrite it? [N/y] "
read -r RESP
if [ "$RESP" = "y" ]; then
rm "$IMG"
else
echo "$IMG unchanged."
exit 0
fi
fi
qemu-img create "$IMG" "$SIZE" # create a qemu disk image
mkfs.ext2 "$IMG" # ext2 is simple and fast, not journaled
mkdir -p "$DIR"
sudo mount -o loop "$IMG" "$DIR" # uses a loop device to map the disk image to the directory
sudo debootstrap --arch amd64 buster "$DIR" # install a minimal Debian Buster system
sudo chroot "$DIR" passwd -d root # remove root password
sudo rm -f "$DIR/etc/hostname" # remove hostname so that it is always localhost
# set up autologin to the ttyS0 terminal using getty
GETTYDIR="$DIR/etc/systemd/system/[email protected]"
sudo mkdir -p "$GETTYDIR"
OVER="/tmp/$TS-override.conf"
echo '[Service]' > "$OVER"
echo 'ExecStart=' >> "$OVER"
echo 'ExecStart=-/sbin/agetty --autologin root --noclear %I vt220' >> "$OVER"
sudo mv "$OVER" "$GETTYDIR/override.conf"
sudo umount "$DIR"
rmdir "$DIR"