xen-drbd
view xen-drbd-install.py @ 44:cbe58da31273
minifix
author | igor@manas.xt.vpn |
---|---|
date | Tue Sep 23 15:51:04 2008 +0300 (2008-09-23) |
parents | 562d6e064e6f |
children | 01eb86555abb |
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.py (default: network.py)
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(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 %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 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 rsync -a /domU/%s/ /domU/%s/
233 """ % (linux_domains[0], dom)
236 def make_install_packages():
237 for dom in linux_domains:
238 try:
239 print """
240 chroot /domU/%s apt-get install -y --force-yes %s
241 kill $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null
242 sleep 2
243 kill -9 $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null
244 """ % (dom,apt_get_install_table[dom],dom,dom)
245 except:
246 pass
248 def make_etcfstab():
249 i=0
250 for dom in linux_domains:
251 i+=1
252 print """
253 cat <<FSTAB > /domU/%s/etc/fstab
254 proc /proc proc defaults 0 0
255 /dev/hda1 / ext3 defaults,errors=remount-ro 0 1
256 FSTAB
257 """ % (dom)
259 def make_etcnetworkinterfaces():
260 i=0
261 for dom in linux_domains:
262 i+=1
263 print """
264 cat <<INTERFACES > /domU/%s/etc/network/interfaces
265 auto lo eth0
266 iface lo inet loopback
268 iface eth0 inet static
269 address %s
270 netmask 255.255.255.0
271 network %s.0
272 broadcast %s.255
273 gateway %s
274 dns-nameservers %s
275 dns-search %s
276 INTERFACES
277 """ % (dom,ip_address_table[dom],ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
279 def make_etcresolvconf():
280 for dom in linux_domains:
281 print """
282 cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
283 search %s
284 nameserver %s
285 RESOLVCONF
286 """ % (dom,domain_name,ip_nameserver)
288 def make_etchostname():
289 for dom in linux_domains:
290 print """
291 cat <<HOSTNAME > /domU/%s/etc/hostname
292 %s
293 HOSTNAME
294 """ % (dom,dom)
297 def make_etchosts():
298 i=0
299 for dom in linux_domains:
300 i+=1
301 print """
302 cat <<HOSTS > /domU/%s/etc/hosts
303 127.0.0.1 localhost
304 %s %s.%s %s
306 ::1 ip6-localhost ip6-loopback
307 fe00::0 ip6-localnet
308 ff00::0 ip6-mcastprefix
309 ff02::1 ip6-allnodes
310 ff02::2 ip6-allrouters
311 ff02::3 ip6-allhosts
312 HOSTS
313 """ % (dom,ip_address_table[dom],dom,domain_name,dom)
315 def make_rootssh():
316 for dom in linux_domains:
317 print """
318 mkdir -p /domU/%s/home/eb/.ssh/
319 chown -R 1000:1000 /domU/%s/home/eb
320 cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
321 chmod 600 /domU/%s/home/eb/.ssh/authorized_keys
322 """ % (dom,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()
367 print "ifconfig %s 0.0.0.0 down" % (trunk)
369 # tagged interfaces
370 for bridge in bridges:
371 vlan=vlans[bridges.index(bridge)]
372 if vlan == 'tagged':
373 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk)
374 print "ip link set %s up" % (trunk)
376 # untagged interfaces
377 for bridge in bridges:
378 vlan=vlans[bridges.index(bridge)]
379 if vlan != 'tagged':
380 print "vconfig add %s %s" % (trunk,vlan)
381 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan)
382 print "ip link set %s.%s up" % (trunk,vlan)
384 print "ifconfig %s.%s %s netmask %s" % (trunk,management_vlan,management_ip,management_netmask)
385 if management_gw:
386 print "route add default gw %s" % (management_gw)
390 #make_all_system()
392 if len(args) > 0:
393 command = args[0]
394 if command == 'make-drbd':
395 make_drbd()
396 elif command == 'make-domains':
397 make_domains()
398 elif command == 'make-bridges':
399 make_bridges()
400 elif command == 'make-links':
401 make_links()
402 else:
403 show_usage()
404 else:
405 show_usage()