xen-drbd

annotate xen-drbd-install.py @ 21:fc201b0bc6f9

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