Skip to content

Commit 5236eaf

Browse files
i-t: rewrite hooks
This produces a leaner image, doesn't fail if zdb doesn't exist, properly handles hostnameless systems, doesn't mention crypto modules for no reason, doesn't add useless empty executable in hopes an eight-year-old PR is merged, uses i-t builtins for all copies Also optimize the checkbashisms filter to spawn one (or a few) awks instead of one per regular file and remove initramfs/hooks therefrom due to a command -v false positive Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Ahelenia Ziemiańska <[email protected]> Closes #12017
1 parent fce29d6 commit 5236eaf

File tree

3 files changed

+43
-111
lines changed

3 files changed

+43
-111
lines changed

Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,10 @@ checkbashisms:
159159
-o -name 'paxcheck.sh' -prune \
160160
-o -name 'make_gitrev.sh' -prune \
161161
-o -name '90zfs' -prune \
162+
-o -path '*initramfs/hooks' -prune \
162163
-o -type f ! -name 'config*' \
163164
! -name 'libtool' \
164-
-exec sh -c 'awk "NR==1 && /#!.*bin\/sh.*/ {print FILENAME;}" "{}"' \;); \
165+
-exec awk 'FNR==1 && /^#!.*bin\/sh/ {print FILENAME}' {} \+); \
165166
else \
166167
echo "skipping checkbashisms because checkbashisms is not installed"; \
167168
fi

contrib/initramfs/hooks/zfs.in

Lines changed: 37 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -3,115 +3,54 @@
33
# Add OpenZFS filesystem capabilities to an initrd, usually for a native ZFS root.
44
#
55

6-
# This hook installs udev rules for OpenZFS.
7-
PREREQ="udev"
8-
9-
# These prerequisites are provided by the zfsutils package. The zdb utility is
10-
# not strictly required, but it can be useful at the initramfs recovery prompt.
11-
COPY_EXEC_LIST="@sbindir@/zdb @sbindir@/zpool @sbindir@/zfs"
12-
COPY_EXEC_LIST="$COPY_EXEC_LIST @mounthelperdir@/mount.zfs @udevdir@/vdev_id"
13-
COPY_EXEC_LIST="$COPY_EXEC_LIST @udevdir@/zvol_id"
14-
COPY_FILE_LIST="/etc/hostid @sysconfdir@/zfs/zpool.cache"
15-
COPY_FILE_LIST="$COPY_FILE_LIST @initconfdir@/zfs"
16-
COPY_FILE_LIST="$COPY_FILE_LIST @sysconfdir@/zfs/zfs-functions"
17-
COPY_FILE_LIST="$COPY_FILE_LIST @sysconfdir@/zfs/vdev_id.conf"
18-
COPY_FILE_LIST="$COPY_FILE_LIST @udevruledir@/60-zvol.rules"
19-
COPY_FILE_LIST="$COPY_FILE_LIST @udevruledir@/69-vdev.rules"
20-
21-
# These prerequisites are provided by the base system.
22-
COPY_EXEC_LIST="$COPY_EXEC_LIST /usr/bin/dirname /bin/hostname /sbin/blkid"
23-
COPY_EXEC_LIST="$COPY_EXEC_LIST /usr/bin/env"
24-
COPY_EXEC_LIST="$COPY_EXEC_LIST $(which systemd-ask-password)"
25-
26-
# Explicitly specify all kernel modules because automatic dependency resolution
27-
# is unreliable on many systems.
28-
BASE_MODULES="zlib_deflate spl zavl zcommon znvpair zunicode zlua zfs icp"
29-
CRPT_MODULES="sun-ccm sun-gcm sun-ctr"
30-
MANUAL_ADD_MODULES_LIST="$BASE_MODULES"
31-
32-
# Generic result code.
33-
RC=0
6+
if [ "$1" = "prereqs" ]; then
7+
echo "udev"
8+
exit
9+
fi
3410

35-
case $1 in
36-
prereqs)
37-
echo "$PREREQ"
38-
exit 0
39-
;;
40-
esac
11+
. /usr/share/initramfs-tools/hook-functions
4112

42-
for ii in $COPY_EXEC_LIST
43-
do
44-
if [ ! -x "$ii" ]
45-
then
46-
echo "Error: $ii is not executable."
47-
RC=2
48-
fi
13+
for req in "@sbindir@/zpool" "@sbindir@/zfs" "@mounthelperdir@/mount.zfs"; do
14+
copy_exec "$req" || {
15+
echo "$req not available!" >&2
16+
exit 2
17+
}
4918
done
5019

