-
Notifications
You must be signed in to change notification settings - Fork 1
/
entrypoint.sh
executable file
·181 lines (169 loc) · 6.5 KB
/
entrypoint.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
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
#!/bin/bash
# Set strict bash mode and allow debugging.
set -euo pipefail
if [ -n "${DEBUG:-}" ]; then
set -x
fi
# Input defaults and script variables
NAME="${NAME:-custom}"
MNT="${MNT:-/mnt}"
MEDIA="${MEDIA:-/media}"
TMP="${TMP:-/tmp}"
DEBUG="${DEBUG:-}"
FORCE="${FORCE:-}"
NOMODESET="${NOMODESET:-}"
TIMEOUT="${TIMEOUT:-5}"
# Check input parameters and existence of preseed.cfg.
check_input() {
if [ -z "${VERSION+set}" ]; then
echo "Error: VERSION is not set. Please set a Debian ISO version number."
exit 1
fi
if [ ! -f "${MNT}/preseed.cfg" ]; then
echo "Error: missing ${MNT}/preseed.cfg. Please attach a volume to ${MNT}." 1>&2
exit 1
fi
}
# Download official Debian netinstall ISO or do nothing if one is already found on the volume.
get_iso() {
if [ ! -f "${MNT}/debian-${VERSION}-amd64-netinst.iso" ]; then
echo "Downloading https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-${VERSION}-amd64-netinst.iso"
wget -O "${MNT}/debian-${VERSION}-amd64-netinst.iso" "https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-${VERSION}-amd64-netinst.iso"
wget -O "${MNT}/SHA512SUMS" "https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/SHA512SUMS"
wget -O "${MNT}/SHA512SUMS.sign" "https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/SHA512SUMS.sign"
cd "${MNT}"
sha512sum --ignore-missing -c SHA512SUMS
gpg --keyserver keyring.debian.org --auto-key-retrieve --verify SHA512SUMS.sign SHA512SUMS
if [ -z "${DEBUG}" ]; then
rm SHA512SUMS.sign SHA512SUMS
fi
else
echo "Using existing ISO at ${MNT}/debian-${VERSION}-amd64-netinst.iso"
fi
}
# Unpack ISO
unpack_iso() {
xorriso -osirrox on -indev "${MNT}/debian-${VERSION}-amd64-netinst.iso" -extract . "${MEDIA}"
}
# Update the MD5 file for existing files. Input: absolute path (Starting with ${MEDIA}).
update_md5() {
LISTNAME=".${1##${MEDIA}}"
MD5SUM="$(cd "${MEDIA}" && md5sum "${LISTNAME}")"
sed -i "s,^[0-9a-f]* ${LISTNAME}$,${MD5SUM}," "${MEDIA}/md5sum.txt"
}
# Add preseed onto the ISO filesystem.
# During installation, it is mounted under /cdrom/preseed.cfg.
preseed_onto_fs() {
cp "${MNT}/preseed.cfg" "${MEDIA}"
cd "${MEDIA}" && md5sum "preseed.cfg" >> "${MEDIA}/md5sum.txt"
PRESEED="file=file:///cdrom/preseed.cfg"
}
# Add the firmware folder to the ISO filesystem, if exists.
# During installation, it is mounted under /cdrom/firmware.
firmware_onto_fs() {
if [ -d "${MNT}/firmware" ]; then
cp -r "${MNT}/firmware" "${MEDIA}"
cd "${MEDIA}"
find ./firmware -type f -exec sh -c 'md5sum "$1" >> "${MEDIA}/md5sum.txt"' \;
fi
}
# Set default menu entry for BIOS boot
bios_set_default_entry() {
# FORCE=no: We will add preseed.cfg to only the "Automated install" entries in the boot menu.
if [ -z "${FORCE}" ]; then
# Remove menu default choices.
grep -l "menu default" "${MEDIA}"/isolinux/*.cfg | while IFS= read -r line
do
sed -i '/^\tmenu default$/d' "$line"
sed -i '/^default \(.*\)$/d' "$line"
update_md5 "$line"
done
# Add default choice for automated (label auto*) menu items.
grep -l "label auto" "${MEDIA}"/isolinux/*.cfg | while IFS= read -r line
do
MENUITEM="$(grep "label auto" "$line" | sed 's/.*label //')"
sed -i '/label '"${MENUITEM}"'/ a default '"${MENUITEM}"'\n menu default' "$line"
update_md5 "$line"
done
# Open boot menu to advanced options
sed -i '/^menu begin advanced$/ a menu start' "${MEDIA}"/isolinux/menu.cfg
update_md5 "${MEDIA}"/isolinux/menu.cfg
# Add preseed.cfg to automatic installation menu items.
APPEND="${PRESEED}"
if [ -n "${NOMODESET}" ]; then
APPEND="${APPEND} nomodeset"
fi
grep -l "auto=true" "${MEDIA}"/isolinux/*.cfg | while IFS= read -r line
do
sed -i 's@auto=true@'"$APPEND auto=true"'@' "$line"
update_md5 "$line"
done
# FORCE=yes: We will add preseed.cfg to all the entries in the boot menu.
else
APPEND="${PRESEED} auto=true" #Note: in some menu items "auto=true" will show twice. That's ok.
if [ -n "${NOMODESET}" ]; then
APPEND="${APPEND} nomodeset"
fi
for i in "${MEDIA}"/isolinux/*.cfg
do
sed -i 's@\---@'"$APPEND ---"'@' "$i"
update_md5 "$i"
done
fi
# Set boot menu timeout.
sed -i 's/^timeout .*$/timeout '"${TIMEOUT}0"'/' "${MEDIA}"/isolinux/isolinux.cfg
update_md5 "${MEDIA}"/isolinux/isolinux.cfg
}
# Set default menu entry for UEFI boot
uefi_set_default_entry() {
# FORCE=no: We will add preseed.cfg to only the "Automated install" entries in the boot menu.
if [ -z "${FORCE}" ]; then
# Add preseed.cfg to automatic installation menu items.
APPEND="${PRESEED}"
if [ -n "${NOMODESET}" ]; then
APPEND="${APPEND} nomodeset"
fi
sed -i 's@auto=true@'"$APPEND auto=true"'@' "${MEDIA}"/boot/grub/grub.cfg
# Set default entry and timeout
sed -i '/insmod play/ a default='\'"2"\''\ntimeout='"${TIMEOUT}"'' "${MEDIA}"/boot/grub/grub.cfg
sed -i '/submenu.*Advanced options/ a default=2' "${MEDIA}"/boot/grub/grub.cfg
sed -i '/submenu.*Speech-enabled advanced options/ a default=2' "${MEDIA}"/boot/grub/grub.cfg
sed -i '/submenu.*Accessible dark contrast installer menu/ a default=2' "${MEDIA}"/boot/grub/grub.cfg
update_md5 "${MEDIA}"/boot/grub/grub.cfg
# FORCE=yes: We will add preseed.cfg to all the entries in the boot menu.
else
APPEND="${PRESEED} auto=true" #Note: in some menu items "auto=true" will show twice. That's ok.
if [ -n "${NOMODESET}" ]; then
APPEND="${APPEND} nomodeset"
fi
sed -i 's@\---@'"$APPEND ---"'@' "${MEDIA}"/boot/grub/grub.cfg
sed -i '/insmod play/ a timeout='"${TIMEOUT}"'' "${MEDIA}"/boot/grub/grub.cfg
update_md5 "${MEDIA}"/boot/grub/grub.cfg
fi
}
# Build ISO using the previous build command with some simplifications.
build_iso() {
dd if="${MNT}/debian-${VERSION}-amd64-netinst.iso" bs=1 count=432 of="${TMP}/isohdpfx.bin"
sed \
-e 's/-jigdo-[^ ]* [^ ]* //g' \
-e 's/-checksum_algorithm_iso [^ ]* //' \
-e 's/-checksum-list [^ ]* //' \
-e 's,-isohybrid-mbr [^ ]* ,-isohybrid-mbr '"${TMP}"'/isohdpfx.bin ,' \
-e 's/ CD1//' \
-e 's, boot1, '"${MEDIA}"',' \
-e 's,-o [^ ]*,-o "'"${MNT}"'/debian-'"${VERSION}"'-amd64-'"${NAME}"'.iso",' < "${MEDIA}/.disk/mkisofs" > "${TMP}/mkiso"
chmod 755 "${TMP}/mkiso"
"${TMP}/mkiso"
if [ -z "${DEBUG}" ]; then
rm "${TMP}/mkiso"
fi
}
check_input
get_iso
unpack_iso
preseed_onto_fs
firmware_onto_fs
bios_set_default_entry
uefi_set_default_entry
build_iso
echo "debian-${VERSION}-amd64-${NAME}.iso created."