Class: OsCtld::Container
- Inherits:
-
Object
- Object
- OsCtld::Container
- Includes:
- OsCtl::Lib::Utils::File, OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, Assets::Definition, Lockable, Manipulable, Utils::SwitchUser
- Defined in:
- lib/osctld/container.rb
Defined Under Namespace
Modules: Adaptor, Hooks Classes: Builder, DatasetBuilder, Hints, Impermanence, Importer, LxcConfig, NfsCancellation, RawConfigs, Recovery, RunConfiguration, RunId, StartMenu, StopHandler
Constant Summary collapse
- MAP_MODES =
%w[native zfs].freeze
- DEFAULT_START_TIMEOUT =
120- DEFAULT_STOP_TIMEOUT =
300
Instance Attribute Summary collapse
-
#apparmor ⇒ Object
Returns the value of attribute apparmor.
-
#arch ⇒ Object
Returns the value of attribute arch.
-
#attrs ⇒ Object
Returns the value of attribute attrs.
-
#autostart ⇒ Object
Returns the value of attribute autostart.
-
#cgparams ⇒ Object
Returns the value of attribute cgparams.
-
#cpu_package ⇒ Object
Returns the value of attribute cpu_package.
-
#dataset ⇒ Object
Returns the value of attribute dataset.
-
#devices ⇒ Object
Returns the value of attribute devices.
-
#distribution ⇒ Object
Returns the value of attribute distribution.
-
#dns_resolvers ⇒ Object
Returns the value of attribute dns_resolvers.
-
#ephemeral ⇒ Object
(also: #ephemeral?)
Returns the value of attribute ephemeral.
-
#group ⇒ Object
Returns the value of attribute group.
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#id ⇒ Object
Returns the value of attribute id.
-
#impermanence ⇒ Object
Returns the value of attribute impermanence.
-
#init_cmd ⇒ Object
Returns the value of attribute init_cmd.
-
#local_transfer_log ⇒ Object
Returns the value of attribute local_transfer_log.
-
#lxc_config ⇒ Object
Returns the value of attribute lxc_config.
-
#map_mode ⇒ Object
Returns the value of attribute map_mode.
-
#mounted ⇒ Object
protected
Returns the value of attribute mounted.
-
#mounts ⇒ Object
Returns the value of attribute mounts.
-
#nesting ⇒ Object
Returns the value of attribute nesting.
-
#netifs ⇒ Object
Returns the value of attribute netifs.
-
#next_run_conf ⇒ Object
readonly
Returns the value of attribute next_run_conf.
-
#pool ⇒ Object
Returns the value of attribute pool.
-
#prlimits ⇒ Object
Returns the value of attribute prlimits.
-
#raw_configs ⇒ Object
readonly
Returns the value of attribute raw_configs.
-
#run_conf ⇒ Object
readonly
Returns the value of attribute run_conf.
-
#seccomp_profile ⇒ Object
Returns the value of attribute seccomp_profile.
-
#send_log ⇒ Object
Returns the value of attribute send_log.
-
#start_menu ⇒ Object
Returns the value of attribute start_menu.
-
#state ⇒ Object
Returns the value of attribute state.
-
#user ⇒ Object
Returns the value of attribute user.
-
#variant ⇒ Object
Returns the value of attribute variant.
-
#vendor ⇒ Object
Returns the value of attribute vendor.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
- #abs_apply_cgroup_path(subsystem) ⇒ Object
- #abs_cgroup_path(subsystem) ⇒ Object
- #assets ⇒ Object
- #attach_cgroup_path ⇒ Object
- #base_cgroup_path ⇒ Object
- #can_dist_configure_network? ⇒ Boolean
- #can_start? ⇒ Boolean
- #cgroup_path ⇒ Object
- #chgrp(grp, missing_devices: nil) ⇒ Object
- #chown(user) ⇒ Object
-
#clear_recovery_taint! ⇒ Object
Recovery owns this flag, not the generic configuration/runtime state.
- #clear_start_menu ⇒ Object
-
#clone_from(_ct, id, opts = {}) ⇒ Object
protected
Change the container so that it becomes a clone of
ctwith a different id. - #close_local_transfer_log(save: true) ⇒ Object
- #close_send_log(save: true) ⇒ Object
- #config_path ⇒ Object
- #configure(distribution, version, arch) ⇒ Object
- #configure_bashrc ⇒ Object
-
#current_state ⇒ Symbol
Fetch current container state by forking into it.
-
#datasets ⇒ Array<OsCtl::Lib::Zfs::Dataset>
Return a list of all container datasets.
- #default_init_cmd ⇒ Object protected
- #default_seccomp_profile ⇒ Object protected
- #dir ⇒ Object
-
#dump_config ⇒ Object
Dump to config.
-
#dup(id, opts = {}) ⇒ Object
Duplicate the container with a different ID.
-
#each_dataset {|ds| ... } ⇒ Object
Iterate over all container datasets.
-
#ensure_run_conf ⇒ Container::RunConfiguration
Call #init_run_conf unless #run_conf is already set.
- #entry_cgroup_path ⇒ Object
-
#export ⇒ Object
Export to clients.
-
#find_cpu_limit(parents: true) ⇒ Integer?
CPU limit in percent (100 % for one CPU).
-
#find_memory_limit(parents: true) ⇒ Integer?
Memory limit in bytes.
-
#find_swap_limit(parents: true) ⇒ Integer?
Swap limit in bytes.
- #forget_past_run_conf(expected_run_conf = nil) ⇒ Object
- #format_exec_init_cmd ⇒ Object
- #format_user_init_cmd ⇒ Object
-
#fresh_state ⇒ Symbol
Fetch current state if the state is not known, otherwise return the known state.
-
#get_exit_promise ⇒ Promise::Token?
STOPPED can precede asynchronous exit cleanup.
- #get_past_run_conf ⇒ Container::RunConfiguration?
- #get_run_conf ⇒ Container::RunConfiguration
- #gid_map ⇒ Object
- #ident ⇒ Object
- #init_pid ⇒ Object
-
#init_run_conf ⇒ Object
This must be called on container start.
-
#initialize(pool, id, user = nil, group = nil, dataset = nil, opts = {}) ⇒ Container
constructor
A new instance of Container.
- #load_config_file ⇒ Object protected
- #load_config_hash(cfg, init_devices: true, dataset_cache: nil, trusted_runtime: false) ⇒ Object protected
- #load_config_string(str) ⇒ Object protected
- #log_path ⇒ Object
- #log_type ⇒ Object
- #lxc_dir(user: nil, group: nil) ⇒ Object
- #lxc_home(user: nil, group: nil) ⇒ Object
- #manipulation_resource ⇒ Object
-
#mount(force: false) ⇒ Object
Mount the container's dataset.
-
#mounted?(force: false) ⇒ Boolean
Check if the container's dataset is mounted.
- #new_run_conf ⇒ Container::RunConfiguration
- #open_local_transfer_log(role, opts = {}) ⇒ Object
- #open_send_log(role, token, opts = {}) ⇒ Object
- #parse_yaml ⇒ Object protected
-
#patch_config(new_config) ⇒ Object
Update keys/values from
new_configin the container's config. - #payload_cgroup_path ⇒ Object
-
#read_hostname ⇒ String?
Read hostname from a running container.
-
#reconfigure ⇒ Object
Regenerate LXC config.
- #recovery_tainted? ⇒ Boolean
- #refresh_init_pid(expected_run_conf: nil) ⇒ Object
- #reload_config ⇒ Object
- #replace_config(config) ⇒ Object
- #root_host_gid ⇒ Object
- #root_host_uid ⇒ Object
- #rootfs ⇒ Object
- #running? ⇒ Boolean
-
#sanitize_external_runtime_state(cfg) ⇒ Object
protected
Runtime state and host-side link identities are daemon-owned metadata and therefore cannot come from imported/replaced/patched container YAML.
- #save_config ⇒ Object
- #set(opts) ⇒ Object
- #set_init_pid(pid, expected_run_conf: nil) ⇒ Object
- #set_next_run_conf(next_run_conf) ⇒ Object
- #setup_start_menu ⇒ Object
- #starting ⇒ Object
- #stopped(expected_run_conf = nil) ⇒ Object
-
#sync_host_link_configuration(&block) ⇒ Object
protected
Keep this ordering consistent with recovery, which holds the global registry while reading the container's installed netif manager.
- #syslogns_tag(run_id: nil) ⇒ Object
- #taint_recovery! ⇒ Object
- #transfer_in_progress? ⇒ Boolean
- #transfer_log ⇒ Object
- #uid_map ⇒ Object
-
#unmount(force: false) ⇒ Object
Unmount the container's dataset.
-
#unregister ⇒ Object
Unregister the container from internal uses in osctld, e.g.
- #unset(opts) ⇒ Object
- #update_hints ⇒ Object
- #user_hook_script_dir ⇒ Object
- #wrapper_cgroup_path ⇒ Object
Methods included from Utils::SwitchUser
Methods included from Assets::Definition
Methods included from Manipulable
#acquire_manipulation_lock, #init_manipulable, #is_being_manipulated?, #manipulate, #manipulated_by, #release_manipulation_lock
Methods included from Lockable
#exclusively, included, #inclusively, #init_lock, #lock, #unlock
Constructor Details
#initialize(pool, id, user = nil, group = nil, dataset = nil, opts = {}) ⇒ Container
Returns a new instance of Container.
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 |
# File 'lib/osctld/container.rb', line 51 def initialize(pool, id, user = nil, group = nil, dataset = nil, opts = {}) init_lock init_manipulable opts[:load] = true unless opts.has_key?(:load) if (user.nil? || group.nil?) && !opts[:load] raise ArgumentError, 'either set load: true or provide user and group' end @pool = pool @id = id @user = user @group = group @dataset = dataset @map_mode = opts[:map_mode] @state = opts[:staged] ? :staged : :unknown @recovery_tainted = false @local_transfer_log = nil @ephemeral = false @netifs = NetInterface::Manager.new(self) @cgparams = nil @devices = nil @prlimits = nil @mounts = nil @hostname = nil @dns_resolvers = nil @nesting = false @cpu_package = 'auto' @seccomp_profile = nil @apparmor = AppArmor.new(self) @lxc_config = Container::LxcConfig.new(self) @init_cmd = nil @raw_configs = Container::RawConfigs.new @attrs = Attributes.new @run_conf = nil @hints = Container::Hints.new(self) return unless opts[:load] load_opts = { init_devices: !opts.has_key?(:devices) || opts[:devices], dataset_cache: opts[:dataset_cache] } setup_state_changed = if opts[:load_from] load_config_string(opts[:load_from], **load_opts) else load_config_file(**load_opts) end save_config if setup_state_changed end |
Instance Attribute Details
#apparmor ⇒ Object
Returns the value of attribute apparmor
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def apparmor @apparmor end |
#arch ⇒ Object
Returns the value of attribute arch
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def arch @arch end |
#attrs ⇒ Object
Returns the value of attribute attrs
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def attrs @attrs end |
#autostart ⇒ Object
Returns the value of attribute autostart
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def autostart @autostart end |
#cgparams ⇒ Object
Returns the value of attribute cgparams
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def cgparams @cgparams end |
#cpu_package ⇒ Object
Returns the value of attribute cpu_package
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def cpu_package @cpu_package end |
#dataset ⇒ Object
Returns the value of attribute dataset
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def dataset @dataset end |
#devices ⇒ Object
Returns the value of attribute devices
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def devices @devices end |
#distribution ⇒ Object
Returns the value of attribute distribution
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def distribution @distribution end |
#dns_resolvers ⇒ Object
Returns the value of attribute dns_resolvers
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def dns_resolvers @dns_resolvers end |
#ephemeral ⇒ Object Also known as: ephemeral?
Returns the value of attribute ephemeral
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def ephemeral @ephemeral end |
#group ⇒ Object
Returns the value of attribute group
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def group @group end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def hints @hints end |
#hostname ⇒ Object
Returns the value of attribute hostname
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def hostname @hostname end |
#id ⇒ Object
Returns the value of attribute id
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def id @id end |
#impermanence ⇒ Object
Returns the value of attribute impermanence
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def impermanence @impermanence end |
#init_cmd ⇒ Object
Returns the value of attribute init_cmd
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def init_cmd @init_cmd end |
#local_transfer_log ⇒ Object
Returns the value of attribute local_transfer_log
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def local_transfer_log @local_transfer_log end |
#lxc_config ⇒ Object
Returns the value of attribute lxc_config
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def lxc_config @lxc_config end |
#map_mode ⇒ Object
Returns the value of attribute map_mode
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def map_mode @map_mode end |
#mounted ⇒ Object (protected)
Returns the value of attribute mounted
1034 1035 1036 |
# File 'lib/osctld/container.rb', line 1034 def mounted @mounted end |
#mounts ⇒ Object
Returns the value of attribute mounts
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def mounts @mounts end |
#nesting ⇒ Object
Returns the value of attribute nesting
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def nesting @nesting end |
#netifs ⇒ Object
Returns the value of attribute netifs
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def netifs @netifs end |
#next_run_conf ⇒ Object (readonly)
Returns the value of attribute next_run_conf
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def next_run_conf @next_run_conf end |
#pool ⇒ Object
Returns the value of attribute pool
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def pool @pool end |
#prlimits ⇒ Object
Returns the value of attribute prlimits
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def prlimits @prlimits end |
#raw_configs ⇒ Object (readonly)
Returns the value of attribute raw_configs
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def raw_configs @raw_configs end |
#run_conf ⇒ Object (readonly)
Returns the value of attribute run_conf
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def run_conf @run_conf end |
#seccomp_profile ⇒ Object
Returns the value of attribute seccomp_profile
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def seccomp_profile @seccomp_profile end |
#send_log ⇒ Object
Returns the value of attribute send_log
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def send_log @send_log end |
#start_menu ⇒ Object
Returns the value of attribute start_menu
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def @start_menu end |
#state ⇒ Object
Returns the value of attribute state
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def state @state end |
#user ⇒ Object
Returns the value of attribute user
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def user @user end |
#variant ⇒ Object
Returns the value of attribute variant
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def variant @variant end |
#vendor ⇒ Object
Returns the value of attribute vendor
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def vendor @vendor end |
#version ⇒ Object
Returns the value of attribute version
29 30 31 |
# File 'lib/osctld/container.rb', line 29 def version @version end |
Class Method Details
.default_dataset(pool, id, dataset_cache: nil) ⇒ Object
24 25 26 27 |
# File 'lib/osctld/container.rb', line 24 def self.default_dataset(pool, id, dataset_cache: nil) name = File.join(pool.ct_ds, id) OsCtl::Lib::Zfs::Dataset.new(name, base: name, cache: dataset_cache) end |
Instance Method Details
#abs_apply_cgroup_path(subsystem) ⇒ Object
582 583 584 |
# File 'lib/osctld/container.rb', line 582 def abs_apply_cgroup_path(subsystem) CGroup.abs_cgroup_path(subsystem, base_cgroup_path) end |
#abs_cgroup_path(subsystem) ⇒ Object
578 579 580 |
# File 'lib/osctld/container.rb', line 578 def abs_cgroup_path(subsystem) CGroup.abs_cgroup_path(subsystem, cgroup_path) end |
#assets ⇒ Object
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/osctld/container.rb', line 126 def assets define_assets do |add| # Datasets add.dataset( dataset.to_s, desc: "Container's rootfs dataset", uidmap: map_mode == 'zfs' ? uid_map.map(&:to_a) : nil, gidmap: map_mode == 'zfs' ? gid_map.map(&:to_a) : nil, properties: if map_mode == 'native' { 'uidmap' => 'none', 'gidmap' => 'none' } end, user: map_mode == 'zfs' ? root_host_uid : 0, group: map_mode == 'zfs' ? root_host_gid : 0, mode: 0o770, validate_if: mounted? ) # Directories and files add.directory( rootfs, desc: "Container's rootfs", user: map_mode == 'zfs' ? root_host_uid : 0, group: map_mode == 'zfs' ? root_host_gid : 0, mode_bit_and: 0o111, # has all executable bits set validate_if: mounted? ) add.directory( user_hook_script_dir, desc: 'User supplied script hooks', user: 0, group: 0, mode: 0o700 ) add.directory( lxc_dir, desc: 'LXC configuration', user: 0, group: user.ugid, mode: 0o750 ) lxc_config.assets(add) add.file( File.join(lxc_dir, '.bashrc'), desc: 'Shell configuration file for osctl ct su', user: 0, group: 0, mode: 0o644 ) add.file( config_path, desc: 'Container config for osctld', user: 0, group: 0, mode: 0o400 ) add.file( log_path, desc: 'LXC log file', user: 0, group: user.ugid, mode: 0o660 ) run_conf.assets(add) if run_conf devices.assets(add) end end |
#attach_cgroup_path ⇒ Object
574 575 576 |
# File 'lib/osctld/container.rb', line 574 def attach_cgroup_path File.join(payload_cgroup_path, 'osctl.attach') end |
#base_cgroup_path ⇒ Object
554 555 556 |
# File 'lib/osctld/container.rb', line 554 def base_cgroup_path inclusively { File.join(group.full_cgroup_path(user), "ct.#{id}") } end |
#can_dist_configure_network? ⇒ Boolean
489 490 491 492 493 494 495 |
# File 'lib/osctld/container.rb', line 489 def can_dist_configure_network? inclusively do next false if netifs.detect { |netif| !netif.can_run_distconfig? } true end end |
#can_start? ⇒ Boolean
413 414 415 416 417 418 |
# File 'lib/osctld/container.rb', line 413 def can_start? inclusively do state != :staged && state != :error && !@recovery_tainted && !netifs.recovery_tainted? && pool.active? end end |
#cgroup_path ⇒ Object
558 559 560 |
# File 'lib/osctld/container.rb', line 558 def cgroup_path File.join(base_cgroup_path, 'user-owned') end |
#chgrp(grp, missing_devices: nil) ⇒ Object
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/osctld/container.rb', line 327 def chgrp(grp, missing_devices: nil) self.group = grp case missing_devices when 'provide' devices.ensure_all devices.init when 'remove' devices.remove_missing devices.init when 'check' devices.check_all_available!(group: grp) else raise "unsupported action for missing devices: '#{missing_devices}'" end save_config lxc_config.configure configure_bashrc end |
#chown(user) ⇒ Object
320 321 322 323 324 325 |
# File 'lib/osctld/container.rb', line 320 def chown(user) self.user = user save_config lxc_config.configure configure_bashrc end |
#clear_recovery_taint! ⇒ Object
Recovery owns this flag, not the generic configuration/runtime state.
408 409 410 411 |
# File 'lib/osctld/container.rb', line 408 def clear_recovery_taint! exclusively { @recovery_tainted = false } save_config end |
#clear_start_menu ⇒ Object
751 752 753 754 |
# File 'lib/osctld/container.rb', line 751 def = .unlink if end |
#clone_from(_ct, id, opts = {}) ⇒ Object (protected)
Change the container so that it becomes a clone of ct with a different id
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 |
# File 'lib/osctld/container.rb', line 1225 def clone_from(_ct, id, opts = {}) init_lock init_manipulable @id = id @pool = opts[:pool] if opts[:pool] @user = opts[:user] if opts[:user] @group = opts[:group] if opts[:group] @state = :staged @send_log = nil @recovery_tainted = false @local_transfer_log = nil @dataset = if opts[:dataset] OsCtl::Lib::Zfs::Dataset.new( opts[:dataset], base: opts[:dataset] ) else Container.default_dataset(@pool, @id) end @apparmor = @apparmor.dup(self) @autostart &&= @autostart.dup(self) @cgparams = cgparams.dup(self) @prlimits = prlimits.dup(self) @mounts = mounts.dup(self) @start_menu &&= @start_menu.dup(self) @impermanence &&= @impermanence.dup @lxc_config = lxc_config.dup(self) @raw_configs = raw_configs.dup @attrs = attrs.dup @run_conf = nil @next_run_conf = nil @past_run_conf = nil @devices = devices.dup(self) devices.init if opts[:network_interfaces] @netifs = netifs.dup(self) netifs.each(&:setup) else @netifs = NetInterface::Manager.new(self) end @hints = hints.dup(self) end |
#close_local_transfer_log(save: true) ⇒ Object
815 816 817 818 819 820 821 |
# File 'lib/osctld/container.rb', line 815 def close_local_transfer_log(save: true) exclusively do local_transfer_log.close self.local_transfer_log = nil save_config if save end end |
#close_send_log(save: true) ⇒ Object
800 801 802 803 804 805 806 |
# File 'lib/osctld/container.rb', line 800 def close_send_log(save: true) exclusively do send_log.close self.send_log = nil save_config if save end end |
#config_path ⇒ Object
517 518 519 |
# File 'lib/osctld/container.rb', line 517 def config_path inclusively { File.join(pool.conf_path, 'ct', "#{id}.yml") } end |
#configure(distribution, version, arch) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/osctld/container.rb', line 108 def configure(distribution, version, arch) exclusively do @distribution = distribution @version = version @arch = arch @netifs = NetInterface::Manager.new(self) @nesting = false @seccomp_profile = default_seccomp_profile @cgparams = CGroup::ContainerParams.new(self) @devices = Devices::Manager.new_for(self) @prlimits = PrLimits::Manager.default(self) @mounts = Mount::Manager.new(self) @run_conf ||= new_run_conf devices.init save_config end end |
#configure_bashrc ⇒ Object
779 780 781 782 783 784 785 786 787 788 789 790 791 |
# File 'lib/osctld/container.rb', line 779 def configure_bashrc ErbTemplate.render_to('ct/bashrc', { ct: self, override: %w[ attach cgroup console device execute freeze info ls monitor stop top unfreeze wait ], disable: %w[ autostart checkpoint clone copy create destroy snapshot start-ephemeral unshare ] }, File.join(lxc_dir, '.bashrc')) end |
#current_state ⇒ Symbol
Fetch current container state by forking into it
371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/osctld/container.rb', line 371 def current_state st = ContainerControl::Commands::State.run!(self) self.state = st.state if st.state == :stopped run_conf&.clear_dead_init_identity elsif st.init_pid set_init_pid(st.init_pid) end st.state rescue ContainerControl::Error self.state = :error end |
#datasets ⇒ Array<OsCtl::Lib::Zfs::Dataset>
Return a list of all container datasets
543 544 545 546 |
# File 'lib/osctld/container.rb', line 543 def datasets ds = inclusively { dataset } [ds] + ds.descendants end |
#default_init_cmd ⇒ Object (protected)
1278 1279 1280 |
# File 'lib/osctld/container.rb', line 1278 def default_init_cmd ['/sbin/init'] end |
#default_seccomp_profile ⇒ Object (protected)
1274 1275 1276 |
# File 'lib/osctld/container.rb', line 1274 def default_seccomp_profile '/etc/lxc/config/common.seccomp' end |
#dir ⇒ Object
497 498 499 |
# File 'lib/osctld/container.rb', line 497 def dir dataset.mountpoint end |
#dump_config ⇒ Object
Dump to config
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 |
# File 'lib/osctld/container.rb', line 891 def dump_config inclusively do data = { 'user' => user.name, 'group' => group.name, 'dataset' => dataset.name, 'map_mode' => map_mode, 'distribution' => distribution, 'version' => version, 'arch' => arch, 'vendor' => vendor, 'variant' => variant, 'net_interfaces' => netifs.dump, 'cgparams' => cgparams.dump, 'devices' => devices.dump, 'prlimits' => prlimits.dump, 'mounts' => mounts.dump, 'autostart' => autostart && autostart.dump, 'ephemeral' => ephemeral, 'hostname' => hostname && hostname.to_s, 'dns_resolvers' => dns_resolvers, 'nesting' => nesting, 'seccomp_profile' => if seccomp_profile == default_seccomp_profile nil else seccomp_profile end, 'cpu_package' => cpu_package, 'init_cmd' => init_cmd, 'start_menu' => && .dump, 'impermanence' => impermanence && impermanence.dump, 'raw' => raw_configs.dump, 'attrs' => attrs.dump, 'hints' => hints.dump } data['state'] = state.to_s if %i[staged error].include?(state) data['recovery_tainted'] = true if @recovery_tainted data['send_log'] = send_log.dump if send_log data['local_transfer_log'] = local_transfer_log.dump if local_transfer_log data end end |
#dup(id, opts = {}) ⇒ Object
Duplicate the container with a different ID
The returned container has state set to :staged and its assets will
not exist, so the caller has to build the container and call
ct.state = :complete for the container to become usable.
214 215 216 217 218 |
# File 'lib/osctld/container.rb', line 214 def dup(id, opts = {}) ct = clone ct.send(:clone_from, self, id, opts) ct end |
#each_dataset {|ds| ... } ⇒ Object
Iterate over all container datasets
550 551 552 |
# File 'lib/osctld/container.rb', line 550 def each_dataset(&) datasets.each(&) end |
#ensure_run_conf ⇒ Container::RunConfiguration
Call #init_run_conf unless #run_conf is already set
278 279 280 281 282 283 |
# File 'lib/osctld/container.rb', line 278 def ensure_run_conf exclusively do init_run_conf if @run_conf.nil? run_conf end end |
#entry_cgroup_path ⇒ Object
566 567 568 |
# File 'lib/osctld/container.rb', line 566 def entry_cgroup_path File.join(cgroup_path, "lxc.monitor.#{id}") end |
#export ⇒ Object
Export to clients
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 |
# File 'lib/osctld/container.rb', line 839 def export refresh_init_pid if running? && init_pid.nil? inclusively do { pool: pool.name, id:, user: user.name, group: group.name, uid_map: user.uid_map.map(&:to_h), gid_map: user.gid_map.map(&:to_h), map_mode: map_mode, dataset: dataset.name, rootfs:, boot_dataset: run_conf ? run_conf.dataset.name : dataset.name, boot_rootfs: run_conf ? run_conf.rootfs : rootfs, lxc_path: lxc_home, lxc_dir:, group_path: cgroup_path, distribution: run_conf ? run_conf.distribution : distribution, version: run_conf ? run_conf.version : version, arch: run_conf ? run_conf.arch : arch, vendor: run_conf ? run_conf.vendor : vendor, variant: run_conf ? run_conf.variant : variant, state:, recovery_tainted: recovery_tainted?, init_pid:, autostart: autostart ? true : false, autostart_priority: autostart && autostart.priority, autostart_delay: autostart && autostart.delay, ephemeral:, hostname:, dns_resolvers:, nesting:, seccomp_profile:, init_cmd: format_user_init_cmd, cpu_package_inuse: run_conf ? run_conf.cpu_package : nil, cpu_package_set: cpu_package, cpu_limit: find_cpu_limit(parents: false), memory_limit: find_memory_limit(parents: false), swap_limit: find_swap_limit(parents: false), start_menu: ? true : false, start_menu_timeout: && .timeout, impermanence: impermanence ? true : false, impermanence_zfs_properties: impermanence&.zfs_properties, raw_lxc: raw_configs.lxc, log_file: log_path }.merge!(attrs.export) end end |
#find_cpu_limit(parents: true) ⇒ Integer?
Returns CPU limit in percent (100 % for one CPU).
613 614 615 616 617 618 619 620 621 622 623 |
# File 'lib/osctld/container.rb', line 613 def find_cpu_limit(parents: true) limit = cgparams&.find_cpu_limit if limit return limit elsif !parents return end group.find_cpu_limit(parents:) end |
#find_memory_limit(parents: true) ⇒ Integer?
Returns memory limit in bytes.
587 588 589 590 591 592 593 594 595 596 597 |
# File 'lib/osctld/container.rb', line 587 def find_memory_limit(parents: true) limit = cgparams&.find_memory_limit if limit return limit elsif !parents return end group.find_memory_limit(parents:) end |
#find_swap_limit(parents: true) ⇒ Integer?
Returns swap limit in bytes.
600 601 602 603 604 605 606 607 608 609 610 |
# File 'lib/osctld/container.rb', line 600 def find_swap_limit(parents: true) limit = cgparams&.find_swap_limit if limit return limit elsif !parents return end group.find_swap_limit(parents:) end |
#forget_past_run_conf(expected_run_conf = nil) ⇒ Object
248 249 250 251 252 |
# File 'lib/osctld/container.rb', line 248 def forget_past_run_conf(expected_run_conf = nil) exclusively do @past_run_conf = nil if expected_run_conf.nil? || @past_run_conf.equal?(expected_run_conf) end end |
#format_exec_init_cmd ⇒ Object
975 976 977 978 979 980 981 982 983 984 |
# File 'lib/osctld/container.rb', line 975 def format_exec_init_cmd cmd = init_cmd || default_init_cmd = if .init_cmd(cmd) else cmd end.join(' ') end |
#format_user_init_cmd ⇒ Object
971 972 973 |
# File 'lib/osctld/container.rb', line 971 def format_user_init_cmd (init_cmd || default_init_cmd).join(' ') end |
#fresh_state ⇒ Symbol
Fetch current state if the state is not known, otherwise return the known state
386 387 388 389 390 391 392 |
# File 'lib/osctld/container.rb', line 386 def fresh_state if state == :unknown current_state else state end end |
#get_exit_promise ⇒ Promise::Token?
STOPPED can precede asynchronous exit cleanup. Do not substitute a fresh configuration for the retired run when subscribing to its completion.
233 234 235 236 237 238 239 240 241 |
# File 'lib/osctld/container.rb', line 233 def get_exit_promise inclusively do if @run_conf&.init_pid @run_conf.get_exit_promise else @past_run_conf&.get_exit_promise end end end |
#get_past_run_conf ⇒ Container::RunConfiguration?
244 245 246 |
# File 'lib/osctld/container.rb', line 244 def get_past_run_conf inclusively { @past_run_conf } end |
#get_run_conf ⇒ Container::RunConfiguration
226 227 228 |
# File 'lib/osctld/container.rb', line 226 def get_run_conf run_conf || new_run_conf end |
#gid_map ⇒ Object
529 530 531 |
# File 'lib/osctld/container.rb', line 529 def gid_map user.gid_map end |
#ident ⇒ Object
104 105 106 |
# File 'lib/osctld/container.rb', line 104 def ident inclusively { "#{pool.name}:#{id}" } end |
#init_pid ⇒ Object
420 421 422 423 424 |
# File 'lib/osctld/container.rb', line 420 def init_pid inclusively do @run_conf ? run_conf.init_pid : nil end end |
#init_run_conf ⇒ Object
This must be called on container start
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/osctld/container.rb', line 260 def init_run_conf exclusively do if @next_run_conf @run_conf = @next_run_conf @next_run_conf = nil else @run_conf = new_run_conf end @run_conf.save end # Generate LXC configs for current time namespace offsets reconfigure end |
#load_config_file ⇒ Object (protected)
1042 1043 1044 1045 1046 1047 1048 |
# File 'lib/osctld/container.rb', line 1042 def load_config_file(**) cfg = parse_yaml do OsCtl::Lib::ConfigFile.load_yaml_file(config_path) end load_config_hash(cfg, trusted_runtime: true, **) end |
#load_config_hash(cfg, init_devices: true, dataset_cache: nil, trusted_runtime: false) ⇒ Object (protected)
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 |
# File 'lib/osctld/container.rb', line 1061 def load_config_hash(cfg, init_devices: true, dataset_cache: nil, trusted_runtime: false) cfg = Container::Adaptor.adapt(self, cfg) setup_state_changed = false sync_host_link_configuration do cfg = sanitize_external_runtime_state(cfg) unless trusted_runtime @state = cfg['state'].to_sym if cfg['state'] @recovery_tainted = cfg.fetch('recovery_tainted', false) unless [true, false].include?(@recovery_tainted) raise ConfigError, 'invalid recovery taint' end @user ||= DB::Users.find(cfg['user'], pool) || (raise ConfigError, "container #{id}: user '#{cfg['user']}' not found") @group ||= DB::Groups.find(cfg['group'], pool) || (raise ConfigError, "container #{id}: group '#{cfg['group']}' not found") @dataset ||= if cfg['dataset'] OsCtl::Lib::Zfs::Dataset.new( cfg['dataset'], base: cfg['dataset'], cache: dataset_cache ) else Container.default_dataset( pool, id, dataset_cache: ) end @map_mode ||= cfg.fetch('map_mode', 'zfs') @distribution = cfg['distribution'] @version = cfg['version'] @arch = cfg['arch'] @vendor = cfg.fetch('vendor', 'default') @variant = cfg.fetch('variant', 'default') @autostart = cfg['autostart'] && AutoStart::Config.load(self, cfg['autostart']) @ephemeral = cfg['ephemeral'] @hostname = cfg['hostname'] && OsCtl::Lib::Hostname.new(cfg['hostname']) @dns_resolvers = cfg['dns_resolvers'] @nesting = cfg['nesting'] || false @cpu_package = cfg.fetch('cpu_package', 'auto') @seccomp_profile = cfg['seccomp_profile'] || default_seccomp_profile @init_cmd = cfg['init_cmd'] @start_menu = if !cfg.has_key?('start_menu') || cfg['start_menu'] Container::StartMenu.load(self, cfg['start_menu'] || {}) end @impermanence = if cfg['impermanence'] Container::Impermanence.load(cfg['impermanence']) end @run_conf = Container::RunConfiguration.load(self) if cfg['send_log'] @send_log = SendReceive::Log.load(cfg['send_log']) SendReceive::Tokens.register(@send_log.token) end if cfg['local_transfer_log'] @local_transfer_log = LocalTransfer::Log.load(cfg['local_transfer_log']) end @cgparams = CGroup::ContainerParams.load(self, cfg['cgparams']) @prlimits = PrLimits::Manager.load(self, cfg['prlimits'] || {}) @raw_configs = Container::RawConfigs.new(cfg['raw'] || {}) @attrs = Attributes.load(cfg['attrs'] || {}) # It's necessary to load devices _before_ netifs. The device manager needs # to create cgroups first, in order for echo a > devices.deny to work. # If the container has a veth interface, the setup code switches to the # container's user, which creates cgroups in all subsystems. Devices then # can't be initialized properly. @devices = Devices::Manager.load(self, cfg['devices'] || []) @devices.init if init_devices @netifs = NetInterface::Manager.load( self, cfg['net_interfaces'] || [], discover_host_links: trusted_runtime ) setup_state_changed = @netifs.setup_state_changed? if @netifs.recovery_tainted? setup_state_changed ||= !@recovery_tainted @recovery_tainted = true end @mounts = Mount::Manager.load(self, cfg['mounts'] || []) @hints = Container::Hints.load(self, cfg['hints'] || {}) end setup_state_changed end |
#load_config_string(str) ⇒ Object (protected)
1050 1051 1052 1053 |
# File 'lib/osctld/container.rb', line 1050 def load_config_string(str, **) cfg = parse_yaml { OsCtl::Lib::ConfigFile.load_yaml(str) } load_config_hash(cfg, **) end |
#log_path ⇒ Object
1015 1016 1017 |
# File 'lib/osctld/container.rb', line 1015 def log_path inclusively { File.join(pool.log_path, 'ct', "#{id}.log") } end |
#log_type ⇒ Object
1019 1020 1021 |
# File 'lib/osctld/container.rb', line 1019 def log_type inclusively { "ct=#{pool.name}:#{id}" } end |
#lxc_dir(user: nil, group: nil) ⇒ Object
505 506 507 |
# File 'lib/osctld/container.rb', line 505 def lxc_dir(user: nil, group: nil) inclusively { File.join(lxc_home(user:, group:), id) } end |
#lxc_home(user: nil, group: nil) ⇒ Object
501 502 503 |
# File 'lib/osctld/container.rb', line 501 def lxc_home(user: nil, group: nil) inclusively { (group || self.group).userdir(user || self.user) } end |
#manipulation_resource ⇒ Object
1023 1024 1025 |
# File 'lib/osctld/container.rb', line 1023 def manipulation_resource ['container', ident] end |
#mount(force: false) ⇒ Object
Mount the container's dataset
288 289 290 291 292 293 |
# File 'lib/osctld/container.rb', line 288 def mount(force: false) return if !force && mounted dataset.mount(recursive: true) self.mounted = true end |
#mounted?(force: false) ⇒ Boolean
Check if the container's dataset is mounted
306 307 308 309 310 311 312 |
# File 'lib/osctld/container.rb', line 306 def mounted?(force: false) if force || mounted.nil? self.mounted = dataset.mounted?(recursive: true) else mounted end end |
#new_run_conf ⇒ Container::RunConfiguration
221 222 223 |
# File 'lib/osctld/container.rb', line 221 def new_run_conf Container::RunConfiguration.new(self, load_conf: false) end |
#open_local_transfer_log(role, opts = {}) ⇒ Object
808 809 810 811 812 813 |
# File 'lib/osctld/container.rb', line 808 def open_local_transfer_log(role, opts = {}) exclusively do self.local_transfer_log = LocalTransfer::Log.new(role:, opts:) save_config end end |
#open_send_log(role, token, opts = {}) ⇒ Object
793 794 795 796 797 798 |
# File 'lib/osctld/container.rb', line 793 def open_send_log(role, token, opts = {}) exclusively do self.send_log = SendReceive::Log.new(role:, token:, opts:) save_config end end |
#parse_yaml ⇒ Object (protected)
1055 1056 1057 1058 1059 |
# File 'lib/osctld/container.rb', line 1055 def parse_yaml yield rescue Psych::Exception => e raise ConfigError.new("Unable to load config of container #{id}", e) end |
#patch_config(new_config) ⇒ Object
Update keys/values from new_config in the container's config
962 963 964 965 966 967 968 969 |
# File 'lib/osctld/container.rb', line 962 def patch_config(new_config) sync_host_link_configuration do tmp = dump_config tmp.update(new_config) load_config_hash(tmp) save_config end end |
#payload_cgroup_path ⇒ Object
570 571 572 |
# File 'lib/osctld/container.rb', line 570 def payload_cgroup_path File.join(cgroup_path, "lxc.payload.#{id}") end |
#read_hostname ⇒ String?
Read hostname from a running container
758 759 760 761 762 763 764 765 766 767 |
# File 'lib/osctld/container.rb', line 758 def read_hostname return nil unless running? begin ContainerControl::Commands::GetHostname.run!(self) rescue ContainerControl::Error => e log(:warn, "Unable to read container hostname: #{e.}") nil end end |
#reconfigure ⇒ Object
Regenerate LXC config
775 776 777 |
# File 'lib/osctld/container.rb', line 775 def reconfigure lxc_config.configure end |
#recovery_tainted? ⇒ Boolean
398 399 400 |
# File 'lib/osctld/container.rb', line 398 def recovery_tainted? inclusively { @recovery_tainted } end |
#refresh_init_pid(expected_run_conf: nil) ⇒ Object
426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/osctld/container.rb', line 426 def refresh_init_pid(expected_run_conf: nil) return init_pid unless running? st = ContainerControl::Commands::State.run!(self) self.state = st.state return unless st.init_pid set_init_pid(st.init_pid, expected_run_conf:) rescue ContainerControl::Error nil end |
#reload_config ⇒ Object
946 947 948 949 950 |
# File 'lib/osctld/container.rb', line 946 def reload_config sync_host_link_configuration do save_config if load_config_file end end |
#replace_config(config) ⇒ Object
953 954 955 956 957 958 |
# File 'lib/osctld/container.rb', line 953 def replace_config(config) sync_host_link_configuration do load_config_string(config) save_config end end |
#root_host_gid ⇒ Object
537 538 539 |
# File 'lib/osctld/container.rb', line 537 def root_host_gid user.gid_map.ns_to_host(0) end |
#root_host_uid ⇒ Object
533 534 535 |
# File 'lib/osctld/container.rb', line 533 def root_host_uid user.uid_map.ns_to_host(0) end |
#rootfs ⇒ Object
509 510 511 512 513 514 515 |
# File 'lib/osctld/container.rb', line 509 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 |
#running? ⇒ Boolean
394 395 396 |
# File 'lib/osctld/container.rb', line 394 def running? state == :running end |
#sanitize_external_runtime_state(cfg) ⇒ Object (protected)
Runtime state and host-side link identities are daemon-owned metadata and therefore cannot come from imported/replaced/patched container YAML. Preserve only records already owned by this daemon, matched to the exact configured type/name. Every retained identity, clean or tainted, must be cleared by deliberate lifecycle cleanup before an external edit can discard or rename its owner.
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 |
# File 'lib/osctld/container.rb', line 1163 def sanitize_external_runtime_state(cfg) current = {} @netifs&.each do |netif| next unless netif.respond_to?(:host_link_identity) name, ifindex, ifb_ifindex = netif.host_link_identity next unless name key = [netif.type.to_s, netif.name] if current.has_key?(key) raise ConfigError, "duplicate internal host-link owner #{key.join('/')}" end current[key] = { 'name' => name, 'ifindex' => ifindex, 'ifb_ifindex' => ifb_ifindex, 'tainted' => netif.respond_to?(:host_link_tainted?) && netif.host_link_tainted? } end owner_keys = current.keys seen = Hash.new(0) netifs = Array(cfg['net_interfaces']).map do |netif_cfg| next netif_cfg unless netif_cfg.is_a?(Hash) sanitized = netif_cfg.dup sanitized.delete('host_link') key = [sanitized['type'].to_s, sanitized['name']] seen[key] += 1 if seen[key] > 1 && owner_keys.include?(key) raise ConfigError, "external config duplicates internal host-link owner #{key.join('/')}" end sanitized['host_link'] = current.delete(key) if seen[key] == 1 && current.has_key?(key) sanitized end unless current.empty? owners = current.keys.map { |type, name| "#{type}/#{name}" }.join(', ') raise ConfigError, "external config cannot discard internal host-link owners: #{owners}" end sanitized = cfg.merge('net_interfaces' => netifs) sanitized['recovery_tainted'] = @recovery_tainted if %i[staged error].include?(@state) sanitized['state'] = @state.to_s else sanitized.delete('state') end sanitized end |
#save_config ⇒ Object
936 937 938 939 940 941 942 943 944 |
# File 'lib/osctld/container.rb', line 936 def save_config data = dump_config regenerate_file(config_path, 0o400) do |f| f.write(OsCtl::Lib::ConfigFile.dump_yaml(data)) end File.chown(0, 0, config_path) end |
#set(opts) ⇒ Object
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 |
# File 'lib/osctld/container.rb', line 625 def set(opts) opts.each do |k, v| case k when :autostart self.autostart = AutoStart::Config.new(self, v[:priority], v[:delay]) when :ephemeral self.ephemeral = true when :hostname original = nil exclusively do original = @hostname @hostname = OsCtl::Lib::Hostname.new(v) end DistConfig.run(get_run_conf, :set_hostname, original:) when :dns_resolvers DistConfig.run(get_run_conf, :dns_resolvers, resolvers: v) self.dns_resolvers = v when :nesting self.nesting = true when :distribution exclusively do @distribution = v[:name] @version = v[:version] @arch = v[:arch] if v[:arch] @vendor = v[:vendor] @variant = v[:variant] end if (rc = run_conf) rc.set_distribution( distribution:, version:, arch:, vendor:, variant: ) end when :cpu_package self.cpu_package = v when :seccomp_profile self.seccomp_profile = v when :init_cmd self.init_cmd = v when :start_menu self. = Container::StartMenu.new(self, v[:timeout]) when :impermanence self.impermanence = Container::Impermanence.new(v[:zfs_properties].transform_keys(&:to_s)) when :raw_lxc raw_configs.lxc = v when :attrs attrs.update(v) end end save_config lxc_config.configure_base end |
#set_init_pid(pid, expected_run_conf: nil) ⇒ Object
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/osctld/container.rb', line 438 def set_init_pid(pid, expected_run_conf: nil) return unless pid target_run_conf = if expected_run_conf inclusively do next unless @state == :running && @run_conf.equal?(expected_run_conf) expected_run_conf end else ensure_run_conf end return unless target_run_conf target_run_conf.init_pid = pid pid rescue Errno::ESRCH, Container::RunConfiguration::LifecycleError nil end |
#set_next_run_conf(next_run_conf) ⇒ Object
255 256 257 |
# File 'lib/osctld/container.rb', line 255 def set_next_run_conf(next_run_conf) exclusively { @next_run_conf = next_run_conf } end |
#setup_start_menu ⇒ Object
746 747 748 749 |
# File 'lib/osctld/container.rb', line 746 def = .deploy if end |
#starting ⇒ Object
459 460 461 462 463 464 465 466 |
# File 'lib/osctld/container.rb', line 459 def starting exclusively do # Normally {#init_run_conf} is called from {Commands::Container::Start}, # but in case the lxc-start was invoked manually outside of osctld, # initiate the run conf if needed. ensure_run_conf end end |
#stopped(expected_run_conf = nil) ⇒ Object
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
# File 'lib/osctld/container.rb', line 468 def stopped(expected_run_conf = nil) retiring_run_conf = nil accepted = exclusively do if expected_run_conf && !@run_conf.equal?(expected_run_conf) next false end if run_conf retiring_run_conf = @run_conf retiring_run_conf.begin_retirement @past_run_conf = @run_conf @run_conf = nil end true end retiring_run_conf&.destroy accepted end |
#sync_host_link_configuration(&block) ⇒ Object (protected)
Keep this ordering consistent with recovery, which holds the global registry while reading the container's installed netif manager.
1038 1039 1040 |
# File 'lib/osctld/container.rb', line 1038 def sync_host_link_configuration(&block) NetInterface.sync_host_link_registry { exclusively(&block) } end |
#syslogns_tag(run_id: nil) ⇒ Object
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 |
# File 'lib/osctld/container.rb', line 986 def syslogns_tag(run_id: nil) max_size = OsCtl::Lib::Sys::SYSLOGNS_MAX_TAG_BYTESIZE base = if run_id || id.bytesize >= max_size - 1 id else ident end base = base.gsub(/[^A-Za-z0-9_.-]/, '_') base = base.sub(/\A[^A-Za-z0-9]+/, '').sub(/[^A-Za-z0-9]+\z/, '') base = 'ct' if base.empty? if run_id digest = Digest::SHA256.hexdigest(run_id.to_s)[0, 4] prefix = base.byteslice(0, max_size - digest.bytesize - 1) prefix = prefix.sub(/[^A-Za-z0-9]+\z/, '') prefix = 'ct' if prefix.empty? return "#{prefix}-#{digest}" end tag = base.byteslice(0, max_size) tag = tag.sub(/[^A-Za-z0-9]+\z/, '') tag.empty? ? 'ct' : tag end |
#taint_recovery! ⇒ Object
402 403 404 405 |
# File 'lib/osctld/container.rb', line 402 def taint_recovery! exclusively { @recovery_tainted = true } save_config end |
#transfer_in_progress? ⇒ Boolean
827 828 829 |
# File 'lib/osctld/container.rb', line 827 def transfer_in_progress? inclusively { !!send_log || !!local_transfer_log } end |
#transfer_log ⇒ Object
823 824 825 |
# File 'lib/osctld/container.rb', line 823 def transfer_log inclusively { send_log || local_transfer_log } end |
#uid_map ⇒ Object
525 526 527 |
# File 'lib/osctld/container.rb', line 525 def uid_map user.uid_map end |
#unmount(force: false) ⇒ Object
Unmount the container's dataset
296 297 298 299 300 301 |
# File 'lib/osctld/container.rb', line 296 def unmount(force: false) return if !force && !mounted dataset.unmount(recursive: true) self.mounted = false end |
#unregister ⇒ Object
Unregister the container from internal uses in osctld, e.g. on pool export
832 833 834 835 836 |
# File 'lib/osctld/container.rb', line 832 def unregister exclusively do SendReceive::Tokens.free(send_log.token) if send_log end end |
#unset(opts) ⇒ Object
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 |
# File 'lib/osctld/container.rb', line 697 def unset(opts) opts.each do |k, v| case k when :autostart self.autostart = false pool.autostart_plan.stop_ct(self) when :ephemeral self.ephemeral = false when :hostname self.hostname = nil DistConfig.run(get_run_conf, :unset_etc_hosts) when :dns_resolvers DistConfig.run(get_run_conf, :unset_dns_resolvers) self.dns_resolvers = nil when :nesting self.nesting = false when :cpu_package self.cpu_package = 'auto' when :seccomp_profile self.seccomp_profile = default_seccomp_profile when :init_cmd self.init_cmd = nil when :start_menu self. = nil when :impermanence self.impermanence = nil when :raw_lxc raw_configs.lxc = nil when :attrs v.each { |attr| attrs.unset(attr) } end end save_config lxc_config.configure_base end |
#update_hints ⇒ Object
769 770 771 772 |
# File 'lib/osctld/container.rb', line 769 def update_hints hints.account_cpu_use save_config end |
#user_hook_script_dir ⇒ Object
521 522 523 |
# File 'lib/osctld/container.rb', line 521 def user_hook_script_dir inclusively { File.join(pool.root_user_hook_script_dir, 'ct', id) } end |
#wrapper_cgroup_path ⇒ Object
562 563 564 |
# File 'lib/osctld/container.rb', line 562 def wrapper_cgroup_path File.join(base_cgroup_path, 'wrapper') end |