18e1300d93b3de2a88713586aaa752f4519ac5f1
[platform/upstream/dracut.git] / modules.d / 95nfs / nfsroot
1 #!/bin/sh
2
3 . /lib/dracut-lib
4
5 PATH=$PATH:/sbin:/usr/sbin
6
7 # XXX needs error handling like ifup/dhclient-script
8
9 getarg rdnetdebug && {
10     exec > /tmp/nfsroot.$1.$$.out
11     exec 2>> /tmp/nfsroot.$1.$$.out
12     set -x
13 }
14
15 # root is in the form root=nfs[4]:server:path:[options]
16 netif="$1"
17 root="$2"
18
19 nfsver=${root%%:*}; root=${root#*:}
20 nfsserver=${root%%:*}; root=${root#*:}
21 nfspath=${root%%:*}
22 flags=${root#*:}
23
24 # look through the flags and see if any are overridden by the command line
25 # Append a , so we know we terminate
26 flags=${flags},
27 while [ -n "$flags" ]; do
28     f=${flags%%,*}
29     flags=${flags#*,}
30     if [ -z "$f" ]; then
31         break
32     fi
33     if [ "$f" = "ro" -o "$f" = "rw" ]; then
34         nfsrw=$f
35         continue
36     fi
37     if [ "$f" = "lock" -o "$f" = "nolock" ]; then
38         nfslock=$f
39         continue
40     fi
41     nfsflags=${nfsflags+$nfsflags,}$f
42 done
43
44 getarg ro && nfsrw=ro
45 getarg rw && nfsrw=rw
46 nfsflags=${nfsflags+$nfsflags,}${nfsrw}
47
48 # Load the modules so the filesystem type is there
49 modprobe nfs || exit 1
50
51 # XXX don't forget to move /var/lib/nfs/rpc_pipefs to new /
52
53 # Start rpcbind and rpc.statd as mount won't let us use locks on a NFSv4
54 # filesystem without talking to them, even though they are unneeded
55 # XXX occasionally saw 'rpcbind: fork failed: No such device' -- why?
56 [ -n "$(pidof rpcbind)" ] || rpcbind
57 [ -n "$(pidof rpc.statd)" ] || rpc.statd
58
59 # XXX should I do rpc.idmapd here, or wait and start in the new root
60 # XXX waiting assumes root can read everything it needs right up until
61 # XXX we start it...
62
63 # XXX really, want to retry in a loop I think, but not here...
64
65 if [ "$nfsver" = "nfs4" ]; then
66     # XXX really needed? Do we need non-root users before we start it in
67     # XXX the real root image?
68     if [ -z "$(pidof rpc.idmapd)" ]; then
69         rpc.idmapd
70     fi
71
72     # NFSv4 does locks internally
73     exec mount -t nfs4 -o${nfsflags}${nfslock+,$nfslock} \
74                         $nfsserver:$nfspath $NEWROOT
75 fi
76
77 # NFSv{2,3} doesn't support using locks as it requires a helper to transfer
78 # the rpcbind state to the new root
79 #
80 [ -z "$nfslock" -o "$nfslock" = "lock" ] &&
81     echo "Locks unsupported on NFSv{2,3}, using nolock" 1>&2
82 exec mount -t nfs -onolock,$nfsflags $nfsserver:$nfspath $NEWROOT