From 834cc85397bb7b1f2e0dc9f0536a94b064ca7141 Mon Sep 17 00:00:00 2001 From: Oluf Lorenzen Date: Fri, 24 Jul 2009 02:15:19 +0200 Subject: [PATCH 108/108] added complete VLAN-setup --- examples/ethernet-iproute-vlan | 10 +++ src/connections/ethernet-iproute-vlan | 134 +++++++++++++++++++++++++++++++++ src/network | 18 ++++- 3 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 examples/ethernet-iproute-vlan create mode 100644 src/connections/ethernet-iproute-vlan diff --git a/examples/ethernet-iproute-vlan b/examples/ethernet-iproute-vlan new file mode 100644 index 0000000..47778a2 --- /dev/null +++ b/examples/ethernet-iproute-vlan @@ -0,0 +1,10 @@ +CONNECTION="ethernet-iproute-vlan" +DESCRIPTION="A more versatile static ethernet connection using iproute and VLAN" +PHYS_INTERFACE="eth0" +VLAN="7" +FLAG="false" +IP="static" +# Any valid iproute command can be placed in this array +IPCFG=("addr add dev eth0.7 192.168.1.23/24 brd +" \ + "route replace default via 192.168.1.1") +DNS=("192.168.1.1") diff --git a/src/connections/ethernet-iproute-vlan b/src/connections/ethernet-iproute-vlan new file mode 100644 index 0000000..61649ee --- /dev/null +++ b/src/connections/ethernet-iproute-vlan @@ -0,0 +1,134 @@ +#! /bin/bash +. /usr/lib/network/network + +INTERFACE=$PHYS_INTERFACE.$VLAN + +error() +{ + err_append "$*" + ip addr flush $INTERFACE &>/dev/null + quirk "nodown" || ip link set $INTERFACE down &>/dev/null + exit 1 +} + +ethernet_up() { + load_profile $1 + + if [[ ! -e /sys/class/net/$PHYS_INTERFACE ]]; then + if ! echo "$PHYS_INTERFACE"|grep ":"; then + error "Physical interface $PHYS_INTERFACE does not exist" + fi + fi + ip link set $PHYS_INTERFACE up + + if [[ ! -e /sys/class/net/$INTERFACE ]]; then + if ! modprobe 8021q; then + echo "Kernel-Module 8021q could not be loaded" + fi + if [[! -e /usr/sbin/vconfig ]]; then + error "vconfig is not executable" + fi + if ! vconfig add $PHYS_INTERFACE $VLAN; then + error "VLAN-Interface $INTERFACE could not be created" + fi + if ! "$FLAG" == "true" ; then + vconfig set_flag $INTERFACE 1 + fi + fi + + ip link set $INTERFACE up + sleep 1 + + if ip link show $PHYS_INTERFACE|grep -q "NO-CARRIER"; then + err_append "No connection" + return 1 + fi + + if checkyesno ${AUTH8021X:-no}; then + . ${SUBR_DIR}/8021x + [[ -z "$WPA_CONF" ]] && WPA_CONF="/etc/wpa_supplicant.conf" + [[ -z "$WPA_OPTS" ]] && WPA_OPTS="-Dwired" + + start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_OPTS" + if ! wpa_check "$INTERFACE"; then + ip link set $INTERFACE down + return 1 + fi + fi + + case $IP in + dhcp) + # Clear remaining pid files. + rm -f /var/run/dhcpcd-${INTERFACE}.{pid,cache} >/dev/null 2>&1 + + # If using own dns, tell dhcpcd to NOT replace resolv.conf + [[ -n "$DNS" ]] && DHCP_OPTIONS="-C resolv.conf $DHCP_OPTIONS" + + if ! dhcpcd -qL -t ${DHCP_TIMEOUT:-10} $DHCP_OPTIONS $INTERFACE; then + error "DHCP IP lease attempt failed" + fi + ;; + static) + if [[ -n "$ADDR" ]]; then + if ! ip addr add ${ADDR}/24 brd + dev $INTERFACE; then + error "Could not configure interface" + fi + fi + if [[ -n "$GATEWAY" ]]; then + if ! ip route add default via $GATEWAY; then + error "Adding gateway failed" + fi + fi + ;; + *) + error "Profile error: IP must be either 'dhcp' or 'static'" + ;; + esac + + if [[ -n "$IPCFG" ]]; then + for line in "${IPCFG[@]}"; do + if ! ip $line; then + error "Could not configure interface" + fi + done + fi + + # Set hostname + if [[ -n "$HOSTNAME" ]]; then + if ! hostname $HOSTNAME; then + error "Cannot set hostname" + fi + fi + + # Generate a new resolv.conf + if [[ -n "$DNS" ]]; then + : >/etc/resolv.conf + [[ -n "$DOMAIN" ]] && echo "domain $DOMAIN" >>/etc/resolv.conf + [[ -n "$SEARCH" ]] && echo "search $SEARCH" >>/etc/resolv.conf + + if [[ -n "$DNS" ]]; then + for dns in ${DNS[@]}; do + echo "nameserver $dns" >>/etc/resolv.conf + done + fi + fi + return 0 +} + +ethernet_down() { + load_profile $1 + + if [[ "$IP" == "dhcp" ]]; then + if [[ -f /var/run/dhcpcd-${INTERFACE}.pid ]]; then + dhcpcd -qx $INTERFACE + fi + fi + + ip addr flush $INTERFACE + quirk "nodown" || vconfig rem $INTERFACE + +} + +ethernet_$1 $2 +exit $? +# vim: set ts=4 et sw=4: diff --git a/src/network b/src/network index b45caaa..19e745b 100644 --- a/src/network +++ b/src/network @@ -34,9 +34,20 @@ validate_profile() return 1 fi . $PROFILE_DIR/$1 - if [[ -z "$INTERFACE" ]]; then - err "Profile missing an interface to configure" - return 1 + if ! $CONNECTION == "ethernet-iproute-vlan"; then + if [[ -z "$INTERFACE" ]]; then + err "Profile missing an interface to configure" + return 1 + fi + else + if [[ -z "$PHYS_INTERFACE" ]]; then + err "Profile missing an physical interface to configure" + return 1 + fi + if [[ -z "$VLAN" ]]; then + .err "Profile missing an VLAN to configure" + return 1 + fi fi if [[ ! -f $CONN_DIR/$CONNECTION ]]; then err "$CONNECTION is not a valid connection, check spelling or look at examples" @@ -295,3 +306,4 @@ checkyesno() ;; esac } +# vim: set ts=4 et sw=4: -- 1.6.3.3