aboutsummaryrefslogtreecommitdiffstats
path: root/preseed.sh
blob: c1226d162acb7030333a74c5d78c56c05a18cbe7 (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
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
#!/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"

# Generate a (hybrid) ISO or an HDD image?  The latter should only be
# useful for older BIOSes that don't support hybrid ISOs.
case "$newiso" in
    *.iso) type=iso;;
    *.tar) type=hdd; archive=;;
    *.tar.gz|*.tgz) type=hdd; archive=z;;
    *.tar.bz|*.tar.bz2|*.tbz|*.tbz2) type=hdd; archive=j;;
    *.tar.xz|*.txz) type=hdd; archive=J;;
    *) echo "Error: $newiso: Unknown file type" >&2; exit 1;;
esac

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"
[ -f $isoeditdir/boot/grub/grub.cfg ] && cfg="$cfg $isoeditdir/boot/grub/grub.cfg"
sed -ri 's@^\s+(append|linux)\s(.*\s)?auto=true\s(.*\s)?priority=critical\b@& preseed/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 -L ./include \( \! -name '.*' -a -type f \) -print0 | \
    xargs -r0 md5sum >> "$isoeditdir/md5sum.txt"

# non-free firmwares:
# wget -O - http://cdimage.debian.org/cdimage/unofficial/non-free/firmware/wheezy/current/firmware.tar.gz | tar -C ./firmware -xz
#rsync -aL ./firmware/ "$isoeditdir/firmware"/
#find ./firmware \( \! -name '.*' -a -type f \) -print0 | \
#    xargs -r0 md5sum >> "$isoeditdir/md5sum.txt"

dist=$(readlink "$isoeditdir/dists/stable")
packages="dists/$dist/main/debian-installer/binary-$DEB_ARCH/Packages"
gunzip "$isoeditdir/$packages.gz"
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

gzip "$isoeditdir/$packages"

md5sums=$(mktemp)
cd "$isoeditdir"
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 [ $type = hdd ]; then
    # Generate a HDD image
    mv ./isolinux                      ./syslinux
    mv ./syslinux/isolinux.cfg         ./syslinux/syslinux.cfg
    mv ./syslinux/isolinux.bin         ./syslinux/syslinux.bin
    sed -i 's/\bisolinux\b/syslinux/g' ./syslinux/*.cfg
    # Dereference links, which are not compatible with FAT devices
    tar --exclude='./debian' --hard-dereference --dereference \
        -c ${archive:+-$archive} \
        -f "$newiso" ./

    ## To burn the archive onto a USB stick:
    # device=/dev/disk/by-id/...    # FIXME
    # mountpoint=/mnt/usbkey        # FIXME
    #
    # device=$(readlink -f "$device")
    # parted $device mklabel msdos
    # parted $device mkpart primary 1 1G
    # parted $device set 1 boot on
    # mkdosfs ${device}1
    #
    # install-mbr $device
    # syslinux -i ${device}1
    #
    # mount ${device}1 $mountpoint
    # tar -x -C "$mountpoint" -f "$image" --no-same-owner
    # umount "$mountpoint"

elif [ $type = iso ]; then
    # Generate a hybrid ISO image
    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

    # /usr/lib/ISOLINUX/isohdpfx.bin is in the 'isolinux' package
    xorriso -as mkisofs -r \
        -checksum_algorithm_iso all \
        -isohybrid-mbr /usr/lib/ISOLINUX/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" \
        ./

    # To burn the ISO onto a USB stick, see
    # http://www.debian.org/CD/faq/#write-usb
fi

rm -rf "$isoeditdir"