xen-drbd
view xen-drbd-install.py @ 9:e2eafd7e1b85
Доработана инсталляции системы виртуализации
author | igor |
---|---|
date | Tue Feb 12 13:34:12 2008 +0200 (2008-02-12) |
parents | afe3c0992ec3 |
children | c14b6387ada5 |
line source
1 #!/usr/bin/python
3 import sys,re
5 network=sys.argv[1]
6 try:
7 exec 'from %s import * ' % (network)
8 # exec 'from %s import domains,disk_table,lvm_vg_name,node1,node1_ip,node2,node2_ip, \
9 # ip_network, domain_name, ip_nameserver, ip_gateway, apt_get_install, \
10 # debian_release, debian_mirror, \
11 # bridges, vlans, management_vlan, trunk, management_ip, management_gw ' % (network)
12 except:
13 print "Can't find or interpret module %s with topology description" %(network)
14 sys.exit(1)
16 drbd_base_port=7790
17 meta_disk='/dev/%s/meta'%lvm_vg_name
18 drbd_device={}
20 def make_drbd_hash():
21 for dom in domains:
22 for disk in disk_table[dom]:
23 if disk.find(":") == -1:
24 drbd_device[disk]=dom
25 else:
26 drbd_device[disk.split(':')[0]]=disk.split(':')[1]
28 def make_lvm():
29 print "###################################################"
30 print "# MAKE LVM FOR DOMAINS DRBD DEVICES"
31 print "#"
32 for lvm_pv_name in re.compile('\s+').split(lvm_pv_names):
33 print "pvcreate %s" % (lvm_pv_name)
34 print "vgcreate /dev/%s %s" % (lvm_vg_name, lvm_pv_names)
35 print "lvcreate -L %s -n %s /dev/%s" % (lvm_lv_drbd_meta_size, lvm_lv_drbd_meta_name, lvm_vg_name)
36 for dom in domains:
37 for disk in disk_table[dom]:
38 if disk.find(":") == -1:
39 #if do_print:
40 # print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name)
41 #drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)])
42 drbd_device[disk]=dom
43 else:
44 drbd_device[disk.split(':')[0]]=disk.split(':')[0]
45 print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[1], disk.split(':')[0], lvm_vg_name)
46 #drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])])
47 print ""
50 def make_drbdconf():
51 print "###################################################"
52 print "# MAKE DRBD CONFIG FILE /etc/drbd.conf"
53 print "#"
55 print "cat <<DRBD > /etc/drbd.conf"
58 for dom in domains:
59 for disk in disk_table[dom]:
60 drbd_number=int(re.compile('drbd').sub('',disk.split(':')[0]))
61 resource=disk.split(':')[1]
62 disk="/dev/%s/%s" % (lvm_vg_name,resource)
64 print """
65 resource %s {
66 protocol C;
67 net {
68 allow-two-primaries;
69 after-sb-0pri discard-least-changes;
70 after-sb-1pri call-pri-lost-after-sb;
71 after-sb-2pri call-pri-lost-after-sb;
72 }
73 syncer {
74 rate 5M;
75 }
76 on %s
77 {
78 device /dev/drbd%s;
79 disk %s;
80 address %s:%s;
81 meta-disk %s[%s];
82 }
83 on %s
84 {
85 device /dev/drbd%s;
86 disk %s;
87 address %s:%s;
88 meta-disk %s[%s];
89 }
90 }
92 """ % (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 )
93 print "DRBD"
94 print ""
97 def make_drbdmeta():
98 print "###################################################"
99 print "# MAKE DRBD DEVICES "
100 print "#"
101 print "drbdadm create-md all"
103 def make_drbdprimary():
104 print "###################################################"
105 print "# SET DRBD DEVICES PRIMARY "
106 print "#"
108 print "drbdadm up all"
109 for dom in domains:
110 for disk in disk_table[dom]:
111 print "drbdsetup %s primary -o" % disk.split(':')[0]
114 def make_links():
116 print "###################################################"
117 print "# MAKE SYMBOLIC LINKS TO /dev/drbd* FILES"
118 print "#"
120 # drbd_devices={
121 # 'dev' : 'resource',
122 # }
124 make_drbd_hash()
125 print "mkdir -p /dev/drbd"
126 for dev in drbd_device.keys():
127 resource=drbd_device[dev]
128 print "ln -sf /dev/%s /dev/drbd/%s" %(dev, resource)
130 def make_fs():
132 print "###################################################"
133 print "# MAKE FILESYSTEMS ON THE DRBDs"
134 print "#"
136 for dev in drbd_device.keys():
137 print "mkfs.ext3 %s /dev/%s" %(mkfs_options,dev)
140 def make_mount():
141 print "###################################################"
142 print "# MOUNT U-DOMAINS FILESYSTEMS"
143 print "#"
145 print "mkdir -p /domU"
146 for dom in domains:
147 print "mkdir -p /domU/%s" % (dom)
148 print "mount /dev/drbd/%s /domU/%s" % (dom,dom)
150 def make_umount():
151 for dom in domains:
152 print "umount /domU/%s" % (dom)
154 def make_dns():
155 i=1
156 for dom in domains:
157 print "%s A %s.%s" % (dom, ip_network, i)
158 i+=1
160 def make_dns_reverse():
161 i=1
162 for dom in domains:
163 print "%s A %s.%s." % (i, dom, domain_name)
164 i+=1
166 def make_install_debian():
167 print """
168 debootstrap %s /domU/%s %s
169 chroot /domU/%s apt-get install -y --force-yes %s
170 """ % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install)
172 def make_rsync():
173 for dom in domains[1:]:
174 print """
175 rsync -a /domU/%s/ /domU/%s/
176 """ % (domains[0], dom)
178 def make_etcfstab():
179 i=0
180 for dom in domains:
181 i+=1
182 print """
183 cat <<FSTAB > /domU/%s/etc/fstab
184 proc /proc proc defaults 0 0
185 /dev/hda1 / ext3 defaults,errors=remount-ro 0 1
186 FSTAB
187 """ % (dom)
189 def make_etcnetworkinterfaces():
190 i=0
191 for dom in domains:
192 i+=1
193 print """
194 cat <<INTERFACES > /domU/%s/etc/network/interfaces
195 auto lo eth0
196 iface lo inet loopback
198 iface eth0 inet static
199 address %s
200 netmask 255.255.255.0
201 network %s.0
202 broadcast %s.255
203 gateway %s
204 dns-nameservers %s
205 dns-search %s
206 INTERFACES
207 """ % (dom,ip_address_table[dom],ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
209 def make_etcresolvconf():
210 for dom in domains:
211 print """
212 cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
213 search %s
214 nameserver %s
215 RESOLVCONF
216 """ % (dom,domain_name,ip_nameserver)
218 def make_etchostname():
219 for dom in domains:
220 print """
221 cat <<HOSTNAME > /domU/%s/etc/hostname
222 %s
223 HOSTNAME
224 """ % (dom,dom)
227 def make_etchosts():
228 i=0
229 for dom in domains:
230 i+=1
231 print """
232 cat <<HOSTS > /domU/%s/etc/hosts
233 127.0.0.1 localhost
234 %s %s.%s %s
236 ::1 ip6-localhost ip6-loopback
237 fe00::0 ip6-localnet
238 ff00::0 ip6-mcastprefix
239 ff02::1 ip6-allnodes
240 ff02::2 ip6-allrouters
241 ff02::3 ip6-allhosts
242 HOSTS
243 """ % (dom,ip_address_table[dom],dom,domain_name,dom)
245 def make_rootssh():
246 for dom in domains:
247 print """
248 mkdir -p /domU/%s/root/.ssh/
249 cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
250 chmod 600 /domU/%s/root/.ssh/authorized_keys
251 """ % (dom,dom,dom)
253 def make_l3rc():
254 for dom in domains:
255 print """
256 cat <<EOF > /domU/%s/root/.l3rc
257 l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
258 EOF
259 """ % (dom,dom,domain_name)
262 def make_etc():
263 make_etcfstab()
264 make_etcnetworkinterfaces()
265 make_etcresolvconf()
266 make_etchosts()
267 make_etchostname()
268 make_rootssh()
269 make_l3rc()
271 def make_drbd():
272 make_lvm()
273 make_drbdconf()
274 make_drbdmeta()
276 def make_domains():
277 make_drbdprimary()
278 make_links()
279 make_fs()
280 make_mount()
281 make_install_debian()
282 make_rsync()
283 make_etc()
284 make_umount()
286 def get_management_ip():
287 return management_ip
289 def get_default_gateway():
290 return management_gw
292 def make_bridges():
293 ip=get_management_ip()
294 gw=get_default_gateway()
295 print "ifconfig %s 0.0.0.0" % (trunk)
297 # tagged interfaces
298 for bridge in bridges:
299 vlan=vlans[bridges.index(bridge)]
300 if vlan == 'tagged':
301 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk)
302 print "ip link set %s up" % (trunk)
304 # untagged interfaces
305 for bridge in bridges:
306 vlan=vlans[bridges.index(bridge)]
307 if vlan != 'tagged':
308 print "vconfig add %s %s" % (trunk,vlan)
309 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan)
310 print "ip link set %s.%s up" % (trunk,vlan)
312 print "ifconfig %s.%s %s" % (trunk,management_vlan,management_ip)
313 if management_gw:
314 print "route add default gw %s" % (management_gw)
317 def show_usage():
318 print """
319 Usage:
320 xen-drbd-install <network> <command> (to view)
321 xen-drbd-install <network> <command> | sh -s (to run)
323 <network> is a name of the file, which contains network description
325 Commands:
326 make-drbd
327 make-domains
328 make-bridges
329 make-links
330 """
332 #make_all_system()
334 if len(sys.argv) > 1:
335 command = sys.argv[2]
336 if command == 'make-drbd':
337 make_drbd()
338 elif command == 'make-domains':
339 make_domains()
340 elif command == 'make-bridges':
341 make_bridges()
342 elif command == 'make-links':
343 make_links()
344 else:
345 show_usage()
346 else:
347 show_usage()