igor@0: #!/usr/bin/python igor@0: igor@0: import sys igor@0: igor@0: network=sys.argv[1] igor@0: try: igor@2: exec 'from %s import domains,disk_table,lvm_vg_name,node1,node1_ip,node2,node2_ip, \ igor@2: ip_network, domain_name, ip_nameserver, ip_gateway, apt_get_install, \ igor@2: debian_release, debian_mirror, \ igor@2: bridges, vlans, management_vlan, trunk, management_ip, management_gw ' % (network) igor@0: except: 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@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@2: drbd_device[disk]=dom igor@2: else: igor@2: drbd_device[disk.split(':')[0]]=disk.split(':')[1] igor@2: igor@2: def make_lvm(): igor@2: if do_print: igor@2: print "lvcreate -L %s -n %s /dev/%s" % ("10G", "meta", lvm_vg_name) igor@2: for dom in 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@2: if do_print: igor@2: print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[1], disk.split(':')[0], lvm_vg_name) igor@2: drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])]) igor@0: igor@0: igor@0: def make_drbdconf(): igor@2: print "cat < /etc/drbd.conf" igor@2: i=0 igor@2: for drbd in drbd_devices: igor@2: resource=drbd[0] igor@2: disk=drbd[1] igor@2: drbd_number=i igor@2: i+=1 igor@2: 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@0: """ % (resource, node1, drbd_number, disk, node1_ip, i+drbd_base_port, meta_disk, drbd_number, node2, drbd_number, disk, node2_ip, i+drbd_base_port, meta_disk, drbd_number ) igor@2: print "DRBD" igor@0: igor@0: igor@2: def make_links(): igor@2: 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@2: for dom in domains: igor@2: print "mkfs.ext3 /dev/drbd/"+dom igor@0: igor@0: igor@0: def make_mount(): igor@2: print "mkdir -p /domU" igor@2: for dom in 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@2: for dom in domains: igor@2: print "umount /domU/%s" % (dom) igor@0: igor@0: def make_dns(): igor@2: i=1 igor@2: for dom in 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@2: for dom in 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@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@2: for dom in 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@2: for dom in 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@0: address %s.%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@2: """ % (dom,ip_network,i,ip_network, ip_network, ip_gateway, domain_name, ip_nameserver) igor@0: igor@0: def make_etcresolvconf(): igor@2: for dom in 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@2: for dom in 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@2: for dom in domains: igor@2: i+=1 igor@2: print """ igor@0: cat < /domU/%s/etc/hosts igor@0: 127.0.0.1 localhost igor@0: 192.168.1.%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@2: """ % (dom,i,dom,domain_name,dom) igor@0: igor@0: def make_rootssh(): igor@2: for dom in domains: igor@2: print """ igor@0: mkdir -p /domU/%s/root/.ssh/ igor@0: cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys igor@0: chmod 600 /domU/%s/root/.ssh/authorized_keys igor@2: """ % (dom,dom,dom) igor@0: igor@0: def make_l3rc(): igor@2: for dom in 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@2: make_l3rc() igor@0: igor@0: def make_all_system(): igor@2: make_drbd_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@2: print "ifconfig %s 0.0.0.0" % (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@2: print "ifconfig %s.%s %s" % (trunk,management_vlan,management_ip) igor@2: if management_gw: igor@2: print "route add default gw %s" % (management_gw) igor@0: igor@0: igor@0: def show_usage(): igor@2: print """ igor@0: Usage: igor@2: xen-drbd-install (to view) igor@2: xen-drbd-install | sh -s (to run) igor@0: igor@2: is a name of the file, which contains network description igor@0: igor@0: Commands: igor@2: make-all igor@2: make-bridges igor@2: make-links 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@2: if command == 'make-all': igor@2: make_all_system() igor@2: if 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: