Class: OsCtld::ContainerControl::Commands::Mount::Runner

Inherits:
Runner
  • Object
show all
Defined in:
lib/osctld/container_control/commands/mount.rb

Constant Summary

Constants inherited from Runner

Runner::LXC_ATTACH_FLAGS

Instance Attribute Summary

Attributes inherited from Runner

#ctid, #log_file, #lxc_home, #network_socket, #pool, #stderr, #stdin, #stdout, #user_home

Instance Method Summary collapse

Methods inherited from Runner

#error, #exitstatus, #initialize, #lxc_attach_command, #lxc_attach_wait, #lxc_attach_wait_for_exit, #lxc_ct, #ok, #reap_interrupted_lxc_attach, #setup_exec_env, #setup_exec_run_env, #system_path, #wait_for_lxc_attachable, #wait_for_lxc_stopped, #wait_for_path, #wait_for_process, #wait_status_exitstatus

Constructor Details

This class inherits a constructor from OsCtld::ContainerControl::Runner

Instance Method Details

#authenticate_current_init!(identity, cgroup_path) ⇒ Object (protected)



123
124
125
126
127
128
129
130
131
# File 'lib/osctld/container_control/commands/mount.rb', line 123

def authenticate_current_init!(identity, cgroup_path)
  identity.authenticate!(cgroup_path:)
  current_pid = lxc_ct.init_pid
  unless lxc_ct.running? && current_pid == identity.pid
    raise Errno::ESTALE, 'container init no longer belongs to the current LXC run'
  end

  identity.authenticate!(cgroup_path:)
end

#create_mountpoint_as(dst, uid:, gid:) ⇒ Object (protected)



143
144
145
146
147
148
149
150
# File 'lib/osctld/container_control/commands/mount.rb', line 143

def create_mountpoint_as(dst, uid:, gid:)
  SwitchUser.switch_to_system('', uid, gid, '/')
  FileUtils.mkdir_p(dst)
  true
rescue StandardError => e
  warn("mkdir -p #{dst.inspect} failed: #{e.class}: #{e.message}")
  false
end

#enter_container_mountns(identity, cgroup_path) ⇒ Object (protected)

Raises:

  • (Errno::ESRCH)


112
113
114
115
116
117
118
119
120
121
# File 'lib/osctld/container_control/commands/mount.rb', line 112

def enter_container_mountns(identity, cgroup_path)
  authenticate_current_init!(identity, cgroup_path)
  sys = OsCtl::Lib::Sys.new
  raise Errno::ESRCH, 'container init process exited' unless sys.pidfd_alive?(identity.pidfd)

  sys.setns_io(identity.namespace(:mnt), OsCtl::Lib::Sys::CLONE_NEWNS)
  sys.fchdir_io(identity.root_dir)
  sys.chroot('.')
  Dir.chdir('/')
end

#execute(opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :shared_dir (String) —

    path to the host-shared directory

  • :src (String) —

    directory inside :shared_dir to relocate

  • :dst (String) —

    target mountpoint

  • :init_identity (ProcessIdentity) —

    exact current-run init

  • :cgroup_path (String) —

    expected container cgroup subtree

  • :root_host_uid (Integer) —

    mapped container root UID

  • :root_host_gid (Integer) —

    mapped container root GID



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/osctld/container_control/commands/mount.rb', line 84

def execute(opts)
  src = File.join(opts[:shared_dir], opts[:src])
  identity = opts.fetch(:init_identity)
  cgroup_path = opts.fetch(:cgroup_path)

  enter_container_mountns(identity, cgroup_path)
  prepare_mountpoint(
    opts[:dst],
    uid: opts.fetch(:root_host_uid),
    gid: opts.fetch(:root_host_gid)
  )

  if !wait_for_path(opts[:shared_dir])
    return error("Shared dir not found at: #{opts[:shared_dir]}")

  elsif !wait_for_path(src)
    return error("Source directory not found at: #{src}")
  end

  identity.authenticate!(cgroup_path:)
  relocate_mount(src, opts[:dst], create: false)
  ok
rescue StandardError => e
  error("Exception (#{e.class}): #{e.message}")
end

#prepare_mountpoint(dst, uid:, gid:) ⇒ Object (protected)



133
134
135
136
137
138
139
140
141
# File 'lib/osctld/container_control/commands/mount.rb', line 133

def prepare_mountpoint(dst, uid:, gid:)
  pid = Process.fork do
    exit!(create_mountpoint_as(dst, uid:, gid:))
  end
  _, status = Process.wait2(pid)
  return if status.success?

  raise "mkdir -p #{dst.inspect} exited with #{status.exitstatus || 'signal'}"
end

#relocate_mount(src, dst, create: true) ⇒ Object (protected)



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

def relocate_mount(src, dst, create: true)
  FileUtils.mkpath(dst) if create
  sys = OsCtl::Lib::Sys.new
  sys.move_mount(src, dst)
  sys.make_private(dst)
end