xen-drbd

view xen-drbd-install.py @ 56:14e75a085db0

minifix
author root@linux10
date Fri Oct 10 23:13:58 2008 +0000 (2008-10-10)
parents 8afb0999f562
children b8f58a090b2f
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 (default: network)
23 """
27 network='network'
28 try:
29 opts, args = getopt.getopt(sys.argv[1:], "hn:", ["help", "network="])
30 except getopt.GetoptError, err:
31 print str(err)
32 usage()
33 sys.exit(2)
35 for o, a in opts:
36 if o in ("-h", "--help"):
37 show_usage()
38 sys.exit()
39 elif o in ("-n", "--network"):
40 network = a
41 else:
42 assert False, "unhandled option"
44 if len(args) != 1:
45 show_usage()
46 sys.exit(1)
48 try:
49 execfile('/etc/xen/'+network)
50 except IOError:
51 print "Network configuration file <%s> not found " % network
52 if network == 'network':
53 print "You can use -n option to specify network filename"
54 sys.exit(2)
55 except NameError:
56 domain=domains[0]
57 execfile('/etc/xen/'+network)
60 drbd_base_port=7790
61 meta_disk='/dev/%s/meta'%lvm_vg_name
62 drbd_device={}
64 linux_domains=[]
65 for dom in domains:
66 if domain_types[domains.index(dom)] == 'linux':
67 linux_domains.append(dom)
69 def make_drbd_hash():
70 for dom in domains:
71 for disk in disk_table[dom]:
72 if disk.find(":") == -1:
73 if disk.find("=") == -1:
74 drbd_device[disk]=dom
75 else:
76 drbd_device[disk.split('=')[0]]=dom
77 else:
78 disk0=disk.split(':')[0]
79 drbd_device[disk0.split('=')[0]]=disk.split(':')[1]
81 def make_lvm():
82 print "###################################################"
83 print "# MAKE LVM FOR DOMAINS DRBD DEVICES"
84 print "#"
85 for lvm_pv_name in re.compile('\s+').split(lvm_pv_names):
86 print "pvcreate %s" % (lvm_pv_name)
87 print "vgcreate /dev/%s %s" % (lvm_vg_name, lvm_pv_names)
88 print "lvcreate -L %s -n %s /dev/%s" % (lvm_lv_drbd_meta_size, lvm_lv_drbd_meta_name, lvm_vg_name)
89 for dom in linux_domains:
90 for disk in disk_table[dom]:
91 if disk.find(":") == -1:
92 #if do_print:
93 # print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name)
94 #drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)])
95 drbd_device[disk]=dom
96 else:
97 drbd_device[disk.split(':')[0]]=disk.split(':')[0]
98 print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[2], disk.split(':')[1], lvm_vg_name)
99 #drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])])
100 print ""
103 def make_drbdconf():
104 print "###################################################"
105 print "# MAKE DRBD CONFIG FILE /etc/drbd.conf"
106 print "#"
108 print "cat <<DRBD > /etc/drbd.conf"
111 for dom in domains:
112 for disk in disk_table[dom]:
113 drbd_number=int(re.compile('drbd').sub('',(disk.split(':')[0]).split('=')[0] ))
114 resource=disk.split(':')[1]
115 disk="/dev/%s/%s" % (lvm_vg_name,resource)
117 print """
118 resource %s {
119 protocol C;
120 net {
121 allow-two-primaries;
122 after-sb-0pri discard-least-changes;
123 after-sb-1pri call-pri-lost-after-sb;
124 after-sb-2pri call-pri-lost-after-sb;
125 }
126 syncer {
127 rate 5M;
128 }
129 on %s
130 {
131 device /dev/drbd%s;
132 disk %s;
133 address %s:%s;
134 meta-disk %s[%s];
135 }
136 on %s
137 {
138 device /dev/drbd%s;
139 disk %s;
140 address %s:%s;
141 meta-disk %s[%s];
142 }
143 }
145 """ % (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 )
146 print "DRBD"
147 print ""
150 def make_drbdmeta():
151 print "###################################################"
152 print "# MAKE DRBD DEVICES "
153 print "#"
154 print "yes yes | drbdadm create-md all"
156 def make_drbdprimary():
157 print "###################################################"
158 print "# SET DRBD DEVICES PRIMARY "
159 print "#"
161 print "drbdadm up all"
162 for dom in domains:
163 for disk in disk_table[dom]:
164 print "drbdsetup /dev/%s primary -o" % disk.split(':')[0]
167 def make_links():
169 print "###################################################"
170 print "# MAKE SYMBOLIC LINKS TO /dev/drbd* FILES"
171 print "#"
173 # drbd_devices={
174 # 'dev' : 'resource',
175 # }
177 make_drbd_hash()
178 print "mkdir -p /dev/drbd"
179 for dev in drbd_device.keys():
180 resource=drbd_device[dev]
181 print "ln -sf /dev/%s /dev/drbd/%s" %(dev, resource)
183 def make_fs():
185 print "###################################################"
186 print "# MAKE FILESYSTEMS ON THE DRBDs"
187 print "#"
189 for dev in drbd_device.keys():
190 print "mkfs.ext3 %s /dev/%s" %(mkfs_options,dev)
193 def make_mount():
194 print "###################################################"
195 print "# MOUNT U-DOMAINS FILESYSTEMS"
196 print "#"
198 print "mkdir -p /domU"
199 for dom in linux_domains:
200 print "mkdir -p /domU/%s" % (dom)
201 print "mount /dev/drbd/%s /domU/%s" % (dom,dom)
203 def make_umount():
204 for dom in linux_domains:
205 print "umount /domU/%s" % (dom)
207 def make_dns():
208 i=1
209 for dom in linux_domains:
210 print "%s A %s.%s" % (dom, ip_network, i)
211 i+=1
213 def make_dns_reverse():
214 i=1
215 for dom in linux_domains:
216 print "%s A %s.%s." % (i, dom, domain_name)
217 i+=1
219 def make_install_debian():
220 print """
221 apt-get install debootstrap lsof
222 debootstrap %s /domU/%s %s
223 LANG=C chroot /domU/%s apt-get install -y --force-yes %s
224 kill $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null
225 sleep 2
226 kill -9 $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null
227 """ % (debian_release,linux_domains[0],debian_mirror,linux_domains[0],apt_get_install,linux_domains[0],linux_domains[0])
229 def make_rsync():
230 for dom in linux_domains[1:]:
231 print """
232 echo -n Doing rsync for %s...
233 rsync -a /domU/%s/ /domU/%s/ && echo Done
234 """ % (dom, linux_domains[0], dom)
237 def make_install_packages():
238 for dom in linux_domains:
239 try:
240 print """
241 LANG=C chroot /domU/%s apt-get install -y --force-yes %s
242 kill $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null
243 sleep 2
244 kill -9 $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null
245 """ % (dom,apt_get_install_table[dom],dom,dom)
246 except:
247 pass
249 def make_etcfstab():
250 i=0
251 for dom in linux_domains:
252 i+=1
253 print """
254 cat <<FSTAB > /domU/%s/etc/fstab
255 proc /proc proc defaults 0 0
256 /dev/hda1 / ext3 defaults,errors=remount-ro 0 1
257 FSTAB
258 """ % (dom)
260 def make_etcnetworkinterfaces():
261 i=0
262 for dom in linux_domains:
263 i+=1
264 print """
265 cat <<INTERFACES > /domU/%s/etc/network/interfaces
266 auto lo eth0
267 iface lo inet loopback
269 iface eth0 inet static
270 address %s
271 netmask 255.255.255.0
272 network %s.0
273 broadcast %s.255
274 gateway %s
275 dns-search %s
276 dns-nameservers %s
277 INTERFACES
278 """ % (dom,ip_address_table[dom],ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
280 def make_etcresolvconf():
281 for dom in linux_domains:
282 print """
283 cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
284 search %s
285 nameserver %s
286 RESOLVCONF
287 """ % (dom,domain_name,ip_nameserver)
289 def make_etchostname():
290 for dom in linux_domains:
291 print """
292 cat <<HOSTNAME > /domU/%s/etc/hostname
293 %s
294 HOSTNAME
295 """ % (dom,dom)
298 def make_etchosts():
299 i=0
300 for dom in linux_domains:
301 i+=1
302 print """
303 cat <<HOSTS > /domU/%s/etc/hosts
304 127.0.0.1 localhost
305 %s %s.%s %s
307 ::1 ip6-localhost ip6-loopback
308 fe00::0 ip6-localnet
309 ff00::0 ip6-mcastprefix
310 ff02::1 ip6-allnodes
311 ff02::2 ip6-allrouters
312 ff02::3 ip6-allhosts
313 HOSTS
314 """ % (dom,ip_address_table[dom],dom,domain_name,dom)
316 def make_rootssh():
317 for dom in linux_domains:
318 print """
319 mkdir -p /domU/%s/root/.ssh/
320 cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
321 chmod 600 /domU/%s/root/.ssh/authorized_keys
322 """ % (dom,dom,dom)
324 def make_l3rc():
325 for dom in linux_domains:
326 print """
327 cat <<EOF > /domU/%s/root/.l3rc
328 l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
329 EOF
330 """ % (dom,dom,domain_name)
333 def make_etc():
334 make_etcfstab()
335 make_etcnetworkinterfaces()
336 make_etcresolvconf()
337 make_etchosts()
338 make_etchostname()
339 make_rootssh()
340 #make_l3rc()
342 def make_drbd():
343 make_lvm()
344 make_drbdconf()
345 make_drbdmeta()
347 def make_domains():
348 make_drbdprimary()
349 make_links()
350 make_fs()
351 make_mount()
352 make_install_debian()
353 make_rsync()
354 make_install_packages()
355 make_etc()
356 make_umount()
358 def get_management_ip():
359 return management_ip
361 def get_default_gateway():
362 return management_gw
364 def make_bridges():
365 ip=get_management_ip()
366 gw=get_default_gateway()
368 trunk_bridge=trunk
370 # tagged interfaces
371 for bridge in bridges:
372 vlan=vlans[bridges.index(bridge)]
373 if vlan == 'tagged':
374 if trunk_bridge != trunk:
375 print "\n=====\nERROR: No more than one tagged bridge allowed.\nYou have defined the tagged bridge already, %s " % trunk_bridge
376 sys.exit(1)
377 trunk_bridge=bridge
378 print "ifconfig %s 0.0.0.0 down" % (trunk)
379 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk)
380 print "ip link set %s up" % (trunk_bridge)
382 if trunk_bridge==trunk:
383 print "ifconfig %s up" % (trunk)
385 # untagged interfaces
386 for bridge in bridges:
387 vlan=vlans[bridges.index(bridge)]
388 if vlan != 'tagged':
389 print "vconfig add %s %s" % (trunk_bridge,vlan)
390 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk_bridge,vlan)
391 print "ip link set %s up" % (bridge)
393 print "ifconfig %s %s netmask %s" % (management_interface,management_ip,management_netmask)
394 if management_gw:
395 print "route add default gw %s" % (management_gw)
399 #make_all_system()
401 if len(args) > 0:
402 command = args[0]
403 if command == 'make-drbd':
404 make_drbd()
405 elif command == 'make-domains':
406 make_domains()
407 elif command == 'make-bridges':
408 make_bridges()
409 elif command == 'make-links':
410 make_links()
411 else:
412 show_usage()
413 else:
414 show_usage()