Class: OsCtld::Container::RunConfiguration

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::File, OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, Lockable
Defined in:
lib/osctld/container/run_configuration.rb

Defined Under Namespace

Classes: InitLease, LifecycleError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lockable

#exclusively, included, #inclusively, #init_lock, #lock, #unlock

Constructor Details

#initialize(ct, load_conf: true) ⇒ RunConfiguration

Returns a new instance of RunConfiguration.

Parameters:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/osctld/container/run_configuration.rb', line 68

def initialize(ct, load_conf: true)
  init_lock
  @ct = ct
  @cpu_package = nil
  @init_pid = nil
  @nfs_cancellation = Container::NfsCancellation.new(ct)
  @init_identity = nil
  @init_lease_mutex = Mutex.new
  @init_lease_cond = ConditionVariable.new
  @init_lease_count = 0
  @retiring = false
  @destroyed = false
  @aborted = false
  @do_reboot = false
  @exit_promise = Promise.new
  @exit_handling_started = false
  @dist_network_configured = false
  self.load_conf(from_file: load_conf)
end

Instance Attribute Details

#aborted=(value) ⇒ Object (writeonly)

Sets the attribute aborted

Parameters:

  • value —

    the value to set the attribute aborted to.



338
339
340
# File 'lib/osctld/container/run_configuration.rb', line 338

def aborted=(value)
  @aborted = value
end

#arch ⇒ Object (readonly)

Returns the value of attribute arch



63
64
65
# File 'lib/osctld/container/run_configuration.rb', line 63

def arch
  @arch
end

#cpu_package ⇒ Object

Returns the value of attribute cpu_package



64
65
66
# File 'lib/osctld/container/run_configuration.rb', line 64

def cpu_package
  @cpu_package
end

#ct ⇒ Container (readonly)

Returns:



59
60
61
# File 'lib/osctld/container/run_configuration.rb', line 59

def ct
  @ct
end

#dataset ⇒ Object (readonly)

Returns the value of attribute dataset



63
64
65
# File 'lib/osctld/container/run_configuration.rb', line 63

def dataset
  @dataset
end

#dist_network_configured ⇒ Object

Returns the value of attribute dist_network_configured



64
65
66
# File 'lib/osctld/container/run_configuration.rb', line 64

def dist_network_configured
  @dist_network_configured
end

#distribution ⇒ Object (readonly)

Returns the value of attribute distribution



63
64
65
# File 'lib/osctld/container/run_configuration.rb', line 63

def distribution
  @distribution
end

#init_pid ⇒ Object

Returns the value of attribute init_pid



65
66
67
# File 'lib/osctld/container/run_configuration.rb', line 65

def init_pid
  @init_pid
end

#mounted ⇒ Object (protected)

Returns the value of attribute mounted



652
653
654
# File 'lib/osctld/container/run_configuration.rb', line 652

def mounted
  @mounted
end

#nfs_cancellation ⇒ Object (readonly)

Returns the value of attribute nfs_cancellation.



61
62
63
# File 'lib/osctld/container/run_configuration.rb', line 61

def nfs_cancellation
  @nfs_cancellation
end

#run_id ⇒ Container::RunId (readonly)

Returns:



56
57
58
# File 'lib/osctld/container/run_configuration.rb', line 56

def run_id
  @run_id
end

#variant ⇒ Object (readonly)

Returns the value of attribute variant



63
64
65
# File 'lib/osctld/container/run_configuration.rb', line 63

def variant
  @variant
end

#vendor ⇒ Object (readonly)

Returns the value of attribute vendor



63
64
65
# File 'lib/osctld/container/run_configuration.rb', line 63

def vendor
  @vendor
end

#version ⇒ Object (readonly)

Returns the value of attribute version



63
64
65
# File 'lib/osctld/container/run_configuration.rb', line 63

def version
  @version
end

Class Method Details

.load(ct) ⇒ Object

Parameters:



46
47
48
49
50
51
52
53
# File 'lib/osctld/container/run_configuration.rb', line 46

def self.load(ct)
  ctrc = new(ct, load_conf: false)

  return unless ctrc.exist?

  ctrc.load_conf
  ctrc
end

Instance Method Details

#aborted? ⇒ Boolean

Returns:

  • (Boolean)


340
341
342
# File 'lib/osctld/container/run_configuration.rb', line 340

def aborted?
  @aborted
end

#acquire_init_lease ⇒ Object

