Class: OsCtld::Mount::SharedDir
- Inherits:
-
Object
- Object
- OsCtld::Mount::SharedDir
- Includes:
- OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, Utils::SwitchUser
- Defined in:
- lib/osctld/mount/shared_dir.rb
Instance Attribute Summary collapse
-
#ct ⇒ Object
readonly
protected
Returns the value of attribute ct.
Instance Method Summary collapse
-
#cleanup_pushed(dir) ⇒ Object
Cleanup after #map_and_push.
-
#create ⇒ Object
Prepare the shared mount directory on the host.
- #create_readme ⇒ Object protected
- #dup(new_ct) ⇒ Object
- #host_path_for(dir) ⇒ String
-
#initialize(ct) ⇒ SharedDir
constructor
A new instance of SharedDir.
-
#map_and_push(dir, user_ns) ⇒ Object
Bind-mount path with ID-mapping and push it through the shared directory.
-
#mountpoint ⇒ String
Mountpoint relative to the container's rootfs.
- #path ⇒ String
-
#propagate(mnt) ⇒ Object
Propagate a new mount inside the container via the shared directory.
- #prune_empty_child_directories(dir) ⇒ Object protected
- #readme_path ⇒ Object protected
-
#remove ⇒ Object
Remove the shared mount directory from the host.
- #unmount(dir) ⇒ Object protected
Methods included from Utils::SwitchUser
Constructor Details
#initialize(ct) ⇒ SharedDir
Returns a new instance of SharedDir.
11 12 13 |
# File 'lib/osctld/mount/shared_dir.rb', line 11 def initialize(ct) @ct = ct end |
Instance Attribute Details
#ct ⇒ Object (readonly, protected)
Returns the value of attribute ct.
133 134 135 |
# File 'lib/osctld/mount/shared_dir.rb', line 133 def ct @ct end |
Instance Method Details
#cleanup_pushed(dir) ⇒ Object
Cleanup after #map_and_push
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/osctld/mount/shared_dir.rb', line 95 def cleanup_pushed(dir) host_path = host_path_for(dir) syscmd("umount \"#{host_path}\"", valid_rcs: [32]) # 32 = not mounted begin Dir.rmdir(host_path) rescue Errno::ENOENT # pass end nil end |
#create ⇒ Object
Prepare the shared mount directory on the host
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/osctld/mount/shared_dir.rb', line 16 def create dir = Pathname.new(path) unless dir.exist? dir.mkdir syscmd("mount --bind \"#{dir}\" \"#{dir}\"") syscmd("mount --make-rshared \"#{dir}\"") end create_readme unless File.exist?(readme_path) end |
#create_readme ⇒ Object (protected)
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/osctld/mount/shared_dir.rb', line 164 def create_readme File.write( readme_path, <<~END Directory `#{File.join('/', mountpoint)}` is used by osctl from vpsAdminOS to propagate new mounts into this container. Do not remove nor unmount this directory, or you'll have to restart your container to create new mounts! END ) end |
#dup(new_ct) ⇒ Object
125 126 127 128 129 |
# File 'lib/osctld/mount/shared_dir.rb', line 125 def dup(new_ct) ret = super() ret.instance_variable_set('@ct', new_ct) ret end |
#host_path_for(dir) ⇒ String
110 111 112 |
# File 'lib/osctld/mount/shared_dir.rb', line 110 def host_path_for(dir) File.join(path, Digest::SHA2.hexdigest(dir)) end |
#map_and_push(dir, user_ns) ⇒ Object
Bind-mount path with ID-mapping and push it through the shared directory
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/osctld/mount/shared_dir.rb', line 72 def map_and_push(dir, user_ns) host_path = host_path_for(dir) user_ns_path = if user_ns.respond_to?(:fileno) File.join('/proc', Process.pid.to_s, 'fd', user_ns.fileno.to_s) else File.join('/proc', user_ns.to_s, 'ns', 'user') end Dir.mkdir(host_path) syscmd_argv([ 'mount', '--bind', '-o', "X-mount.idmap=#{user_ns_path}", dir, host_path ]) host_path end |
#mountpoint ⇒ String
Mountpoint relative to the container's rootfs
121 122 123 |
# File 'lib/osctld/mount/shared_dir.rb', line 121 def mountpoint 'dev/.osctl-mount-helper' end |
#path ⇒ String
115 116 117 |
# File 'lib/osctld/mount/shared_dir.rb', line 115 def path File.join(ct.pool.mount_dir, ct.id) end |
#propagate(mnt) ⇒ Object
Propagate a new mount inside the container via the shared directory
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 65 66 |
# File 'lib/osctld/mount/shared_dir.rb', line 39 def propagate(mnt) # Bind-mount the new mount into the shared directory host_path = host_path_for(mnt.mountpoint) Dir.mkdir(host_path) opts = if ct.map_mode == 'native' && mnt.map_ids "-o X-mount.idmap=/proc/#{ct.init_pid}/ns/user" end syscmd("mount --bind #{opts} \"#{mnt.fs}\" \"#{host_path}\"") # Move the mount inside the container to the right place begin ContainerControl::Commands::Mount.run!( ct, shared_dir: File.join('/', mountpoint), src: File.basename(host_path), dst: File.join('/', mnt.mountpoint) ) rescue ContainerControl::Error => e log(:warn, ct, "Failed to mount #{mnt.mountpoint} at runtime: #{e.}") end syscmd("umount \"#{host_path}\"") Dir.rmdir(host_path) end |
#prune_empty_child_directories(dir) ⇒ Object (protected)
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/osctld/mount/shared_dir.rb', line 149 def prune_empty_child_directories(dir) dir.children.each do |child| next unless File.lstat(child).directory? child.rmdir log(:warn, ct, "Removed stale empty shared mount directory #{child}") rescue Errno::ENOENT, Errno::ENOTEMPTY next end end |
#readme_path ⇒ Object (protected)
160 161 162 |
# File 'lib/osctld/mount/shared_dir.rb', line 160 def readme_path File.join(path, 'README.txt') end |
#remove ⇒ Object
Remove the shared mount directory from the host
29 30 31 32 33 34 35 |
# File 'lib/osctld/mount/shared_dir.rb', line 29 def remove dir = Pathname.new(path) unmount(dir) FileUtils.rm_f(readme_path) prune_empty_child_directories(dir) if dir.exist? dir.rmdir if dir.exist? end |
#unmount(dir) ⇒ Object (protected)
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/osctld/mount/shared_dir.rb', line 135 def unmount(dir) return unless dir.exist? cmd = "umount -f \"#{dir}\"" ret = syscmd(cmd, valid_rcs: :all) return if ret.success? return if ret.output.include?('not mounted') return if ret.output.include?('not found') return if ret.output.include?('no mount point specified') raise SystemCommandFailed.new(cmd, ret.exitstatus, ret.output) end |