aboutsummaryrefslogtreecommitdiffstats
path: root/preseed.sh
blob: ff885351085f1f4c7d1ab4abd44a58ea03515e8b (plain)
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
#!/bin/sh
#
# Usage: preseed.sh preseeded.iso source.iso [udebs]
#
# Credits: https://wiki.debian.org/DebianInstaller/Preseed/EditIso
#          https://wiki.debian.org/DebianInstaller/Modify/CD
#
# Install with 'Advanced options > Automated install'

set -ue

newiso=$(readlink -f "$1")
[ -e "$newiso" ] && rm -f "$newiso"

iso="$2"
test -r "$iso" || exit 1
shift; shift
pwd=$(pwd)

for prog in fuseiso fusermount rsync md5sum xorriso find; do
    if ! which "$prog" >/dev/null; then
        echo "Error: Missing $prog" >&2
        exit 1
    fi
done

mountdir=$(mktemp -d)
fuseiso "$iso" "$mountdir"

isoeditdir=$(mktemp -d)
rsync -aH --exclude=TRANS.TBL --chmod=u+w "$mountdir"/ "$isoeditdir"/

fusermount -u "$mountdir"
rmdir "$mountdir"

cp preseed.cfg "$isoeditdir/"
md5sum ./preseed.cfg >> "$isoeditdir/md5sum.txt"

cfg="$isoeditdir/isolinux/*.cfg $isoeditdir/isolinux/*/*.cfg"
[ -d $isoeditdir/boot -a -d $isoeditdir/boot/grub ] && cfg="$cfg $isoeditdir/boot/grub/grub.cfg"
sed -ri "s@auto=true\s+priority=critical\b@& file=/cdrom/preseed.cfg @" $cfg
# ^ no need to update isolinux' checksum, as it's not in './md5sum.txt'.

mkdir "$isoeditdir/include"
rsync -aL ./include/ "$isoeditdir/include"/
find ./include \( \! -name '.*' -a -type f \) -print0 | \
    xargs -r0 md5sum >> "$isoeditdir/md5sum.txt"

# XXX: that's ugly
dist=$(for x in "$isoeditdir"/dists/*; do [ -h "$x" ] && continue; [ -d "$x" ] && echo ${x##*/} && break; done)
arch=$(for x in "$isoeditdir/dists/$dist/main"/binary-*; do test -d "$x" && echo ${x##*/binary-} && break; done)
packages="dists/$dist/main/debian-installer/binary-$arch/Packages"
while [ $# -gt 0 ]; do
    cd "${1%%_*}-udeb"; shift
    name="$(sed -n 's/Package:\s\s*//p' ./debian/control)"
    udeb=$(cut -d' ' -f1 ./debian/files)
    dir="pool/main/$(expr substr "$name" 1 1)/$name"
    mkdir -p "$isoeditdir/$dir/"
    cp ../"$udeb" "$isoeditdir/$dir/"

    cat "./debian/$name/DEBIAN/control"                    >> "$isoeditdir/$packages"
    echo "Filename: $dir/$udeb"                            >> "$isoeditdir/$packages"
    echo "MD5sum: $(md5sum    ../"$udeb" | cut -d' ' -f1)" >> "$isoeditdir/$packages"
    echo "SHA1:   $(sha1sum   ../"$udeb" | cut -d' ' -f1)" >> "$isoeditdir/$packages"
    echo "SHA256: $(sha256sum ../"$udeb" | cut -d' ' -f1)" >> "$isoeditdir/$packages"
    echo                                                   >> "$isoeditdir/$packages"

    cd "$isoeditdir"
    md5sum "./$dir/$udeb" >> "$isoeditdir/md5sum.txt"
    cd "$pwd"
done

cd "$isoeditdir"
gzip -fk ./"$packages"

md5sums=$(mktemp)
while read sum file; do
    if [ "$file" = "./${packages}" -o \
         "$file" = "./${packages}.gz" -o \
         "$file" = "./boot/grub/grub.cfg" ]; then
         md5sum "$file"
     else
         echo "$sum $file"
     fi
done < ./md5sum.txt > "$md5sums"
mv "$md5sums" "$isoeditdir/md5sum.txt"

if [ -f ./boot/grub/efi.img ]; then
    efi_opts="--efi-boot boot/grub/efi.img -append_partition 2 0x01 ./boot/grub/efi.img"
else
    efi_opts=
fi
xorriso -as mkisofs -r \
    -checksum_algorithm_iso all \
    -isohybrid-mbr /usr/lib/syslinux/isohdpfx.bin \
    -b isolinux/isolinux.bin -c isolinux/boot.cat \
    -partition_offset 16 \
    -no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot $efi_opts \
    -o "$newiso" \
    ./

rm -rf "$isoeditdir"