Class: OsCtl::Cli::Container

Inherits:
Command
  • Object
show all
Includes:
Assets, Attributes, CGroupParams, Devices, TransferProgress
Defined in:
lib/osctl/cli/container.rb

Constant Summary collapse

FIELDS =
%i[
  pool
  id
  user
  group
  map_mode
  dataset
  rootfs
  boot_dataset
  boot_rootfs
  lxc_path
  lxc_dir
  group_path
  distribution
  version
  arch
  vendor
  variant
  state
  init_pid
  cpu_package_inuse
  cpu_package_set
  cpu_limit
  memory_limit
  swap_limit
  autostart
  autostart_priority
  autostart_delay
  start_menu
  start_menu_timeout
  impermanence
  impermanence_zfs_properties
  ephemeral
  hostname
  hostname_readout
  dns_resolvers
  nesting
  seccomp_profile
  init_cmd
  raw_lxc
  loadavg
] + CGroupParams::CGPARAM_STATS
FILTERS =
%i[
  pool
  user
  group
  distribution
  version
  arch
  vendor
  variant
  state
].freeze
DEFAULT_FIELDS =
%i[
  pool
  id
  user
  group
  distribution
  version
  state
  init_pid
  memory
  cpu_us
].freeze
PRLIMIT_FIELDS =
%i[
  name
  soft
  hard
].freeze
DATASET_FIELDS =
%i[
  name
  dataset
].freeze
MOUNT_FIELDS =
%i[
  fs
  dataset
  mountpoint
  type
  opts
  automount
  map_ids
  temporary
].freeze

Constants included from Devices

Devices::DEVICES_FIELDS

Constants included from CGroupParams

OsCtl::Cli::CGroupParams::CGPARAM_DEFAULT_FIELDS, OsCtl::Cli::CGroupParams::CGPARAM_FIELDS, OsCtl::Cli::CGroupParams::CGPARAM_FILTERS, OsCtl::Cli::CGroupParams::CGPARAM_STATS

Instance Method Summary collapse

Methods included from TransferProgress

#format_str, #json_progress, #terminal_progress, #with_progress

Methods included from Attributes

#do_set_attr, #do_unset_attr

Methods included from Assets

#print_assets

Methods included from Devices

#do_device_add, #do_device_chmod, #do_device_delete, #do_device_inherit, #do_device_list, #do_device_promote, #do_device_replace, #do_device_set_inherit, #do_device_unset_inherit

Methods included from CGroupParams

#cg_add_raw_cgroup_params, #cg_add_stats, #cg_init_subsystems, #cg_list_raw_cgroup_params, #do_cgparam_apply, #do_cgparam_list, #do_cgparam_replace, #do_cgparam_set, #do_cgparam_unset, #do_set_cpu_limit, #do_set_memory, #do_unset_cpu_limit, #do_unset_memory, #parse_cgparams, #parse_subsystem

Methods inherited from Command

#cli_opt, #format_output, #osctld_call, #osctld_fmt, #osctld_open, #osctld_resp, run

Instance Method Details

#add_loadavg(ct) ⇒ Object (protected)



1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
# File 'lib/osctl/cli/container.rb', line 1866

def add_loadavg(ct)
  if ct[:state] != 'running'
    ct[:loadavg] = nil
    return
  end

  begin
    lavgs = OsCtl::Lib::LoadAvgReader.read_for([ct])
  rescue SystemCallError => e
    warn "Unable to read container load averages: #{e.message} (#{e.class})"
    return
  end

  lavg = lavgs["#{ct[:pool]}:#{ct[:id]}"]
  ct[:loadavg] = lavg ? lavg.averages : nil
end

#add_loadavgs(cts) ⇒ Object (protected)



1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
# File 'lib/osctl/cli/container.rb', line 1883

def add_loadavgs(cts)
  begin
    lavgs = OsCtl::Lib::LoadAvgReader.read_for(cts)
  rescue SystemCallError => e
    warn "Unable to read container load averages: #{e.message} (#{e.class})"
    return
  end

  cts.each do |ct|
    if ct[:state] != 'running'
      ct[:loadavg] = nil
      next
    end

    lavg = lavgs["#{ct[:pool]}:#{ct[:id]}"]
    ct[:loadavg] = lavg ? lavg.averages : nil
  end
end

#assets ⇒ Object



1280
1281
1282
1283
1284
# File 'lib/osctl/cli/container.rb', line 1280

def assets
  require_args!('id')

  print_assets(:ct_assets, id: args[0], pool: gopts[:pool])
end

#attach ⇒ Object



488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/osctl/cli/container.rb', line 488

def attach
  require_args!('id')

  cmd = osctld_call(
    :ct_attach,
    id: args[0],
    pool: gopts[:pool],
    user_shell: opts['user-shell']
  )

  handle_ct_attach(cmd)
end

#bisect ⇒ Object



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
1156
1157
1158
1159
1160
1161
1162
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
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
# File 'lib/osctl/cli/container.rb', line 1131

def bisect
  conn = osctld_open
  cg_init_subsystems(conn)

  cgparams = cg_list_raw_cgroup_params.map(&:to_sym)

  param_selector = OsCtl::Lib::Cli::ParameterSelector.new(
    all_params: FIELDS + cgparams,
    default_params: DEFAULT_FIELDS
  )

  if opts[:list]
    puts param_selector
    return
  end

  cmd_opts = {
    state: 'running'
  }

  FILTERS.each do |v|
    [gopts, opts].each do |options|
      next unless options[v]

      cmd_opts[v] = options[v].split(',')
    end
  end

  if opts[:ephemeral]
    cmd_opts[:ephemeral] = true
  elsif opts[:persistent]
    cmd_opts[:ephemeral] = false
  end

  cmd_opts[:ids] = args if args.count > 0

  cts = conn.cmd_data!(:ct_list, **cmd_opts)

  if opts[:exclude]
    exclude_ctids = opts[:exclude].split(',').map do |v|
      if v.index(':')
        pool, id = v.split(':')
        [pool, id]
      else
        [nil, v]
      end
    end

    cts.reject! do |ct|
      exclude_ctids.detect do |ex_pool, ex_id|
        (ex_pool.nil? || ct[:pool] == ex_pool) && ct[:id] == ex_id
      end
    end
  end

  cols = param_selector.parse_option(opts[:output])

  cg_add_stats(
    cts,
    ->(ct) { ct[:group_path] },
    cols,
    gopts[:parsable]
  )

  add_loadavgs(cts)

  cg_add_raw_cgroup_params(
    cts,
    ->(ct) { ct[:group_path] },
    cols & cgparams
  )

  if opts[:sort]
    sort_cols = param_selector.parse_option(opts[:sort])

    sort_cols.each do |c|
      unless all_fields.include?(c)
        raise GLI::BadCommandLine, "unknown sort parameter '#{c}'"
      end
    end

    cts.sort! do |a, b|
      a_vals = sort_cols.map { |c| a[c] }
      b_vals = sort_cols.map { |c| b[c] }
      cmp = a_vals <=> b_vals
      next(cmp) if cmp

      next(-1) if [nil, false].detect { |v| a_vals.include?(v) }
      next(1) if [nil, false].detect { |v| b_vals.include?(v) }

      0
    end
  end

  bis = Bisect.new(
    cts,
    suspend_action: opts[:action].to_sym,
    cols:
  )
  bis.run
end

#boot ⇒ Object



974
975
976
977
978
979
980
981
982
983
984
985
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
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
# File 'lib/osctl/cli/container.rb', line 974

