xen-drbd

changeset 0:b6dc3480caae

xen-drbd merge
author igor
date Thu Sep 27 18:56:29 2007 +0300 (2007-09-27)
parents
children d6f7c53b085d
files INSTALL config-example.py default-xen-drbd init.d-xen-drbd xen-drbd-install.py xen-drbd-start.py xen-drbd.py xen-scripts-network-xen-drbd
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/INSTALL	Thu Sep 27 18:56:29 2007 +0300
     1.3 @@ -0,0 +1,27 @@
     1.4 +#!/bin/sh
     1.5 +
     1.6 +DEST_DIR=/usr/bin
     1.7 +
     1.8 +cp init.d-xen-drbd /etc/init.d/xen-drbd
     1.9 +chmod +x /etc/init.d/xen-drbd
    1.10 +
    1.11 +cp xen-scripts-network-xen-drbd /etc/xen/scripts/network-xen-drbd
    1.12 +chmod +x /etc/xen/scripts/network-xen-drbd
    1.13 +
    1.14 +cp default-xen-drbd /etc/default/xen-drbd
    1.15 +cp xen-drbd-start.py /etc/xen/xen-drbd-start
    1.16 +cp xen-drbd.py ${DEST_DIR}
    1.17 +
    1.18 +for i in 2 3 4 5
    1.19 +do
    1.20 +	ln -sf ../init.d/xen-drbd /etc/rc$i.d/S95xen-drbd
    1.21 +	#rm /etc/rc$i.d/S22xen-drbd
    1.22 +done
    1.23 +
    1.24 +for i in 0 1 6
    1.25 +do
    1.26 +	ln -sf ../init.d/xen-drbd /etc/rc$i.d/K05xen-drbd
    1.27 +	#rm /etc/rc$i.d/K21xen-drbd
    1.28 +done
    1.29 +
    1.30 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/config-example.py	Thu Sep 27 18:56:29 2007 +0300
     2.3 @@ -0,0 +1,86 @@
     2.4 +
     2.5 +node1='dom0'
     2.6 +node2='dom0m'
     2.7 +i_am=node1
     2.8 +
     2.9 +ip_address = {
    2.10 +	node1: '192.168.1.190',
    2.11 +	node2: '192.168.1.191'
    2.12 +}
    2.13 +
    2.14 +node1_ip=ip_address[node1]
    2.15 +node2_ip=ip_address[node2]
    2.16 +
    2.17 +domains=['log', 'dns', 'gw', 'igw', 'ldap', 'mail', 'proxy', 'samba', 'vpn', 'intraweb', 'jabber'] 
    2.18 +
    2.19 +domain_home = {
    2.20 +		node1 : ['log', 'dns', 'gw', 'igw', 'ldap', 'mail', 'proxy', 'vpn', 'intraweb', 'jabber'], 
    2.21 +		node2 : ['samba'], 
    2.22 +	}
    2.23 +
    2.24 +kernel = "/boot/vmlinuz-2.6.18-5-xen-686"
    2.25 +ramdisk = "/boot/initrd.img-2.6.18-5-xen-686"
    2.26 +
    2.27 +mem_table={
    2.28 +	'log'	:256, 
    2.29 +	'dns'	:64, 
    2.30 +	'gw' 	:64, 
    2.31 +	'igw'	:128, 
    2.32 +	'ldap'	:64, 
    2.33 +	'mail'	:256, 
    2.34 +	'proxy'	:256, 
    2.35 +	'samba'	:256, 
    2.36 +	'vpn'	:64,
    2.37 +	'web'	:128,
    2.38 +	'intraweb':128,
    2.39 +	'jabber':64,
    2.40 +}
    2.41 +
    2.42 +lvm_vg_name="XEN"
    2.43 +disk_table={
    2.44 +	'log'		: ['10G'], 
    2.45 +	'dns'		: ['2G'], 
    2.46 +	'gw' 		: ['2G'], 
    2.47 +	'igw'		: ['2G'], 
    2.48 +	'ldap'		: ['2G'], 
    2.49 +	'mail'		: ['2G','maildir:2G'], 
    2.50 +	'proxy'		: ['10G'], 
    2.51 +	'samba'		: ['2G','samba-share:2G'],
    2.52 +	'vpn'		: ['2G'],
    2.53 +	'web'		: ['10G'],
    2.54 +	'intraweb'	: ['10G'],
    2.55 +	'jabber'	: ['2G'],
    2.56 +}
    2.57 +
    2.58 +bridges=['br0','br1']
    2.59 +vlans=[4094,4093]
    2.60 +management_vlan=4094		# vlan 2
    2.61 +trunk='eth1'
    2.62 +management_ip=ip_address[i_am]
    2.63 +management_gw='192.168.1.197'
    2.64 +
    2.65 +vbridges_table={
    2.66 +	'log'		: ['br1'], 
    2.67 +	'dns'		: ['br1'], 
    2.68 +	'gw' 		: ['br0', 'br1'], 
    2.69 +	'igw'		: ['br1'], 
    2.70 +	'ldap'		: ['br1'], 
    2.71 +	'mail'		: ['br1'], 
    2.72 +	'proxy'		: ['br1'], 
    2.73 +	'samba'		: ['br1'],
    2.74 +	'vpn'		: ['br1'],
    2.75 +	'web'		: ['br1'],
    2.76 +	'intraweb'	: ['br1'],
    2.77 +	'jabber'	: ['br1'],
    2.78 +}
    2.79 +
    2.80 +ip_network="192.168.3"
    2.81 +domain_name="example.com"
    2.82 +ip_nameserver="192.168.3.2"
    2.83 +ip_gateway="192.168.3.3"
    2.84 +
    2.85 +debian_release="etch"
    2.86 +debian_mirror="http://debian.org.ua/debian"
    2.87 +apt_get_install="less tcpdump libpam-ldap libnss-ldap snmpd libc6-xen openssh-server"
    2.88 +
    2.89 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/default-xen-drbd	Thu Sep 27 18:56:29 2007 +0300
     3.3 @@ -0,0 +1,4 @@
     3.4 +XEN_DRBD_PATH='/usr/local/bin'
     3.5 +TOPOLOGY_NAME='eb'
     3.6 +START_ACTION=nothing 	#migrate-and-start-all
     3.7 +
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/init.d-xen-drbd	Thu Sep 27 18:56:29 2007 +0300
     4.3 @@ -0,0 +1,25 @@
     4.4 +#!/bin/sh
     4.5 +
     4.6 +XEN_DRBD_PATH='/usr/bin'
     4.7 +TOPOLOGY_NAME='xen-drbd'
     4.8 +START_ACTION='nothing'
     4.9 +[ -e /etc/default/xen-drbd ] && . /etc/default/xen-drbd
    4.10 +
    4.11 +case $1 in
    4.12 +		start)
    4.13 +			$XEN_DRBD_PATH/xen-drbd-install.py ${TOPOLOGY_NAME} make-links | sh -s
    4.14 +			if [ -z "$START_ACTION" ] || echo $START_ACTION | grep -qix nothing 
    4.15 +			then
    4.16 +				true
    4.17 +			else
    4.18 +				$XEN_DRBD_PATH/xen-drbd.py $START_ACTION
    4.19 +			fi
    4.20 +		;;
    4.21 +		stop)
    4.22 +			$XEN_DRBD_PATH/xen-drbd.py migrate-all-out
    4.23 +		;;
    4.24 +		*)
    4.25 +			echo $0 'start|stop'
    4.26 +		;;
    4.27 +esac
    4.28 +
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/xen-drbd-install.py	Thu Sep 27 18:56:29 2007 +0300
     5.3 @@ -0,0 +1,273 @@
     5.4 +#!/usr/bin/python
     5.5 +
     5.6 +import sys
     5.7 +
     5.8 +network=sys.argv[1]
     5.9 +try:
    5.10 +	exec 'from %s import domains,disk_table,lvm_vg_name,node1,node1_ip,node2,node2_ip, \
    5.11 +		  ip_network, domain_name, ip_nameserver, ip_gateway, apt_get_install, \
    5.12 +		  debian_release, debian_mirror, \
    5.13 +		  bridges, vlans, management_vlan, trunk, management_ip, management_gw ' % (network)
    5.14 +except:
    5.15 +	print "Can't find or interpret module %s with topology description" %(network)
    5.16 +	sys.exit(1)
    5.17 +
    5.18 +drbd_base_port=7790
    5.19 +meta_disk='/dev/%s/meta'%lvm_vg_name
    5.20 +drbd_devices=[]
    5.21 +
    5.22 +def make_lvm(do_print=1):
    5.23 +		if do_print:
    5.24 +				print "lvcreate -L %s -n %s /dev/%s" % ("10G", "meta", lvm_vg_name)
    5.25 +		for dom in domains:
    5.26 +			for disk in disk_table[dom]:
    5.27 +				if disk.find(":") == -1:
    5.28 +					if do_print:
    5.29 +						print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name)
    5.30 +					drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)])
    5.31 +				else:
    5.32 +					if do_print:
    5.33 +						print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[1], disk.split(':')[0], lvm_vg_name)
    5.34 +					drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])])
    5.35 +
    5.36 +
    5.37 +def make_drbdconf():
    5.38 +		print "cat <<DRBD > /etc/drbd.conf"
    5.39 +		i=0
    5.40 +		for drbd in drbd_devices:
    5.41 +			resource=drbd[0]
    5.42 +			disk=drbd[1]
    5.43 +			drbd_number=i
    5.44 +			i+=1
    5.45 +			print """
    5.46 +resource %s {
    5.47 +	protocol C;
    5.48 +	net { 
    5.49 +		allow-two-primaries;
    5.50 +	 	after-sb-0pri discard-least-changes;
    5.51 +	  	after-sb-1pri call-pri-lost-after-sb;
    5.52 +	   	after-sb-2pri call-pri-lost-after-sb;
    5.53 +	}
    5.54 +	syncer { 
    5.55 +		rate 5M; 
    5.56 +	}
    5.57 +	on %s 
    5.58 +	{
    5.59 +		device /dev/drbd%s;
    5.60 +  		disk %s;
    5.61 +		address %s:%s;
    5.62 +	    meta-disk %s[%s];
    5.63 +	}
    5.64 +	on %s 
    5.65 +	{
    5.66 +		device /dev/drbd%s;
    5.67 +  		disk %s;
    5.68 +		address %s:%s;
    5.69 +	    meta-disk %s[%s];
    5.70 +	}
    5.71 +}
    5.72 +
    5.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 )
    5.74 +		print "DRBD"
    5.75 +
    5.76 +
    5.77 +def make_drbd_links():
    5.78 +	make_lvm(0)
    5.79 +	print "mkdir -p /dev/drbd"
    5.80 +	i=0
    5.81 +	for drbd in drbd_devices:
    5.82 +		drbd_number=i
    5.83 +		i+=1
    5.84 +		resource=drbd[0]
    5.85 +		disk=drbd[1]
    5.86 +		print "ln -sf /dev/drbd%s /dev/drbd/%s"	%(drbd_number, resource)
    5.87 +
    5.88 +def make_fs():
    5.89 +	for dom in domains:
    5.90 +		print "mkfs.ext3 /dev/drbd/"+dom
    5.91 +
    5.92 +
    5.93 +def make_mount():
    5.94 +	print "mkdir -p /domU"
    5.95 +	for dom in domains:
    5.96 +		print "mkdir -p /domU/%s" % (dom)
    5.97 +		print "mount /dev/drbd/%s /domU/%s" % (dom,dom)
    5.98 +
    5.99 +def make_umount():
   5.100 +	for dom in domains:
   5.101 +		print "umount /domU/%s" % (dom)
   5.102 +
   5.103 +def make_dns():
   5.104 +	i=1
   5.105 +	for dom in domains:
   5.106 +		print "%s A %s.%s" % (dom, ip_network, i)
   5.107 +		i+=1
   5.108 +
   5.109 +def make_dns_reverse():
   5.110 +	i=1
   5.111 +	for dom in domains:
   5.112 +		print "%s A %s.%s." % (i, dom, domain_name)
   5.113 +		i+=1
   5.114 +
   5.115 +def make_install_debian():
   5.116 +	print """
   5.117 +debootstrap %s /domU/%s %s
   5.118 +chroot /domU/%s apt-get install -y --force-yes %s
   5.119 +	""" % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install)
   5.120 +
   5.121 +def make_rsync():
   5.122 +	for dom in domains[1:]:
   5.123 +		print """
   5.124 +rsync -a /domU/%s/ /domU/%s/
   5.125 +		""" % (domains[0], dom)
   5.126 +
   5.127 +def make_etcfstab():
   5.128 +	i=0
   5.129 +	for dom in domains:
   5.130 +		i+=1
   5.131 +		print """
   5.132 +cat <<FSTAB > /domU/%s/etc/fstab
   5.133 +proc                /proc           proc    defaults                    0       0
   5.134 +/dev/hda1           /               ext3    defaults,errors=remount-ro  0       1
   5.135 +FSTAB
   5.136 +		""" % (dom)
   5.137 +
   5.138 +def make_etcnetworkinterfaces():
   5.139 +	i=0
   5.140 +	for dom in domains:
   5.141 +		i+=1
   5.142 +		print """
   5.143 +cat <<INTERFACES > /domU/%s/etc/network/interfaces
   5.144 +auto lo eth0
   5.145 +iface lo inet loopback
   5.146 +
   5.147 +iface eth0 inet static
   5.148 +    address %s.%s
   5.149 +    netmask 255.255.255.0
   5.150 +    network %s.0
   5.151 +    broadcast %s.255
   5.152 +    gateway %s
   5.153 +    dns-nameservers %s
   5.154 +    dns-search %s
   5.155 +INTERFACES
   5.156 +		""" % (dom,ip_network,i,ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
   5.157 +
   5.158 +def make_etcresolvconf():
   5.159 +	for dom in domains:
   5.160 +		print """
   5.161 +cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
   5.162 +search %s
   5.163 +nameserver %s
   5.164 +RESOLVCONF
   5.165 +		""" % (dom,domain_name,ip_nameserver)
   5.166 +
   5.167 +def make_etchostname():
   5.168 +	for dom in domains:
   5.169 +		print """
   5.170 +cat <<HOSTNAME > /domU/%s/etc/hostname
   5.171 +%s
   5.172 +HOSTNAME
   5.173 +		""" % (dom,dom)
   5.174 +
   5.175 +
   5.176 +def make_etchosts():
   5.177 +	i=0
   5.178 +	for dom in domains:
   5.179 +		i+=1
   5.180 +		print """
   5.181 +cat <<HOSTS > /domU/%s/etc/hosts
   5.182 +127.0.0.1       localhost
   5.183 +192.168.1.%s   %s.%s  %s
   5.184 +
   5.185 +::1     ip6-localhost ip6-loopback
   5.186 +fe00::0 ip6-localnet
   5.187 +ff00::0 ip6-mcastprefix
   5.188 +ff02::1 ip6-allnodes
   5.189 +ff02::2 ip6-allrouters
   5.190 +ff02::3 ip6-allhosts
   5.191 +HOSTS
   5.192 +		""" % (dom,i,dom,domain_name,dom)
   5.193 +
   5.194 +def make_rootssh():
   5.195 +	for dom in domains:
   5.196 +		print """
   5.197 +mkdir -p /domU/%s/root/.ssh/
   5.198 +cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
   5.199 +chmod 600 /domU/%s/root/.ssh/authorized_keys
   5.200 +		"""  % (dom,dom,dom)
   5.201 +
   5.202 +def make_l3rc():
   5.203 +	for dom in domains:
   5.204 +		print """
   5.205 +		cat <<EOF > /domU/%s/root/.l3rc
   5.206 +l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
   5.207 +EOF
   5.208 +		"""  % (dom,dom,domain_name)
   5.209 +
   5.210 +
   5.211 +def make_etc():
   5.212 +		make_etcfstab()
   5.213 +		make_etcnetworkinterfaces()
   5.214 +		make_etcresolvconf()
   5.215 +		make_etchosts()
   5.216 +		make_etchostname()
   5.217 +		make_rootssh()
   5.218 +		make_l3rc()
   5.219 +
   5.220 +def make_all_system():
   5.221 +	make_drbd_links()
   5.222 +	make_fs()
   5.223 +	make_mount()
   5.224 +	make_install_debian()
   5.225 +	make_rsync()
   5.226 +	make_etc()
   5.227 +	make_umount()
   5.228 +
   5.229 +def get_management_ip():
   5.230 +	return management_ip
   5.231 +
   5.232 +def get_default_gateway():
   5.233 +	return management_gw
   5.234 +
   5.235 +def make_bridges():
   5.236 +	ip=get_management_ip()
   5.237 +	gw=get_default_gateway()
   5.238 +	print "ifconfig %s 0.0.0.0" % (trunk)
   5.239 +	for bridge in bridges:
   5.240 +		vlan=vlans[bridges.index(bridge)]
   5.241 +		print "vconfig add %s %s" % (trunk,vlan)
   5.242 +		print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan)
   5.243 +	print "ifconfig %s.%s %s" % (trunk,management_vlan,management_ip)
   5.244 +	if management_gw:
   5.245 +			print "route add default gw %s" % (management_gw)
   5.246 +
   5.247 +
   5.248 +def show_usage():
   5.249 +	print """
   5.250 +Usage:
   5.251 +	xen-drbd-install <network> <command>  			(to view)
   5.252 +	xen-drbd-install <network> <command> | sh -s 	(to run)
   5.253 +
   5.254 +	<network> is a name of the file, which contains network description
   5.255 +
   5.256 +Commands:
   5.257 +	make-all
   5.258 +	make-bridges
   5.259 +	make-links
   5.260 +"""
   5.261 +
   5.262 +#make_all_system()
   5.263 +
   5.264 +if len(sys.argv) > 1:
   5.265 +	command = sys.argv[2]
   5.266 +	if command == 'make-all':
   5.267 +		make_all_system()
   5.268 +	if command == 'make-bridges':	
   5.269 +		make_bridges()
   5.270 +	elif command == 'make-links':	
   5.271 +		make_drbd_links()
   5.272 +	else:
   5.273 +		show_usage()
   5.274 +else:
   5.275 +	show_usage()
   5.276 +
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/xen-drbd-start.py	Thu Sep 27 18:56:29 2007 +0300
     6.3 @@ -0,0 +1,51 @@
     6.4 +#!/usr/bin/python
     6.5 +
     6.6 +
     6.7 +# External variables:
     6.8 +# * network
     6.9 +# * domain
    6.10 +
    6.11 +import sys
    6.12 +
    6.13 +sys.path.append('/root')
    6.14 +try:
    6.15 +	exec 'from %s import *' % (network)
    6.16 +except:
    6.17 +	print "Can't find or interpret module %s with topology description" %(network)
    6.18 +	sys.exit(1)
    6.19 +
    6.20 +name=domain
    6.21 +N = domains.index(domain)
    6.22 +
    6.23 +
    6.24 +memory=mem_table[domain]
    6.25 +
    6.26 +disk=[]
    6.27 +i=0
    6.28 +for disk_description in disk_table[domain]:
    6.29 +	i+=1
    6.30 +	if disk_description.find(":") == -1:
    6.31 +		disk.append('phy:/dev/drbd/%s,hda%s,w' % (domain,i) )
    6.32 +	else:
    6.33 +		disk.append('phy:/dev/drbd/%s,hda%s,w' % ((disk_description.split(':'))[0],i) )
    6.34 +
    6.35 +vif=[]
    6.36 +vbridges = vbridges_table[domain]
    6.37 +x=1
    6.38 +for i in vbridges:
    6.39 + vif.append('bridge='+i+',mac=00:16:3e:01:'+hex(int(N))[2:]+':'+hex(int('c0',16)+x)[2:])
    6.40 + x+=1
    6.41 +
    6.42 +root="/dev/hda1 ro"
    6.43 +extra=""
    6.44 +
    6.45 +def print_config():
    6.46 +    print "name =", name
    6.47 +    print "kernel =", kernel
    6.48 +    print "ramdisk =", ramdisk
    6.49 +    print "memory =", memory
    6.50 +    print "disk =", disk
    6.51 +    print "vif =", vif
    6.52 +    print "root =", root
    6.53 +    print "extra =", extra
    6.54 +
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/xen-drbd.py	Thu Sep 27 18:56:29 2007 +0300
     7.3 @@ -0,0 +1,238 @@
     7.4 +#!/usr/bin/python
     7.5 +
     7.6 +network='eb'
     7.7 +
     7.8 +import sys,os
     7.9 +from commands import mkarg
    7.10 +
    7.11 +sys.path.append('/etc/xen')
    7.12 +try:
    7.13 +        exec 'from %s import *' % (network)
    7.14 +except:
    7.15 +        print "Can't find or interpret module %s with topology description" %(network)
    7.16 +        sys.exit(1)
    7.17 +
    7.18 +domain_create_line="xm create xen-drbd-start network="+network+" domain=%s"
    7.19 +
    7.20 +def the_peer_of(node):
    7.21 +	if node == node1:
    7.22 +		another_node=node2
    7.23 +	else:
    7.24 +		another_node=node1
    7.25 +	return another_node
    7.26 +
    7.27 +he_is=the_peer_of(i_am)
    7.28 +
    7.29 +def log_error(error):
    7.30 +	print error
    7.31 +
    7.32 +def run_now(command,node=i_am):
    7.33 +	if node == i_am:
    7.34 +		line=command+" > /dev/stderr"
    7.35 +	else:
    7.36 +		line="ssh %s %s < /dev/null > /dev/stderr"  % (node,mkarg(command))
    7.37 +	(p, child_stdout, child_stderr) = os.popen3(line)
    7.38 +	output = child_stderr.read()
    7.39 +	#p = os.popen(line)
    7.40 +	#output = p.read()
    7.41 +	p.close()
    7.42 +	return output
    7.43 +
    7.44 +def run(command,node=i_am):
    7.45 +	run_now(command,node)
    7.46 +#	if node == i_am:
    7.47 +#		print command
    7.48 +#	else:
    7.49 +#		print "ssh %s %s < /dev/null > /dev/stderr" % (node,mkarg(command))
    7.50 +
    7.51 +
    7.52 +def get_drbd_resources(domain):
    7.53 +	disk=[]
    7.54 +	for disk_description in disk_table[domain]:
    7.55 +		if disk_description.find(":") == -1:
    7.56 +			disk.append(domain)
    7.57 +		else:
    7.58 +			disk.append((disk_description.split(':'))[0])
    7.59 +	return disk
    7.60 +	
    7.61 +
    7.62 +def set_drbd_primary(domain, node=i_am):
    7.63 +	drbd_resources=get_drbd_resources(domain)
    7.64 +	for drbd in drbd_resources:
    7.65 +		run("drbdadm primary %s"%(drbd),node)
    7.66 +
    7.67 +def set_drbd_secondary(domain, node=i_am):
    7.68 +	drbd_resources=get_drbd_resources(domain)
    7.69 +	for drbd in drbd_resources:
    7.70 +		run("drbdadm secondary %s"%(drbd),node)
    7.71 +
    7.72 +def get_domain_id(domain,node=i_am):
    7.73 +	"""
    7.74 +	Returns domain id of the <domain> or -1 if the <domain> is not running on the <node>
    7.75 +	"""
    7.76 +	res=run_now("xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain, node).rstrip("\n")
    7.77 +	if not res:
    7.78 +		res = -1
    7.79 +	else:
    7.80 +		res = int(res)
    7.81 +	return res
    7.82 +
    7.83 +def get_drbd_id(resource,node=i_am):
    7.84 +	res=run_now("ls -l /dev/drbd/%s 2> /dev/null | awk '{print $10}' | sed s@/dev/drbd@@" %resource, node).rstrip("\n")
    7.85 +	if not res:
    7.86 +		res = -1
    7.87 +	else:
    7.88 +		res = int(res)
    7.89 +	return res
    7.90 +
    7.91 +def get_drbd_state(resource,node=i_am):
    7.92 +	res=run_now("drbdadm state %s | sed s@/.*@@" %resource, node).rstrip("\n")
    7.93 +	if not res:
    7.94 +		res = -1
    7.95 +	return res
    7.96 +
    7.97 +def get_drbd_cstate(resource,node=i_am):
    7.98 +	res=run_now("drbdadm cstate %s " %resource, node).rstrip("\n")
    7.99 +	if not res:
   7.100 +		res = -1
   7.101 +	return res
   7.102 +
   7.103 +
   7.104 +def start_domain(domain,node=i_am):
   7.105 +	if (get_domain_id(domain,i_am) != -1):
   7.106 +		log_error("Domain %s is running already on the node %s" % (domain,i_am))
   7.107 +		return -1
   7.108 +	if (get_domain_id(domain,he_is) != -1):
   7.109 +		log_error("Domain %s is running already on the node %s" % (domain,he_is))
   7.110 +		return -1
   7.111 +	another_node=the_peer_of(node)
   7.112 +	set_drbd_secondary(domain,another_node)
   7.113 +	set_drbd_primary(domain,node)
   7.114 +	run(domain_create_line % domain)
   7.115 +
   7.116 +def migrate_domain_out(domain,node=i_am):
   7.117 +	if (get_domain_id(domain,node) == -1):
   7.118 +		log_error("Domain %s is not running on the node %s" % (domain,node))
   7.119 +		return -1
   7.120 +	another_node=the_peer_of(node)
   7.121 +	print "Migrating the domain <%s> from the node <%s> to the node <%s>" % (domain, node, another_node)
   7.122 +	set_drbd_primary(domain,another_node)
   7.123 +	run("xm migrate %s %s --live" % (domain,another_node),node)
   7.124 +	run("sleep 2")
   7.125 +	set_drbd_secondary(domain,node)
   7.126 +	print "+ Done"
   7.127 +	
   7.128 +def migrate_domain_in(domain,node=i_am):
   7.129 +	migrate_domain_out(domain,the_peer_of(node))
   7.130 +
   7.131 +def migrate_domain(domain,node):
   7.132 +	migrate_domain_in(domain,node)
   7.133 +
   7.134 +def running_domains(node=i_am):
   7.135 +	xm_domains=run_now("xm list | awk '{print $1}'", node).split("\n")
   7.136 +	return filter(lambda x: x in xm_domains, domains)
   7.137 +
   7.138 +def migrate_all_out(node=i_am):
   7.139 +	for domain in running_domains(node):
   7.140 +		migrate_domain_out(domain,node)
   7.141 +
   7.142 +def migrate_all_in(node=i_am):
   7.143 +	for domain in running_domains(the_peer_of(node)):
   7.144 +		migrate_domain_in(domain,node)
   7.145 +
   7.146 +def start_all(node=i_am):
   7.147 +	for domain in domains:
   7.148 +		if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
   7.149 +			start_domain(domain,node)
   7.150 +
   7.151 +def start_my_domains(node=i_am):
   7.152 +	for domain in domain_home[node]:
   7.153 +		if not domain in running_domains(node) and not domain in running_domains(the_peer_of(node)):
   7.154 +			start_domain(domain,node)
   7.155 +
   7.156 +def migrate_my_domains_home(node=i_am):
   7.157 +	for domain in domain_home[node]:
   7.158 +		if not domain in running_domains(node) and domain in running_domains(the_peer_of(node)):
   7.159 +			migrate_domain_in(domain,node)
   7.160 +	
   7.161 +def migrate_and_start_all(node=i_am):
   7.162 +	migrate_my_domains_home(node)
   7.163 +	start_my_domains(node)
   7.164 +
   7.165 +def show_usage():
   7.166 +	print """
   7.167 +Usage:
   7.168 +	xen-drbd command [domain]
   7.169 +
   7.170 +Commands:
   7.171 +	start domain
   7.172 +	start-all
   7.173 +	start-my-domains
   7.174 +
   7.175 +	migrate-out domain
   7.176 +	migrate-in domain
   7.177 +	migrate-all-out
   7.178 +	migrate-all-in
   7.179 +	migrate-my-domains-home
   7.180 +
   7.181 +	migrate-and-start-all
   7.182 +	"""
   7.183 +
   7.184 +def test():
   7.185 +	print "get_drbd_resources(samba)=",get_drbd_resources("samba")
   7.186 +	print "get_domain_id(Domain-0)=",get_domain_id("Domain-0")
   7.187 +	print "get_domain_id(samba)=",get_domain_id("samba")
   7.188 +	print "get_drbd_id(samba)=",get_drbd_id("samba")
   7.189 +	print "get_drbd_id(samba, he_is)=",get_drbd_id("samba",he_is)
   7.190 +	print "get_drbd_id(unknown_resource, he_is)=",get_drbd_id("unknown_resource",he_is)
   7.191 +	print "get_drbd_state(samba, he_is)=",get_drbd_state("samba")
   7.192 +	print "get_drbd_state(samba, he_is)=",get_drbd_state("samba",he_is)
   7.193 +	print "get_drbd_cstate(samba)=",get_drbd_cstate("samba")
   7.194 +	print "get_drbd_cstate(samba, he_is)=",get_drbd_cstate("samba",he_is)
   7.195 +	print "start_domain(samba)"
   7.196 +	start_domain("samba")
   7.197 +	print "migrate_domain_out(samba)"
   7.198 +	migrate_domain_in("samba")
   7.199 +
   7.200 +
   7.201 +#print "get_domain_id(samba,node2)=",get_domain_id("samba",node2)
   7.202 +#sys.exit(0)
   7.203 +
   7.204 +if len(sys.argv) == 1:
   7.205 +	show_usage()
   7.206 +	sys.exit(0)
   7.207 +
   7.208 +command=sys.argv[1]
   7.209 +if len(sys.argv) == 3:
   7.210 +	domain=sys.argv[2]
   7.211 +	if command == 'start':
   7.212 +		start_domain(domain)
   7.213 +	elif command == 'migrate-out':
   7.214 +		migrate_domain_out(domain)
   7.215 +	elif command == 'migrate-in':
   7.216 +		migrate_domain_in(domain)
   7.217 +	else:
   7.218 +		show_usage()
   7.219 +		sys.exit(0)
   7.220 +elif len(sys.argv) == 2:
   7.221 +	if command == 'start-all':
   7.222 +		start_all()
   7.223 +	elif command == 'start-my-domains':
   7.224 +		start_my_domains()
   7.225 +	elif command == 'migrate-all-out':
   7.226 +		migrate_all_out()
   7.227 +	elif command == 'migrate-all-in':
   7.228 +		migrate_all_in()
   7.229 +	elif command == 'migrate-my-domains-home':
   7.230 +		migrate_my_domains_home()
   7.231 +	elif command == 'migrate-and-start-all':
   7.232 +		migrate_and_start_all()
   7.233 +	elif command == 'list':
   7.234 +		print running_domains()
   7.235 +	else:
   7.236 +		show_usage()
   7.237 +		sys.exit(0)
   7.238 +
   7.239 +
   7.240 +
   7.241 +
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/xen-scripts-network-xen-drbd	Thu Sep 27 18:56:29 2007 +0300
     8.3 @@ -0,0 +1,17 @@
     8.4 +#!/bin/sh
     8.5 +
     8.6 +XEN_DRBD_PATH='/usr/bin'
     8.7 +TOPOLOGY_NAME='xen-drbd'
     8.8 +[ -e /etc/default/xen-drbd ] && . /etc/default/xen-drbd
     8.9 +
    8.10 +case $1 in
    8.11 +		start)
    8.12 +			$XEN_DRBD_PATH/xen-drbd-install.py ${TOPOLOGY_NAME} make-bridges | sh -s
    8.13 +		;;
    8.14 +		stop)
    8.15 +		;;
    8.16 +		*)
    8.17 +			echo $0 'start|stop'
    8.18 +		;;
    8.19 +esac
    8.20 +