[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] Fix a couple of problems in the TPM backend driver.



# HG changeset patch
# User kaf24@xxxxxxxxxxxxxxxxxxxx
# Node ID 8685b64538fa17f51d6d088935e833a4821f4f29
# Parent  0ee5869b4894b70ff08c2b16b19c0ef27def4558
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@xxxxxxxxxx>
---
 tools/examples/external-device-migrate |  124 +++++++++++++++++++--------------
 tools/examples/vtpm                    |    3 
 tools/examples/vtpm-common.sh          |  120 +++++++++++++++++--------------
 3 files changed, 142 insertions(+), 105 deletions(-)

diff -r 0ee5869b4894 -r 8685b64538fa tools/examples/external-device-migrate
--- a/tools/examples/external-device-migrate    Wed May 10 15:58:16 2006 +0100
+++ b/tools/examples/external-device-migrate    Wed May 10 16:01:34 2006 +0100
@@ -27,59 +27,85 @@ dir=$(dirname "$0")
 . "$dir/logging.sh"
 
 
-function usage() {
-       echo " Pass the following command line paremeters to the script:"
-       echo ""
-       echo "-step <n>     : n-th migration step"
-       echo "-host <host>  : the destination host"
-       echo "-domname <domain name> : name of the domain that is migrating"
-       echo "-type <device type>    : the type of device that is migrating"
-       echo "-recover               : indicates recovery request; an error"
-       echo "                         occurred during migration"
-       echo "-help                  : display this help screen"
+function ext_dev_migrate_usage() {
+cat <<EOF
+Pass the following command line parameters to the script:
+
+-step <n>              : n-th migration step
+-host <host>           : the destination host
+-domname <domain name> : name of the domain that is migrating
+-type <device type>    : the type of device that is migrating
+-subtype <dev. subtype>: the subtype of the device
+-recover               : indicates recovery request; an error
+                         occurred during migration
+-help                  : display this help screen
+EOF
 }
 
-while [ 1 ]; do
-       if [ "$1" == "-step" ]; then
+# Parse the command line paramters. The following parameters must be
+# passed as the first ones in the sequence:
+#  -step       [required]
+#  -host       [required]
+#  -domname    [required]
+#  -type       [required]
+#  -subtype    [optional]
+#  -recover    [optional]
+# The remaining ones will be passed to the called function.
+function evaluate_params()
+{
+       local step host domname typ recover filename func stype
+       stype=""
+       while [ 1 ]; do
+               if [ "$1" == "-step" ]; then
+                       shift
+                       step=$1
+               elif [ "$1" == "-host" ]; then
+                       shift
+                       host=$1
+               elif [ "$1" == "-domname" ]; then
+                       shift
+                       domname=$1
+               elif [ "$1" == "-type" ]; then
+                       shift
+                       typ=$1
+               elif [ "$1" == "-subtype" ]; then
+                       shift
+                       stype="_$1"
+               elif [ "$1" == "-recover" ]; then
+                       recover=1
+               elif [ "$1" == "-help" ]; then
+                       ext_dev_migrate_usage
+                       exit
+               else
+                       break
+               fi
                shift
-               step=$1
-       elif [ "$1" == "-host" ]; then
-               shift
-               host=$1
-       elif [ "$1" == "-domname" ]; then
-               shift
-               domname=$1
-       elif [ "$1" == "-type" ]; then
-               shift
-               typ=$1
-       elif [ "$1" == "-recover" ]; then
-               recover=1
-       elif [ "$1" == "-help" ]; then
-               usage
+       done
+
+       if [ "$step"    == "" -o \
+            "$host"    == "" -o \
+            "$typ"     == "" -o \
+            "$domname" == "" ]; then
+               echo "Error: Parameter(s) missing (-step/-host/-type/-domname)"
+               echo ""
+               echo "$0 -help for usage."
                exit
+       fi
+
+       filename="$dir/$typ$stype-migration.sh"
+       if [ ! -r $filename ]; then
+               echo "Error: Could not find script '$filename'"
+               return
+       fi
+       . "$filename"
+
+       if [ "$recover" == "1" ]; then
+               func="$typ"_recover
+               eval $func $host $domname $step $*
        else
-               break
+               func="$typ"_migration_step
+               eval $func $host $domname $step $*
        fi
-       shift
-done
+}
 
-if [ "$step"    == "" -o \
-     "$host"    == "" -o \
-     "$typ"     == "" -o \
-     "$domname" == "" ]; then
-       echo "Error: Parameter(s) missing (-step/-host/-type/-domname)"
-set
-       echo ""
-       echo "$0 --help for usage."
-       exit
-fi
-
-. "$dir/$typ-migration.sh"
-
-if [ "$recover" == "1" ]; then
-       func="$typ"_recover
-       eval $func $host $domname $step
-else
-       func="$typ"_migration_step
-       eval $func $host $domname $step
-fi
+evaluate_params $*
diff -r 0ee5869b4894 -r 8685b64538fa tools/examples/vtpm
--- a/tools/examples/vtpm       Wed May 10 15:58:16 2006 +0100
+++ b/tools/examples/vtpm       Wed May 10 16:01:34 2006 +0100
@@ -23,5 +23,6 @@ if [ $vtpm_fatal_error -eq 0 ]; then
 if [ $vtpm_fatal_error -eq 0 ]; then
        log debug "Successful vTPM operation '$command'."
        success
+else
+       fatal "Error while executing vTPM operation '$command'."
 fi
-
diff -r 0ee5869b4894 -r 8685b64538fa tools/examples/vtpm-common.sh
--- a/tools/examples/vtpm-common.sh     Wed May 10 15:58:16 2006 +0100
+++ b/tools/examples/vtpm-common.sh     Wed May 10 16:01:34 2006 +0100
@@ -64,8 +64,10 @@ fi
 #  Returns '0' if instance number could not be found, otherwise
 #  it returns the instance number in the variable 'instance'
 function vtpmdb_find_instance () {
-       local vmname=$1
-       local ret=0
+       local vmname ret instance
+       vmname=$1
+       ret=0
+
        instance=$(cat $VTPMDB |                   \
                  awk -vvmname=$vmname             \
                  '{                               \
@@ -86,8 +88,9 @@ function vtpmdb_find_instance () {
 # Check whether a particular instance number is still available
 # returns "0" if it is not available, "1" otherwise.
 function vtpmdb_is_free_instancenum () {
-       local instance=$1
-       local avail=1
+       local instance instances avail i
+       instance=$1
+       avail=1
        #Allowed instance number range: 1-255
        if [ $instance -eq 0 -o $instance -gt 255 ]; then
                avail=0
@@ -113,9 +116,7 @@ function vtpmdb_is_free_instancenum () {
 # Get an available instance number given the database
 # Returns an unused instance number
 function vtpmdb_get_free_instancenum () {
-       local ctr
-       local instances
-       local don
+       local ctr instances don found
        instances=$(cat $VTPMDB |                \
                   gawk                          \
                   '{                            \
@@ -126,7 +127,6 @@ function vtpmdb_get_free_instancenum () 
        ctr=1
        don=0
        while [ $don -eq 0 ]; do
-               local found
                found=0
                for i in $instances; do
                        if [ $i -eq $ctr ]; then
@@ -147,8 +147,9 @@ function vtpmdb_get_free_instancenum () 
 
 # Add a domain name and instance number to the DB file
 function vtpmdb_add_instance () {
-       local vmname=$1
-       local inst=$2
+       local res vmname inst
+       vmname=$1
+       inst=$2
 
        if [ ! -f $VTPMDB ]; then
                echo "#Database for VM to vTPM association" > $VTPMDB
@@ -165,9 +166,10 @@ function vtpmdb_add_instance () {
 #Validate whether an entry is the same as passed to this
 #function
 function vtpmdb_validate_entry () {
-       local rc=0
-       local vmname=$1
-       local inst=$2
+       local res rc vmname inst
+       rc=0
+       vmname=$1
+       inst=$2
 
        res=$(cat $VTPMDB |            \
             gawk -vvmname=$vmname     \
@@ -188,9 +190,9 @@ function vtpmdb_validate_entry () {
             }')
 
        if [ "$res" == "1" ]; then
-               let rc=1
+               rc=1
        elif [ "$res" == "2" ]; then
-               let rc=2
+               rc=2
        fi
        echo "$rc"
 }
@@ -199,9 +201,11 @@ function vtpmdb_validate_entry () {
 #Remove an entry from the vTPM database given its domain name
 #and instance number
 function vtpmdb_remove_entry () {
-       local vmname=$1
-       local instance=$2
-       local VTPMDB_TMP="$VTPMDB".tmp
+       local vmname instance VTPMDB_TMP
+       vmname=$1
+       instance=$2
+       VTPMDB_TMP="$VTPMDB".tmp
+
        $(cat $VTPMDB |            \
         gawk -vvmname=$vmname     \
         '{                        \
@@ -219,13 +223,14 @@ function vtpmdb_remove_entry () {
 
 
 # Find the reason for the creation of this device:
-# Set global REASON variable to 'resume' or 'create'
+# Returns 'resume' or 'create'
 function vtpm_get_create_reason () {
-       local resume=$(xenstore-read $XENBUS_PATH/resume)
+       local resume
+       resume=$(xenstore-read $XENBUS_PATH/resume)
        if [ "$resume" == "True" ]; then
-               REASON="resume"
-       else
-               REASON="create"
+               echo "resume"
+       else
+               echo "create"
        fi
 }
 
@@ -234,10 +239,9 @@ function vtpm_get_create_reason () {
 # If no entry in the TPM database is found, the instance is
 # created and an entry added to the database.
 function vtpm_create_instance () {
-       local domname=$(xenstore_read "$XENBUS_PATH"/domain)
-       local res
-       local instance
-       vtpm_get_create_reason
+       local res instance domname reason
+       domname=$(xenstore_read "$XENBUS_PATH"/domain)
+       reason=$(vtpm_get_create_reason)
 
        claim_lock vtpmdb
        instance=$(vtpmdb_find_instance $domname)
@@ -252,20 +256,20 @@ function vtpm_create_instance () {
                else
                        instance=$(vtpmdb_get_free_instancenum)
                fi
-               vtpmdb_add_instance $domname $instance
-               if [ "$REASON" == "create" ]; then
+               if [ "$reason" == "create" ]; then
                        vtpm_create $instance
-               elif [ "$REASON" == "resume" ]; then
+               else
                        vtpm_resume $instance $domname
-               else
-                       #default case for 'now'
-                       vtpm_create $instance
+               fi
+               if [ $vtpm_fatal_error -eq 0 ]; then
+                       vtpmdb_add_instance $domname $instance
                fi
        fi
 
        release_lock vtpmdb
 
-       if [ "$REASON" == "create" ]; then
+       if [ $vtpm_fatal_error -eq 0 -a \
+            "$reason" == "create" ]; then
                vtpm_reset $instance
        fi
        xenstore_write $XENBUS_PATH/instance $instance
@@ -276,15 +280,18 @@ function vtpm_create_instance () {
 #Since it is assumed that the VM will appear again, the
 #entry is kept in the VTPMDB file.
 function vtpm_remove_instance () {
-       local domname=$(xenstore_read "$XENBUS_PATH"/domain)
-
-       claim_lock vtpmdb
-
-       instance=$(vtpmdb_find_instance $domname)
-
-       if [ "$instance" != "0" ]; then
-               if [ "$REASON" == "suspend" ]; then
-                       vtpm_suspend $instance
+       local instance reason domname
+       domname=$(xenstore_read "$XENBUS_PATH"/domain)
+
+       if [ "$doname" != "" ]; then
+               claim_lock vtpmdb
+
+               instance=$(vtpmdb_find_instance $domname)
+
+               if [ "$instance" != "0" ]; then
+                       if [ "$reason" == "suspend" ]; then
+                               vtpm_suspend $instance
+                       fi
                fi
        fi
 
@@ -295,7 +302,7 @@ function vtpm_remove_instance () {
 #Remove an entry in the VTPMDB file given the domain's name
 #1st parameter: The name of the domain
 function vtpm_delete_instance () {
-       local rc
+       local instance
 
        claim_lock vtpmdb
 
@@ -313,20 +320,21 @@ function vtpm_delete_instance () {
 #  "0"  : this is not an address of this machine
 #  "1"  : this is an address local to this machine
 function isLocalAddress() {
-       local addr=$(ping $1 -c 1 |  \
-                    gawk '{ print substr($3,2,length($3)-2); exit }')
+       local addr res
+       addr=$(ping $1 -c 1 |  \
+              gawk '{ print substr($3,2,length($3)-2); exit }')
        if [ "$addr" == "" ]; then
                echo "-1"
                return
        fi
-       local res=$(ifconfig | grep "inet addr" |  \
-                  gawk -vaddr=$addr               \
-                  '{                              \
-                     if ( addr == substr($2, 6)) {\
-                       print "1";                 \
-                     }                            \
-                  }'                              \
-                 )
+       res=$(ifconfig | grep "inet addr" |  \
+            gawk -vaddr=$addr               \
+            '{                              \
+               if ( addr == substr($2, 6)) {\
+                 print "1";                 \
+               }                            \
+            }'                              \
+           )
        if [ "$res" == "" ]; then
                echo "0"
                return
@@ -341,7 +349,8 @@ function isLocalAddress() {
 # 2nd: name of the domain to migrate
 # 3rd: the migration step to perform
 function vtpm_migration_step() {
-       local instance=$(vtpmdb_find_instance $2)
+       local instance res
+       instance=$(vtpmdb_find_instance $2)
        if [ "$instance" == "" ]; then
                echo "Error: Translation of domain name ($2) to instance 
failed. Check /etc/xen/vtpm.db"
                log err "Error during translation of domain name"
@@ -360,6 +369,7 @@ function vtpm_migration_step() {
 # 2nd: name of the domain that was to be migrated
 # 3rd: the last successful migration step that was done
 function vtpm_recover() {
+       local res
        res=$(isLocalAddress $1)
        if [ "$res" == "0" ]; then
                vtpm_migrate_recover $1 $2 $3

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.