def boot
  require_args!('id')

  if opts[:foreground] && opts[:attach]
    raise GLI::BadCommandLine, 'use either --foreground or --attach'
  end

  cmd_opts = {
    id: args[0],
    pool: opts[:pool] || gopts[:pool],
    repository: opts[:repository],
    force: opts[:force],
    mount_root: opts['mount-root-dataset'],
    zfs_properties: opts['zfs-property'].to_h do |v|
      k, v = v.split('=')
      raise GLI::BadCommandLine, "invalid ZFS property '#{v}'" if v.nil?

      [k, v]
    end,
    wait: get_ct_wait(opts[:wait]),
    queue: opts[:queue],
    priority: opts[:priority],
    debug: opts[:debug]
  }

  if opts['from-file']
    cmd_opts.update(
      type: :image,
      path: File.absolute_path(opts['from-file'])
    )
  else
    cmd_opts.update(
      type: :remote,
      image: repo_image_attrs(defaults: false)
    )
  end

  if opts[:foreground]
    open_console(args[0], gopts[:pool], 0, gopts[:json]) do |sock|
      sock.close if osctld_resp(:ct_boot, **cmd_opts).error?
    end

    return
  end

  osctld_fmt(:ct_boot, cmd_opts:)

  return unless opts[:attach]

  puts 'Attaching'
  attach
end

#cat ⇒ Object



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/osctl/cli/container.rb', line 544

def cat
  require_args!('id', 'file', strict: false)

  c = osctld_open
  cont = c.cmd_data!(
    :ct_cat,
    id: args[0],
    pool: gopts[:pool],
    files: args[1..]
  )

  if cont != 'continue'
    warn "cat not available: invalid response '#{cont}'"
    exit(false)
  end

  c.send_io($stdout)
  resp = c.response!
  return if resp[:errors].empty?

  resp[:errors].each do |file, error|
    warn "'#{file}': #{error}"
  end

  raise 'failed to read one or more files'
end

#cgparam_apply ⇒ Object



1352
1353
1354
1355
# File 'lib/osctl/cli/container.rb', line 1352

def cgparam_apply
  require_args!('id')
  do_cgparam_apply(:ct_cgparam_apply, id: args[0], pool: gopts[:pool])
end

#cgparam_list ⇒ Object



1336
1337
1338
1339
1340
# File 'lib/osctl/cli/container.rb', line 1336

def cgparam_list
  require_args!('id', strict: false)

  do_cgparam_list(:ct_cgparam_list, id: args[0], pool: gopts[:pool])
end

#cgparam_replace ⇒ Object



1357
1358
1359
1360
# File 'lib/osctl/cli/container.rb', line 1357

def cgparam_replace
  require_args!('name')
  do_cgparam_replace(:ct_cgparam_replace, id: args[0], pool: gopts[:pool])
end

#cgparam_set ⇒ Object



1342
1343
1344
1345
# File 'lib/osctl/cli/container.rb', line 1342

def cgparam_set
  require_args!('id', 'parameter', 'value', strict: false)
  do_cgparam_set(:ct_cgparam_set, id: args[0], pool: gopts[:pool])
end

#cgparam_unset ⇒ Object



1347
1348
1349
1350
# File 'lib/osctl/cli/container.rb', line 1347

def cgparam_unset
  require_args!('id', 'parameter')
  do_cgparam_unset(:ct_cgparam_unset, id: args[0], pool: gopts[:pool])
end

#chgrp ⇒ Object



964
965
966
967
968
969
970
971
972
# File 'lib/osctl/cli/container.rb', line 964

def chgrp
  require_args!('id', 'group')
  osctld_fmt(:ct_chgrp, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    group: args[1],
    missing_devices: opts['missing-devices']
  })
end

#chown ⇒ Object



955
956
957
958
959
960
961
962
# File 'lib/osctl/cli/container.rb', line 955

def chown
  require_args!('id', 'user')
  osctld_fmt(:ct_chown, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    user: args[1]
  })
end

#config_reload ⇒ Object



1027
1028
1029
1030
1031
1032
1033
# File 'lib/osctl/cli/container.rb', line 1027

def config_reload
  require_args!('id')
  osctld_fmt(:ct_cfg_reload, cmd_opts: {
    id: args[0],
    pool: gopts[:pool]
  })
end

#config_replace ⇒ Object



1035
1036
1037
1038
1039
1040
1041
1042
# File 'lib/osctl/cli/container.rb', line 1035

def config_replace
  require_args!('id')
  osctld_fmt(:ct_cfg_replace, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    config: $stdin.read
  })
end

#console ⇒ Object



482
483
484
485
486
# File 'lib/osctl/cli/container.rb', line 482

def console
  require_args!('id')

  open_console(args[0], gopts[:pool], opts[:tty], gopts[:json])
end

#copy ⇒ Object



853
854
855
856
857
858
859
860
861
# File 'lib/osctl/cli/container.rb', line 853

def copy
  with_progress(
    :ct_copy,
    progress_title: 'Copying',
    **copy_config_opts,
    consistent: opts[:consistent],
    restart: opts[:restart]
  )
end

#copy_cancel ⇒ Object



894
895
896
897
898
899
900
901
902
903
# File 'lib/osctl/cli/container.rb', line 894

def copy_cancel
  require_args!('id')
  with_progress(
    :ct_copy_cancel,
    progress_title: 'Copying',
    pool: gopts[:pool],
    id: args[0],
    force: opts[:force]
  )
end

#copy_cleanup ⇒ Object



889
890
891
892
# File 'lib/osctl/cli/container.rb', line 889

def copy_cleanup
  require_args!('id')
  with_progress(:ct_copy_cleanup, progress_title: 'Copying', pool: gopts[:pool], id: args[0])
end

#copy_config ⇒ Object



863
864
865
# File 'lib/osctl/cli/container.rb', line 863

def copy_config
  with_progress(:ct_copy_config, progress_title: 'Copying', **copy_config_opts)
end

#copy_config_opts ⇒ Object



1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
# File 'lib/osctl/cli/container.rb', line 1739

def copy_config_opts
  target_pool, target_id = parse_target_arg

  cmd_opts = {
    pool: gopts[:pool],
    id: args[0],
    target_pool:,
    target_id:,
    network_interfaces: opts['network-interfaces']
  }

  cmd_opts[:target_user] = opts[:user] if opts[:user]
  cmd_opts[:target_group] = opts[:group] if opts[:group]
  cmd_opts[:target_dataset] = opts[:dataset] if opts[:dataset]
  cmd_opts[:from_snapshot] = opts['from-snapshot'] if opts['from-snapshot']
  cmd_opts
end

#copy_rootfs ⇒ Object



867
868
869
870
# File 'lib/osctl/cli/container.rb', line 867

def copy_rootfs
  require_args!('id')
  with_progress(:ct_copy_rootfs, progress_title: 'Copying', pool: gopts[:pool], id: args[0])
end

#copy_state ⇒ Object



877
878
879
880
881
882
883
884
885
886
887
# File 'lib/osctl/cli/container.rb', line 877

def copy_state
  require_args!('id')
  with_progress(
    :ct_copy_state,
    progress_title: 'Copying',
    pool: gopts[:pool],
    id: args[0],
    consistent: opts[:consistent],
    restart: opts[:restart]
  )
end

#copy_sync ⇒ Object



872
873
874
875
# File 'lib/osctl/cli/container.rb', line 872

def copy_sync
  require_args!('id')
  with_progress(:ct_copy_sync, progress_title: 'Copying', pool: gopts[:pool], id: args[0])
end

#create ⇒ Object



295
296
297
298
299
300
301
302
303
# File 'lib/osctl/cli/container.rb', line 295

def create
  require_args!('id')

  if opts['skip-image']
    create_empty
  else
    create_with_remote_image
  end
end

#create_empty ⇒ Object (protected)



1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
# File 'lib/osctl/cli/container.rb', line 1805

def create_empty
  if !opts[:distribution]
    raise GLI::BadCommandLine, 'provide --distribution'
  elsif !opts[:version]
    raise GLI::BadCommandLine, 'provide --version'
  elsif !opts[:arch]
    raise GLI::BadCommandLine, 'provide --arch'
  end

  cmd_opts = {
    id: args[0],
    pool: opts[:pool] || gopts[:pool],
    user: opts[:user],
    distribution: opts[:distribution],
    version: opts[:version],
    arch: opts[:arch]
  }

  %i[group dataset map_mode].each do |v|
    cmd_opts[v] = opts[v] if opts[v]
  end

  osctld_fmt(:ct_create_empty, cmd_opts:)
