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]
if opts[:mount_root]
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|
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)
fh = File.open(tpl_path, 'r')
importer = Container::Importer.new(ct.pool, fh, ct_id: ct.id, image_file: tpl_path)
importer.load_metadata
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
)
importer.import_root_dataset(builder)
builder.shift_or_mount_dataset
builder.setup_ct_dir
builder.setup_rootfs
call_cmd!(
Commands::Container::Stop,
pool: ct.pool.name,
id: ct.id
)
ct.set_next_run_conf(ctrc)
GarbageCollector.add_container_run_dataset(ctrc, tmp_ds)
start_ct(ct, root_mnt) unless opts[:queue]
end
end
start_ct(ct, root_mnt) if opts[:queue]
ok
ensure
fh && fh.close
end
|