xen-drbd

annotate xen-drbd-install.py @ 39:263d00b75422

path to network file
author igor@manas.xt.vpn
date Mon Sep 22 15:06:26 2008 +0300 (2008-09-22)
parents 30671fe7fd79
children 562d6e064e6f
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
igor@37 22 --network=name (-n) -- the network descriptions is in the file name.py (default: network.py)
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@38 57 execfile(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]:
igor@9 164 print "drbdsetup %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@17 221 apt-get install debootstrap
igor@0 222 debootstrap %s /domU/%s %s
igor@0 223 chroot /domU/%s apt-get install -y --force-yes %s
igor@2 224 """ % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install)
igor@0 225
igor@0 226 def make_rsync():
igor@2 227 for dom in domains[1:]:
igor@2 228 print """
igor@0 229 rsync -a /domU/%s/ /domU/%s/
igor@2 230 """ % (domains[0], dom)
igor@0 231
igor@0 232 def make_etcfstab():
igor@2 233 i=0
igor@33 234 for dom in linux_domains:
igor@2 235 i+=1
igor@2 236 print """
igor@0 237 cat <<FSTAB > /domU/%s/etc/fstab
igor@0 238 proc /proc proc defaults 0 0
igor@0 239 /dev/hda1 / ext3 defaults,errors=remount-ro 0 1
igor@0 240 FSTAB
igor@2 241 """ % (dom)
igor@0 242
igor@0 243 def make_etcnetworkinterfaces():
igor@2 244 i=0
igor@33 245 for dom in linux_domains:
igor@2 246 i+=1
igor@2 247 print """
igor@0 248 cat <<INTERFACES > /domU/%s/etc/network/interfaces
igor@0 249 auto lo eth0
igor@0 250 iface lo inet loopback
igor@0 251
igor@0 252 iface eth0 inet static
igor@9 253 address %s
igor@0 254 netmask 255.255.255.0
igor@0 255 network %s.0
igor@0 256 broadcast %s.255
igor@0 257 gateway %s
igor@0 258 dns-nameservers %s
igor@0 259 dns-search %s
igor@0 260 INTERFACES
igor@9 261 """ % (dom,ip_address_table[dom],ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
igor@0 262
igor@0 263 def make_etcresolvconf():
igor@33 264 for dom in linux_domains:
igor@2 265 print """
igor@0 266 cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
igor@0 267 search %s
igor@0 268 nameserver %s
igor@0 269 RESOLVCONF
igor@2 270 """ % (dom,domain_name,ip_nameserver)
igor@0 271
igor@0 272 def make_etchostname():
igor@33 273 for dom in linux_domains:
igor@2 274 print """
igor@0 275 cat <<HOSTNAME > /domU/%s/etc/hostname
igor@0 276 %s
igor@0 277 HOSTNAME
igor@2 278 """ % (dom,dom)
igor@0 279
igor@0 280
igor@0 281 def make_etchosts():
igor@2 282 i=0
igor@33 283 for dom in linux_domains:
igor@2 284 i+=1
igor@2 285 print """
igor@0 286 cat <<HOSTS > /domU/%s/etc/hosts
igor@0 287 127.0.0.1 localhost
igor@9 288 %s %s.%s %s
igor@0 289
igor@0 290 ::1 ip6-localhost ip6-loopback
igor@0 291 fe00::0 ip6-localnet
igor@0 292 ff00::0 ip6-mcastprefix
igor@0 293 ff02::1 ip6-allnodes
igor@0 294 ff02::2 ip6-allrouters
igor@0 295 ff02::3 ip6-allhosts
igor@0 296 HOSTS
igor@9 297 """ % (dom,ip_address_table[dom],dom,domain_name,dom)
igor@0 298
igor@0 299 def make_rootssh():
igor@33 300 for dom in linux_domains:
igor@2 301 print """
igor@14 302 mkdir -p /domU/%s/home/eb/.ssh/
igor@14 303 chown -R 1000:1000 /domU/%s/home/eb
igor@0 304 cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
igor@14 305 chmod 600 /domU/%s/home/eb/.ssh/authorized_keys
igor@16 306 """ % (dom,dom,dom,dom)
igor@0 307
igor@0 308 def make_l3rc():
igor@33 309 for dom in linux_domains:
igor@2 310 print """
igor@2 311 cat <<EOF > /domU/%s/root/.l3rc
igor@0 312 l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
igor@0 313 EOF
igor@2 314 """ % (dom,dom,domain_name)
igor@0 315
igor@0 316
igor@0 317 def make_etc():
igor@2 318 make_etcfstab()
igor@2 319 make_etcnetworkinterfaces()
igor@2 320 make_etcresolvconf()
igor@2 321 make_etchosts()
igor@2 322 make_etchostname()
igor@2 323 make_rootssh()
igor@15 324 #make_l3rc()
igor@0 325
igor@9 326 def make_drbd():
igor@9 327 make_lvm()
igor@9 328 make_drbdconf()
igor@9 329 make_drbdmeta()
igor@9 330
igor@9 331 def make_domains():
igor@9 332 make_drbdprimary()
igor@9 333 make_links()
igor@2 334 make_fs()
igor@2 335 make_mount()
igor@2 336 make_install_debian()
igor@2 337 make_rsync()
igor@2 338 make_etc()
igor@2 339 make_umount()
igor@0 340
igor@0 341 def get_management_ip():
igor@2 342 return management_ip
igor@0 343
igor@0 344 def get_default_gateway():
igor@2 345 return management_gw
igor@0 346
igor@0 347 def make_bridges():
igor@2 348 ip=get_management_ip()
igor@2 349 gw=get_default_gateway()
igor@21 350 print "ifconfig %s 0.0.0.0 down" % (trunk)
igor@2 351
igor@2 352 # tagged interfaces
igor@2 353 for bridge in bridges:
igor@2 354 vlan=vlans[bridges.index(bridge)]
igor@2 355 if vlan == 'tagged':
igor@2 356 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk)
igor@2 357 print "ip link set %s up" % (trunk)
igor@2 358
igor@2 359 # untagged interfaces
igor@2 360 for bridge in bridges:
igor@2 361 vlan=vlans[bridges.index(bridge)]
igor@2 362 if vlan != 'tagged':
igor@2 363 print "vconfig add %s %s" % (trunk,vlan)
igor@2 364 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan)
igor@2 365 print "ip link set %s.%s up" % (trunk,vlan)
igor@2 366
igor@22 367 print "ifconfig %s.%s %s netmask %s" % (trunk,management_vlan,management_ip,management_netmask)
igor@2 368 if management_gw:
igor@2 369 print "route add default gw %s" % (management_gw)
igor@0 370
igor@0 371
igor@0 372
igor@0 373 #make_all_system()
igor@0 374
igor@37 375 if len(args) > 0:
igor@37 376 command = args[0]
igor@9 377 if command == 'make-drbd':
igor@9 378 make_drbd()
igor@9 379 elif command == 'make-domains':
igor@9 380 make_domains()
igor@9 381 elif command == 'make-bridges':
igor@2 382 make_bridges()
igor@2 383 elif command == 'make-links':
igor@2 384 make_links()
igor@2 385 else:
igor@2 386 show_usage()
igor@0 387 else:
igor@2 388 show_usage()
igor@0 389