This repository has been archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathisopatch.sh
executable file
·212 lines (180 loc) · 6.03 KB
/
isopatch.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
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
#!/bin/bash
export PS4='+ \e[31m${FUNCNAME[0]:-isopatch}\e[39m:\e[97m${LINENO}\e[39m:> '
set -ouex pipefail
# Global state, please keep to a minimum
BOOT_MENU_PATH="${BOOT_MENU_PATH:-boot_menu.yml}"
readonly BOOT_MENU_PATH
GITHUB_REPOSITORY_OWNER="${GITHUB_REPOSITORY_OWNER:-ublue-os}"
readonly GITHUB_REPOSITORY_OWNER
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
readonly SCRIPT_DIR
WORK_DIR="$(mktemp -d -p "$SCRIPT_DIR")"
readonly WORK_DIR
REPO="${REPO:-releases}"
readonly REPO
if [[ ! "${WORK_DIR}" || ! -d "${WORK_DIR}" ]]; then
echo "Could not create temp dir"
exit 1
fi
cleanup() {
sudo umount "${WORK_DIR}/overlay/EFI" || true
sudo rm -rf "${WORK_DIR}"
}
trap cleanup EXIT ERR
# Main Function for the script
main() {
INPUT_ISO="$1"
readonly INPUT_ISO
OUTPUT_ISO="${SCRIPT_DIR}/${2:-output.iso}"
readonly OUTPUT_ISO
# ISO release version and architecture are embedded in .discinfo
xorriso -indev "${INPUT_ISO}" -osirrox on -extract /.discinfo "${WORK_DIR}/.discinfo"
RELEASE="$(sed "2q;d" "${WORK_DIR}/.discinfo")"
[[ "${RELEASE}" -eq "39" ]] && RELEASE="latest"
readonly RELEASE
ARCH="$(sed "3q;d" "${WORK_DIR}/.discinfo")"
readonly ARCH
VOLUME_ID="$(xorriso -indev "${INPUT_ISO}" -pvd_info 2> /dev/null | awk '/^Volume Id/{print $NF}')"
readonly VOLUME_ID
ESP_IMG="$(mkesp "${INPUT_ISO}")"
readonly ESP_IMG
# Prepare overlay
patch_grub_cfg
generate_ks
copy_secure_boot_keys
# Some overlayed files will have a different UID and GID from the ones that
# already came on the source ISO unless setting to root
sudo chown 0:0 -R "${ESP_IMG}" "${WORK_DIR}/overlay"
sudo umount "${ESP_IMG}"
generate_bootable_iso
}
patch_grub_cfg() {
EFI_GRUB_CFG_FILES=(
EFI/BOOT/grub.cfg
EFI/BOOT/BOOT.conf
)
readonly EFI_ROOT="${WORK_DIR}/overlay/EFI"
mkdir -p "${WORK_DIR}/overlay/boot/grub2"
mkdir -p "${WORK_DIR}/overlay/EFI/BOOT"
tree "${WORK_DIR}"
for GRUB_CFG in "${EFI_GRUB_CFG_FILES[@]}"; do
jinja2 --strict \
-o "${WORK_DIR}/overlay/${GRUB_CFG}" \
-D "BOOT_TYPE=efi" \
-D "GITHUB_REPOSITORY_OWNER=${GITHUB_REPOSITORY_OWNER}" \
-D "VOLUME_ID=${VOLUME_ID}" \
-D "RELEASE=${RELEASE}" \
"${SCRIPT_DIR}/installer/overlay/${ARCH}/${GRUB_CFG}" \
"${BOOT_MENU_PATH}"
done
sudo mount -o loop "${ESP_IMG}" "${WORK_DIR}/overlay/EFI"
sudo mkdir -p "${EFI_ROOT}/EFI/BOOT"
tree "${WORK_DIR}"
for GRUB_CFG in "${EFI_GRUB_CFG_FILES[@]}"; do
sudo jinja2 --strict \
-o "${EFI_ROOT}/${GRUB_CFG}" \
-D "BOOT_TYPE=efi" \
-D "GITHUB_REPOSITORY_OWNER=${GITHUB_REPOSITORY_OWNER}" \
-D "VOLUME_ID=${VOLUME_ID}" \
-D "RELEASE=${RELEASE}" \
"${SCRIPT_DIR}/installer/overlay/${ARCH}/${GRUB_CFG}" \
"${BOOT_MENU_PATH}"
done
MBR_GRUB_CFG_FILES=(
boot/grub2/grub.cfg
)
for GRUB_CFG in "${MBR_GRUB_CFG_FILES[@]}"; do
jinja2 --strict \
-o "${WORK_DIR}/overlay/${GRUB_CFG}" \
-D "BOOT_TYPE=mbr" \
-D "GITHUB_REPOSITORY_OWNER=${GITHUB_REPOSITORY_OWNER}" \
-D "VOLUME_ID=${VOLUME_ID}" \
-D "RELEASE=${RELEASE}" \
"${SCRIPT_DIR}/installer/overlay/${ARCH}/${GRUB_CFG}" \
"${BOOT_MENU_PATH}"
done
}
generate_ks() {
cp -rv "${SCRIPT_DIR}/installer/kickstart" "${WORK_DIR}/overlay"
jinja2 --strict \
-D "REPO=${REPO}" \
"${SCRIPT_DIR}/installer/kickstart/pre-install.sh" \
-o "${WORK_DIR}/overlay/kickstart/pre-install.sh"
sudo cp -rv "${SCRIPT_DIR}/installer/kickstart" "${EFI_ROOT}/kickstart"
sudo jinja2 --strict \
-D "REPO=${REPO}" \
"${SCRIPT_DIR}/installer/kickstart/pre-install.sh" \
-o "${EFI_ROOT}/kickstart/pre-install.sh"
}
copy_secure_boot_keys() {
cp -v "${SCRIPT_DIR}"/installer/securebootkeys/*.der "${WORK_DIR}/overlay"
sudo cp -v "${SCRIPT_DIR}"/installer/securebootkeys/*.der "${EFI_ROOT}"
}
get_esp_offset() {
IMG_FILE="$1"
parted --json --script "${IMG_FILE}" \
unit b \
print \
| jq --raw-output '
.disk.partitions[]
| select(.number == 2)
| .start[:-1]'
}
get_esp_size() {
IMG_FILE="$1"
parted --json --script "${IMG_FILE}" \
unit b \
print \
| jq --raw-output '
.disk.partitions[]
| select(.number == 2)
| .size[:-1]'
}
mkesp() {
IMG_FILE="$1"
ESP_OFFSET="$(get_esp_offset "${INPUT_ISO}")"
ESP_SIZE="$(get_esp_size "${INPUT_ISO}")"
echo "ESP Partition detected at offset: ${ESP_OFFSET}" >&2
dd bs=1 \
skip="${ESP_OFFSET}" \
count="${ESP_SIZE}" \
if="${IMG_FILE}" \
of="${WORK_DIR}/esp.img"
echo "${WORK_DIR}/esp.img"
}
generate_bootable_iso() {
{
cat <<EOF
stdio_sync off
padding 0
EOF
xorriso -indev "${INPUT_ISO}" -report_el_torito cmd \
| awk '!/append_partition/ && !/efi_path/' \
| sed 's/^-//'
cat <<EOF
padding 0
map ${WORK_DIR}/overlay /
boot_image isolinux partition_entry=gpt_basdat
append_partition 2 C12A7328-F81F-11D2-BA4B-00A0C93EC93B ${ESP_IMG}
boot_image any efi_path=--interval:appended_partition_2:all::
EOF
} | tee el_torito.log
xorriso \
-indev "${INPUT_ISO}" \
-outdev "${OUTPUT_ISO}" \
-options_from_file el_torito.log
implantisomd5 "${OUTPUT_ISO}"
echo "Created ISO: ${OUTPUT_ISO}"
# Only include the filename in the output or it breaks validation checks
(
ISO_FILE="$(basename "${OUTPUT_ISO}")"
cd "$(dirname "${OUTPUT_ISO}")"
stat -c "# ${ISO_FILE} - size %s bytes" "${ISO_FILE}"
sha256sum --tag "${ISO_FILE}"
) | tee "${OUTPUT_ISO}.sha256sum"
if [ "${GITHUB_WORKSPACE:-}" != "${SCRIPT_DIR}" ]; then
mv "${OUTPUT_ISO}" "${GITHUB_WORKSPACE}"
mv "${OUTPUT_ISO}.sha256sum" "${GITHUB_WORKSPACE}"
fi
}
main "$@"