Module: OsCtld::Utils::Devices

Instance Method Summary collapse

Instance Method Details

#add(entity) ⇒ Object

Parameters:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/osctld/utils/devices.rb', line 8

def add(entity)
  dev = Devices::Device.import(opts)
  error!('device already exists') if entity.devices.include?(dev)
  check_mode!

  Devices::Lock.sync(entity.pool) do
    unless opts[:parents]
      entity.devices.check_availability!(dev)
    end

    entity.devices.add(dev)
    entity.save_config
  end

  ok
rescue DeviceNotAvailable, DeviceModeInsufficient => e
  error(e.message)
end

#check_mode!Object (protected)



129
130
131
132
133
134
135
136
# File 'lib/osctld/utils/devices.rb', line 129

def check_mode!
  return unless /^[rwm]{1,3}$/ !~ opts[:mode] || /(.)\1+/ =~ opts[:mode]

  error!(
    'invalid mode, allowed characters are: r for read, w for write, ' \
    'm for mknod'
  )
end

#chmod(entity) ⇒ Object

Parameters:



28
29
30
31
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
# File 'lib/osctld/utils/devices.rb', line 28

def chmod(entity)
  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
    unless opts[:parents]
      entity.devices.check_availability!(dev, mode: new_mode)
    end

    # Check if descendants do not require broader access mode
    unless 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



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/osctld/utils/devices.rb', line 73

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



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/osctld/utils/devices.rb', line 61

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



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/osctld/utils/devices.rb', line 115

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



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/osctld/utils/devices.rb', line 87

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/osctld/utils/devices.rb', line 100

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