]> git.proxmox.com Git - pve-container.git/commitdiff
fix #1607: implement pct fstrim
authorOguz Bektas <o.bektas@proxmox.com>
Thu, 28 Mar 2019 13:01:44 +0000 (14:01 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 28 Mar 2019 13:05:00 +0000 (14:05 +0100)
runs fstrim on the rootfs and all mountpoints of a given container. this
works for both running and stopped containers.

lock the CT during this operation using a config lock as it is
potentially long running. While fstrim itself wouldn't really need
the lock, as multiple parallel fstrim calls can be made without
problems, we want to forbid migrations during it and want to avoid
that we unmount a with the CT mounted with 'mount' lock (race) -
while we could handle and allow this its just not needed and easier
this way

Signed-off-by: Oguz Bektas <o.bektas@proxmox.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
src/PVE/CLI/pct.pm

index 794bc453a3b2a65ee5d8c7bd01add4b3ce7e67f8..ab7af33b57c5d8986eb72492b5fdb9cadddc3505 100755 (executable)
@@ -755,6 +755,44 @@ __PACKAGE__->register_method ({
        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 }),
+       },
+    },
+    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');
+       PVE::LXC::mount_all($vmid, $storecfg, $conf);
+       eval {
+           my $path = "";
+           PVE::LXC::Config->foreach_mountpoint($conf, sub {
+               my ($name, $mp) = @_;
+               $path = $mp->{mp};
+               my $cmd = ["fstrim", "-v", "$rootdir$path"];
+               PVE::Tools::run_command($cmd);
+           });
+       };
+
+       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;
@@ -841,6 +879,8 @@ our $cmddef = {
 
     cpusets => [ __PACKAGE__, 'cpusets', []],
 
+    fstrim => [ __PACKAGE__, 'fstrim', ['vmid']],
+
 };