xen-drbd

annotate xen-drbd-install.py @ 62:d9780d8380b9

minifix
author igor@book.xt.vpn
date Thu Jun 25 17:08:22 2009 +0300 (2009-06-25)
parents ae3cd87f8837
children
rev   line source
igor@0 1 #!/usr/bin/python
igor@0 2
igor@37 3 import sys,re,getopt
igor@20 4 sys.path.append('/etc/xen')
igor@0 5
igor@10 6 def show_usage():
igor@10 7 print """
igor@10 8 Usage:
igor@37 9 xen-drbd-install [options] <command> (to view)
igor@37 10 xen-drbd-install [options] <command> | sh -s (to run)
igor@10 11
igor@30 12 <network> is the name of the file, which contains network description
igor@10 13
igor@10 14 Commands:
igor@10 15 make-drbd
igor@10 16 make-domains
igor@10 17 make-bridges
igor@10 18 make-links
igor@37 19
igor@37 20 Options:
igor@37 21 --help (-h) -- show usage information
root@53 22 --network=name (-n) -- the network descriptions is in the file name (default: network)
igor@10 23 """
igor@10 24
igor@37 25
igor@37 26
igor@37 27 network='network'
igor@37 28 try:
igor@37 29 opts, args = getopt.getopt(sys.argv[1:], "hn:", ["help", "network="])
igor@37 30 except getopt.GetoptError, err:
igor@38 31 print str(err)
igor@37 32 usage()
igor@37 33 sys.exit(2)
igor@37 34
igor@37 35 for o, a in opts:
igor@37 36 if o in ("-h", "--help"):
igor@37 37 show_usage()
igor@37 38 sys.exit()
igor@37 39 elif o in ("-n", "--network"):
igor@37 40 network = a
igor@37 41 else:
igor@37 42 assert False, "unhandled option"
igor@37 43
igor@37 44 if len(args) != 1:
igor@10 45 show_usage()
igor@10 46 sys.exit(1)
igor@10 47
igor@0 48 try:
igor@39 49 execfile('/etc/xen/'+network)
igor@38 50 except IOError:
igor@38 51 print "Network configuration file <%s> not found " % network
igor@38 52 if network == 'network':
igor@38 53 print "You can use -n option to specify network filename"
igor@38 54 sys.exit(2)
igor@38 55 except NameError:
igor@38 56 domain=domains[0]
igor@48 57 execfile('/etc/xen/'+network)
igor@0 58
igor@37 59
igor@0 60 drbd_base_port=7790
igor@0 61 meta_disk='/dev/%s/meta'%lvm_vg_name
igor@2 62 drbd_device={}
igor@0 63
igor@33 64 linux_domains=[]
igor@33 65 for dom in domains:
igor@33 66 if domain_types[domains.index(dom)] == 'linux':
igor@33 67 linux_domains.append(dom)
igor@33 68
igor@2 69 def make_drbd_hash():
igor@2 70 for dom in domains:
igor@2 71 for disk in disk_table[dom]:
igor@2 72 if disk.find(":") == -1:
igor@36 73 if disk.find("=") == -1:
igor@36 74 drbd_device[disk]=dom
igor@36 75 else:
igor@36 76 drbd_device[disk.split('=')[0]]=dom
igor@2 77 else:
igor@36 78 disk0=disk.split(':')[0]
igor@36 79 drbd_device[disk0.split('=')[0]]=disk.split(':')[1]
igor@2 80
igor@2 81 def make_lvm():
igor@9 82 print "###################################################"
igor@9 83 print "# MAKE LVM FOR DOMAINS DRBD DEVICES"
igor@9 84 print "#"
igor@9 85 for lvm_pv_name in re.compile('\s+').split(lvm_pv_names):
igor@9 86 print "pvcreate %s" % (lvm_pv_name)
igor@9 87 print "vgcreate /dev/%s %s" % (lvm_vg_name, lvm_pv_names)
igor@9 88 print "lvcreate -L %s -n %s /dev/%s" % (lvm_lv_drbd_meta_size, lvm_lv_drbd_meta_name, lvm_vg_name)
igor@33 89 for dom in linux_domains:
igor@2 90 for disk in disk_table[dom]:
igor@2 91 if disk.find(":") == -1:
igor@2 92 #if do_print:
igor@2 93 # print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name)
igor@2 94 #drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)])
igor@2 95 drbd_device[disk]=dom
igor@2 96 else:
igor@2 97 drbd_device[disk.split(':')[0]]=disk.split(':')[0]
igor@12 98 print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[2], disk.split(':')[1], lvm_vg_name)
igor@9 99 #drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])])
igor@9 100 print ""
igor@0 101
igor@0 102
igor@0 103 def make_drbdconf():
igor@9 104 print "###################################################"
igor@9 105 print "# MAKE DRBD CONFIG FILE /etc/drbd.conf"
igor@9 106 print "#"
igor@9 107
igor@2 108 print "cat <<DRBD > /etc/drbd.conf"
igor@9 109
igor@9 110
igor@9 111 for dom in domains:
igor@9 112 for disk in disk_table[dom]:
igor@37 113 drbd_number=int(re.compile('drbd').sub('',(disk.split(':')[0]).split('=')[0] ))
igor@9 114 resource=disk.split(':')[1]
igor@9 115 disk="/dev/%s/%s" % (lvm_vg_name,resource)
igor@9 116
igor@9 117 print """
igor@0 118 resource %s {
igor@2 119 protocol C;
igor@2 120 net {
igor@2 121 allow-two-primaries;
igor@2 122 after-sb-0pri discard-least-changes;
igor@2 123 after-sb-1pri call-pri-lost-after-sb;
igor@2 124 after-sb-2pri call-pri-lost-after-sb;
igor@2 125 }
igor@2 126 syncer {
igor@2 127 rate 5M;
igor@2 128 }
igor@2 129 on %s
igor@2 130 {
igor@2 131 device /dev/drbd%s;
igor@2 132 disk %s;
igor@2 133 address %s:%s;
igor@2 134 meta-disk %s[%s];
igor@2 135 }
igor@2 136 on %s
igor@2 137 {
igor@2 138 device /dev/drbd%s;
igor@2 139 disk %s;
igor@2 140 address %s:%s;
igor@2 141 meta-disk %s[%s];
igor@2 142 }
igor@0 143 }
igor@0 144
igor@9 145 """ % (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 )
igor@9 146 print "DRBD"
igor@9 147 print ""
igor@9 148
igor@9 149
igor@9 150 def make_drbdmeta():
igor@9 151 print "###################################################"
igor@9 152 print "# MAKE DRBD DEVICES "
igor@9 153 print "#"
igor@13 154 print "yes yes | drbdadm create-md all"
igor@9 155
igor@9 156 def make_drbdprimary():
igor@9 157 print "###################################################"
igor@9 158 print "# SET DRBD DEVICES PRIMARY "
igor@9 159 print "#"
igor@9 160
igor@9 161 print "drbdadm up all"
igor@9 162 for dom in domains:
igor@9 163 for disk in disk_table[dom]:
root@51 164 print "drbdsetup /dev/%s primary -o" % disk.split(':')[0]
igor@0 165
igor@0 166
igor@2 167 def make_links():
igor@2 168
igor@9 169 print "###################################################"
igor@9 170 print "# MAKE SYMBOLIC LINKS TO /dev/drbd* FILES"
igor@9 171 print "#"
igor@2 172
igor@2 173 # drbd_devices={
igor@2 174 # 'dev' : 'resource',
igor@2 175 # }
igor@2 176
igor@2 177 make_drbd_hash()
igor@2 178 print "mkdir -p /dev/drbd"
igor@2 179 for dev in drbd_device.keys():
igor@2 180 resource=drbd_device[dev]
igor@2 181 print "ln -sf /dev/%s /dev/drbd/%s" %(dev, resource)
igor@0 182
igor@0 183 def make_fs():
igor@9 184
igor@9 185 print "###################################################"
igor@9 186 print "# MAKE FILESYSTEMS ON THE DRBDs"
igor@9 187 print "#"
igor@9 188
igor@9 189 for dev in drbd_device.keys():
igor@9 190 print "mkfs.ext3 %s /dev/%s" %(mkfs_options,dev)
igor@0 191
igor@0 192
igor@0 193 def make_mount():
igor@9 194 print "###################################################"
igor@9 195 print "# MOUNT U-DOMAINS FILESYSTEMS"
igor@9 196 print "#"
igor@9 197
igor@2 198 print "mkdir -p /domU"
igor@33 199 for dom in linux_domains:
igor@2 200 print "mkdir -p /domU/%s" % (dom)
igor@2 201 print "mount /dev/drbd/%s /domU/%s" % (dom,dom)
igor@0 202
igor@0 203 def make_umount():
igor@33 204 for dom in linux_domains:
igor@2 205 print "umount /domU/%s" % (dom)
igor@0 206
igor@0 207 def make_dns():
igor@2 208 i=1
igor@33 209 for dom in linux_domains:
igor@2 210 print "%s A %s.%s" % (dom, ip_network, i)
igor@2 211 i+=1
igor@0 212
igor@0 213 def make_dns_reverse():
igor@2 214 i=1
igor@33 215 for dom in linux_domains:
igor@2 216 print "%s A %s.%s." % (i, dom, domain_name)
igor@2 217 i+=1
igor@0 218
igor@0 219 def make_install_debian():
igor@2 220 print """
igor@43 221 apt-get install debootstrap lsof
igor@0 222 debootstrap %s /domU/%s %s
igor@57 223 for i in %s
igor@57 224 do
igor@57 225 echo deb %s $i main contrib non-free >> /domU/%s/etc/apt/sources.list
igor@57 226 done
igor@59 227 LANG=C chroot /domU/%s apt-get update
root@55 228 LANG=C chroot /domU/%s apt-get install -y --force-yes %s
igor@43 229 kill $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null
igor@43 230 sleep 2
igor@43 231 kill -9 $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null
igor@59 232 """ % (debian_release,linux_domains[0],debian_mirror,additional_debian_releases,debian_mirror,linux_domains[0],linux_domains[0],linux_domains[0],apt_get_install,linux_domains[0],linux_domains[0])
igor@0 233
igor@0 234 def make_rsync():
igor@44 235 for dom in linux_domains[1:]:
igor@2 236 print """
root@55 237 echo -n Doing rsync for %s...
root@55 238 rsync -a /domU/%s/ /domU/%s/ && echo Done
root@55 239 """ % (dom, linux_domains[0], dom)
igor@0 240
igor@43 241
igor@43 242 def make_install_packages():
igor@44 243 for dom in linux_domains:
igor@43 244 try:
igor@43 245 print """
root@55 246 LANG=C chroot /domU/%s apt-get install -y --force-yes %s
igor@43 247 kill $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null
igor@43 248 sleep 2
igor@43 249 kill -9 $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null
igor@43 250 """ % (dom,apt_get_install_table[dom],dom,dom)
igor@43 251 except:
igor@43 252 pass
igor@43 253
igor@0 254 def make_etcfstab():
igor@2 255 i=0
igor@33 256 for dom in linux_domains:
igor@2 257 i+=1
igor@2 258 print """
igor@0 259 cat <<FSTAB > /domU/%s/etc/fstab
igor@0 260 proc /proc proc defaults 0 0
igor@0 261 /dev/hda1 / ext3 defaults,errors=remount-ro 0 1
igor@0 262 FSTAB
igor@2 263 """ % (dom)
igor@0 264
igor@0 265 def make_etcnetworkinterfaces():
igor@2 266 i=0
igor@33 267 for dom in linux_domains:
igor@2 268 i+=1
igor@2 269 print """
igor@0 270 cat <<INTERFACES > /domU/%s/etc/network/interfaces
igor@0 271 auto lo eth0
igor@0 272 iface lo inet loopback
igor@0 273
igor@0 274 iface eth0 inet static
igor@9 275 address %s
igor@0 276 netmask 255.255.255.0
igor@0 277 network %s.0
igor@0 278 broadcast %s.255
igor@0 279 gateway %s
root@51 280 dns-search %s
igor@0 281 dns-nameservers %s
igor@0 282 INTERFACES
igor@9 283 """ % (dom,ip_address_table[dom],ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
igor@0 284
igor@0 285 def make_etcresolvconf():
igor@33 286 for dom in linux_domains:
igor@2 287 print """
igor@0 288 cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
igor@0 289 search %s
igor@0 290 nameserver %s
igor@0 291 RESOLVCONF
igor@2 292 """ % (dom,domain_name,ip_nameserver)
igor@0 293
igor@0 294 def make_etchostname():
igor@33 295 for dom in linux_domains:
igor@2 296 print """
igor@0 297 cat <<HOSTNAME > /domU/%s/etc/hostname
igor@0 298 %s
igor@0 299 HOSTNAME
igor@2 300 """ % (dom,dom)
igor@0 301
igor@0 302
igor@0 303 def make_etchosts():
igor@2 304 i=0
igor@33 305 for dom in linux_domains:
igor@2 306 i+=1
igor@2 307 print """
igor@0 308 cat <<HOSTS > /domU/%s/etc/hosts
igor@0 309 127.0.0.1 localhost
igor@9 310 %s %s.%s %s
igor@0 311
igor@0 312 ::1 ip6-localhost ip6-loopback
igor@0 313 fe00::0 ip6-localnet
igor@0 314 ff00::0 ip6-mcastprefix
igor@0 315 ff02::1 ip6-allnodes
igor@0 316 ff02::2 ip6-allrouters
igor@0 317 ff02::3 ip6-allhosts
igor@0 318 HOSTS
igor@9 319 """ % (dom,ip_address_table[dom],dom,domain_name,dom)
igor@0 320
igor@0 321 def make_rootssh():
igor@33 322 for dom in linux_domains:
igor@2 323 print """
root@55 324 mkdir -p /domU/%s/root/.ssh/
igor@0 325 cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
root@55 326 chmod 600 /domU/%s/root/.ssh/authorized_keys
root@56 327 """ % (dom,dom,dom)
igor@0 328
igor@0 329 def make_l3rc():
igor@33 330 for dom in linux_domains:
igor@2 331 print """
igor@2 332 cat <<EOF > /domU/%s/root/.l3rc
igor@0 333 l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
igor@0 334 EOF
igor@2 335 """ % (dom,dom,domain_name)
igor@0 336
igor@0 337
igor@0 338 def make_etc():
igor@2 339 make_etcfstab()
igor@2 340 make_etcnetworkinterfaces()
igor@2 341 make_etcresolvconf()
igor@2 342 make_etchosts()
igor@2 343 make_etchostname()
igor@2 344 make_rootssh()
igor@15 345 #make_l3rc()
igor@0 346
igor@9 347 def make_drbd():
igor@9 348 make_lvm()
igor@9 349 make_drbdconf()
igor@9 350 make_drbdmeta()
igor@9 351
igor@9 352 def make_domains():
igor@9 353 make_drbdprimary()
igor@9 354 make_links()
igor@2 355 make_fs()
igor@2 356 make_mount()
igor@2 357 make_install_debian()
igor@2 358 make_rsync()
igor@43 359 make_install_packages()
igor@2 360 make_etc()
igor@2 361 make_umount()
igor@0 362
igor@0 363 def get_management_ip():
igor@2 364 return management_ip
igor@0 365
igor@0 366 def get_default_gateway():
igor@2 367 return management_gw
igor@0 368
igor@0 369 def make_bridges():
igor@2 370 ip=get_management_ip()
igor@2 371 gw=get_default_gateway()
igor@2 372
root@53 373 trunk_bridge=trunk
root@53 374
igor@2 375 # tagged interfaces
igor@2 376 for bridge in bridges:
igor@2 377 vlan=vlans[bridges.index(bridge)]
igor@2 378 if vlan == 'tagged':
root@53 379 if trunk_bridge != trunk:
root@53 380 print "\n=====\nERROR: No more than one tagged bridge allowed.\nYou have defined the tagged bridge already, %s " % trunk_bridge
root@53 381 sys.exit(1)
root@53 382 trunk_bridge=bridge
root@53 383 print "ifconfig %s 0.0.0.0 down" % (trunk)
root@53 384 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk)
root@53 385 print "ip link set %s up" % (trunk_bridge)
root@53 386
root@53 387 if trunk_bridge==trunk:
root@53 388 print "ifconfig %s up" % (trunk)
igor@2 389
igor@2 390 # untagged interfaces
igor@2 391 for bridge in bridges:
igor@2 392 vlan=vlans[bridges.index(bridge)]
igor@2 393 if vlan != 'tagged':
root@53 394 print "vconfig add %s %s" % (trunk_bridge,vlan)
root@53 395 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk_bridge,vlan)
root@53 396 print "ip link set %s up" % (bridge)
igor@2 397
root@53 398 print "ifconfig %s %s netmask %s" % (management_interface,management_ip,management_netmask)
igor@2 399 if management_gw:
igor@2 400 print "route add default gw %s" % (management_gw)
igor@0 401
igor@0 402
igor@0 403
igor@0 404 #make_all_system()
igor@0 405
igor@37 406 if len(args) > 0:
igor@37 407 command = args[0]
igor@9 408 if command == 'make-drbd':
igor@9 409 make_drbd()
igor@9 410 elif command == 'make-domains':
igor@9 411 make_domains()
igor@9 412 elif command == 'make-bridges':
igor@2 413 make_bridges()
igor@2 414 elif command == 'make-links':
igor@2 415 make_links()
igor@2 416 else:
igor@2 417 show_usage()
igor@0 418 else:
igor@2 419 show_usage()
igor@0 420