Module: OsCtld::Utils::Devices
- Included in:
- Commands::Container::DeviceAdd, Commands::Container::DeviceChmod, Commands::Container::DeviceDelete, Commands::Container::DeviceInherit, Commands::Container::DeviceList, Commands::Container::DevicePromote, Commands::Container::DeviceReplace, Commands::Container::DeviceSetInherit, Commands::Group::DeviceAdd, Commands::Group::DeviceChmod, Commands::Group::DeviceDel, Commands::Group::DeviceInherit, Commands::Group::DeviceList, Commands::Group::DevicePromote, Commands::Group::DeviceReplace, Commands::Group::DeviceSetInherit, Commands::Group::DeviceUnsetInherit
- Defined in:
- lib/osctld/utils/devices.rb
Instance Method Summary collapse
- #add(entity, parent) ⇒ Object
- #check_mode! ⇒ Object protected
- #chmod(entity, parent = nil) ⇒ Object
- #inherit(entity) ⇒ Object
- #list(groupable, opts) ⇒ Object
- #promote(entity) ⇒ Object
- #replace(entity) ⇒ Object
- #set_inherit(entity) ⇒ Object
- #unset_inherit(entity) ⇒ Object
Instance Method Details
#add(entity, parent) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/osctld/utils/devices.rb', line 9 def add(entity, parent) dev = Devices::Device.import(opts) error!('device already exists') if entity.devices.include?(dev) check_mode! Devices::Lock.sync(entity.pool) do if !opts[:parents] && parent entity.devices.check_availability!(dev, parent) end entity.devices.add(dev, opts[:parents] ? parent : nil) entity.save_config entity.devices.apply(parents: true, descendants: true, containers: true) end ok rescue DeviceNotAvailable, DeviceModeInsufficient => e error(e.) end |
#check_mode! ⇒ Object (protected)
136 137 138 139 140 141 142 143 |
# File 'lib/osctld/utils/devices.rb', line 136 def check_mode! if /^[rwm]{1,3}$/ !~ opts[:mode] || /(.)\1+/ =~ opts[:mode] error!( 'invalid mode, allowed characters are: r for read, w for write, '+ 'm for mknod' ) end end |
#chmod(entity, parent = nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/osctld/utils/devices.rb', line 32 def chmod(entity, parent = nil) dev = entity.devices.find(opts[:type].to_sym, opts[:major], opts[:minor]) error!('device not found') unless dev check_mode! unless opts[:mode].empty? new_mode = Devices::Mode.new(opts[:mode]) Devices::Lock.sync(entity.pool) do # Check parents for device & mode if !opts[:parents] && parent entity.devices.check_availability!(dev, parent, mode: new_mode) end # Check if descendants do not require broader access mode if !opts[:recursive] entity.devices.check_descendants!(dev, mode: new_mode) end entity.devices.chmod( dev, new_mode, promote: true, parents: opts[:parents], descendants: opts[:recursive], containers: opts[:recursive] ) end ok rescue DeviceModeInsufficient, DeviceDescendantRequiresMode => e error(e.) end |
#inherit(entity) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/osctld/utils/devices.rb', line 78 def inherit(entity) dev = entity.devices.find(opts[:type].to_sym, opts[:major], opts[:minor]) error!('device not found') unless dev error!('device is already inherited') if dev.inherited? Devices::Lock.sync(entity.pool) do entity.devices.inherit_promoted(dev) end ok rescue DeviceInUse => e error(e.) end |
#list(groupable, opts) ⇒ Object
3 4 5 |
# File 'lib/osctld/utils/devices.rb', line 3 def list(groupable, opts) ok(groupable.devices.export) end |
#promote(entity) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/osctld/utils/devices.rb', line 66 def promote(entity) dev = entity.devices.find(opts[:type].to_sym, opts[:major], opts[:minor]) error!('device not found') unless dev error!('device is already promoted') unless dev.inherited? Devices::Lock.sync(entity.pool) do entity.devices.promote(dev) end ok end |
#replace(entity) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/osctld/utils/devices.rb', line 122 def replace(entity) devices = opts[:devices].map { |v| Devices::Device.import(v) } Devices::Lock.sync(entity.pool) do entity.devices.replace(devices) end ok rescue DeviceNotAvailable, DeviceModeInsufficient => e error(e.) end |
#set_inherit(entity) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/osctld/utils/devices.rb', line 93 def set_inherit(entity) dev = entity.devices.find(opts[:type].to_sym, opts[:major], opts[:minor]) error!('device not found') unless dev error!('inherit is already set') if dev.inherit? error!('only promoted devices can be manipulated') if dev.inherited? Devices::Lock.sync(entity.pool) do entity.devices.set_inherit(dev) end ok end |
#unset_inherit(entity) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/osctld/utils/devices.rb', line 106 def unset_inherit(entity) dev = entity.devices.find(opts[:type].to_sym, opts[:major], opts[:minor]) error!('device not found') unless dev error!('inherit is not set') unless dev.inherit? error!('only promoted devices can be manipulated') if dev.inherited? Devices::Lock.sync(entity.pool) do entity.devices.unset_inherit(dev) end ok rescue DeviceInUse => e error(e.) end |