xen-drbd
view xen-drbd-install.py @ 57:b8f58a090b2f
additional_debian_releases
author | Igor Chubin <igor@chub.in> |
---|---|
date | Tue Oct 14 10:06:50 2008 +0300 (2008-10-14) |
parents | 14e75a085db0 |
children | ae3cd87f8837 |
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 for i in %s
224 do
225 echo deb %s $i main contrib non-free >> /domU/%s/etc/apt/sources.list
226 done
227 LANG=C chroot apt-get update
228 LANG=C chroot /domU/%s apt-get install -y --force-yes %s
229 kill $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null
230 sleep 2
231 kill -9 $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null
232 """ % (debian_release,linux_domains[0],debian_mirror,debian_additional_releases,debian_mirror,linux_domains[0],linux_domains[0],apt_get_install,linux_domains[0],linux_domains[0])
234 def make_rsync():
235 for dom in linux_domains[1:]:
236 print """
237 echo -n Doing rsync for %s...
238 rsync -a /domU/%s/ /domU/%s/ && echo Done
239 """ % (dom, linux_domains[0], dom)
242 def make_install_packages():
243 for dom in linux_domains:
244 try:
245 print """
246 LANG=C chroot /domU/%s apt-get install -y --force-yes %s
247 kill $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null
248 sleep 2
249 kill -9 $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null
250 """ % (dom,apt_get_install_table[dom],dom,dom)
251 except:
252 pass
254 def make_etcfstab():
255 i=0
256 for dom in linux_domains:
257 i+=1
258 print """
259 cat <<FSTAB > /domU/%s/etc/fstab
260 proc /proc proc defaults 0 0
261 /dev/hda1 / ext3 defaults,errors=remount-ro 0 1
262 FSTAB
263 """ % (dom)
265 def make_etcnetworkinterfaces():
266 i=0
267 for dom in linux_domains:
268 i+=1
269 print """
270 cat <<INTERFACES > /domU/%s/etc/network/interfaces
271 auto lo eth0
272 iface lo inet loopback
274 iface eth0 inet static
275 address %s
276 netmask 255.255.255.0
277 network %s.0
278 broadcast %s.255
279 gateway %s
280 dns-search %s
281 dns-nameservers %s
282 INTERFACES
283 """ % (dom,ip_address_table[dom],ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
285 def make_etcresolvconf():
286 for dom in linux_domains:
287 print """
288 cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
289 search %s
290 nameserver %s
291 RESOLVCONF
292 """ % (dom,domain_name,ip_nameserver)
294 def make_etchostname():
295 for dom in linux_domains:
296 print """
297 cat <<HOSTNAME > /domU/%s/etc/hostname
298 %s
299 HOSTNAME
300 """ % (dom,dom)
303 def make_etchosts():
304 i=0
305 for dom in linux_domains:
306 i+=1
307 print """
308 cat <<HOSTS > /domU/%s/etc/hosts
309 127.0.0.1 localhost
310 %s %s.%s %s
312 ::1 ip6-localhost ip6-loopback
313 fe00::0 ip6-localnet
314 ff00::0 ip6-mcastprefix
315 ff02::1 ip6-allnodes
316 ff02::2 ip6-allrouters
317 ff02::3 ip6-allhosts
318 HOSTS
319 """ % (dom,ip_address_table[dom],dom,domain_name,dom)
321 def make_rootssh():
322 for dom in linux_domains:
323 print """
324 mkdir -p /domU/%s/root/.ssh/
325 cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
326 chmod 600 /domU/%s/root/.ssh/authorized_keys
327 """ % (dom,dom,dom)
329 def make_l3rc():
330 for dom in linux_domains:
331 print """
332 cat <<EOF > /domU/%s/root/.l3rc
333 l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
334 EOF
335 """ % (dom,dom,domain_name)
338 def make_etc():
339 make_etcfstab()
340 make_etcnetworkinterfaces()
341 make_etcresolvconf()
342 make_etchosts()
343 make_etchostname()
344 make_rootssh()
345 #make_l3rc()
347 def make_drbd():
348 make_lvm()
349 make_drbdconf()
350 make_drbdmeta()
352 def make_domains():
353 make_drbdprimary()
354 make_links()
355 make_fs()
356 make_mount()
357 make_install_debian()
358 make_rsync()
359 make_install_packages()
360 make_etc()
361 make_umount()
363 def get_management_ip():
364 return management_ip
366 def get_default_gateway():
367 return management_gw
369 def make_bridges():
370 ip=get_management_ip()
371 gw=get_default_gateway()
373 trunk_bridge=trunk
375 # tagged interfaces
376 for bridge in bridges:
377 vlan=vlans[bridges.index(bridge)]
378 if vlan == 'tagged':
379 if trunk_bridge != trunk:
380 print "\n=====\nERROR: No more than one tagged bridge allowed.\nYou have defined the tagged bridge already, %s " % trunk_bridge
381 sys.exit(1)
382 trunk_bridge=bridge
383 print "ifconfig %s 0.0.0.0 down" % (trunk)
384 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk)
385 print "ip link set %s up" % (trunk_bridge)
387 if trunk_bridge==trunk:
388 print "ifconfig %s up" % (trunk)
390 # untagged interfaces
391 for bridge in bridges:
392 vlan=vlans[bridges.index(bridge)]
393 if vlan != 'tagged':
394 print "vconfig add %s %s" % (trunk_bridge,vlan)
395 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk_bridge,vlan)
396 print "ip link set %s up" % (bridge)
398 print "ifconfig %s %s netmask %s" % (management_interface,management_ip,management_netmask)
399 if management_gw:
400 print "route add default gw %s" % (management_gw)
404 #make_all_system()
406 if len(args) > 0:
407 command = args[0]
408 if command == 'make-drbd':
409 make_drbd()
410 elif command == 'make-domains':
411 make_domains()
412 elif command == 'make-bridges':
413 make_bridges()
414 elif command == 'make-links':
415 make_links()
416 else:
417 show_usage()
418 else:
419 show_usage()