Class: OsCtld::Mount::SharedDir

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::SwitchUser

#ct_attach, #ct_syscmd

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

#ctObject (readonly, protected)

Returns the value of attribute ct.



81
82
83
# File 'lib/osctld/mount/shared_dir.rb', line 81

def ct
  @ct
end

Instance Method Details

#createObject

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_readmeObject (protected)



87
88
89
90
91
92
93
94
95
96
# File 'lib/osctld/mount/shared_dir.rb', line 87

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



73
74
75
76
77
# File 'lib/osctld/mount/shared_dir.rb', line 73

def dup(new_ct)
  ret = super()
  ret.instance_variable_set('@ct', new_ct)
  ret
end

#mountpointString

Mountpoint relative to the container’s rootfs

Returns:

  • (String)


69
70
71
# File 'lib/osctld/mount/shared_dir.rb', line 69

def mountpoint
  'dev/.osctl-mount-helper'
end

#pathString

Returns:

  • (String)


63
64
65
# File 'lib/osctld/mount/shared_dir.rb', line 63

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

Parameters:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/osctld/mount/shared_dir.rb', line 38

def propagate(mnt)
  tmp = Digest::SHA2.hexdigest(mnt.mountpoint)

  # Bind-mount the new mount into the shared directory
  host_path = File.join(path, tmp)
  Dir.mkdir(host_path)
  syscmd("mount --bind \"#{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: tmp,
      dst: File.join('/', mnt.mountpoint)
    )
  rescue ContainerControl::Error => e
    log(:warn, ct, "Failed to mount #{mnt.mountpoint} at runtime: #{e.message}")
  end

  syscmd("umount \"#{host_path}\"")
  Dir.rmdir(host_path)
end

#readme_pathObject (protected)



83
84
85
# File 'lib/osctld/mount/shared_dir.rb', line 83

def readme_path
  File.join(path, 'README.txt')
end

#removeObject

Remove the shared mount directory from the host



29
30
31
32
33
34
# File 'lib/osctld/mount/shared_dir.rb', line 29

def remove
  dir = Pathname.new(path)
  syscmd("umount -f \"#{dir}\"", valid_rcs: [32]) # 32 = not mounted
  FileUtils.rm_f(readme_path)
  dir.rmdir if dir.exist?
end