1#!/bin/sh
2
3BASEDIR=$(dirname $(realpath "$0"))
4
5orig_iso=mini.iso
6auto_extract_efi=1
7efi_img=efi.img
8
9new_files=iso_unpacked_and_modified
10new_iso=preseed-mini.iso
11
12PRESEEDFILE=$(realpath "${BASEDIR}"/preseed/preseed.cfg)
13AFTERINSTALLSCRIPT=$(realpath "${BASEDIR}"/preseed/after_install_1.sh)
14
15part_img_ready=1
16if test "$auto_extract_efi" = 1; then
17  start_block=$(/sbin/fdisk -l "$orig_iso" | fgrep "$orig_iso"2 | \
18                awk '{print $2}')
19  block_count=$(/sbin/fdisk -l "$orig_iso" | fgrep "$orig_iso"2 | \
20                awk '{print $4}')
21  if test "$start_block" -gt 0 -a "$block_count" -gt 0 2>/dev/null
22  then
23    dd if="$orig_iso" bs=512 skip="$start_block" count="$block_count" \
24       of="$efi_img"
25  else
26    echo "Cannot read plausible start block and block count from fdisk" >&2
27    part_img_ready=0
28  fi
29fi
30
31# add preseed
32mkdir ${new_files}
33bsdtar -C ${new_files} -xf "$orig_iso"
34cd ${new_files}
35cp -f "${PRESEEDFILE}" preseed.cfg
36cp -f "${AFTERINSTALLSCRIPT}" after_install_1.sh
37chmod a+rx after_install_1.sh
38
39# add preseed to console based installer
40chmod ug+w initrd.gz
41gzip -d -f initrd.gz
42echo preseed.cfg | cpio -H newc -o -A -F initrd
43echo after_install_1.sh | cpio -H newc -o -A -F initrd
44gzip -9 initrd
45chmod a-w initrd.gz
46# add preseed to GTK based installer
47chmod ug+w gtk
48cd gtk
49chmod ug+w initrd.gz
50gzip -d -f initrd.gz
51cp -f ../preseed.cfg .
52cp -f ../after_install_1.sh .
53echo preseed.cfg | cpio -H newc -o -A -F initrd
54echo after_install_1.sh | cpio -H newc -o -A -F initrd
55gzip -9 initrd
56chmod a-w initrd.gz
57rm -f preseed.cfg after_install_1.sh
58cd ..
59chmod a-w gtk
60# modify Graphical installer to use tty1
61chmod ug+w boot
62chmod ug+w boot/grub
63chmod ug+w boot/grub/grub.cfg
64sed -i '0,/menuentry/{s#menuentry#menuentry '\''Ampere Install'\'' {\n    set background_color=black\n    linux    /linux --- quiet console=tty1\n    initrd   /gtk/initrd.gz\n}\nmenuentry#}' boot/grub/grub.cfg
65sed -i '0,/insmod gzio/{s#insmod gzio#set timeout=120\n\ninsmod gzio#}' boot/grub/grub.cfg
66chmod a-w boot/grub/grub.cfg
67chmod a-w boot/grub
68chmod a-w boot
69cd ..
70
71rm -f "${new_iso}"
72
73# Create the new ISO image if not partition extraction failed
74test "$part_img_ready" = 1 && \
75xorriso -as mkisofs \
76   -r -V 'Debian arm64 n' \
77   -o "$new_iso" \
78   -J -joliet-long -cache-inodes \
79   -e boot/grub/efi.img \
80   -no-emul-boot \
81   -append_partition 2 0xef "$efi_img" \
82   -partition_cyl_align all \
83   "$new_files"
84
85# clean
86rm -f efi.img
87chmod ug+w -R "${new_files}"
88rm -rf "${new_files}"
89