#!/bin/sh # /etc/rc.d/rc.vde2 # This script is used to bring up various tap interfaces that can be used by # virtual machines as host-only interfaces. # # (c) 2010, 2013 Michiel Broek ############################ # READ NETWORK CONFIG FILE # ############################ # Get the configuration information from /etc/rc.d/rc.vde2.conf: . /etc/rc.d/rc.vde2.conf # If possible, log events in /var/log/messages: if [ -f /var/run/syslogd.pid -a -x /usr/bin/logger ]; then LOGGER=/usr/bin/logger else # output to stdout/stderr: LOGGER=/bin/cat fi start(){ echo -n "Starting VDE Switch:" # Load tun module /sbin/modprobe tun || { echo "Error, cannot load 'tun' module. Exiting..." ; exit 1 ; } # Start VDE switches VDE="" TAP="" i=0 while [ $i -lt 6 ]; do # Switch configured? [ "${VDENAME[$i]}" = "" ] && break echo -n " ${VDENAME[$i]}:" VDECMD="/usr/bin/vde_switch --sock /var/run/vde.ctl.${VDENAME[$i]} --mode 0660 --group vboxusers" VDECMD="$VDECMD --mgmt /var/run/vde.mgmt.${VDENAME[$i]} --mgmtmode 660 --mgmtgroup vboxusers --daemon" if [ -z "${TAPNAME[$i]}" ]; then # Just a switch echo -n "switch" echo "/etc/rc.d/rc.vde2: $VDECMD" | $LOGGER $VDECMD || { echo "Error, cannot create switch ${VDENAME[$i]}. Exiting..." ; exit 1 ; } # Attach slirp ? if [ "${VDESLIRP[$i]}" == "yes" ]; then echo -n ":slirp" if [ "${VDEDHCP[$i]}" == "yes" ]; then echo "/etc/rc.d/rc.vde2: /usr/bin/slirpvde --socket /var/run/vde.ctl.${VDENAME[$i]} --dhcp --daemon" | $LOGGER /usr/bin/slirpvde --socket /var/run/vde.ctl.${VDENAME[$i]} --dhcp --daemon else echo "/etc/rc.d/rc.vde2: /usr/bin/slirpvde --socket /var/run/vde.ctl.${VDENAME[$i]} --daemon" | $LOGGER /usr/bin/slirpvde --socket /var/run/vde.ctl.${VDENAME[$i]} --daemon fi fi else # Switch with tap to the host TAP="${TAPNAME[$i]}" echo -n "tap:${TAPCIDR[$i]}" echo "/etc/rc.d/rc.vde2: $VDECMD --tap ${TAP}" | $LOGGER $VDECMD --tap ${TAP} || { echo "Error, cannot create tap switch ${VDENAME[$i]}. Exiting..." ; exit 1 ; } # Bring tap interface up if [ -n "${TAPCIDR[$i]}" ]; then echo "/etc/rc.d/rc.vde2: /usr/sbin/ip addr add ${TAPCIDR[$i]} broadcast + dev ${TAP}" | $LOGGER /usr/sbin/ip addr add ${TAPCIDR[$i]} broadcast + dev ${TAP} fi echo "/etc/rc.d/rc.vde2: /usr/sbin/ip link set ${TAP} up" | $LOGGER /usr/sbin/ip link set ${TAP} up if [ -n "${IP6ADDR[$i]}" ]; then echo "/etc/rc.d/rc.vde2: /sbin/ifconfig ${TAP} add ${IP6ADDR[$i]}" | $LOGGER /sbin/ifconfig ${TAP} add ${IP6ADDR[$i]} fi fi echo -n ":started " i=$(($i+1)) done echo } stop(){ echo -n "Stopping VDE Switch:" i=0 while [ $i -lt 6 ]; do [ "${VDENAME[$i]}" = "" ] && break echo -n " ${VDENAME[$i]}:" if [ -z "${TAPNAME[$i]}" ]; then # Just a switch echo -n "switch" if [ "${VDESLIRP[$i]}" == "yes" ]; then echo -n ":slirp" PID=$( /usr/bin/ps ax | /usr/bin/grep /usr/bin/slirpvde | /usr/bin/grep /var/run/vde.ctl.${VDENAME[$i]} | /usr/bin/cut -b 1-5) echo "/etc/rc.d/rc.vde2: kill $PID" | $LOGGER kill $PID fi else # Bring tap interface down echo "/etc/rc.d/rc.vde2: /usr/sbin/ip addr flush dev ${TAPNAME[$i]}" | $LOGGER /usr/sbin/ip addr flush dev ${TAPNAME[$i]} echo "/etc/rc.d/rc.vde2: /usr/sbin/ip link set ${TAPNAME[$i]} down" | $LOGGER /usr/sbin/ip link set ${TAPNAME[$i]} down echo -n "tap:${TAPCIDR[$i]}" fi # Shutdown this switch if [ -S /var/run/vde.mgmt.${VDENAME[$i]} ]; then echo "/etc/rc.d/rc.vde2: /usr/bin/vdecmd -s /var/run/vde.mgmt.${VDENAME[$i]} shutdown" | $LOGGER /usr/bin/vdecmd -s /var/run/vde.mgmt.${VDENAME[$i]} shutdown echo -n ":shutdown" fi i=$(($i+1)) done # Remove tun module /usr/bin/sleep .2 /sbin/modprobe -r tun 2>/dev/null echo } status() { echo "Status Virtual Distributed Ethernet:" i=0 while [ $i -lt 6 ]; do [ "${VDENAME[$i]}" = "" ] && break echo echo -n "Ethernet switch \`${VDENAME[$i]}' " if [ -S /var/run/vde.mgmt.${VDENAME[$i]} ]; then echo "is up, status:" # Show IP address if this is a tap network if [ -n "${TAPNAME[$i]}" ]; then /usr/sbin/ip addr show dev ${TAPNAME[$i]} | grep inet fi # Somw other info to show. /usr/bin/vdecmd -s /var/run/vde.mgmt.${VDENAME[$i]} vlan/allprint /usr/bin/vdecmd -s /var/run/vde.mgmt.${VDENAME[$i]} port/showinfo /usr/bin/vdecmd -s /var/run/vde.mgmt.${VDENAME[$i]} port/allprint else echo "is down" fi i=$(($i+1)) done } case "$1" in start) start ;; stop) stop ;; status) status ;; restart) stop start ;; *) echo "Usage: $0 {start|stop|status|restart}" ;; esac