network: don't use "ifup -m"
[dracut.git] / modules.d / 95nbd / nbdroot.sh
blobdd2bb559d6aeff9414a30667a81e5365ee3aeb42
1 #!/bin/sh
3 type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
5 PATH=/usr/sbin:/usr/bin:/sbin:/bin
7 # Huh? Empty $1?
8 [ -z "$1" ] && exit 1
10 # Huh? Empty $2?
11 [ -z "$2" ] && exit 1
13 # Huh? Empty $3?
14 [ -z "$3" ] && exit 1
16 # root is in the form root=nbd:srv:port[:fstype[:rootflags[:nbdopts]]]
17 netif="$1"
18 nroot="$2"
19 NEWROOT="$3"
21 # If it's not nbd we don't continue
22 [ "${nroot%%:*}" = "nbd" ] || return
24 nroot=${nroot#nbd:}
25 nbdserver=${nroot%%:*}; nroot=${nroot#*:}
26 nbdport=${nroot%%:*}; nroot=${nroot#*:}
27 nbdfstype=${nroot%%:*}; nroot=${nroot#*:}
28 nbdflags=${nroot%%:*}
29 nbdopts=${nroot#*:}
31 # If nbdport not an integer, then assume name based import
32 if [ ! -z $(echo "$nbdport" | sed 's/[0-9]//g') ]; then
33 nbdport="-N $nbdport"
36 if [ "$nbdopts" = "$nbdflags" ]; then
37 unset nbdopts
39 if [ "$nbdflags" = "$nbdfstype" ]; then
40 unset nbdflags
42 if [ "$nbdfstype" = "$nbdport" ]; then
43 unset nbdfstype
45 if [ -z "$nbdfstype" ]; then
46 nbdfstype=auto
49 # look through the NBD options and pull out the ones that need to
50 # go before the host etc. Append a ',' so we know we terminate the loop
51 nbdopts=${nbdopts},
52 while [ -n "$nbdopts" ]; do
53 f=${nbdopts%%,*}
54 nbdopts=${nbdopts#*,}
55 if [ -z "$f" ]; then
56 break
58 if [ -z "${f%bs=*}" -o -z "${f%timeout=*}" ]; then
59 preopts="$preopts $f"
60 continue
62 opts="$opts $f"
63 done
65 # look through the flags and see if any are overridden by the command line
66 nbdflags=${nbdflags},
67 while [ -n "$nbdflags" ]; do
68 f=${nbdflags%%,*}
69 nbdflags=${nbdflags#*,}
70 if [ -z "$f" ]; then
71 break
73 if [ "$f" = "ro" -o "$f" = "rw" ]; then
74 nbdrw=$f
75 continue
77 fsopts=${fsopts:+$fsopts,}$f
78 done
80 getarg ro && nbdrw=ro
81 getarg rw && nbdrw=rw
82 fsopts=${fsopts:+$fsopts,}${nbdrw}
84 # XXX better way to wait for the device to be made?
85 i=0
86 while [ ! -b /dev/nbd0 ]; do
87 [ $i -ge 20 ] && exit 1
88 if [ $UDEVVERSION -ge 143 ]; then
89 udevadm settle --exit-if-exists=/dev/nbd0
90 else
91 sleep 0.1
93 i=$(($i + 1))
94 done
96 # If we didn't get a root= on the command line, then we need to
97 # add the udev rules for mounting the nbd0 device
98 if [ "$root" = "block:/dev/root" -o "$root" = "dhcp" ]; then
99 printf 'KERNEL=="nbd0", ENV{DEVTYPE}!="partition", ENV{ID_FS_TYPE}=="?*", SYMLINK+="root"\n' >> /etc/udev/rules.d/99-nbd-root.rules
100 udevadm control --reload
101 type write_fs_tab >/dev/null 2>&1 || . /lib/fs-lib.sh
102 write_fs_tab /dev/root "$nbdfstype" "$fsopts"
103 wait_for_dev -n /dev/root
105 if [ -z "$DRACUT_SYSTEMD" ]; then
106 printf '/bin/mount %s\n' \
107 "$NEWROOT" \
108 > $hookdir/mount/01-$$-nbd.sh
112 if strstr "$(nbd-client --help 2>&1)" "systemd-mark"; then
113 preopts="--systemd-mark $preopts"
116 nbd-client $preopts "$nbdserver" $nbdport /dev/nbd0 $opts || exit 1
118 # NBD doesn't emit uevents when it gets connected, so kick it
119 echo change > /sys/block/nbd0/uevent
120 udevadm settle
121 need_shutdown
122 exit 0