Class: OsCtld::Devices::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/osctld/devices/device.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, major, minor, mode, **opts) ⇒ Device

Returns a new instance of Device.

Parameters:

  • type (:char, :block)
  • major (Integer, Symbol)
  • minor (Integer, Symbol)
  • mode (String)
  • opts (Hash)

Options Hash (**opts):

  • :name (String)
  • :inherit (Boolean)

    should the child groups/containers inherit this device?

  • :inherited (Boolean)

    was this device inherited from the parent group?



42
43
44
45
46
47
48
49
50
# File 'lib/osctld/devices/device.rb', line 42

def initialize(type, major, minor, mode, **opts)
  @type = type
  @major = major
  @minor = minor
  @mode = Devices::Mode.new(mode)
  @name = opts[:name]
  @inherit = opts.has_key?(:inherit) ? opts[:inherit] : true
  @inherited = opts.has_key?(:inherited) ? opts[:inherited] : false
end

Instance Attribute Details

#inherit=(value) ⇒ Object (writeonly)

Sets the attribute inherit

Parameters:

  • value

    the value to set the attribute inherit to.



30
31
32
# File 'lib/osctld/devices/device.rb', line 30

def inherit=(value)
  @inherit = value
end

#inherited=(value) ⇒ Object (writeonly)

Sets the attribute inherited

Parameters:

  • value

    the value to set the attribute inherited to.



30
31
32
# File 'lib/osctld/devices/device.rb', line 30

def inherited=(value)
  @inherited = value
end

#majorObject (readonly)

Returns the value of attribute major.



29
30
31
# File 'lib/osctld/devices/device.rb', line 29

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



29
30
31
# File 'lib/osctld/devices/device.rb', line 29

def minor
  @minor
end

#modeObject

Returns the value of attribute mode.



29
30
31
# File 'lib/osctld/devices/device.rb', line 29

def mode
  @mode
end

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/osctld/devices/device.rb', line 29

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



29
30
31
# File 'lib/osctld/devices/device.rb', line 29

def type
  @type
end

Class Method Details

.import(hash) ⇒ Object

Load from client



18
19
20
21
22
23
24
25
26
27
# File 'lib/osctld/devices/device.rb', line 18

def self.import(hash)
  new(
    hash[:type].to_sym,
    hash[:major].to_s,
    hash[:minor].to_s,
    hash[:mode],
    name: hash[:dev_name],
    inherit: hash[:inherit]
  )
end

.load(hash) ⇒ Object

Load from config



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/osctld/devices/device.rb', line 4

def self.load(hash)
  major = hash['major'].to_s
  minor = hash['minor'].to_s
  new(
    hash['type'].to_sym,
    major == 'all' ? '*' : major,
    minor == 'all' ? '*' : minor,
    hash['mode'],
    name: hash['name'],
    inherit: hash['inherit']
  )
end

Instance Method Details

#==(other) ⇒ Object



123
124
125
# File 'lib/osctld/devices/device.rb', line 123

def ==(other)
  type == other.type && major == other.major && minor == other.minor
end

#chmod(new_mode) ⇒ Hash<Symbol, String>

Change mode and return action for cgroup update

The return value is a hash describing actions that need to be taken to update cgroups. The hash can have two keys: ‘:allow` and `:deny`, each pointing to a value that has to be written to `devices.allow` or `devices.deny`, depending on the action type.

For example, for transition from ‘rm` to `wm`, the return value would be:

{allow: 'c 1:5 w', deny: 'c 1:5 r'}

Parameters:

Returns:

  • (Hash<Symbol, String>)


82
83
84
85
86
# File 'lib/osctld/devices/device.rb', line 82

def chmod(new_mode)
  diff = mode.diff(new_mode)
  self.mode = new_mode
  diff.reject { |_k, v| v.empty? }.transform_values { |v| to_s(mode: v) }
end

#dumpObject

Dump to config



89
90
91
# File 'lib/osctld/devices/device.rb', line 89

def dump
  export.transform_keys(&:to_s)
end

#exportObject

Export to client



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/osctld/devices/device.rb', line 94

def export
  {
    type: type.to_s,
    major:,
    minor:,
    mode: mode.to_s,
    name:,
    inherit: inherit?,
    inherited: inherited?
  }
end

#inherit?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/osctld/devices/device.rb', line 52

def inherit?
  @inherit
end

#inherited?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/osctld/devices/device.rb', line 56

def inherited?
  @inherited
end

#promoted?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/osctld/devices/device.rb', line 60

def promoted?
  !@inherited
end

#to_s(opts = {}) ⇒ Object

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):



119
120
121
# File 'lib/osctld/devices/device.rb', line 119

def to_s(opts = {})
  "#{type_s} #{major}:#{minor} #{opts[:mode] || mode || 'rwm'}"
end

#type_sObject



106
107
108
109
110
111
112
113
114
115
# File 'lib/osctld/devices/device.rb', line 106

def type_s
  case type
  when :char
    'c'
  when :block
    'b'
  else
    raise "invalid device type '#{type}'"
  end
end