end

#create_with_remote_image ⇒ Object (protected)



1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
# File 'lib/osctl/cli/container.rb', line 1776

def create_with_remote_image
  cmd_opts = {
    id: args[0],
    pool: opts[:pool] || gopts[:pool],
    user: opts[:user],
    repository: opts[:repository],
    zfs_properties: opts['zfs-property'].to_h do |v|
      k, v = v.split('=')
      raise GLI::BadCommandLine, "invalid ZFS property '#{v}'" if v.nil?

      [k, v]
    end
  }

  %i[group dataset].each do |v|
    cmd_opts[v] = opts[v] if opts[v]
  end

  cmd_opts[:map_mode] = opts['map-mode'] if opts['map-mode']

  unless opts[:distribution]
    raise GLI::BadCommandLine, 'provide --distribution'
  end

  cmd_opts[:image] = repo_image_attrs

  osctld_fmt(:ct_create, cmd_opts:)
end

#dataset_create ⇒ Object



1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
# File 'lib/osctl/cli/container.rb', line 1480

def dataset_create
  require_args!('id', 'name', optional: %w[mountpoint])
  osctld_fmt(:ct_dataset_create, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    name: args[1],
    mount: opts[:mount],
    mountpoint: args[2]
  })
end

#dataset_delete ⇒ Object



1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
# File 'lib/osctl/cli/container.rb', line 1491

def dataset_delete
  require_args!('id', 'name')
  osctld_fmt(:ct_dataset_delete, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    name: args[1],
    recursive: opts[:recursive],
    unmount: opts[:unmount]
  })
end

#dataset_list ⇒ Object



1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
# File 'lib/osctl/cli/container.rb', line 1457

def dataset_list
  param_selector = OsCtl::Lib::Cli::ParameterSelector.new(
    all_params: DATASET_FIELDS,
    default_params: DATASET_FIELDS
  )

  if opts[:list]
    puts param_selector
    return
  end

  require_args!('id', strict: false)
  props = args[1..]

  cmd_opts = { id: args[0], pool: gopts[:pool], properties: props }
  fmt_opts = { layout: :columns }

  fmt_opts[:header] = false if opts['hide-header']
  fmt_opts[:cols] = param_selector.parse_option(opts[:output])

  osctld_fmt(:ct_dataset_list, cmd_opts:, fmt_opts:)
end

#delete ⇒ Object



305
306
307
308
309
310
311
312
313
314
# File 'lib/osctl/cli/container.rb', line 305

def delete
  require_args!('id')

  osctld_fmt(:ct_delete, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    force: opts[:force],
    prune: opts[:prune]
  })
end

#device_add ⇒ Object



1367
1368
1369
1370
# File 'lib/osctl/cli/container.rb', line 1367

def device_add
  require_args!('id', 'type', 'major', 'minor', 'mode', optional: %w[device])
  do_device_add(:ct_device_add, id: args[0], pool: gopts[:pool])
end

#device_chmod ⇒ Object



1377
1378
1379
1380
# File 'lib/osctl/cli/container.rb', line 1377

def device_chmod
  require_args!('id', 'type', 'major', 'minor', 'mode')
  do_device_chmod(:ct_device_chmod, id: args[0], pool: gopts[:pool])
end

#device_delete ⇒ Object



1372
1373
1374
1375
# File 'lib/osctl/cli/container.rb', line 1372

def device_delete
  require_args!('id', 'type', 'major', 'minor')
  do_device_delete(:ct_device_delete, id: args[0], pool: gopts[:pool])
end

#device_inherit ⇒ Object



1387
1388
1389
1390
# File 'lib/osctl/cli/container.rb', line 1387

def device_inherit
  require_args!('id', 'type', 'major', 'minor')
  do_device_inherit(:ct_device_inherit, id: args[0], pool: gopts[:pool])
end

#device_list ⇒ Object



1362
1363
1364
1365
# File 'lib/osctl/cli/container.rb', line 1362

def device_list
  require_args!('id')
  do_device_list(:ct_device_list, id: args[0], pool: gopts[:pool])
end

#device_promote ⇒ Object



1382
1383
1384
1385
# File 'lib/osctl/cli/container.rb', line 1382

def device_promote
  require_args!('id', 'type', 'major', 'minor')
  do_device_promote(:ct_device_promote, id: args[0], pool: gopts[:pool])
end

#device_replace ⇒ Object



1402
1403
1404
1405
# File 'lib/osctl/cli/container.rb', line 1402

def device_replace
  require_args!('id')
  do_device_replace(:ct_device_replace, id: args[0], pool: gopts[:pool])
end

#device_set_inherit ⇒ Object



1392
1393
1394
1395
# File 'lib/osctl/cli/container.rb', line 1392

def device_set_inherit
  require_args!('id', 'type', 'major', 'minor')
  do_device_set_inherit(:ct_device_set_inherit, id: args[0], pool: gopts[:pool])
end

#device_unset_inherit ⇒ Object



1397
1398
1399
1400
# File 'lib/osctl/cli/container.rb', line 1397

def device_unset_inherit
  require_args!('id', 'type', 'major', 'minor')
  do_device_unset_inherit(:ct_device_unset_inherit, id: args[0], pool: gopts[:pool])
end

#exec ⇒ Object



501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/osctl/cli/container.rb', line 501

def exec
  require_args!('id', 'command', strict: false)

  c = osctld_open
  cont = c.cmd_data!(
    :ct_exec,
    id: args[0],
    pool: gopts[:pool],
    cmd: args[1..],
    run: opts['run-container'],
    network: opts['network']
  )

  if cont != 'continue'
    warn "exec not available: invalid response '#{cont}'"
    exit(false)
  end

  handle_exec_io(c)
end

#export ⇒ Object



1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
# File 'lib/osctl/cli/container.rb', line 1063

def export
  require_args!('id', 'file')

  osctld_fmt(:ct_export, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    file: File.expand_path(args[1]),
    consistent: opts[:consistent],
    compression: opts[:compression]
  })
end

#filter_ugids(raw_ids) ⇒ Object (protected)



2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
# File 'lib/osctl/cli/container.rb', line 2028

def filter_ugids(raw_ids)
  ret = []

  raw_ids.each do |raw_id|
    stripped = raw_id.strip

    if /\A\d+\z/ !~ stripped
      warn "Ignoring #{stripped.inspect}, not a valid user/group ID"
      next
    end

    id = stripped.to_i

    if id < 0
      warn "Ignoring #{id}, not a valid user/group ID"
      next
    end

    ret << id
  end

  ret
end

#freeze ⇒ Object



1121
1122
1123
1124
# File 'lib/osctl/cli/container.rb', line 1121

def freeze
  require_args!('id')
  osctld_fmt(:ct_freeze, cmd_opts: { id: args[0], pool: gopts[:pool] })
end

#get_ct_wait(v) ⇒ Object (protected)



1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
# File 'lib/osctl/cli/container.rb', line 1902

def get_ct_wait(v)
  if v == 'infinity'
    return v
  elsif v.is_a?(::String) && /^\d+$/ !~ v
    raise GLI::BadCommandLine, 'invalid value for --wait'
  end

  v_i = v.to_i

  if v_i < 0
    raise GLI::BadCommandLine, 'invalid value for --wait'
  elsif v_i == 0 || opts[:foreground]
    false
  else
    v_i
  end
end

#gid ⇒ Object



1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
# File 'lib/osctl/cli/container.rb', line 1263

def gid
  require_args!('gid|-', strict: false)

  raw_gids = []

  if args[0] == '-'
    raw_gids << $stdin.readline.strip until $stdin.eof?
  else
    raw_gids = args
  end

  gids = filter_ugids(raw_gids)

  finder = UgidFinder.new(header: !opts['hide-header'])
  finder.list_by_gid(gids)
