From c04e7b19f97c8eb4bbb6d48edb0eccf9998611f8 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Tue, 1 Feb 2022 17:35:42 +0100 Subject: [PATCH] F #5429: Add mount options to lxc disks - rootfs options - data disk options. Can be defined per fs co-authored-by: Daniel Clavijo Coca --- src/vmm_mad/remotes/lib/lxc/container.rb | 10 +-- src/vmm_mad/remotes/lib/lxc/opennebula_vm.rb | 79 +++++++++++++------ .../remotes/lib/lxc/storage/storageutils.rb | 16 ++-- src/vmm_mad/remotes/lxc/lxcrc | 21 ++++- 4 files changed, 88 insertions(+), 38 deletions(-) diff --git a/src/vmm_mad/remotes/lib/lxc/container.rb b/src/vmm_mad/remotes/lib/lxc/container.rb index 547aef6f82..3c278962e7 100644 --- a/src/vmm_mad/remotes/lib/lxc/container.rb +++ b/src/vmm_mad/remotes/lib/lxc/container.rb @@ -52,12 +52,6 @@ class Container # Creates container in Linux def create(options = {}) - options[:config] = "#{@one.location}/deployment.file" - - File.open(options[:config], 'w+') do |file| - file.write(@one.to_lxc) - end - # Map storage error = false mounted = [] @@ -77,6 +71,10 @@ class Container return false end + # write container config file + options[:config] = "#{@one.location}/deployment.file" + File.write(options[:config], @one.to_lxc) + @client.create(@one.vm_name, options) end diff --git a/src/vmm_mad/remotes/lib/lxc/opennebula_vm.rb b/src/vmm_mad/remotes/lib/lxc/opennebula_vm.rb index 4d36948606..090ccc2d42 100644 --- a/src/vmm_mad/remotes/lib/lxc/opennebula_vm.rb +++ b/src/vmm_mad/remotes/lib/lxc/opennebula_vm.rb @@ -45,7 +45,13 @@ class LXCConfiguration < Hash }, :datastore_location => '/var/lib/one/datastores', :default_lxc_config => '/usr/share/lxc/config/common.conf', - :bindfs_mountopts => 'suid' # bindfs -o opt1,opt2 + :mountopts => { + :bindfs => 'suid', + :disk => 'rbind,create=dir,optional', + :dev_xfs => 'nouuid', + :rootfs => '', + :mountpoint => 'media/one-disk.$id', + } } # Configuration attributes that are not customizable @@ -158,16 +164,19 @@ class LXCVM < OpenNebulaVM next if xml['TYPE'].downcase == 'swap' if xml['DISK_ID'] == @rootfs_id - adisks << Disk.new_root(xml, @sysds_path, @vm_id) + adisks << Disk.new_root(xml, @sysds_path, @vm_id, + @lxcrc[:mountopts]) else - adisks << Disk.new_disk(xml, @sysds_path, @vm_id) + adisks << Disk.new_disk(xml, @sysds_path, @vm_id, + @lxcrc[:mountopts]) end end context_xml = @xml.element('//TEMPLATE/CONTEXT') if context_xml - adisks << Disk.new_context(context_xml, @sysds_path, @vm_id) + adisks << Disk.new_context(context_xml, @sysds_path, @vm_id, + @lxcrc[:mountopts]) end adisks @@ -256,18 +265,20 @@ class Disk attr_accessor :id, :vm_id, :sysds_path + DISK_TYPE = [:context, :rootfs, :entry] + class << self - def new_context(xml, sysds_path, vm_id) - Disk.new(xml, sysds_path, vm_id, false, true) + def new_context(xml, sysds_path, vm_id, mountopts) + Disk.new(xml, sysds_path, vm_id, mountopts, :context) end - def new_root(xml, sysds_path, vm_id) - Disk.new(xml, sysds_path, vm_id, true, false) + def new_root(xml, sysds_path, vm_id, mountopts) + Disk.new(xml, sysds_path, vm_id, mountopts, :rootfs) end - def new_disk(xml, sysds_path, vm_id) - Disk.new(xml, sysds_path, vm_id, false, false) + def new_disk(xml, sysds_path, vm_id, mountopts) + Disk.new(xml, sysds_path, vm_id, mountopts, :entry) end end @@ -306,18 +317,38 @@ class Disk # Generate disk into LXC config format def to_lxc - return { 'lxc.rootfs.path' => @bindpoint } if @is_rootfs + case @kind + when :context + {'lxc.mount.entry' => + "#{@bindpoint} context none ro,rbind,create=dir,optional 0 0" } - if @is_context - path = 'context' - opts = 'none rbind,ro,create=dir,optional 0 0' + when :rootfs + ropt = @lxcrc_mopts[:rootfs] + + root = { 'lxc.rootfs.path' => @bindpoint } + root['lxc.rootfs.options'] = ropt if !(ropt.nil? || ropt.empty?) + + root + + when :entry + opts = [] + + opts << 'ro' if @read_only + opts << @lxcrc_mopts[:disk] + + opts.delete_if {|o| o.nil? || o.empty? } + opt_str = opts.join(',') + + path = @xml['TARGET'] + + point = @lxcrc_mopts[:mountpoint].sub('$id', @id.to_s) + point = path[1..-1] if !(path.empty? || path[0] != '/') + + {'lxc.mount.entry' => + "#{@bindpoint} #{point} none #{opt_str} 0 0" } else - # TODO: Adjustable guest mountpoint - path = "media/one-disk.#{@id}" - opts = 'none rbind,create=dir,optional 0 0' + raise 'invalid disk type' end - - { 'lxc.mount.entry' => "#{@bindpoint} #{path} #{opts}" } end def swap? @@ -335,15 +366,17 @@ class Disk private - def initialize(xml, sysds_path, vm_id, is_rootfs, is_context) + def initialize(xml, sysds_path, vm_id, lxcrc_mopts, kind) @xml = xml @vm_id = vm_id @id = @xml['DISK_ID'].to_i - @sysds_path = sysds_path + @kind = kind - @is_rootfs = is_rootfs - @is_context = is_context + @sysds_path = sysds_path + @lxcrc_mopts = lxcrc_mopts + + @read_only = true if @xml['READONLY'].casecmp('yes').zero? # rubocop:disable all @mapper = if @xml['FORMAT'].downcase == 'qcow2' diff --git a/src/vmm_mad/remotes/lib/lxc/storage/storageutils.rb b/src/vmm_mad/remotes/lib/lxc/storage/storageutils.rb index edd8e14dc9..ce5dd06557 100644 --- a/src/vmm_mad/remotes/lib/lxc/storage/storageutils.rb +++ b/src/vmm_mad/remotes/lib/lxc/storage/storageutils.rb @@ -37,7 +37,7 @@ module Storage :e2fsck => "#{PRE}e2fsck", :resize2fs => "#{PRE}resize2fs", :xfs_growfs => "#{PRE}xfs_growfs", - :bind => "#{PRE} bindfs" # TODO, allow customize offset + :bind => "#{PRE} bindfs" } private_constant :COMMANDS @@ -55,23 +55,27 @@ module Storage device_fs = device_fs(device) end + opts_fs = options[:mountopts]["dev_#{device_fs}".to_sym] + opts_fs ||= '' + # resize and mount operations according to the used filesystem case device_fs when /^ext([2-4])$/ resize_ext(device) - return false unless mount(device, mountpoint) + return false unless mount(device, mountpoint, opts_fs) when 'xfs' - return false unless mount(device, mountpoint, 'nouuid') + return false unless mount(device, mountpoint, opts_fs) resize_xfs(mountpoint) else - return false unless mount(device, mountpoint) + return false unless mount(device, mountpoint, opts_fs) end # Bind @mountpoint into to the public accesible folder (@bindpoint) return true if bind(mountpoint, bindpoint, options) - rescue StandardError + rescue StandardError => e + OpenNebula.log_error e end umount(bindpoint) @@ -166,7 +170,7 @@ module Storage cmd_opts = "--uid-offset=#{offset} "\ "--gid-offset=#{offset} "\ - "-o #{options[:bindfs_mountopts]}" + "-o #{options[:mountopts][:bindfs]}" # Bindfs cmd = "#{COMMANDS[:bind]} #{cmd_opts} #{src} #{target}" diff --git a/src/vmm_mad/remotes/lxc/lxcrc b/src/vmm_mad/remotes/lxc/lxcrc index d3ca90b79e..defb074666 100644 --- a/src/vmm_mad/remotes/lxc/lxcrc +++ b/src/vmm_mad/remotes/lxc/lxcrc @@ -40,7 +40,22 @@ # Path to the LXC default configuration file. This file will be included in # the configuration of every LXC container (lxc.include) :default_lxc_config: /usr/share/lxc/config/common.conf + +################################################################################ +# Container storage interface mount options +################################################################################ # -# Comma separated list of mount options used when shifting the uid/gid with -# bindfs. See command help -:bindfs_mountopts: suid \ No newline at end of file +# Options to customize the mounting process of the container disks +# :bindfs: arguments passed to bindfs -o +# :dev_: Mount options for disk devices (in the host). Options are set per +# fs type (e.g. dev_xfs, dev_ext3...) +# :disk: Mount options for data DISK (lxc.mount.entry). +# :rootfs: Mount options for root fs (lxc.rootfs.options) +# :mountpoint: Default Path to mount data disk in the container. This can be +# set per DISK using the TARGET attribute. +:mountopts: + :bindfs: 'suid' + :dev_xfs: 'nouuid' + :disk: 'rbind,create=dir,optional' + :rootfs: '' + :mountpoint: 'media/one-disk.$id'