xen-drbd
view xen-drbd-install.py @ 22:2ecda36e8d0d
ip netmask
author | igor |
---|---|
date | Sun Feb 17 13:32:01 2008 +0200 (2008-02-17) |
parents | fc201b0bc6f9 |
children | 6e01df357a6a |
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 a 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:
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 def make_drbd_hash():
38 for dom in domains:
39 for disk in disk_table[dom]:
40 if disk.find(":") == -1:
41 drbd_device[disk]=dom
42 else:
43 drbd_device[disk.split(':')[0]]=disk.split(':')[1]
45 def make_lvm():
46 print "###################################################"
47 print "# MAKE LVM FOR DOMAINS DRBD DEVICES"
48 print "#"
49 for lvm_pv_name in re.compile('\s+').split(lvm_pv_names):
50 print "pvcreate %s" % (lvm_pv_name)
51 print "vgcreate /dev/%s %s" % (lvm_vg_name, lvm_pv_names)
52 print "lvcreate -L %s -n %s /dev/%s" % (lvm_lv_drbd_meta_size, lvm_lv_drbd_meta_name, lvm_vg_name)
53 for dom in domains:
54 for disk in disk_table[dom]:
55 if disk.find(":") == -1:
56 #if do_print:
57 # print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name)
58 #drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)])
59 drbd_device[disk]=dom
60 else:
61 drbd_device[disk.split(':')[0]]=disk.split(':')[0]
62 print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[2], disk.split(':')[1], lvm_vg_name)
63 #drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])])
64 print ""
67 def make_drbdconf():
68 print "###################################################"
69 print "# MAKE DRBD CONFIG FILE /etc/drbd.conf"
70 print "#"
72 print "cat <<DRBD > /etc/drbd.conf"
75 for dom in domains:
76 for disk in disk_table[dom]:
77 drbd_number=int(re.compile('drbd').sub('',disk.split(':')[0]))
78 resource=disk.split(':')[1]
79 disk="/dev/%s/%s" % (lvm_vg_name,resource)
81 print """
82 resource %s {
83 protocol C;
84 net {
85 allow-two-primaries;
86 after-sb-0pri discard-least-changes;
87 after-sb-1pri call-pri-lost-after-sb;
88 after-sb-2pri call-pri-lost-after-sb;
89 }
90 syncer {
91 rate 5M;
92 }
93 on %s
94 {
95 device /dev/drbd%s;
96 disk %s;
97 address %s:%s;
98 meta-disk %s[%s];
99 }
100 on %s
101 {
102 device /dev/drbd%s;
103 disk %s;
104 address %s:%s;
105 meta-disk %s[%s];
106 }
107 }
109 """ % (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 )
110 print "DRBD"
111 print ""
114 def make_drbdmeta():
115 print "###################################################"
116 print "# MAKE DRBD DEVICES "
117 print "#"
118 print "yes yes | drbdadm create-md all"
120 def make_drbdprimary():
121 print "###################################################"
122 print "# SET DRBD DEVICES PRIMARY "
123 print "#"
125 print "drbdadm up all"
126 for dom in domains:
127 for disk in disk_table[dom]:
128 print "drbdsetup %s primary -o" % disk.split(':')[0]
131 def make_links():
133 print "###################################################"
134 print "# MAKE SYMBOLIC LINKS TO /dev/drbd* FILES"
135 print "#"
137 # drbd_devices={
138 # 'dev' : 'resource',
139 # }
141 make_drbd_hash()
142 print "mkdir -p /dev/drbd"
143 for dev in drbd_device.keys():
144 resource=drbd_device[dev]
145 print "ln -sf /dev/%s /dev/drbd/%s" %(dev, resource)
147 def make_fs():
149 print "###################################################"
150 print "# MAKE FILESYSTEMS ON THE DRBDs"
151 print "#"
153 for dev in drbd_device.keys():
154 print "mkfs.ext3 %s /dev/%s" %(mkfs_options,dev)
157 def make_mount():
158 print "###################################################"
159 print "# MOUNT U-DOMAINS FILESYSTEMS"
160 print "#"
162 print "mkdir -p /domU"
163 for dom in domains:
164 print "mkdir -p /domU/%s" % (dom)
165 print "mount /dev/drbd/%s /domU/%s" % (dom,dom)
167 def make_umount():
168 for dom in domains:
169 print "umount /domU/%s" % (dom)
171 def make_dns():
172 i=1
173 for dom in domains:
174 print "%s A %s.%s" % (dom, ip_network, i)
175 i+=1
177 def make_dns_reverse():
178 i=1
179 for dom in domains:
180 print "%s A %s.%s." % (i, dom, domain_name)
181 i+=1
183 def make_install_debian():
184 print """
185 apt-get install debootstrap
186 debootstrap %s /domU/%s %s
187 chroot /domU/%s apt-get install -y --force-yes %s
188 """ % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install)
190 def make_rsync():
191 for dom in domains[1:]:
192 print """
193 rsync -a /domU/%s/ /domU/%s/
194 """ % (domains[0], dom)
196 def make_etcfstab():
197 i=0
198 for dom in domains:
199 i+=1
200 print """
201 cat <<FSTAB > /domU/%s/etc/fstab
202 proc /proc proc defaults 0 0
203 /dev/hda1 / ext3 defaults,errors=remount-ro 0 1
204 FSTAB
205 """ % (dom)
207 def make_etcnetworkinterfaces():
208 i=0
209 for dom in domains:
210 i+=1
211 print """
212 cat <<INTERFACES > /domU/%s/etc/network/interfaces
213 auto lo eth0
214 iface lo inet loopback
216 iface eth0 inet static
217 address %s
218 netmask 255.255.255.0
219 network %s.0
220 broadcast %s.255
221 gateway %s
222 dns-nameservers %s
223 dns-search %s
224 INTERFACES
225 """ % (dom,ip_address_table[dom],ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
227 def make_etcresolvconf():
228 for dom in domains:
229 print """
230 cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
231 search %s
232 nameserver %s
233 RESOLVCONF
234 """ % (dom,domain_name,ip_nameserver)
236 def make_etchostname():
237 for dom in domains:
238 print """
239 cat <<HOSTNAME > /domU/%s/etc/hostname
240 %s
241 HOSTNAME
242 """ % (dom,dom)
245 def make_etchosts():
246 i=0
247 for dom in domains:
248 i+=1
249 print """
250 cat <<HOSTS > /domU/%s/etc/hosts
251 127.0.0.1 localhost
252 %s %s.%s %s
254 ::1 ip6-localhost ip6-loopback
255 fe00::0 ip6-localnet
256 ff00::0 ip6-mcastprefix
257 ff02::1 ip6-allnodes
258 ff02::2 ip6-allrouters
259 ff02::3 ip6-allhosts
260 HOSTS
261 """ % (dom,ip_address_table[dom],dom,domain_name,dom)
263 def make_rootssh():
264 for dom in domains:
265 print """
266 mkdir -p /domU/%s/home/eb/.ssh/
267 chown -R 1000:1000 /domU/%s/home/eb
268 cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
269 chmod 600 /domU/%s/home/eb/.ssh/authorized_keys
270 """ % (dom,dom,dom,dom)
272 def make_l3rc():
273 for dom in domains:
274 print """
275 cat <<EOF > /domU/%s/root/.l3rc
276 l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
277 EOF
278 """ % (dom,dom,domain_name)
281 def make_etc():
282 make_etcfstab()
283 make_etcnetworkinterfaces()
284 make_etcresolvconf()
285 make_etchosts()
286 make_etchostname()
287 make_rootssh()
288 #make_l3rc()
290 def make_drbd():
291 make_lvm()
292 make_drbdconf()
293 make_drbdmeta()
295 def make_domains():
296 make_drbdprimary()
297 make_links()
298 make_fs()
299 make_mount()
300 make_install_debian()
301 make_rsync()
302 make_etc()
303 make_umount()
305 def get_management_ip():
306 return management_ip
308 def get_default_gateway():
309 return management_gw
311 def make_bridges():
312 ip=get_management_ip()
313 gw=get_default_gateway()
314 print "ifconfig %s 0.0.0.0 down" % (trunk)
316 # tagged interfaces
317 for bridge in bridges:
318 vlan=vlans[bridges.index(bridge)]
319 if vlan == 'tagged':
320 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk)
321 print "ip link set %s up" % (trunk)
323 # untagged interfaces
324 for bridge in bridges:
325 vlan=vlans[bridges.index(bridge)]
326 if vlan != 'tagged':
327 print "vconfig add %s %s" % (trunk,vlan)
328 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan)
329 print "ip link set %s.%s up" % (trunk,vlan)
331 print "ifconfig %s.%s %s netmask %s" % (trunk,management_vlan,management_ip,management_netmask)
332 if management_gw:
333 print "route add default gw %s" % (management_gw)
337 #make_all_system()
339 if len(sys.argv) > 1:
340 command = sys.argv[2]
341 if command == 'make-drbd':
342 make_drbd()
343 elif command == 'make-domains':
344 make_domains()
345 elif command == 'make-bridges':
346 make_bridges()
347 elif command == 'make-links':
348 make_links()
349 else:
350 show_usage()
351 else:
352 show_usage()