}
sub get_container_disk_usage {
- my ($vmid) = @_;
-
- my $cmd = ['lxc-attach', '-n', $vmid, '--', 'df', '-P', '-B', '1', '/'];
-
- my $res = {
- total => 0,
- used => 0,
- avail => 0,
- };
-
- my $parser = sub {
- my $line = shift;
- if (my ($fsid, $total, $used, $avail) = $line =~
- m/^(\S+.*)\s+(\d+)\s+(\d+)\s+(\d+)\s+\d+%\s.*$/) {
- $res = {
- total => $total,
- used => $used,
- avail => $avail,
- };
- }
- };
- eval { PVE::Tools::run_command($cmd, timeout => 1, outfunc => $parser); };
- warn $@ if $@;
+ my ($vmid, $pid, $timeout) = @_;
- return $res;
+ return PVE::Tools::df("/proc/$pid/root/", $timeout // 3);
}
my $last_proc_vmid_stat;
my $uptime = (PVE::ProcFSTools::read_proc_uptime(1))[0];
+ # Cache the pids
+ my $pids = { map {
+ my $vmid = $_;
+ $vmid => ($active_hash->{$vmid} ? find_lxc_pid($vmid) : undef)
+ } keys %$list };
+
foreach my $vmid (keys %$list) {
my $d = $list->{$vmid};
$d->{cpus} = $conf->{cpulimit} || $cpucount;
if ($running) {
- my $res = get_container_disk_usage($vmid);
+ my $res = get_container_disk_usage($vmid, $pids->{$vmid});
$d->{disk} = $res->{used};
$d->{maxdisk} = $res->{total};
} else {
my $d = $list->{$vmid};
next if $d->{status} ne 'running';
- my $pid = find_lxc_pid($vmid);
+ my $pid = $pids->{$vmid};
my $ctime = (stat("/proc/$pid"))[10]; # 10 = ctime
$d->{uptime} = time - $ctime; # the method lxcfs uses