Acquire a descriptor-authenticated identity lease. Ordinary run locks are held only while the descriptors are duplicated. Retirement and identity replacement then coordinate through the lease condition instead of holding shared state locks across external work.



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/osctld/container/run_configuration.rb', line 302

def acquire_init_lease
  identity = nil
  leased = false

  @init_lease_mutex.synchronize do
    raise LifecycleError, 'container run is retiring' if @retiring || @destroyed

    identity = inclusively do
      @init_identity&.duplicate
    end
    return unless identity

    @init_lease_count += 1
    leased = true
  end

  InitLease.new(self, identity)
rescue StandardError
  identity&.close
  release_init_lease if leased
  raise
end

#assets(add) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/osctld/container/run_configuration.rb', line 88

def assets(add)
  add.file(
    file_path,
    desc: 'Container runtime configuration',
    user: 0,
    group: 0,
    mode: 0o400,
    optional: true
  )
end

#begin_retirement ⇒ Object

Prevent new init leases without waiting for current holders. This is called while the container still points at this run, making detachment atomic with respect to lease acquisition.



328
329
330
331
332
333
334
335
336
# File 'lib/osctld/container/run_configuration.rb', line 328

def begin_retirement
  @init_lease_mutex.synchronize do
    return false if @retiring

    @retiring = true
    @init_lease_cond.broadcast
    true
  end
end

#boot_from(dataset:, distribution:, version:, arch:, vendor:, variant:, destroy_dataset_on_stop: false) ⇒ Object

Set custom boot dataset



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/osctld/container/run_configuration.rb', line 109

def boot_from(dataset:, distribution:, version:, arch:, vendor:, variant:, destroy_dataset_on_stop: false)
  exclusively do
    @dataset = dataset
    @distribution = distribution
    @version = version
    @arch = arch
    @vendor = vendor
    @variant = variant
    @destroy_dataset_on_stop = destroy_dataset_on_stop
  end
end

#claim_exit_handling ⇒ Object



357
358
359
360
361
362
363
# File 'lib/osctld/container/run_configuration.rb', line 357

def claim_exit_handling
  exclusively do
    next false if @exit_handling_started

    @exit_handling_started = true
  end
end

#clear_dead_init_identity ⇒ Object

A transient LXC run can stop without retiring its runtime configuration. Discard only a proven-dead pinned identity; a stopped monitor alone is insufficient proof, since an orphaned init may still be alive. Existing leases own duplicate descriptors and remain valid until their release.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/osctld/container/run_configuration.rb', line 283

def clear_dead_init_identity
  identity = nil
  @init_lease_mutex.synchronize do
    exclusively do
      return false if @init_identity.nil? || @init_identity.alive?

      identity = @init_identity
      @init_identity = nil
      @init_pid = nil
    end
  end
  identity.close
  true
end

#close_init_identity ⇒ Object (protected)



640
641
642
643
644
645
646
647
648
649
650
# File 'lib/osctld/container/run_configuration.rb', line 640

def close_init_identity
  identity = nil

  exclusively do
    identity = @init_identity
    @init_identity = nil
    @init_pid = nil
  end

  identity&.close
end

#destroy ⇒ Object



453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/osctld/container/run_configuration.rb', line 453

def destroy
  begin_retirement
  wait_for_init_leases
  File.unlink(file_path)
rescue Errno::ENOENT
  # ignore
ensure
  begin
    nfs_cancellation.close
  ensure
    close_init_identity
  end
end

#destroy_dataset_on_stop? ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/osctld/container/run_configuration.rb', line 133

def destroy_dataset_on_stop?
  inclusively { @destroy_dataset_on_stop }
end

#dir ⇒ String

Countainer dataset mountpoint

Returns:

  • (String)


139
140
141
# File 'lib/osctld/container/run_configuration.rb', line 139

def dir
  dataset.mountpoint
end

#dir_path ⇒ Object (protected)



654
655
656
# File 'lib/osctld/container/run_configuration.rb', line 654

def dir_path
  File.join(ct.pool.ct_dir, ct.id)
end

#dist_configure_network? ⇒ Boolean

Returns:

  • (Boolean)


369
370
371
372
373
# File 'lib/osctld/container/run_configuration.rb', line 369

def dist_configure_network?
  inclusively do
    !dist_network_configured && can_dist_configure_network?
  end
end

#dump ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/osctld/container/run_configuration.rb', line 379

