diff --git a/src/vmm_mad/remotes/lib/lxd/container.rb b/src/vmm_mad/remotes/lib/lxd/container.rb index 63a812e244..ceb24f9566 100644 --- a/src/vmm_mad/remotes/lib/lxd/container.rb +++ b/src/vmm_mad/remotes/lib/lxd/container.rb @@ -152,7 +152,7 @@ class Container # Runs command inside container # @param command [String] to execute through lxc exec def exec(command) - Command.lxd_execute(name, command) + Command.lxc_execute(name, command) end #--------------------------------------------------------------------------- @@ -186,7 +186,7 @@ class Container nic_xml = @one.get_nic_by_mac(mac) - return unless nic_xml + raise 'Missing NIC xml' unless nic_xml nic_config = @one.nic(nic_xml) @@ -211,7 +211,8 @@ class Container return unless @one @one.get_disks.each do |disk| - setup_disk(disk, operation) + status = setup_disk(disk, operation) + return nil unless status end return unless @one.has_context? @@ -224,7 +225,12 @@ class Container context_path = "#{@one.lxdrc[:containers]}/#{name}/rootfs/context" create_context_dir = "#{Mapper::COMMANDS[:su_mkdir]} #{context_path}" - Command.execute(create_context_dir, false) + rc, _o, e = Command.execute(create_context_dir, false) + + if rc != 0 + OpenNebula.log_error("setup_storage: #{e}") + return + end mapper.public_send(operation, @one, context, csrc) end @@ -238,9 +244,10 @@ class Container context = @one.get_context_disk mapper = FSRawMapper.new - mapper.map(@one, context, csrc) + return unless mapper.map(@one, context, csrc) update + true end # Removes the context section from the LXD configuration and unmap the @@ -263,9 +270,11 @@ class Container # Attach disk to container (ATTACH = YES) in VM description def attach_disk(source) disk_element = hotplug_disk - return unless disk_element - setup_disk(disk_element, 'map') + raise 'Missing hotplug info' unless disk_element + + status = setup_disk(disk_element, 'map') + return unless status source2 = source.dup if source @@ -278,6 +287,7 @@ class Container @lxc['devices'].update(disk_hash) update + true end # Detects disk being hotplugged diff --git a/src/vmm_mad/remotes/lib/lxd/mapper/mapper.rb b/src/vmm_mad/remotes/lib/lxd/mapper/mapper.rb index f7ac80e7da..1ce0f1e06d 100644 --- a/src/vmm_mad/remotes/lib/lxd/mapper/mapper.rb +++ b/src/vmm_mad/remotes/lib/lxd/mapper/mapper.rb @@ -219,15 +219,16 @@ class Mapper partitions.sort! { |a,b| b['mountpoint'].length <=> a['mountpoint'].length } - - umount(partitions) - do_unmap(device, one_vm, disk, real_path) + return unless umount(partitions) + + return unless do_unmap(device, one_vm, disk, real_path) true end private + #--------------------------------------------------------------------------- # Methods to mount/umount partitions #--------------------------------------------------------------------------- @@ -238,7 +239,7 @@ class Mapper partitions.each { |p| next if !p['mountpoint'] - umount_dev(p['path']) + return nil unless umount_dev(p['path']) } end @@ -270,7 +271,8 @@ class Mapper rc, fstab, e = Command.execute(cmd, false) if fstab.empty? - umount_dev(p['path']) + return false unless umount_dev(p['path']) + next end @@ -306,11 +308,12 @@ class Mapper rc = mount_dev(p['path'], path + mount_point) return false if !rc + break } end - return rc + rc end # -------------------------------------------------------------------------- @@ -333,7 +336,12 @@ class Mapper cmd = COMMANDS[:mkdir] end - Command.execute("#{cmd} #{path}", false) + rc, _out, err = Command.execute("#{cmd} #{path}", false) + + if rc != 0 + OpenNebula.log_error("mount_dev: #{err}") + return false + end rc, _out, err = Command.execute("#{COMMANDS[:mount]} #{dev} #{path}", true) @@ -350,7 +358,12 @@ class Mapper def umount_dev(dev) OpenNebula.log_info "Umounting disk mapped at #{dev}" - Command.execute("#{COMMANDS[:umount]} #{dev}", true) + rc, _o, e = Command.execute("#{COMMANDS[:umount]} #{dev}", true) + + return true if rc.zero? + + OpenNebula.log_error("umount_dev: #{e}") + nil end #--------------------------------------------------------------------------- diff --git a/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb b/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb index 209ddfa756..a97ae56293 100644 --- a/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb +++ b/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb @@ -35,14 +35,14 @@ class Qcow2Mapper < Mapper ds = one_vm.lxdrc[:datastore_location] + "/#{one_vm.sysds_id}" File.chmod(0664, dsrc) if File.symlink?(ds) - + rc, _out, err = Command.execute(cmd, true) - - if rc != 0 + + if rc != 0 OpenNebula.log_error("do_map: #{err}") return end - + sleep 0.5 # TODO: improve settledown, lsblk -f fails device @@ -51,9 +51,12 @@ class Qcow2Mapper < Mapper def do_unmap(device, one_vm, disk, directory) cmd = "#{COMMANDS[:nbd]} -d #{device}" - rc, out, err = Command.execute(cmd, true) + rc, _out, err = Command.execute(cmd, true) - OpenNebula.log_error("do_unmap: #{err}") if rc != 0 + return true if rc.zero? + + OpenNebula.log_error("do_unmap: #{err}") + nil end private diff --git a/src/vmm_mad/remotes/lib/lxd/mapper/raw.rb b/src/vmm_mad/remotes/lib/lxd/mapper/raw.rb index a8636cc763..abfd1c709f 100644 --- a/src/vmm_mad/remotes/lib/lxd/mapper/raw.rb +++ b/src/vmm_mad/remotes/lib/lxd/mapper/raw.rb @@ -31,12 +31,10 @@ class FSRawMapper < Mapper rc, out, err = Command.execute(cmd, true) - if rc != 0 || out.empty? - OpenNebula.log_error("do_map: #{err}") - return - end + return out.chomp unless rc != 0 || out.empty? - out.chomp + OpenNebula.log_error("do_map: #{err}") + nil end def do_unmap(device, one_vm, disk, directory) @@ -44,7 +42,10 @@ class FSRawMapper < Mapper rc, _out, err = Command.execute(cmd, true) + return true if rc.zero? + OpenNebula.log_error("do_unmap: #{err}") if rc != 0 + nil end end @@ -78,10 +79,11 @@ class DiskRawMapper < Mapper dsrc = disk_source(one_vm, disk) cmd = "#{COMMANDS[:kpartx]} -d #{dsrc}" - rc, out, err = Command.execute(cmd, true) + rc, _out, err = Command.execute(cmd, true) - if rc != 0 - OpenNebula.log_error("do_unmap: #{err}") - end + return true if rc.zero? + + OpenNebula.log_error("do_unmap: #{err}") + nil end end diff --git a/src/vmm_mad/remotes/lib/lxd/mapper/rbd.rb b/src/vmm_mad/remotes/lib/lxd/mapper/rbd.rb index 58d75c5415..4132b2f8dc 100644 --- a/src/vmm_mad/remotes/lib/lxd/mapper/rbd.rb +++ b/src/vmm_mad/remotes/lib/lxd/mapper/rbd.rb @@ -39,12 +39,10 @@ class RBDMapper < Mapper rc, out, err = Command.execute(cmd, false) - if rc != 0 - OpenNebula.log_error("do_map: #{err}") - return - end + return out.chomp if rc.zero? - out.chomp + OpenNebula.log_error("do_map: #{err}") + nil end def do_unmap(device, one_vm, disk, directory) @@ -52,9 +50,9 @@ class RBDMapper < Mapper rc, _out, err = Command.execute(cmd, false) - if rc != 0 - OpenNebula.log_error("do_map: #{err}") - return - end + return true if rc.zero? + + OpenNebula.log_error("do_unmap: #{err}") + nil end end diff --git a/src/vmm_mad/remotes/lxd/attach_disk b/src/vmm_mad/remotes/lxd/attach_disk index cb73366dd0..8801fdd3e8 100755 --- a/src/vmm_mad/remotes/lxd/attach_disk +++ b/src/vmm_mad/remotes/lxd/attach_disk @@ -37,4 +37,4 @@ xml = STDIN.read client = LXDClient.new container = Container.get(vm_name, xml, client) -container.attach_disk(image_path) +raise 'failed to attach disk' unless container.attach_disk(image_path) diff --git a/src/vmm_mad/remotes/lxd/deploy b/src/vmm_mad/remotes/lxd/deploy index dfd698bb62..b68b1d641f 100755 --- a/src/vmm_mad/remotes/lxd/deploy +++ b/src/vmm_mad/remotes/lxd/deploy @@ -68,12 +68,15 @@ end if container.wild? container.start else - container.setup_storage('map') + mapped = container.setup_storage('map') + raise 'failed to setup container storage' unless mapped if container.start != 'Running' OpenNebula.log_error('Container failed to start') - container.setup_storage('unmap') + deleted = container.setup_storage('unmap') + raise 'failed to dismantle container storage' unless deleted + container.delete raise LXDError, container.status diff --git a/src/vmm_mad/remotes/lxd/detach_disk b/src/vmm_mad/remotes/lxd/detach_disk index 0a2d51b344..45efe8b308 100755 --- a/src/vmm_mad/remotes/lxd/detach_disk +++ b/src/vmm_mad/remotes/lxd/detach_disk @@ -35,4 +35,4 @@ xml = STDIN.read client = LXDClient.new container = Container.get(vm_name, xml, client) -container.detach_disk +raise 'failed to detach disk' unless container.detach_disk diff --git a/src/vmm_mad/remotes/lxd/prereconfigure b/src/vmm_mad/remotes/lxd/prereconfigure index 6ea584c9d3..6ad067905e 100755 --- a/src/vmm_mad/remotes/lxd/prereconfigure +++ b/src/vmm_mad/remotes/lxd/prereconfigure @@ -36,4 +36,4 @@ xml = STDIN.read client = LXDClient.new container = Container.get(vm_name, xml, client) -container.detach_context +raise 'Failed to detach context' unless container.detach_context diff --git a/src/vmm_mad/remotes/lxd/reconfigure b/src/vmm_mad/remotes/lxd/reconfigure index 2d24aeca83..0916d07d78 100755 --- a/src/vmm_mad/remotes/lxd/reconfigure +++ b/src/vmm_mad/remotes/lxd/reconfigure @@ -38,6 +38,8 @@ if iso_path != '' client = LXDClient.new container = Container.get(vm_name, xml, client) - container.attach_context - container.exec('one-contextd local 2>/dev/null') + raise 'Failed to attach context' unless container.attach_context + + rc, _out, err = container.exec('one-contextd local 2>/dev/null') + raise err unless rc.zero? end diff --git a/src/vmm_mad/remotes/lxd/shutdown b/src/vmm_mad/remotes/lxd/shutdown index bdcee03782..54fada3403 100755 --- a/src/vmm_mad/remotes/lxd/shutdown +++ b/src/vmm_mad/remotes/lxd/shutdown @@ -43,11 +43,10 @@ else end if !container.wild? - begin - container.setup_storage('unmap') - rescue StandardError => e + unmapped = container.setup_storage('unmap') + unless unmapped container.start - raise e + raise 'Failed to dismantle container storage' end container.delete