Added help to network command.
Some more important changes.
local i
local VID
local DEVICE
- for i in $(cat /proc/net/vlan/config 2>/dev/null); do
- awk -F'|' '{ print $2 $3 }' | read DEVICE VID PARENT
- if [ -n "${VID}" ] || [ -n "${DEVICE}" ]; then
- continue
- fi
- if [ "${vid}" = "${VID}" ] && [ "$(macify ${PARENT})" = "${mac}" ]; then
- echo "${DEVICE}"
- return 0
- fi
- done
+ grep '|' < /proc/net/vlan/config 2>/dev/null | sed "s/|//g" | \
+ while read DEVICE VID PARENT; do
+ if [ "${vid}" = "${VID}" ] && [ "$(macify ${PARENT})" = "${mac}" ]; then
+ echo "${DEVICE}"
+ return 0
+ fi
+ done
return 1
}
+function get_device() {
+ if [ ${#@} -gt 1 ]; then
+ get_device_by_mac_and_vid $@
+ else
+ get_device_by_mac $@
+ fi
+}
+
function get_mac_by_device() {
local device
device=$1
return 1
}
+function get_mac() {
+ get_mac_by_device $@
+}
+
function devicify() {
local device
local mac
}
function zone_add_port() {
- brctl addif ${1} ${2}
+ local zone
+ local port
+
+ zone=${1}
+ port=${2}
+
+ brctl addif ${zone} ${port}
+}
+
+function zone_del_port() {
+ local zone
+ local port
+
+ zone=${1}
+ port=${2}
+
+ brctl delif ${zone} ${port}
}
function all_zones() {
${CONFIG_PORTS}/${port}/ethernet
;;
+ attach)
+ zone_add_port ${ZONE} $(get_device ${port})
+ ;;
+
+ detach)
+ zone_del_port ${ZONE} $(get_device ${port})
+ ;;
+
*)
- echo "Usage: ${0} [interface] {up|down|add|remove}"
+ echo "Usage: ${0} [interface] {up|down|add|remove|attach|detach}"
exit 1
;;
esac
case "${2}" in
up)
- modprobe 8021q
- MESSAGE="Adding VLAN ${ID} to port ${port}..."
- vconfig add ${device} ${ID} &>/dev/null
- evaluate_retval
+ if ! get_device ${port} ${ID}; then
+ modprobe 8021q
+ MESSAGE="Adding VLAN ${ID} to port ${port}..."
+ vconfig add ${device} ${ID} &>/dev/null
+ evaluate_retval
+ fi
;;
down)
- MESSAGE="Removing VLAN ${ID} from port ${port}..."
- vconfig rem ${device} ${ID} &>/dev/null
- evaluate_retval
+ if get_device ${port} ${ID}; then
+ MESSAGE="Removing VLAN ${ID} from port ${port}..."
+ vconfig rem ${device} ${ID} &>/dev/null
+ evaluate_retval
+ fi
;;
add)
;;
remove)
+ # Should we do this here? What about detaching?
+ CONFIG=${CONFIG_ZONES}/${ZONE}/port-${port}-vlan-${ID} ${0} ${port} down
rm -f \
${CONFIG_PORTS}/${port}/vlan-${ID} \
${CONFIG_ZONES}/${ZONE}/port-${port}-vlan-${ID}
;;
+ attach)
+ zone_add_port ${ZONE} $(get_device ${port} ${ID})
+ ;;
+
+ detach)
+ zone_del_port ${ZONE} $(get_device ${port} ${ID})
+ ;;
+
*)
- echo "Usage: ${0} [interface] {up|down|add|remove}"
+ echo "Usage: ${0} [interface] {up|down|add|remove|attach|detach}"
exit 1
;;
esac
exit 1
fi
-MAC=$(get_mac_by_device ${INTERFACE})
-
-logger -t net "Received uevent for $INTERFACE ($ACTION)."
+MAC=$(get_mac ${INTERFACE})
case $ACTION in
add|register)
# interfaces that are registered then brought up
*)
export IN_HOTPLUG=1
- for file in ${NETWORK_DEVICES}/*/*; do
- if [ "$(basename ${file})" = "${MAC}" ]; then
- /etc/init.d/networking/ifup ${file}
- exit $?
- fi
- done
+ /etc/init.d/networking/port ${MAC} up
+ # XXX Walk through zones here?
;;
esac
;;
;;
*)
export IN_HOTPLUG=1
- for file in ${NETWORK_DEVICES}/*/*; do
- if [ "$(basename ${file})" = "${MAC}" ]; then
- /etc/init.d/networking/ifdown ${file}
- exit $?
- fi
- done
+ : # Nothing to do here at the moment
;;
esac
;;
hooks="${hooks} ${CONFIG_PORTS}/${port}/${arg}"
done
-echo "DEBUG: hooks: $hooks"
-
# Getting zone variable
#local zone
#if [ -z "$ZONE" ]; then
case "$action" in
up)
message="Bringing up zone ${zone}..."
-
+
# Check if bridge already exists
zone_status=`brctl show 2>/dev/null`
if ! echo "${zone_status}" | grep -q "^${zone}"; then
- #/etc/init.d/networking/hooks pre-up ${CONFIG_ZONES}/${zone}
-
# Create and bring up the zone
brctl addbr ${zone} || failed=1
ip link set ${zone} up || failed=1
(exit ${failed})
evaluate_retval standard
+ # When we boot, we should bring up every port
[ "${IN_BOOT}" = "1" ] && \
for port in ${CONFIG_ZONES}/${zone}/port-*; do
- #port=$(readlink ${port})
ZONE=${zone} /etc/init.d/networking/port ${port} up
done
+ # Attach ports
+ for config in ${CONFIG_ZONES}/${zone}/port-*; do
+ port=${config##*/}; port=${port#port-}; port=${port%%-*}
+ echo "DEBUG: port $port"
+ ZONE=${zone} CONFIG=${config} \
+ /etc/init.d/networking/port ${port} attach
+ done
+
###/etc/init.d/networking/hooks post-up ${CONFIG_ZONES}/${zone}
else
log_warning_msg
zone_status=`brctl show 2>/dev/null`
if echo "${zone_status}" | grep -q "^${zone}"; then
#/etc/init.d/networking/hooks pre-down ${CONFIG_ZONES}/${zone}
- # remove devices
- for port in ${CONFIG_ZONES}/${zone}/port-*; do
- brctl delif ${zone} ${port##*/}
- /etc/init.d/networking/port ${port} down
+ # Detach ports
+ for config in ${CONFIG_ZONES}/${zone}/port-*; do
+ port=${file#port-}; port=${port%%-*}
+ ZONE=${zone} CONFIG=${config} \
+ /etc/init.d/networking/port ${port} detach
done
# Bring down the zone and delete it
brctl delbr ${zone} || failed=1
(exit ${failed})
evaluate_retval standard
-
- #/etc/init.d/networking/hooks post-down ${CONFIG_ZONES}/${zone}
else
log_warning_msg
message="Zone ${zone} does not exist."
. /etc/init.d/networking/functions
function usage() {
- echo "Usage $0 - TODO"
- _exit $1
+ echo -e "${BOLD}Usage $0${NORMAL}\n"
+ case "$1" in
+ main|"")
+ echo "This script will help you configuring your network."
+ echo "You should know that there are two different things:"
+ echo
+ echo " zone: A group of ports."
+ echo " port: Connection to somewhere."
+ echo
+ echo " $0 [global flags] <port|zone> ... or"
+ echo " $0 [global flags] <cmd line options...>"
+ echo
+ echo -e "${BOLD}Global flags:${NORMAL}"
+ echo " --verbose -v - Turn on verbose mode."
+ echo " --debug -d - Turn on debug mode."
+ echo
+ echo -e "${BOLD}Command line options:${NORMAL}"
+ echo " help - Prints this help message."
+ echo " start - Starts the whole network."
+ echo " stop - Stops the whole network."
+ echo " restart - Restarts the whole network."
+ echo " reload - Reloads the whole network."
+ echo
+ echo " zone - Run \"$0 zone help\" for more information."
+ echo " port - Run \"$0 port help\" for more information."
+ echo
+ ;;
+ port)
+ echo -e "${BOLD}Port Configuration:${NORMAL}"
+ echo
+ echo " $0 [global options] port <command> ..."
+ echo
+ echo -e "${BOLD}Commands:${NORMAL}"
+ echo -e " ${BOLD}show:${NORMAL}"
+ echo " Displays information about a given port."
+ echo
+ echo " Requires a \"port\"."
+ echo " Example: $0 port show 00:11:22:33:44:55"
+ echo " $0 port show black0"
+ echo
+ ;;
+ zone)
+ echo -e "${BOLD}Zone Configuration:${NORMAL}"
+ echo
+ echo " $0 [global options] zone <command> ..."
+ echo
+ echo -e "${BOLD}Commands:${NORMAL}"
+ echo -e " ${BOLD}show:${NORMAL}"
+ echo " Displays information about a given zone."
+ echo
+ echo " Requires a \"zone\"."
+ echo " Example: $0 zone show green0"
+ echo
+ echo -e " ${BOLD}add:${NORMAL}"
+ echo " Adds a new zone."
+ echo
+ echo " Requires a \"zone\"."
+ echo " Example: $0 zone add green0"
+ echo
+ echo -e " ${BOLD}del:${NORMAL}"
+ echo " Deletes a zone."
+ echo
+ echo " Requires a \"zone\"."
+ echo " Example: $0 zone del green0"
+ echo
+ echo -e " ${BOLD}addport:${NORMAL}"
+ echo " Adds a port to a zone."
+ echo
+ echo " Requires a \"zone\" and \"port\"."
+ echo " Example: $0 zone addport green0"
+ echo
+ echo " You may also pass a hook and its parameters:"
+ echo " $0 zone addport green0 black0 vlan 10"
+ echo
+ echo -e " ${BOLD}delport:${NORMAL}"
+ echo " Deletes a port from a zone."
+ echo
+ echo " Requires a \"zone\" and \"port\"."
+ echo " Example: $0 zone delport green0"
+ echo
+ echo " You may also pass a hook and its parameters:"
+ echo " $0 zone delport green0 black0 vlan 10"
+ echo
+ esac
+ _exit ${2-1}
}
function debug() {
vecho "${BOLD}Verbose mode is enabled.${NORMAL}"
;;
help|-h|--help)
- usage 0
+ usage main 0
;;
start|stop|restart|reload)
exec /etc/init.d/network $arg
port_show $@
_exit $?
;;
+ help)
+ usage port 0
+ ;;
esac
;;
zone|zo|z)
port_del $@
_exit $?
;;
+ help)
+ usage zone 0
+ ;;
esac
;;
- *)
- usage
- ;;
esac
done
+
+usage main