1#!/bin/sh
2#
3# Add OpenZFS filesystem capabilities to an initrd, usually for a native ZFS root.
4#
5
6if [ "$1" = "prereqs" ]; then
7	echo "udev"
8	exit
9fi
10
11. /usr/share/initramfs-tools/hook-functions
12
13for req in "@sbindir@/zpool" "@sbindir@/zfs" "@mounthelperdir@/mount.zfs"; do
14	copy_exec "$req" || {
15		echo "$req not available!" >&2
16		exit 2
17	}
18done
19
20copy_exec "@udevdir@/vdev_id"
21copy_exec "@udevdir@/zvol_id"
22if command -v systemd-ask-password > /dev/null; then
23	copy_exec "$(command -v systemd-ask-password)"
24fi
25
26# We use pthreads, but i-t from buster doesn't automatically
27# copy this indirect dependency: this can be removed when buster finally dies.
28find /lib/ -type f -name "libgcc_s.so.[1-9]" | while read -r libgcc; do
29	copy_exec "$libgcc"
30done
31
32# shellcheck disable=SC2050
33if [ @LIBFETCH_DYNAMIC@ -gt 0 ]; then
34	find /lib/ -name "@LIBFETCH_SONAME@" | while read -r libfetch; do
35		copy_exec "$libfetch"
36	done
37fi
38
39copy_file config "/etc/hostid"
40copy_file cache  "@sysconfdir@/zfs/zpool.cache"
41copy_file config "@initconfdir@/zfs"
42copy_file config "@sysconfdir@/zfs/zfs-functions"
43copy_file config "@sysconfdir@/zfs/vdev_id.conf"
44for f in "@sysconfdir@/zfs/initramfs-tools-load-key" "@sysconfdir@/zfs/initramfs-tools-load-key.d/"*; do
45	copy_file config "$f"
46done
47copy_file rule   "@udevruledir@/60-zvol.rules"
48copy_file rule   "@udevruledir@/69-vdev.rules"
49
50manual_add_modules zfs
51
52if [ -f "/etc/hostname" ]; then
53	copy_file config "/etc/hostname"
54else
55	hostname="$(mktemp -t hostname.XXXXXXXXXX)"
56	hostname > "$hostname"
57	copy_file config "$hostname" "/etc/hostname"
58	rm -f "$hostname"
59fi
60