Repository navigation
Expand file tree
/
Copy pathcreate_baseimg.sh
More file actions
executable file
·161 lines (137 loc) · 4.44 KB
/
Copy pathcreate_baseimg.sh
File metadata and controls
executable file
·161 lines (137 loc) · 4.44 KB
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
#!/bin/bash
# Settings
UBUNTU_VERSION="26.04.1"
ISO64="ubuntu-${UBUNTU_VERSION}-live-server-amd64.iso"
OUT64="unattended-${ISO64}"
IMG64="base-amd64.img"
TMPDIR="tmp"
USERDATA="configs/2604_autoinstall.yaml"
METADATA="configs/2604_metadata"
function usage() {
echo "Usage: create_baseimg.sh [-s size] [--no-usb]"
echo ""
echo "-s|--size n Size of the resulting image(default 28500M)"
echo "--no-usb Don't create a fat32 partition for easy usb mounting"
exit 1
}
ISO=$ISO64
OUTISO=$OUT64
IMG=$IMG64
USB_PARTITION=1
while [[ $# -ge 1 ]]; do
key="$1"
case $key in
-s|--size)
IMGSIZE=$2
shift
;;
--no-usb)
USB_PARTITION=0
;;
*)
usage
;;
esac
shift
done
# Default image size 14700M(fits on an 16G flash drive)
# IMGSIZE=${IMGSIZE:-14700M}
# Default image size 28500M(fits on an 32G flash drive)
IMGSIZE=${IMGSIZE:-28500M}
# fetch <url> [file]: to stdout, or resuming into file
function fetch() {
if hash curl 2>/dev/null; then
if [ -n "${2:-}" ]; then curl -fL -C - --progress-bar -o "$2" "$1"; else curl -fsSL "$1"; fi
else
if [ -n "${2:-}" ]; then wget -c -O "$2" "$1"; else wget -qO- "$1"; fi
fi
}
function download_iso() {
[ -f "$ISO" ] && return 0
# Unlike releases.ubuntu.com/26.04/, this keeps serving a point release after the next one is out
local url="https://releases.ubuntu.com/$UBUNTU_VERSION"
echo "Downloading $ISO from $url"
SUMS=$(fetch "$url/SHA256SUMS") && fetch "$url/$ISO" "$ISO.part" || exit 1
if ! grep " \*\?$ISO\$" <<<"$SUMS" | sed "s/ \*\?$ISO\$/ $ISO.part/" | sha256sum -c -; then
echo "Checksum mismatch for $ISO, deleting it" >&2
rm -f "$ISO.part"
exit 1
fi
mv "$ISO.part" "$ISO"
}
function create_unattended_iso() {
CONTENTSDIR="$TMPDIR/contents"
rm -rf "$CONTENTSDIR"
mkdir -p "$CONTENTSDIR"
# Extract the efi partition out of the iso
read -a EFI_PARTITION < <(parted -m $ISO unit b print | awk -F: '$1 == "2" { print $2,$3,$4}' | tr -d 'B')
dd if=$ISO of=$TMPDIR/efi.img skip=${EFI_PARTITION[0]} bs=1 count=${EFI_PARTITION[2]}
# # this is basically /usr/lib/grub/i386-pc/boot_hybrid.img from grub-pc-bin package (we just skip the end bits which xorriso will recreate)
dd if=$ISO of=$TMPDIR/mbr.img bs=1 count=440
#Use bsdtar if possible to extract(no root required)
if hash bsdtar 2>/dev/null; then
bsdtar xfp $ISO -C $CONTENTSDIR
chmod -R u+w "$CONTENTSDIR"
else
# mount the iso, then copy the contents
LOOPDIR="$TMPDIR/iso"
mkdir -p "$LOOPDIR"
sudo mount -o loop "$ISO" "$LOOPDIR"
cp -rT "$LOOPDIR" "$CONTENTSDIR"
sudo umount "$LOOPDIR"
fi
mkdir -p "$CONTENTSDIR/autoinst"
cp "$USERDATA" "$CONTENTSDIR/autoinst/user-data"
cp "$METADATA" "$CONTENTSDIR/autoinst/meta-data"
if [[ $USB_PARTITION == 0 ]]; then
# remove the ICPC partition from the user-data yaml if we aren't going to use it
sed -i -e "/USB_PARTITION_ENABLED/d" "$CONTENTSDIR/autoinst/user-data"
fi
# TODO: skip all this isolinux stuff, and change boot/grub/grub.cfg instead
# Configure grub to start the autoinstall after 3 seconds
cat <<EOF > "$CONTENTSDIR/boot/grub/grub.cfg"
set timeout=3
loadfont unicode
set menu_color_normal=white/black
set menu_color_highlight=black/light-gray
menuentry "Install Ubuntu Server (Unattended)" {
set gfxpayload=keep
linux /casper/vmlinuz autoinstall ds=nocloud\;seedfrom=/cdrom/autoinst/ --
initrd /casper/initrd
}
EOF
set -x
# Finally pack up an ISO the new way
xorriso -as mkisofs -r \
-V 'ATTENDLESS_UBUNTU' \
-o $OUTISO \
--grub2-mbr $TMPDIR/mbr.img \
-partition_offset 16 \
--mbr-force-bootable \
-append_partition 2 28732ac11ff8d211ba4b00a0c93ec93b $TMPDIR/efi.img \
-appended_part_as_gpt \
-iso_mbr_part_type a2a0d0ebe5b9334487c068b6b72699c7 \
-c '/boot.catalog' \
-b '/boot/grub/i386-pc/eltorito.img' \
-no-emul-boot -boot-load-size 4 -boot-info-table --grub2-boot-info \
-eltorito-alt-boot \
-e '--interval:appended_partition_2:::' \
-no-emul-boot \
$CONTENTSDIR
set +x
# cleanup
rm -rf "$CONTENTSDIR"
}
download_iso
create_unattended_iso
# Install that base image
rm -f "output/$IMG"
set -x
qemu-img create -f qcow2 -o size="$IMGSIZE" "output/$IMG"
qemu-system-x86_64 \
--enable-kvm -smp 4 -m 4096 \
-drive file="output/$IMG",index=0,media=disk,format=qcow2 \
-cdrom $OUTISO -boot order=d \
-net nic -net user,hostfwd=tcp::5222-:22,hostfwd=tcp::5280-:80 \
-vga qxl -vnc :0 \
-usbdevice tablet