Class: OsCtld::Mount::Manager
- Inherits:
-
Object
- Object
- OsCtld::Mount::Manager
- Includes:
- Enumerable, OsCtl::Lib::Utils::Log, Lockable, Utils::SwitchUser
- Defined in:
- lib/osctld/mount/manager.rb
Instance Attribute Summary collapse
-
#ct ⇒ Object
readonly
protected
Returns the value of attribute ct.
-
#entries ⇒ Object
readonly
protected
Returns the value of attribute entries.
-
#shared_dir ⇒ Object
readonly
Returns the value of attribute shared_dir.
Class Method Summary collapse
-
.load(ct, cfg) ⇒ Object
Load mounts from config.
Instance Method Summary collapse
- #<<(mnt) ⇒ Object
-
#activate(mountpoint) ⇒ Object
Mount the directory inside the container.
- #add(mnt) ⇒ Object
-
#all_entries ⇒ Array<Mount::Entry>
Return all mount entries, including internal entries.
-
#clear ⇒ Object
Remove all mounts.
-
#deactivate(mountpoint) ⇒ Object
Unmount the directory from the container.
- #delete_at(mountpoint) ⇒ Object
-
#dump ⇒ Object
Dump mounts into config.
- #dup(new_ct) ⇒ Object
- #each(&block) ⇒ Object
- #find_at(mountpoint) ⇒ Object
-
#initialize(ct, entries: []) ⇒ Manager
constructor
A new instance of Manager.
-
#prune ⇒ Object
Remote temporal mounts.
- #register(mnt) ⇒ Object
- #unmount(mnt) ⇒ Object protected
Methods included from Utils::SwitchUser
Methods included from Lockable
#exclusively, included, #inclusively, #init_lock, #lock, #unlock
Constructor Details
Instance Attribute Details
#ct ⇒ Object (readonly, protected)
Returns the value of attribute ct.
181 182 183 |
# File 'lib/osctld/mount/manager.rb', line 181 def ct @ct end |
#entries ⇒ Object (readonly, protected)
Returns the value of attribute entries.
181 182 183 |
# File 'lib/osctld/mount/manager.rb', line 181 def entries @entries end |
#shared_dir ⇒ Object (readonly)
Returns the value of attribute shared_dir.
18 19 20 |
# File 'lib/osctld/mount/manager.rb', line 18 def shared_dir @shared_dir end |
Class Method Details
Instance Method Details
#<<(mnt) ⇒ Object
47 48 49 |
# File 'lib/osctld/mount/manager.rb', line 47 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.
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/osctld/mount/manager.rb', line 147 def activate(mountpoint) mnt = find_at(mountpoint) if mnt.nil? raise MountNotFound, mountpoint elsif !mnt.fs || !mnt.type || !mnt.opts raise MountInvalid, 'incomplete mount: missing fs, type or opts' elsif !%w[bind none].include?(mnt.type) raise MountInvalid, "can activate only bind mounts, not #{mnt.type}" end shared_dir.propagate(mnt) end |
#add(mnt) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/osctld/mount/manager.rb', line 29 def add(mnt) exclusively do entries << mnt end ct.save_config ct.lxc_config.configure_mounts return if !mnt.automount? || mnt.type != 'bind' ct.exclusively do next unless ct.fresh_state == :running shared_dir.propagate(mnt) end end |
#all_entries ⇒ Array<Mount::Entry>
Return all mount entries, including internal entries
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/osctld/mount/manager.rb', line 123 def all_entries inclusively do [Mount::Entry.new( shared_dir.path, shared_dir.mountpoint, 'none', 'bind,create=dir,ro', true ), Mount::Entry.new( BpfFs.ct_mount_path(ct.pool.name, ct.id), '/sys/fs/bpf', 'none', 'bind,create=dir,rw', true )] + entries.select { |m| m.in_config? || (m.automount? && !m.temp?) } end end |
#clear ⇒ Object
Remove all mounts
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/osctld/mount/manager.rb', line 88 def clear exclusively do if ct.fresh_state == :running entries.reverse_each { |m| unmount(m) } end entries.clear end ct.save_config ct.lxc_config.configure_mounts end |
#deactivate(mountpoint) ⇒ Object
Unmount the directory from the container
163 164 165 166 167 168 |
# File 'lib/osctld/mount/manager.rb', line 163 def deactivate(mountpoint) mnt = find_at(mountpoint) raise MountNotFound, mountpoint unless mnt unmount(mnt) end |
#delete_at(mountpoint) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/osctld/mount/manager.rb', line 69 def delete_at(mountpoint) exclusively do mnt = entries.detect { |m| m.mountpoint == mountpoint } next unless mnt ct.exclusively do next unless ct.fresh_state == :running unmount(mnt) end entries.delete(mnt) end ct.save_config ct.lxc_config.configure_mounts end |
#dump ⇒ Object
Dump mounts into config
117 118 119 |
# File 'lib/osctld/mount/manager.rb', line 117 def dump map(&:dump) end |
#dup(new_ct) ⇒ Object
170 171 172 173 174 175 176 177 |
# File 'lib/osctld/mount/manager.rb', line 170 def dup(new_ct) ret = super() ret.init_lock ret.instance_variable_set('@ct', new_ct) ret.instance_variable_set('@entries', entries.map { |v| v.dup(new_ct) }) ret.instance_variable_set('@shared_dir', shared_dir.dup(new_ct)) ret end |
#each(&block) ⇒ Object
108 109 110 111 112 |
# File 'lib/osctld/mount/manager.rb', line 108 def each(&block) inclusively do entries.each(&block) end end |
#find_at(mountpoint) ⇒ Object
62 63 64 65 66 |
# File 'lib/osctld/mount/manager.rb', line 62 def find_at(mountpoint) inclusively do entries.detect { |m| m.mountpoint == mountpoint } end end |
#prune ⇒ Object
Remote temporal mounts
102 103 104 105 106 |
# File 'lib/osctld/mount/manager.rb', line 102 def prune exclusively do entries.delete_if(&:temp?) end end |
#register(mnt) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/osctld/mount/manager.rb', line 52 def register(mnt) exclusively do entries << mnt end ct.save_config ct.lxc_config.configure_mounts end |
#unmount(mnt) ⇒ Object (protected)
183 184 185 186 187 |
# File 'lib/osctld/mount/manager.rb', line 183 def unmount(mnt) ContainerControl::Commands::Unmount.run!(ct, mnt.mountpoint) rescue ContainerControl::Error => e raise UnmountError, e. end |