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