diff --git a/src/vmm_mad/remotes/lib/lxd/container.rb b/src/vmm_mad/remotes/lib/lxd/container.rb index b3f9b7a4aa..a4888df0e0 100644 --- a/src/vmm_mad/remotes/lib/lxd/container.rb +++ b/src/vmm_mad/remotes/lib/lxd/container.rb @@ -312,15 +312,13 @@ class Container return unless @one @one.get_disks.each do |disk| + next if @one.swap?(disk) return nil unless setup_disk(disk, operation) end - return true unless @one.has_context? + return true unless @one.context? - csrc = @lxc['devices']['context']['source'].clone - - context = @one.get_context_disk - mapper = FSRawMapper.new + mapper = FSRawMapper.new if operation == 'map' mk_context_dir = "#{Mapper::COMMANDS[:su_mkdir]} #{@context_path}" @@ -333,39 +331,32 @@ class Container end end - mapper.public_send(operation, @one, context, csrc) + mapper.public_send(operation, @one, @one.context_disk, context_source) end # Generate the context devices and maps the context the device def attach_context @one.context(@lxc['devices']) - csrc = @lxc['devices']['context']['source'].clone - - context = @one.get_context_disk - mapper = FSRawMapper.new - - return unless mapper.map(@one, context, csrc) + mapper = FSRawMapper.new + return unless mapper.map(@one, @one.context_disk, context_source) update true end - # Removes the context section from the LXD configuration and unmap the - # context device + # Removes the context section from the LXD configuration and + # unmaps the context device def detach_context - return true unless @one.has_context? + return true unless @one.context? - csrc = @lxc['devices']['context']['source'].clone + context_src = context_source @lxc['devices'].delete('context') - update - context = @one.get_context_disk - mapper = FSRawMapper.new - - mapper.unmap(@one, context, csrc) + mapper = FSRawMapper.new + mapper.unmap(@one, @one.context_disk, context_src) end # Attach disk to container (ATTACH = YES) in VM description @@ -373,11 +364,11 @@ class Container disk_element = hotplug_disk raise 'Missing hotplug info' unless disk_element + return if @one.swap?(disk_element) return unless setup_disk(disk_element, 'map') disk_hash = @one.disk(disk_element, nil, nil) - @lxc['devices'].update(disk_hash) update @@ -548,10 +539,10 @@ class Container # TODO This maps should be built dynamically or based on a DISK attribute # so new mappers does not need to modified source code def new_disk_mapper(disk) - case disk['TYPE'] - when 'FILE', 'BLOCK' + ds = @one.disk_source(disk) - ds = @one.disk_source(disk) + case disk['DISK_TYPE'] + when 'FILE', 'BLOCK' cmd = "#{Mapper::COMMANDS[:file]} #{ds}" rc, out, err = Command.execute(cmd, false) @@ -586,4 +577,14 @@ class Container end end + def context_source + return unless @one.context? + + disk_source('context') + end + + def disk_source(disk_name) + @lxc['devices'][disk_name]['source'].clone + end + end diff --git a/src/vmm_mad/remotes/lib/lxd/mapper/mapper.rb b/src/vmm_mad/remotes/lib/lxd/mapper/mapper.rb index fc6ea1d293..b499d9b9bf 100644 --- a/src/vmm_mad/remotes/lib/lxd/mapper/mapper.rb +++ b/src/vmm_mad/remotes/lib/lxd/mapper/mapper.rb @@ -70,7 +70,8 @@ class Mapper :xfs_growfs => 'sudo xfs_growfs', :rbd => 'sudo rbd-nbd --id', :xfs_admin => 'sudo xfs_admin', - :tune2fs => 'sudo tune2fs' + :tune2fs => 'sudo tune2fs', + :mkfs => '/sbin/mkfs' } #--------------------------------------------------------------------------- @@ -86,7 +87,7 @@ class Mapper # Errors should be log using OpenNebula driver functions def do_map(_one_vm, _disk, _directory) OpenNebula.log_error("map function not implemented for #{self.class}") - nil + "" end # Unmaps a previously mapped partition @@ -97,7 +98,7 @@ class Mapper # @return nil def do_unmap(_device, _one_vm, _disk, _directory) OpenNebula.log_error("unmap function not implemented for #{self.class}") - nil + "" end #--------------------------------------------------------------------------- @@ -120,6 +121,10 @@ class Mapper return false unless device + if one_vm.volatile?(disk) + return false unless mkfs(device, one_vm.lxdrc[:filesystem]) + end + partitions = lsblk(device) return false unless partitions @@ -167,7 +172,7 @@ class Mapper real_path = File.realpath(directory) if !is_rootfs && is_shared_ds - sys_parts.each {|d| + sys_parts.each do |d| if d['mountpoint'] == real_path partitions = [d] device = d['path'] @@ -175,32 +180,32 @@ class Mapper end if d['children'] - d['children'].each {|c| + d['children'].each do |c| next unless c['mountpoint'] == real_path partitions = d['children'] device = d['path'] break - } + end end break unless partitions.empty? - } + end partitions.delete_if {|p| !p['mountpoint'] } - partitions.sort! {|a, b| + partitions.sort! do |a, b| b['mountpoint'].length <=> a['mountpoint'].length - } - - if device.empty? - OpenNebula.log_error("Failed to detect block device from #{directory}") - return true end - return unless umount(partitions) + if device.empty? + OpenNebula.log_error("Cannot detect block device from #{directory}") + return false + end - return unless do_unmap(device, one_vm, disk, real_path) + return false unless umount(partitions) + + return false unless do_unmap(device, one_vm, disk, real_path) true end @@ -214,11 +219,11 @@ class Mapper # Umounts partitions # @param partitions [Array] with partition device names def umount(partitions) - partitions.each {|p| + partitions.each do |p| next unless p['mountpoint'] return nil unless umount_dev(p['path']) - } + end end # Mounts partitions @@ -287,15 +292,15 @@ class Mapper # for host partitions # @return [Hash] with partitions def lsblk(device) + partitions = {} + rc, o, e = Command.execute("#{COMMANDS[:lsblk]} -OJ #{device}", false) if rc != 0 || o.empty? OpenNebula.log_error("lsblk: #{e}") - return + return partitions end - partitions = nil - begin partitions = JSON.parse(o)['blockdevices'] @@ -308,21 +313,21 @@ class Mapper partitions = [partitions] end - partitions.delete_if {|p| - p['fstype'].casecmp('swap').zero? if p['fstype'] - } + partitions.delete_if do |p| + p['fstype'].casecmp('swap').zero? if p['fstype'] + end end - rescue + rescue StandardError OpenNebula.log_error("lsblk: error parsing lsblk -OJ #{device}") - return + return {} end # Fix for lsblk paths for version < 2.33 - partitions.each {|p| + partitions.each do |p| lsblk_path(p) p['children'].each {|q| lsblk_path(q) } if p['children'] - } + end partitions end @@ -446,13 +451,13 @@ class Mapper next if %w[/ swap].include?(mount_point) - partitions.each {|p| + partitions.each do |p| next if p[key] != value return false unless mount_dev(p['path'], path + mount_point) break - } + end end true @@ -521,13 +526,14 @@ class Mapper cmd = "#{COMMANDS[:tune2fs]} -U random #{device}" else - return + return true end rc, o, e = Command.execute(cmd, false) - return if rc.zero? + return true if rc.zero? OpenNebula.log_error "#{__method__}: error changing UUID: #{o}\n#{e}\n" + false end def get_fstype(device) @@ -536,12 +542,12 @@ class Mapper fstype = '' - o.each_line {|l| + o.each_line do |l| next unless (m = l.match(/TYPE=(.*)/)) fstype = m[1] break - } + end fstype end @@ -552,4 +558,15 @@ class Mapper Command.execute(cmd, false) end + # Formats a block device + def mkfs(device, format) + cmd = "#{COMMANDS[:mkfs]}.#{format} #{device}" + rc, o, e = Command.execute(cmd, false) + + return true if rc.zero? + + OpenNebula.log_error "Failed to format #{device}\n#{o}\n#{e}" + false + end + end diff --git a/src/vmm_mad/remotes/lib/lxd/opennebula_vm.rb b/src/vmm_mad/remotes/lib/lxd/opennebula_vm.rb index 47df5c64b8..9ac659983a 100644 --- a/src/vmm_mad/remotes/lib/lxd/opennebula_vm.rb +++ b/src/vmm_mad/remotes/lib/lxd/opennebula_vm.rb @@ -26,6 +26,7 @@ class LXDConfiguration < Hash :height => '600', :timeout => '300' }, + :filesystem => 'ext4', :datastore_location => '/var/lib/one/datastores' } @@ -77,7 +78,7 @@ class OpenNebulaVM @rootfs_id = boot_order.split(',')[0][-1] unless boot_order.empty? end - def has_context? + def context? !@xml['//TEMPLATE/CONTEXT/DISK_ID'].empty? end @@ -156,7 +157,7 @@ class OpenNebulaVM # Creates a nic hash from NIC xml root def nic(info) eth = { - 'name' => "eth#{info['NIC_ID']}", + 'name' => "eth#{info['NIC_ID']}", 'host_name' => info['TARGET'], 'parent' => info['BRIDGE'], 'hwaddr' => info['MAC'], @@ -194,20 +195,20 @@ class OpenNebulaVM end end - def get_context_disk + # Context disk XML + def context_disk @xml.element('//TEMPLATE/CONTEXT') end + # Disk XML array def get_disks @xml.elements('//TEMPLATE/DISK') end # Sets up the storage devices configuration in devices def storage(hash) - disks = @xml.elements('//TEMPLATE/DISK') - - disks.each do |n| - next unless valid_disk?(n) + get_disks.each do |n| + next if swap?(n) hash.update(disk(n, nil, nil)) end @@ -230,6 +231,7 @@ class OpenNebulaVM } end + # Returns the disk mountpoint on the LXD node fs def disk_mountpoint(disk_id) datastore = @sysds_path datastore = File.readlink(@sysds_path) if File.symlink?(@sysds_path) @@ -238,16 +240,20 @@ class OpenNebulaVM # @return [String] the canonical disk path for the given disk def disk_source(disk) - disk_id = disk['DISK_ID'] + disk_id = disk['DISK_ID'] + disk_type = disk['DISK_TYPE'] - if disk['TYPE'] == 'RBD' - src = disk['SOURCE'] - return "#{src}-#{vm_id}-#{disk['DISK_ID']}" if disk['CLONE'] == 'YES' + return "#{@sysds_path}/#{@vm_id}/disk.#{disk_id}" unless disk_type == 'RBD' - return src + src = disk['SOURCE'] + + if disk['CLONE'] == 'YES' + "#{src}-#{vm_id}-#{disk_id}" + elsif volatile?(disk) + "#{disk['POOL_NAME']}/one-sys-#{vm_id}-#{disk_id}" + else + src end - - "#{@sysds_path}/#{@vm_id}/disk.#{disk_id}" end # Creates a disk hash from DISK xml element @@ -318,10 +324,12 @@ class OpenNebulaVM { disk_name => disk } end - def valid_disk?(disk) - return true if %w[FILE BLOCK RBD].include? disk['TYPE'] + def swap?(disk) + return disk['TYPE'] == 'swap' + end - false + def volatile?(disk) + return disk['TYPE'] == 'fs' end #--------------------------------------------------------------------------- diff --git a/src/vmm_mad/remotes/lxd/lxdrc b/src/vmm_mad/remotes/lxd/lxdrc index 2c54353d97..9ff6c5ef3f 100644 --- a/src/vmm_mad/remotes/lxd/lxdrc +++ b/src/vmm_mad/remotes/lxd/lxdrc @@ -14,9 +14,9 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -################################################################################ +############################################################################## # VNC Options -################################################################################ +############################################################################## # # Options to customize the VNC access to the container: # - :command: to be executed in the VNC terminal. @@ -29,6 +29,14 @@ :height: 600 :timeout: 300 +############################################################################## +# Mapper Options +############################################################################## +# +# Default filesystem format for LXD-driver formatted disks. Value should be +# the same as would be in mkfs -t $filesystem $device +:filesystem: ext4 + ################################################################################ # OpenNebula Configuration Options ################################################################################