xen-drbd

view xen-drbd-install.py @ 37:e80bdf496aa2

1. --network (-n) option; topology name is not hardcoded since present
2. fix for incorrect processing of the paramater spicified in form like drbd8=sda1 in xen-drbd-install
author igor@manas.xt.vpn
date Mon Sep 15 10:03:24 2008 +0300 (2008-09-15)
parents 44efea4507ac
children 30671fe7fd79
line source
1 #!/usr/bin/python
3 import sys,re,getopt
4 sys.path.append('/etc/xen')
6 def show_usage():
7 print """
8 Usage:
9 xen-drbd-install [options] <command> (to view)
10 xen-drbd-install [options] <command> | sh -s (to run)
12 <network> is the name of the file, which contains network description
14 Commands:
15 make-drbd
16 make-domains
17 make-bridges
18 make-links
20 Options:
21 --help (-h) -- show usage information
22 --network=name (-n) -- the network descriptions is in the file name.py (default: network.py)
23 """
27 network='network'
28 try:
29 opts, args = getopt.getopt(sys.argv[1:], "hn:", ["help", "network="])
30 except getopt.GetoptError, err:
31 # print help information and exit:
32 print str(err) # will print something like "option -a not recognized"
33 usage()
34 sys.exit(2)
36 for o, a in opts:
37 if o in ("-h", "--help"):
38 show_usage()
39 sys.exit()
40 elif o in ("-n", "--network"):
41 network = a
42 else:
43 assert False, "unhandled option"
45 if len(args) != 1:
46 show_usage()
47 sys.exit(1)
49 try:
50 exec 'from %s import * ' % (network)
51 except ImportError:
52 print "Can't find or interpret module <%s> with topology description" %(network)
53 sys.exit(1)
56 drbd_base_port=7790
57 meta_disk='/dev/%s/meta'%lvm_vg_name
58 drbd_device={}
60 linux_domains=[]
61 for dom in domains:
62 if domain_types[domains.index(dom)] == 'linux':
63 linux_domains.append(dom)
65 def make_drbd_hash():
66 for dom in domains:
67 for disk in disk_table[dom]:
68 if disk.find(":") == -1:
69 if disk.find("=") == -1:
70 drbd_device[disk]=dom
71 else:
72 drbd_device[disk.split('=')[0]]=dom
73 else:
74 disk0=disk.split(':')[0]
75 drbd_device[disk0.split('=')[0]]=disk.split(':')[1]
77 def make_lvm():
78 print "###################################################"
79 print "# MAKE LVM FOR DOMAINS DRBD DEVICES"
80 print "#"
81 for lvm_pv_name in re.compile('\s+').split(lvm_pv_names):
82 print "pvcreate %s" % (lvm_pv_name)
83 print "vgcreate /dev/%s %s" % (lvm_vg_name, lvm_pv_names)
84 print "lvcreate -L %s -n %s /dev/%s" % (lvm_lv_drbd_meta_size, lvm_lv_drbd_meta_name, lvm_vg_name)
85 for dom in linux_domains:
86 for disk in disk_table[dom]:
87 if disk.find(":") == -1:
88 #if do_print:
89 # print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name)
90 #drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)])
91 drbd_device[disk]=dom
92 else:
93 drbd_device[disk.split(':')[0]]=disk.split(':')[0]
94 print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[2], disk.split(':')[1], lvm_vg_name)
95 #drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])])
96 print ""
99 def make_drbdconf():
100 print "###################################################"
101 print "# MAKE DRBD CONFIG FILE /etc/drbd.conf"
102 print "#"
104 print "cat <<DRBD > /etc/drbd.conf"
107 for dom in domains:
108 for disk in disk_table[dom]:
109 drbd_number=int(re.compile('drbd').sub('',(disk.split(':')[0]).split('=')[0] ))
110 resource=disk.split(':')[1]
111 disk="/dev/%s/%s" % (lvm_vg_name,resource)
113 print """
114 resource %s {
115 protocol C;
116 net {
117 allow-two-primaries;
118 after-sb-0pri discard-least-changes;
119 after-sb-1pri call-pri-lost-after-sb;
120 after-sb-2pri call-pri-lost-after-sb;
121 }
122 syncer {
123 rate 5M;
124 }
125 on %s
126 {
127 device /dev/drbd%s;
128 disk %s;
129 address %s:%s;
130 meta-disk %s[%s];
131 }
132 on %s
133 {
134 device /dev/drbd%s;
135 disk %s;
136 address %s:%s;
137 meta-disk %s[%s];
138 }
139 }
141 """ % (resource, node1, drbd_number, disk, node1_ip, drbd_number+drbd_base_port, meta_disk, drbd_number, node2, drbd_number, disk, node2_ip, drbd_number+drbd_base_port, meta_disk, drbd_number )
142 print "DRBD"
143 print ""
146 def make_drbdmeta():
147 print "###################################################"
148 print "# MAKE DRBD DEVICES "
149 print "#"
150 print "yes yes | drbdadm create-md all"
152 def make_drbdprimary():
153 print "###################################################"
154 print "# SET DRBD DEVICES PRIMARY "
155 print "#"
157 print "drbdadm up all"
158 for dom in domains:
159 for disk in disk_table[dom]:
160 print "drbdsetup %s primary -o" % disk.split(':')[0]
163 def make_links():
165 print "###################################################"
166 print "# MAKE SYMBOLIC LINKS TO /dev/drbd* FILES"
167 print "#"
169 # drbd_devices={
170 # 'dev' : 'resource',
171 # }
173 make_drbd_hash()
174 print "mkdir -p /dev/drbd"
175 for dev in drbd_device.keys():
176 resource=drbd_device[dev]
177 print "ln -sf /dev/%s /dev/drbd/%s" %(dev, resource)
179 def make_fs():
181 print "###################################################"
182 print "# MAKE FILESYSTEMS ON THE DRBDs"
183 print "#"
185 for dev in drbd_device.keys():
186 print "mkfs.ext3 %s /dev/%s" %(mkfs_options,dev)
189 def make_mount():
190 print "###################################################"
191 print "# MOUNT U-DOMAINS FILESYSTEMS"
192 print "#"
194 print "mkdir -p /domU"
195 for dom in linux_domains:
196 print "mkdir -p /domU/%s" % (dom)
197 print "mount /dev/drbd/%s /domU/%s" % (dom,dom)
199 def make_umount():
200 for dom in linux_domains:
201 print "umount /domU/%s" % (dom)
203 def make_dns():
204 i=1
205 for dom in linux_domains:
206 print "%s A %s.%s" % (dom, ip_network, i)
207 i+=1
209 def make_dns_reverse():
210 i=1
211 for dom in linux_domains:
212 print "%s A %s.%s." % (i, dom, domain_name)
213 i+=1
215 def make_install_debian():
216 print """
217 apt-get install debootstrap
218 debootstrap %s /domU/%s %s
219 chroot /domU/%s apt-get install -y --force-yes %s
220 """ % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install)
222 def make_rsync():
223 for dom in domains[1:]:
224 print """
225 rsync -a /domU/%s/ /domU/%s/
226 """ % (domains[0], dom)
228 def make_etcfstab():
229 i=0
230 for dom in linux_domains:
231 i+=1
232 print """
233 cat <<FSTAB > /domU/%s/etc/fstab
234 proc /proc proc defaults 0 0
235 /dev/hda1 / ext3 defaults,errors=remount-ro 0 1
236 FSTAB
237 """ % (dom)
239 def make_etcnetworkinterfaces():
240 i=0
241 for dom in linux_domains:
242 i+=1
243 print """
244 cat <<INTERFACES > /domU/%s/etc/network/interfaces
245 auto lo eth0
246 iface lo inet loopback
248 iface eth0 inet static
249 address %s
250 netmask 255.255.255.0
251 network %s.0
252 broadcast %s.255
253 gateway %s
254 dns-nameservers %s
255 dns-search %s
256 INTERFACES
257 """ % (dom,ip_address_table[dom],ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
259 def make_etcresolvconf():
260 for dom in linux_domains:
261 print """
262 cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
263 search %s
264 nameserver %s
265 RESOLVCONF
266 """ % (dom,domain_name,ip_nameserver)
268 def make_etchostname():
269 for dom in linux_domains:
270 print """
271 cat <<HOSTNAME > /domU/%s/etc/hostname
272 %s
273 HOSTNAME
274 """ % (dom,dom)
277 def make_etchosts():
278 i=0
279 for dom in linux_domains:
280 i+=1
281 print """
282 cat <<HOSTS > /domU/%s/etc/hosts
283 127.0.0.1 localhost
284 %s %s.%s %s
286 ::1 ip6-localhost ip6-loopback
287 fe00::0 ip6-localnet
288 ff00::0 ip6-mcastprefix
289 ff02::1 ip6-allnodes
290 ff02::2 ip6-allrouters
291 ff02::3 ip6-allhosts
292 HOSTS
293 """ % (dom,ip_address_table[dom],dom,domain_name,dom)
295 def make_rootssh():
296 for dom in linux_domains:
297 print """
298 mkdir -p /domU/%s/home/eb/.ssh/
299 chown -R 1000:1000 /domU/%s/home/eb
300 cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
301 chmod 600 /domU/%s/home/eb/.ssh/authorized_keys
302 """ % (dom,dom,dom,dom)
304 def make_l3rc():
305 for dom in linux_domains:
306 print """
307 cat <<EOF > /domU/%s/root/.l3rc
308 l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
309 EOF
310 """ % (dom,dom,domain_name)
313 def make_etc():
314 make_etcfstab()
315 make_etcnetworkinterfaces()
316 make_etcresolvconf()
317 make_etchosts()
318 make_etchostname()
319 make_rootssh()
320 #make_l3rc()
322 def make_drbd():
323 make_lvm()
324 make_drbdconf()
325 make_drbdmeta()
327 def make_domains():
328 make_drbdprimary()
329 make_links()
330 make_fs()
331 make_mount()
332 make_install_debian()
333 make_rsync()
334 make_etc()
335 make_umount()
337 def get_management_ip():
338 return management_ip
340 def get_default_gateway():
341 return management_gw
343 def make_bridges():
344 ip=get_management_ip()
345 gw=get_default_gateway()
346 print "ifconfig %s 0.0.0.0 down" % (trunk)
348 # tagged interfaces
349 for bridge in bridges:
350 vlan=vlans[bridges.index(bridge)]
351 if vlan == 'tagged':
352 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk)
353 print "ip link set %s up" % (trunk)
355 # untagged interfaces
356 for bridge in bridges:
357 vlan=vlans[bridges.index(bridge)]
358 if vlan != 'tagged':
359 print "vconfig add %s %s" % (trunk,vlan)
360 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan)
361 print "ip link set %s.%s up" % (trunk,vlan)
363 print "ifconfig %s.%s %s netmask %s" % (trunk,management_vlan,management_ip,management_netmask)
364 if management_gw:
365 print "route add default gw %s" % (management_gw)
369 #make_all_system()
371 if len(args) > 0:
372 command = args[0]
373 if command == 'make-drbd':
374 make_drbd()
375 elif command == 'make-domains':
376 make_domains()
377 elif command == 'make-bridges':
378 make_bridges()
379 elif command == 'make-links':
380 make_links()
381 else:
382 show_usage()
383 else:
384 show_usage()