xen-drbd

diff xen-drbd-install.py @ 0:b6dc3480caae

xen-drbd merge
author igor
date Thu Sep 27 18:56:29 2007 +0300 (2007-09-27)
parents
children afe3c0992ec3
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xen-drbd-install.py	Thu Sep 27 18:56:29 2007 +0300
     1.3 @@ -0,0 +1,273 @@
     1.4 +#!/usr/bin/python
     1.5 +
     1.6 +import sys
     1.7 +
     1.8 +network=sys.argv[1]
     1.9 +try:
    1.10 +	exec 'from %s import domains,disk_table,lvm_vg_name,node1,node1_ip,node2,node2_ip, \
    1.11 +		  ip_network, domain_name, ip_nameserver, ip_gateway, apt_get_install, \
    1.12 +		  debian_release, debian_mirror, \
    1.13 +		  bridges, vlans, management_vlan, trunk, management_ip, management_gw ' % (network)
    1.14 +except:
    1.15 +	print "Can't find or interpret module %s with topology description" %(network)
    1.16 +	sys.exit(1)
    1.17 +
    1.18 +drbd_base_port=7790
    1.19 +meta_disk='/dev/%s/meta'%lvm_vg_name
    1.20 +drbd_devices=[]
    1.21 +
    1.22 +def make_lvm(do_print=1):
    1.23 +		if do_print:
    1.24 +				print "lvcreate -L %s -n %s /dev/%s" % ("10G", "meta", lvm_vg_name)
    1.25 +		for dom in domains:
    1.26 +			for disk in disk_table[dom]:
    1.27 +				if disk.find(":") == -1:
    1.28 +					if do_print:
    1.29 +						print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name)
    1.30 +					drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)])
    1.31 +				else:
    1.32 +					if do_print:
    1.33 +						print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[1], disk.split(':')[0], lvm_vg_name)
    1.34 +					drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])])
    1.35 +
    1.36 +
    1.37 +def make_drbdconf():
    1.38 +		print "cat <<DRBD > /etc/drbd.conf"
    1.39 +		i=0
    1.40 +		for drbd in drbd_devices:
    1.41 +			resource=drbd[0]
    1.42 +			disk=drbd[1]
    1.43 +			drbd_number=i
    1.44 +			i+=1
    1.45 +			print """
    1.46 +resource %s {
    1.47 +	protocol C;
    1.48 +	net { 
    1.49 +		allow-two-primaries;
    1.50 +	 	after-sb-0pri discard-least-changes;
    1.51 +	  	after-sb-1pri call-pri-lost-after-sb;
    1.52 +	   	after-sb-2pri call-pri-lost-after-sb;
    1.53 +	}
    1.54 +	syncer { 
    1.55 +		rate 5M; 
    1.56 +	}
    1.57 +	on %s 
    1.58 +	{
    1.59 +		device /dev/drbd%s;
    1.60 +  		disk %s;
    1.61 +		address %s:%s;
    1.62 +	    meta-disk %s[%s];
    1.63 +	}
    1.64 +	on %s 
    1.65 +	{
    1.66 +		device /dev/drbd%s;
    1.67 +  		disk %s;
    1.68 +		address %s:%s;
    1.69 +	    meta-disk %s[%s];
    1.70 +	}
    1.71 +}
    1.72 +
    1.73 +""" % (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 )
    1.74 +		print "DRBD"
    1.75 +
    1.76 +
    1.77 +def make_drbd_links():
    1.78 +	make_lvm(0)
    1.79 +	print "mkdir -p /dev/drbd"
    1.80 +	i=0
    1.81 +	for drbd in drbd_devices:
    1.82 +		drbd_number=i
    1.83 +		i+=1
    1.84 +		resource=drbd[0]
    1.85 +		disk=drbd[1]
    1.86 +		print "ln -sf /dev/drbd%s /dev/drbd/%s"	%(drbd_number, resource)
    1.87 +
    1.88 +def make_fs():
    1.89 +	for dom in domains:
    1.90 +		print "mkfs.ext3 /dev/drbd/"+dom
    1.91 +
    1.92 +
    1.93 +def make_mount():
    1.94 +	print "mkdir -p /domU"
    1.95 +	for dom in domains:
    1.96 +		print "mkdir -p /domU/%s" % (dom)
    1.97 +		print "mount /dev/drbd/%s /domU/%s" % (dom,dom)
    1.98 +
    1.99 +def make_umount():
   1.100 +	for dom in domains:
   1.101 +		print "umount /domU/%s" % (dom)
   1.102 +
   1.103 +def make_dns():
   1.104 +	i=1
   1.105 +	for dom in domains:
   1.106 +		print "%s A %s.%s" % (dom, ip_network, i)
   1.107 +		i+=1
   1.108 +
   1.109 +def make_dns_reverse():
   1.110 +	i=1
   1.111 +	for dom in domains:
   1.112 +		print "%s A %s.%s." % (i, dom, domain_name)
   1.113 +		i+=1
   1.114 +
   1.115 +def make_install_debian():
   1.116 +	print """
   1.117 +debootstrap %s /domU/%s %s
   1.118 +chroot /domU/%s apt-get install -y --force-yes %s
   1.119 +	""" % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install)
   1.120 +
   1.121 +def make_rsync():
   1.122 +	for dom in domains[1:]:
   1.123 +		print """
   1.124 +rsync -a /domU/%s/ /domU/%s/
   1.125 +		""" % (domains[0], dom)
   1.126 +
   1.127 +def make_etcfstab():
   1.128 +	i=0
   1.129 +	for dom in domains:
   1.130 +		i+=1
   1.131 +		print """
   1.132 +cat <<FSTAB > /domU/%s/etc/fstab
   1.133 +proc                /proc           proc    defaults                    0       0
   1.134 +/dev/hda1           /               ext3    defaults,errors=remount-ro  0       1
   1.135 +FSTAB
   1.136 +		""" % (dom)
   1.137 +
   1.138 +def make_etcnetworkinterfaces():
   1.139 +	i=0
   1.140 +	for dom in domains:
   1.141 +		i+=1
   1.142 +		print """
   1.143 +cat <<INTERFACES > /domU/%s/etc/network/interfaces
   1.144 +auto lo eth0
   1.145 +iface lo inet loopback
   1.146 +
   1.147 +iface eth0 inet static
   1.148 +    address %s.%s
   1.149 +    netmask 255.255.255.0
   1.150 +    network %s.0
   1.151 +    broadcast %s.255
   1.152 +    gateway %s
   1.153 +    dns-nameservers %s
   1.154 +    dns-search %s
   1.155 +INTERFACES
   1.156 +		""" % (dom,ip_network,i,ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
   1.157 +
   1.158 +def make_etcresolvconf():
   1.159 +	for dom in domains:
   1.160 +		print """
   1.161 +cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
   1.162 +search %s
   1.163 +nameserver %s
   1.164 +RESOLVCONF
   1.165 +		""" % (dom,domain_name,ip_nameserver)
   1.166 +
   1.167 +def make_etchostname():
   1.168 +	for dom in domains:
   1.169 +		print """
   1.170 +cat <<HOSTNAME > /domU/%s/etc/hostname
   1.171 +%s
   1.172 +HOSTNAME
   1.173 +		""" % (dom,dom)
   1.174 +
   1.175 +
   1.176 +def make_etchosts():
   1.177 +	i=0
   1.178 +	for dom in domains:
   1.179 +		i+=1
   1.180 +		print """
   1.181 +cat <<HOSTS > /domU/%s/etc/hosts
   1.182 +127.0.0.1       localhost
   1.183 +192.168.1.%s   %s.%s  %s
   1.184 +
   1.185 +::1     ip6-localhost ip6-loopback
   1.186 +fe00::0 ip6-localnet
   1.187 +ff00::0 ip6-mcastprefix
   1.188 +ff02::1 ip6-allnodes
   1.189 +ff02::2 ip6-allrouters
   1.190 +ff02::3 ip6-allhosts
   1.191 +HOSTS
   1.192 +		""" % (dom,i,dom,domain_name,dom)
   1.193 +
   1.194 +def make_rootssh():
   1.195 +	for dom in domains:
   1.196 +		print """
   1.197 +mkdir -p /domU/%s/root/.ssh/
   1.198 +cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
   1.199 +chmod 600 /domU/%s/root/.ssh/authorized_keys
   1.200 +		"""  % (dom,dom,dom)
   1.201 +
   1.202 +def make_l3rc():
   1.203 +	for dom in domains:
   1.204 +		print """
   1.205 +		cat <<EOF > /domU/%s/root/.l3rc
   1.206 +l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
   1.207 +EOF
   1.208 +		"""  % (dom,dom,domain_name)
   1.209 +
   1.210 +
   1.211 +def make_etc():
   1.212 +		make_etcfstab()
   1.213 +		make_etcnetworkinterfaces()
   1.214 +		make_etcresolvconf()
   1.215 +		make_etchosts()
   1.216 +		make_etchostname()
   1.217 +		make_rootssh()
   1.218 +		make_l3rc()
   1.219 +
   1.220 +def make_all_system():
   1.221 +	make_drbd_links()
   1.222 +	make_fs()
   1.223 +	make_mount()
   1.224 +	make_install_debian()
   1.225 +	make_rsync()
   1.226 +	make_etc()
   1.227 +	make_umount()
   1.228 +
   1.229 +def get_management_ip():
   1.230 +	return management_ip
   1.231 +
   1.232 +def get_default_gateway():
   1.233 +	return management_gw
   1.234 +
   1.235 +def make_bridges():
   1.236 +	ip=get_management_ip()
   1.237 +	gw=get_default_gateway()
   1.238 +	print "ifconfig %s 0.0.0.0" % (trunk)
   1.239 +	for bridge in bridges:
   1.240 +		vlan=vlans[bridges.index(bridge)]
   1.241 +		print "vconfig add %s %s" % (trunk,vlan)
   1.242 +		print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan)
   1.243 +	print "ifconfig %s.%s %s" % (trunk,management_vlan,management_ip)
   1.244 +	if management_gw:
   1.245 +			print "route add default gw %s" % (management_gw)
   1.246 +
   1.247 +
   1.248 +def show_usage():
   1.249 +	print """
   1.250 +Usage:
   1.251 +	xen-drbd-install <network> <command>  			(to view)
   1.252 +	xen-drbd-install <network> <command> | sh -s 	(to run)
   1.253 +
   1.254 +	<network> is a name of the file, which contains network description
   1.255 +
   1.256 +Commands:
   1.257 +	make-all
   1.258 +	make-bridges
   1.259 +	make-links
   1.260 +"""
   1.261 +
   1.262 +#make_all_system()
   1.263 +
   1.264 +if len(sys.argv) > 1:
   1.265 +	command = sys.argv[2]
   1.266 +	if command == 'make-all':
   1.267 +		make_all_system()
   1.268 +	if command == 'make-bridges':	
   1.269 +		make_bridges()
   1.270 +	elif command == 'make-links':	
   1.271 +		make_drbd_links()
   1.272 +	else:
   1.273 +		show_usage()
   1.274 +else:
   1.275 +	show_usage()
   1.276 +