Last active
August 20, 2017 10:30
-
-
Save danielkza/14d2f72f5c388fb4eea73e08a88433a7 to your computer and use it in GitHub Desktop.
ZFS root systemd generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
generator_dir="$1" | |
list_datasets() { | |
/usr/sbin/zfs list -r -H -o name,canmount,mountpoint "$1" | |
} | |
get_root_zfs_dataset() { | |
while read mnt_dev mnt_path mnt_type rest; do | |
if [ "$mnt_path" = "/" ] && [ "$mnt_type" = "zfs" ]; then | |
echo "$mnt_dev" | |
return 0 | |
fi | |
done < /proc/self/mounts | |
return 1 | |
} | |
root_dataset=$(get_root_zfs_dataset) | |
if [ $? -ne 0 ] || [ -z "$root_dataset" ]; then | |
exit 0 | |
fi | |
echo "zfs-root-fs-generator: found root dataset $root_dataset" >> /dev/kmsg | |
list_datasets "${root_dataset%%/*}" | { | |
while IFS=$(printf '\t') read dataset canmount mountpoint; do | |
if ! [ "$canmount" = on ] || [ "$mountpoint" = legacy ] || [ "$mountpoint" = none ]; then | |
continue | |
fi | |
unit_name=$(systemd-escape -p "$mountpoint") | |
echo "zfs-root-fs-generator: adding $dataset -> ${unit_name}.mount" >> /dev/kmsg | |
cat > "${generator_dir}/${unit_name}.mount" <<EOF | |
# Automatically generated by zfs-root-fs-generator | |
[Unit] | |
After=zfs-import-scan.service | |
After=zfs-import-cache.service | |
Before=local-fs.target | |
[Mount] | |
What=$dataset | |
Where=$mountpoint | |
Type=zfs | |
Options=zfsutil | |
EOF | |
mkdir -p "$generator_dir/local-fs.target.requires" | |
(cd "$generator_dir/local-fs.target.requires"; ln -s "../${unit_name}.mount" "${unit_name}.mount") | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment