]> git.proxmox.com Git - pve-container.git/commitdiff
LXC: use Tools::df for get_container_disk_usage
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Fri, 30 Oct 2015 08:50:27 +0000 (09:50 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Fri, 30 Oct 2015 09:28:16 +0000 (10:28 +0100)
Run df on /proc/$pid/root instead of attaching to the
container and running its contained 'df' binary, as this
could create freezed processes of the container is frozen.

Also, since the container PIDs are now used in both loops
they're precached beforehand.

Fixes #793

src/PVE/LXC.pm

index 7c1ea534998135233129355a27d70ca420064826..0289c219b4d29d1d3e72e8d510d81f7cff8593ce 100644 (file)
@@ -737,31 +737,9 @@ sub check_running {
 }
 
 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;
@@ -796,6 +774,12 @@ sub vmstatus {
 
     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};
 
@@ -812,7 +796,7 @@ sub vmstatus {
        $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 {
@@ -847,7 +831,7 @@ sub vmstatus {
        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