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