xen-drbd

view xen-drbd-install.py @ 11:f94023de662c

show usage fixed
author igor
date Tue Feb 12 13:50:33 2008 +0200 (2008-02-12)
parents c14b6387ada5
children 6e2436e09983
line source
1 #!/usr/bin/python
3 import sys,re
5 def show_usage():
6 print """
7 Usage:
8 xen-drbd-install <network> <command> (to view)
9 xen-drbd-install <network> <command> | sh -s (to run)
11 <network> is a name of the file, which contains network description
13 Commands:
14 make-drbd
15 make-domains
16 make-bridges
17 make-links
18 """
20 if len(sys.argv) != 3:
21 show_usage()
22 sys.exit(1)
25 network=sys.argv[1]
26 try:
27 exec 'from %s import * ' % (network)
28 except:
29 print "Can't find or interpret module %s with topology description" %(network)
30 sys.exit(1)
32 drbd_base_port=7790
33 meta_disk='/dev/%s/meta'%lvm_vg_name
34 drbd_device={}
36 def make_drbd_hash():
37 for dom in domains:
38 for disk in disk_table[dom]:
39 if disk.find(":") == -1:
40 drbd_device[disk]=dom
41 else:
42 drbd_device[disk.split(':')[0]]=disk.split(':')[1]
44 def make_lvm():
45 print "###################################################"
46 print "# MAKE LVM FOR DOMAINS DRBD DEVICES"
47 print "#"
48 for lvm_pv_name in re.compile('\s+').split(lvm_pv_names):
49 print "pvcreate %s" % (lvm_pv_name)
50 print "vgcreate /dev/%s %s" % (lvm_vg_name, lvm_pv_names)
51 print "lvcreate -L %s -n %s /dev/%s" % (lvm_lv_drbd_meta_size, lvm_lv_drbd_meta_name, lvm_vg_name)
52 for dom in domains:
53 for disk in disk_table[dom]:
54 if disk.find(":") == -1:
55 #if do_print:
56 # print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name)
57 #drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)])
58 drbd_device[disk]=dom
59 else:
60 drbd_device[disk.split(':')[0]]=disk.split(':')[0]
61 print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[1], disk.split(':')[0], lvm_vg_name)
62 #drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])])
63 print ""
66 def make_drbdconf():
67 print "###################################################"
68 print "# MAKE DRBD CONFIG FILE /etc/drbd.conf"
69 print "#"
71 print "cat <<DRBD > /etc/drbd.conf"
74 for dom in domains:
75 for disk in disk_table[dom]:
76 drbd_number=int(re.compile('drbd').sub('',disk.split(':')[0]))
77 resource=disk.split(':')[1]
78 disk="/dev/%s/%s" % (lvm_vg_name,resource)
80 print """
81 resource %s {
82 protocol C;
83 net {
84 allow-two-primaries;
85 after-sb-0pri discard-least-changes;
86 after-sb-1pri call-pri-lost-after-sb;
87 after-sb-2pri call-pri-lost-after-sb;
88 }
89 syncer {
90 rate 5M;
91 }
92 on %s
93 {
94 device /dev/drbd%s;
95 disk %s;
96 address %s:%s;
97 meta-disk %s[%s];
98 }
99 on %s
100 {
101 device /dev/drbd%s;
102 disk %s;
103 address %s:%s;
104 meta-disk %s[%s];
105 }
106 }
108 """ % (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 )
109 print "DRBD"
110 print ""
113 def make_drbdmeta():
114 print "###################################################"
115 print "# MAKE DRBD DEVICES "
116 print "#"
117 print "drbdadm create-md all"
119 def make_drbdprimary():
120 print "###################################################"
121 print "# SET DRBD DEVICES PRIMARY "
122 print "#"
124 print "drbdadm up all"
125 for dom in domains:
126 for disk in disk_table[dom]:
127 print "drbdsetup %s primary -o" % disk.split(':')[0]
130 def make_links():
132 print "###################################################"
133 print "# MAKE SYMBOLIC LINKS TO /dev/drbd* FILES"
134 print "#"
136 # drbd_devices={
137 # 'dev' : 'resource',
138 # }
140 make_drbd_hash()
141 print "mkdir -p /dev/drbd"
142 for dev in drbd_device.keys():
143 resource=drbd_device[dev]
144 print "ln -sf /dev/%s /dev/drbd/%s" %(dev, resource)
146 def make_fs():
148 print "###################################################"
149 print "# MAKE FILESYSTEMS ON THE DRBDs"
150 print "#"
152 for dev in drbd_device.keys():
153 print "mkfs.ext3 %s /dev/%s" %(mkfs_options,dev)
156 def make_mount():
157 print "###################################################"
158 print "# MOUNT U-DOMAINS FILESYSTEMS"
159 print "#"
161 print "mkdir -p /domU"
162 for dom in domains:
163 print "mkdir -p /domU/%s" % (dom)
164 print "mount /dev/drbd/%s /domU/%s" % (dom,dom)
166 def make_umount():
167 for dom in domains:
168 print "umount /domU/%s" % (dom)
170 def make_dns():
171 i=1
172 for dom in domains:
173 print "%s A %s.%s" % (dom, ip_network, i)
174 i+=1
176 def make_dns_reverse():
177 i=1
178 for dom in domains:
179 print "%s A %s.%s." % (i, dom, domain_name)
180 i+=1
182 def make_install_debian():
183 print """
184 debootstrap %s /domU/%s %s
185 chroot /domU/%s apt-get install -y --force-yes %s
186 """ % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install)
188 def make_rsync():
189 for dom in domains[1:]:
190 print """
191 rsync -a /domU/%s/ /domU/%s/
192 """ % (domains[0], dom)
194 def make_etcfstab():
195 i=0
196 for dom in domains:
197 i+=1
198 print """
199 cat <<FSTAB > /domU/%s/etc/fstab
200 proc /proc proc defaults 0 0
201 /dev/hda1 / ext3 defaults,errors=remount-ro 0 1
202 FSTAB
203 """ % (dom)
205 def make_etcnetworkinterfaces():
206 i=0
207 for dom in domains:
208 i+=1
209 print """
210 cat <<INTERFACES > /domU/%s/etc/network/interfaces
211 auto lo eth0
212 iface lo inet loopback
214 iface eth0 inet static
215 address %s
216 netmask 255.255.255.0
217 network %s.0
218 broadcast %s.255
219 gateway %s
220 dns-nameservers %s
221 dns-search %s
222 INTERFACES
223 """ % (dom,ip_address_table[dom],ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
225 def make_etcresolvconf():
226 for dom in domains:
227 print """
228 cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
229 search %s
230 nameserver %s
231 RESOLVCONF
232 """ % (dom,domain_name,ip_nameserver)
234 def make_etchostname():
235 for dom in domains:
236 print """
237 cat <<HOSTNAME > /domU/%s/etc/hostname
238 %s
239 HOSTNAME
240 """ % (dom,dom)
243 def make_etchosts():
244 i=0
245 for dom in domains:
246 i+=1
247 print """
248 cat <<HOSTS > /domU/%s/etc/hosts
249 127.0.0.1 localhost
250 %s %s.%s %s
252 ::1 ip6-localhost ip6-loopback
253 fe00::0 ip6-localnet
254 ff00::0 ip6-mcastprefix
255 ff02::1 ip6-allnodes
256 ff02::2 ip6-allrouters
257 ff02::3 ip6-allhosts
258 HOSTS
259 """ % (dom,ip_address_table[dom],dom,domain_name,dom)
261 def make_rootssh():
262 for dom in domains:
263 print """
264 mkdir -p /domU/%s/root/.ssh/
265 cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
266 chmod 600 /domU/%s/root/.ssh/authorized_keys
267 """ % (dom,dom,dom)
269 def make_l3rc():
270 for dom in domains:
271 print """
272 cat <<EOF > /domU/%s/root/.l3rc
273 l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
274 EOF
275 """ % (dom,dom,domain_name)
278 def make_etc():
279 make_etcfstab()
280 make_etcnetworkinterfaces()
281 make_etcresolvconf()
282 make_etchosts()
283 make_etchostname()
284 make_rootssh()
285 make_l3rc()
287 def make_drbd():
288 make_lvm()
289 make_drbdconf()
290 make_drbdmeta()
292 def make_domains():
293 make_drbdprimary()
294 make_links()
295 make_fs()
296 make_mount()
297 make_install_debian()
298 make_rsync()
299 make_etc()
300 make_umount()
302 def get_management_ip():
303 return management_ip
305 def get_default_gateway():
306 return management_gw
308 def make_bridges():
309 ip=get_management_ip()
310 gw=get_default_gateway()
311 print "ifconfig %s 0.0.0.0" % (trunk)
313 # tagged interfaces
314 for bridge in bridges:
315 vlan=vlans[bridges.index(bridge)]
316 if vlan == 'tagged':
317 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk)
318 print "ip link set %s up" % (trunk)
320 # untagged interfaces
321 for bridge in bridges:
322 vlan=vlans[bridges.index(bridge)]
323 if vlan != 'tagged':
324 print "vconfig add %s %s" % (trunk,vlan)
325 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan)
326 print "ip link set %s.%s up" % (trunk,vlan)
328 print "ifconfig %s.%s %s" % (trunk,management_vlan,management_ip)
329 if management_gw:
330 print "route add default gw %s" % (management_gw)
334 #make_all_system()
336 if len(sys.argv) > 1:
337 command = sys.argv[2]
338 if command == 'make-drbd':
339 make_drbd()
340 elif command == 'make-domains':
341 make_domains()
342 elif command == 'make-bridges':
343 make_bridges()
344 elif command == 'make-links':
345 make_links()
346 else:
347 show_usage()
348 else:
349 show_usage()