igor@0: #!/usr/bin/python igor@0: igor@9: import sys,re igor@20: sys.path.append('/etc/xen') igor@0: igor@10: def show_usage(): igor@10: print """ igor@10: Usage: igor@10: xen-drbd-install (to view) igor@10: xen-drbd-install | 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@10: """ igor@10: igor@11: if len(sys.argv) != 3: igor@10: show_usage() igor@10: sys.exit(1) igor@10: igor@10: igor@0: network=sys.argv[1] igor@0: try: igor@9: exec 'from %s import * ' % (network) igor@23: except ImportError: igor@2: print "Can't find or interpret module %s with topology description" %(network) igor@2: sys.exit(1) igor@0: 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@9: drbd_number=int(re.compile('drbd').sub('',disk.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]: igor@9: print "drbdsetup %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@17: apt-get install debootstrap igor@0: debootstrap %s /domU/%s %s igor@0: chroot /domU/%s apt-get install -y --force-yes %s igor@2: """ % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install) igor@0: igor@0: def make_rsync(): igor@2: for dom in domains[1:]: igor@2: print """ igor@0: rsync -a /domU/%s/ /domU/%s/ igor@2: """ % (domains[0], dom) igor@0: 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 igor@0: dns-nameservers %s igor@0: dns-search %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 """ igor@14: mkdir -p /domU/%s/home/eb/.ssh/ igor@14: chown -R 1000:1000 /domU/%s/home/eb igor@0: cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys igor@14: chmod 600 /domU/%s/home/eb/.ssh/authorized_keys igor@16: """ % (dom,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@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@21: print "ifconfig %s 0.0.0.0 down" % (trunk) igor@2: igor@2: # tagged interfaces igor@2: for bridge in bridges: igor@2: vlan=vlans[bridges.index(bridge)] igor@2: if vlan == 'tagged': igor@2: print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s" % (bridges.index(bridge)+2,bridge,trunk) igor@2: print "ip link set %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': igor@2: print "vconfig add %s %s" % (trunk,vlan) igor@2: print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan) igor@2: print "ip link set %s.%s up" % (trunk,vlan) igor@2: igor@22: print "ifconfig %s.%s %s netmask %s" % (trunk,management_vlan,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@0: if len(sys.argv) > 1: igor@2: command = sys.argv[2] 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: