ia64/xen-unstable

view tools/xenmgr/netfix @ 1495:8fc674dc158e

bitkeeper revision 1.975 (40d190ebrCE6SrZPrPmHz1ZNQbdYSQ)

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