ia64/xen-unstable

view tools/examples/vtpm-common.sh @ 9785:5b1e10215e38

Under FC5 multi-line commands in bash script language have to be wrapped
in $(...) instead of '`...`', otherwise the parser seems to get
confused.

Signed-off-by: Stefan Berger <stefanb@us.ibm.com>
author emellor@leeni.uk.xensource.com
date Wed Apr 19 22:54:49 2006 +0100 (2006-04-19)
parents 568b8d8fc782
children 6af7b456e684
line source
1 #
2 # Copyright (c) 2005 IBM Corporation
3 # Copyright (c) 2005 XenSource Ltd.
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of version 2.1 of the GNU Lesser General Public
7 # License as published by the Free Software Foundation.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #
19 dir=$(dirname "$0")
20 . "$dir/logging.sh"
21 . "$dir/locking.sh"
23 VTPMDB="/etc/xen/vtpm.db"
25 #In the vtpm-impl file some commands should be defined:
26 # vtpm_create, vtpm_setup, vtpm_reset, etc. (see below)
27 #This should be indicated by setting VTPM_IMPL_DEFINED.
28 if [ -r "$dir/vtpm-impl" ]; then
29 . "$dir/vtpm-impl"
30 fi
32 if [ -z "$VTPM_IMPL_DEFINED" ]; then
33 function vtpm_create () {
34 true
35 }
36 function vtpm_setup() {
37 true
38 }
39 function vtpm_reset() {
40 true
41 }
42 function vtpm_suspend() {
43 true
44 }
45 function vtpm_resume() {
46 true
47 }
48 function vtpm_delete() {
49 true
50 }
51 fi
54 #Find the instance number for the vtpm given the name of the domain
55 # Parameters
56 # - vmname : the name of the vm
57 # Return value
58 # Returns '0' if instance number could not be found, otherwise
59 # it returns the instance number in the variable 'instance'
60 function vtpmdb_find_instance () {
61 local vmname=$1
62 local ret=0
63 instance=$(cat $VTPMDB | \
64 awk -vvmname=$vmname \
65 '{ \
66 if ( 1 != index($1,"#")) { \
67 if ( $1 == vmname ) { \
68 print $2; \
69 exit; \
70 } \
71 } \
72 }')
73 if [ "$instance" != "" ]; then
74 ret=$instance
75 fi
76 echo "$ret"
77 }
80 # Check whether a particular instance number is still available
81 # returns "0" if it is not available, "1" otherwise.
82 function vtpmdb_is_free_instancenum () {
83 local instance=$1
84 local avail=1
85 #Allowed instance number range: 1-255
86 if [ $instance -eq 0 -o $instance -gt 255 ]; then
87 avail=0
88 else
89 instances=$(cat $VTPMDB | \
90 gawk \
91 '{ \
92 if (1 != index($1,"#")) { \
93 printf("%s ",$2); \
94 } \
95 }')
96 for i in $instances; do
97 if [ $i -eq $instance ]; then
98 avail=0
99 break
100 fi
101 done
102 fi
103 echo "$avail"
104 }
107 # Get an available instance number given the database
108 # Returns an unused instance number
109 function vtpmdb_get_free_instancenum () {
110 local ctr
111 local instances
112 local don
113 instances=$(cat $VTPMDB | \
114 gawk \
115 '{ \
116 if (1 != index($1,"#")) { \
117 printf("%s ",$2); \
118 } \
119 }')
120 ctr=1
121 don=0
122 while [ $don -eq 0 ]; do
123 local found
124 found=0
125 for i in $instances; do
126 if [ $i -eq $ctr ]; then
127 found=1;
128 break;
129 fi
130 done
132 if [ $found -eq 0 ]; then
133 don=1
134 break
135 fi
136 let ctr=ctr+1
137 done
138 echo "$ctr"
139 }
142 # Add a domain name and instance number to the DB file
143 function vtpmdb_add_instance () {
144 local vmname=$1
145 local inst=$2
147 if [ ! -f $VTPMDB ]; then
148 echo "#Database for VM to vTPM association" > $VTPMDB
149 echo "#1st column: domain name" >> $VTPMDB
150 echo "#2nd column: TPM instance number" >> $VTPMDB
151 fi
152 res=$(vtpmdb_validate_entry $vmname $inst)
153 if [ $res -eq 0 ]; then
154 echo "$vmname $inst" >> $VTPMDB
155 fi
156 }
159 #Validate whether an entry is the same as passed to this
160 #function
161 function vtpmdb_validate_entry () {
162 local rc=0
163 local vmname=$1
164 local inst=$2
166 res=$(cat $VTPMDB | \
167 gawk -vvmname=$vmname \
168 -vinst=$inst \
169 '{ \
170 if ( 1 == index($1,"#")) {\
171 } else \
172 if ( $1 == vmname && \
173 $2 == inst) { \
174 printf("1"); \
175 exit; \
176 } else \
177 if ( $1 == vmname || \
178 $2 == inst) { \
179 printf("2"); \
180 exit; \
181 } \
182 }')
184 if [ "$res" == "1" ]; then
185 let rc=1
186 elif [ "$res" == "2" ]; then
187 let rc=2
188 fi
189 echo "$rc"
190 }
193 #Remove an entry from the vTPM database given its domain name
194 #and instance number
195 function vtpmdb_remove_entry () {
196 local vmname=$1
197 local instance=$2
198 local VTPMDB_TMP="$VTPMDB".tmp
199 $(cat $VTPMDB | \
200 gawk -vvmname=$vmname \
201 '{ \
202 if ( $1 != vmname ) { \
203 print $0; \
204 } \
205 '} > $VTPMDB_TMP)
206 if [ -e $VTPMDB_TMP ]; then
207 mv -f $VTPMDB_TMP $VTPMDB
208 vtpm_delete $instance
209 else
210 log err "Error creating temporary file '$VTPMDB_TMP'."
211 fi
212 }
215 # Find the reason for the creation of this device:
216 # Set global REASON variable to 'resume' or 'create'
217 function vtpm_get_create_reason () {
218 local resume=$(xenstore-read $XENBUS_PATH/resume)
219 if [ "$resume" == "True" ]; then
220 REASON="resume"
221 else
222 REASON="create"
223 fi
224 }
227 #Create a vTPM instance
228 # If no entry in the TPM database is found, the instance is
229 # created and an entry added to the database.
230 function vtpm_create_instance () {
231 local domname=$(xenstore_read "$XENBUS_PATH"/domain)
232 local res
233 local instance
234 vtpm_get_create_reason
236 claim_lock vtpmdb
237 instance=$(vtpmdb_find_instance $domname)
238 if [ "$instance" == "0" ]; then
239 #Try to give the preferred instance to the domain
240 instance=$(xenstore_read "$XENBUS_PATH"/pref_instance)
241 if [ "$instance" != "" ]; then
242 res=$(vtpmdb_is_free_instancenum $instance)
243 if [ $res -eq 0 ]; then
244 instance=$(vtpmdb_get_free_instancenum)
245 fi
246 else
247 instance=$(vtpmdb_get_free_instancenum)
248 fi
249 vtpmdb_add_instance $domname $instance
250 if [ "$REASON" == "create" ]; then
251 vtpm_create $instance
252 elif [ "$REASON" == "resume" ]; then
253 vtpm_resume $instance $domname
254 else
255 #default case for 'now'
256 vtpm_create $instance
257 fi
258 fi
260 release_lock vtpmdb
262 if [ "$REASON" == "create" ]; then
263 vtpm_reset $instance
264 fi
265 xenstore_write $XENBUS_PATH/instance $instance
266 }
269 #Remove an instance when a VM is terminating or suspending.
270 #Since it is assumed that the VM will appear again, the
271 #entry is kept in the VTPMDB file.
272 function vtpm_remove_instance () {
273 local domname=$(xenstore_read "$XENBUS_PATH"/domain)
275 claim_lock vtpmdb
277 instance=$(vtpmdb_find_instance $domname)
279 if [ "$instance" != "0" ]; then
280 if [ "$REASON" == "suspend" ]; then
281 vtpm_suspend $instance
282 fi
283 fi
285 release_lock vtpmdb
286 }
289 #Remove an entry in the VTPMDB file given the domain's name
290 #1st parameter: The name of the domain
291 function vtpm_delete_instance () {
292 local rc
294 claim_lock vtpmdb
296 instance=$(vtpmdb_find_instance $1)
297 if [ "$instance" != "0" ]; then
298 vtpmdb_remove_entry $1 $instance
299 fi
301 release_lock vtpmdb
302 }