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 (Symbol)

    `:char` or `:block`

  • major (Integer, Symbol)
  • minor (Integer, Symbol)
  • mode (String)
  • opts (Hash) (defaults to: {})

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



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

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>)


78
79
80
81
82
# File 'lib/osctld/devices/device.rb', line 78

def chmod(new_mode)
  diff = mode.diff(new_mode)
  self.mode = new_mode
  Hash[ diff.reject { |k,v| v.empty? }.map { |k,v| [k, to_s(mode: v)] } ]
end

#dumpObject

Dump to config



85
86
87
# File 'lib/osctld/devices/device.rb', line 85

def dump
  Hash[export.map { |k,v| [k.to_s, v] }]
end

#exportObject

Export to client



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/osctld/devices/device.rb', line 90

def export
  {
    type: type.to_s,
    major: major,
    minor: minor,
    mode: mode.to_s,
    name: 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

#to_s(opts = {}) ⇒ Object

Parameters:

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

Options Hash (opts):



115
116
117
# File 'lib/osctld/devices/device.rb', line 115

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

#type_sObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/osctld/devices/device.rb', line 102

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