Class: OsCtld::Commands::Container::Boot

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

Instance Method Summary collapse

Methods included from Utils::Container

#format_unavailable_repositories, #get_image_path, #get_image_path!, #get_repositories, #image_not_found_message, #image_spec, #image_unavailable_message, #remove_accounting_cgroups, #with_image_path, #with_repository_image_path!

Methods inherited from Logged

#base_execute

Instance Method Details

#execute(ct) ⇒ Object



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
55
56
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
83
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
# File 'lib/osctld/commands/container/boot.rb', line 17

def execute(ct)
  fh = nil
  root_mnt = nil

  manipulate(ct) do
    error!('container is running') if ct.running? && !opts[:force]

    # Check rootfs mount
    if opts[:mount_root]
      # Ensure the rootfs is mounted
      ct.mount(force: true)

      root_mnt = Mount::Entry.new(
        ct.rootfs,
        opts[:mount_root],
        'bind',
        'bind,rw,create=dir',
        false,
        map_ids: true,
        temp: true,
        in_config: true
      )

      mnt_at = ct.mounts.find_at(opts[:mount_root])

      if mnt_at && !mnt_at.temp
        error!("unable to mount rootfs at '#{opts[:mount_root]}': the path " \
               'is already mounted')
      end
    end

    tpl = opts[:image]
    if opts[:type] == 'remote'
      tpl[:distribution] ||= ct.distribution
      tpl[:version] ||= ct.version
      tpl[:arch] ||= ct.arch
      tpl[:vendor] ||= ct.vendor
      tpl[:variant] ||= ct.variant
    end

    with_image_path(ct.pool, type: opts[:type], path: opts[:path], image: tpl) do |tpl_path|
      # Prepare a new, temporary dataset
      tmp_name = "#{ct.dataset}.boot-#{SecureRandom.hex(3)}"
      tmp_ds = OsCtl::Lib::Zfs::Dataset.new(
        tmp_name,
        base: tmp_name
      )
      tmp_ds.create!(properties: {
        canmount: 'noauto'
      }.merge(opts[:zfs_properties] || {}))

      ctrc = ct.new_run_conf
      builder = Container::Builder.new(ctrc, cmd: self)

      # Open the image
      fh = File.open(tpl_path, 'r')
      importer = Container::Importer.new(ct.pool, fh, ct_id: ct.id, image_file: tpl_path)
      importer.

      # Reconfigure the container for boot
      ct_cfg = importer.get_container_config

      ctrc.boot_from(
        dataset: tmp_ds,
        distribution: ct_cfg['distribution'] || ct.distribution,
        version: ct_cfg['version'] || ct.version,
        arch: ct_cfg['arch'] || ct.arch,
        vendor: ct_cfg['vendor'] || ct.vendor,
        variant: ct_cfg['variant'] || ct.variant,
        destroy_dataset_on_stop: true
      )

      # Apply the image
      importer.import_root_dataset(builder)

      builder.shift_or_mount_dataset
      builder.setup_ct_dir
      builder.setup_rootfs

      # Ensure the container is stopped
      call_cmd!(
        Commands::Container::Stop,
        pool: ct.pool.name,
        id: ct.id
      )

      # Apply run configuration
      ct.set_next_run_conf(ctrc)
      GarbageCollector.add_container_run_dataset(ctrc, tmp_ds)

      # Boot it
      start_ct(ct, root_mnt) unless opts[:queue]
    end
  end

  # When starting as queued, we can't be holding the manipulation lock
  start_ct(ct, root_mnt) if opts[:queue]
  ok
ensure
  fh && fh.close
end

#findObject



12
13
14
15
# File 'lib/osctld/commands/container/boot.rb', line 12

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

#start_ct(ct, root_mnt) ⇒ Object (protected)



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/osctld/commands/container/boot.rb', line 121

def start_ct(ct, root_mnt)
  call_cmd!(
    Commands::Container::Start,
    pool: ct.pool.name,
    id: ct.id,
    wait: opts[:wait],
    queue: opts[:queue],
    priority: opts[:priority],
    debug: opts[:debug],
    mounts: [root_mnt].compact,
    custom_boot: true
  )
end