Class: OsCtld::UserControl::Commands::CtOnStart

Inherits:
Base show all
Includes:
OsCtl::Lib::Utils::Log
Defined in:
lib/osctld/user_control/commands/ct_on_start.rb

Constant Summary collapse

CGROUP_DELEGATE_BUSY_RETRIES =
100
CGROUP_DELEGATE_BUSY_RETRY_DELAY =
0.1
CGROUP_DELEGATE_RETRYABLE_ERRORS =
[
  Errno::EBUSY,
  Errno::EOPNOTSUPP,
  Errno::EINVAL
].freeze

Instance Attribute Summary

Attributes inherited from Base

#user

Attributes inherited from Commands::Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

Methods inherited from Base

handle, #initialize, #owns_ct?, run

Methods inherited from Commands::Base

#base_execute, #call_cmd, #call_cmd!, #error, #error!, handle, #handled, #indirect?, #initialize, #manipulate, #manipulation_holder, #ok, #progress, #request_stop, run, run!

Constructor Details

This class inherits a constructor from OsCtld::UserControl::Commands::Base

Instance Method Details

#delegate_start_cgroup_ancestors(ct, root_cgroup) ⇒ Object (protected)



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/osctld/user_control/commands/ct_on_start.rb', line 75

def delegate_start_cgroup_ancestors(ct, root_cgroup)
  cgroup_fs = File.expand_path(CGroup.fs)
  path = File.dirname(File.expand_path(root_cgroup))
  ancestors = []

  loop do
    break if path == cgroup_fs || path == '/'
    break unless path.start_with?("#{cgroup_fs}/")

    ancestors << path
    path = File.dirname(path)
  end

  ancestors.reverse_each do |ancestor|
    delegate_start_cgroup_controllers(ct, ancestor)
  end
end

#delegate_start_cgroup_controllers(ct, root_cgroup) ⇒ Object (protected)



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/osctld/user_control/commands/ct_on_start.rb', line 93

def delegate_start_cgroup_controllers(ct, root_cgroup)
  attempts = 0

  begin
    CGroup.delegate_available_controllers(root_cgroup) do
      yield if block_given?
    end
  rescue *CGROUP_DELEGATE_RETRYABLE_ERRORS => e
    if attempts < CGROUP_DELEGATE_BUSY_RETRIES
      attempts += 1
      sleep(CGROUP_DELEGATE_BUSY_RETRY_DELAY)
      retry
    end

    log(
      :warn,
      ct,
      "Unable to delegate cgroup v2 controllers on #{root_cgroup}: " \
      "#{e.message} after #{CGROUP_DELEGATE_BUSY_RETRIES} retries over " \
      "#{CGROUP_DELEGATE_BUSY_RETRIES * CGROUP_DELEGATE_BUSY_RETRY_DELAY} seconds; " \
      'nested cgroup managers may be unavailable'
    )
  end
end

#delegate_start_cgroups(ct) ⇒ Object (protected)



36
37
38
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
67
68
69
70
71
72
73
# File 'lib/osctld/user_control/commands/ct_on_start.rb', line 36

def delegate_start_cgroups(ct)
  return unless CGroup.v2?

  uid = ct.user.ugid
  gid = ct.root_host_gid

  root_cgroup = CGroup.abs_cgroup_path(nil, ct.cgroup_path)

  delegate_start_cgroup_ancestors(ct, root_cgroup)

  CGroup.chown_delegated(
    root_cgroup,
    uid:,
    gid:
  )

  # The start path prepares this cgroup and the wrapper moves itself into
  # it, so v2 domain controllers cannot be enabled there initially. Once
  # start-host runs, LXC has moved the monitor/payload into child cgroups
  # and the cgroup namespace root can be delegated to nested managers such
  # as Docker.
  delegate_start_cgroup_controllers(ct, root_cgroup) do
    ensure_start_cgroup_empty!(ct)
  end

  # start-host can run before LXC has created the payload cgroup. If the
  # payload is already visible, make it writable as well; otherwise the
  # parent delegation above is the CT-level contract systemd can rely on.
  begin
    CGroup.chown_delegated(
      CGroup.abs_cgroup_path(nil, ct.payload_cgroup_path),
      uid:,
      gid:
    )
  rescue Errno::ENOENT
    nil
  end
end

#ensure_start_cgroup_empty!(ct) ⇒ Object (protected)

Raises:

  • (Errno::EBUSY)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/osctld/user_control/commands/ct_on_start.rb', line 118

def ensure_start_cgroup_empty!(ct)
  pids = CGroup.get_cgroup_pids(nil, ct.cgroup_path)
  return if pids.empty?

  # LXC moves its monitor itself before it creates the payload, creates the
  # payload with CLONE_INTO_CGROUP when available, and runs start-host from
  # the already placed monitor. A process left here is therefore an LXC
  # placement failure, not an identity osctld can safely repair: the cgroup
  # interface accepts only a numeric PID and cannot bind a write to our
  # held pidfd. Retry while LXC is settling, but never move an unidentified
  # or possibly reused PID.
  raise Errno::EBUSY,
        "unexpected processes remain in the container cgroup: #{pids.join(', ')}"
end

#execute ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/osctld/user_control/commands/ct_on_start.rb', line 18

def execute
  ct = DB::Containers.find(opts[:id], opts[:pool])
  return error('container not found') unless ct
  return error('access denied') unless owns_ct?(ct)

  delegate_start_cgroups(ct)

  # Configure the system
  DistConfig.run(ct.run_conf, :start)

  Hook.run(ct, :on_start)
  ok
rescue HookFailed => e
  error(e.message)
end