]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/CLI/pct.pm
pct cpusets: cleanup output if there are no running containers
[pve-container.git] / src / PVE / CLI / pct.pm
index ca8722941c07723c63dda7ed12478485d8bf9617..cc06926fa2bd8088c26b7259584bd8498f778bec 100755 (executable)
@@ -10,6 +10,7 @@ use Term::ReadLine;
 
 use PVE::SafeSyslog;
 use PVE::Tools qw(extract_param);
+use PVE::CpuSet;
 use PVE::Cluster;
 use PVE::INotify;
 use PVE::RPCEnvironment;
@@ -32,6 +33,45 @@ my $upid_exit = sub {
     exit($status eq 'OK' ? 0 : -1);
 };
 
+__PACKAGE__->register_method ({
+    name => 'status',
+    path => 'status',
+    method => 'GET',
+    description => "Show CT status.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
+           verbose => {
+               description => "Verbose output format",
+               type => 'boolean',
+               optional => 1,
+           }
+       },
+    },
+    returns => { type => 'null'},
+    code => sub {
+       my ($param) = @_;
+
+       # test if CT exists
+       my $conf = PVE::LXC::Config->load_config ($param->{vmid});
+
+       my $vmstatus = PVE::LXC::vmstatus($param->{vmid});
+       my $stat = $vmstatus->{$param->{vmid}};
+       if ($param->{verbose}) {
+           foreach my $k (sort (keys %$stat)) {
+               my $v = $stat->{$k};
+               next if !defined($v);
+               print "$k: $v\n";
+           }
+       } else {
+           my $status = $stat->{status} || 'unknown';
+           print "status: $status\n";
+       }
+
+       return undef;
+    }});
+
 sub read_password {
     my $term = new Term::ReadLine ('pct');
     my $attribs = $term->Attribs;
@@ -283,6 +323,89 @@ __PACKAGE__->register_method({
        return undef;
     }});
 
+__PACKAGE__->register_method({
+    name => 'df',
+    path => 'df',
+    method => 'GET',
+    description => "Get the container's current disk usage.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
+       },
+    },
+    returns => { type => 'null' },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       # JSONSchema's format_size is exact, this uses floating point numbers
+       my $format = sub {
+           my ($size) = @_;
+           return $size if $size < 1024.;
+           $size /= 1024.;
+           return sprintf('%.1fK', ${size}) if $size < 1024.;
+           $size /= 1024.;
+           return sprintf('%.1fM', ${size}) if $size < 1024.;
+           $size /= 1024.;
+           return sprintf('%.1fG', ${size}) if $size < 1024.;
+           $size /= 1024.;
+           return sprintf('%.1fT', ${size}) if $size < 1024.;
+       };
+
+       my $vmid = extract_param($param, 'vmid');
+       PVE::LXC::Config->lock_config($vmid, sub {
+           my $pid = eval { PVE::LXC::find_lxc_pid($vmid) };
+           my ($conf, $rootdir, $storecfg, $mounted);
+           if ($@ || !$pid) {
+               $conf = PVE::LXC::Config->set_lock($vmid, 'mounted');
+               $rootdir = "/var/lib/lxc/$vmid/rootfs";
+               $storecfg = PVE::Storage::config();
+               PVE::LXC::mount_all($vmid, $storecfg, $conf);
+               $mounted = 1;
+           } else {
+               $conf = PVE::LXC::Config->load_config($vmid);
+               $rootdir = "/proc/$pid/root";
+           }
+
+           my @list = [qw(MP Volume Size Used Avail Use% Path)];
+           my @len = map { length($_) } @{$list[0]};
+
+           eval {
+               PVE::LXC::Config->foreach_mountpoint($conf, sub {
+                   my ($name, $mp) = @_;
+                   my $path = $mp->{mp};
+
+                   my $df = PVE::Tools::df("$rootdir/$path", 3);
+                   my $total = $format->($df->{total});
+                   my $used = $format->($df->{used});
+                   my $avail = $format->($df->{avail});
+
+                   my $pc = sprintf('%.1f', $df->{used}/$df->{total});
+
+                   my $entry = [ $name, $mp->{volume}, $total, $used, $avail, $pc, $path ];
+                   push @list, $entry;
+
+                   foreach my $i (0..5) {
+                       $len[$i] = length($entry->[$i])
+                           if $len[$i] < length($entry->[$i]);
+                   }
+               });
+
+               my $format = "%-$len[0]s %-$len[1]s %$len[2]s %$len[3]s %$len[4]s %$len[5]s %s\n";
+               printf($format, @$_) foreach @list;
+           };
+           warn $@ if $@;
+
+           if ($mounted) {
+               PVE::LXC::umount_all($vmid, $storecfg, $conf, 0);
+               PVE::LXC::Config->remove_lock($vmid, 'mounted');
+           }
+       });
+       return undef;
+    }});
+
 # File creation with specified ownership and permissions.
 # User and group can be names or decimal numbers.
 # Permissions are explicit (not affected by umask) and can be numeric with the
