xen-drbd

view xen-drbd-install.py @ 17:866525165db8

debootstrap fix
author igor
date Tue Feb 12 16:52:24 2008 +0200 (2008-02-12)
parents dc2a2305e196
children 1a20c33686ba
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(':')[2], disk.split(':')[1], 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 "yes yes | 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 apt-get install debootstrap
185 debootstrap %s /domU/%s %s
186 chroot /domU/%s apt-get install -y --force-yes %s
187 """ % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install)
189 def make_rsync():
190 for dom in domains[1:]:
191 print """
192 rsync -a /domU/%s/ /domU/%s/
193 """ % (domains[0], dom)
195 def make_etcfstab():
196 i=0
197 for dom in domains:
198 i+=1
199 print """
200 cat <<FSTAB > /domU/%s/etc/fstab
201 proc /proc proc defaults 0 0
202 /dev/hda1 / ext3 defaults,errors=remount-ro 0 1
203 FSTAB
204 """ % (dom)
206 def make_etcnetworkinterfaces():
207 i=0
208 for dom in domains:
209 i+=1
210 print """
211 cat <<INTERFACES > /domU/%s/etc/network/interfaces
212 auto lo eth0
213 iface lo inet loopback
215 iface eth0 inet static
216 address %s
217 netmask 255.255.255.0
218 network %s.0
219 broadcast %s.255
220 gateway %s
221 dns-nameservers %s
222 dns-search %s
223 INTERFACES
224 """ % (dom,ip_address_table[dom],ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
226 def make_etcresolvconf():
227 for dom in domains:
228 print """
229 cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
230 search %s
231 nameserver %s
232 RESOLVCONF
233 """ % (dom,domain_name,ip_nameserver)
235 def make_etchostname():
236 for dom in domains:
237 print """
238 cat <<HOSTNAME > /domU/%s/etc/hostname
239 %s
240 HOSTNAME
241 """ % (dom,dom)
244 def make_etchosts():
245 i=0
246 for dom in domains:
247 i+=1
248 print """
249 cat <<HOSTS > /domU/%s/etc/hosts
250 127.0.0.1 localhost
251 %s %s.%s %s
253 ::1 ip6-localhost ip6-loopback
254 fe00::0 ip6-localnet
255 ff00::0 ip6-mcastprefix
256 ff02::1 ip6-allnodes
257 ff02::2 ip6-allrouters
258 ff02::3 ip6-allhosts
259 HOSTS
260 """ % (dom,ip_address_table[dom],dom,domain_name,dom)
262 def make_rootssh():
263 for dom in domains:
264 print """
265 mkdir -p /domU/%s/home/eb/.ssh/
266 chown -R 1000:1000 /domU/%s/home/eb
267 cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
268 chmod 600 /domU/%s/home/eb/.ssh/authorized_keys
269 """ % (dom,dom,dom,dom)
271 def make_l3rc():
272 for dom in domains:
273 print """
274 cat <<EOF > /domU/%s/root/.l3rc
275 l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
276 EOF
277 """ % (dom,dom,domain_name)
280 def make_etc():
281 make_etcfstab()
282 make_etcnetworkinterfaces()
283 make_etcresolvconf()
284 make_etchosts()
285 make_etchostname()
286 make_rootssh()
287 #make_l3rc()
289 def make_drbd():
290 make_lvm()
291 make_drbdconf()
292 make_drbdmeta()
294 def make_domains():
295 make_drbdprimary()
296 make_links()
297 make_fs()
298 make_mount()
299 make_install_debian()
300 make_rsync()
301 make_etc()
302 make_umount()
304 def get_management_ip():
305 return management_ip
307 def get_default_gateway():
308 return management_gw
310 def make_bridges():
311 ip=get_management_ip()
312 gw=get_default_gateway()
313 print "ifconfig %s 0.0.0.0" % (trunk)
315 # tagged interfaces
316 for bridge in bridges:
317 vlan=vlans[bridges.index(bridge)]
318 if vlan == 'tagged':
319 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk)
320 print "ip link set %s up" % (trunk)
322 # untagged interfaces
323 for bridge in bridges:
324 vlan=vlans[bridges.index(bridge)]
325 if vlan != 'tagged':
326 print "vconfig add %s %s" % (trunk,vlan)
327 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan)
328 print "ip link set %s.%s up" % (trunk,vlan)
330 print "ifconfig %s.%s %s" % (trunk,management_vlan,management_ip)
331 if management_gw:
332 print "route add default gw %s" % (management_gw)
336 #make_all_system()
338 if len(sys.argv) > 1:
339 command = sys.argv[2]
340 if command == 'make-drbd':
341 make_drbd()
342 elif command == 'make-domains':
343 make_domains()
344 elif command == 'make-bridges':
345 make_bridges()
346 elif command == 'make-links':
347 make_links()
348 else:
349 show_usage()
350 else:
351 show_usage()