xen-drbd
view xen-drbd-install.py @ 0:b6dc3480caae
xen-drbd merge
author | igor |
---|---|
date | Thu Sep 27 18:56:29 2007 +0300 (2007-09-27) |
parents | |
children | afe3c0992ec3 |
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_devices=[]
19 def make_lvm(do_print=1):
20 if do_print:
21 print "lvcreate -L %s -n %s /dev/%s" % ("10G", "meta", lvm_vg_name)
22 for dom in domains:
23 for disk in disk_table[dom]:
24 if disk.find(":") == -1:
25 if do_print:
26 print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name)
27 drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)])
28 else:
29 if do_print:
30 print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[1], disk.split(':')[0], lvm_vg_name)
31 drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])])
34 def make_drbdconf():
35 print "cat <<DRBD > /etc/drbd.conf"
36 i=0
37 for drbd in drbd_devices:
38 resource=drbd[0]
39 disk=drbd[1]
40 drbd_number=i
41 i+=1
42 print """
43 resource %s {
44 protocol C;
45 net {
46 allow-two-primaries;
47 after-sb-0pri discard-least-changes;
48 after-sb-1pri call-pri-lost-after-sb;
49 after-sb-2pri call-pri-lost-after-sb;
50 }
51 syncer {
52 rate 5M;
53 }
54 on %s
55 {
56 device /dev/drbd%s;
57 disk %s;
58 address %s:%s;
59 meta-disk %s[%s];
60 }
61 on %s
62 {
63 device /dev/drbd%s;
64 disk %s;
65 address %s:%s;
66 meta-disk %s[%s];
67 }
68 }
70 """ % (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 )
71 print "DRBD"
74 def make_drbd_links():
75 make_lvm(0)
76 print "mkdir -p /dev/drbd"
77 i=0
78 for drbd in drbd_devices:
79 drbd_number=i
80 i+=1
81 resource=drbd[0]
82 disk=drbd[1]
83 print "ln -sf /dev/drbd%s /dev/drbd/%s" %(drbd_number, resource)
85 def make_fs():
86 for dom in domains:
87 print "mkfs.ext3 /dev/drbd/"+dom
90 def make_mount():
91 print "mkdir -p /domU"
92 for dom in domains:
93 print "mkdir -p /domU/%s" % (dom)
94 print "mount /dev/drbd/%s /domU/%s" % (dom,dom)
96 def make_umount():
97 for dom in domains:
98 print "umount /domU/%s" % (dom)
100 def make_dns():
101 i=1
102 for dom in domains:
103 print "%s A %s.%s" % (dom, ip_network, i)
104 i+=1
106 def make_dns_reverse():
107 i=1
108 for dom in domains:
109 print "%s A %s.%s." % (i, dom, domain_name)
110 i+=1
112 def make_install_debian():
113 print """
114 debootstrap %s /domU/%s %s
115 chroot /domU/%s apt-get install -y --force-yes %s
116 """ % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install)
118 def make_rsync():
119 for dom in domains[1:]:
120 print """
121 rsync -a /domU/%s/ /domU/%s/
122 """ % (domains[0], dom)
124 def make_etcfstab():
125 i=0
126 for dom in domains:
127 i+=1
128 print """
129 cat <<FSTAB > /domU/%s/etc/fstab
130 proc /proc proc defaults 0 0
131 /dev/hda1 / ext3 defaults,errors=remount-ro 0 1
132 FSTAB
133 """ % (dom)
135 def make_etcnetworkinterfaces():
136 i=0
137 for dom in domains:
138 i+=1
139 print """
140 cat <<INTERFACES > /domU/%s/etc/network/interfaces
141 auto lo eth0
142 iface lo inet loopback
144 iface eth0 inet static
145 address %s.%s
146 netmask 255.255.255.0
147 network %s.0
148 broadcast %s.255
149 gateway %s
150 dns-nameservers %s
151 dns-search %s
152 INTERFACES
153 """ % (dom,ip_network,i,ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
155 def make_etcresolvconf():
156 for dom in domains:
157 print """
158 cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
159 search %s
160 nameserver %s
161 RESOLVCONF
162 """ % (dom,domain_name,ip_nameserver)
164 def make_etchostname():
165 for dom in domains:
166 print """
167 cat <<HOSTNAME > /domU/%s/etc/hostname
168 %s
169 HOSTNAME
170 """ % (dom,dom)
173 def make_etchosts():
174 i=0
175 for dom in domains:
176 i+=1
177 print """
178 cat <<HOSTS > /domU/%s/etc/hosts
179 127.0.0.1 localhost
180 192.168.1.%s %s.%s %s
182 ::1 ip6-localhost ip6-loopback
183 fe00::0 ip6-localnet
184 ff00::0 ip6-mcastprefix
185 ff02::1 ip6-allnodes
186 ff02::2 ip6-allrouters
187 ff02::3 ip6-allhosts
188 HOSTS
189 """ % (dom,i,dom,domain_name,dom)
191 def make_rootssh():
192 for dom in domains:
193 print """
194 mkdir -p /domU/%s/root/.ssh/
195 cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
196 chmod 600 /domU/%s/root/.ssh/authorized_keys
197 """ % (dom,dom,dom)
199 def make_l3rc():
200 for dom in domains:
201 print """
202 cat <<EOF > /domU/%s/root/.l3rc
203 l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
204 EOF
205 """ % (dom,dom,domain_name)
208 def make_etc():
209 make_etcfstab()
210 make_etcnetworkinterfaces()
211 make_etcresolvconf()
212 make_etchosts()
213 make_etchostname()
214 make_rootssh()
215 make_l3rc()
217 def make_all_system():
218 make_drbd_links()
219 make_fs()
220 make_mount()
221 make_install_debian()
222 make_rsync()
223 make_etc()
224 make_umount()
226 def get_management_ip():
227 return management_ip
229 def get_default_gateway():
230 return management_gw
232 def make_bridges():
233 ip=get_management_ip()
234 gw=get_default_gateway()
235 print "ifconfig %s 0.0.0.0" % (trunk)
236 for bridge in bridges:
237 vlan=vlans[bridges.index(bridge)]
238 print "vconfig add %s %s" % (trunk,vlan)
239 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan)
240 print "ifconfig %s.%s %s" % (trunk,management_vlan,management_ip)
241 if management_gw:
242 print "route add default gw %s" % (management_gw)
245 def show_usage():
246 print """
247 Usage:
248 xen-drbd-install <network> <command> (to view)
249 xen-drbd-install <network> <command> | sh -s (to run)
251 <network> is a name of the file, which contains network description
253 Commands:
254 make-all
255 make-bridges
256 make-links
257 """
259 #make_all_system()
261 if len(sys.argv) > 1:
262 command = sys.argv[2]
263 if command == 'make-all':
264 make_all_system()
265 if command == 'make-bridges':
266 make_bridges()
267 elif command == 'make-links':
268 make_drbd_links()
269 else:
270 show_usage()
271 else:
272 show_usage()