Class: OsCtld::Mount::Manager

Inherits:
Object
  • Object
show all
Includes:
Enumerable, OsCtl::Lib::Utils::Log, Lockable, Utils::SwitchUser
Defined in:
lib/osctld/mount/manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::SwitchUser

#ct_attach, #ct_syscmd

Methods included from Lockable

#exclusively, included, #inclusively, #init_lock, #lock, #unlock

Constructor Details

#initialize(ct, entries: []) ⇒ Manager

Returns a new instance of Manager

Parameters:



20
21
22
23
24
25
# File 'lib/osctld/mount/manager.rb', line 20

def initialize(ct, entries: [])
  init_lock
  @ct = ct
  @entries = entries
  @shared_dir = Mount::SharedDir.new(ct)
end

Instance Attribute Details

#ctObject (readonly, protected)

Returns the value of attribute ct



151
152
153
# File 'lib/osctld/mount/manager.rb', line 151

def ct
  @ct
end

#entriesObject (readonly, protected)

Returns the value of attribute entries



151
152
153
# File 'lib/osctld/mount/manager.rb', line 151

def entries
  @entries
end

#shared_dirObject (readonly)

Returns the value of attribute shared_dir



17
18
19
# File 'lib/osctld/mount/manager.rb', line 17

def shared_dir
  @shared_dir
end

Class Method Details

.load(ct, cfg) ⇒ Object

Load mounts from config

Parameters:



13
14
15
# File 'lib/osctld/mount/manager.rb', line 13

def self.load(ct, cfg)
  new(ct, entries: cfg.map { |v| Mount::Entry.load(ct, v) })
end

Instance Method Details

#<<(mnt) ⇒ Object

Parameters:



45
46
47
# File 'lib/osctld/mount/manager.rb', line 45

def <<(mnt)
  add(mnt)
end

#activate(mountpoint) ⇒ Object

Mount the directory inside the container

WARNING: this method can mount the directory multiple times! It is the caller's responsibility to ensure that the container is running.

Parameters:

  • mountpoint (String)

Raises:



124
125
126
127
128
129
130
# File 'lib/osctld/mount/manager.rb', line 124

def activate(mountpoint)
  mnt = find_at(mountpoint)
  raise MountNotFound, mountpoint unless mnt
  raise MountInvalid if !mnt.fs || !mnt.type || !mnt.opts

  shared_dir.propagate(mnt)
end

#add(mnt) ⇒ Object

Parameters:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/osctld/mount/manager.rb', line 28

def add(mnt)
  exclusively do
    entries << mnt
  end

  ct.save_config
  ct.lxc_config.configure_mounts

  return unless mnt.automount?

  ct.exclusively do
    next unless ct.current_state == :running
    shared_dir.propagate(mnt)
  end
end

#all_entriesArray<Mount::Entry>

Return all mount entries, including internal entries

Returns:



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

def all_entries
  inclusively do
    [Mount::Entry.new(
      shared_dir.path,
      shared_dir.mountpoint,
      'none',
      'bind,create=dir,ro',
      true
    )] + entries.select { |m| m.automount? && !m.temp? }
  end
end

#deactivate(mountpoint) ⇒ Object

Unmount the directory from the container

Parameters:

  • mountpoint (String)

Raises:



134
135
136
137
138
139
# File 'lib/osctld/mount/manager.rb', line 134

def deactivate(mountpoint)
  mnt = find_at(mountpoint)
  raise MountNotFound, mountpoint unless mnt

  unmount(mnt)
end

#delete_at(mountpoint) ⇒ Object

Parameters:

  • mountpoint (String)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/osctld/mount/manager.rb', line 67

def delete_at(mountpoint)
  exclusively do
    mnt = entries.detect { |m| m.mountpoint == mountpoint }
    next unless mnt

    ct.exclusively do
      next unless ct.current_state == :running
      unmount(mnt)
    end

    entries.delete(mnt)
  end

  ct.save_config
  ct.lxc_config.configure_mounts
end

#dumpObject

Dump mounts into config



100
101
102
# File 'lib/osctld/mount/manager.rb', line 100

def dump
  map(&:dump)
end

#dup(new_ct) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/osctld/mount/manager.rb', line 141

def dup(new_ct)
  ret = super()
  ret.init_lock
  ret.instance_variable_set('@ct', new_ct)
  ret.instance_variable_set('@entries', entries.map(&:clone))
  ret.instance_variable_set('@shared_dir', shared_dir.dup(new_ct))
  ret
end

#each(&block) ⇒ Object



91
92
93
94
95
# File 'lib/osctld/mount/manager.rb', line 91

def each(&block)
  inclusively do
    entries.each(&block)
  end
end

#find_at(mountpoint) ⇒ Object

Parameters:

  • mountpoint (String)


60
61
62
63
64
# File 'lib/osctld/mount/manager.rb', line 60

def find_at(mountpoint)
  inclusively do
    entries.detect { |m| m.mountpoint == mountpoint }
  end
end

#pruneObject

Remote temporal mounts



85
86
87
88
89
# File 'lib/osctld/mount/manager.rb', line 85

def prune
  exclusively do
    entries.delete_if { |m| m.temp? }
  end
end

#register(mnt) ⇒ Object

Parameters:



50
51
52
53
54
55
56
57
# File 'lib/osctld/mount/manager.rb', line 50

def register(mnt)
  exclusively do
    entries << mnt
  end

  ct.save_config
  ct.lxc_config.configure_mounts
end

#unmount(mnt) ⇒ Object (protected)



153
154
155
156
157
# File 'lib/osctld/mount/manager.rb', line 153

def unmount(mnt)
  ContainerControl::Commands::Unmount.run!(ct, mnt.mountpoint)
rescue ContainerControl::Error => e
  raise UnmountError, e.message
end