igor@0: #!/usr/bin/python igor@0: igor@37: import sys,re,getopt igor@20: sys.path.append('/etc/xen') igor@0: igor@10: def show_usage(): igor@10: print """ igor@10: Usage: igor@37: xen-drbd-install [options] (to view) igor@37: xen-drbd-install [options] | sh -s (to run) igor@10: igor@30: is the name of the file, which contains network description igor@10: igor@10: Commands: igor@10: make-drbd igor@10: make-domains igor@10: make-bridges igor@10: make-links igor@37: igor@37: Options: igor@37: --help (-h) -- show usage information root@53: --network=name (-n) -- the network descriptions is in the file name (default: network) igor@10: """ igor@10: igor@37: igor@37: igor@37: network='network' igor@37: try: igor@37: opts, args = getopt.getopt(sys.argv[1:], "hn:", ["help", "network="]) igor@37: except getopt.GetoptError, err: igor@38: print str(err) igor@37: usage() igor@37: sys.exit(2) igor@37: igor@37: for o, a in opts: igor@37: if o in ("-h", "--help"): igor@37: show_usage() igor@37: sys.exit() igor@37: elif o in ("-n", "--network"): igor@37: network = a igor@37: else: igor@37: assert False, "unhandled option" igor@37: igor@37: if len(args) != 1: igor@10: show_usage() igor@10: sys.exit(1) igor@10: igor@0: try: igor@39: execfile('/etc/xen/'+network) igor@38: except IOError: igor@38: print "Network configuration file <%s> not found " % network igor@38: if network == 'network': igor@38: print "You can use -n option to specify network filename" igor@38: sys.exit(2) igor@38: except NameError: igor@38: domain=domains[0] igor@48: execfile('/etc/xen/'+network) igor@0: igor@37: igor@0: drbd_base_port=7790 igor@0: meta_disk='/dev/%s/meta'%lvm_vg_name igor@2: drbd_device={} igor@0: igor@33: linux_domains=[] igor@33: for dom in domains: igor@33: if domain_types[domains.index(dom)] == 'linux': igor@33: linux_domains.append(dom) igor@33: igor@2: def make_drbd_hash(): igor@2: for dom in domains: igor@2: for disk in disk_table[dom]: igor@2: if disk.find(":") == -1: igor@36: if disk.find("=") == -1: igor@36: drbd_device[disk]=dom igor@36: else: igor@36: drbd_device[disk.split('=')[0]]=dom igor@2: else: igor@36: disk0=disk.split(':')[0] igor@36: drbd_device[disk0.split('=')[0]]=disk.split(':')[1] igor@2: igor@2: def make_lvm(): igor@9: print "###################################################" igor@9: print "# MAKE LVM FOR DOMAINS DRBD DEVICES" igor@9: print "#" igor@9: for lvm_pv_name in re.compile('\s+').split(lvm_pv_names): igor@9: print "pvcreate %s" % (lvm_pv_name) igor@9: print "vgcreate /dev/%s %s" % (lvm_vg_name, lvm_pv_names) igor@9: print "lvcreate -L %s -n %s /dev/%s" % (lvm_lv_drbd_meta_size, lvm_lv_drbd_meta_name, lvm_vg_name) igor@33: for dom in linux_domains: igor@2: for disk in disk_table[dom]: igor@2: if disk.find(":") == -1: igor@2: #if do_print: igor@2: # print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name) igor@2: #drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)]) igor@2: drbd_device[disk]=dom igor@2: else: igor@2: drbd_device[disk.split(':')[0]]=disk.split(':')[0] igor@12: print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[2], disk.split(':')[1], lvm_vg_name) igor@9: #drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])]) igor@9: print "" igor@0: igor@0: igor@0: def make_drbdconf(): igor@9: print "###################################################" igor@9: print "# MAKE DRBD CONFIG FILE /etc/drbd.conf" igor@9: print "#" igor@9: igor@2: print "cat < /etc/drbd.conf" igor@9: igor@9: igor@9: for dom in domains: igor@9: for disk in disk_table[dom]: igor@37: drbd_number=int(re.compile('drbd').sub('',(disk.split(':')[0]).split('=')[0] )) igor@9: resource=disk.split(':')[1] igor@9: disk="/dev/%s/%s" % (lvm_vg_name,resource) igor@9: igor@9: print """ igor@0: resource %s { igor@2: protocol C; igor@2: net { igor@2: allow-two-primaries; igor@2: after-sb-0pri discard-least-changes; igor@2: after-sb-1pri call-pri-lost-after-sb; igor@2: after-sb-2pri call-pri-lost-after-sb; igor@2: } igor@2: syncer { igor@2: rate 5M; igor@2: } igor@2: on %s igor@2: { igor@2: device /dev/drbd%s; igor@2: disk %s; igor@2: address %s:%s; igor@2: meta-disk %s[%s]; igor@2: } igor@2: on %s igor@2: { igor@2: device /dev/drbd%s; igor@2: disk %s; igor@2: address %s:%s; igor@2: meta-disk %s[%s]; igor@2: } igor@0: } igor@0: igor@9: """ % (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: print "DRBD" igor@9: print "" igor@9: igor@9: igor@9: def make_drbdmeta(): igor@9: print "###################################################" igor@9: print "# MAKE DRBD DEVICES " igor@9: print "#" igor@13: print "yes yes | drbdadm create-md all" igor@9: igor@9: def make_drbdprimary(): igor@9: print "###################################################" igor@9: print "# SET DRBD DEVICES PRIMARY " igor@9: print "#" igor@9: igor@9: print "drbdadm up all" igor@9: for dom in domains: igor@9: for disk in disk_table[dom]: root@51: print "drbdsetup /dev/%s primary -o" % disk.split(':')[0] igor@0: igor@0: igor@2: def make_links(): igor@2: igor@9: print "###################################################" igor@9: print "# MAKE SYMBOLIC LINKS TO /dev/drbd* FILES" igor@9: print "#" igor@2: igor@2: # drbd_devices={ igor@2: # 'dev' : 'resource', igor@2: # } igor@2: igor@2: make_drbd_hash() igor@2: print "mkdir -p /dev/drbd" igor@2: for dev in drbd_device.keys(): igor@2: resource=drbd_device[dev] igor@2: print "ln -sf /dev/%s /dev/drbd/%s" %(dev, resource) igor@0: igor@0: def make_fs(): igor@9: igor@9: print "###################################################" igor@9: print "# MAKE FILESYSTEMS ON THE DRBDs" igor@9: print "#" igor@9: igor@9: for dev in drbd_device.keys(): igor@9: print "mkfs.ext3 %s /dev/%s" %(mkfs_options,dev) igor@0: igor@0: igor@0: def make_mount(): igor@9: print "###################################################" igor@9: print "# MOUNT U-DOMAINS FILESYSTEMS" igor@9: print "#" igor@9: igor@2: print "mkdir -p /domU" igor@33: for dom in linux_domains: igor@2: print "mkdir -p /domU/%s" % (dom) igor@2: print "mount /dev/drbd/%s /domU/%s" % (dom,dom) igor@0: igor@0: def make_umount(): igor@33: for dom in linux_domains: igor@2: print "umount /domU/%s" % (dom) igor@0: igor@0: def make_dns(): igor@2: i=1 igor@33: for dom in linux_domains: igor@2: print "%s A %s.%s" % (dom, ip_network, i) igor@2: i+=1 igor@0: igor@0: def make_dns_reverse(): igor@2: i=1 igor@33: for dom in linux_domains: igor@2: print "%s A %s.%s." % (i, dom, domain_name) igor@2: i+=1 igor@0: igor@0: def make_install_debian(): igor@2: print """ igor@43: apt-get install debootstrap lsof igor@0: debootstrap %s /domU/%s %s root@55: LANG=C chroot /domU/%s apt-get install -y --force-yes %s igor@43: kill $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null igor@43: sleep 2 igor@43: kill -9 $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null igor@44: """ % (debian_release,linux_domains[0],debian_mirror,linux_domains[0],apt_get_install,linux_domains[0],linux_domains[0]) igor@0: igor@0: def make_rsync(): igor@44: for dom in linux_domains[1:]: igor@2: print """ root@55: echo -n Doing rsync for %s... root@55: rsync -a /domU/%s/ /domU/%s/ && echo Done root@55: """ % (dom, linux_domains[0], dom) igor@0: igor@43: igor@43: def make_install_packages(): igor@44: for dom in linux_domains: igor@43: try: igor@43: print """ root@55: LANG=C chroot /domU/%s apt-get install -y --force-yes %s igor@43: kill $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null igor@43: sleep 2 igor@43: kill -9 $(lsof +D /domU/%s | grep -v PID | awk '{print $2}' | sort -u) 2> /dev/null igor@43: """ % (dom,apt_get_install_table[dom],dom,dom) igor@43: except: igor@43: pass igor@43: igor@0: def make_etcfstab(): igor@2: i=0 igor@33: for dom in linux_domains: igor@2: i+=1 igor@2: print """ igor@0: cat < /domU/%s/etc/fstab igor@0: proc /proc proc defaults 0 0 igor@0: /dev/hda1 / ext3 defaults,errors=remount-ro 0 1 igor@0: FSTAB igor@2: """ % (dom) igor@0: igor@0: def make_etcnetworkinterfaces(): igor@2: i=0 igor@33: for dom in linux_domains: igor@2: i+=1 igor@2: print """ igor@0: cat < /domU/%s/etc/network/interfaces igor@0: auto lo eth0 igor@0: iface lo inet loopback igor@0: igor@0: iface eth0 inet static igor@9: address %s igor@0: netmask 255.255.255.0 igor@0: network %s.0 igor@0: broadcast %s.255 igor@0: gateway %s root@51: dns-search %s igor@0: dns-nameservers %s igor@0: INTERFACES igor@9: """ % (dom,ip_address_table[dom],ip_network, ip_network, ip_gateway, domain_name, ip_nameserver) igor@0: igor@0: def make_etcresolvconf(): igor@33: for dom in linux_domains: igor@2: print """ igor@0: cat < /domU/%s/etc/resolv.conf igor@0: search %s igor@0: nameserver %s igor@0: RESOLVCONF igor@2: """ % (dom,domain_name,ip_nameserver) igor@0: igor@0: def make_etchostname(): igor@33: for dom in linux_domains: igor@2: print """ igor@0: cat < /domU/%s/etc/hostname igor@0: %s igor@0: HOSTNAME igor@2: """ % (dom,dom) igor@0: igor@0: igor@0: def make_etchosts(): igor@2: i=0 igor@33: for dom in linux_domains: igor@2: i+=1 igor@2: print """ igor@0: cat < /domU/%s/etc/hosts igor@0: 127.0.0.1 localhost igor@9: %s %s.%s %s igor@0: igor@0: ::1 ip6-localhost ip6-loopback igor@0: fe00::0 ip6-localnet igor@0: ff00::0 ip6-mcastprefix igor@0: ff02::1 ip6-allnodes igor@0: ff02::2 ip6-allrouters igor@0: ff02::3 ip6-allhosts igor@0: HOSTS igor@9: """ % (dom,ip_address_table[dom],dom,domain_name,dom) igor@0: igor@0: def make_rootssh(): igor@33: for dom in linux_domains: igor@2: print """ root@55: mkdir -p /domU/%s/root/.ssh/ igor@0: cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys root@55: chmod 600 /domU/%s/root/.ssh/authorized_keys root@56: """ % (dom,dom,dom) igor@0: igor@0: def make_l3rc(): igor@33: for dom in linux_domains: igor@2: print """ igor@2: cat < /domU/%s/root/.l3rc igor@0: l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root igor@0: EOF igor@2: """ % (dom,dom,domain_name) igor@0: igor@0: igor@0: def make_etc(): igor@2: make_etcfstab() igor@2: make_etcnetworkinterfaces() igor@2: make_etcresolvconf() igor@2: make_etchosts() igor@2: make_etchostname() igor@2: make_rootssh() igor@15: #make_l3rc() igor@0: igor@9: def make_drbd(): igor@9: make_lvm() igor@9: make_drbdconf() igor@9: make_drbdmeta() igor@9: igor@9: def make_domains(): igor@9: make_drbdprimary() igor@9: make_links() igor@2: make_fs() igor@2: make_mount() igor@2: make_install_debian() igor@2: make_rsync() igor@43: make_install_packages() igor@2: make_etc() igor@2: make_umount() igor@0: igor@0: def get_management_ip(): igor@2: return management_ip igor@0: igor@0: def get_default_gateway(): igor@2: return management_gw igor@0: igor@0: def make_bridges(): igor@2: ip=get_management_ip() igor@2: gw=get_default_gateway() igor@2: root@53: trunk_bridge=trunk root@53: igor@2: # tagged interfaces igor@2: for bridge in bridges: igor@2: vlan=vlans[bridges.index(bridge)] igor@2: if vlan == 'tagged': root@53: if trunk_bridge != trunk: root@53: print "\n=====\nERROR: No more than one tagged bridge allowed.\nYou have defined the tagged bridge already, %s " % trunk_bridge root@53: sys.exit(1) root@53: trunk_bridge=bridge root@53: print "ifconfig %s 0.0.0.0 down" % (trunk) root@53: print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk) root@53: print "ip link set %s up" % (trunk_bridge) root@53: root@53: if trunk_bridge==trunk: root@53: print "ifconfig %s up" % (trunk) igor@2: igor@2: # untagged interfaces igor@2: for bridge in bridges: igor@2: vlan=vlans[bridges.index(bridge)] igor@2: if vlan != 'tagged': root@53: print "vconfig add %s %s" % (trunk_bridge,vlan) root@53: print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk_bridge,vlan) root@53: print "ip link set %s up" % (bridge) igor@2: root@53: print "ifconfig %s %s netmask %s" % (management_interface,management_ip,management_netmask) igor@2: if management_gw: igor@2: print "route add default gw %s" % (management_gw) igor@0: igor@0: igor@0: igor@0: #make_all_system() igor@0: igor@37: if len(args) > 0: igor@37: command = args[0] igor@9: if command == 'make-drbd': igor@9: make_drbd() igor@9: elif command == 'make-domains': igor@9: make_domains() igor@9: elif command == 'make-bridges': igor@2: make_bridges() igor@2: elif command == 'make-links': igor@2: make_links() igor@2: else: igor@2: show_usage() igor@0: else: igor@2: show_usage() igor@0: