Class: OsCtld::Commands::Container::Start

Inherits:
Logged
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, Utils::SwitchUser
Defined in:
lib/osctld/commands/container/start.rb

Instance Attribute Summary

Attributes inherited from Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

Methods included from Utils::SwitchUser

#ct_attach, #ct_syscmd

Methods inherited from Logged

#base_execute

Methods inherited from Base

#base_execute, #call_cmd, #call_cmd!, 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::Commands::Base

Instance Method Details

#execute(ct) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/osctld/commands/container/start.rb', line 16

def execute(ct)
  return start_queued(ct) if opts[:queue]

  event_queue = nil

  manipulate(ct) do
    event_queue = Eventd.subscribe
    ret = start_now(ct)

    # Exit if we don't need to wait
    if ret != :wait
      return ret

    elsif opts[:wait] === false
      return ok
    end

    # Wait for the container to enter state `running`
    progress('Waiting for the container to start')
    started = wait_for_ct(event_queue, ct)
    Eventd.unsubscribe(event_queue)

    if started
      # Access `/proc/stat` and `/proc/loadavg` within the container, so that
      # LXCFS starts tracking it immediately.
      begin
        ct_syscmd(ct, 'cat /proc/stat', valid_rcs: :all)
        ct_syscmd(ct, 'cat /proc/loadavg', valid_rcs: :all)
      rescue OsCtld::SystemCommandFailed => e
        log(:warn, ct, "Failed to initiate lxcfs accounting: #{e.message}")
      end

      ok

    else
      error('container failed to start')
    end
  end
end

#findObject



11
12
13
14
# File 'lib/osctld/commands/container/start.rb', line 11

def find
  ct = DB::Containers.find(opts[:id], opts[:pool])
  ct || error!('container not found')
end

#start_now(ct) ⇒ Object (protected)



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/osctld/commands/container/start.rb', line 84

def start_now(ct)
  error!('start not available') unless ct.can_start?
  return ok if ct.running? && !opts[:force]

  # Remove any left-over temporary mounts
  ct.mounts.prune

  # Reset log file
  File.open(ct.log_path, 'w').close
  File.chmod(0660, ct.log_path)
  File.chown(0, ct.user.ugid, ct.log_path)

  # Console dir
  console_dir = File.join(ct.pool.console_dir, ct.id)
  Dir.mkdir(console_dir) unless Dir.exist?(console_dir)
  File.chown(ct.user.ugid, 0, console_dir)
  File.chmod(0700, console_dir)

  # Remove stray sockets
  sock_path = Console.socket_path(ct)
  if File.exist?(sock_path)
    log(:info, ct, "Removing leftover tty0 socket at #{sock_path}")

    begin
      File.unlink(sock_path)
    rescue Errno::ENOENT
      # Continue if the socket was already deleted
    end
  end

  # Containers are started through two wrappers: pty-wrapper and osctld-ct-start.
  #
  # pty-wrapper is used to allocate a pty and provide access to input/output
  # of the started process.
  #
  # osctld-ct-start is used to reset oom_score_adj to zero, since pty-wrapper
  # have its own oom_score_adj set to -1000 to ensure the OOM killer will
  # not target it. oom_score_adj is inherited on fork, so the process
  # pty-wrapper starts has it set to -1000 as well. Because the process
  # is already run as an unprivileged user, changing oom_score_adj will leave
  # oom_score_adj_min untouched. That would let all container users to disable
  # OOM killer altogether, so osctld-ct-start pings back to osctld, which is
  # running with CAP_SYS_RESOURCE and can set both obj_score_adj and
  # obj_score_adj_min to zero. When it's done, osctld-ct-start execs to
  # lxc-start.
  cmd = [
    OsCtld.bin('osctld-ct-wrapper'),
    "#{ct.pool.name}:#{ct.id}",
    Console.socket_path(ct),
    OsCtld.bin('osctld-ct-start'),
    ct.pool.name,
    ct.id,
    'lxc-start',
    '-P', ct.lxc_home,
    '-n', ct.id,
    '-o', ct.log_path,
    '-l', opts[:debug] ? 'DEBUG' : 'ERROR',
    '-F'
  ]

  r, w = IO.pipe

  progress('Starting container')
  pid = SwitchUser.fork_and_switch_to(
    ct.user.sysusername,
    ct.user.ugid,
    ct.user.homedir,
    ct.cgroup_path,
    prlimits: ct.prlimits.export,
    oom_score_adj: -1000,
    keep_fds: [w],
  ) do
    # Closed by SwitchUser.fork_and_switch_to
    # r.close

    wrapper_pid = Process.spawn(
      *cmd,
      pgroup: true, in: :close, out: :close, err: :close
    )

    w.puts(wrapper_pid.to_s)
  end

  w.close
  wrapper_pid = r.readline.strip.to_i
  r.close

  progress('Connecting console')

  begin
    Console.connect_tty0(ct, wrapper_pid)
  rescue Errno::ENOENT
    log(:warn, ct, "Unable to connect to tty0")
  end

  Process.wait(pid)
  :wait
end

#start_queued(ct) ⇒ Object (protected)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/osctld/commands/container/start.rb', line 57

def start_queued(ct)
  progress('Joining the queue')

  if opts[:wait] === false
    ct.pool.autostart_plan.enqueue(
      ct,
      priority: opts[:priority],
      start_opts: opts,
    )
    return ok
  end

  ret = ct.pool.autostart_plan.start_ct(
    ct,
    priority: opts[:priority],
    start_opts: opts,
    client_handler: client_handler,
  )

  if ret.nil?
    ok('Timed out')

  else
    ret
  end
end

#wait_for_ct(event_queue, ct) ⇒ Object (protected)

Wait for the container to start or fail



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/osctld/commands/container/start.rb', line 184

def wait_for_ct(event_queue, ct)
  # Sequence of events that lead to the container being started.
  # We're accepting even `stopping` and `stopped`, since when the container
  # is being restarted, these events may be received and should not cause
  # this method to exit.
  sequence = %i(stopping stopped starting running)
  last_i = nil
  wait_until = Time.now + (opts[:wait] || 60)

  loop do
    timeout = wait_until - Time.now
    return false if timeout < 0

    event = event_queue.pop(timeout: timeout)
    return false if event.nil?

    # Ignore irrelevant events
    next if event.type != :state \
            || event.opts[:pool] != ct.pool.name \
            || event.opts[:id] != ct.id

    state = event.opts[:state]
    cur_i = sequence.index(state)

    return false if cur_i.nil? || (last_i && cur_i < last_i)
    return true if state == sequence.last

    last_i = cur_i
  end
end