From 712e27f178d0fcabbc12444d28df123fb851b1c3 Mon Sep 17 00:00:00 2001 From: Christian Ebner Date: Thu, 24 Jan 2019 14:58:22 +0100 Subject: [PATCH] Fix #1941: remove empty directories when freeing image on FS based storages Remove directories if they are empty, which can happen if all images from a VM got deleted, e.g., after destroying said VM. Signed-off-by: Christian Ebner --- PVE/Storage/Plugin.pm | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/PVE/Storage/Plugin.pm b/PVE/Storage/Plugin.pm index e0c2a4e..7dbb41d 100644 --- a/PVE/Storage/Plugin.pm +++ b/PVE/Storage/Plugin.pm @@ -5,6 +5,7 @@ use warnings; use File::chdir; use File::Path; +use File::Basename; use PVE::Tools qw(run_command); use PVE::JSONSchema qw(get_standard_option); @@ -689,6 +690,11 @@ sub free_image { unlink($path) || die "unlink '$path' failed - $!\n"; } + + # try to cleanup directory to not clutter storage with empty $vmid dirs if + # all images from a guest got deleted + my $dir = dirname($path); + rmdir($dir); return undef; }