def dump
  inclusively do
    {
      'id' => run_id.dump,
      'dataset' => dataset.to_s,
      'distribution' => distribution,
      'version' => version,
      'arch' => arch,
      'vendor' => vendor,
      'variant' => variant,
      'cpu_package' => cpu_package,
      'destroy_dataset_on_stop' => destroy_dataset_on_stop?
    }
  end
end

#exist? ⇒ Boolean

Returns:

  • (Boolean)


375
376
377
# File 'lib/osctld/container/run_configuration.rb', line 375

def exist?
  File.exist?(file_path)
end

#file_path ⇒ Object (protected)



658
659
660
# File 'lib/osctld/container/run_configuration.rb', line 658

def file_path
  File.join(dir_path, 'config.yml')
end

#fulfil_exit ⇒ Object



365
366
367
# File 'lib/osctld/container/run_configuration.rb', line 365

def fulfil_exit
  @exit_promise.fulfil
end

#get_exit_promise ⇒ Object



353
354
355
# File 'lib/osctld/container/run_configuration.rb', line 353

def get_exit_promise
  @exit_promise.add
end

#impermanent_root_owner(records) ⇒ Object (protected)



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/osctld/container/run_configuration.rb', line 498

def impermanent_root_owner(records)
  matching = records.select { |row| row['run_id'] == run_id.dump }
  unless matching.one?
    raise ConfigError, 'temporary root requires one existing GC owner for the saved run'
  end

  candidate = matching.first['dataset']
  suffix = dataset.name == ct.dataset.name ? 'impermanence' : '(?:impermanence|boot)'
  pattern = /\A#{Regexp.escape(ct.dataset.name)}\.#{suffix}-[0-9a-f]{6}\z/
  unless candidate.is_a?(String) && candidate.match?(pattern)
    raise ConfigError, 'temporary root has conflicting or foreign GC ownership'
  end

  overlapping = records.count do |row|
    other = row['dataset']
    other.is_a?(String) &&
      (other == candidate || other.start_with?("#{candidate}/") || candidate.start_with?("#{other}/"))
  end
  unless overlapping == 1
    raise ConfigError, 'temporary root has conflicting or foreign GC ownership'
  end

  candidate
end

#init_birth(identity) ⇒ Object (protected)

Raises:



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/osctld/container/run_configuration.rb', line 523

def init_birth(identity)
  namespace_pids = identity.read_proc_file('status').lines.grep(/\ANSpid:\s/)
  pids = namespace_pids.one? ? namespace_pids.first.split.drop(1) : []
  unless pids.length >= 2 && pids.first == identity.pid.to_s && pids.last == '1'
    raise ConfigError, 'live process is not the container namespace init'
  end

  stat = identity.read_proc_file('stat')
  unless stat.start_with?("#{identity.pid} (") && stat.include?(')')
    raise ConfigError, 'invalid init process birth identity'
  end

  birth = stat.rpartition(')').last.split.fetch(19)
  raise ConfigError, 'invalid init process birth identity' unless birth.match?(/\A[0-9]+\z/) && birth.to_i > 0

  birth
end

#init_root_mount(identity, candidate) ⇒ Object (protected)

Raises:



554
555
556
557
558
559
560
561
# File 'lib/osctld/container/run_configuration.rb', line 554

def init_root_mount(identity, candidate)
  roots = root_mount_entries(identity.read_proc_file('mountinfo')).select { |row| row[2] == '/' }
  stat = identity.root_dir.stat
  expected = ["#{stat.dev_major}:#{stat.dev_minor}", '/private', '/', 'zfs', candidate]
  raise ConfigError, 'init root is not the selected dataset private root' unless roots == [expected]

  roots.first
end

#install_init_identity ⇒ Object (protected)



595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/osctld/container/run_configuration.rb', line 595

def install_init_identity
  identity = nil
  old_identity = nil

  @init_lease_mutex.synchronize do
    raise LifecycleError, 'container run is retiring' if @retiring || @destroyed
  end

  identity = yield

  @init_lease_mutex.synchronize do
    @init_lease_cond.wait(@init_lease_mutex) while @init_lease_count > 0 && !@retiring

    raise LifecycleError, 'container run is retiring' if @retiring || @destroyed

    exclusively do
      old_identity = @init_identity
      @init_identity = identity
      @init_pid = identity.pid
      identity = nil
    end
  end

  @init_pid
