network: fix ifup and netroot calling
[platform/upstream/dracut.git] / modules.d / 40network / netroot.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 PATH=/usr/sbin:/usr/bin:/sbin:/bin
6 command -v getarg >/dev/null    || . /lib/dracut-lib.sh
7 command -v setup_net >/dev/null || . /lib/net-lib.sh
8
9 # Huh? Empty $1?
10 [ -z "$1" ] && exit 1
11
12 # [ ! -z $2 ] means this is for manually bringing up network
13 # instead of real netroot; If It's called without $2, then there's
14 # no sense in doing something if no (net)root info is available
15 # or root is already there
16 [ -d $NEWROOT/proc ] && exit 0
17 [ -z "$netroot" ] && exit 1
18
19 # Set or override primary interface
20 netif=$1
21 [ -e "/tmp/net.bootdev" ] && read netif < /tmp/net.bootdev
22
23 # Figure out the handler for root=dhcp by recalling all netroot cmdline
24 # handlers when this is not called from manually network bringing up.
25 if [ -z "$2" ]; then
26     if [ "$netroot" = "dhcp" ] || [ "$netroot" = "dhcp6" ] ; then
27         # Unset root so we can check later
28         unset root
29
30         # Load dhcp options
31         [ -e /tmp/dhclient.$netif.dhcpopts ] && . /tmp/dhclient.$netif.dhcpopts
32
33         # If we have a specific bootdev with no dhcpoptions or empty root-path,
34         # we die. Otherwise we just warn
35         if [ -z "$new_root_path" ] ; then
36             [ -n "$BOOTDEV" ] && die "No dhcp root-path received for '$BOOTDEV'"
37             warn "No dhcp root-path received for '$BOOTDEV' trying other interfaces if available"
38             exit 1
39         fi
40
41         # Set netroot to new_root_path, so cmdline parsers don't call
42         netroot=$new_root_path
43
44         # FIXME!
45         for f in $hookdir/cmdline/90*.sh; do
46             [ -f "$f" ] && . "$f";
47         done
48     else
49         rootok="1"
50     fi
51
52     # Check: do we really know how to handle (net)root?
53     [ -z "$root" ] && die "No or empty root= argument"
54     [ -z "$rootok" ] && die "Don't know how to handle 'root=$root'"
55
56     handler=${netroot%%:*}
57     handler=${handler%%4}
58     handler=$(command -v ${handler}root)
59     if [ -z "$netroot" ] || [ ! -e "$handler" ] ; then
60         die "No handler for netroot type '$netroot'"
61     fi
62 fi
63
64 # Source netroot hooks before we start the handler
65 source_hook netroot $netif
66
67 # Run the handler; don't store the root, it may change from device to device
68 # XXX other variables to export?
69 if $handler $netif $netroot $NEWROOT; then
70     # Network rootfs mount successful - save interface info for ifcfg etc.
71     save_netinfo $netif
72 fi
73 exit 0