type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
PATH=/usr/sbin:/usr/bin:/sbin:/bin
# root is in the form root=nbd:srv:port[:fstype[:rootflags[:nbdopts]]]
# If it's not nbd we don't continue
[ "${nroot%%:*}" = "nbd" ] || return
nbdserver=${nroot%%:*}; nroot=${nroot#*:}
nbdport=${nroot%%:*}; nroot=${nroot#*:}
nbdfstype=${nroot%%:*}; nroot=${nroot#*:}
if [ "$nbdopts" = "$nbdflags" ]; then
if [ "$nbdflags" = "$nbdfstype" ]; then
if [ "$nbdfstype" = "$nbdport" ]; then
if [ -z "$nbdfstype" ]; then
# look through the NBD options and pull out the ones that need to
# go before the host etc. Append a ',' so we know we terminate the loop
while [ -n "$nbdopts" ]; do
if [ -z "${f%bs=*}" -o -z "${f%timeout=*}" ]; then
# look through the flags and see if any are overridden by the command line
while [ -n "$nbdflags" ]; do
if [ "$f" = "ro" -o "$f" = "rw" ]; then
fsopts=${fsopts:+$fsopts,}$f
fsopts=${fsopts:+$fsopts,}${nbdrw}
# XXX better way to wait for the device to be made?
while [ ! -b /dev/nbd0 ]; do
if [ $UDEVVERSION -ge 143 ]; then
udevadm settle --exit-if-exists=/dev/nbd0
# If we didn't get a root= on the command line, then we need to
# add the udev rules for mounting the nbd0 device
if [ "$root" = "block:/dev/root" -o "$root" = "dhcp" ]; then
printf 'KERNEL=="nbd0", ENV{DEVTYPE}!="partition", ENV{ID_FS_TYPE}=="?*", SYMLINK+="root"\n' >> /etc/udev/rules.d/99-nbd-root.rules
type write_fs_tab >/dev/null 2>&1 || . /lib/fs-lib.sh
write_fs_tab /dev/root "$nbdfstype" "$fsopts"
wait_for_dev -n /dev/root
if [ -z "$DRACUT_SYSTEMD" ]; then
printf '/bin/mount %s\n' \
> $hookdir/mount/01-$$-nbd.sh
if strstr "$(nbd-client --help 2>&1)" "systemd-mark"; then
preopts="--systemd-mark $preopts"
if [ "$nbdport" -gt 0 ] 2>/dev/null; then
if [ -z "$DRACUT_SYSTEMD" ]; then
nbd-client "$nbdserver" $nbdport /dev/nbd0 $preopts $opts || exit 1
systemd-run --no-block --service-type=forking --quiet \
--description="nbd nbd0" \
-p 'DefaultDependencies=no' \
--unit="nbd0" -- nbd-client "$nbdserver" $nbdport /dev/nbd0 $preopts $opts >/dev/null 2>&1 || exit 1
if [ -z "$DRACUT_SYSTEMD" ]; then
nbd-client -name "$nbdport" "$nbdserver" /dev/nbd0 $preopts $opts || exit 1
systemd-run --no-block --service-type=forking --quiet \
--description="nbd nbd0" \
-p 'DefaultDependencies=no' \
--unit="nbd0" -- nbd-client -name "$nbdport" "$nbdserver" /dev/nbd0 $preopts $opts >/dev/null 2>&1 || exit 1
# NBD doesn't emit uevents when it gets connected, so kick it
echo change > /sys/block/nbd0/uevent