end

#handle_ct_attach(cmd) ⇒ Object (protected)



2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
# File 'lib/osctl/cli/container.rb', line 2001

def handle_ct_attach(cmd)
  f = Tempfile.create(['osctl-ct-attach-settings', '.json'], '/tmp')
  f.puts(cmd[:settings].to_json)
  f.close

  pid = Process.fork do
    cmd[:env].each do |k, v|
      ENV[k.to_s] = v
    end

    Process.exec(cmd[:cmd], f.path, '--', *cmd[:args])
  end

  _, status = Process.wait2(pid)

  return if status.success?

  code = status.signaled? ? 128 + status.termsig : (status.exitstatus || 1)
  raise GLI::CustomExit.new('attached command failed', code)
ensure
  begin
    f && File.unlink(f.path)
  rescue Errno::ENOENT
    # pass
  end
end

#handle_exec_io(c) ⇒ Object (protected)



1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
# File 'lib/osctl/cli/container.rb', line 1920

def handle_exec_io(c)
  r_in, w_in = IO.pipe
  r_out, w_out = IO.pipe
  r_err, w_err = IO.pipe

  c.send_io(r_in)
  c.send_io(w_out)
  c.send_io(w_err)

  r_in.close
  w_out.close
  w_err.close

  outputs = { r_out => $stdout, r_err => $stderr }
  watch_ios = [$stdin, r_out, r_err, c.socket]

  loop do
    rs, = IO.select(watch_ios)

    rs.each do |r|
      case r
      when r_out, r_err
        watch_ios.delete(r) unless read_exec_output(r, outputs.fetch(r))

      when $stdin
        begin
          data = r.read_nonblock(4096)
          w_in.write(data)
        rescue EOFError, Errno::EPIPE
          w_in.close
          watch_ios.delete($stdin)
        rescue IO::WaitReadable
          next
        end

      when c.socket
        # The command has completed, but either output pipe can still
        # contain data. Drain both before handling its response. Do not
        # wait for EOF or follow new writes from background descendants.
        outputs.each { |io, output| read_exec_output(io, output, limit: io.nread) }

        handle_exec_response(c)
        return # rubocop:disable Lint/NonLocalExitFromIterator
      end
    end
  end
ensure
  [r_in, w_in, r_out, w_out, r_err, w_err].compact.each do |io|
    io.close unless io.closed?
  end
end

#handle_exec_response(c) ⇒ Object (protected)



1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
# File 'lib/osctl/cli/container.rb', line 1987

def handle_exec_response(c)
  resp = c.receive_resp

  if resp.error?
    raise(resp.message || 'exec failed')

  elsif !resp.data || !resp.data.has_key?(:exitstatus)
    raise 'exec failed: osctld returned no exit status'

  elsif resp[:exitstatus] && resp[:exitstatus] > 0
    raise GLI::CustomExit.new('executed command failed', resp[:exitstatus])
  end
end

#import ⇒ Object



1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
# File 'lib/osctl/cli/container.rb', line 1075

def import
  require_args!('file')

  file = File.expand_path(args[0])
  raise "#{file}: not found" unless File.exist?(file)

  cmd_opts = {
    pool: gopts[:pool],
    file:,
    zfs_properties: opts['zfs-property'].to_h do |v|
      k, v = v.split('=')
      raise GLI::BadCommandLine, "invalid ZFS property '#{v}'" if v.nil?

      [k, v]
    end
  }

  %w[as-id as-user as-group dataset map_mode missing-devices].each do |v|
    cmd_opts[v.sub('-', '_').to_sym] = opts[v] if opts[v]
  end

  osctld_fmt(:ct_import, cmd_opts:)
end

#list ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/osctl/cli/container.rb', line 112

def list
  c = osctld_open
  cg_init_subsystems(c)

  cgparams = cg_list_raw_cgroup_params
  zfsprops = ZfsProperties.new
  keyring = KernelKeyring.new

  param_selector = OsCtl::Lib::Cli::ParameterSelector.new(
    all_params: FIELDS + cgparams + zfsprops.list_property_names + keyring.list_param_names,
    default_params: DEFAULT_FIELDS
  )

  if opts[:list]
    puts param_selector
    return
  end

  sort = opts[:sort] && param_selector.parse_option(opts[:sort])

  cmd_opts = {}
  fmt_opts = {
    layout: :columns,
    sort:,
    opts: {
      memory_limit: {
        align: 'right',
        display: proc do |v|
          if v.nil? || gopts[:parsable] || gopts[:json]
            v
          else
            humanize_data(v)
          end
        end
      },
      swap_limit: {
        align: 'right',
        display: proc do |v|
          if v.nil? || gopts[:parsable] || gopts[:json]
            v
          else
            humanize_data(v)
          end
        end
      }
    }
  }

  FILTERS.each do |v|
    [gopts, opts].each do |options|
      next unless options[v]

      cmd_opts[v] = options[v].split(',')
    end
  end

  if opts[:ephemeral]
    cmd_opts[:ephemeral] = true
  elsif opts[:persistent]
    cmd_opts[:ephemeral] = false
  end

  cmd_opts[:ids] = args if args.count > 0
  fmt_opts[:header] = false if opts['hide-header']

  cols = param_selector.parse_option(opts[:output])
  cols = zfsprops.validate_property_names(cols)

  if sort
    sort.each do |v|
      cols << v unless cols.include?(v)
    end
  end

  fmt_opts[:cols] = cols

  if cols.include?(:hostname_readout)
    cmd_opts[:read_hostname] = true
  end

  cts = cg_add_stats(
    c.cmd_data!(:ct_list, **cmd_opts),
    ->(ct) { ct[:group_path] },
    cols,
    gopts[:parsable]
  )

  add_loadavgs(cts)

  cg_add_raw_cgroup_params(
    cts,
    ->(ct) { ct[:group_path] },
    cols & cgparams.map(&:to_sym)
  )

  zfsprops.add_container_values(cts, cols, precise: gopts[:parsable])
  keyring.add_container_values(cts, cols, precise: gopts[:parsable])

  format_output(cts, **fmt_opts)
end

#log_cat ⇒ Object



1099
1100
1101
1102
1103
1104
1105
1106
1107
# File 'lib/osctl/cli/container.rb', line 1099

def log_cat
  require_args!('id')

  ct = osctld_call(:ct_show, id: args[0], pool: gopts[:pool])

  File.open(ct[:log_file]) do |f|
    puts f.readline until f.eof?
  end
end

#log_path ⇒ Object



1109
1110
1111
1112
1113
1114
# File 'lib/osctl/cli/container.rb', line 1109

def log_path
  require_args!('id')

  ct = osctld_call(:ct_show, id: args[0], pool: gopts[:pool])
  puts ct[:log_file]
end

#mount ⇒ Object



341
342
343
344
# File 'lib/osctl/cli/container.rb', line 341

def mount
  require_args!('id')
  osctld_fmt(:ct_mount, cmd_opts: { id: args[0], pool: gopts[:pool] })
end

#mount_activate ⇒ Object



1577
1578
1579
1580
1581
1582
1583
1584
1585
# File 'lib/osctl/cli/container.rb', line 1577

def mount_activate
  require_args!('id', 'mountpoint')

  osctld_fmt(:ct_mount_activate, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    mountpoint: args[1]
  })
end

#mount_clear ⇒ Object



1607
1608
1609
1610
1611
1612
1613
1614
# File 'lib/osctl/cli/container.rb', line 1607

def mount_clear
  require_args!('id')

  osctld_fmt(:ct_mount_clear, cmd_opts: {
    id: args[0],
    pool: gopts[:pool]
  })
end

#mount_create ⇒ Object



1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
# File 'lib/osctl/cli/container.rb', line 1524

