xen-drbd

view xen-drbd-install.py @ 7:826230820396

*** empty log message ***
author eb
date Mon Feb 11 17:08:54 2008 +0200 (2008-02-11)
parents b6dc3480caae
children e2eafd7e1b85
line source
1 #!/usr/bin/python
3 import sys
5 network=sys.argv[1]
6 try:
7 exec 'from %s import domains,disk_table,lvm_vg_name,node1,node1_ip,node2,node2_ip, \
8 ip_network, domain_name, ip_nameserver, ip_gateway, apt_get_install, \
9 debian_release, debian_mirror, \
10 bridges, vlans, management_vlan, trunk, management_ip, management_gw ' % (network)
11 except:
12 print "Can't find or interpret module %s with topology description" %(network)
13 sys.exit(1)
15 drbd_base_port=7790
16 meta_disk='/dev/%s/meta'%lvm_vg_name
17 drbd_device={}
19 def make_drbd_hash():
20 for dom in domains:
21 for disk in disk_table[dom]:
22 if disk.find(":") == -1:
23 drbd_device[disk]=dom
24 else:
25 drbd_device[disk.split(':')[0]]=disk.split(':')[1]
27 def make_lvm():
28 if do_print:
29 print "lvcreate -L %s -n %s /dev/%s" % ("10G", "meta", lvm_vg_name)
30 for dom in domains:
31 for disk in disk_table[dom]:
32 if disk.find(":") == -1:
33 #if do_print:
34 # print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name)
35 #drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)])
36 drbd_device[disk]=dom
37 else:
38 drbd_device[disk.split(':')[0]]=disk.split(':')[0]
39 if do_print:
40 print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[1], disk.split(':')[0], lvm_vg_name)
41 drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])])
44 def make_drbdconf():
45 print "cat <<DRBD > /etc/drbd.conf"
46 i=0
47 for drbd in drbd_devices:
48 resource=drbd[0]
49 disk=drbd[1]
50 drbd_number=i
51 i+=1
52 print """
53 resource %s {
54 protocol C;
55 net {
56 allow-two-primaries;
57 after-sb-0pri discard-least-changes;
58 after-sb-1pri call-pri-lost-after-sb;
59 after-sb-2pri call-pri-lost-after-sb;
60 }
61 syncer {
62 rate 5M;
63 }
64 on %s
65 {
66 device /dev/drbd%s;
67 disk %s;
68 address %s:%s;
69 meta-disk %s[%s];
70 }
71 on %s
72 {
73 device /dev/drbd%s;
74 disk %s;
75 address %s:%s;
76 meta-disk %s[%s];
77 }
78 }
80 """ % (resource, node1, drbd_number, disk, node1_ip, i+drbd_base_port, meta_disk, drbd_number, node2, drbd_number, disk, node2_ip, i+drbd_base_port, meta_disk, drbd_number )
81 print "DRBD"
84 def make_links():
87 # drbd_devices={
88 # 'dev' : 'resource',
89 # }
91 make_drbd_hash()
92 print "mkdir -p /dev/drbd"
93 for dev in drbd_device.keys():
94 resource=drbd_device[dev]
95 print "ln -sf /dev/%s /dev/drbd/%s" %(dev, resource)
97 def make_fs():
98 for dom in domains:
99 print "mkfs.ext3 /dev/drbd/"+dom
102 def make_mount():
103 print "mkdir -p /domU"
104 for dom in domains:
105 print "mkdir -p /domU/%s" % (dom)
106 print "mount /dev/drbd/%s /domU/%s" % (dom,dom)
108 def make_umount():
109 for dom in domains:
110 print "umount /domU/%s" % (dom)
112 def make_dns():
113 i=1
114 for dom in domains:
115 print "%s A %s.%s" % (dom, ip_network, i)
116 i+=1
118 def make_dns_reverse():
119 i=1
120 for dom in domains:
121 print "%s A %s.%s." % (i, dom, domain_name)
122 i+=1
124 def make_install_debian():
125 print """
126 debootstrap %s /domU/%s %s
127 chroot /domU/%s apt-get install -y --force-yes %s
128 """ % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install)
130 def make_rsync():
131 for dom in domains[1:]:
132 print """
133 rsync -a /domU/%s/ /domU/%s/
134 """ % (domains[0], dom)
136 def make_etcfstab():
137 i=0
138 for dom in domains:
139 i+=1
140 print """
141 cat <<FSTAB > /domU/%s/etc/fstab
142 proc /proc proc defaults 0 0
143 /dev/hda1 / ext3 defaults,errors=remount-ro 0 1
144 FSTAB
145 """ % (dom)
147 def make_etcnetworkinterfaces():
148 i=0
149 for dom in domains:
150 i+=1
151 print """
152 cat <<INTERFACES > /domU/%s/etc/network/interfaces
153 auto lo eth0
154 iface lo inet loopback
156 iface eth0 inet static
157 address %s.%s
158 netmask 255.255.255.0
159 network %s.0
160 broadcast %s.255
161 gateway %s
162 dns-nameservers %s
163 dns-search %s
164 INTERFACES
165 """ % (dom,ip_network,i,ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
167 def make_etcresolvconf():
168 for dom in domains:
169 print """
170 cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
171 search %s
172 nameserver %s
173 RESOLVCONF
174 """ % (dom,domain_name,ip_nameserver)
176 def make_etchostname():
177 for dom in domains:
178 print """
179 cat <<HOSTNAME > /domU/%s/etc/hostname
180 %s
181 HOSTNAME
182 """ % (dom,dom)
185 def make_etchosts():
186 i=0
187 for dom in domains:
188 i+=1
189 print """
190 cat <<HOSTS > /domU/%s/etc/hosts
191 127.0.0.1 localhost
192 192.168.1.%s %s.%s %s
194 ::1 ip6-localhost ip6-loopback
195 fe00::0 ip6-localnet
196 ff00::0 ip6-mcastprefix
197 ff02::1 ip6-allnodes
198 ff02::2 ip6-allrouters
199 ff02::3 ip6-allhosts
200 HOSTS
201 """ % (dom,i,dom,domain_name,dom)
203 def make_rootssh():
204 for dom in domains:
205 print """
206 mkdir -p /domU/%s/root/.ssh/
207 cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
208 chmod 600 /domU/%s/root/.ssh/authorized_keys
209 """ % (dom,dom,dom)
211 def make_l3rc():
212 for dom in domains:
213 print """
214 cat <<EOF > /domU/%s/root/.l3rc
215 l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
216 EOF
217 """ % (dom,dom,domain_name)
220 def make_etc():
221 make_etcfstab()
222 make_etcnetworkinterfaces()
223 make_etcresolvconf()
224 make_etchosts()
225 make_etchostname()
226 make_rootssh()
227 make_l3rc()
229 def make_all_system():
230 make_drbd_links()
231 make_fs()
232 make_mount()
233 make_install_debian()
234 make_rsync()
235 make_etc()
236 make_umount()
238 def get_management_ip():
239 return management_ip
241 def get_default_gateway():
242 return management_gw
244 def make_bridges():
245 ip=get_management_ip()
246 gw=get_default_gateway()
247 print "ifconfig %s 0.0.0.0" % (trunk)
249 # tagged interfaces
250 for bridge in bridges:
251 vlan=vlans[bridges.index(bridge)]
252 if vlan == 'tagged':
253 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk)
254 print "ip link set %s up" % (trunk)
256 # untagged interfaces
257 for bridge in bridges:
258 vlan=vlans[bridges.index(bridge)]
259 if vlan != 'tagged':
260 print "vconfig add %s %s" % (trunk,vlan)
261 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan)
262 print "ip link set %s.%s up" % (trunk,vlan)
264 print "ifconfig %s.%s %s" % (trunk,management_vlan,management_ip)
265 if management_gw:
266 print "route add default gw %s" % (management_gw)
269 def show_usage():
270 print """
271 Usage:
272 xen-drbd-install <network> <command> (to view)
273 xen-drbd-install <network> <command> | sh -s (to run)
275 <network> is a name of the file, which contains network description
277 Commands:
278 make-all
279 make-bridges
280 make-links
281 """
283 #make_all_system()
285 if len(sys.argv) > 1:
286 command = sys.argv[2]
287 if command == 'make-all':
288 make_all_system()
289 if command == 'make-bridges':
290 make_bridges()
291 elif command == 'make-links':
292 make_links()
293 else:
294 show_usage()
295 else:
296 show_usage()