]> git.ipfire.org Git - thirdparty/dracut.git/blame - modules.d/95nbd/nbdroot.sh
Renamed all shell scripts to *.sh
[thirdparty/dracut.git] / modules.d / 95nbd / nbdroot.sh
CommitLineData
2b117123 1#!/bin/sh
cc02093d
HH
2# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3# ex: ts=8 sw=4 sts=4 et filetype=sh
2b117123 4
c9f1e3d1 5type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
2b117123 6
fb59f4c9 7PATH=/usr/sbin:/usr/bin:/sbin:/bin
2b117123 8
268de90f
PS
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
50e7ff76 18# root is in the form root=nbd:srv:port[:fstype[:rootflags[:nbdopts]]]
2b117123
DD
19netif="$1"
20root="$2"
9cdfd735 21NEWROOT="$3"
2b117123 22
50e7ff76
PS
23# If it's not nbd we don't continue
24[ "${root%%:*}" = "nbd" ] || return
25
2b117123
DD
26root=${root#nbd:}
27nbdserver=${root%%:*}; root=${root#*:}
28nbdport=${root%%:*}; root=${root#*:}
29nbdfstype=${root%%:*}; root=${root#*:}
30nbdflags=${root%%:*}
31nbdopts=${root#*:}
32
33if [ "$nbdopts" = "$nbdflags" ]; then
34 unset nbdopts
35fi
36if [ "$nbdflags" = "$nbdfstype" ]; then
37 unset nbdflags
38fi
39if [ "$nbdfstype" = "$nbdport" ]; then
40 unset nbdfstype
41fi
42if [ -z "$nbdfstype" ]; then
43 nbdfstype=auto
44fi
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
48nbdopts=${nbdopts},
49while [ -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"
60done
61
62# look through the flags and see if any are overridden by the command line
63nbdflags=${nbdflags},
64while [ -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
75done
76
77getarg ro && nbdrw=ro
78getarg rw && nbdrw=rw
79fsopts=${fsopts+$fsopts,}${nbdrw}
80
2b117123
DD
81# XXX better way to wait for the device to be made?
82i=0
83while [ ! -b /dev/nbd0 ]; do
84 [ $i -ge 20 ] && exit 1
cf476dbc
HH
85 if [ $UDEVVERSION -ge 143 ]; then
86 udevadm settle --exit-if-exists=/dev/nbd0
87 else
88 sleep 0.1
89 fi
2b117123
DD
90 i=$(( $i + 1))
91done
92
00c5ab3e
HH
93nbd-client $preopts "$nbdserver" "$nbdport" /dev/nbd0 $opts || exit 1
94
8bd5873f
DD
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
cee3b896
HH
97root=$(getarg root=)
98if [ -z "$root" ] || strstr "$root" "nbd:" || strstr "$root" "dhcp"; then
d84c1df9 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;' \
0b53ca70 100 > $hookdir/initqueue/settled/nbd.sh
5a8d8dfb 101
97190241 102 printf '/bin/mount -t %s -o %s %s %s\n' \
cc02093d 103 "$nbdfstype" "$fsopts" /dev/nbd0 "$NEWROOT" \
0b53ca70 104 > $hookdir/mount/01-$$-nbd.sh
8bd5873f 105fi
2b117123 106
8bd5873f
DD
107# NBD doesn't emit uevents when it gets connected, so kick it
108echo change > /sys/block/nbd0/uevent
cee3b896 109udevadm settle
fb67e4aa 110need_shutdown
2b117123 111exit 0