]> git.ipfire.org Git - thirdparty/dracut.git/blob - modules.d/40network/netroot
TEST-30-ISCSI/test.sh: factor out client runs
[thirdparty/dracut.git] / modules.d / 40network / netroot
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 type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
7
8 # Huh? Empty $1?
9 [ -z "$1" ] && exit 1
10
11 # Huh? No interface config?
12 [ ! -e /tmp/net.$1.up ] && exit 1
13
14 # [ ! -z $2 ] means this is for manually bringing up network
15 # instead of real netroot; If It's called without $2, then there's
16 # no sense in doing something if no (net)root info is available
17 # or root is already there
18 [ -e /tmp/root.info ] || exit 1
19 . /tmp/root.info
20 if [ -z "$2" ]; then
21 [ -d $NEWROOT/proc ] && exit 0
22 [ -z "$netroot" ] && exit 1
23 fi
24
25 # Let's see if we have to wait for other interfaces
26 # Note: exit works just fine, since the last interface to be
27 # online'd should see all files
28 [ -e "/tmp/net.ifaces" ] && read IFACES < /tmp/net.ifaces
29 for iface in $IFACES ; do
30 [ -e /tmp/net.$iface.up ] || exit 1
31 done
32
33 # Set or override primary interface
34 netif=$1
35 [ -e "/tmp/net.bootdev" ] && read netif < /tmp/net.bootdev
36
37 if [ -e /tmp/net.$netif.manualup ]; then
38 rm -f /tmp/net.$netif.manualup
39 fi
40
41 # Figure out the handler for root=dhcp by recalling all netroot cmdline
42 # handlers when this is not called from manually network bringing up.
43 if [ -z "$2" ]; then
44 if [ "$netroot" = "dhcp" ] || [ "$netroot" = "dhcp6" ] ; then
45 # Unset root so we can check later
46 unset root
47
48 # Load dhcp options
49 [ -e /tmp/dhclient.$netif.dhcpopts ] && . /tmp/dhclient.$netif.dhcpopts
50
51 # If we have a specific bootdev with no dhcpoptions or empty root-path,
52 # we die. Otherwise we just warn
53 if [ -z "$new_root_path" ] ; then
54 [ -n "$BOOTDEV" ] && die "No dhcp root-path received for '$BOOTDEV'"
55 warn "No dhcp root-path received for '$BOOTDEV' trying other interfaces if available"
56 exit 1
57 fi
58
59 # Set netroot to new_root_path, so cmdline parsers don't call
60 netroot=$new_root_path
61
62 # FIXME!
63 for f in $hookdir/cmdline/90*.sh; do
64 [ -f "$f" ] && . "$f";
65 done
66 else
67 rootok="1"
68 fi
69
70 # Check: do we really know how to handle (net)root?
71 [ -z "$root" ] && die "No or empty root= argument"
72 [ -z "$rootok" ] && die "Don't know how to handle 'root=$root'"
73
74 handler=${netroot%%:*}
75 handler=${handler%%4}
76 handler=$(command -v ${handler}root)
77 if [ -z "$netroot" ] || [ ! -e "$handler" ] ; then
78 die "No handler for netroot type '$netroot'"
79 fi
80 fi
81
82 # We're here, so we can assume that upping interfaces is now ok
83 [ -z "$IFACES" ] && IFACES="$netif"
84 for iface in $IFACES ; do
85 . /tmp/net.$iface.up
86 done
87
88 [ -e /tmp/net.$netif.gw ] && . /tmp/net.$netif.gw
89 [ -e /tmp/net.$netif.hostname ] && . /tmp/net.$netif.hostname
90 [ -e /tmp/net.$netif.resolv.conf ] && cp -f /tmp/net.$netif.resolv.conf /etc/resolv.conf
91
92 # Load interface options
93 [ -e /tmp/net.$netif.override ] && . /tmp/net.$netif.override
94 [ -e /tmp/dhclient.$netif.dhcpopts ] && . /tmp/dhclient.$netif.dhcpopts
95
96 # Handle STP Timeout: arping the default router if root server is
97 # unknown or not local, or if not available the root server.
98 # Note: This assumes that if no router is present the
99 # root server is on the same subnet.
100 #
101 # TODO There's some netroot variants that don't (yet) have their
102 # server-ip netroot
103
104 # Get router IP if set
105 [ -n "$new_routers" ] && gw_ip=${new_routers%%,*}
106 [ -n "$gw" ] && gw_ip=$gw
107 # Get root server IP if set
108 if [ -n "$netroot" ]; then
109 dummy=${netroot#*:}
110 dummy=${dummy%%:*}
111 case "$dummy" in
112 [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*) netroot_ip=$dummy;;
113 esac
114 fi
115 # Default arping dest to router
116 dest="$gw_ip"
117 # Change to arping root server if appropriate
118 if [ -n "$netroot_ip" ]; then
119 if [ -z "$dest" ]; then
120 # no gateway so check root server
121 dest="$netroot_ip"
122 else
123 r=$(ip route get "$netroot_ip")
124 if ! strstr "$r" ' via ' ; then
125 # local root server, so don't arping gateway
126 dest="$netroot_ip"
127 fi
128 fi
129 fi
130 if [ -n "$dest" ] && ! arping -q -f -w 60 -I $netif $dest ; then
131 dinfo "Resolving $dest via ARP on $netif failed"
132 fi
133
134 # exit in case manually bring up network
135 [ -n "$2" ] && exit 0
136
137 # Source netroot hooks before we start the handler
138 source_all $hookdir/netroot
139
140 # Run the handler; don't store the root, it may change from device to device
141 # XXX other variables to export?
142 if $handler $netif $netroot $NEWROOT; then
143 # Network rootfs mount successful
144 for iface in $IFACES ; do
145 [ -f /tmp/dhclient.$iface.lease ] && cp /tmp/dhclient.$iface.lease /tmp/net.$iface.lease
146 [ -f /tmp/dhclient.$iface.dhcpopts ] && cp /tmp/dhclient.$iface.dhcpopts /tmp/net.$iface.dhcpopts
147 done
148
149 # Save used netif for later use
150 [ ! -f /tmp/net.ifaces ] && echo $netif > /tmp/net.ifaces
151 else
152 warn "Mounting root via '$netif' failed"
153 # If we're trying with multiple interfaces, put that one down.
154 # ip down/flush ensures that routeing info goes away as well
155 if [ -z "$BOOTDEV" ] ; then
156 ip link set $netif down
157 ip addr flush dev $netif
158 echo "#empty" > /etc/resolv.conf
159 fi
160 fi
161 exit 0