def mount_create
  require_args!('id')

  osctld_fmt(:ct_mount_create, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    fs: opts[:fs],
    mountpoint: opts[:mountpoint],
    type: opts[:type],
    opts: opts[:opts],
    automount: opts[:automount],
    map_ids: opts[:map_ids]
  })
end

#mount_dataset ⇒ Object



1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
# File 'lib/osctl/cli/container.rb', line 1539

def mount_dataset
  require_args!('id', 'name', 'mountpoint')

  if opts[:ro] && opts[:rw]
    raise GLI::BadCommandLine, 'use either --ro or --rw, not both'

  elsif opts[:ro]
    mode = 'ro'

  else
    mode = 'rw'
  end

  osctld_fmt(:ct_mount_dataset, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    name: args[1],
    mountpoint: args[2],
    mode:,
    automount: opts[:automount]
  })
end

#mount_deactivate ⇒ Object



1587
1588
1589
1590
1591
1592
1593
1594
1595
# File 'lib/osctl/cli/container.rb', line 1587

def mount_deactivate
  require_args!('id', 'mountpoint')

  osctld_fmt(:ct_mount_deactivate, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    mountpoint: args[1]
  })
end

#mount_delete ⇒ Object



1597
1598
1599
1600
1601
1602
1603
1604
1605
# File 'lib/osctl/cli/container.rb', line 1597

def mount_delete
  require_args!('id', 'mountpoint')

  osctld_fmt(:ct_mount_delete, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    mountpoint: args[1]
  })
end

#mount_list ⇒ Object



1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
# File 'lib/osctl/cli/container.rb', line 1502

def mount_list
  param_selector = OsCtl::Lib::Cli::ParameterSelector.new(
    all_params: MOUNT_FIELDS,
    default_params: MOUNT_FIELDS
  )

  if opts[:list]
    puts param_selector
    return
  end

  require_args!('id')

  cmd_opts = { id: args[0], pool: gopts[:pool] }
  fmt_opts = { layout: :columns }

  fmt_opts[:header] = false if opts['hide-header']
  fmt_opts[:cols] = param_selector.parse_option(opts[:output])

  osctld_fmt(:ct_mount_list, cmd_opts:, fmt_opts:)
end

#mount_register ⇒ Object



1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
# File 'lib/osctl/cli/container.rb', line 1562

def mount_register
  require_args!('id', 'mountpoint')

  osctld_fmt(:ct_mount_register, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    fs: opts[:fs],
    mountpoint: args[1],
    type: opts[:type],
    opts: opts[:opts],
    map_ids: opts[:map_ids],
    lock: !opts['on-ct-start']
  })
end

#move ⇒ Object



905
906
907
908
909
910
911
912
# File 'lib/osctl/cli/container.rb', line 905

def move
  with_progress(
    :ct_move,
    progress_title: 'Moving',
    **move_config_opts,
    start: opts[:start]
  )
end

#move_cancel ⇒ Object



944
945
946
947
948
949
950
951
952
953
# File 'lib/osctl/cli/container.rb', line 944

def move_cancel
  require_args!('id')
  with_progress(
    :ct_move_cancel,
    progress_title: 'Moving',
    pool: gopts[:pool],
    id: args[0],
    force: opts[:force]
  )
end

#move_cleanup ⇒ Object



939
940
941
942
# File 'lib/osctl/cli/container.rb', line 939

def move_cleanup
  require_args!('id')
  with_progress(:ct_move_cleanup, progress_title: 'Moving', pool: gopts[:pool], id: args[0])
end

#move_config ⇒ Object



914
915
916
# File 'lib/osctl/cli/container.rb', line 914

def move_config
  with_progress(:ct_move_config, progress_title: 'Moving', **move_config_opts)
end

#move_config_opts ⇒ Object



1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
# File 'lib/osctl/cli/container.rb', line 1757

def move_config_opts
  target_pool, target_id = parse_target_arg

  cmd_opts = {
    pool: gopts[:pool],
    id: args[0],
    target_pool:,
    target_id:,
    network_interfaces: true
  }

  cmd_opts[:target_user] = opts[:user] if opts[:user]
  cmd_opts[:target_group] = opts[:group] if opts[:group]
  cmd_opts[:target_dataset] = opts[:dataset] if opts[:dataset]
  cmd_opts
end

#move_rootfs ⇒ Object



918
919
920
921
# File 'lib/osctl/cli/container.rb', line 918

def move_rootfs
  require_args!('id')
  with_progress(:ct_move_rootfs, progress_title: 'Moving', pool: gopts[:pool], id: args[0])
end

#move_state ⇒ Object



928
929
930
931
932
933
934
935
936
937
# File 'lib/osctl/cli/container.rb', line 928

def move_state
  require_args!('id')
  with_progress(
    :ct_move_state,
    progress_title: 'Moving',
    pool: gopts[:pool],
    id: args[0],
    start: opts[:start]
  )
end

#move_sync ⇒ Object



923
924
925
926
# File 'lib/osctl/cli/container.rb', line 923

def move_sync
  require_args!('id')
  with_progress(:ct_move_sync, progress_title: 'Moving', pool: gopts[:pool], id: args[0])
end

#oomd_action(action) ⇒ Object



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/osctl/cli/container.rb', line 378

def oomd_action(action)
  require_args!('id')
  %w[expected-init-pid expected-init-start-time].each do |flag|
    value = opts[flag]
    unless value.is_a?(Integer) && value > 0
      raise GLI::BadCommandLine, "--#{flag} must be a positive integer"
    end
  end
  osctld_fmt(
    :"ct_oomd_#{action}",
    cmd_opts: {
      id: args[0], pool: gopts[:pool],
      expected_init_pid: opts['expected-init-pid'],
      expected_init_start_time: opts['expected-init-start-time'],
      wait: 120
    }
  ) do |message|
    next if gopts[:quiet]

    puts message
    $stdout.flush
  end
end

#open_console(ctid, pool, tty, raw) ⇒ Object



1286
1287
1288
1289
1290
1291
1292
1293
# File 'lib/osctl/cli/container.rb', line 1286

def open_console(ctid, pool, tty, raw, &)
  if raw
    open_console_raw(ctid, pool, tty)

  else
    open_console_tty(ctid, pool, tty, &)
  end
end

#open_console_raw(ctid, pool, tty) ⇒ Object



1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
# File 'lib/osctl/cli/container.rb', line 1323

def open_console_raw(ctid, pool, tty)
  c = osctld_open
  c.cmd_response!(:ct_console, id: ctid, pool:, tty:)

  console = OsCtl::Console.new(c.socket, $stdin, $stdout, raw: true)

  Signal.trap('TERM') do
    console.close
  end

  console.open
end

#open_console_tty(ctid, pool, tty) {|c| ... } ⇒ Object

Yields:

  • (c)


1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
# File 'lib/osctl/cli/container.rb', line 1295

def open_console_tty(ctid, pool, tty)
  c = osctld_open
  c.cmd_response!(:ct_console, id: ctid, pool:, tty:)

  puts 'Press Ctrl+a q to detach the console'
  puts

  state = `stty -g`
  `stty raw -echo -icanon -isig`

  pid = Process.fork do
    console = OsCtl::Console.new(c.socket, $stdin, $stdout)

    Signal.trap('WINCH') do
      console.resize(*$stdin.winsize)
    end

    console.open
  end

  yield(c) if block_given?

  Process.wait(pid)

  `stty #{state}`
  puts
end

#parse_target_arg ⇒ Object



1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
# File 'lib/osctl/cli/container.rb', line 1726

def parse_target_arg
  require_args!('id', 'new-id')

  if args[1].include?(':')
    target_pool, target_id = args[1].split(':', 2)
  else
    target_pool = opts[:pool]
    target_id = args[1]
  end

  [target_pool, target_id]
end

#passwd ⇒ Object



1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
# File 'lib/osctl/cli/container.rb', line 1044

