[pve-devel] [PATCH v2 container] fix #1607: implement pct fstrim

Wolfgang Bumiller w.bumiller at proxmox.com
Thu Mar 28 13:49:03 CET 2019


On Fri, Mar 22, 2019 at 02:09:29PM +0100, Oguz Bektas wrote:
> runs fstrim on the rootfs and all mountpoints of a given container. this
> works for both running and stopped containers.
> 
> Signed-off-by: Oguz Bektas <o.bektas at proxmox.com>
> ---
> 
> v2:
> * update description
> * don't lock config but instead make an 'fstrim' lock to avoid
> containers being stopped during io operation

Makes sense I suppose. At most we could also add an `fstrim+mounted`
lock combination, but that's not worth the effort I think.

Minor nit inline...

> 
>  src/PVE/CLI/pct.pm | 41 +++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 41 insertions(+)
> 
> diff --git a/src/PVE/CLI/pct.pm b/src/PVE/CLI/pct.pm
> index 794bc45..238ffe6 100755
> --- a/src/PVE/CLI/pct.pm
> +++ b/src/PVE/CLI/pct.pm
> @@ -755,6 +755,45 @@ __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();
> +	PVE::LXC::Config->set_lock($vmid, 'fstrim');
> +	my $conf = PVE::LXC::Config->load_config($vmid);

The set_lock() call already returns the config, so the above 2 lines
could be condensed to

    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;




More information about the pve-devel mailing list