@@ -527,6 +650,67 @@ __PACKAGE__->register_method({
        return PVE::LXC::Config->lock_config($vmid, $code);
     }});
 
+__PACKAGE__->register_method ({
+    name => 'cpusets',
+    path => 'cpusets',
+    method => 'GET',
+    description => "Print the list of assigned CPU sets.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {},
+    },
+    returns => { type => 'null'},
+    code => sub {
+       my ($param) = @_;
+
+       my $ctlist = PVE::LXC::config_list();
+
+       my $len = 0;
+       my $id_len = 0;
+       my $res = {};
+
+       foreach my $vmid (sort keys %$ctlist) {
+           next if ! -d "/sys/fs/cgroup/cpuset/lxc/$vmid";
+
+           my $cpuset = eval { PVE::CpuSet->new_from_cgroup("lxc/$vmid"); };
+           if (my $err = $@) {
+               warn $err;
+               next;
+           }
+           my @cpuset_members = $cpuset->members();
+
+           my $line = ': ';
+
+           my $last = $cpuset_members[-1];
+
+           for (my $id = 0; $id <= $last; $id++) {
+               my $empty = ' ' x length("$id");
+               $line .= ' ' . ($cpuset->has($id) ? $id : $empty);
+           }
+           $len = length($line) if length($line) > $len;
+           $id_len = length($vmid) if length($vmid) > $id_len;
+
+           $res->{$vmid} = $line;
+       }
+
+       my @vmlist = sort keys %$res;
+
+       if (scalar(@vmlist)) {
+           my $header = '-' x ($len + $id_len) . "\n";
+
+           print $header;
+           foreach my $vmid (@vmlist) {
+               print sprintf("%${id_len}i%s\n", $vmid, $res->{$vmid});
+           }
+           print $header;
+
+       } else {
+           print "no running containers\n";
+       }
+
+       return undef;
+    }});
+
 our $cmddef = {
     list=> [ 'PVE::API2::LXC', 'vmlist', [], { node => $nodename }, sub {
        my $res = shift;
@@ -542,12 +726,19 @@ our $cmddef = {
                    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"
+                       }
+                   }
                }],
     set => [ 'PVE::API2::LXC::Config', 'update_vm', ['vmid'], { node => $nodename }],
 
@@ -565,6 +756,7 @@ our $cmddef = {
     clone => [ "PVE::API2::LXC", 'clone_vm', ['vmid', 'newid'], { node => $nodename }, $upid_exit ],
     migrate => [ "PVE::API2::LXC", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit],
     
+    status => [ __PACKAGE__, 'status', ['vmid']],
     console => [ __PACKAGE__, 'console', ['vmid']],
     enter => [ __PACKAGE__, 'enter', ['vmid']],
     unlock => [ __PACKAGE__, 'unlock', ['vmid']],
@@ -575,7 +767,9 @@ our $cmddef = {
     unmount => [ __PACKAGE__, 'unmount', ['vmid']],
     push => [ __PACKAGE__, 'push', ['vmid', 'file', 'destination']],
     pull => [ __PACKAGE__, 'pull', ['vmid', 'path', 'destination']],
-    
+
+    df => [ __PACKAGE__, 'df', ['vmid']],
+
     destroy => [ 'PVE::API2::LXC', 'destroy_vm', ['vmid'], 
                 { node => $nodename }, $upid_exit ],
 
@@ -598,6 +792,9 @@ our $cmddef = {
     rollback => [ "PVE::API2::LXC::Snapshot", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
 
     template => [ "PVE::API2::LXC", 'template', ['vmid'], { node => $nodename }],
+
+    cpusets => [ __PACKAGE__, 'cpusets', []],
+
 };