def passwd
  require_args!('id', 'user', optional: %w[password])

  if args[2]
    password = args[2]

  else
    cli = HighLine.new
    password = cli.ask('Password: ') { |q| q.echo = false }.strip
  end

  osctld_fmt(:ct_passwd, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    user: args[1],
    password:
  })
end

#pid ⇒ Object



1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
# File 'lib/osctl/cli/container.rb', line 1233

def pid
  require_args!('pid|-', strict: false)

  finder = PidFinder.new(header: !opts['hide-header'])

  if args[0] == '-'
    finder.find($stdin.readline.strip.to_i) until $stdin.eof?

  else
    args.each { |pid| finder.find(pid.to_i) }
  end
end

#prlimit_list ⇒ Object



1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
# File 'lib/osctl/cli/container.rb', line 1407

def prlimit_list
  param_selector = OsCtl::Lib::Cli::ParameterSelector.new(
    all_params: PRLIMIT_FIELDS
  )

  if opts[:list]
    puts param_selector
    return
  end

  require_args!('id', strict: false)

  cmd_opts = { id: args[0], pool: gopts[:pool] }
  fmt_opts = { layout: :columns }

  cmd_opts[:limits] = args[1..] if args.count > 1
  fmt_opts[:header] = false if opts['hide-header']

  fmt_opts[:cols] = param_selector.parse_option(opts[:output])

  data = osctld_call(:ct_prlimit_list, **cmd_opts)
  format_output(data.map { |k, v| v.merge(name: k) }, **fmt_opts)
end

#prlimit_set ⇒ Object



1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
# File 'lib/osctl/cli/container.rb', line 1431

def prlimit_set
  require_args!('id', 'limit', 'value', optional: %w[hard])

  soft, hard = args[2..3].map { |v| /^\d+$/ =~ v ? v.to_i : v }
  hard = soft if hard.nil?

  osctld_fmt(:ct_prlimit_set, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    name: args[1],
    soft:,
    hard:
  })
end

#prlimit_unset ⇒ Object



1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
# File 'lib/osctl/cli/container.rb', line 1446

def prlimit_unset
  require_args!('id', 'limit')

  do_cgparam_unset(
    :ct_prlimit_unset,
    id: args[0],
    pool: gopts[:pool],
    name: args[1]
  )
end

#read_exec_output(io, output, limit: 4096) ⇒ Object (protected)



1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
# File 'lib/osctl/cli/container.rb', line 1972

def read_exec_output(io, output, limit: 4096)
  while limit > 0
    data = io.read_nonblock([limit, 4096].min)
    output.write(data)
    output.flush
    limit -= data.bytesize
  end

  true
rescue IO::WaitReadable
  true
rescue EOFError
  false
end

#reconfigure ⇒ Object



1116
1117
1118
1119
# File 'lib/osctl/cli/container.rb', line 1116

def reconfigure
  require_args!('id')
  osctld_fmt(:ct_reconfigure, cmd_opts: { id: args[0], pool: gopts[:pool] })
end

#recover_cleanup ⇒ Object



1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
# File 'lib/osctl/cli/container.rb', line 1709

def recover_cleanup
  require_args!('id')

  cleanup = []
  cleanup << 'cgroups' if opts['cgroups']
  cleanup << 'netifs' if opts['network-interfaces']

  cleanup = 'all' if cleanup.empty?

  osctld_fmt(:ct_recover_cleanup, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    force: opts[:force],
    cleanup:
  })
end


1702
1703
1704
1705
1706
1707
# File 'lib/osctl/cli/container.rb', line 1702

def recover_forget_host_link
  require_args!('id', 'interface')
  osctld_fmt(:ct_recover_forget_host_link, cmd_opts: {
    id: args[0], pool: gopts[:pool], netif: args[1]
  })
end

#recover_kill ⇒ Object



1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
# File 'lib/osctl/cli/container.rb', line 1616

def recover_kill
  require_args!('id', optional: %w[signal])

  if args[0].index(':')
    pool, id = args[0].split(':')
  else
    pool = gopts[:pool]
    id = args[0]
  end

  if args[1]
    if args[1] =~ /^\d+$/
      signal = Signal.list.key(args[1].to_i)

      if signal.nil?
        raise GLI::BadCommandLine, "invalid signal '#{args[1]}'"
      end
    else
      name = args[1].upcase

      raise GLI::BadCommandLine, "invalid signal '#{args[1]}'" unless Signal.list.has_key?(name)

      signal = name

    end
  else
    signal = 'KILL'
  end

  pl = OsCtl::Lib::ProcessList.new do |p|
    ctid = p.ct_id

    next(false) if ctid.nil?

    if pool.nil?
      next(false) unless ctid[1] == id

      pool = ctid[0]

    end

    ctid[0] == pool && ctid[1] == id
  end

  if pl.empty?
    puts 'No processes found'
    return
  end

  out_cols, out_data = Ps::Columns.generate(
    pl,
    Ps::Columns::DEFAULT_ONE_CT,
    gopts[:parsable]
  )

  OsCtl::Lib::Cli::OutputFormatter.print(
    out_data,
    cols: out_cols,
    layout: :columns,
    header: true
  )

  puts
  $stdout.write("Kill #{pl.length} processes with SIG#{signal}? [yes/NO]: ")
  $stdout.flush

  if $stdin.readline.strip.downcase == 'yes'
    pl.each do |p|
      puts "kill -SIG#{signal} #{p.pid}"
      Process.kill(signal, p.pid)
    end
  else
    puts 'Aborted'
  end
end

#recover_state ⇒ Object



1692
1693
1694
1695
1696
1697
1698
1699
1700
# File 'lib/osctl/cli/container.rb', line 1692

def recover_state
  require_args!('id')

  osctld_fmt(:ct_recover_state, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    manipulation_lock: opts[:lock] ? nil : 'ignore'
  })
end

#reinstall ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/osctl/cli/container.rb', line 316

def reinstall
  require_args!('id')

  cmd_opts = {
    id: args[0],
    pool: opts[:pool] || gopts[:pool],
    repository: opts[:repository],
    remove_snapshots: opts['remove-snapshots']
  }

  if opts['from-file']
    cmd_opts.update(
      type: :image,
      path: File.absolute_path(opts['from-file'])
    )
  else
    cmd_opts.update(
      type: :remote,
      image: repo_image_attrs(defaults: false)
    )
  end

  osctld_fmt(:ct_reinstall, cmd_opts:)
end

#repo_image_attrs(defaults: true) ⇒ Object (protected)



1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
# File 'lib/osctl/cli/container.rb', line 1846

def repo_image_attrs(defaults: true)
  ret = {}

  if defaults
    ret[:vendor] ||= opts[:vendor] || 'default'
    ret[:variant] ||= opts[:variant] || 'default'
    ret[:arch] ||= opts[:arch] || `uname -m`.strip
    ret[:distribution] ||= opts[:distribution]
    ret[:version] ||= opts[:version] || 'stable'
  else
    ret[:vendor] ||= opts[:vendor]
    ret[:variant] ||= opts[:variant]
    ret[:arch] ||= opts[:arch]
    ret[:distribution] ||= opts[:distribution]
    ret[:version] ||= opts[:version]
  end

  ret
end

#restart ⇒ Object



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/osctl/cli/container.rb', line 433

def restart
  require_args!('id')

  if opts[:kill] && opts['dont-kill']
    raise GLI::BadCommandLine, '--kill and --dont-kill cannot be used together'

  elsif (opts[:kill] || opts['dont-kill']) && opts[:reboot]
    raise GLI::BadCommandLine, '--kill and --dont-kill cannot be used with --reboot'

  elsif opts[:kill]
    m = :kill

  elsif opts['dont-kill']
    m = :shutdown_or_fail

  else
    m = :shutdown_or_kill
  end

  if opts[:foreground] && opts[:attach]
    raise GLI::BadCommandLine, 'use either --foreground or --attach'
  end

  cmd_opts = {
    id: args[0],
    pool: gopts[:pool],
    wait: get_ct_wait(opts[:wait]),
    reboot: opts[:reboot],
    stop_timeout: opts[:timeout],
    stop_method: m,
    message: opts[:message]
  }

  if opts[:foreground]
    open_console(args[0], gopts[:pool], 0, gopts[:json]) do |sock|
      sock.close if osctld_resp(:ct_restart, **cmd_opts).error?
    end

    return
  end

  osctld_fmt(:ct_restart, cmd_opts:)

  return unless opts[:attach]

  puts 'Attaching'
  attach