ensure
  old_identity&.close
  identity&.close
end

#load_conf(from_file: true) ⇒ Object



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/osctld/container/run_configuration.rb', line 395

def load_conf(from_file: true)
  cfg =
    if from_file && File.exist?(file_path)
      @loaded_input = runtime_config_input
      @loaded_config = OsCtl::Lib::ConfigFile.load_yaml(@loaded_input.last)
      unless @loaded_config.is_a?(Hash)
        raise ConfigError, 'runtime configuration must be a mapping'
      end
      if @loaded_config.has_key?('id') && !@loaded_config['id'].is_a?(Hash)
        raise ConfigError, 'runtime configuration has an invalid run ID'
      end

      @loaded_config
    else
      {}
    end

  @run_id =
    if cfg.has_key?('id')
      Container::RunId.load(cfg['id'])
    else
      Container::RunId.new(pool_name: pool.name, container_id: id)
    end
  @dataset =
    if cfg['dataset']
      OsCtl::Lib::Zfs::Dataset.new(cfg['dataset'], base: cfg['dataset'])
    else
      ct.dataset
    end
  @distribution = cfg['distribution'] || ct.distribution
  @version = cfg['version'] || ct.version
  @arch = cfg['arch'] || ct.arch
  @vendor = cfg['vendor'] || ct.vendor
  @variant = cfg['variant'] || ct.variant
  @cpu_package = cfg['cpu_package']
  @destroy_dataset_on_stop =
    if cfg.has_key?('destroy_dataset_on_stop')
      cfg['destroy_dataset_on_stop']
    else
      false
    end
  nil
rescue Psych::Exception, KeyError, ArgumentError, TypeError => e
  raise ConfigError.new('invalid runtime configuration', e)
end

#mount(force: false) ⇒ Object

Mount the container's dataset

Parameters:

  • force (Boolean) (defaults to: false) —

    ensure the datasets are mounted even if osctld already mounted them



156
157
158
159
160
161
162
163
# File 'lib/osctld/container/run_configuration.rb', line 156

def mount(force: false)
  return if !force && mounted

  ct.mount(force: force)
  dataset.mount(recursive: true) if ct.dataset.name != dataset.name

  self.mounted = true
end

#mounted?(force: false) ⇒ Boolean

Check if the container's dataset is mounted

Parameters:

  • force (Boolean) (defaults to: false) —

    check if the dataset is mounted even if osctld already mounted it

Returns:

  • (Boolean)


168
169
170
171
172
173
174
# File 'lib/osctld/container/run_configuration.rb', line 168

def mounted?(force: false)
  if force || mounted.nil?
    self.mounted = dataset.mounted?(recursive: true)
  else
    mounted
  end
end

#reboot? ⇒ Boolean

Returns:

  • (Boolean)


349
350
351
# File 'lib/osctld/container/run_configuration.rb', line 349

def reboot?
  @do_reboot
end

#recover_impermanent_root!(pid, publish:) ⇒ Object

Called only for an unpublished, running impermanent CT during pool import. Prove the saved base root, or the temporary root and its existing GC owner. Preparation may use the selected root, but persistence waits for all final checks. There are no monitor callbacks or collector worker during this call.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/osctld/container/run_configuration.rb', line 197

