From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 0BADE1FF09B for ; Mon, 28 Sep 2026 15:37:17 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 5B6BA216F9; Mon, 28 Sep 2026 15:37:13 +0200 (CEST) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 28 Sep 2026 15:37:08 +0200 Message-Id: Subject: Re: [PATCH storage] fix #6969: plugin: keep content directory when freeing its last file To: "Michal Fox" , From: "Max R. Carrara" X-Mailer: aerc 0.18.2-0-ge037c095a049 References: <20260926093257.7-1-me@dualfroz.com> In-Reply-To: <20260926093257.7-1-me@dualfroz.com> X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1790602628448 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.354 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: YH4IRTDXJI2WUW4FVVUKXKX3J3SGZJGK X-Message-ID-Hash: YH4IRTDXJI2WUW4FVVUKXKX3J3SGZJGK X-MailFrom: m.carrara@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: On Sat Sep 26, 2026 at 11:32 AM CEST, Michal Fox wrote: > After removing a volume, free_image tries to remove the parent > directory, so that no empty per-guest directories are left behind once > all images of a guest are gone. This was done unconditionally, so > deleting the last ISO, container template, snippet or import file also > removed the shared content directory like 'snippets' or 'template/iso'. > > The directory gets re-created shortly after by pvestatd, but as > root:root with default permissions, losing any ownership or mode that > the admin set up, for example to allow a non-root user to upload > snippets. > > Only clean up the parent directory for guest images, which are the only > volumes stored in per-guest directories. > > Signed-off-by: Michal Fox > --- Tested this on my workstation by creating an empty file in `/var/lib/vz/snippets/` and changing the directory's permissions to see whether it was re-created at a glance. With the patch, the content dir is indeed left as-is. So, works as advertised. LGTM! Thanks for your contribution, it's very appreciated! Consider: Reviewed-by: Max R. Carrara Tested-by: Max R. Carrara > src/PVE/Storage/Plugin.pm | 6 ++--- > src/test/free_image_test.pm | 51 ++++++++++++++++++++++++++++++++++++ > src/test/run_plugin_tests.pl | 1 + > 3 files changed, 55 insertions(+), 3 deletions(-) > create mode 100644 src/test/free_image_test.pm > > diff --git a/src/PVE/Storage/Plugin.pm b/src/PVE/Storage/Plugin.pm > index a9e1751..d499fd9 100644 > --- a/src/PVE/Storage/Plugin.pm > +++ b/src/PVE/Storage/Plugin.pm > @@ -1166,9 +1166,9 @@ sub free_image { > } > > # try to cleanup directory to not clutter storage with empty $vmid d= irs if > - # all images from a guest got deleted > - my $dir =3D dirname($path); > - rmdir($dir); > + # all images from a guest got deleted, but keep the shared content t= ype dirs > + my ($vtype) =3D $class->parse_volname($volname); > + rmdir(dirname($path)) if $vtype eq 'images'; > > return undef; > } > diff --git a/src/test/free_image_test.pm b/src/test/free_image_test.pm > new file mode 100644 > index 0000000..6161866 > --- /dev/null > +++ b/src/test/free_image_test.pm > @@ -0,0 +1,51 @@ > +package PVE::Storage::TestFreeImage; > + > +use strict; > +use warnings; > + > +use lib qw(..); > + > +use File::Path qw(make_path); > +use File::Temp; > +use PVE::Storage; > +use PVE::Tools qw(file_set_contents); > +use Test::More; > + > +my $storage_dir =3D File::Temp->newdir(); > +my $scfg =3D { type =3D> 'dir', path =3D> "$storage_dir" }; > + > +# each test is comprised of the following array keys: > +# [0] =3D> volname of the only volume in its directory, freed by the tes= t > +# [1] =3D> parent directory of the volume, relative to the storage path > +# [2] =3D> whether the now empty parent directory is expected to be kept > +my $tests =3D [ > + ['100/vm-100-disk-0.raw', 'images/100', 0], > + ['iso/some.iso', 'template/iso', 1], > + ['vztmpl/some.tar.zst', 'template/cache', 1], > + ['snippets/hook.pl', 'snippets', 1], > + ['import/some.ova', 'import', 1], > +]; > + > +plan tests =3D> 2 * scalar(@$tests); > + > +for my $tt (@$tests) { > + my ($volname, $subdir, $keep_dir) =3D @$tt; > + > + my $path =3D PVE::Storage::DirPlugin->filesystem_path($scfg, $volnam= e); > + make_path("$storage_dir/$subdir"); > + file_set_contents($path, ''); > + > + my $format =3D (PVE::Storage::DirPlugin->parse_volname($volname))[6]= ; > + PVE::Storage::DirPlugin->free_image('local', $scfg, $volname, 0, $fo= rmat); > + > + ok(!-e $path, "$volname - volume removed"); > + is( > + !!-d "$storage_dir/$subdir", > + !!$keep_dir, > + "$volname - empty directory '$subdir' " . ($keep_dir ? 'kept' : = 'removed'), > + ); > +} > + > +done_testing(); > + > +1; > diff --git a/src/test/run_plugin_tests.pl b/src/test/run_plugin_tests.pl > index 8bce9d3..2e1c64f 100755 > --- a/src/test/run_plugin_tests.pl > +++ b/src/test/run_plugin_tests.pl > @@ -17,6 +17,7 @@ my $res =3D $harness->runtests( > "get_subdir_test.pm", > "filesystem_path_test.pm", > "prune_backups_test.pm", > + "free_image_test.pm", > ); > > exit -1 if !$res || $res->{failed} || $res->{parse_errors};