b3d55848edea12143db6fc7697f3358cdb8bdc87
[platform/upstream/dracut.git] / modules.d / 40network / net-genrules.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 # pxelinux provides macaddr '-' separated, but we need ':'
6 fix_bootif() {
7     local macaddr=${1}
8     local IFS='-'
9     macaddr=$(for i in ${macaddr} ; do echo -n $i:; done)
10     macaddr=${macaddr%:}
11     # strip hardware type field from pxelinux
12     [ -n "${macaddr%??:??:??:??:??:??}" ] && macaddr=${macaddr#??:}
13     echo $macaddr
14 }
15
16 # Don't continue if we don't need network
17 [ -z "$netroot" ] && ! getargbool 0 rd.neednet && return;
18
19 # Write udev rules
20 {
21     # bridge: attempt only the defined interface
22     if [ -e /tmp/bridge.info ]; then
23         . /tmp/bridge.info
24         IFACES=$ethname
25     fi
26
27     # bond: attempt only the defined interface (override bridge defines)
28     if [ -e /tmp/bond.info ]; then
29         . /tmp/bond.info
30         # It is enough to fire up only one
31         IFACES=${bondslaves%% *}
32     fi
33
34     ifup='/sbin/ifup $env{INTERFACE}'
35     [ -z "$netroot" ] && ifup="$ifup -m"
36
37     # BOOTIF says everything, use only that one
38     BOOTIF=$(getarg 'BOOTIF=')
39     if [ -n "$BOOTIF" ] ; then
40         BOOTIF=$(fix_bootif "$BOOTIF")
41         printf 'ACTION=="add", SUBSYSTEM=="net", ATTR{address}=="%s", RUN+="%s"\n' "$BOOTIF" "/sbin/initqueue --onetime $ifup"
42
43     # If we have to handle multiple interfaces, handle only them.
44     elif [ -n "$IFACES" ] ; then
45         for iface in $IFACES ; do
46             printf 'SUBSYSTEM=="net", ENV{INTERFACE}=="%s", RUN+="%s"\n' "$iface" "/sbin/initqueue --onetime $ifup"
47         done
48
49     # Default: We don't know the interface to use, handle all
50     else
51         printf 'SUBSYSTEM=="net", RUN+="%s"\n' "/sbin/initqueue --onetime $ifup"
52     fi
53
54     # Run the "online" hook
55     printf 'SUBSYSTEM=="net", ACTION=="online", RUN+="/sbin/initqueue --onetime --env netif=$env{INTERFACE} source_hook initqueue/online"\n'
56     # And make sure we run setup_net at the start of the hook
57     echo '. /lib/net-lib.sh; setup_net $netif' > \
58             $hookdir/initqueue/online/05-setup_net.sh
59
60 } > /etc/udev/rules.d/60-net.rules