xen-drbd

annotate xen-drbd-install.py @ 33:85165c844ac9

fill only linux domains; skip all other
author igor@manas.xt.vpn
date Mon Aug 18 12:29:26 2008 +0300 (2008-08-18)
parents fc4025aa8287
children 44efea4507ac
rev   line source
igor@0 1 #!/usr/bin/python
igor@0 2
igor@9 3 import sys,re
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@10 9 xen-drbd-install <network> <command> (to view)
igor@10 10 xen-drbd-install <network> <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@10 19 """
igor@10 20
igor@11 21 if len(sys.argv) != 3:
igor@10 22 show_usage()
igor@10 23 sys.exit(1)
igor@10 24
igor@10 25
igor@0 26 network=sys.argv[1]
igor@0 27 try:
igor@9 28 exec 'from %s import * ' % (network)
igor@23 29 except ImportError:
igor@2 30 print "Can't find or interpret module %s with topology description" %(network)
igor@2 31 sys.exit(1)
igor@0 32
igor@0 33 drbd_base_port=7790
igor@0 34 meta_disk='/dev/%s/meta'%lvm_vg_name
igor@2 35 drbd_device={}
igor@0 36
igor@33 37 linux_domains=[]
igor@33 38 for dom in domains:
igor@33 39 if domain_types[domains.index(dom)] == 'linux':
igor@33 40 linux_domains.append(dom)
igor@33 41
igor@2 42 def make_drbd_hash():
igor@2 43 for dom in domains:
igor@2 44 for disk in disk_table[dom]:
igor@2 45 if disk.find(":") == -1:
igor@2 46 drbd_device[disk]=dom
igor@2 47 else:
igor@2 48 drbd_device[disk.split(':')[0]]=disk.split(':')[1]
igor@2 49
igor@2 50 def make_lvm():
igor@9 51 print "###################################################"
igor@9 52 print "# MAKE LVM FOR DOMAINS DRBD DEVICES"
igor@9 53 print "#"
igor@9 54 for lvm_pv_name in re.compile('\s+').split(lvm_pv_names):
igor@9 55 print "pvcreate %s" % (lvm_pv_name)
igor@9 56 print "vgcreate /dev/%s %s" % (lvm_vg_name, lvm_pv_names)
igor@9 57 print "lvcreate -L %s -n %s /dev/%s" % (lvm_lv_drbd_meta_size, lvm_lv_drbd_meta_name, lvm_vg_name)
igor@33 58 for dom in linux_domains:
igor@2 59 for disk in disk_table[dom]:
igor@2 60 if disk.find(":") == -1:
igor@2 61 #if do_print:
igor@2 62 # print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name)
igor@2 63 #drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)])
igor@2 64 drbd_device[disk]=dom
igor@2 65 else:
igor@2 66 drbd_device[disk.split(':')[0]]=disk.split(':')[0]
igor@12 67 print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[2], disk.split(':')[1], lvm_vg_name)
igor@9 68 #drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])])
igor@9 69 print ""
igor@0 70
igor@0 71
igor@0 72 def make_drbdconf():
igor@9 73 print "###################################################"
igor@9 74 print "# MAKE DRBD CONFIG FILE /etc/drbd.conf"
igor@9 75 print "#"
igor@9 76
igor@2 77 print "cat <<DRBD > /etc/drbd.conf"
igor@9 78
igor@9 79
igor@9 80 for dom in domains:
igor@9 81 for disk in disk_table[dom]:
igor@9 82 drbd_number=int(re.compile('drbd').sub('',disk.split(':')[0]))
igor@9 83 resource=disk.split(':')[1]
igor@9 84 disk="/dev/%s/%s" % (lvm_vg_name,resource)
igor@9 85
igor@9 86 print """
igor@0 87 resource %s {
igor@2 88 protocol C;
igor@2 89 net {
igor@2 90 allow-two-primaries;
igor@2 91 after-sb-0pri discard-least-changes;
igor@2 92 after-sb-1pri call-pri-lost-after-sb;
igor@2 93 after-sb-2pri call-pri-lost-after-sb;
igor@2 94 }
igor@2 95 syncer {
igor@2 96 rate 5M;
igor@2 97 }
igor@2 98 on %s
igor@2 99 {
igor@2 100 device /dev/drbd%s;
igor@2 101 disk %s;
igor@2 102 address %s:%s;
igor@2 103 meta-disk %s[%s];
igor@2 104 }
igor@2 105 on %s
igor@2 106 {
igor@2 107 device /dev/drbd%s;
igor@2 108 disk %s;
igor@2 109 address %s:%s;
igor@2 110 meta-disk %s[%s];
igor@2 111 }
igor@0 112 }
igor@0 113
igor@9 114 """ % (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 115 print "DRBD"
igor@9 116 print ""
igor@9 117
igor@9 118
igor@9 119 def make_drbdmeta():
igor@9 120 print "###################################################"
igor@9 121 print "# MAKE DRBD DEVICES "
igor@9 122 print "#"
igor@13 123 print "yes yes | drbdadm create-md all"
igor@9 124
igor@9 125 def make_drbdprimary():
igor@9 126 print "###################################################"
igor@9 127 print "# SET DRBD DEVICES PRIMARY "
igor@9 128 print "#"
igor@9 129
igor@9 130 print "drbdadm up all"
igor@9 131 for dom in domains:
igor@9 132 for disk in disk_table[dom]:
igor@9 133 print "drbdsetup %s primary -o" % disk.split(':')[0]
igor@0 134
igor@0 135
igor@2 136 def make_links():
igor@2 137
igor@9 138 print "###################################################"
igor@9 139 print "# MAKE SYMBOLIC LINKS TO /dev/drbd* FILES"
igor@9 140 print "#"
igor@2 141
igor@2 142 # drbd_devices={
igor@2 143 # 'dev' : 'resource',
igor@2 144 # }
igor@2 145
igor@2 146 make_drbd_hash()
igor@2 147 print "mkdir -p /dev/drbd"
igor@2 148 for dev in drbd_device.keys():
igor@2 149 resource=drbd_device[dev]
igor@2 150 print "ln -sf /dev/%s /dev/drbd/%s" %(dev, resource)
igor@0 151
igor@0 152 def make_fs():
igor@9 153
igor@9 154 print "###################################################"
igor@9 155 print "# MAKE FILESYSTEMS ON THE DRBDs"
igor@9 156 print "#"
igor@9 157
igor@9 158 for dev in drbd_device.keys():
igor@9 159 print "mkfs.ext3 %s /dev/%s" %(mkfs_options,dev)
igor@0 160
igor@0 161
igor@0 162 def make_mount():
igor@9 163 print "###################################################"
igor@9 164 print "# MOUNT U-DOMAINS FILESYSTEMS"
igor@9 165 print "#"
igor@9 166
igor@2 167 print "mkdir -p /domU"
igor@33 168 for dom in linux_domains:
igor@2 169 print "mkdir -p /domU/%s" % (dom)
igor@2 170 print "mount /dev/drbd/%s /domU/%s" % (dom,dom)
igor@0 171
igor@0 172 def make_umount():
igor@33 173 for dom in linux_domains:
igor@2 174 print "umount /domU/%s" % (dom)
igor@0 175
igor@0 176 def make_dns():
igor@2 177 i=1
igor@33 178 for dom in linux_domains:
igor@2 179 print "%s A %s.%s" % (dom, ip_network, i)
igor@2 180 i+=1
igor@0 181
igor@0 182 def make_dns_reverse():
igor@2 183 i=1
igor@33 184 for dom in linux_domains:
igor@2 185 print "%s A %s.%s." % (i, dom, domain_name)
igor@2 186 i+=1
igor@0 187
igor@0 188 def make_install_debian():
igor@2 189 print """
igor@17 190 apt-get install debootstrap
igor@0 191 debootstrap %s /domU/%s %s
igor@0 192 chroot /domU/%s apt-get install -y --force-yes %s
igor@2 193 """ % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install)
igor@0 194
igor@0 195 def make_rsync():
igor@2 196 for dom in domains[1:]:
igor@2 197 print """
igor@0 198 rsync -a /domU/%s/ /domU/%s/
igor@2 199 """ % (domains[0], dom)
igor@0 200
igor@0 201 def make_etcfstab():
igor@2 202 i=0
igor@33 203 for dom in linux_domains:
igor@2 204 i+=1
igor@2 205 print """
igor@0 206 cat <<FSTAB > /domU/%s/etc/fstab
igor@0 207 proc /proc proc defaults 0 0
igor@0 208 /dev/hda1 / ext3 defaults,errors=remount-ro 0 1
igor@0 209 FSTAB
igor@2 210 """ % (dom)
igor@0 211
igor@0 212 def make_etcnetworkinterfaces():
igor@2 213 i=0
igor@33 214 for dom in linux_domains:
igor@2 215 i+=1
igor@2 216 print """
igor@0 217 cat <<INTERFACES > /domU/%s/etc/network/interfaces
igor@0 218 auto lo eth0
igor@0 219 iface lo inet loopback
igor@0 220
igor@0 221 iface eth0 inet static
igor@9 222 address %s
igor@0 223 netmask 255.255.255.0
igor@0 224 network %s.0
igor@0 225 broadcast %s.255
igor@0 226 gateway %s
igor@0 227 dns-nameservers %s
igor@0 228 dns-search %s
igor@0 229 INTERFACES
igor@9 230 """ % (dom,ip_address_table[dom],ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
igor@0 231
igor@0 232 def make_etcresolvconf():
igor@33 233 for dom in linux_domains:
igor@2 234 print """
igor@0 235 cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
igor@0 236 search %s
igor@0 237 nameserver %s
igor@0 238 RESOLVCONF
igor@2 239 """ % (dom,domain_name,ip_nameserver)
igor@0 240
igor@0 241 def make_etchostname():
igor@33 242 for dom in linux_domains:
igor@2 243 print """
igor@0 244 cat <<HOSTNAME > /domU/%s/etc/hostname
igor@0 245 %s
igor@0 246 HOSTNAME
igor@2 247 """ % (dom,dom)
igor@0 248
igor@0 249
igor@0 250 def make_etchosts():
igor@2 251 i=0
igor@33 252 for dom in linux_domains:
igor@2 253 i+=1
igor@2 254 print """
igor@0 255 cat <<HOSTS > /domU/%s/etc/hosts
igor@0 256 127.0.0.1 localhost
igor@9 257 %s %s.%s %s
igor@0 258
igor@0 259 ::1 ip6-localhost ip6-loopback
igor@0 260 fe00::0 ip6-localnet
igor@0 261 ff00::0 ip6-mcastprefix
igor@0 262 ff02::1 ip6-allnodes
igor@0 263 ff02::2 ip6-allrouters
igor@0 264 ff02::3 ip6-allhosts
igor@0 265 HOSTS
igor@9 266 """ % (dom,ip_address_table[dom],dom,domain_name,dom)
igor@0 267
igor@0 268 def make_rootssh():
igor@33 269 for dom in linux_domains:
igor@2 270 print """
igor@14 271 mkdir -p /domU/%s/home/eb/.ssh/
igor@14 272 chown -R 1000:1000 /domU/%s/home/eb
igor@0 273 cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
igor@14 274 chmod 600 /domU/%s/home/eb/.ssh/authorized_keys
igor@16 275 """ % (dom,dom,dom,dom)
igor@0 276
igor@0 277 def make_l3rc():
igor@33 278 for dom in linux_domains:
igor@2 279 print """
igor@2 280 cat <<EOF > /domU/%s/root/.l3rc
igor@0 281 l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
igor@0 282 EOF
igor@2 283 """ % (dom,dom,domain_name)
igor@0 284
igor@0 285
igor@0 286 def make_etc():
igor@2 287 make_etcfstab()
igor@2 288 make_etcnetworkinterfaces()
igor@2 289 make_etcresolvconf()
igor@2 290 make_etchosts()
igor@2 291 make_etchostname()
igor@2 292 make_rootssh()
igor@15 293 #make_l3rc()
igor@0 294
igor@9 295 def make_drbd():
igor@9 296 make_lvm()
igor@9 297 make_drbdconf()
igor@9 298 make_drbdmeta()
igor@9 299
igor@9 300 def make_domains():
igor@9 301 make_drbdprimary()
igor@9 302 make_links()
igor@2 303 make_fs()
igor@2 304 make_mount()
igor@2 305 make_install_debian()
igor@2 306 make_rsync()
igor@2 307 make_etc()
igor@2 308 make_umount()
igor@0 309
igor@0 310 def get_management_ip():
igor@2 311 return management_ip
igor@0 312
igor@0 313 def get_default_gateway():
igor@2 314 return management_gw
igor@0 315
igor@0 316 def make_bridges():
igor@2 317 ip=get_management_ip()
igor@2 318 gw=get_default_gateway()
igor@21 319 print "ifconfig %s 0.0.0.0 down" % (trunk)
igor@2 320
igor@2 321 # tagged interfaces
igor@2 322 for bridge in bridges:
igor@2 323 vlan=vlans[bridges.index(bridge)]
igor@2 324 if vlan == 'tagged':
igor@2 325 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk)
igor@2 326 print "ip link set %s up" % (trunk)
igor@2 327
igor@2 328 # untagged interfaces
igor@2 329 for bridge in bridges:
igor@2 330 vlan=vlans[bridges.index(bridge)]
igor@2 331 if vlan != 'tagged':
igor@2 332 print "vconfig add %s %s" % (trunk,vlan)
igor@2 333 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan)
igor@2 334 print "ip link set %s.%s up" % (trunk,vlan)
igor@2 335
igor@22 336 print "ifconfig %s.%s %s netmask %s" % (trunk,management_vlan,management_ip,management_netmask)
igor@2 337 if management_gw:
igor@2 338 print "route add default gw %s" % (management_gw)
igor@0 339
igor@0 340
igor@0 341
igor@0 342 #make_all_system()
igor@0 343
igor@0 344 if len(sys.argv) > 1:
igor@2 345 command = sys.argv[2]
igor@9 346 if command == 'make-drbd':
igor@9 347 make_drbd()
igor@9 348 elif command == 'make-domains':
igor@9 349 make_domains()
igor@9 350 elif command == 'make-bridges':
igor@2 351 make_bridges()
igor@2 352 elif command == 'make-links':
igor@2 353 make_links()
igor@2 354 else:
igor@2 355 show_usage()
igor@0 356 else:
igor@2 357 show_usage()
igor@0 358