@@ -28,6 +28,7 @@ type error =
2828 | API_not_enabled
2929 | Other of string
3030 | VM_CDR_not_found
31+ | VM_CDR_eject
3132 | VM_misses_feature
3233 | VM_not_running
3334 | VM_sysprep_timeout
@@ -205,9 +206,18 @@ let find_vdi ~__context ~label =
205206 warn " %s: more than one VDI with label %s" __FUNCTION__ label ;
206207 vdi
207208
209+ (* Ejecting the CD/VDI/ISO may fail with a timeout *)
210+ let eject ~rpc ~session_id ~vbd ~iso =
211+ try
212+ Client.VBD. eject ~rpc ~session_id ~vbd ;
213+ Sys. remove iso
214+ with exn ->
215+ warn " %s: ejecting CD failed: %s" __FUNCTION__ (Printexc. to_string exn ) ;
216+ fail VM_CDR_eject
217+
208218(* * notify the VM with [domid] to run sysprep and where to find the
209219 file. *)
210- let trigger ~domid ~uuid ~timeout =
220+ let trigger ~rpc ~ session_id ~ domid ~uuid ~timeout ~ vbd ~ iso =
211221 let open Ezxenstore_core.Xenstore in
212222 let control = Printf. sprintf " /local/domain/%Ld/control/sysprep" domid in
213223 let domain = Printf. sprintf " /local/domain/%Ld" domid in
@@ -217,17 +227,21 @@ let trigger ~domid ~uuid ~timeout =
217227 xs.Xs. write (control // " action" ) " sysprep" ;
218228 debug " %s: notified domain %Ld" __FUNCTION__ domid ;
219229 try
220- (* wait for sysprep to start, then domain to dissapear *)
221- Ezxenstore_core.Watch. (
222- wait_for ~xs ~timeout: 5.0
223- (value_to_become (control // " action" ) " running" )
224- ) ;
225- debug " %s: sysprep is runnung; waiting for sysprep to finish"
226- __FUNCTION__ ;
227- Ezxenstore_core.Watch. (
228- wait_for ~xs ~timeout (key_to_disappear (control // " action" ))
229- ) ;
230- debug " %s sysprep is finished" __FUNCTION__ ;
230+ finally
231+ (fun () ->
232+ (* wait for sysprep to start, then domain to dissapear *)
233+ Ezxenstore_core.Watch. (
234+ wait_for ~xs ~timeout: 5.0
235+ (value_to_become (control // " action" ) " running" )
236+ ) ;
237+ debug " %s: sysprep is running; waiting for sysprep to finish"
238+ __FUNCTION__ ;
239+ Ezxenstore_core.Watch. (
240+ wait_for ~xs ~timeout (key_to_disappear (control // " action" ))
241+ )
242+ )
243+ (fun () -> eject ~rpc ~session_id ~vbd ~iso ) ;
244+ debug " %s waiting for domain to dissapear" __FUNCTION__ ;
231245 Ezxenstore_core.Watch. (wait_for ~xs ~timeout (key_to_disappear domain)) ;
232246 true
233247 with Ezxenstore_core.Watch. Timeout _ ->
@@ -269,13 +283,8 @@ let sysprep ~__context ~vm ~unattend ~timeout =
269283 call ~__context @@ fun rpc session_id ->
270284 Client.VBD. insert ~rpc ~session_id ~vdi ~vbd ;
271285 Thread. delay ! Xapi_globs. vm_sysprep_wait ;
272- match trigger ~domid ~uuid ~timeout with
286+ match trigger ~rpc ~session_id ~ domid ~uuid ~timeout ~vbd ~iso with
273287 | true ->
274- debug " %s: sysprep running, ejecting CD" __FUNCTION__ ;
275- Client.VBD. eject ~rpc ~session_id ~vbd ;
276- Sys. remove iso
288+ ()
277289 | false ->
278- debug " %s: sysprep timeout, ejecting CD" __FUNCTION__ ;
279- Client.VBD. eject ~rpc ~session_id ~vbd ;
280- Sys. remove iso ;
281290 fail VM_sysprep_timeout
0 commit comments