TEST-30-ISCSI/test.sh: factor out client runs
[platform/upstream/dracut.git] / modules.d / 95nbd / nbdroot.sh
1 #!/bin/sh
2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
4
5 type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
6
7 PATH=/usr/sbin:/usr/bin:/sbin:/bin
8
9 # Huh? Empty $1?
10 [ -z "$1" ] && exit 1
11
12 # Huh? Empty $2?
13 [ -z "$2" ] && exit 1
14
15 # Huh? Empty $3?
16 [ -z "$3" ] && exit 1
17
18 # root is in the form root=nbd:srv:port[:fstype[:rootflags[:nbdopts]]]
19 netif="$1"
20 root="$2"
21 NEWROOT="$3"
22
23 # If it's not nbd we don't continue
24 [ "${root%%:*}" = "nbd" ] || return
25
26 root=${root#nbd:}
27 nbdserver=${root%%:*}; root=${root#*:}
28 nbdport=${root%%:*}; root=${root#*:}
29 nbdfstype=${root%%:*}; root=${root#*:}
30 nbdflags=${root%%:*}
31 nbdopts=${root#*:}
32
33 if [ "$nbdopts" = "$nbdflags" ]; then
34     unset nbdopts
35 fi
36 if [ "$nbdflags" = "$nbdfstype" ]; then
37     unset nbdflags
38 fi
39 if [ "$nbdfstype" = "$nbdport" ]; then
40     unset nbdfstype
41 fi
42 if [ -z "$nbdfstype" ]; then
43     nbdfstype=auto
44 fi
45
46 # look through the NBD options and pull out the ones that need to
47 # go before the host etc. Append a ',' so we know we terminate the loop
48 nbdopts=${nbdopts},
49 while [ -n "$nbdopts" ]; do
50     f=${nbdopts%%,*}
51     nbdopts=${nbdopts#*,}
52     if [ -z "$f" ]; then
53         break
54     fi
55     if [ -z "${f%bs=*}" -o -z "${f%timeout=*}" ]; then
56         preopts="$preopts $f"
57         continue
58     fi
59     opts="$opts $f"
60 done
61
62 # look through the flags and see if any are overridden by the command line
63 nbdflags=${nbdflags},
64 while [ -n "$nbdflags" ]; do
65     f=${nbdflags%%,*}
66     nbdflags=${nbdflags#*,}
67     if [ -z "$f" ]; then
68         break
69     fi
70     if [ "$f" = "ro" -o "$f" = "rw" ]; then
71         nbdrw=$f
72         continue
73     fi
74     fsopts=${fsopts+$fsopts,}$f
75 done
76
77 getarg ro && nbdrw=ro
78 getarg rw && nbdrw=rw
79 fsopts=${fsopts+$fsopts,}${nbdrw}
80
81 # XXX better way to wait for the device to be made?
82 i=0
83 while [ ! -b /dev/nbd0 ]; do
84     [ $i -ge 20 ] && exit 1
85     if [ $UDEVVERSION -ge 143 ]; then
86         udevadm settle --exit-if-exists=/dev/nbd0
87     else
88         sleep 0.1
89     fi
90     i=$(( $i + 1))
91 done
92
93 nbd-client $preopts "$nbdserver" "$nbdport" /dev/nbd0 $opts || exit 1
94
95 # If we didn't get a root= on the command line, then we need to
96 # add the udev rules for mounting the nbd0 device
97 root=$(getarg root=)
98 if [ -z "$root" ] || strstr "$root" "nbd:" || strstr "$root" "dhcp"; then
99     echo '[ -e /dev/root ] || { info=$(udevadm info --query=env --name=/dev/nbd0); [ -z "${info%%*ID_FS_TYPE*}" ] && { ln -s /dev/nbd0 /dev/root 2>/dev/null; :; };} && rm $job;' \
100         > $hookdir/initqueue/settled/nbd.sh
101
102     printf '/bin/mount -t %s -o %s %s %s\n' \
103         "$nbdfstype" "$fsopts" /dev/nbd0 "$NEWROOT" \
104         > $hookdir/mount/01-$$-nbd.sh
105 fi
106
107 # NBD doesn't emit uevents when it gets connected, so kick it
108 echo change > /sys/block/nbd0/uevent
109 udevadm settle
110 need_shutdown
111 exit 0