From 5803caa9c8e8be9e45a8a514272c0928fc6d8ab7 Mon Sep 17 00:00:00 2001 From: Simon Fuhrer Date: Mon, 19 Apr 2021 11:23:13 +0200 Subject: [PATCH] detach cdrom if host installed --- .../concerns/foreman_xen/host_extensions.rb | 5 ++++- app/models/foreman_xen/xenserver.rb | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/models/concerns/foreman_xen/host_extensions.rb b/app/models/concerns/foreman_xen/host_extensions.rb index 49f768e..692ea6a 100644 --- a/app/models/concerns/foreman_xen/host_extensions.rb +++ b/app/models/concerns/foreman_xen/host_extensions.rb @@ -3,7 +3,10 @@ module HostExtensions extend ActiveSupport::Concern def built(installed = true) - compute_resource.cleanup_configdrive(uuid) if compute_resource && compute_resource.type == 'ForemanXen::Xenserver' + if compute_resource && compute_resource.type == 'ForemanXen::Xenserver' + compute_resource.detach_cdrom(uuid) + compute_resource.cleanup_configdrive(uuid) + end super(installed) end diff --git a/app/models/foreman_xen/xenserver.rb b/app/models/foreman_xen/xenserver.rb index f30a720..183c911 100644 --- a/app/models/foreman_xen/xenserver.rb +++ b/app/models/foreman_xen/xenserver.rb @@ -39,6 +39,15 @@ def iso_library_mountpoint=(path) attrs[:iso_library_mountpoint] = mountpoint end + def detach_cdrom(uuid) + begin + vm = find_vm_by_uuid(uuid) + detach_cdrom_vbd(vm) if vm + rescue => e + logger.error "unable to detach cdrom:#{e}" + end + end + def cleanup_configdrive(uuid) iso_file_name = "foreman-configdrive-#{uuid}.iso" begin @@ -497,6 +506,13 @@ def attach_iso(vm, iso_vdi) true end + def detach_cdrom_vbd(vm) + cd_drive = client.vbds.find { |v| v.vm == vm && v.type == 'CD' } + unless cd_drive&.empty + client.eject_vbd cd_drive.reference + end + end + def find_free_userdevice(vm) # Find next free userdevice id for vbd # vm.vbds is not current, vm.reload not working.