def recover_impermanent_root!(pid, publish:)
  original_dataset = @dataset
  original_cleanup = @destroy_dataset_on_stop
  identity = nil
  installed = false
  completed = false

  validate_saved_run!
  base_selection = dataset.name == ct.dataset.name
  if base_selection && destroy_dataset_on_stop?
    raise ConfigError, 'stale base selection has contradictory cleanup state'
  end

  ownership = pool.garbage_collector.container_run_dataset_ownership(allow_missing: base_selection)
  records = ownership.first
  ordinary_base = base_selection && records.none? { |row| row['run_id'] == run_id.dump }
  if ordinary_base
    conflicting = records.any? do |row|
      other = row['dataset']
      other.is_a?(String) &&
        (other == dataset.name || other.start_with?("#{dataset.name}/") || dataset.name.start_with?("#{other}/"))
    end
    raise ConfigError, 'base root has conflicting GC ownership' if conflicting

    candidate = dataset.name
  else
    candidate = impermanent_root_owner(records)
  end
  repair = base_selection && !ordinary_base

  unless ordinary_base || repair || (dataset.name == candidate && destroy_dataset_on_stop?)
    raise ConfigError, 'saved temporary root contradicts GC ownership'
  end

  identity = ProcessIdentity.new(pid, namespaces: [:mnt], root: true)
  identity.authenticate!(cgroup_path: ct.payload_cgroup_path)
  birth = init_birth(identity)
  mount = init_root_mount(identity, candidate)
  zfs_identity = root_dataset_identity(candidate, identity, temporary: !ordinary_base)

  unless ordinary_base
    @dataset = OsCtl::Lib::Zfs::Dataset.new(candidate, base: candidate)
    @destroy_dataset_on_stop = true
  end
  yield if block_given?

  # Installation can wait for leases. Do it before the last authentication,
  # while the standalone identity is still held; never lease this run here.
  retain_init_identity(identity)
  installed = true
  unless root_dataset_identity(candidate, identity, temporary: !ordinary_base) == zfs_identity &&
         init_root_mount(identity, candidate) == mount && init_birth(identity) == birth
    raise ConfigError, 'live root changed during import'
  end
  unless runtime_config_input == @loaded_input &&
         pool.garbage_collector.container_run_dataset_ownership(allow_missing: ordinary_base) == ownership
    raise ConfigError, 'saved run or GC ownership changed during import'
  end

  identity.authenticate!(cgroup_path: ct.payload_cgroup_path)
  if repair
    regenerate_file(file_path, 0o400) do |new|
      new.write(OsCtl::Lib::ConfigFile.dump_yaml(@loaded_config.merge(
                                                   'dataset' => candidate,
                                                   'destroy_dataset_on_stop' => true
                                                 )))
    end
  end
  publish.call
  completed = true
  nil
rescue SystemCallError, IOError, ArgumentError, KeyError, Psych::Exception, SystemCommandFailed => e
  raise ConfigError.new('unable to authenticate impermanent live root', e)
ensure
  unless completed
    @dataset = original_dataset
    @destroy_dataset_on_stop = original_cleanup
    close_init_identity if installed
  end
  identity&.close
end

#release_init_lease ⇒ Object (protected)



631
632
633
634
635
636
637
638
# File 'lib/osctld/container/run_configuration.rb', line 631

def release_init_lease
  @init_lease_mutex.synchronize do
    raise LifecycleError, 'init identity lease underflow' unless @init_lease_count > 0

    @init_lease_count -= 1
    @init_lease_cond.broadcast if @init_lease_count == 0
  end
end

#request_reboot ⇒ Object

After the current container run stops, start it again



345
346
347
# File 'lib/osctld/container/run_configuration.rb', line 345

def request_reboot
  @do_reboot = true
end

#retain_init_identity(identity) ⇒ Object

Import retains its authenticated descriptors; never reopen the numeric PID. The caller keeps its identity, while this run owns an independent copy.



189
190
191
# File 'lib/osctld/container/run_configuration.rb', line 189

def retain_init_identity(identity)
  install_init_identity { identity.duplicate }
end

#root_dataset_identity(candidate, identity, temporary:) ⇒ Object (protected)



563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'lib/osctld/container/run_configuration.rb', line 563

def root_dataset_identity(candidate, identity, temporary:)
  output = syscmd_argv([
                         'zfs', 'list', '-H', '-p', '-t', 'filesystem',
                         '-o', 'name,guid,mountpoint,mounted,origin', candidate
                       ]).output.lines.map { |line| line.chomp.split("\t") }
  unless output.one? && output.first.length == 5 && output.first[0] == candidate &&
         output.first[1].match?(/\A[1-9][0-9]*\z/) && output.first[2].start_with?('/') &&
         output.first[3] == 'yes' && (!temporary || output.first[4] == '-')
    raise ConfigError, 'root ZFS identity is missing or unsupported'
  end

  properties = output.first
  File.open(properties[2], File::RDONLY | File::NOFOLLOW) do |directory|
    private_root = OsCtl::Lib::Sys.new.open_beneath(directory, 'private')
    begin
      host = private_root.stat
      guest = identity.root_dir.stat
      unless directory.stat.directory? && host.directory? && host.dev == guest.dev && host.ino == guest.ino
        raise ConfigError, 'selected private root differs from the held init root'
      end

      mounts = root_mount_entries(File.read('/proc/self/mountinfo')).select { |row| row[2] == properties[2] }
      expected = ["#{host.dev_major}:#{host.dev_minor}", '/', properties[2], 'zfs', candidate]
      raise ConfigError, 'selected dataset mount identity is ambiguous' unless mounts == [expected]

      [properties, host.dev, host.ino, directory.stat.dev, directory.stat.ino]
    ensure
      private_root.close
    end
  end
