xen-drbd
view xen-drbd-install.py @ 36:44efea4507ac
Virtual disk name can be secified after = sign (e.g.: drbd1=sda1:dns:2G )
author | igor@manas.xt.vpn |
---|---|
date | Sat Aug 30 11:14:50 2008 +0300 (2008-08-30) |
parents | 85165c844ac9 |
children | e80bdf496aa2 |
line source
1 #!/usr/bin/python
3 import sys,re
4 sys.path.append('/etc/xen')
6 def show_usage():
7 print """
8 Usage:
9 xen-drbd-install <network> <command> (to view)
10 xen-drbd-install <network> <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
19 """
21 if len(sys.argv) != 3:
22 show_usage()
23 sys.exit(1)
26 network=sys.argv[1]
27 try:
28 exec 'from %s import * ' % (network)
29 except ImportError:
30 print "Can't find or interpret module %s with topology description" %(network)
31 sys.exit(1)
33 drbd_base_port=7790
34 meta_disk='/dev/%s/meta'%lvm_vg_name
35 drbd_device={}
37 linux_domains=[]
38 for dom in domains:
39 if domain_types[domains.index(dom)] == 'linux':
40 linux_domains.append(dom)
42 def make_drbd_hash():
43 for dom in domains:
44 for disk in disk_table[dom]:
45 if disk.find(":") == -1:
46 if disk.find("=") == -1:
47 drbd_device[disk]=dom
48 else:
49 drbd_device[disk.split('=')[0]]=dom
50 else:
51 disk0=disk.split(':')[0]
52 drbd_device[disk0.split('=')[0]]=disk.split(':')[1]
54 def make_lvm():
55 print "###################################################"
56 print "# MAKE LVM FOR DOMAINS DRBD DEVICES"
57 print "#"
58 for lvm_pv_name in re.compile('\s+').split(lvm_pv_names):
59 print "pvcreate %s" % (lvm_pv_name)
60 print "vgcreate /dev/%s %s" % (lvm_vg_name, lvm_pv_names)
61 print "lvcreate -L %s -n %s /dev/%s" % (lvm_lv_drbd_meta_size, lvm_lv_drbd_meta_name, lvm_vg_name)
62 for dom in linux_domains:
63 for disk in disk_table[dom]:
64 if disk.find(":") == -1:
65 #if do_print:
66 # print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name)
67 #drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)])
68 drbd_device[disk]=dom
69 else:
70 drbd_device[disk.split(':')[0]]=disk.split(':')[0]
71 print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[2], disk.split(':')[1], lvm_vg_name)
72 #drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])])
73 print ""
76 def make_drbdconf():
77 print "###################################################"
78 print "# MAKE DRBD CONFIG FILE /etc/drbd.conf"
79 print "#"
81 print "cat <<DRBD > /etc/drbd.conf"
84 for dom in domains:
85 for disk in disk_table[dom]:
86 drbd_number=int(re.compile('drbd').sub('',disk.split(':')[0]))
87 resource=disk.split(':')[1]
88 disk="/dev/%s/%s" % (lvm_vg_name,resource)
90 print """
91 resource %s {
92 protocol C;
93 net {
94 allow-two-primaries;
95 after-sb-0pri discard-least-changes;
96 after-sb-1pri call-pri-lost-after-sb;
97 after-sb-2pri call-pri-lost-after-sb;
98 }
99 syncer {
100 rate 5M;
101 }
102 on %s
103 {
104 device /dev/drbd%s;
105 disk %s;
106 address %s:%s;
107 meta-disk %s[%s];
108 }
109 on %s
110 {
111 device /dev/drbd%s;
112 disk %s;
113 address %s:%s;
114 meta-disk %s[%s];
115 }
116 }
118 """ % (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 )
119 print "DRBD"
120 print ""
123 def make_drbdmeta():
124 print "###################################################"
125 print "# MAKE DRBD DEVICES "
126 print "#"
127 print "yes yes | drbdadm create-md all"
129 def make_drbdprimary():
130 print "###################################################"
131 print "# SET DRBD DEVICES PRIMARY "
132 print "#"
134 print "drbdadm up all"
135 for dom in domains:
136 for disk in disk_table[dom]:
137 print "drbdsetup %s primary -o" % disk.split(':')[0]
140 def make_links():
142 print "###################################################"
143 print "# MAKE SYMBOLIC LINKS TO /dev/drbd* FILES"
144 print "#"
146 # drbd_devices={
147 # 'dev' : 'resource',
148 # }
150 make_drbd_hash()
151 print "mkdir -p /dev/drbd"
152 for dev in drbd_device.keys():
153 resource=drbd_device[dev]
154 print "ln -sf /dev/%s /dev/drbd/%s" %(dev, resource)
156 def make_fs():
158 print "###################################################"
159 print "# MAKE FILESYSTEMS ON THE DRBDs"
160 print "#"
162 for dev in drbd_device.keys():
163 print "mkfs.ext3 %s /dev/%s" %(mkfs_options,dev)
166 def make_mount():
167 print "###################################################"
168 print "# MOUNT U-DOMAINS FILESYSTEMS"
169 print "#"
171 print "mkdir -p /domU"
172 for dom in linux_domains:
173 print "mkdir -p /domU/%s" % (dom)
174 print "mount /dev/drbd/%s /domU/%s" % (dom,dom)
176 def make_umount():
177 for dom in linux_domains:
178 print "umount /domU/%s" % (dom)
180 def make_dns():
181 i=1
182 for dom in linux_domains:
183 print "%s A %s.%s" % (dom, ip_network, i)
184 i+=1
186 def make_dns_reverse():
187 i=1
188 for dom in linux_domains:
189 print "%s A %s.%s." % (i, dom, domain_name)
190 i+=1
192 def make_install_debian():
193 print """
194 apt-get install debootstrap
195 debootstrap %s /domU/%s %s
196 chroot /domU/%s apt-get install -y --force-yes %s
197 """ % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install)
199 def make_rsync():
200 for dom in domains[1:]:
201 print """
202 rsync -a /domU/%s/ /domU/%s/
203 """ % (domains[0], dom)
205 def make_etcfstab():
206 i=0
207 for dom in linux_domains:
208 i+=1
209 print """
210 cat <<FSTAB > /domU/%s/etc/fstab
211 proc /proc proc defaults 0 0
212 /dev/hda1 / ext3 defaults,errors=remount-ro 0 1
213 FSTAB
214 """ % (dom)
216 def make_etcnetworkinterfaces():
217 i=0
218 for dom in linux_domains:
219 i+=1
220 print """
221 cat <<INTERFACES > /domU/%s/etc/network/interfaces
222 auto lo eth0
223 iface lo inet loopback
225 iface eth0 inet static
226 address %s
227 netmask 255.255.255.0
228 network %s.0
229 broadcast %s.255
230 gateway %s
231 dns-nameservers %s
232 dns-search %s
233 INTERFACES
234 """ % (dom,ip_address_table[dom],ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
236 def make_etcresolvconf():
237 for dom in linux_domains:
238 print """
239 cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
240 search %s
241 nameserver %s
242 RESOLVCONF
243 """ % (dom,domain_name,ip_nameserver)
245 def make_etchostname():
246 for dom in linux_domains:
247 print """
248 cat <<HOSTNAME > /domU/%s/etc/hostname
249 %s
250 HOSTNAME
251 """ % (dom,dom)
254 def make_etchosts():
255 i=0
256 for dom in linux_domains:
257 i+=1
258 print """
259 cat <<HOSTS > /domU/%s/etc/hosts
260 127.0.0.1 localhost
261 %s %s.%s %s
263 ::1 ip6-localhost ip6-loopback
264 fe00::0 ip6-localnet
265 ff00::0 ip6-mcastprefix
266 ff02::1 ip6-allnodes
267 ff02::2 ip6-allrouters
268 ff02::3 ip6-allhosts
269 HOSTS
270 """ % (dom,ip_address_table[dom],dom,domain_name,dom)
272 def make_rootssh():
273 for dom in linux_domains:
274 print """
275 mkdir -p /domU/%s/home/eb/.ssh/
276 chown -R 1000:1000 /domU/%s/home/eb
277 cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
278 chmod 600 /domU/%s/home/eb/.ssh/authorized_keys
279 """ % (dom,dom,dom,dom)
281 def make_l3rc():
282 for dom in linux_domains:
283 print """
284 cat <<EOF > /domU/%s/root/.l3rc
285 l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
286 EOF
287 """ % (dom,dom,domain_name)
290 def make_etc():
291 make_etcfstab()
292 make_etcnetworkinterfaces()
293 make_etcresolvconf()
294 make_etchosts()
295 make_etchostname()
296 make_rootssh()
297 #make_l3rc()
299 def make_drbd():
300 make_lvm()
301 make_drbdconf()
302 make_drbdmeta()
304 def make_domains():
305 make_drbdprimary()
306 make_links()
307 make_fs()
308 make_mount()
309 make_install_debian()
310 make_rsync()
311 make_etc()
312 make_umount()
314 def get_management_ip():
315 return management_ip
317 def get_default_gateway():
318 return management_gw
320 def make_bridges():
321 ip=get_management_ip()
322 gw=get_default_gateway()
323 print "ifconfig %s 0.0.0.0 down" % (trunk)
325 # tagged interfaces
326 for bridge in bridges:
327 vlan=vlans[bridges.index(bridge)]
328 if vlan == 'tagged':
329 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk)
330 print "ip link set %s up" % (trunk)
332 # untagged interfaces
333 for bridge in bridges:
334 vlan=vlans[bridges.index(bridge)]
335 if vlan != 'tagged':
336 print "vconfig add %s %s" % (trunk,vlan)
337 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan)
338 print "ip link set %s.%s up" % (trunk,vlan)
340 print "ifconfig %s.%s %s netmask %s" % (trunk,management_vlan,management_ip,management_netmask)
341 if management_gw:
342 print "route add default gw %s" % (management_gw)
346 #make_all_system()
348 if len(sys.argv) > 1:
349 command = sys.argv[2]
350 if command == 'make-drbd':
351 make_drbd()
352 elif command == 'make-domains':
353 make_domains()
354 elif command == 'make-bridges':
355 make_bridges()
356 elif command == 'make-links':
357 make_links()
358 else:
359 show_usage()
360 else:
361 show_usage()