#!/usr/bin/env python2.4 # -*- mode: python; -*- #============================================================================ # Copyright (C) 2005, 2006 Mike Wray # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #============================================================================ # Vnet (network virtualization) control utility. import os import os.path import re import socket import sys from getopt import getopt, GetoptError from xen.xend import sxp from xen.xend.PrettyPrint import prettyprint # Path of unix-domain socket to vnetd. VNETD_PATH = "/tmp/vnetd" def vnetd_running(): return os.path.exists(VNETD_PATH) def vnetd_open(): sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(VNETD_PATH) fi = sock.makefile('r', 0) fo = sock.makefile('w', 0) return (fi, fo) os.defpath += ':/sbin:/usr/sbin:/usr/local/sbin' CMD_IFCONFIG = 'ifconfig' CMD_BRCTL = 'brctl' opts = None class Opts: def __init__(self, **kwds): for (k, v) in kwds.items(): setattr(self, k, v) opts = Opts(verbose=False, dryrun=False) def set_opts(val): global opts opts = val return opts def cmd(prog, *args): """Execute command 'prog' with 'args', optionally printing the command. """ global opts command = " ".join([ prog ] + map(str, args)) if opts.verbose: print command if not opts.dryrun: os.system(command) def vif_bridge_add(bridge, vif): """Add a network interface to a bridge. """ cmd(CMD_BRCTL, 'addif', bridge, vif) def vif_bridge_rem(bridge, vif): """Remove a network interface from a bridge. """ cmd(CMD_BRCTL, 'delif', bridge, vif) def bridge_create(bridge, **kwd): """Create a bridge. Defaults hello time to 0, forward delay to 0 and stp off. """ cmd(CMD_BRCTL, 'addbr', bridge) if kwd.get('hello', None) is None: kwd['hello'] = 0 if kwd.get('fd', None) is None: kwd['fd'] = 0 if kwd.get('stp', None) is None: kwd['stp'] = 'off' bridge_set(bridge, **kwd) cmd(CMD_IFCONFIG, bridge, "up") def bridge_set(bridge, hello=None, fd=None, stp=None): """Set bridge parameters. """ if hello is not None: cmd(CMD_BRCTL, 'sethello', bridge, hello) if fd is not None: cmd(CMD_BRCTL, 'setfd', bridge, fd) if stp is not None: cmd(CMD_BRCTL, 'stp', bridge, stp) def bridge_del(bridge): """Delete a bridge. """ cmd(CMD_IFCONFIG, bridge, 'down') cmd(CMD_BRCTL, 'delbr', bridge) class Bridge: # Network interfaces are at /sys/class/net/*. # A bridge interface has ./bridge dir, ./brif is dir of bridged interfaces # (symlinks to the brport dirs). # If an interface is bridged ./brport is bridged port info, # brport/bridge is a symlink to the bridge. INTERFACE_DIR = "/sys/class/net" def isBridge(klass, dev): """Test if a network interface is a bridge. """ devdir = os.path.join(klass.INTERFACE_DIR, dev) brdir = os.path.join(devdir, "bridge") try: os.stat(brdir) return True except: return False isBridge = classmethod(isBridge) def getInterfaces(klass): """Get a list of the network interfaces. """ try: v = os.listdir(klass.INTERFACE_DIR) v.sort() return v except: return [] getInterfaces = classmethod(getInterfaces) def getInterfaceAddr(klass, intf): intfdir = os.path.join(klass.INTERFACE_DIR, intf) addrfile = os.path.join(intfdir, "address") try: f = file(addrfile, "rb") except Exception, ex: #print ex return None try: return f.readline().strip() finally: f.close() getInterfaceAddr = classmethod(getInterfaceAddr) def getBridges(klass): """Get a list of the bridges. """ return [ dev for dev in klass.getInterfaces() if klass.isBridge(dev) ] getBridges = classmethod(getBridges) def getBridgeInterfaces(klass, dev): """Get a list of the interfaces attached t
#!/usr/bin/env bash
#
# Copyright (C) 2012 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
SELF=${0##*/}

READELF="${READELF:-readelf}"
OBJCOPY="${OBJCOPY:-objcopy}"
TARGETS=$*
XARGS="${XARGS:-xargs -r}"

[ -z "$TARGETS" ] && {
  echo "$SELF: no directories / files specified"
  echo "usage: $SELF [PATH...]"
  exit 1
}

find $TARGETS -type f -a -exec file {} \; | \
  sed -n -e 's/^\(.*\):.*ELF.*\(executable\|shared object\).*,.*/\1/p' | \
  $XARGS -n1 $READELF -d | \
  awk '$2 ~ /NEEDED/ && $NF !~ /interpreter/ && $NF ~ /^\[?lib.*\.so/ { gsub(/[\[\]]/, "", $NF); print $NF }' | \
  sort -u

tmp=`mktemp $TMP_DIR/dep.XXXXXXXX`
for kmod in `find $TARGETS -type f -name \*.ko`; do
	$OBJCOPY -O binary -j .modinfo $kmod $tmp
	sed -e 's,\x00,\n,g' $tmp | \
		sed -ne '/^depends=.\+/ { s/^depends=//; s/,/.ko\n/g; s/$/.ko/p; q }'
done | sort -u
rm -f $tmp
[] for x in opts: l.append(x.help()) for x in args: l.append(x.help()) return " ".join(l) gethelp = classmethod(gethelp) """A command=-line option. @param name option name (this attribute is set to value in opts) @param short short option flag (single-character string) @param long long option name (defaults to option name, pass "" to suppress) @param arg argument name (option has no arg if not specified) """ def __init__(self, name, short=None, long=None, arg=False): self.name = name self.short = short if long is None: long = name elif not long: long = None self.long = long self.arg = arg def help(self): s = self.keyShort() l = self.keyLong() if s and l: return "[%s | %s]" % (s, l) else: return s or l def keyShort(self): if self.short: return "-%s" % self.short else: return None def keyLong(self): if self.long: return "--%s" % self.long else: return None def optLong(self): if not self.long: return None if self.arg: return "%s=" % self.long else: return self.long def optShort(self): if not self.short: return None if self.arg: return "%s:" % self.short else: return self.short def setDefault(self, vals): if self.arg: setattr(vals, self.name, None) else: setattr(vals, self.name, False) def setOpt(self, k, v, vals): if k in [ self.keyShort(), self.keyLong() ]: if self.arg: setattr(vals, self.name, v) else: if v not in [ None, '' ]: raise GetoptError("option %s does not take an argument" % k) setattr(vals, self.name, True) class Arg: """A command-line parameter. Args get their values from arguments left over after option processing and are assigned in order. The value is accessible as the attribute called 'name' in opts. @param name argument name """ def __init__(self, name): self.name = name def setArg(self, v, vals): setattr(vals, self.name, v) def help(self): return "<%s>" % self.name class VnMain: """Methods beginning with this prefix are commands. They must all have arguments like this: op_foo(self, argv, args, opts) argv: original command-line arguments args: arguments left after option processing opts: option and arg values (accessible as attributes) Method options are specified by setting attribute .opts on the method to a list of Option objects. For args set .args to a list of Arg objects. Use .use for short usage string, .help for long help. Each option or arg defines an attribute in opts. For example an option with name 'foo' is accessible as 'opts.foo'. """ opPrefix = "op_" def __init__(self, argv): if argv: self.name = argv[0] else: self.name = "vn" self.argv = argv self.argc = len(argv) def error(self, v): print >>sys.stderr, "%s: %s" % (self.name, v) sys.exit(1) def getFunction(self, opname): key = self.opPrefix + opname.replace("-", "_") fn = getattr(self, key, None) if not fn: raise ValueError("unknown command: %s" % opname) return fn def main(self): if self.argc < 2: args = ["help"] else: args = self.argv[1:] try: fn = self.getFunction(args[0]) except ValueError, ex: self.error(ex) try: fnopts = self.getOpts(fn) fnargs = self.getArgs(fn) (opts, parms) = Opt.getopt(args, fnopts, fnargs) return fn(args, parms, opts) except GetoptError, ex: self.error(ex) except ValueError, ex: self.error(ex) except Exception, ex: import traceback; traceback.print_exc() self.error(ex) def getOpts(self, meth): return getattr(meth, "opts", []) def getArgs(self, meth): return getattr(meth, "args", []) def getUse(self, meth): return getattr(meth, "use", "") def getHelp(self, meth): return getattr(meth, "help", "") or self.getUse(meth) def fnHelp(self, meth): return Opt.gethelp(self.getOpts(meth), self.getArgs(meth)) def printHelp(self, fn, opt_long): meth = getattr(self, fn) opname = fn[len(self.opPrefix):].replace("_", "-") if opt_long: help = self.getHelp(meth) print "\n %s" % opname if help: print "%s" % help else: use = self.getUse(meth) print " %s %s" % (opname, self.fnHelp(meth)) if use: print "\t\t%s" % use def show_vnif(self, dev): cmd(CMD_IFCONFIG, dev) bridge = Bridge.getBridge(dev) if bridge: print " Bridge:", bridge interfaces = Bridge.getBridgeInterfaces(bridge) if dev in interfaces: interfaces.remove(dev) if interfaces: print " Interfaces:", ", ".join(interfaces) print def op_help(self, argv, args, opts): if opts.long: print '%s ' % self.name print self.long_help else: print '%s:' % self.name l = dir(self) l.sort() for fn in l: if fn.startswith(self.opPrefix): self.printHelp(fn, opts.long) print op_help.opts = [ Opt('long', short='l') ] def op_vnets(self, argv, args, opts): vnets = vnet_list(vnets=args or None) for v in vnets: prettyprint(v, width=50) print if not opts.long: continue vnif = sxp.child_value(v, "vnetif") if not vnif: continue self.show_vnif(vnif) if opts.all: vnetids = {} for v in vnets: vnetids[sxp.child_value(v, "id")] = v for v in vif_list(): vnet = sxp.child_value(v, "vnet") if vnet not in vnetids: continue prettyprint(v) print for v in varp_list(): prettyprint(v) print op_vnets.opts = [ Opt('all', short='a'), Opt('long', short='l') ] def op_vnifs(self, argv, args, opts): vnifs = vnif_list(vnets=args or None) for vnif in vnifs: self.show_vnif(vnif) def op_vifs(self, argv, args, opts): for v in vif_list(): prettyprint(v) print def op_varp(self, argv, args, opts): for v in varp_list(): prettyprint(v) print def op_varp_flush(self, argv, args, opts): varp_flush() def op_vnet_create(self, argv, args, opts): return vnet_create(opts.vnet, vnetif=opts.vnetif, bridge=opts.bridge, security=opts.security) op_vnet_create.args = [ Arg('vnet') ] op_vnet_create.opts = [ Opt('security', short='s', arg="SECURITY"), Opt('bridge', short='b', arg="BRIDGE"), Opt('vnetif', short='v', arg="VNETIF") ] def op_vnet_delete(self, argv, args, opts): vnetid = get_vnetid(opts.vnet) return vnet_delete(vnetid, delbridge=opts.bridge) op_vnet_delete.args = [ Arg('vnet') ] op_vnet_delete.opts = [ Opt('bridge', short='b') ] def op_vif_add(self, argv, args, opts): vnetid = get_vnetid(opts.vnet) if opts.interface: vmac = get_mac(opts.vmac) if not vmac: raise ValueError("interface not found: %s" % opts.vmac) else: vmac = opts.vmac return vif_add(vnetid, vmac) op_vif_add.args = [ Arg('vnet'), Arg('vmac') ] op_vif_add.opts = [ Opt('interface', short='i') ] def op_vif_delete(self, argv, args, opts): vnetid = get_vnetid(opts.vnet) if opts.interface: vmac = get_mac(opts.vmac) else: vmac = opts.vmac return vif_del(vnetid, vmac) op_vif_delete.args = [ Arg('vnet'), Arg('vmac') ] op_vif_delete.opts = [ Opt('interface', short='i') ] def op_peer_add(self, argv, args, opts): addr = get_addr(opts.addr) if(opts.port): port = get_port(opts.port) else: port = None return peer_add(addr, port) op_peer_add.args = [ Arg('addr') ] op_peer_add.opts = [ Opt('port', short='p') ] def op_peer_delete(self, argv, args, opts): addr = get_addr(opts.addr) return peer_del(addr) op_peer_delete.args = [ Arg('addr') ] def op_peers(self, argv, args, opts): for v in peer_list(): prettyprint(v) print def op_bridges(self, argv, args, opts): if opts.long: for bridge in Bridge.getBridges(): cmd(CMD_IFCONFIG, bridge) interfaces = Bridge.getBridgeInterfaces(bridge) if interfaces: print " Interfaces:", ", ".join(interfaces) print else: for bridge in Bridge.getBridges(): print bridge, interfaces = Bridge.getBridgeInterfaces(bridge) if interfaces: print ":", ", ".join(interfaces) else: print op_bridges.opts = [ Opt('long', short='l') ] def op_insmod(self, argv, args, opts): """Insert the vnet kernel module.""" cmd("/etc/xen/scripts/vnet-insert", *args) long_help = """Control utility for vnets (virtual networking). Report bugs to Mike Wray . """ op_help.use = "Print help." op_help.help = "Print help, long help if the option -l or --long is given." op_vnets.use = """Print vnets.""" op_vnets.help = """Print vnet information, where options are: -a, -all Print vnets, vifs and varp info. -l, --long Print ifconfigs for vnet interfaces.""" op_vifs.use = "Print vifs." op_vnifs.use = "Print ifconfigs for vnet network interfaces." op_varp.use = "Print varp info and entries in the varp cache." op_varp_flush.use = "Flush the varp cache." op_vnet_create.use = "Create a vnet." op_vnet_delete.use = "Delete a vnet." op_vnet_delete.help = """Delete a vnet. -b, --bridge Delete the bridge the vnet interface is attached to. """ op_vif_add.use = "Add a vif to a vnet." op_vif_add.help = """Add a vif to a vnet. Not usually needed as vifs are added automatically. -i, --interface The vmac is the name of an interface to get the mac from.""" op_vif_delete.use = "Delete a vif from a vnet." op_vif_delete.help = """Delete a vif from a vnet. Not usually needed as vifs are removed periodically. -i, --interface The vmac is the name of an interface to get the mac from.""" op_peer_add.use = "Add a peer." op_peer_add.help = """Add a peer: Vnets use multicast to discover interfaces, but networks are often configured not to forward multicast. Vnets forward multicasts to peers using UDP. Only add peers if multicasts are not working, check with ping -b 224.10.0.1 Only add peers at one machine in a subnet, otherwise you may cause forwarding loops. """ op_peer_delete.use = "Delete a peer." op_peer_delete.help= "Delete a peer: " op_peers.use = "List peers." op_peers.help = "List peers." op_bridges.use = "Print bridges." op_insmod.use = "Insert the vnet kernel module, optionally with parameters." if __name__ == "__main__": vn = VnMain(sys.argv) vn.main()