dracut-systemd/dracut-initqueue: only start service if really needed
[dracut.git] / modules.d / 95nfs / module-setup.sh
blob9b5b8c8f022a6dfe62cc0a2ad525c754df1b06c9
1 #!/bin/bash
3 # called by dracut
4 check() {
5 # If our prerequisites are not met, fail anyways.
6 require_any_binary rpcbind portmap || return 1
7 require_binaries rpc.statd mount.nfs mount.nfs4 umount || return 1
9 [[ $hostonly ]] || [[ $mount_needs ]] && {
10 for fs in "${host_fs_types[@]}"; do
11 [[ "$fs" == "nfs" ]] && return 0
12 [[ "$fs" == "nfs3" ]] && return 0
13 [[ "$fs" == "nfs4" ]] && return 0
14 done
15 return 255
17 return 0
20 # called by dracut
21 depends() {
22 # We depend on network modules being loaded
23 echo network
26 # called by dracut
27 installkernel() {
28 instmods nfs sunrpc ipv6 nfsv2 nfsv3 nfsv4 nfs_acl nfs_layout_nfsv41_files
31 cmdline() {
32 local nfs_device
33 local nfs_options
34 local nfs_root
35 local nfs_address
36 local lookup
37 local ifname
39 ### nfsroot= ###
40 nfs_device=$(findmnt -t nfs4 -n -o SOURCE /)
41 if [ -n "$nfs_device" ];then
42 nfs_root="root=nfs4:$nfs_device"
43 else
44 nfs_device=$(findmnt -t nfs -n -o SOURCE /)
45 [ -z "$nfs_device" ] && return
46 nfs_root="root=nfs:$nfs_device"
48 nfs_options=$(findmnt -t nfs4,nfs -n -o OPTIONS /)
49 [ -n "$nfs_options" ] && nfs_root="$nfs_root:$nfs_options"
50 echo "$nfs_root"
52 ### ip= ###
53 if [[ $nfs_device = [0-9]*\.[0-9]*\.[0-9]*.[0-9]* ]] || [[ $nfs_device = \[.*\] ]]; then
54 nfs_address="$nfs_device"
55 else
56 lookup=$(host $(echo ${nfs_device%%:*})| head -n1)
57 nfs_address=${lookup##* }
59 ifname=$(ip -o route get to $nfs_address | sed -n 's/.*dev \([^ ]*\).*/\1/p')
60 if [ -e /sys/class/net/$ifname/address ] ; then
61 ifmac=$(cat /sys/class/net/$ifname/address)
62 printf 'ifname=%s:%s ' ${ifname} ${ifmac}
65 printf 'ip=%s:static\n' ${ifname}
68 # called by dracut
69 install() {
70 local _i
71 local _nsslibs
72 inst_multiple -o portmap rpcbind rpc.statd mount.nfs \
73 mount.nfs4 umount rpc.idmapd sed /etc/netconfig
74 inst_multiple /etc/services /etc/nsswitch.conf /etc/rpc /etc/protocols /etc/idmapd.conf
76 if [[ $hostonly_cmdline == "yes" ]]; then
77 local _netconf="$(cmdline)"
78 [[ $_netconf ]] && printf "%s\n" "$_netconf" >> "${initdir}/etc/cmdline.d/95nfs.conf"
81 if [ -f /lib/modprobe.d/nfs.conf ]; then
82 inst_multiple /lib/modprobe.d/nfs.conf
83 else
84 [ -d $initdir/etc/modprobe.d/ ] || mkdir $initdir/etc/modprobe.d
85 echo "alias nfs4 nfs" > $initdir/etc/modprobe.d/nfs.conf
88 inst_libdir_file 'libnfsidmap_nsswitch.so*' 'libnfsidmap/*.so' 'libnfsidmap*.so*'
90 _nsslibs=$(sed -e '/^#/d' -e 's/^.*://' -e 's/\[NOTFOUND=return\]//' /etc/nsswitch.conf \
91 | tr -s '[:space:]' '\n' | sort -u | tr -s '[:space:]' '|')
92 _nsslibs=${_nsslibs#|}
93 _nsslibs=${_nsslibs%|}
95 inst_libdir_file -n "$_nsslibs" 'libnss_*.so*'
97 inst_hook cmdline 90 "$moddir/parse-nfsroot.sh"
98 inst_hook pre-udev 99 "$moddir/nfs-start-rpc.sh"
99 inst_hook cleanup 99 "$moddir/nfsroot-cleanup.sh"
100 inst "$moddir/nfsroot.sh" "/sbin/nfsroot"
101 inst "$moddir/nfs-lib.sh" "/lib/nfs-lib.sh"
102 mkdir -m 0755 -p "$initdir/var/lib/nfs/rpc_pipefs"
103 mkdir -m 0755 -p "$initdir/var/lib/rpcbind"
104 mkdir -m 0755 -p "$initdir/var/lib/nfs/statd/sm"
106 # Rather than copy the passwd file in, just set a user for rpcbind
107 # We'll save the state and restart the daemon from the root anyway
108 egrep '^nfsnobody:|^rpc:|^rpcuser:' /etc/passwd >> "$initdir/etc/passwd"
109 egrep '^nogroup:|^rpc:|^nobody:' /etc/group >> "$initdir/etc/group"
111 # rpc user needs to be able to write to this directory to save the warmstart
112 # file
113 chmod 770 "$initdir/var/lib/rpcbind"
114 egrep -q '^rpc:' /etc/passwd \
115 && egrep -q '^rpc:' /etc/group \
116 && chown rpc.rpc "$initdir/var/lib/rpcbind"
117 dracut_need_initqueue