ia64/xen-unstable

changeset 10033:8685b64538fa

Clean up and refactor some parts of the vTPM and migration-related scripts.
In the external device migration script I am introducing a subtype
(-subtype <subtype>) from which the script's name is formed. Also all
unparsed parameters are passed to the called script.

Signed-off-by: Stefan Berger <stefanb@us.ibm.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed May 10 16:01:34 2006 +0100 (2006-05-10)
parents 0ee5869b4894
children 25da74e2f8fb
files tools/examples/external-device-migrate tools/examples/vtpm tools/examples/vtpm-common.sh
line diff
     1.1 --- a/tools/examples/external-device-migrate	Wed May 10 15:58:16 2006 +0100
     1.2 +++ b/tools/examples/external-device-migrate	Wed May 10 16:01:34 2006 +0100
     1.3 @@ -27,59 +27,85 @@ dir=$(dirname "$0")
     1.4  . "$dir/logging.sh"
     1.5  
     1.6  
     1.7 -function usage() {
     1.8 -	echo " Pass the following command line paremeters to the script:"
     1.9 -	echo ""
    1.10 -	echo "-step <n>     : n-th migration step"
    1.11 -	echo "-host <host>  : the destination host"
    1.12 -	echo "-domname <domain name> : name of the domain that is migrating"
    1.13 -	echo "-type <device type>    : the type of device that is migrating"
    1.14 -	echo "-recover               : indicates recovery request; an error"
    1.15 -	echo "                         occurred during migration"
    1.16 -	echo "-help                  : display this help screen"
    1.17 +function ext_dev_migrate_usage() {
    1.18 +cat <<EOF
    1.19 +Pass the following command line parameters to the script:
    1.20 +
    1.21 +-step <n>              : n-th migration step
    1.22 +-host <host>           : the destination host
    1.23 +-domname <domain name> : name of the domain that is migrating
    1.24 +-type <device type>    : the type of device that is migrating
    1.25 +-subtype <dev. subtype>: the subtype of the device
    1.26 +-recover               : indicates recovery request; an error
    1.27 +                         occurred during migration
    1.28 +-help                  : display this help screen
    1.29 +EOF
    1.30  }
    1.31  
    1.32 -while [ 1 ]; do
    1.33 -	if [ "$1" == "-step" ]; then
    1.34 -		shift
    1.35 -		step=$1
    1.36 -	elif [ "$1" == "-host" ]; then
    1.37 -		shift
    1.38 -		host=$1
    1.39 -	elif [ "$1" == "-domname" ]; then
    1.40 +# Parse the command line paramters. The following parameters must be
    1.41 +# passed as the first ones in the sequence:
    1.42 +#  -step       [required]
    1.43 +#  -host       [required]
    1.44 +#  -domname    [required]
    1.45 +#  -type       [required]
    1.46 +#  -subtype    [optional]
    1.47 +#  -recover    [optional]
    1.48 +# The remaining ones will be passed to the called function.
    1.49 +function evaluate_params()
    1.50 +{
    1.51 +	local step host domname typ recover filename func stype
    1.52 +	stype=""
    1.53 +	while [ 1 ]; do
    1.54 +		if [ "$1" == "-step" ]; then
    1.55 +			shift
    1.56 +			step=$1
    1.57 +		elif [ "$1" == "-host" ]; then
    1.58 +			shift
    1.59 +			host=$1
    1.60 +		elif [ "$1" == "-domname" ]; then
    1.61 +			shift
    1.62 +			domname=$1
    1.63 +		elif [ "$1" == "-type" ]; then
    1.64 +			shift
    1.65 +			typ=$1
    1.66 +		elif [ "$1" == "-subtype" ]; then
    1.67 +			shift
    1.68 +			stype="_$1"
    1.69 +		elif [ "$1" == "-recover" ]; then
    1.70 +			recover=1
    1.71 +		elif [ "$1" == "-help" ]; then
    1.72 +			ext_dev_migrate_usage
    1.73 +			exit
    1.74 +		else
    1.75 +			break
    1.76 +		fi
    1.77  		shift
    1.78 -		domname=$1
    1.79 -	elif [ "$1" == "-type" ]; then
    1.80 -		shift
    1.81 -		typ=$1
    1.82 -	elif [ "$1" == "-recover" ]; then
    1.83 -		recover=1
    1.84 -	elif [ "$1" == "-help" ]; then
    1.85 -		usage
    1.86 +	done
    1.87 +
    1.88 +	if [ "$step"    == "" -o \
    1.89 +	     "$host"    == "" -o \
    1.90 +	     "$typ"     == "" -o \
    1.91 +	     "$domname" == "" ]; then
    1.92 +	 	echo "Error: Parameter(s) missing (-step/-host/-type/-domname)"
    1.93 +		echo ""
    1.94 +		echo "$0 -help for usage."
    1.95  		exit
    1.96 -	else
    1.97 -		break
    1.98  	fi
    1.99 -	shift
   1.100 -done
   1.101  
   1.102 -if [ "$step"    == "" -o \
   1.103 -     "$host"    == "" -o \
   1.104 -     "$typ"     == "" -o \
   1.105 -     "$domname" == "" ]; then
   1.106 -	echo "Error: Parameter(s) missing (-step/-host/-type/-domname)"
   1.107 -set
   1.108 -	echo ""
   1.109 -	echo "$0 --help for usage."
   1.110 -	exit
   1.111 -fi
   1.112 +	filename="$dir/$typ$stype-migration.sh"
   1.113 +	if [ ! -r $filename ]; then
   1.114 +		echo "Error: Could not find script '$filename'"
   1.115 +		return
   1.116 +	fi
   1.117 +	. "$filename"
   1.118  
   1.119 -. "$dir/$typ-migration.sh"
   1.120 +	if [ "$recover" == "1" ]; then
   1.121 +		func="$typ"_recover
   1.122 +		eval $func $host $domname $step $*
   1.123 +	else
   1.124 +		func="$typ"_migration_step
   1.125 +		eval $func $host $domname $step $*
   1.126 +	fi
   1.127 +}
   1.128  
   1.129 -if [ "$recover" == "1" ]; then
   1.130 -	func="$typ"_recover
   1.131 -	eval $func $host $domname $step
   1.132 -else
   1.133 -	func="$typ"_migration_step
   1.134 -	eval $func $host $domname $step
   1.135 -fi
   1.136 +evaluate_params $*
     2.1 --- a/tools/examples/vtpm	Wed May 10 15:58:16 2006 +0100
     2.2 +++ b/tools/examples/vtpm	Wed May 10 16:01:34 2006 +0100
     2.3 @@ -23,5 +23,6 @@ esac
     2.4  if [ $vtpm_fatal_error -eq 0 ]; then
     2.5  	log debug "Successful vTPM operation '$command'."
     2.6  	success
     2.7 +else
     2.8 +	fatal "Error while executing vTPM operation '$command'."
     2.9  fi
    2.10 -
     3.1 --- a/tools/examples/vtpm-common.sh	Wed May 10 15:58:16 2006 +0100
     3.2 +++ b/tools/examples/vtpm-common.sh	Wed May 10 16:01:34 2006 +0100
     3.3 @@ -64,8 +64,10 @@ fi
     3.4  #  Returns '0' if instance number could not be found, otherwise
     3.5  #  it returns the instance number in the variable 'instance'
     3.6  function vtpmdb_find_instance () {
     3.7 -	local vmname=$1
     3.8 -	local ret=0
     3.9 +	local vmname ret instance
    3.10 +	vmname=$1
    3.11 +	ret=0
    3.12 +
    3.13  	instance=$(cat $VTPMDB |                   \
    3.14  	          awk -vvmname=$vmname             \
    3.15  	          '{                               \
    3.16 @@ -86,8 +88,9 @@ function vtpmdb_find_instance () {
    3.17  # Check whether a particular instance number is still available
    3.18  # returns "0" if it is not available, "1" otherwise.
    3.19  function vtpmdb_is_free_instancenum () {
    3.20 -	local instance=$1
    3.21 -	local avail=1
    3.22 +	local instance instances avail i
    3.23 +	instance=$1
    3.24 +	avail=1
    3.25  	#Allowed instance number range: 1-255
    3.26  	if [ $instance -eq 0 -o $instance -gt 255 ]; then
    3.27  		avail=0
    3.28 @@ -113,9 +116,7 @@ function vtpmdb_is_free_instancenum () {
    3.29  # Get an available instance number given the database
    3.30  # Returns an unused instance number
    3.31  function vtpmdb_get_free_instancenum () {
    3.32 -	local ctr
    3.33 -	local instances
    3.34 -	local don
    3.35 +	local ctr instances don found
    3.36  	instances=$(cat $VTPMDB |                \
    3.37  	           gawk                          \
    3.38  	           '{                            \
    3.39 @@ -126,7 +127,6 @@ function vtpmdb_get_free_instancenum () 
    3.40  	ctr=1
    3.41  	don=0
    3.42  	while [ $don -eq 0 ]; do
    3.43 -		local found
    3.44  		found=0
    3.45  		for i in $instances; do
    3.46  			if [ $i -eq $ctr ]; then
    3.47 @@ -147,8 +147,9 @@ function vtpmdb_get_free_instancenum () 
    3.48  
    3.49  # Add a domain name and instance number to the DB file
    3.50  function vtpmdb_add_instance () {
    3.51 -	local vmname=$1
    3.52 -	local inst=$2
    3.53 +	local res vmname inst
    3.54 +	vmname=$1
    3.55 +	inst=$2
    3.56  
    3.57  	if [ ! -f $VTPMDB ]; then
    3.58  		echo "#Database for VM to vTPM association" > $VTPMDB
    3.59 @@ -165,9 +166,10 @@ function vtpmdb_add_instance () {
    3.60  #Validate whether an entry is the same as passed to this
    3.61  #function
    3.62  function vtpmdb_validate_entry () {
    3.63 -	local rc=0
    3.64 -	local vmname=$1
    3.65 -	local inst=$2
    3.66 +	local res rc vmname inst
    3.67 +	rc=0
    3.68 +	vmname=$1
    3.69 +	inst=$2
    3.70  
    3.71  	res=$(cat $VTPMDB |            \
    3.72  	     gawk -vvmname=$vmname     \
    3.73 @@ -188,9 +190,9 @@ function vtpmdb_validate_entry () {
    3.74  	     }')
    3.75  
    3.76  	if [ "$res" == "1" ]; then
    3.77 -		let rc=1
    3.78 +		rc=1
    3.79  	elif [ "$res" == "2" ]; then
    3.80 -		let rc=2
    3.81 +		rc=2
    3.82  	fi
    3.83  	echo "$rc"
    3.84  }
    3.85 @@ -199,9 +201,11 @@ function vtpmdb_validate_entry () {
    3.86  #Remove an entry from the vTPM database given its domain name
    3.87  #and instance number
    3.88  function vtpmdb_remove_entry () {
    3.89 -	local vmname=$1
    3.90 -	local instance=$2
    3.91 -	local VTPMDB_TMP="$VTPMDB".tmp
    3.92 +	local vmname instance VTPMDB_TMP
    3.93 +	vmname=$1
    3.94 +	instance=$2
    3.95 +	VTPMDB_TMP="$VTPMDB".tmp
    3.96 +
    3.97  	$(cat $VTPMDB |            \
    3.98  	 gawk -vvmname=$vmname     \
    3.99  	 '{                        \
   3.100 @@ -219,13 +223,14 @@ function vtpmdb_remove_entry () {
   3.101  
   3.102  
   3.103  # Find the reason for the creation of this device:
   3.104 -# Set global REASON variable to 'resume' or 'create'
   3.105 +# Returns 'resume' or 'create'
   3.106  function vtpm_get_create_reason () {
   3.107 -	local resume=$(xenstore-read $XENBUS_PATH/resume)
   3.108 +	local resume
   3.109 +	resume=$(xenstore-read $XENBUS_PATH/resume)
   3.110  	if [ "$resume" == "True" ]; then
   3.111 -		REASON="resume"
   3.112 +		echo "resume"
   3.113  	else
   3.114 -		REASON="create"
   3.115 +		echo "create"
   3.116  	fi
   3.117  }
   3.118  
   3.119 @@ -234,10 +239,9 @@ function vtpm_get_create_reason () {
   3.120  # If no entry in the TPM database is found, the instance is
   3.121  # created and an entry added to the database.
   3.122  function vtpm_create_instance () {
   3.123 -	local domname=$(xenstore_read "$XENBUS_PATH"/domain)
   3.124 -	local res
   3.125 -	local instance
   3.126 -	vtpm_get_create_reason
   3.127 +	local res instance domname reason
   3.128 +	domname=$(xenstore_read "$XENBUS_PATH"/domain)
   3.129 +	reason=$(vtpm_get_create_reason)
   3.130  
   3.131  	claim_lock vtpmdb
   3.132  	instance=$(vtpmdb_find_instance $domname)
   3.133 @@ -252,20 +256,20 @@ function vtpm_create_instance () {
   3.134  		else
   3.135  			instance=$(vtpmdb_get_free_instancenum)
   3.136  		fi
   3.137 -		vtpmdb_add_instance $domname $instance
   3.138 -		if [ "$REASON" == "create" ]; then
   3.139 +		if [ "$reason" == "create" ]; then
   3.140  			vtpm_create $instance
   3.141 -		elif [ "$REASON" == "resume" ]; then
   3.142 +		else
   3.143  			vtpm_resume $instance $domname
   3.144 -		else
   3.145 -			#default case for 'now'
   3.146 -			vtpm_create $instance
   3.147 +		fi
   3.148 +		if [ $vtpm_fatal_error -eq 0 ]; then
   3.149 +			vtpmdb_add_instance $domname $instance
   3.150  		fi
   3.151  	fi
   3.152  
   3.153  	release_lock vtpmdb
   3.154  
   3.155 -	if [ "$REASON" == "create" ]; then
   3.156 +	if [ $vtpm_fatal_error -eq 0 -a \
   3.157 +	     "$reason" == "create" ]; then
   3.158  		vtpm_reset $instance
   3.159  	fi
   3.160  	xenstore_write $XENBUS_PATH/instance $instance
   3.161 @@ -276,15 +280,18 @@ function vtpm_create_instance () {
   3.162  #Since it is assumed that the VM will appear again, the
   3.163  #entry is kept in the VTPMDB file.
   3.164  function vtpm_remove_instance () {
   3.165 -	local domname=$(xenstore_read "$XENBUS_PATH"/domain)
   3.166 -
   3.167 -	claim_lock vtpmdb
   3.168 +	local instance reason domname
   3.169 +	domname=$(xenstore_read "$XENBUS_PATH"/domain)
   3.170  
   3.171 -	instance=$(vtpmdb_find_instance $domname)
   3.172 +	if [ "$doname" != "" ]; then
   3.173 +		claim_lock vtpmdb
   3.174  
   3.175 -	if [ "$instance" != "0" ]; then
   3.176 -		if [ "$REASON" == "suspend" ]; then
   3.177 -			vtpm_suspend $instance
   3.178 +		instance=$(vtpmdb_find_instance $domname)
   3.179 +
   3.180 +		if [ "$instance" != "0" ]; then
   3.181 +			if [ "$reason" == "suspend" ]; then
   3.182 +				vtpm_suspend $instance
   3.183 +			fi
   3.184  		fi
   3.185  	fi
   3.186  
   3.187 @@ -295,7 +302,7 @@ function vtpm_remove_instance () {
   3.188  #Remove an entry in the VTPMDB file given the domain's name
   3.189  #1st parameter: The name of the domain
   3.190  function vtpm_delete_instance () {
   3.191 -	local rc
   3.192 +	local instance
   3.193  
   3.194  	claim_lock vtpmdb
   3.195  
   3.196 @@ -313,20 +320,21 @@ function vtpm_delete_instance () {
   3.197  #  "0"  : this is not an address of this machine
   3.198  #  "1"  : this is an address local to this machine
   3.199  function isLocalAddress() {
   3.200 -	local addr=$(ping $1 -c 1 |  \
   3.201 -	             gawk '{ print substr($3,2,length($3)-2); exit }')
   3.202 +	local addr res
   3.203 +	addr=$(ping $1 -c 1 |  \
   3.204 +	       gawk '{ print substr($3,2,length($3)-2); exit }')
   3.205  	if [ "$addr" == "" ]; then
   3.206  		echo "-1"
   3.207  		return
   3.208  	fi
   3.209 -	local res=$(ifconfig | grep "inet addr" |  \
   3.210 -	           gawk -vaddr=$addr               \
   3.211 -	           '{                              \
   3.212 -	              if ( addr == substr($2, 6)) {\
   3.213 -	                print "1";                 \
   3.214 -	              }                            \
   3.215 -	           }'                              \
   3.216 -	          )
   3.217 +	res=$(ifconfig | grep "inet addr" |  \
   3.218 +	     gawk -vaddr=$addr               \
   3.219 +	     '{                              \
   3.220 +	        if ( addr == substr($2, 6)) {\
   3.221 +	          print "1";                 \
   3.222 +	        }                            \
   3.223 +	     }'                              \
   3.224 +	    )
   3.225  	if [ "$res" == "" ]; then
   3.226  		echo "0"
   3.227  		return
   3.228 @@ -341,7 +349,8 @@ function isLocalAddress() {
   3.229  # 2nd: name of the domain to migrate
   3.230  # 3rd: the migration step to perform
   3.231  function vtpm_migration_step() {
   3.232 -	local instance=$(vtpmdb_find_instance $2)
   3.233 +	local instance res
   3.234 +	instance=$(vtpmdb_find_instance $2)
   3.235  	if [ "$instance" == "" ]; then
   3.236  		echo "Error: Translation of domain name ($2) to instance failed. Check /etc/xen/vtpm.db"
   3.237  		log err "Error during translation of domain name"
   3.238 @@ -360,6 +369,7 @@ function vtpm_migration_step() {
   3.239  # 2nd: name of the domain that was to be migrated
   3.240  # 3rd: the last successful migration step that was done
   3.241  function vtpm_recover() {
   3.242 +	local res
   3.243  	res=$(isLocalAddress $1)
   3.244  	if [ "$res" == "0" ]; then
   3.245  		vtpm_migrate_recover $1 $2 $3