]> git.ipfire.org Git - thirdparty/dracut.git/blob - modules.d/95nbd/nbdroot.sh
fix(nbd): shellcheck regression
[thirdparty/dracut.git] / modules.d / 95nbd / nbdroot.sh
1 #!/bin/sh
2
3 type getarg > /dev/null 2>&1 || . /lib/dracut-lib.sh
4
5 PATH=/usr/sbin:/usr/bin:/sbin:/bin
6
7 # Huh? Empty $1?
8 [ -z "$1" ] && exit 1
9
10 # Huh? Empty $2?
11 [ -z "$2" ] && exit 1
12
13 # Huh? Empty $3?
14 [ -z "$3" ] && exit 1
15
16 # root is in the form root=nbd:srv:port[:fstype[:rootflags[:nbdopts]]]
17 # shellcheck disable=SC2034
18 netif="$1"
19 nroot="$2"
20 NEWROOT="$3"
21
22 # If it's not nbd we don't continue
23 [ "${nroot%%:*}" = "nbd" ] || return
24
25 nroot=${nroot#nbd:}
26 nbdserver=${nroot%%:*}
27 if [ "${nbdserver%"${nbdserver#?}"}" = "[" ]; then
28 nbdserver=${nroot#[}
29 nbdserver=${nbdserver%%]:*}
30 nroot=${nroot#*]:}
31 else
32 nroot=${nroot#*:}
33 fi
34 nbdport=${nroot%%:*}
35 nroot=${nroot#*:}
36 nbdfstype=${nroot%%:*}
37 nroot=${nroot#*:}
38 nbdflags=${nroot%%:*}
39 nbdopts=${nroot#*:}
40
41 if [ "$nbdopts" = "$nbdflags" ]; then
42 unset nbdopts
43 fi
44 if [ "$nbdflags" = "$nbdfstype" ]; then
45 unset nbdflags
46 fi
47 if [ "$nbdfstype" = "$nbdport" ]; then
48 unset nbdfstype
49 fi
50 if [ -z "$nbdfstype" ]; then
51 nbdfstype=auto
52 fi
53
54 # look through the NBD options and pull out the ones that need to
55 # go before the host etc. Append a ',' so we know we terminate the loop
56 nbdopts=${nbdopts},
57 while [ -n "$nbdopts" ]; do
58 f=${nbdopts%%,*}
59 nbdopts=${nbdopts#*,}
60 if [ -z "$f" ]; then
61 break
62 fi
63 if [ -z "${f%bs=*}" -o -z "${f%timeout=*}" ]; then
64 preopts="$preopts $f"
65 continue
66 fi
67 opts="$opts $f"
68 done
69
70 # look through the flags and see if any are overridden by the command line
71 nbdflags=${nbdflags},
72 while [ -n "$nbdflags" ]; do
73 f=${nbdflags%%,*}
74 nbdflags=${nbdflags#*,}
75 if [ -z "$f" ]; then
76 break
77 fi
78 if [ "$f" = "ro" -o "$f" = "rw" ]; then
79 nbdrw=$f
80 continue
81 fi
82 fsopts=${fsopts:+$fsopts,}$f
83 done
84
85 getarg ro && nbdrw=ro
86 getarg rw && nbdrw=rw
87 fsopts=${fsopts:+$fsopts,}${nbdrw}
88
89 # XXX better way to wait for the device to be made?
90 i=0
91 while [ ! -b /dev/nbd0 ]; do
92 [ $i -ge 20 ] && exit 1
93 if [ "$UDEVVERSION" -ge 143 ]; then
94 udevadm settle --exit-if-exists=/dev/nbd0
95 else
96 sleep 0.1
97 fi
98 i=$((i + 1))
99 done
100
101 # If we didn't get a root= on the command line, then we need to
102 # add the udev rules for mounting the nbd0 device
103 if [ "$root" = "block:/dev/root" -o "$root" = "dhcp" ]; then
104 printf 'KERNEL=="nbd0", ENV{DEVTYPE}!="partition", ENV{ID_FS_TYPE}=="?*", SYMLINK+="root"\n' > /etc/udev/rules.d/99-nbd-root.rules
105 udevadm control --reload
106 wait_for_dev -n /dev/root
107
108 if [ -z "$DRACUT_SYSTEMD" ]; then
109 type write_fs_tab > /dev/null 2>&1 || . /lib/fs-lib.sh
110
111 write_fs_tab /dev/root "$nbdfstype" "$fsopts"
112
113 printf '/bin/mount %s\n' \
114 "$NEWROOT" \
115 > "$hookdir"/mount/01-$$-nbd.sh
116 fi
117 # if we're on systemd, use the nbd-generator script
118 # to create the /sysroot mount.
119 fi
120
121 # supported since nbd 3.8 via 77e97612
122 if strstr "$(nbd-client --help 2>&1)" "systemd-mark"; then
123 preopts="-systemd-mark $preopts"
124 fi
125
126 if [ "$nbdport" -gt 0 ] 2> /dev/null; then
127 nbdport="$nbdport"
128 else
129 nbdport="-name $nbdport"
130 fi
131
132 if ! nbd-client -check /dev/nbd0 > /dev/null; then
133 # shellcheck disable=SC2086
134 nbd-client "$nbdserver" $nbdport /dev/nbd0 $preopts $opts || exit 1
135 fi
136
137 # NBD doesn't emit uevents when it gets connected, so kick it
138 echo change > /sys/block/nbd0/uevent
139 udevadm settle
140 need_shutdown
141 exit 0