dotfiles

<-- duh.
git clone https://hhvn.uk/dotfiles
git clone git://hhvn.uk/dotfiles
Log | Files | Refs | Submodules | LICENSE

autoconf-bond (2929B)


      1 #!/bin/rc
      2 # Create and automatically configure a bond0 device
      3 # and enslave ethernet/wlan interfaces to it.
      4 
      5 if (!~ `{id -u} 0) {
      6 	printf 'You should probably run this script as root.\n' >[1=2]
      7 	exit 1
      8 
      9 }
     10 
     11 cfile = /etc/ip.rc
     12 if (test -f $cfile) {
     13 	. $cfile
     14 }
     15 
     16 # Remove excess elements
     17 ip      = $ip(1)
     18 gateway = $gateway(1)
     19 lanmask = $lanmask(1)
     20 mode    = $mode(1)
     21 miimon  = $miimon(1)
     22 
     23 # Include it in /etc/ip.rc to avoid being prompted
     24 if (~ $ip ()) {
     25 	printf 'Ip: '
     26 	ip = `$nl{head -n 1}
     27 }
     28 if (~ $gateway ())
     29 	gateway = 192.168.1.1
     30 if (~ $lanmask ())
     31 	lanmask = 192.168.1.0/24
     32 if (~ $mode ())
     33 	mode = active-backup
     34 if (~ $miimon ())
     35 	miimon = 100
     36 
     37 printf 'Checking if module ''bonding'' is loaded...\n'
     38 if (~ `{lsmod | awk '$1 == "bonding"'} ()) {
     39 	printf 'Loading module ''bonding''...'
     40 	modprobe bonding || exit 1
     41 }
     42 
     43 printf 'Finding interfaces...\n'
     44 interfaces = `$nl{ip a | awk '/^[0-9]/ {gsub(/:/, ""); print $2}'}
     45 bond    = ()
     46 slaves  = ()
     47 pslaves = () # already enslaved
     48 primary = ()
     49 eth     = ()
     50 wlan    = ()
     51 
     52 for (i in $interfaces) {
     53 	if (~ $i en* || ~ $i eth*) {
     54 		eth = ($eth $i)
     55 	} else if (~ $i wl*) {
     56 		wlan = ($wlan $i)
     57 	} else if (~ $i bond*) {
     58 		bond = ($bond $i)
     59 	}
     60 }
     61 
     62 if (~ $bond ()) {
     63 	printf 'No bonding interface found\n'
     64 	exit 1
     65 }
     66 
     67 idir = /sys/class/net/ ^ $bond(1) ^ /bonding
     68 
     69 printf 'Setting bonding mode to: %s...\n' $mode
     70 echo $mode > $idir/mode || exit $status(1)
     71 
     72 printf 'Setting bonding miimon to: %s...\n' $miimon
     73 echo $miimon > $idir/miimon || exit $status(1)
     74 
     75 printf 'Getting bonding primary interface...\n'
     76 curprimary = `{cat $idir/primary}
     77 
     78 printf 'Checking for enslavable interfaces...\n'
     79 for (i in ($eth $wlan)) {
     80 	if (~ `{ip link show $i} 'master') {
     81 		printf 'Ignoring already enslaved interface %s\n' $i
     82 		pslaves = ($pslaves $i)
     83 	} else {
     84 		if (~ $curprimary ()) {
     85 			# TODO: better way to get primary
     86 			printf 'Setting %s as primary interface...\n' $i
     87 			echo $i > $idir/primary
     88 			curprimary = $i
     89 		}
     90 		slaves = ($slaves $i)
     91 	}
     92 }
     93 
     94 if (~ $slaves ()) {
     95 	printf 'No interfaces to enslave\n'
     96 	# If interfaces are already enslaved, consider it a success
     97 	if (~ $pslaves ()) {
     98 		exit 1
     99 	}
    100 } else {
    101 	for (i in $slaves) {
    102 		printf 'Enslaving %s...\n' $i
    103 		ifenslave $bond(1) $i || exit 1
    104 	}
    105 }
    106 
    107 if (!~ `{ip addr show $interface | awk '$1 ~ /^inet/ {sub(/\/.*/, ""); print $2}'} $ip) {
    108 	printf 'Adding address %s to %s...\n' $ip $bond(1)
    109 	ip addr add $ip/24 brd + dev $bond(1) || exit 1
    110 }
    111 
    112 printf 'Flushing default route...\n'
    113 ip route flush default || exit 1
    114 printf 'Configuring default route via %s\n' $gateway
    115 ip route add default dev $bond(1) || exit 1
    116 ip route change default via $gateway dev $bond(1) || exit 1
    117 printf 'Flushing LAN route...\n'
    118 ip route flush $lanmask || exit 1
    119 printf 'Configuring LAN route from %s\n' $ip
    120 ip route add $lanmask dev $bond(1) || exit 1
    121 ip route change $lanmask src $ip dev $bond(1) || exit 1
    122 
    123 printf 'Successfully configured %s with ip %s\n' $bond(1) $ip