end

#root_mount_entries(bytes) ⇒ Object (protected)



541
542
543
544
545
546
547
548
549
550
551
552
# File 'lib/osctld/container/run_configuration.rb', line 541

def root_mount_entries(bytes)
  bytes.lines.map do |line|
    fields, filesystem = line.strip.split(' - ', 2)
    left = fields&.split
    right = filesystem&.split
    raise ConfigError, 'malformed mount identity' unless left && left.length >= 6 && right && right.length >= 3

    [left[2], left[3], left[4], right[0], right[1]].map do |value|
      value.gsub(/\\([0-7]{3})/) { Regexp.last_match(1).to_i(8).chr }
    end
  end
end

#rootfs ⇒ String

Container rootfs path

Returns:

  • (String)


145
146
147
148
149
150
151
# File 'lib/osctld/container/run_configuration.rb', line 145

def rootfs
  File.join(dir, 'private')
rescue SystemCommandFailed
  # Dataset for staged containers does not have to exist yet, relevant
  # primarily for ct show/list
  nil
end

#runtime_config_input ⇒ Object (protected)



469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/osctld/container/run_configuration.rb', line 469

def runtime_config_input
  File.open(file_path, File::RDONLY | File::NOFOLLOW) do |f|
    before = f.stat
    bytes = f.read
    after = f.stat
    previous = [before.dev, before.ino, before.size, before.mtime, before.ctime]
    current = [after.dev, after.ino, after.size, after.mtime, after.ctime]
    unless before.file? && previous == current
      raise ConfigError, 'runtime configuration changed during import'
    end

    [current, bytes]
  end
end

#runtime_rootfs ⇒ Object



176
177
178
179
180
181
# File 'lib/osctld/container/run_configuration.rb', line 176

def runtime_rootfs
  pid = init_pid
  raise 'init_pid not set' unless pid

  File.join('/proc', pid.to_s, 'root')
end

#save ⇒ Object



441
442
443
444
445
446
447
448
449
450
451
# File 'lib/osctld/container/run_configuration.rb', line 441

def save
  begin
    Dir.mkdir(dir_path)
  rescue Errno::EEXIST
    # ignore
  end

  regenerate_file(file_path, 0o400) do |new|
    new.write(OsCtl::Lib::ConfigFile.dump_yaml(dump))
  end
end

#set_distribution(distribution:, version:, arch:, vendor:, variant:) ⇒ Object

Update distribution info



122
123
124
125
126
127
128
129
130
131
# File 'lib/osctld/container/run_configuration.rb', line 122

def set_distribution(distribution:, version:, arch:, vendor:, variant:)
  exclusively do
    @distribution = distribution
    @version = version
    @arch = arch
    @vendor = vendor
    @variant = variant
    save
  end
end

#validate_saved_run! ⇒ Object (protected)



484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/osctld/container/run_configuration.rb', line 484

def validate_saved_run!
  cfg = @loaded_config
  required = %w[id dataset distribution version arch vendor variant destroy_dataset_on_stop]
  unless cfg.is_a?(Hash) && required.all? { |key| cfg.has_key?(key) } &&
         cfg['id'].is_a?(Hash) && cfg['id']['pool_name'] == pool.name &&
         cfg['id']['container_id'] == id && cfg['id']['timestamp'].is_a?(Numeric) &&
         cfg['id']['timestamp'].finite? && cfg['id']['timestamp'] > 0 &&
         cfg['id'] == run_id.dump && cfg['dataset'] == dataset.name &&
         [true, false].include?(cfg['destroy_dataset_on_stop']) &&
         %w[distribution version arch vendor variant].all? { |key| cfg[key].is_a?(String) && !cfg[key].empty? }
    raise ConfigError, 'running impermanent container requires complete saved run metadata'
  end
end

#wait_for_init_leases ⇒ Object (protected)



624
625
626
627
628
629
# File 'lib/osctld/container/run_configuration.rb', line 624

def wait_for_init_leases
  @init_lease_mutex.synchronize do
    @init_lease_cond.wait(@init_lease_mutex) while @init_lease_count > 0
    @destroyed = true
  end
end