From 545ec988b619b4ccc20b21740bd4ede19aeee480 Mon Sep 17 00:00:00 2001 From: Daniel Clavijo Coca Date: Thu, 4 Apr 2019 14:14:41 -0600 Subject: [PATCH 1/4] L #3177 --- src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb b/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb index 1587710b33b..eba1a74b159 100644 --- a/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb +++ b/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb @@ -20,12 +20,9 @@ require 'mapper' +# Block device mapping for qcow2 disks, backed by nbd kernel module class Qcow2Mapper < Mapper - # Max number of block devices. This should be set to the parameter used - # to load the nbd kernel module (default in kernel is 16) - NBDS_MAX = 256 # TODO: Read system config file - def do_map(one_vm, disk, _directory) device = nbd_device @@ -49,10 +46,11 @@ def do_map(one_vm, disk, _directory) end def do_unmap(device, _one_vm, _disk, _directory) - #After mapping and unmapping a qcow2 disk the next mapped qcow2 may collide with the previous one. - #The use of kpartx before unmapping seems to prevent this behavior on the nbd module used with - #the kernel versions in ubuntu 16.04 - # + # After mapping and unmapping a qcow2 disk the next mapped qcow2 + # may collide with the previous one. The use of kpartx + # before unmapping seems to prevent this behavior on the nbd module + # used with the kernel versions in ubuntu 16.04 + # TODO: avoid using if kpartx was not used hide_parts(device) cmd = "#{COMMANDS[:nbd]} -d #{device}" From 6a4d9d0a8c46ad1432793aa89c229a0eea54e01a Mon Sep 17 00:00:00 2001 From: Daniel Clavijo Coca Date: Thu, 4 Apr 2019 14:15:22 -0600 Subject: [PATCH 2/4] B #3177 Load max_nbds from running module --- src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb b/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb index eba1a74b159..d95efa4d76e 100644 --- a/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb +++ b/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb @@ -65,6 +65,21 @@ def do_unmap(device, _one_vm, _disk, _directory) private + # Detects Max number of block devices + def load_nbds_max + cmd = '/sbin/modprobe -c' + rc, mods_conf, e = Command.execute(cmd, false) + + if !rc.zero? + OpenNebula.log_error("Cannot load max number of nbd devices\n#{e}") + return 0 + end + + param = mods_conf[/(nbds_max=)(\d+)/] + param.split('=').last.to_i + end + + # Returns the first non-used nbd device def nbd_device sys_parts = lsblk('') nbds = [] @@ -76,7 +91,7 @@ def nbd_device nbds << m[1].to_i end - NBDS_MAX.times do |i| + load_nbds_max.times do |i| return "/dev/nbd#{i}" unless nbds.include?(i) end From 11ae494c0daf86d5b45818cbeb9bca31912604e4 Mon Sep 17 00:00:00 2001 From: Daniel Clavijo Coca Date: Fri, 5 Apr 2019 09:47:29 -0600 Subject: [PATCH 3/4] B #3175 Updated new nbds_max method --- src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb b/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb index d95efa4d76e..e26b58d0501 100644 --- a/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb +++ b/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb @@ -23,6 +23,8 @@ # Block device mapping for qcow2 disks, backed by nbd kernel module class Qcow2Mapper < Mapper + NBDS_MAX = 256 # Default nbd module param config provided by opennebula + def do_map(one_vm, disk, _directory) device = nbd_device @@ -66,17 +68,17 @@ def do_unmap(device, _one_vm, _disk, _directory) private # Detects Max number of block devices - def load_nbds_max + def nbds_max cmd = '/sbin/modprobe -c' rc, mods_conf, e = Command.execute(cmd, false) if !rc.zero? - OpenNebula.log_error("Cannot load max number of nbd devices\n#{e}") - return 0 + OpenNebula.log_error("Cannot load max number of nbd devices\n#{e}"\ + "Falling back to default #{NBDS_MAX}") + return NBDS_MAX end - param = mods_conf[/(nbds_max=)(\d+)/] - param.split('=').last.to_i + mods_conf.match(/(nbds_max=)(\d+)/)[-1].to_i end # Returns the first non-used nbd device @@ -91,7 +93,7 @@ def nbd_device nbds << m[1].to_i end - load_nbds_max.times do |i| + nbds_max.times do |i| return "/dev/nbd#{i}" unless nbds.include?(i) end From 274019177f90698ed243ea5671e24f252e11e0b8 Mon Sep 17 00:00:00 2001 From: Daniel Clavijo Coca Date: Tue, 9 Apr 2019 00:41:01 -0500 Subject: [PATCH 4/4] B #3175 Load nbds_max from fs --- src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb b/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb index e26b58d0501..145f74f8cb3 100644 --- a/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb +++ b/src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb @@ -23,8 +23,6 @@ # Block device mapping for qcow2 disks, backed by nbd kernel module class Qcow2Mapper < Mapper - NBDS_MAX = 256 # Default nbd module param config provided by opennebula - def do_map(one_vm, disk, _directory) device = nbd_device @@ -69,16 +67,10 @@ def do_unmap(device, _one_vm, _disk, _directory) # Detects Max number of block devices def nbds_max - cmd = '/sbin/modprobe -c' - rc, mods_conf, e = Command.execute(cmd, false) - - if !rc.zero? - OpenNebula.log_error("Cannot load max number of nbd devices\n#{e}"\ - "Falling back to default #{NBDS_MAX}") - return NBDS_MAX - end - - mods_conf.match(/(nbds_max=)(\d+)/)[-1].to_i + File.read('/sys/module/nbd/parameters/nbds_max').chomp.to_i + rescue => e + OpenNebula.log_error("Cannot load kernel module parameter\n#{e}") + 0 end # Returns the first non-used nbd device @@ -93,7 +85,7 @@ def nbd_device nbds << m[1].to_i end - nbds_max.times do |i| + nbds_max.times do |i| # if nbds_max returns 0 block is skipped return "/dev/nbd#{i}" unless nbds.include?(i) end