end

#runscript ⇒ Object



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/osctl/cli/container.rb', line 522

def runscript
  require_args!('id', 'script', strict: false)

  c = osctld_open
  cont = c.cmd_data!(
    :ct_runscript,
    id: args[0],
    pool: gopts[:pool],
    script: args[1] == '-' ? nil : File.realpath(args[1]),
    arguments: args[2..],
    run: opts['run-container'],
    network: opts['network']
  )

  if cont != 'continue'
    warn "runscript not available: invalid response '#{cont}'"
    exit(false)
  end

  handle_exec_io(c)
end

#set(option) ⇒ Object (protected)



1830
1831
1832
1833
1834
1835
1836
# File 'lib/osctl/cli/container.rb', line 1830

def set(option)
  require_args!('id', strict: false)
  cmd_opts = { id: args[0], pool: gopts[:pool] }
  cmd_opts[option] = yield(args[1..])

  osctld_fmt(:ct_set, cmd_opts:)
end

#set_attr ⇒ Object



834
835
836
837
838
839
840
841
842
# File 'lib/osctl/cli/container.rb', line 834

def set_attr
  require_args!('id', 'attribute', 'value')
  do_set_attr(
    :ct_set,
    { id: args[0], pool: gopts[:pool] },
    args[1],
    args[2]
  )
end

#set_autostart ⇒ Object



591
592
593
594
595
596
597
598
599
600
# File 'lib/osctl/cli/container.rb', line 591

def set_autostart
  require_args!('id')

  set(:autostart) do
    {
      priority: opts[:priority],
      delay: opts[:delay]
    }
  end
end

#set_cpu_limit ⇒ Object



809
810
811
812
# File 'lib/osctl/cli/container.rb', line 809

def set_cpu_limit
  require_args!('id', 'limit')
  do_set_cpu_limit(:ct_cgparam_set, id: args[0], pool: gopts[:pool])
end

#set_cpu_package ⇒ Object



696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# File 'lib/osctl/cli/container.rb', line 696

def set_cpu_package
  require_args!('id', 'cpu-package')

  set(:cpu_package) do |args|
    str = args[0]

    if %w[auto none].include?(str)
      str
    elsif /^\d+$/ =~ str
      pkg_id = str.to_i

      topology = OsCtl::Lib::CpuTopology.new

      unless topology.packages.has_key?(pkg_id)
        warn "Warning: CPU package #{pkg_id.inspect} does not exist on this system"
        warn "Available CPU packages: #{topology.packages.keys.join(', ')}"
      end

      pkg_id
    else
      raise GLI::BadCommandLine, 'CPU package must be a number or auto/none'
    end
  end
end

#set_distribution ⇒ Object



656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'lib/osctl/cli/container.rb', line 656

def set_distribution
  set(:distribution) do |args|
    if args.count < 2 || args.count > 5
      raise GLI::BadCommandLine, 'expected <distribution> <version> [arch [vendor [variant]]]'
    end

    {
      name: args[0],
      version: args[1],
      arch: args[2],
      vendor: args[3],
      variant: args[4]
    }
  end
end

#set_dns_resolver ⇒ Object



630
631
632
633
634
635
636
# File 'lib/osctl/cli/container.rb', line 630

def set_dns_resolver
  set(:dns_resolvers) do |args|
    raise GLI::BadCommandLine, 'expected at least one address' if args.empty?

    args
  end
end

#set_ephemeral ⇒ Object



607
608
609
610
# File 'lib/osctl/cli/container.rb', line 607

def set_ephemeral
  require_args!('id')
  set(:ephemeral) { true }
end

#set_hostname ⇒ Object



617
618
619
620
621
622
623
# File 'lib/osctl/cli/container.rb', line 617

def set_hostname
  require_args!('id', 'hostname')

  set(:hostname) do |args|
    args[0] || (raise 'expected hostname')
  end
end

#set_image_config ⇒ Object



672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
# File 'lib/osctl/cli/container.rb', line 672

def set_image_config
  require_args!('id')

  cmd_opts = {
    id: args[0],
    pool: opts[:pool] || gopts[:pool],
    repository: opts[:repository],
    image: repo_image_attrs(defaults: false)
  }

  if opts['from-file']
    cmd_opts.update(
      type: :image,
      path: File.absolute_path(opts['from-file'])
    )
  else
    cmd_opts.update(
      type: :remote
    )
  end

  osctld_fmt(:ct_set_image_config, cmd_opts:)
end

#set_impermanence ⇒ Object



770
771
772
773
774
775
776
777
778
779
780
781
782
783
# File 'lib/osctl/cli/container.rb', line 770

def set_impermanence
  require_args!('id')

  set(:impermanence) do
    {
      zfs_properties: opts['zfs-property'].to_h do |v|
        k, v = v.split('=')
        raise GLI::BadCommandLine, "invalid ZFS property '#{v}'" if v.nil?

        [k, v]
      end
    }
  end
end

#set_init_cmd ⇒ Object



747
748
749
# File 'lib/osctl/cli/container.rb', line 747

def set_init_cmd
  set(:init_cmd) { |args| args }
end

#set_map_mode ⇒ Object



790
791
792
793
794
795
796
797
# File 'lib/osctl/cli/container.rb', line 790

def set_map_mode
  require_args!('id', 'mode')
  osctld_fmt(:ct_map_mode, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    map_mode: args[1]
  })
end

#set_memory_limit ⇒ Object



819
820
821
822
823
824
825
826
827
# File 'lib/osctl/cli/container.rb', line 819

def set_memory_limit
  require_args!('id', 'memory', optional: %w[swap])
  do_set_memory(
    :ct_cgparam_set,
    :ct_cgparam_unset,
    id: args[0],
    pool: gopts[:pool]
  )
end

#set_nesting ⇒ Object



643
644
645
646
647
648
649
# File 'lib/osctl/cli/container.rb', line 643

def set_nesting
  require_args!('id')

  set(:nesting) do |_args|
    true
  end
end

#set_raw_lxc ⇒ Object



799
800
801
802
# File 'lib/osctl/cli/container.rb', line 799

def set_raw_lxc
  require_args!('id')
  set(:raw_lxc) { |_args| $stdin.read }
end

#set_seccomp_profile ⇒ Object



726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
# File 'lib/osctl/cli/container.rb', line 726

def set_seccomp_profile
  require_args!('id', 'profile')

  set(:seccomp_profile) do |args|
    if args.count != 1
      raise GLI::BadCommandLine, 'expected <profile>'

    elsif !File.exist?(args[0])
      raise GLI::BadCommandLine, "file '#{args[0]}' does not exist"

    else
      File.absolute_path(args[0])
    end
  end
end

#set_start_menu ⇒ Object



755
756
757
758
759
760
761
762
763
# File 'lib/osctl/cli/container.rb', line 755

def set_start_menu
  require_args!('id')

  set(:start_menu) do
    {
      timeout: opts[:timeout]
    }
  end
end

#show ⇒ Object



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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/osctl/cli/container.rb', line 223

