Merge remote-tracking branch 'origin/github/pr/148'
[grml-live.git] / etc / grml / fai / config / hooks / instsoft.ZFS
1 #!/bin/bash
2 # Filename:      ${GRML_FAI_CONFIG}/hooks/instsoft.ZFS
3 # Purpose:       Build zfs modules in the chroot, then get rid of packages installed for this alone
4 # Authors:       (c) AndrĂ¡s Korn <korn-grml@elan.rulez.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2, or, at your option, any later version.
7 ################################################################################
8
9 set -u
10 set -e
11
12 # We don't want to install build-essential, dkms et al via package_config
13 # because they will end up bloating the iso; it seems cleaner to install
14 # them, build the zfs modules, then remove them.
15 #
16 # TODO: if https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1009179 is ever
17 # fixed, switch to building a zfs-modules deb and including that.
18 #
19 # TODO: convert this into a framework for other classes to ship dkms
20 # modules: have a hook/script that installs the build-related packages,
21 # another that builds whatever must be built, and a third that does the cleanup.
22 #
23 # TODO: support other architectures grml and grml-live supports (PRs
24 # welcome, I'm sure).
25
26 echo "$0: Installing latest kernel and its headers, as well as build-essential."
27 # For some reason, apt's autoremove function doesn't pick up some of the
28 # extra packages we install here, e.g. gcc-11, so work around that by
29 # keeping track of what gets installed. This is an ugly hack and should not
30 # be needed, but without it the resulting ISO is hundreds of megabytes
31 # larger. I hope this kludge can go away eventually.
32 extra_packages=($($ROOTCMD apt-get --assume-no --download-only --mark-auto -u install \
33         build-essential linux-image-amd64 linux-headers-amd64 \
34         | sed '0,/The following NEW packages will be installed/d;/^[^ ]/,$d'))
35 $ROOTCMD apt-get --yes --mark-auto -u install build-essential linux-image-amd64 linux-headers-amd64
36
37 # Remove all but the latest kernel (TODO: support passing in the desired
38 # kernel version by configuration variable instead of using the latest):
39 echo "$0: Removing all kernel packages except the latest one, if any."
40 for kernelversion in $($ROOTCMD sh -c 'cd /boot; ls -rt vmlinuz-*' | sed 's/^vmlinuz-//;$d'); do
41         echo "$0: Removing obsolete kernel version $kernelversion"
42         $ROOTCMD apt-get --yes --purge remove "linux-.*$kernelversion.*"
43 done
44
45 # Earlier Debian releases have a dwarves package; newer ones have pahole.
46 # This can be needed to build the zfs modules, perhaps depending on kernel
47 # configuration (TODO: look into this).
48 echo "$0: Installing pahole or dwarves, whichever is available."
49 if $ROOTCMD apt-get --yes --mark-auto -u install pahole; then
50         pahole=pahole
51 else
52         $ROOTCMD apt-get --mark-auto --yes -u install dwarves
53         pahole=dwarves
54 fi
55
56 echo "$0: Installing zfs-dkms itself."
57 extra_packages=(${extra_packages[@]} $($ROOTCMD apt-get --assume-no --download-only --mark-auto -u install zfs-dkms | sed '0,/The following NEW packages will be installed/d;/^[^ ]/,$d'))
58 $ROOTCMD apt-get --yes --mark-auto -u install zfs-dkms
59
60 # Now invoke the dkms kernel postinst script for the only kernel that's left
61 # -- normally the zfs-dkms postinst script should do this, but maybe it
62 # didn't, and redoing it is almost free:
63 kernelversion=$($ROOTCMD sh -c 'ls -1 /boot/vmlinuz-*' | sed 's@.*boot/vmlinuz-@@')
64 echo "$0: Building zfs-dkms modules for kernel $kernelversion."
65 $ROOTCMD /etc/kernel/postinst.d/dkms "$kernelversion"
66
67 tempfile=$(mktemp)
68 echo "$0: Saving built modules into a backup file (removing the dkms package will remove them, but we'll put them back)."
69 $ROOTCMD tar cf - /lib/modules/$kernelversion/updates/dkms >$tempfile
70
71 echo "$0: Removing packages only needed to build zfs modules."
72 remove_packages=($(echo "${extra_packages[@]}" zfs-dkms '^linux-headers-.*' build-essential $pahole | tr ' ' '\n' | sort -u))
73 $ROOTCMD apt-get --yes --purge --autoremove remove ${remove_packages[@]}
74 echo "$0: Trying extra hard to get rid of auto-installed packages. This is a hack that is one of the ways we're trying to work around a perceived bug in apt autoremove and should be a no-op."
75 $ROOTCMD apt-get --yes --purge autoremove
76
77 echo "$0: Restoring backed-up kernel modules."
78 $ROOTCMD tar xf - <$tempfile
79 rm $tempfile
80 $ROOTCMD depmod -a $kernelversion
81 echo "$0: Completed successfully. Enjoy your zfs."