package PVE::CLI::pct;
-
+
use strict;
use warnings;
-use POSIX;
use Fcntl;
use File::Copy 'copy';
-use Term::ReadLine;
+use POSIX;
-use PVE::SafeSyslog;
-use PVE::Tools qw(extract_param);
-use PVE::CpuSet;
+use PVE::CLIHandler;
use PVE::Cluster;
+use PVE::CpuSet;
+use PVE::GuestHelpers;
use PVE::INotify;
-use PVE::RPCEnvironment;
use PVE::JSONSchema qw(get_standard_option);
-use PVE::CLIHandler;
-use PVE::API2::LXC;
+use PVE::LXC::CGroup;
+use PVE::RPCEnvironment;
+use PVE::SafeSyslog;
+use PVE::Tools qw(extract_param);
+
use PVE::API2::LXC::Config;
-use PVE::API2::LXC::Status;
use PVE::API2::LXC::Snapshot;
-
-use Data::Dumper;
+use PVE::API2::LXC::Status;
+use PVE::API2::LXC;
use base qw(PVE::CLIHandler);
my $upid_exit = sub {
my $upid = shift;
my $status = PVE::Tools::upid_read_status($upid);
- exit($status eq 'OK' ? 0 : -1);
+ exit(PVE::Tools::upid_status_is_error($status) ? -1 : 0);
};
sub setup_environment {
return undef;
}});
-sub read_password {
- my $term = new Term::ReadLine ('pct');
- my $attribs = $term->Attribs;
- $attribs->{redisplay_function} = $attribs->{shadow_redisplay};
- my $input = $term->readline('Enter password: ');
- my $conf = $term->readline('Retype password: ');
- die "Passwords do not match.\n" if ($input ne $conf);
- return $input;
-}
-
-sub string_param_file_mapping {
+sub param_mapping {
my ($name) = @_;
my $mapping = {
- 'create_vm' => ['ssh-public-keys'],
+ 'create_vm' => [
+ PVE::CLIHandler::get_standard_mapping('pve-password'),
+ 'ssh-public-keys',
+ ],
};
- return defined($mapping->{$name}) ? $mapping->{$name} : [];
+ return $mapping->{$name};
}
__PACKAGE__->register_method ({
method => 'GET',
description => "Launch a console for the specified container.",
parameters => {
- additionalProperties => 0,
+ additionalProperties => 0,
properties => {
vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
+ escape => {
+ description => "Escape sequence prefix. For example to use <Ctrl+b q> as the escape sequence pass '^b'.",
+ default => '^a',
+ type => 'string',
+ pattern => '\^?[a-z]',
+ optional => 1,
+ },
},
},
returns => { type => 'null' },
# test if container exists on this node
my $conf = PVE::LXC::Config->load_config($param->{vmid});
- my $cmd = PVE::LXC::get_console_command($param->{vmid}, $conf);
+ my $cmd = PVE::LXC::get_console_command($param->{vmid}, $conf, $param->{escape});
exec(@$cmd);
}});
method => 'GET',
description => "Launch a shell for the specified container.",
parameters => {
- additionalProperties => 0,
+ additionalProperties => 0,
properties => {
vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
},
my $vmid = $param->{vmid};
- # test if container exists on this node
- PVE::LXC::Config->load_config($vmid);
-
- die "Error: container '$vmid' not running!\n" if !PVE::LXC::check_running($vmid);
+ PVE::LXC::Config->load_config($vmid); # test if container exists on this node
+ die "container '$vmid' not running!\n" if !PVE::LXC::check_running($vmid);
exec('lxc-attach', '-n', $vmid);
}});
method => 'GET',
description => "Launch a command inside the specified container.",
parameters => {
- additionalProperties => 0,
+ additionalProperties => 0,
properties => {
vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
'extra-args' => get_standard_option('extra-args'),
},
},
returns => { type => 'null' },
-
code => sub {
my ($param) = @_;
- # test if container exists on this node
- PVE::LXC::Config->load_config($param->{vmid});
+ my $vmid = $param->{vmid};
+ PVE::LXC::Config->load_config($vmid); # test if container exists on this node
+ die "container '$vmid' not running!\n" if !PVE::LXC::check_running($vmid);
- if (!@{$param->{'extra-args'}}) {
- die "missing command";
- }
- exec('lxc-attach', '-n', $param->{vmid}, '--', @{$param->{'extra-args'}});
+ die "missing command" if !@{$param->{'extra-args'}};
+
+ exec('lxc-attach', '-n', $vmid, '--', @{$param->{'extra-args'}});
}});
__PACKAGE__->register_method ({
optional => 1,
type => 'string',
description => "A volume on which to run the filesystem check",
- enum => [PVE::LXC::Config->mountpoint_names()],
+ enum => [PVE::LXC::Config->valid_volume_keys()],
},
},
},
returns => { type => 'null' },
code => sub {
-
my ($param) = @_;
+
my $vmid = $param->{'vmid'};
my $device = defined($param->{'device'}) ? $param->{'device'} : 'rootfs';
defined($conf->{$device}) || die "cannot run command on non-existing mount point $device\n";
- my $mount_point = $device eq 'rootfs' ? PVE::LXC::Config->parse_ct_rootfs($conf->{$device}) :
- PVE::LXC::Config->parse_ct_mountpoint($conf->{$device});
+ my $mount_point = PVE::LXC::Config->parse_volume($device, $conf->{$device});
+
+ die "cannot run fsck when container is running\n"
+ if PVE::LXC::check_running($vmid);
my $volid = $mount_point->{volume};
die "unable to run fsck for '$volid' (format == $format)\n"
if $format ne 'raw';
- $path = PVE::Storage::path($storage_cfg, $volid);
+ $path = PVE::Storage::map_volume($storage_cfg, $volid);
} else {
if (($volid =~ m|^/.+|) && (-b $volid)) {
}
push(@$command, $path);
-
- PVE::LXC::check_running($vmid) &&
- die "cannot run fsck on active container\n";
-
PVE::Tools::run_command($command);
+
+ PVE::Storage::unmap_volume($storage_cfg, $volid) if $storage_id;
};
PVE::LXC::Config->lock_config($vmid, $do_fsck);
my $conf = PVE::LXC::Config->set_lock($vmid, 'mounted');
PVE::LXC::mount_all($vmid, $storecfg, $conf);
});
+
+ print "mounted CT $vmid in '/var/lib/lxc/$vmid/rootfs'\n";
return undef;
}});
my @len = map { length($_) } @{$list[0]};
eval {
- PVE::LXC::Config->foreach_mountpoint($conf, sub {
+ PVE::LXC::Config->foreach_volume($conf, sub {
my ($name, $mp) = @_;
my $path = $mp->{mp};
if ($user =~ /^\d+$/) {
$uid = int($user);
} else {
- $uid = getpwnam($user) or die "failed to get uid for: $user\n"
+ $uid = getpwnam($user);
+ die "failed to get uid for: $user\n" if !defined($uid);
}
}
if (defined($group)) {
if ($group =~ /^\d+$/) {
$gid = int($group);
} else {
- $gid = getgrnam($group) or die "failed to get gid for: $group\n"
+ $gid = getgrnam($group);
+ die "failed to get gid for: $group\n" if !defined($gid);
}
}
return $fd;
}
+__PACKAGE__->register_method ({
+ name => 'rescan',
+ path => 'rescan',
+ method => 'POST',
+ description => "Rescan all storages and update disk sizes and unused disk images.",
+ parameters => {
+ additionalProperties => 0,
+ properties => {
+ vmid => get_standard_option('pve-vmid', {
+ optional => 1,
+ completion => \&PVE::LXC::complete_ctid,
+ }),
+ dryrun => {
+ type => 'boolean',
+ optional => 1,
+ default => 0,
+ description => 'Do not actually write changes out to conifg.',
+ },
+ },
+ },
+ returns => { type => 'null'},
+ code => sub {
+ my ($param) = @_;
+
+ my $dryrun = $param->{dryrun};
+
+ print "NOTE: running in dry-run mode, won't write changes out!\n" if $dryrun;
+
+ PVE::LXC::rescan($param->{vmid}, 0, $dryrun);
+
+ return undef;
+ }});
+
__PACKAGE__->register_method({
name => 'pull',
path => 'pull',
my $code = sub {
my $running = PVE::LXC::check_running($vmid);
- die "can only push files to a running VM" if !$running;
+ die "can only push files to a running CT\n" if !$running;
my $conf = PVE::LXC::Config->load_config($vmid);
my $unprivileged = $conf->{unprivileged};
my $res = {};
foreach my $vmid (sort keys %$ctlist) {
- next if ! -d "/sys/fs/cgroup/cpuset/lxc/$vmid";
+ my $cgroup = PVE::LXC::CGroup->new($vmid);
- my $cpuset = eval { PVE::CpuSet->new_from_cgroup("lxc/$vmid"); };
+ my ($cpuset, $path);
+ if (defined($path = $cgroup->get_path('cpuset', 1))) {
+ $cpuset = eval { PVE::CpuSet->new_from_path($path, 1); };
+ } elsif (defined($path = $cgroup->get_path(undef, 1))) {
+ $cpuset = eval { PVE::CpuSet->new_from_path($path, 1); };
+ } else {
+ # Container not running.
+ next;
+ }
if (my $err = $@) {
warn $err;
next;
}
+
my @cpuset_members = $cpuset->members();
my $line = ': ';
return undef;
}});
+__PACKAGE__->register_method ({
+ name => 'fstrim',
+ path => 'fstrim',
+ method => 'POST',
+ description => "Run fstrim on a chosen CT and its mountpoints.",
+ parameters => {
+ additionalProperties => 0,
+ properties => {
+ vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
+ 'ignore-mountpoints' => {
+ description => 'Skip all mountpoints, only do fstrim on the container root.',
+ optional => 1,
+ type => 'boolean',
+ },
+ },
+ },
+ returns => { type => 'null' },
+ code => sub {
+
+ my ($param) = @_;
+ my $vmid = $param->{'vmid'};
+
+ my $rootdir = "/var/lib/lxc/$vmid/rootfs";
+
+ my $storecfg = PVE::Storage::config();
+ my $conf = PVE::LXC::Config->set_lock($vmid, 'fstrim');
+ eval {
+ my $path = "";
+ PVE::LXC::mount_all($vmid, $storecfg, $conf);
+ PVE::LXC::Config->foreach_volume($conf, sub {
+ my ($name, $mp) = @_;
+ $path = $mp->{mp};
+ return if $param->{'ignore-mountpoints'} && $name =~ /^mp\d+/;
+ my $cmd = ["fstrim", "-v", "$rootdir$path"];
+ PVE::Tools::run_command($cmd, noerr => 1);
+ });
+ };
+ warn $@ if $@;
+
+ PVE::LXC::umount_all($vmid, $storecfg, $conf, 0);
+ PVE::LXC::Config->remove_lock($vmid, 'fstrim');
+
+ return undef;
+ }});
+
our $cmddef = {
list=> [ 'PVE::API2::LXC', 'vmlist', [], { node => $nodename }, sub {
my $res = shift;
my $format = "%-10s %-10s %-12s %-20s\n";
printf($format, 'VMID', 'Status', 'Lock', 'Name');
foreach my $d (sort {$a->{vmid} <=> $b->{vmid} } @$res) {
- printf($format, $d->{vmid}, $d->{status}, $d->{lock}, $d->{name});
+ my $lock = $d->{lock} || '';
+ printf($format, $d->{vmid}, $d->{status}, $lock, $d->{name});
}
}],
- config => [ "PVE::API2::LXC::Config", 'vm_config', ['vmid'],
- { node => $nodename }, sub {
- my $config = shift;
- foreach my $k (sort (keys %$config)) {
- next if $k eq 'digest';
- next if $k eq 'lxc';
- my $v = $config->{$k};
- if ($k eq 'description') {
- $v = PVE::Tools::encode_text($v);
- }
- print "$k: $v\n";
- }
- if (defined($config->{'lxc'})) {
- my $lxc_list = $config->{'lxc'};
- foreach my $lxc_opt (@$lxc_list) {
- print "$lxc_opt->[0]: $lxc_opt->[1]\n"
- }
- }
- }],
+ config => [ "PVE::API2::LXC::Config", 'vm_config', ['vmid'], { node => $nodename }, sub {
+ my $config = shift;
+ for my $k (sort (keys %$config)) {
+ next if $k eq 'digest' || $k eq 'lxc';
+ my $v = $config->{$k};
+ if ($k eq 'description') {
+ $v = PVE::Tools::encode_text($v);
+ }
+ print "$k: $v\n";
+ }
+ if (defined(my $lxc_list = $config->{'lxc'})) {
+ for my $lxc_opt (@$lxc_list) {
+ print "$lxc_opt->[0]: $lxc_opt->[1]\n"
+ }
+ }
+ }],
+
+ pending => [ "PVE::API2::LXC", "vm_pending", ['vmid'], { node => $nodename }, \&PVE::GuestHelpers::format_pending ],
set => [ 'PVE::API2::LXC::Config', 'update_vm', ['vmid'], { node => $nodename }],
resize => [ "PVE::API2::LXC", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ],
-
+
create => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename }, $upid_exit ],
restore => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename, restore => 1 }, $upid_exit ],
+ destroy => [ 'PVE::API2::LXC', 'destroy_vm', ['vmid'], { node => $nodename }, $upid_exit ],
start => [ 'PVE::API2::LXC::Status', 'vm_start', ['vmid'], { node => $nodename }, $upid_exit],
suspend => [ 'PVE::API2::LXC::Status', 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit],
resume => [ 'PVE::API2::LXC::Status', 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit],
shutdown => [ 'PVE::API2::LXC::Status', 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit],
stop => [ 'PVE::API2::LXC::Status', 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit],
-
+ reboot => [ 'PVE::API2::LXC::Status', 'vm_reboot', ['vmid'], { node => $nodename }, $upid_exit],
+
clone => [ "PVE::API2::LXC", 'clone_vm', ['vmid', 'newid'], { node => $nodename }, $upid_exit ],
migrate => [ "PVE::API2::LXC", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit],
-
+ 'move-volume' => [ "PVE::API2::LXC", 'move_volume', ['vmid', 'volume', 'storage', 'target-vmid', 'target-volume'], { node => $nodename }, $upid_exit ],
+ move_volume => { alias => 'move-volume' },
+
+ snapshot => [ "PVE::API2::LXC::Snapshot", 'snapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
+ delsnapshot => [ "PVE::API2::LXC::Snapshot", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
+ listsnapshot => [ "PVE::API2::LXC::Snapshot", 'list', ['vmid'], { node => $nodename }, \&PVE::GuestHelpers::print_snapshot_tree ],
+ rollback => [ "PVE::API2::LXC::Snapshot", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
+ template => [ "PVE::API2::LXC", 'template', ['vmid'], { node => $nodename }],
+
status => [ __PACKAGE__, 'status', ['vmid']],
console => [ __PACKAGE__, 'console', ['vmid']],
enter => [ __PACKAGE__, 'enter', ['vmid']],
pull => [ __PACKAGE__, 'pull', ['vmid', 'path', 'destination']],
df => [ __PACKAGE__, 'df', ['vmid']],
-
- destroy => [ 'PVE::API2::LXC', 'destroy_vm', ['vmid'],
- { node => $nodename }, $upid_exit ],
-
- snapshot => [ "PVE::API2::LXC::Snapshot", 'snapshot', ['vmid', 'snapname'],
- { node => $nodename } , $upid_exit ],
-
- delsnapshot => [ "PVE::API2::LXC::Snapshot", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
-
- listsnapshot => [ "PVE::API2::LXC::Snapshot", 'list', ['vmid'], { node => $nodename },
- sub {
- my $res = shift;
- foreach my $e (@$res) {
- my $headline = $e->{description} || 'no-description';
- $headline =~ s/\n.*//sg;
- my $parent = $e->{parent} // 'no-parent';
- printf("%-20s %-20s %s\n", $e->{name}, $parent, $headline);
- }
- }],
-
- rollback => [ "PVE::API2::LXC::Snapshot", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
-
- template => [ "PVE::API2::LXC", 'template', ['vmid'], { node => $nodename }],
-
+ rescan => [ __PACKAGE__, 'rescan', []],
cpusets => [ __PACKAGE__, 'cpusets', []],
-
+ fstrim => [ __PACKAGE__, 'fstrim', ['vmid']],
};
-
1;