F #5429: Add mount options to lxc disks

- rootfs options
  - data disk options. Can be defined per fs

co-authored-by: Daniel Clavijo Coca <dclavijo@opennebula.io>
This commit is contained in:
Ruben S. MonteroandDaniel Clavijo Coca committed 2022-02-01 17:35:42 +01:00
1 parent 3a3798a239
commit c04e7b19f9
4 files changed
+88 -38

No files matched your search

+4 -6
View File
@@ -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
+56 -23
View File
@@ -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'
@@ -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}"
+18 -3
View File
@@ -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 <bindfs -o> command help
:bindfs_mountopts: suid
# Options to customize the mounting process of the container disks
# :bindfs: arguments passed to bindfs -o <opts>
# :dev_<fs>: 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'