ia64/xen-unstable

view tools/examples/vtpm-common.sh @ 8964:8946b6dcd49e

Fix x86_64 Xen build.

event_callback_cs and failsafe_callback_cs are x86_32 only.

Signed-off-by: Ian Campbell <Ian.Campbell@XenSource.com>
author Ian.Campbell@xensource.com
date Wed Feb 22 17:26:39 2006 +0000 (2006-02-22)
parents 2401f414d1ad
children 136aea67b369
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/xen-hotplug-common.sh"
22 findCommand "$@"
23 if [ "$command" != "online" ] &&
24 [ "$command" != "offline" ] &&
25 [ "$command" != "add" ] &&
26 [ "$command" != "remove" ]
27 then
28 log err "Invalid command: $command"
29 exit 1
30 fi
33 XENBUS_PATH="${XENBUS_PATH:?}"
36 VTPMDB="/etc/xen/vtpm.db"
38 #In the vtpm-impl file some commands should be defined:
39 # vtpm_create, vtpm_setup, vtpm_reset, etc. (see below)
40 #This should be indicated by setting VTPM_IMPL_DEFINED.
41 if [ -r "$dir/vtpm-impl" ]; then
42 . "$dir/vtpm-impl"
43 fi
45 if [ -z "$VTPM_IMPL_DEFINED" ]; then
46 function vtpm_create () {
47 true
48 }
49 function vtpm_setup() {
50 true
51 }
52 function vtpm_reset() {
53 true
54 }
55 function vtpm_suspend() {
56 true
57 }
58 function vtpm_resume() {
59 true
60 }
61 fi
63 #Find the instance number for the vtpm given the name of the domain
64 # Parameters
65 # - vmname : the name of the vm
66 # Return value
67 # Returns '0' if instance number could not be found, otherwise
68 # it returns the instance number in the variable 'instance'
69 function find_instance () {
70 local vmname=$1
71 local ret=0
72 instance=`cat $VTPMDB | \
73 awk -vvmname=$vmname \
74 '{ \
75 if ( 1 != index($1,"#")) { \
76 if ( $1 == vmname ) { \
77 print $2; \
78 exit; \
79 } \
80 } \
81 }'`
82 if [ "$instance" != "" ]; then
83 ret=1
84 fi
85 return $ret
86 }
89 # Check whether a particular instance number is still available
90 # returns '1' if it is available
91 function is_free_instancenum () {
92 local instance=$1
93 local avail=1
95 #Allowed instance number range: 1-255
96 if [ $instance -eq 0 -o $instance -gt 255 ]; then
97 avail=0
98 else
99 instances=`cat $VTPMDB | \
100 gawk \
101 '{ \
102 if (1 != index($1,"#")) { \
103 printf("%s ",$2); \
104 } \
105 }'`
106 for i in $instances; do
107 if [ $i -eq $instance ]; then
108 avail=0
109 break
110 fi
111 done
112 fi
113 return $avail
114 }
117 # Get an available instance number given the database
118 # Returns an unused instance number
119 function get_free_instancenum () {
120 local ctr
121 local instances
122 local don
123 instances=`cat $VTPMDB | \
124 gawk \
125 '{ \
126 if (1 != index($1,"#")) { \
127 printf("%s ",$2); \
128 } \
129 }'`
130 ctr=1
131 don=0
132 while [ $don -eq 0 ]; do
133 local found
134 found=0
135 for i in $instances; do
136 if [ $i -eq $ctr ]; then
137 found=1;
138 break;
139 fi
140 done
142 if [ $found -eq 0 ]; then
143 don=1
144 break
145 fi
146 let ctr=ctr+1
147 done
148 let instance=$ctr
149 }
152 # Add a domain name and instance number to the DB file
153 function add_instance () {
154 local vmname=$1
155 local inst=$2
157 if [ ! -f $VTPMDB ]; then
158 echo "#Database for VM to vTPM association" > $VTPMDB
159 echo "#1st column: domain name" >> $VTPMDB
160 echo "#2nd column: TPM instance number" >> $VTPMDB
161 fi
162 validate_entry $vmname $inst
163 if [ $? -eq 0 ]; then
164 echo "$vmname $inst" >> $VTPMDB
165 fi
166 }
169 #Validate whether an entry is the same as passed to this
170 #function
171 function validate_entry () {
172 local rc=0
173 local vmname=$1
174 local inst=$2
175 local res
177 res=`cat $VTPMDB | \
178 gawk -vvmname=$vmname \
179 -vinst=$inst \
180 '{ \
181 if ( 1 == index($1,"#")) {\
182 } else \
183 if ( $1 == vmname && \
184 $2 == inst) { \
185 printf("1"); \
186 exit; \
187 } else \
188 if ( $1 == vmname || \
189 $2 == inst) { \
190 printf("2"); \
191 exit; \
192 } \
193 }'`
195 if [ "$res" == "1" ]; then
196 let rc=1
197 elif [ "$res" == "2" ]; then
198 let rc=2
199 fi
200 return $rc
201 }
204 #Remove an entry from the vTPM database given its domain name
205 function remove_entry () {
206 local vmname=$1
207 local VTPMDB_TMP="$VTPMDB".tmp
208 `cat $VTPMDB | \
209 gawk -vvmname=$vmname \
210 '{ \
211 if ( $1 != vmname ) { \
212 print $0; \
213 } \
214 '} > $VTPMDB_TMP`
215 if [ -e $VTPMDB_TMP ]; then
216 mv -f $VTPMDB_TMP $VTPMDB
217 else
218 log err "Error creating temporary file '$VTPMDB_TMP'."
219 fi
220 }
223 # Find the reason for the creation of this device:
224 # Set global REASON variable to 'resume' or 'create'
225 function get_create_reason () {
226 local resume=$(xenstore-read $XENBUS_PATH/resume)
227 if [ "$resume" == "True" ]; then
228 REASON="resume"
229 else
230 REASON="create"
231 fi
232 }
234 #Create a vTPM instance
235 # If no entry in the TPM database is found, the instance is
236 # created and an entry added to the database.
237 function vtpm_create_instance () {
238 local domname=$(xenstore_read "$XENBUS_PATH"/domain)
239 local res
240 set +e
241 get_create_reason
243 claim_lock vtpmdb
245 find_instance $domname
246 res=$?
247 if [ $res -eq 0 ]; then
248 #Try to give the preferred instance to the domain
249 instance=$(xenstore_read "$XENBUS_PATH"/pref_instance)
250 if [ "$instance" != "" ]; then
251 is_free_instancenum $instance
252 res=$?
253 if [ $res -eq 0 ]; then
254 get_free_instancenum
255 fi
256 else
257 get_free_instancenum
258 fi
259 add_instance $domname $instance
260 if [ "$REASON" == "create" ]; then
261 vtpm_create $instance
262 elif [ "$REASON" == "resume" ]; then
263 vtpm_resume $instance $domname
264 else
265 #default case for 'now'
266 vtpm_create $instance
267 fi
268 fi
270 release_lock vtpmdb
272 if [ "$REASON" == "create" ]; then
273 vtpm_reset $instance
274 elif [ "$REASON" == "resume" ]; then
275 vtpm_setup $instance
276 else
277 #default case for 'now'
278 #vtpm_reset $instance
279 true
280 fi
281 xenstore_write $XENBUS_PATH/instance $instance
282 set -e
283 }
286 #Remove an instance
287 function vtpm_remove_instance () {
288 local domname=$(xenstore_read "$XENBUS_PATH"/domain)
289 set +e
290 find_instance $domname
291 res=$?
292 if [ $res -eq 0 ]; then
293 #Something is really wrong with the DB
294 log err "vTPM DB file $VTPMDB has no entry for '$domname'"
295 else
296 if [ "$REASON" == "suspend" ]; then
297 vtpm_suspend $instance
298 fi
299 fi
300 set -e
301 }