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
26
# 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
    if !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)



133
134
135
136
137
138
139
140
# File 'lib/osctld/utils/devices.rb', line 133

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

Parameters:



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

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
    if !opts[:parents]
      entity.devices.check_availability!(dev, 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



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

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



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

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



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/osctld/utils/devices.rb', line 119

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



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

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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/osctld/utils/devices.rb', line 103

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