-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathubuntu-autoinstall-generator-tools.sh
executable file
·600 lines (545 loc) · 22.8 KB
/
ubuntu-autoinstall-generator-tools.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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#!/bin/bash
set -Eeuo pipefail
function cleanup() {
trap - SIGINT SIGTERM ERR EXIT
if [ -n "${tmpdir+x}" ]; then
rm -rf "$tmpdir" "$bootdir"
log "🚽 Deleted temporary working directory $tmpdir"
fi
}
# Capture signals to clear temporary files
trap cleanup SIGINT SIGTERM ERR EXIT
bootdir="/tmp/BOOT"
# Gets the current location of the script
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
# Check whether the date command-line tools exists
[[ ! -x "$(command -v date)" ]] && echo "💥 date command not found." && exit 1
today=$(date +"%Y-%m-%d")
ubuntu_gpg_key_id="843938DF228D22F7B3742BC0D94AA3F0EFE21092"
function log() {
echo >&2 -e "[$(date +"%Y-%m-%d %H:%M:%S")] ${1-}"
}
function die() {
local msg=$1
local code=${2-1} # Bash parameter expansion - default exit status 1. See https://wiki.bash-hackers.org/syntax/pe#use_a_default_value
log "$msg"
exit "$code"
}
# usage of the command line tool
function usage() {
cat <<EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [-a] [-e] [-u user-data-file] [-m meta-data-file] [-p package-name] [-c config-data-file] [-t temaplate-config-file] [-s service-dir-name] [-j job-name] [-k] [-o] [-r] [-d destination-iso-file]
💁 This script will create fully-automated Ubuntu release version 20 to 24 installation media.
Available options:
-h, --help Print this help and exit
-v, --verbose Print script debug info
-a, --all-in-one Bake user-data and meta-data into the generated ISO. By default you will
need to boot systems with a CIDATA volume attached containing your
autoinstall user-data and meta-data files.
For more information see: https://ubuntu.com/server/docs/install/autoinstall-quickstart
-e, --use-hwe-kernel Force the generated ISO to boot using the hardware enablement (HWE) kernel. Not supported
by early Ubuntu 20.04 release ISOs.
-u, --user-data Path to user-data file. Required if using -a
-n, --release-name Specifies the code name to download the ISO image distribution, You must select any string
from the list as an argument. eg: focal, jammy, noble.
-m, --meta-data Path to meta-data file. Will be an empty file if not specified and using -a
-p, --package-name Bake he package-name downloaded installation into the generated ISO. if the package-name is empty,
no installation package will be downloaded. Path to package-name file. Required if using -a
-c, --config-data Path to config-data file. Required if using -a
-t --temaplate-config Path to temaplate-config file. Required if using -a
-k, --no-verify Disable GPG verification of the source ISO file. By default SHA256SUMS-$today and
SHA256SUMS-$today.gpg in ${script_dir} will be used to verify the authenticity and integrity
of the source ISO file. If they are not present the latest daily SHA256SUMS will be
downloaded and saved in ${script_dir}. The Ubuntu signing key will be downloaded and
saved in a new keyring in ${script_dir}
-o, --no-md5 Disable MD5 checksum on boot
-r, --use-release-iso Use the current release ISO instead of the daily ISO. The file will be used if it already
exists.
-d, --destination Destination ISO file. By default ${script_dir}/ubuntu-autoinstall-$today.iso will be
created, overwriting any existing file.
-s --service-dir-name Bake service-dir-name into the generated ISO. if service-dir-name is not specified, no local application
will be uploaded to complete the ISO build. Path to service-dir-name directory. Required if using -a
-j --job-name Bake job-name into the generated ISO. if job-name is not specified, there will be
no action to change after the service starts. Path to job-name file. Required if using -a
EOF
exit
}
function parse_params() {
# default values of variables set from params
release_name=''
user_data_file=''
meta_data_file=''
destination_iso="${script_dir}/ubuntu-autoinstall-$today.iso"
sha_suffix="${today}"
gpg_verify=1
all_in_one=0
use_hwe_kernel=0
md5_checksum=1
use_release_iso=1
package_name=''
service_dir_name=''
job_name=''
config_data_file=''
temaplate_config_file=''
while :; do
case "${1-}" in
-h | --help) usage ;;
-v | --verbose) set -x ;;
-a | --all-in-one) all_in_one=1 ;;
-e | --use-hwe-kernel) use_hwe_kernel=1 ;;
-o | --no-md5) md5_checksum=0 ;;
-k | --no-verify) gpg_verify=0 ;;
-r | --use-release-iso) use_release_iso=0 ;;
-j | --job-name)
job_name="${2-}"
shift
;;
-s | --service-dir-name)
service_dir_name="${2-}"
shift
;;
-n | --release-name)
release_name="${2-}"
shift
;;
-p | --package-name)
package_name="${2-}"
shift
;;
-c | --config-data)
config_data_file="${2-}"
shift
;;
-t | --temaplate-config)
temaplate_config_file="${2-}"
shift
;;
-u | --user-data)
user_data_file="${2-}"
shift
;;
-d | --destination)
destination_iso="${2-}"
shift
;;
-m | --meta-data)
meta_data_file="${2-}"
shift
;;
-?*) die "Unknown option: $1" ;;
*) break ;;
esac
shift
done
log "👶 Starting up..."
# check required params and arguments
if [ ${all_in_one} -ne 0 ]; then
[[ -z "${user_data_file}" ]] && die "💥 user-data file was not specified."
[[ ! -f "$user_data_file" ]] && die "💥 user-data file could not be found."
[[ -n "${meta_data_file}" ]] && [[ ! -f "$meta_data_file" ]] && die "💥 meta-data file could not be found."
fi
[[ -z "${release_name}" ]] && die "💥 release_name was not specified. eg: focal, jammy, kinetic, noble."
if [ "${use_release_iso}" -eq 1 ]; then
download_url="https://releases.ubuntu.com/${release_name}"
log "🔎 Checking for current release..."
download_iso=$(curl -sSL "${download_url}" | grep -oP "ubuntu-(\d{2}\.04)(\.\d+)?-live-server-amd64\.iso" | head -n 1)
source_iso="${script_dir}/${download_iso}"
current_release=$(echo "${download_iso}" | cut -f2 -d-)
sha_suffix="${current_release}"
log "💿 Current release is ${current_release}"
fi
destination_iso=$(realpath "${destination_iso}")
return 0
}
parse_params "$@"
# do something prep work for generation iso image
function preprocess() {
# The default create a random directory in /tmp
tmpdir=$(mktemp -d)
if [[ ! "$tmpdir" || ! -d "$tmpdir" ]]; then
die "💥 Could not create temporary working directory."
else
log "📁 Created temporary working directory $tmpdir"
fi
log "🔎 Checking for required utilities..."
if [[ ! -x "$(command -v xorriso)" ]]; then
apt-get install xorriso isolinux binutils fakeroot -y
log "👍 xorriso has been installed on ubuntu"
fi
if [[ ! -x "$(command -v 7z)" ]]; then
apt-get install p7zip-full -y
log "👍 7z has been installed on ubuntu"
fi
if [[ ! -x "$(command -v dpkg-scanpackages)" ]]; then
apt-get install dpkg-dev -y
log "👍 dpkg-dev has been installed on ubuntu"
fi
if [[ ! -x "$(command -v aptitude)" ]]; then
apt-get install aptitude -y
log "👍 aptitude has been installed on ubuntu"
fi
if [ ${release_name} == "focal" ]; then
[[ ! -f "/usr/lib/ISOLINUX/isohdpfx.bin" ]] && die "💥 isolinux is not installed. On Ubuntu, install the 'isolinux' package."
fi
[[ ! -x "$(command -v xorriso)" ]] && die "💥 xorriso is not installed. On Ubuntu, install the 'xorriso' package."
[[ ! -x "$(command -v sed)" ]] && die "💥 sed is not installed. On Ubuntu, install the 'sed' package."
[[ ! -x "$(command -v curl)" ]] && die "💥 curl is not installed. On Ubuntu, install the 'curl' package."
[[ ! -x "$(command -v gpg)" ]] && die "💥 gpg is not installed. On Ubuntu, install the 'gpg' package."
[[ ! -x "$(command -v 7z)" ]] && die "💥 7z is not installed. On Ubuntu, install the '7z' package."
[[ ! -x "$(command -v dpkg-scanpackages)" ]] && die "💥 dpkg-dev is not installed. On Ubuntu, install the 'dpkg-dev' package."
[[ ! -x "$(command -v aptitude)" ]] && die "💥 aptitude is not installed. On Ubuntu, install the 'aptitude' package."
log "👍 All required utilities are installed."
}
# download ISO image
function download_image() {
if [ ! -f "${source_iso}" ]; then
log "🌎 Downloading ISO image for Ubuntu ${sha_suffix} Focal Fossa..."
if [ ! -f ${source_iso} ]; then
curl -NsSLO "${download_url}/${download_iso}"
log "👍 Downloaded and saved to ${source_iso}"
else
log "👍 ${source_iso} is exists"
fi
else
log "☑️ Using existing ${source_iso} file."
if [ ${gpg_verify} -eq 1 ]; then
if [ "${source_iso}" != "${script_dir}/${download_iso}" ]; then
log "⚠️ Automatic GPG verification is enabled. If the source ISO file is not the latest daily or release image, verification will fail!"
fi
fi
fi
}
# cheak ISO md5
function iso_gpg_verify() {
if [ ${gpg_verify} -eq 1 ]; then
if [ ! -f "${script_dir}/SHA256SUMS-${sha_suffix}" ]; then
log "🌎 Downloading SHA256SUMS & SHA256SUMS.gpg files..."
curl -NsSL "${download_url}/SHA256SUMS" -o "${script_dir}/SHA256SUMS-${sha_suffix}"
curl -NsSL "${download_url}/SHA256SUMS.gpg" -o "${script_dir}/SHA256SUMS-${sha_suffix}.gpg"
else
log "☑️ Using existing SHA256SUMS-${sha_suffix} & SHA256SUMS-${sha_suffix}.gpg files."
fi
if [ ! -f "${script_dir}/${ubuntu_gpg_key_id}.keyring" ]; then
log "🌎 Downloading and saving Ubuntu signing key..."
gpg -q --no-default-keyring --keyring "${script_dir}/${ubuntu_gpg_key_id}.keyring" --keyserver "hkp://keyserver.ubuntu.com" --recv-keys "${ubuntu_gpg_key_id}"
log "👍 Downloaded and saved to ${script_dir}/${ubuntu_gpg_key_id}.keyring"
else
log "☑️ Using existing Ubuntu signing key saved in ${script_dir}/${ubuntu_gpg_key_id}.keyring"
fi
log "🔐 Verifying ${source_iso} integrity and authenticity..."
gpg -q --keyring "${script_dir}/${ubuntu_gpg_key_id}.keyring" --verify "${script_dir}/SHA256SUMS-${sha_suffix}.gpg" "${script_dir}/SHA256SUMS-${sha_suffix}" 2>/dev/null
if [ $? -ne 0 ]; then
rm -f "${script_dir}/${ubuntu_gpg_key_id}.keyring~"
die "👿 Verification of SHA256SUMS signature failed."
fi
rm -f "${script_dir}/${ubuntu_gpg_key_id}.keyring~"
digest=$(sha256sum "${source_iso}" | cut -f1 -d ' ')
set +e
grep -Fq "$digest" "${script_dir}/SHA256SUMS-${sha_suffix}"
if [ $? -eq 0 ]; then
log "👍 Verification succeeded."
set -e
else
die "👿 Verification of ISO digest failed."
fi
else
log "🤞 Skipping verification of source ISO."
fi
}
function extracte_iso() {
log "🔧 Extracting ISO image..."
if [ ${release_name} == "focal" ]; then
xorriso -osirrox on -indev "${source_iso}" -extract / "$tmpdir" &>/dev/null
rm -rf "$tmpdir/"'[BOOT]'
else
7z -y x ${source_iso} -o"$tmpdir" &>/dev/null
[[ -d "$bootdir" ]] && rm -rf "$bootdir"
mv $tmpdir/'[BOOT]' "${bootdir}"
fi
chmod -R u+w "$tmpdir"
log "👍 Extracted to $tmpdir"
}
function for_hwe_kernel() {
if [ ${use_hwe_kernel} -eq 1 ]; then
if grep -q "hwe-vmlinuz" "$tmpdir/boot/grub/grub.cfg"; then
log "☑️ Destination ISO will use HWE kernel."
sed -i -e 's|/casper/vmlinuz|/casper/hwe-vmlinuz|g' "$tmpdir/boot/grub/grub.cfg"
sed -i -e 's|/casper/initrd|/casper/hwe-initrd|g' "$tmpdir/boot/grub/grub.cfg"
if [ ${release_name} == "focal" ]; then
sed -i -e 's|/casper/vmlinuz|/casper/hwe-vmlinuz|g' "$tmpdir/isolinux/txt.cfg"
sed -i -e 's|/casper/initrd|/casper/hwe-initrd|g' "$tmpdir/isolinux/txt.cfg"
sed -i -e 's|/casper/vmlinuz|/casper/hwe-vmlinuz|g' "$tmpdir/boot/grub/loopback.cfg"
sed -i -e 's|/casper/initrd|/casper/hwe-initrd|g' "$tmpdir/boot/grub/loopback.cfg"
fi
else
log "⚠️ This source ISO does not support the HWE kernel. Proceeding with the regular kernel."
fi
fi
}
function additional_parameter_to_kernel() {
log "🧩 Adding autoinstall parameter to kernel command line..."
grep 'autoinstall' "$tmpdir/boot/grub/grub.cfg" &>/dev/null || sed -i -e 's/---/quiet autoinstall ---/g' "$tmpdir/boot/grub/grub.cfg"
if [ ${release_name} == "focal" ]; then
grep 'autoinstall' "$tmpdir/boot/grub/loopback.cfg" &>/dev/null || sed -i -e 's/---/quiet autoinstall ---/g' "$tmpdir/boot/grub/loopback.cfg"
grep 'autoinstall' "$tmpdir/isolinux/txt.cfg" &>/dev/null || sed -i -e 's/---/quiet autoinstall ---/g' "$tmpdir/isolinux/txt.cfg"
fi
log "👍 Added parameter to UEFI and BIOS kernel command lines."
}
# late-commands insert to user-data
function dynamic_injection_section_command() {
grep "late-commands:" "${user_data_file}" &>/dev/null || sed -i '$a\ late-commands:' "${user_data_file}"
}
# mount cdrom to target
function dynamic_injection_copy_data_to_target() {
dynamic_injection_section_command
grep "/cdrom/mnt /target/" "${user_data_file}" &>/dev/null || sed -i '$a\ - cp -rp /cdrom/mnt /target/' "${user_data_file}"
}
# install pkgs
function dynamic_injection_install_pkgs() {
dynamic_injection_copy_data_to_target
grep "/mnt/script/install-pkgs.sh" "${user_data_file}" &>/dev/null || sed -i '$a\ - curtin in-target --target=/target -- /mnt/script/install-pkgs.sh' "${user_data_file}"
}
# add config file function
function dynamic_injection_config_file() {
dynamic_injection_copy_data_to_target
if [ -n "${config_data_file}" ]; then
dynamic_injection_install_pkgs
fi
grep "/mnt/script/config.sh" "${user_data_file}" &>/dev/null || sed -i '$a\ - curtin in-target --target=/target -- /mnt/script/config.sh' "${user_data_file}"
}
# mount runcmd first boot script file
function dynamic_injection_first_boot_task() {
dynamic_injection_section_command
grep "/cdrom/runcmd-first-boot.sh" "${user_data_file}" &>/dev/null || sed -i '$a\ - cp /cdrom/runcmd-first-boot.sh /target/opt/runcmd-first-boot.sh' "${user_data_file}"
dynamic_injection_user_data_runcmd
}
# perform runcmd script file
function dynamic_injection_user_data_runcmd() {
grep "user-data:" "${user_data_file}" &>/dev/null || sed -i '$a\ user-data:' "${user_data_file}"
grep "runcmd:" "${user_data_file}" &>/dev/null || sed -i '$a\ runcmd:' "${user_data_file}"
grep "/opt/runcmd-first-boot.sh" "${user_data_file}" &>/dev/null || sed -i '$a\ - /opt/runcmd-first-boot.sh' "${user_data_file}"
}
# custom application upload
function dynamic_injection_custom_application() {
dynamic_injection_section_command
fuzzy_app_name=$(find ${service_dir_name} -type f -iname *.service)
exact_app_name=$(echo ${fuzzy_app_name#*/})
sed -i "\$a\ - cp -rp /cdrom/mnt/${service_dir_name} /target/opt" "${user_data_file}"
sed -i "\$a\ - cp /cdrom/mnt/${fuzzy_app_name} /target/lib/systemd/system/${exact_app_name}" "${user_data_file}"
sed -i "\$a\ - curtin in-target --target=/target -- ln -sn /lib/systemd/system/${exact_app_name} /etc/systemd/system/multi-user.target.wants/${exact_app_name}" "${user_data_file}"
}
# Dynamic Injection Subfunction Aggregation as a Key Entry for Dynamic Injection
function dynamic_injection_all() {
# load install packages
if [ -n "${package_name}" ]; then
dynamic_injection_install_pkgs
fi
# Preprocessing configuration file
if [ -n "${config_data_file}" ]; then
dynamic_injection_config_file
fi
# first boot start handler task
if [ -n "${job_name}" ]; then
dynamic_injection_first_boot_task
fi
# local custom application
if [ -n "${service_dir_name}" ]; then
dynamic_injection_custom_application
fi
}
# create user-data-esxi.yml and meta-data, then change grub.cfg file
function all_in_ones() {
if [ ${all_in_one} -eq 1 ]; then
log "🧩 Dynamic injection attach parameters into user-data files."
cp ${user_data_file} ${user_data_file}_${today}
dynamic_injection_all
log "🧩 Adding user-data and meta-data files..."
cp "${user_data_file}" "$tmpdir/user-data"
rm -rf "${user_data_file}"
mv ${user_data_file}_${today} "${user_data_file}"
# todo something, add dynamic inject parameters to user-data file
if [ -n "${meta_data_file}" ]; then
cp "$meta_data_file" "$tmpdir/meta-data"
else
touch "$tmpdir/meta-data"
fi
grep 'cdrom' "$tmpdir/boot/grub/grub.cfg" &>/dev/null || sed -i -e 's,---, ds=nocloud\\\;s=/cdrom/ ---,g' "$tmpdir/boot/grub/grub.cfg"
if [ ${release_name} == "focal" ]; then
grep 'cdrom' "$tmpdir/isolinux/txt.cfg" &>/dev/null || sed -i -e 's,---, ds=nocloud;s=/cdrom/ ---,g' "$tmpdir/isolinux/txt.cfg"
grep 'cdrom' "$tmpdir/boot/grub/loopback.cfg" &>/dev/null || sed -i -e 's,---, ds=nocloud\\\;s=/cdrom/ ---,g' "$tmpdir/boot/grub/loopback.cfg"
fi
log "👍 Added data and configured kernel command line."
# download dependency packages from the internet
if [ -n "${package_name}" ]; then
# Create an mnt directory in the $tmpdir directory for other files
mkdir -p $tmpdir/mnt/{packages,script}
pkgs_destination_dir="$tmpdir/mnt/packages"
exec_script_dir="$tmpdir/mnt/script/"
script_file="install-pkgs.sh"
# customer script is used to install dependency packages
grep -Ev '^#|^$' $package_name >$tmpdir/$package_name
read_file=$(cat $tmpdir/$package_name)
[ -d "${pkgs_destination_dir}" ] || mkdir -p "${pkgs_destination_dir}"
for line in $read_file; do
log "🌎 Downloading and saving packages ${line}"
apt-get download $(
apt-cache depends --recurse --no-recommends --no-suggests --no-conflicts --no-breaks \
--no-replaces --no-enhances --no-pre-depends ${line} | grep -v i386 | grep "^\w"
) &>/dev/null || apt-get download $(
apt-cache depends --recurse --no-recommends --no-suggests --no-conflicts --no-breaks \
--no-replaces --no-enhances --no-pre-depends $(aptitude show ${line} | grep "Provided by" | awk -F ' ' '{print $3}') | grep -v i386 | grep "^\w"
) &>/dev/null
mv ${script_dir}/*.deb ${pkgs_destination_dir}
log "👍 Downloaded and saved the all of packages to ${pkgs_destination_dir}/${line}"
done
# create the index file of the local software sources
cd ${pkgs_destination_dir}
dpkg-scanpackages ./ &>/dev/null | gzip -9c >Packages.gz
apt-ftparchive packages ./ >Packages
cd ${script_dir}
log "👍 Building local dependency packages"
echo '#!/bin/bash' >${script_file}
echo "# The default installation package will be downloaded to /cdrom/mnt/packages/ directory" >>${script_file}
echo "cp /etc/apt/sources.list /etc/apt/sources.list.bak" >>${script_file}
echo 'echo 'deb [trusted=yes] file:///mnt/packages/ ./' > /etc/apt/sources.list' >>${script_file}
echo 'apt-get update' >>${script_file}
for name in $read_file; do
echo "apt-get install -y ${name}" >>${script_file}
done
log "👍 Building local dependency packages to write script automatic execution"
if [ "${script_file##*.}"x = "sh"x ]; then
chmod +x "$script_file"
mv "$script_file" "$exec_script_dir"
else
die "👿 Verification of script file failed."
fi
log "🧩 Adding config-data files..."
if [ -n "$config_data_file" ]; then
chmod +x "$config_data_file"
cp -rp "$config_data_file" "$exec_script_dir/config.sh"
else
echo "👍 No configuration profile available."
fi
log "🧩 Adding template-config files..."
if [ -n "$temaplate_config_file" ]; then
case ${temaplate_config_file##*.} in
xml)
cp -p "$temaplate_config_file" "$tmpdir/mnt/template.xml"
;;
conf | cnf)
cp -p "$temaplate_config_file" "$tmpdir/mnt/template.conf"
;;
yaml | yml)
cp -p "$temaplate_config_file" "$tmpdir/mnt/template.yaml"
;;
ini)
cp -p "$temaplate_config_file" "$tmpdir/mnt/template.ini"
;;
toml)
cp -p "$temaplate_config_file" "$tmpdir/mnt/template.toml"
;;
json)
cp -p "$temaplate_config_file" "$tmpdir/mnt/template.json"
;;
*)
echo "${temaplate_config_file##*.} template profile do not support this format. Usage: xml,conf,yaml,ini,toml,json. "
exit 1
;;
esac
else
log "👍 No template profile available."
fi
rm $tmpdir/$package_name
log "🚽 Deleted temporary file $tmpdir/$package_name."
fi
# After application starting up, todo it
if [ -n "${job_name}" ]; then
cp -p ${job_name} $tmpdir/runcmd-first-boot.sh
log "📁 Moving ${job_name} file to temporary working directory $tmpdir."
fi
# Provide an interface for users to customize the application
if [ -n "${service_dir_name}" ]; then
[[ ! -d ${service_dir_name} ]] && die "👿 ${service_dir_name} is not a legal directory."
variable=${service_dir_name}
cp -rp ${variable%%/} $tmpdir/mnt/
log "📁 Moving ${variable%%/} directory to temporary working directory $tmpdir/mnt/ "
fi
fi
}
function update_md5_for_grub_file() {
# Update the md5 value of the grub.cfg file
if [ ${md5_checksum} -eq 1 ]; then
log "👷 Updating $tmpdir/md5sum.txt with hashes of modified files..."
md5=$(md5sum "$tmpdir/boot/grub/grub.cfg" | cut -f1 -d ' ')
sed -i -e 's,^.*[[:space:]] ./boot/grub/grub.cfg,'"$md5"' ./boot/grub/grub.cfg,' "$tmpdir/md5sum.txt"
if [ ${release_name} == "focal" ]; then
md5=$(md5sum "$tmpdir/boot/grub/loopback.cfg" | cut -f1 -d ' ')
sed -i -e 's,^.*[[:space:]] ./boot/grub/loopback.cfg,'"$md5"' ./boot/grub/loopback.cfg,' "$tmpdir/md5sum.txt"
fi
log "👍 Updated hashes."
else
log "🗑️ Clearing MD5 hashes..."
echo >"$tmpdir/md5sum.txt"
log "👍 Cleared hashes."
fi
}
function repackage_iso_image() {
log "📦 Repackaging extracted files into an ISO image..."
cd "$tmpdir"
# Check if the iso image format is correct
if [ "${destination_iso##*.}"x = "iso"x ]; then
if [ ${release_name} == "focal" ]; then
xorriso -as mkisofs -r \
-V "ubuntu-autoinstall-${release_name}" \
-J -b isolinux/isolinux.bin \
-c isolinux/boot.cat \
-no-emul-boot \
-boot-load-size 4 \
-isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin \
-boot-info-table \
-input-charset utf-8 \
-eltorito-alt-boot \
-e boot/grub/efi.img \
-no-emul-boot \
-isohybrid-gpt-basdat \
-o "${destination_iso}" \
. &>/dev/null
else
xorriso -as mkisofs -r \
-V "ubuntu-autoinstall-${release_name}" \
-o "${destination_iso}" \
--grub2-mbr ../BOOT/1-Boot-NoEmul.img \
-partition_offset 16 \
--mbr-force-bootable \
-append_partition 2 28732ac11ff8d211ba4b00a0c93ec93b ../BOOT/2-Boot-NoEmul.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 \
. &>/dev/null
fi
else
die "👿 Verification of iso image format is failed."
fi
cd "$OLDPWD"
log "👍 Repackaged into ${destination_iso}"
die "✅ Completed." 0
}
function main() {
log
preprocess
download_image
iso_gpg_verify
extracte_iso
for_hwe_kernel
additional_parameter_to_kernel
all_in_ones
update_md5_for_grub_file
repackage_iso_image
cleanup
}
main