xen-drbd
view xen-drbd-install.py @ 35:cbd4d942d059
.py eliminated in script calling
author | igor@manas.xt.vpn |
---|---|
date | Mon Aug 18 21:27:14 2008 +0300 (2008-08-18) |
parents | fc4025aa8287 |
children | 44efea4507ac |
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 drbd_device[disk]=dom
47 else:
48 drbd_device[disk.split(':')[0]]=disk.split(':')[1]
50 def make_lvm():
51 print "###################################################"
52 print "# MAKE LVM FOR DOMAINS DRBD DEVICES"
53 print "#"
54 for lvm_pv_name in re.compile('\s+').split(lvm_pv_names):
55 print "pvcreate %s" % (lvm_pv_name)
56 print "vgcreate /dev/%s %s" % (lvm_vg_name, lvm_pv_names)
57 print "lvcreate -L %s -n %s /dev/%s" % (lvm_lv_drbd_meta_size, lvm_lv_drbd_meta_name, lvm_vg_name)
58 for dom in linux_domains:
59 for disk in disk_table[dom]:
60 if disk.find(":") == -1:
61 #if do_print:
62 # print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name)
63 #drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)])
64 drbd_device[disk]=dom
65 else:
66 drbd_device[disk.split(':')[0]]=disk.split(':')[0]
67 print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[2], disk.split(':')[1], lvm_vg_name)
68 #drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])])
69 print ""
72 def make_drbdconf():
73 print "###################################################"
74 print "# MAKE DRBD CONFIG FILE /etc/drbd.conf"
75 print "#"
77 print "cat <<DRBD > /etc/drbd.conf"
80 for dom in domains:
81 for disk in disk_table[dom]:
82 drbd_number=int(re.compile('drbd').sub('',disk.split(':')[0]))
83 resource=disk.split(':')[1]
84 disk="/dev/%s/%s" % (lvm_vg_name,resource)
86 print """
87 resource %s {
88 protocol C;
89 net {
90 allow-two-primaries;
91 after-sb-0pri discard-least-changes;
92 after-sb-1pri call-pri-lost-after-sb;
93 after-sb-2pri call-pri-lost-after-sb;
94 }
95 syncer {
96 rate 5M;
97 }
98 on %s
99 {
100 device /dev/drbd%s;
101 disk %s;
102 address %s:%s;
103 meta-disk %s[%s];
104 }
105 on %s
106 {
107 device /dev/drbd%s;
108 disk %s;
109 address %s:%s;
110 meta-disk %s[%s];
111 }
112 }
114 """ % (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 )
115 print "DRBD"
116 print ""
119 def make_drbdmeta():
120 print "###################################################"
121 print "# MAKE DRBD DEVICES "
122 print "#"
123 print "yes yes | drbdadm create-md all"
125 def make_drbdprimary():
126 print "###################################################"
127 print "# SET DRBD DEVICES PRIMARY "
128 print "#"
130 print "drbdadm up all"
131 for dom in domains:
132 for disk in disk_table[dom]:
133 print "drbdsetup %s primary -o" % disk.split(':')[0]
136 def make_links():
138 print "###################################################"
139 print "# MAKE SYMBOLIC LINKS TO /dev/drbd* FILES"
140 print "#"
142 # drbd_devices={
143 # 'dev' : 'resource',
144 # }
146 make_drbd_hash()
147 print "mkdir -p /dev/drbd"
148 for dev in drbd_device.keys():
149 resource=drbd_device[dev]
150 print "ln -sf /dev/%s /dev/drbd/%s" %(dev, resource)
152 def make_fs():
154 print "###################################################"
155 print "# MAKE FILESYSTEMS ON THE DRBDs"
156 print "#"
158 for dev in drbd_device.keys():
159 print "mkfs.ext3 %s /dev/%s" %(mkfs_options,dev)
162 def make_mount():
163 print "###################################################"
164 print "# MOUNT U-DOMAINS FILESYSTEMS"
165 print "#"
167 print "mkdir -p /domU"
168 for dom in linux_domains:
169 print "mkdir -p /domU/%s" % (dom)
170 print "mount /dev/drbd/%s /domU/%s" % (dom,dom)
172 def make_umount():
173 for dom in linux_domains:
174 print "umount /domU/%s" % (dom)
176 def make_dns():
177 i=1
178 for dom in linux_domains:
179 print "%s A %s.%s" % (dom, ip_network, i)
180 i+=1
182 def make_dns_reverse():
183 i=1
184 for dom in linux_domains:
185 print "%s A %s.%s." % (i, dom, domain_name)
186 i+=1
188 def make_install_debian():
189 print """
190 apt-get install debootstrap
191 debootstrap %s /domU/%s %s
192 chroot /domU/%s apt-get install -y --force-yes %s
193 """ % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install)
195 def make_rsync():
196 for dom in domains[1:]:
197 print """
198 rsync -a /domU/%s/ /domU/%s/
199 """ % (domains[0], dom)
201 def make_etcfstab():
202 i=0
203 for dom in linux_domains:
204 i+=1
205 print """
206 cat <<FSTAB > /domU/%s/etc/fstab
207 proc /proc proc defaults 0 0
208 /dev/hda1 / ext3 defaults,errors=remount-ro 0 1
209 FSTAB
210 """ % (dom)
212 def make_etcnetworkinterfaces():
213 i=0
214 for dom in linux_domains:
215 i+=1
216 print """
217 cat <<INTERFACES > /domU/%s/etc/network/interfaces
218 auto lo eth0
219 iface lo inet loopback
221 iface eth0 inet static
222 address %s
223 netmask 255.255.255.0
224 network %s.0
225 broadcast %s.255
226 gateway %s
227 dns-nameservers %s
228 dns-search %s
229 INTERFACES
230 """ % (dom,ip_address_table[dom],ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
232 def make_etcresolvconf():
233 for dom in linux_domains:
234 print """
235 cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
236 search %s
237 nameserver %s
238 RESOLVCONF
239 """ % (dom,domain_name,ip_nameserver)
241 def make_etchostname():
242 for dom in linux_domains:
243 print """
244 cat <<HOSTNAME > /domU/%s/etc/hostname
245 %s
246 HOSTNAME
247 """ % (dom,dom)
250 def make_etchosts():
251 i=0
252 for dom in linux_domains:
253 i+=1
254 print """
255 cat <<HOSTS > /domU/%s/etc/hosts
256 127.0.0.1 localhost
257 %s %s.%s %s
259 ::1 ip6-localhost ip6-loopback
260 fe00::0 ip6-localnet
261 ff00::0 ip6-mcastprefix
262 ff02::1 ip6-allnodes
263 ff02::2 ip6-allrouters
264 ff02::3 ip6-allhosts
265 HOSTS
266 """ % (dom,ip_address_table[dom],dom,domain_name,dom)
268 def make_rootssh():
269 for dom in linux_domains:
270 print """
271 mkdir -p /domU/%s/home/eb/.ssh/
272 chown -R 1000:1000 /domU/%s/home/eb
273 cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
274 chmod 600 /domU/%s/home/eb/.ssh/authorized_keys
275 """ % (dom,dom,dom,dom)
277 def make_l3rc():
278 for dom in linux_domains:
279 print """
280 cat <<EOF > /domU/%s/root/.l3rc
281 l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
282 EOF
283 """ % (dom,dom,domain_name)
286 def make_etc():
287 make_etcfstab()
288 make_etcnetworkinterfaces()
289 make_etcresolvconf()
290 make_etchosts()
291 make_etchostname()
292 make_rootssh()
293 #make_l3rc()
295 def make_drbd():
296 make_lvm()
297 make_drbdconf()
298 make_drbdmeta()
300 def make_domains():
301 make_drbdprimary()
302 make_links()
303 make_fs()
304 make_mount()
305 make_install_debian()
306 make_rsync()
307 make_etc()
308 make_umount()
310 def get_management_ip():
311 return management_ip
313 def get_default_gateway():
314 return management_gw
316 def make_bridges():
317 ip=get_management_ip()
318 gw=get_default_gateway()
319 print "ifconfig %s 0.0.0.0 down" % (trunk)
321 # tagged interfaces
322 for bridge in bridges:
323 vlan=vlans[bridges.index(bridge)]
324 if vlan == 'tagged':
325 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk)
326 print "ip link set %s up" % (trunk)
328 # untagged interfaces
329 for bridge in bridges:
330 vlan=vlans[bridges.index(bridge)]
331 if vlan != 'tagged':
332 print "vconfig add %s %s" % (trunk,vlan)
333 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan)
334 print "ip link set %s.%s up" % (trunk,vlan)
336 print "ifconfig %s.%s %s netmask %s" % (trunk,management_vlan,management_ip,management_netmask)
337 if management_gw:
338 print "route add default gw %s" % (management_gw)
342 #make_all_system()
344 if len(sys.argv) > 1:
345 command = sys.argv[2]
346 if command == 'make-drbd':
347 make_drbd()
348 elif command == 'make-domains':
349 make_domains()
350 elif command == 'make-bridges':
351 make_bridges()
352 elif command == 'make-links':
353 make_links()
354 else:
355 show_usage()
356 else:
357 show_usage()