@@ -575,6 +575,10 @@ let open_database ~__context ~self =
575575 debug " %s" " Attaching VDI for metadata import" ;
576576 Static_vdis. permanent_vdi_attach ~__context ~vdi ~reason
577577 in
578+ let detach vdi =
579+ debug " %s" " Detaching VDI after metadata import" ;
580+ Static_vdis. permanent_vdi_detach ~__context ~vdi
581+ in
578582 (* Open the database contained in the VDI *)
579583 let db_ref_of_attached_vdi reason =
580584 (* Read db to temporary file *)
@@ -589,25 +593,22 @@ let open_database ~__context ~self =
589593 Db_ref. update_database db_ref (fun db -> Database. reindex db);
590594 db_ref
591595 in
592- let detach vdi =
593- debug " %s " " Detaching VDI after metadata import " ;
594- Static_vdis. permanent_vdi_detach ~__context ~vdi
596+ let reason = Printf. sprintf " %s %s "
597+ Xapi_globs. foreign_metadata_vdi_reason
598+ ( Db.VDI. get_uuid ~__context ~self )
595599 in
596600 try
597- let reason = Printf. sprintf " %s %s"
598- Xapi_globs. foreign_metadata_vdi_reason
599- (Db.VDI. get_uuid ~__context ~self )
600- in
601- debug " Attaching database VDI to master with reason [%s]" reason;
602601 attach self reason;
603- debug " %s" " Attempting to read database" ;
604602 let db_ref = db_ref_of_attached_vdi reason in
605- debug " %s" " Detaching metadata VDI" ;
606603 detach self;
607604 (* Create a new session to query the database, and associate it with the db ref *)
608605 debug " %s" " Creating readonly session" ;
609606 let read_only_session = Xapi_session. create_readonly_session ~__context in
610607 Db_backend. register_session_with_database read_only_session db_ref;
611608 read_only_session
612- with _ ->
613- raise (Api_errors. Server_error (Api_errors. could_not_import_database, [] ))
609+ with e ->
610+ (* Make sure to detach if either the attach or database read fail. *)
611+ detach self;
612+ let error = Printexc. to_string e in
613+ debug " Caught %s while trying to open database" error;
614+ raise (Api_errors. Server_error (Api_errors. could_not_import_database, [error]))
0 commit comments