def show
  c = osctld_open
  cg_init_subsystems(c)

  cgparams = cg_list_raw_cgroup_params
  zfsprops = ZfsProperties.new
  keyring = KernelKeyring.new

  param_selector = OsCtl::Lib::Cli::ParameterSelector.new(
    all_params: FIELDS + cgparams + zfsprops.list_property_names + keyring.list_param_names,
    default_params: DEFAULT_FIELDS
  )

  if opts[:list]
    puts param_selector
    return
  end

  require_args!('id')

  cols = param_selector.parse_option(opts[:output])
  cols = zfsprops.validate_property_names(cols)

  cmd_opts = {
    id: args[0],
    pool: gopts[:pool]
  }

  if cols.include?(:hostname_readout)
    cmd_opts[:read_hostname] = true
  end

  ct = c.cmd_data!(:ct_show, **cmd_opts)

  cg_add_stats(ct, ct[:group_path], cols, gopts[:parsable])
  c.close

  add_loadavg(ct)

  cg_add_raw_cgroup_params(
    ct,
    ct[:group_path],
    cols & cgparams.map(&:to_sym)
  )

  zfsprops.add_container_values(ct, cols, precise: gopts[:parsable])
  keyring.add_container_values(ct, cols, precise: gopts[:parsable])

  format_output(ct, cols:, header: !opts['hide-header'], opts: {
    memory_limit: {
      align: 'right',
      display: proc do |v|
        if v.nil? || gopts[:parsable] || gopts[:json]
          v
        else
          humanize_data(v)
        end
      end
    },
    swap_limit: {
      align: 'right',
      display: proc do |v|
        if v.nil? || gopts[:parsable] || gopts[:json]
          v
        else
          humanize_data(v)
        end
      end
    }
  })
end

#start ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/osctl/cli/container.rb', line 346

def start
  require_args!('id')

  if opts[:foreground] && opts[:attach]
    raise GLI::BadCommandLine, 'use either --foreground or --attach'
  end

  cmd_opts = {
    id: args[0],
    pool: gopts[:pool],
    wait: get_ct_wait(opts[:wait]),
    queue: opts[:queue],
    priority: opts[:priority],
    debug: opts[:debug]
  }

  if opts[:foreground]
    open_console(args[0], gopts[:pool], 0, gopts[:json]) do |sock|
      sock.close if osctld_resp(:ct_start, **cmd_opts).error?
    end

    return
  end

  osctld_fmt(:ct_start, cmd_opts:)

  return unless opts[:attach]

  puts 'Attaching'
  attach
end

#stop ⇒ Object



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
# File 'lib/osctl/cli/container.rb', line 402

def stop
  require_args!('id')

  if opts[:kill] && opts['dont-kill']
    raise GLI::BadCommandLine, '--kill and --dont-kill cannot be used together'

  elsif opts[:kill]
    m = :kill

  elsif opts['dont-kill']
    m = :shutdown_or_fail

  else
    m = :shutdown_or_kill
  end

  cmd_opts = {
    id: args[0],
    pool: gopts[:pool],
    timeout: opts[:timeout],
    method: m,
    message: opts[:message]
  }

  return osctld_fmt(:ct_stop, cmd_opts:) unless opts[:foreground]

  open_console(args[0], gopts[:pool], 0, gopts[:json]) do |sock|
    sock.close if osctld_resp(:ct_stop, **cmd_opts).error?
  end
end

#su ⇒ Object



584
585
586
587
588
589
# File 'lib/osctl/cli/container.rb', line 584

def su
  require_args!('id')

  cmd = osctld_call(:ct_su, id: args[0], pool: gopts[:pool])
  handle_ct_attach(cmd)
end

#tree ⇒ Object



213
214
215
216
217
218
219
220
221
# File 'lib/osctl/cli/container.rb', line 213

def tree
  require_args!('pool')
  Tree.print(
    args[0],
    parsable: gopts[:parsable],
    color: gopts[:color],
    containers: true
  )
end

#uid ⇒ Object



1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
# File 'lib/osctl/cli/container.rb', line 1246

def uid
  require_args!('uid|-', strict: false)

  raw_uids = []

  if args[0] == '-'
    raw_uids << $stdin.readline.strip until $stdin.eof?
  else
    raw_uids = args
  end

  uids = filter_ugids(raw_uids)

  finder = UgidFinder.new(header: !opts['hide-header'])
  finder.list_by_uid(uids)
end

#unfreeze ⇒ Object



1126
1127
1128
1129
# File 'lib/osctl/cli/container.rb', line 1126

def unfreeze
  require_args!('id')
  osctld_fmt(:ct_unfreeze, cmd_opts: { id: args[0], pool: gopts[:pool] })
end

#unset(option) ⇒ Object (protected)



1838
1839
1840
1841
1842
1843
1844
# File 'lib/osctl/cli/container.rb', line 1838

def unset(option)
  require_args!('id', strict: false)
  cmd_opts = { id: args[0], pool: gopts[:pool] }
  cmd_opts[option] = block_given? ? yield(args[1..]) : true

  osctld_fmt(:ct_unset, cmd_opts:)
end

#unset_attr ⇒ Object



844
845
846
847
848
849
850
851
# File 'lib/osctl/cli/container.rb', line 844

def unset_attr
  require_args!('id', 'attribute')
  do_unset_attr(
    :ct_unset,
    { id: args[0], pool: gopts[:pool] },
    args[1]
  )
end

#unset_autostart ⇒ Object



602
603
604
605
# File 'lib/osctl/cli/container.rb', line 602

def unset_autostart
  require_args!('id')
  unset(:autostart)
end

#unset_cpu_limit ⇒ Object



814
815
816
817
# File 'lib/osctl/cli/container.rb', line 814

def unset_cpu_limit
  require_args!('id')
  do_unset_cpu_limit(:ct_cgparam_unset, id: args[0], pool: gopts[:pool])
end

#unset_cpu_package ⇒ Object



721
722
723
724
# File 'lib/osctl/cli/container.rb', line 721

def unset_cpu_package
  require_args!('id')
  unset(:cpu_package)
end

#unset_dns_resolver ⇒ Object



638
639
640
641
# File 'lib/osctl/cli/container.rb', line 638

def unset_dns_resolver
  require_args!('id')
  unset(:dns_resolvers)
end

#unset_ephemeral ⇒ Object



612
613
614
615
# File 'lib/osctl/cli/container.rb', line 612

def unset_ephemeral
  require_args!('id')
  unset(:ephemeral)
end

#unset_hostname ⇒ Object



625
626
627
628
# File 'lib/osctl/cli/container.rb', line 625

def unset_hostname
  require_args!('id')
  unset(:hostname)
end

#unset_impermanence ⇒ Object



785
786
787
788
# File 'lib/osctl/cli/container.rb', line 785

def unset_impermanence
  require_args!('id')
  unset(:impermanence)
end

#unset_init_cmd ⇒ Object



751
752
753
# File 'lib/osctl/cli/container.rb', line 751

def unset_init_cmd
  unset(:init_cmd)
end

#unset_memory_limit ⇒ Object



829
830
831
832
# File 'lib/osctl/cli/container.rb', line 829

def unset_memory_limit
  require_args!('id')
  do_unset_memory(:ct_cgparam_unset, id: args[0], pool: gopts[:pool])
end

#unset_nesting ⇒ Object



651
652
653
654
# File 'lib/osctl/cli/container.rb', line 651

def unset_nesting
  require_args!('id')
  unset(:nesting)
end

#unset_raw_lxc ⇒ Object



804
805
806
807
# File 'lib/osctl/cli/container.rb', line 804

def unset_raw_lxc
  require_args!('id')
  unset(:raw_lxc)
end

#unset_seccomp_profile ⇒ Object



742
743
744
745
# File 'lib/osctl/cli/container.rb', line 742

def unset_seccomp_profile
  require_args!('id')
  unset(:seccomp_profile)
end

#unset_start_menu ⇒ Object



765
766
767
768
# File 'lib/osctl/cli/container.rb', line 765

def unset_start_menu
  require_args!('id')
  unset(:start_menu)
end

#wall ⇒ Object



571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/osctl/cli/container.rb', line 571

def wall
  msg = opts[:message] || $stdin.read

  osctld_fmt(
    :ct_wall,
    cmd_opts: {
      ids: args.empty? ? nil : args,
      message: msg,
      banner: !opts['hide-banner']
    }
  )
end