Module: OsCtld::Utils::Devices

Instance Method Summary collapse

Instance Method Details

#add(entity, parent) ⇒ Object

Parameters:



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

Parameters:



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.message)
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.message)
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.message)
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.message)
end