51-
if [ "$RC" -ne 0 ]
52-
then
53-
exit "$RC"
20+
copy_exec "@sbindir@/zdb"
21+
copy_exec "@udevdir@/vdev_id"
22+
copy_exec "@udevdir@/zvol_id"
23+
if command -v systemd-ask-password > /dev/null; then
24+
copy_exec "$(command -v systemd-ask-password)"
5425
fi
5526

56-
. /usr/share/initramfs-tools/hook-functions
57-
58-
mkdir -p "$DESTDIR/etc/"
59-
60-
# ZDB uses pthreads for some functions, but the library dependency is not
61-
# automatically detected. The `find` utility and extended `cp` options are
62-
# used here because libgcc_s.so could be in a subdirectory of /lib for
63-
# multi-arch installations.
64-
cp --target-directory="$DESTDIR" --parents $(find /lib/ -type f -name libgcc_s.so.1)
27+
# We use pthreads, but i-t from buster doesn't automatically
28+
# copy this indirect dependency: this can be removed when buster finally dies.
29+
for libgcc in $(find /lib/ -type f -name libgcc_s.so.[1-9]); do
30+
copy_exec "$libgcc"
31+
done
6532

66-
if [ @LIBFETCH_DYNAMIC@ != 0 ]
67-
then
68-
for l in $(find /lib/ -name @LIBFETCH_SONAME@)
69-
do
70-
copy_exec "$l"
33+
if [ @LIBFETCH_DYNAMIC@ != 0 ]; then
34+
for libfetch in $(find /lib/ -name @LIBFETCH_SONAME@); do
35+
copy_exec "$libfetch"
7136
done
7237
fi
7338

74-
for ii in $COPY_EXEC_LIST
75-
do
76-
copy_exec "$ii"
77-
done
39+
copy_file config "/etc/hostid"
40+
copy_file cache "@sysconfdir@/zfs/zpool.cache"
41+
copy_file config "@initconfdir@/zfs"
42+
copy_file config "@sysconfdir@/zfs/zfs-functions"
43+
copy_file config "@sysconfdir@/zfs/vdev_id.conf"
44+
copy_file rule "@udevruledir@/60-zvol.rules"
45+
copy_file rule "@udevruledir@/69-vdev.rules"
7846

79-
for ii in $COPY_FILE_LIST
80-
do
81-
dir=$(dirname "$ii")
82-
[ -d "$dir" ] && mkdir -p "$DESTDIR/$dir"
83-
[ -f "$ii" ] && cp -p "$ii" "$DESTDIR/$ii"
84-
done
47+
manual_add_modules zfs
8548

86-
for ii in $MANUAL_ADD_MODULES_LIST
87-
do
88-
manual_add_modules "$ii"
89-
done
90-
91-
if [ -f "/etc/hostname" ]
92-
then
93-
cp -p "/etc/hostname" "$DESTDIR/etc/"
49+
if [ -f "/etc/hostname" ]; then
50+
copy_file config "/etc/hostname"
9451
else
95-
hostname >"$DESTDIR/etc/hostname"
52+
hostname="$(mktemp -t hostname.XXXXXXXXXX)"
53+
hostname > "$hostname"
54+
copy_file config "$hostname" "/etc/hostname"
55+
rm -f "$hostname"
9656
fi
97-
98-
for ii in zfs zfs.conf spl spl.conf
99-
do
100-
if [ -f "/etc/modprobe.d/$ii" ]; then
101-
if [ ! -d "$DESTDIR/etc/modprobe.d" ]; then
102-
mkdir -p $DESTDIR/etc/modprobe.d
103-
fi
104-
cp -p "/etc/modprobe.d/$ii" $DESTDIR/etc/modprobe.d/
105-
fi
106-
done
107-
108-
# With pull request #1476 (not yet merged) comes a verbose warning
109-
# if /usr/bin/net doesn't exist or isn't executable. Just create
110-
# a dummy...
111-
[ ! -d "$DESTDIR/usr/bin" ] && mkdir -p "$DESTDIR/usr/bin"
112-
if [ ! -x "$DESTDIR/usr/bin/net" ]; then
113-
touch "$DESTDIR/usr/bin/net"
114-
chmod +x "$DESTDIR/usr/bin/net"
115-
fi
116-
117-
exit 0

contrib/initramfs/hooks/zfsunlock.in

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
#!/bin/sh
22

3-
PREREQ="dropbear"
4-
5-
prereqs() {
6-
echo "$PREREQ"
7-
}
8-
9-
case "$1" in
10-
prereqs)
11-
prereqs
12-
exit 0
13-
;;
14-
esac
3+
if [ "$1" = "prereqs" ]; then
4+
echo "dropbear"
5+
exit
6+
fi
157

168
. /usr/share/initramfs-tools/hook-functions
179

0 commit comments

Comments
 (0)