xen-drbd

annotate xen-drbd-install.py @ 43:562d6e064e6f

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