ia64/xen-unstable

view tools/xenmgr/netfix @ 1494:72b8fe8a4636

bitkeeper revision 1.967.1.1 (40d190e3V7PN13crvyU7Jlnc76jn_g)

add searchpath to netfix
author iap10@labyrinth.cl.cam.ac.uk
date Thu Jun 17 12:38:59 2004 +0000 (2004-06-17)
parents 2c6f86e0083e
children 8fc674dc158e
line source
1 #!/usr/bin/python
2 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
3 #============================================================================
4 # Move the IP address from eth0 onto the Xen bridge (nbe-br).
5 # Works best if the bridge control utils (brctl) have been installed.
6 #============================================================================
7 import os
8 import os.path
9 import re
10 import sys
12 from getopt import getopt
14 os.defpath = os.defpath+':/sbin:/usr/sbin:/usr/local/sbin'
15 CMD_IFCONFIG = 'ifconfig'
16 CMD_ROUTE = 'route'
17 CMD_BRCTL = 'brctl'
19 def routes():
20 """Return a list of the routes.
21 """
22 fin = os.popen(CMD_ROUTE + ' -n', 'r')
23 routes = []
24 for x in fin:
25 if x.startswith('Kernel'): continue
26 if x.startswith('Destination'): continue
27 x = x.strip()
28 y = x.split()
29 z = { 'destination': y[0],
30 'gateway' : y[1],
31 'mask' : y[2],
32 'flags' : y[3],
33 'metric' : y[4],
34 'ref' : y[5],
35 'use' : y[6],
36 'interface' : y[7] }
37 routes.append(z)
38 return routes
40 def cmd(p, s):
41 """Print and execute command 'p' with args 's'.
42 """
43 global opts
44 c = p + ' ' + s
45 if opts.verbose: print c
46 if not opts.dryrun:
47 os.system(c)
49 def ifconfig(interface):
50 """Return the ip config for an interface,
51 """
52 fin = os.popen(CMD_IFCONFIG + ' %s' % interface, 'r')
53 inetre = re.compile('\s*inet\s*addr:(?P<address>\S*)\s*Bcast:(?P<broadcast>\S*)\s*Mask:(?P<mask>\S*)')
54 info = None
55 for x in fin:
56 m = inetre.match(x)
57 if not m: continue
58 info = m.groupdict()
59 info['interface'] = interface
60 break
61 return info
63 def reconfigure(interface, bridge):
64 """Reconfigure an interface to be attached to a bridge, and give the bridge
65 the IP address etc. from interface. Move the default route to the interface
66 to the bridge.
67 """
68 intf_info = ifconfig(interface)
69 if not intf_info:
70 print 'Interface not found:', interface
71 return
72 #bridge_info = ifconfig(bridge)
73 #if not bridge_info:
74 # print 'Bridge not found:', bridge
75 # return
76 route_info = routes()
77 intf_info['bridge'] = bridge
78 intf_info['gateway'] = None
79 for r in route_info:
80 if (r['destination'] == '0.0.0.0' and
81 'G' in r['flags'] and
82 r['interface'] == interface):
83 intf_info['gateway'] = r['gateway']
84 if not intf_info['gateway']:
85 print 'Gateway not found: ', interface
86 return
87 cmd(CMD_IFCONFIG, '%(interface)s 0.0.0.0' % intf_info)
88 cmd(CMD_IFCONFIG, '%(bridge)s %(address)s netmask %(mask)s broadcast %(broadcast)s up' % intf_info)
89 cmd(CMD_ROUTE, 'add default gateway %(gateway)s dev %(bridge)s' % intf_info)
90 if os.path.exists(CMD_BRCTL):
91 cmd(CMD_BRCTL, 'addif %(bridge)s %(interface)s' % intf_info)
93 defaults = {
94 'interface': 'eth0',
95 'bridge' : 'nbe-br',
96 'verbose' : 1,
97 'dryrun' : 0,
98 }
100 short_options = 'hvqni:b:'
101 long_options = ['help', 'verbose', 'quiet', 'interface=', 'bridge=']
103 def usage():
104 print """Usage:
105 %s [options]
107 Reconfigure routing so that <bridge> has the IP address from
108 <interface>. This lets IP carry on working when <interface>
109 is attached to <bridge> for virtual networking.
110 If brctl is available, <interface> is added to <bridge>,
111 so this can be run before any domains have been created.
112 """ % sys.argv[0]
113 print """
114 -i, --interface <interface> interface, default %(interface)s.
115 -b, --bridge <bridge> bridge, default %(bridge)s.
116 -v, --verbose Print commands.
117 -q, --quiet Don't print commands.
118 -n, --dry-run Don't execute commands.
119 -h, --help Print this help.
120 """ % defaults
121 sys.exit(1)
123 class Opts:
125 def __init__(self, defaults):
126 for (k, v) in defaults.items():
127 setattr(self, k, v)
128 pass
130 def main():
131 global opts
132 opts = Opts(defaults)
133 (options, args) = getopt(sys.argv[1:], short_options, long_options)
134 if args: usage()
135 for k, v in options:
136 if k in ['-h', '--help']:
137 usage()
138 elif k in ['-i', '--interface']:
139 opts.interface = v
140 elif k in ['-b', '--bridge']:
141 opts.bridge = v
142 elif k in ['-q', '--quiet']:
143 opts.verbose = 0
144 elif k in ['-v', '--verbose']:
145 opts.verbose = 1
146 elif k in ['-n', '--dry-run']:
147 opts.dryrun = 1
148 reconfigure(opts.interface, opts.bridge)
150 if __name__ == '__main__':
151 main()