xen-drbd
diff 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 diff
1.1 --- a/xen-drbd-install.py Thu Sep 27 18:56:29 2007 +0300 1.2 +++ b/xen-drbd-install.py Mon Feb 11 17:08:54 2008 +0200 1.3 @@ -4,139 +4,151 @@ 1.4 1.5 network=sys.argv[1] 1.6 try: 1.7 - exec 'from %s import domains,disk_table,lvm_vg_name,node1,node1_ip,node2,node2_ip, \ 1.8 - ip_network, domain_name, ip_nameserver, ip_gateway, apt_get_install, \ 1.9 - debian_release, debian_mirror, \ 1.10 - bridges, vlans, management_vlan, trunk, management_ip, management_gw ' % (network) 1.11 + exec 'from %s import domains,disk_table,lvm_vg_name,node1,node1_ip,node2,node2_ip, \ 1.12 + ip_network, domain_name, ip_nameserver, ip_gateway, apt_get_install, \ 1.13 + debian_release, debian_mirror, \ 1.14 + bridges, vlans, management_vlan, trunk, management_ip, management_gw ' % (network) 1.15 except: 1.16 - print "Can't find or interpret module %s with topology description" %(network) 1.17 - sys.exit(1) 1.18 + print "Can't find or interpret module %s with topology description" %(network) 1.19 + sys.exit(1) 1.20 1.21 drbd_base_port=7790 1.22 meta_disk='/dev/%s/meta'%lvm_vg_name 1.23 -drbd_devices=[] 1.24 +drbd_device={} 1.25 1.26 -def make_lvm(do_print=1): 1.27 - if do_print: 1.28 - print "lvcreate -L %s -n %s /dev/%s" % ("10G", "meta", lvm_vg_name) 1.29 - for dom in domains: 1.30 - for disk in disk_table[dom]: 1.31 - if disk.find(":") == -1: 1.32 - if do_print: 1.33 - print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name) 1.34 - drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)]) 1.35 - else: 1.36 - if do_print: 1.37 - print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[1], disk.split(':')[0], lvm_vg_name) 1.38 - drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])]) 1.39 +def make_drbd_hash(): 1.40 + for dom in domains: 1.41 + for disk in disk_table[dom]: 1.42 + if disk.find(":") == -1: 1.43 + drbd_device[disk]=dom 1.44 + else: 1.45 + drbd_device[disk.split(':')[0]]=disk.split(':')[1] 1.46 + 1.47 +def make_lvm(): 1.48 + if do_print: 1.49 + print "lvcreate -L %s -n %s /dev/%s" % ("10G", "meta", lvm_vg_name) 1.50 + for dom in domains: 1.51 + for disk in disk_table[dom]: 1.52 + if disk.find(":") == -1: 1.53 + #if do_print: 1.54 + # print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name) 1.55 + #drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)]) 1.56 + drbd_device[disk]=dom 1.57 + else: 1.58 + drbd_device[disk.split(':')[0]]=disk.split(':')[0] 1.59 + if do_print: 1.60 + print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[1], disk.split(':')[0], lvm_vg_name) 1.61 + drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])]) 1.62 1.63 1.64 def make_drbdconf(): 1.65 - print "cat <<DRBD > /etc/drbd.conf" 1.66 - i=0 1.67 - for drbd in drbd_devices: 1.68 - resource=drbd[0] 1.69 - disk=drbd[1] 1.70 - drbd_number=i 1.71 - i+=1 1.72 - print """ 1.73 + print "cat <<DRBD > /etc/drbd.conf" 1.74 + i=0 1.75 + for drbd in drbd_devices: 1.76 + resource=drbd[0] 1.77 + disk=drbd[1] 1.78 + drbd_number=i 1.79 + i+=1 1.80 + print """ 1.81 resource %s { 1.82 - protocol C; 1.83 - net { 1.84 - allow-two-primaries; 1.85 - after-sb-0pri discard-least-changes; 1.86 - after-sb-1pri call-pri-lost-after-sb; 1.87 - after-sb-2pri call-pri-lost-after-sb; 1.88 - } 1.89 - syncer { 1.90 - rate 5M; 1.91 - } 1.92 - on %s 1.93 - { 1.94 - device /dev/drbd%s; 1.95 - disk %s; 1.96 - address %s:%s; 1.97 - meta-disk %s[%s]; 1.98 - } 1.99 - on %s 1.100 - { 1.101 - device /dev/drbd%s; 1.102 - disk %s; 1.103 - address %s:%s; 1.104 - meta-disk %s[%s]; 1.105 - } 1.106 + protocol C; 1.107 + net { 1.108 + allow-two-primaries; 1.109 + after-sb-0pri discard-least-changes; 1.110 + after-sb-1pri call-pri-lost-after-sb; 1.111 + after-sb-2pri call-pri-lost-after-sb; 1.112 + } 1.113 + syncer { 1.114 + rate 5M; 1.115 + } 1.116 + on %s 1.117 + { 1.118 + device /dev/drbd%s; 1.119 + disk %s; 1.120 + address %s:%s; 1.121 + meta-disk %s[%s]; 1.122 + } 1.123 + on %s 1.124 + { 1.125 + device /dev/drbd%s; 1.126 + disk %s; 1.127 + address %s:%s; 1.128 + meta-disk %s[%s]; 1.129 + } 1.130 } 1.131 1.132 """ % (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 ) 1.133 - print "DRBD" 1.134 + print "DRBD" 1.135 1.136 1.137 -def make_drbd_links(): 1.138 - make_lvm(0) 1.139 - print "mkdir -p /dev/drbd" 1.140 - i=0 1.141 - for drbd in drbd_devices: 1.142 - drbd_number=i 1.143 - i+=1 1.144 - resource=drbd[0] 1.145 - disk=drbd[1] 1.146 - print "ln -sf /dev/drbd%s /dev/drbd/%s" %(drbd_number, resource) 1.147 +def make_links(): 1.148 + 1.149 + 1.150 + # drbd_devices={ 1.151 + # 'dev' : 'resource', 1.152 + # } 1.153 + 1.154 + make_drbd_hash() 1.155 + print "mkdir -p /dev/drbd" 1.156 + for dev in drbd_device.keys(): 1.157 + resource=drbd_device[dev] 1.158 + print "ln -sf /dev/%s /dev/drbd/%s" %(dev, resource) 1.159 1.160 def make_fs(): 1.161 - for dom in domains: 1.162 - print "mkfs.ext3 /dev/drbd/"+dom 1.163 + for dom in domains: 1.164 + print "mkfs.ext3 /dev/drbd/"+dom 1.165 1.166 1.167 def make_mount(): 1.168 - print "mkdir -p /domU" 1.169 - for dom in domains: 1.170 - print "mkdir -p /domU/%s" % (dom) 1.171 - print "mount /dev/drbd/%s /domU/%s" % (dom,dom) 1.172 + print "mkdir -p /domU" 1.173 + for dom in domains: 1.174 + print "mkdir -p /domU/%s" % (dom) 1.175 + print "mount /dev/drbd/%s /domU/%s" % (dom,dom) 1.176 1.177 def make_umount(): 1.178 - for dom in domains: 1.179 - print "umount /domU/%s" % (dom) 1.180 + for dom in domains: 1.181 + print "umount /domU/%s" % (dom) 1.182 1.183 def make_dns(): 1.184 - i=1 1.185 - for dom in domains: 1.186 - print "%s A %s.%s" % (dom, ip_network, i) 1.187 - i+=1 1.188 + i=1 1.189 + for dom in domains: 1.190 + print "%s A %s.%s" % (dom, ip_network, i) 1.191 + i+=1 1.192 1.193 def make_dns_reverse(): 1.194 - i=1 1.195 - for dom in domains: 1.196 - print "%s A %s.%s." % (i, dom, domain_name) 1.197 - i+=1 1.198 + i=1 1.199 + for dom in domains: 1.200 + print "%s A %s.%s." % (i, dom, domain_name) 1.201 + i+=1 1.202 1.203 def make_install_debian(): 1.204 - print """ 1.205 + print """ 1.206 debootstrap %s /domU/%s %s 1.207 chroot /domU/%s apt-get install -y --force-yes %s 1.208 - """ % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install) 1.209 + """ % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install) 1.210 1.211 def make_rsync(): 1.212 - for dom in domains[1:]: 1.213 - print """ 1.214 + for dom in domains[1:]: 1.215 + print """ 1.216 rsync -a /domU/%s/ /domU/%s/ 1.217 - """ % (domains[0], dom) 1.218 + """ % (domains[0], dom) 1.219 1.220 def make_etcfstab(): 1.221 - i=0 1.222 - for dom in domains: 1.223 - i+=1 1.224 - print """ 1.225 + i=0 1.226 + for dom in domains: 1.227 + i+=1 1.228 + print """ 1.229 cat <<FSTAB > /domU/%s/etc/fstab 1.230 proc /proc proc defaults 0 0 1.231 /dev/hda1 / ext3 defaults,errors=remount-ro 0 1 1.232 FSTAB 1.233 - """ % (dom) 1.234 + """ % (dom) 1.235 1.236 def make_etcnetworkinterfaces(): 1.237 - i=0 1.238 - for dom in domains: 1.239 - i+=1 1.240 - print """ 1.241 + i=0 1.242 + for dom in domains: 1.243 + i+=1 1.244 + print """ 1.245 cat <<INTERFACES > /domU/%s/etc/network/interfaces 1.246 auto lo eth0 1.247 iface lo inet loopback 1.248 @@ -150,31 +162,31 @@ 1.249 dns-nameservers %s 1.250 dns-search %s 1.251 INTERFACES 1.252 - """ % (dom,ip_network,i,ip_network, ip_network, ip_gateway, domain_name, ip_nameserver) 1.253 + """ % (dom,ip_network,i,ip_network, ip_network, ip_gateway, domain_name, ip_nameserver) 1.254 1.255 def make_etcresolvconf(): 1.256 - for dom in domains: 1.257 - print """ 1.258 + for dom in domains: 1.259 + print """ 1.260 cat <<RESOLVCONF > /domU/%s/etc/resolv.conf 1.261 search %s 1.262 nameserver %s 1.263 RESOLVCONF 1.264 - """ % (dom,domain_name,ip_nameserver) 1.265 + """ % (dom,domain_name,ip_nameserver) 1.266 1.267 def make_etchostname(): 1.268 - for dom in domains: 1.269 - print """ 1.270 + for dom in domains: 1.271 + print """ 1.272 cat <<HOSTNAME > /domU/%s/etc/hostname 1.273 %s 1.274 HOSTNAME 1.275 - """ % (dom,dom) 1.276 + """ % (dom,dom) 1.277 1.278 1.279 def make_etchosts(): 1.280 - i=0 1.281 - for dom in domains: 1.282 - i+=1 1.283 - print """ 1.284 + i=0 1.285 + for dom in domains: 1.286 + i+=1 1.287 + print """ 1.288 cat <<HOSTS > /domU/%s/etc/hosts 1.289 127.0.0.1 localhost 1.290 192.168.1.%s %s.%s %s 1.291 @@ -186,88 +198,100 @@ 1.292 ff02::2 ip6-allrouters 1.293 ff02::3 ip6-allhosts 1.294 HOSTS 1.295 - """ % (dom,i,dom,domain_name,dom) 1.296 + """ % (dom,i,dom,domain_name,dom) 1.297 1.298 def make_rootssh(): 1.299 - for dom in domains: 1.300 - print """ 1.301 + for dom in domains: 1.302 + print """ 1.303 mkdir -p /domU/%s/root/.ssh/ 1.304 cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys 1.305 chmod 600 /domU/%s/root/.ssh/authorized_keys 1.306 - """ % (dom,dom,dom) 1.307 + """ % (dom,dom,dom) 1.308 1.309 def make_l3rc(): 1.310 - for dom in domains: 1.311 - print """ 1.312 - cat <<EOF > /domU/%s/root/.l3rc 1.313 + for dom in domains: 1.314 + print """ 1.315 + cat <<EOF > /domU/%s/root/.l3rc 1.316 l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root 1.317 EOF 1.318 - """ % (dom,dom,domain_name) 1.319 + """ % (dom,dom,domain_name) 1.320 1.321 1.322 def make_etc(): 1.323 - make_etcfstab() 1.324 - make_etcnetworkinterfaces() 1.325 - make_etcresolvconf() 1.326 - make_etchosts() 1.327 - make_etchostname() 1.328 - make_rootssh() 1.329 - make_l3rc() 1.330 + make_etcfstab() 1.331 + make_etcnetworkinterfaces() 1.332 + make_etcresolvconf() 1.333 + make_etchosts() 1.334 + make_etchostname() 1.335 + make_rootssh() 1.336 + make_l3rc() 1.337 1.338 def make_all_system(): 1.339 - make_drbd_links() 1.340 - make_fs() 1.341 - make_mount() 1.342 - make_install_debian() 1.343 - make_rsync() 1.344 - make_etc() 1.345 - make_umount() 1.346 + make_drbd_links() 1.347 + make_fs() 1.348 + make_mount() 1.349 + make_install_debian() 1.350 + make_rsync() 1.351 + make_etc() 1.352 + make_umount() 1.353 1.354 def get_management_ip(): 1.355 - return management_ip 1.356 + return management_ip 1.357 1.358 def get_default_gateway(): 1.359 - return management_gw 1.360 + return management_gw 1.361 1.362 def make_bridges(): 1.363 - ip=get_management_ip() 1.364 - gw=get_default_gateway() 1.365 - print "ifconfig %s 0.0.0.0" % (trunk) 1.366 - for bridge in bridges: 1.367 - vlan=vlans[bridges.index(bridge)] 1.368 - print "vconfig add %s %s" % (trunk,vlan) 1.369 - print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan) 1.370 - print "ifconfig %s.%s %s" % (trunk,management_vlan,management_ip) 1.371 - if management_gw: 1.372 - print "route add default gw %s" % (management_gw) 1.373 + ip=get_management_ip() 1.374 + gw=get_default_gateway() 1.375 + print "ifconfig %s 0.0.0.0" % (trunk) 1.376 + 1.377 + # tagged interfaces 1.378 + for bridge in bridges: 1.379 + vlan=vlans[bridges.index(bridge)] 1.380 + if vlan == 'tagged': 1.381 + print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk) 1.382 + print "ip link set %s up" % (trunk) 1.383 + 1.384 + # untagged interfaces 1.385 + for bridge in bridges: 1.386 + vlan=vlans[bridges.index(bridge)] 1.387 + if vlan != 'tagged': 1.388 + print "vconfig add %s %s" % (trunk,vlan) 1.389 + print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan) 1.390 + print "ip link set %s.%s up" % (trunk,vlan) 1.391 + 1.392 + print "ifconfig %s.%s %s" % (trunk,management_vlan,management_ip) 1.393 + if management_gw: 1.394 + print "route add default gw %s" % (management_gw) 1.395 1.396 1.397 def show_usage(): 1.398 - print """ 1.399 + print """ 1.400 Usage: 1.401 - xen-drbd-install <network> <command> (to view) 1.402 - xen-drbd-install <network> <command> | sh -s (to run) 1.403 + xen-drbd-install <network> <command> (to view) 1.404 + xen-drbd-install <network> <command> | sh -s (to run) 1.405 1.406 - <network> is a name of the file, which contains network description 1.407 + <network> is a name of the file, which contains network description 1.408 1.409 Commands: 1.410 - make-all 1.411 - make-bridges 1.412 - make-links 1.413 + make-all 1.414 + make-bridges 1.415 + make-links 1.416 """ 1.417 1.418 #make_all_system() 1.419 1.420 if len(sys.argv) > 1: 1.421 - command = sys.argv[2] 1.422 - if command == 'make-all': 1.423 - make_all_system() 1.424 - if command == 'make-bridges': 1.425 - make_bridges() 1.426 - elif command == 'make-links': 1.427 - make_drbd_links() 1.428 - else: 1.429 - show_usage() 1.430 + command = sys.argv[2] 1.431 + if command == 'make-all': 1.432 + make_all_system() 1.433 + if command == 'make-bridges': 1.434 + make_bridges() 1.435 + elif command == 'make-links': 1.436 + make_links() 1.437 + else: 1.438 + show_usage() 1.439 else: 1.440 - show_usage() 1.441 + show_usage() 1.442