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 Attribute Summary

Attributes inherited from Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

Methods included from Utils::Container

#get_image_path, #get_repositories, #remove_accounting_cgroups

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



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
118
119
120
121
122
123
124
# File 'lib/osctld/commands/container/boot.rb', line 17

def execute(ct)
  fh = 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',
        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

    # Prepare the image
    if opts[:type] == 'image'
      tpl_path = opts[:path]
    elsif opts[:type] == 'remote'
      progress('Fetching image')

      tpl = opts[:image]
      tpl[:distribution] ||= ct.distribution
      tpl[:version] ||= ct.version
      tpl[:arch] ||= ct.arch
      tpl[:vendor] ||= 'default'
      tpl[:variant] ||= 'default'

      tpl_path = get_image_path(get_repositories(ct.pool), tpl)
      error!('image not found in searched repositories') if tpl_path.nil?
    else
      error!('invalid type')
    end

    # 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)
    importer.

    # Reconfigure the container for boot
    ct_cfg = importer.get_container_config

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

    # Apply the image
    importer.import_root_dataset(builder)
    builder.shift_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)

    # Boot it
    call_cmd!(
      Commands::Container::Start,
      pool: ct.pool.name,
      id: ct.id,
      wait: opts[:wait],
      mounts: [root_mnt].compact,
    )

    ok
  end

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