From 0e29282e6a62b138357adc4faacaf05f5809ce5b Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Thu, 10 Jul 2025 16:35:49 +0300 Subject: [PATCH 1/4] force unmount and remove in flist clean up if the path is not mountpoint but the module can not delete it --- pkg/flist/cleanup.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/pkg/flist/cleanup.go b/pkg/flist/cleanup.go index d169daf8..1313f11a 100644 --- a/pkg/flist/cleanup.go +++ b/pkg/flist/cleanup.go @@ -139,9 +139,33 @@ func (f *flistModule) cleanUnusedMounts() error { } if err := os.Remove(path); err != nil { - log.Error().Err(err).Msgf("failed to clean mountpoint %s", path) + if err := f.forceUnmountAndRemove(path); err != nil { + log.Error().Err(err).Msgf("failed to clean mountpoint %s", path) + } } } return nil } + +func (f *flistModule) forceUnmountAndRemove(path string) error { + log.Debug().Msgf("trying to forcibly clean up : %+v", path) + // Try normal unmount first + err := f.system.Unmount(path, 0) + if err != nil { + log.Warn().Err(err).Msgf("normal unmount failed for %s, trying lazy unmount", path) + + // Try lazy unmount (MNT_DETACH) + err = syscall.Unmount(path, syscall.MNT_DETACH) + if err != nil { + log.Error().Err(err).Msgf("lazy unmount also failed for %s", path) + return err + } + } + // Now try to remove the directory + if err := os.RemoveAll(path); err != nil { + log.Error().Err(err).Msgf("failed to remove mountpoint %s", path) + return err + } + return nil +} From bb225bfd2c49e086e204286b5e64610188ed96fc Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Thu, 10 Jul 2025 16:43:06 +0300 Subject: [PATCH 2/4] log debug message if failed to remove --- pkg/flist/cleanup.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pkg/flist/cleanup.go b/pkg/flist/cleanup.go index 1313f11a..06bfc2f8 100644 --- a/pkg/flist/cleanup.go +++ b/pkg/flist/cleanup.go @@ -139,6 +139,7 @@ func (f *flistModule) cleanUnusedMounts() error { } if err := os.Remove(path); err != nil { + log.Debug().Msgf("failed to remove path, trying to forcibly clean up : %+v", path) if err := f.forceUnmountAndRemove(path); err != nil { log.Error().Err(err).Msgf("failed to clean mountpoint %s", path) } @@ -149,7 +150,6 @@ func (f *flistModule) cleanUnusedMounts() error { } func (f *flistModule) forceUnmountAndRemove(path string) error { - log.Debug().Msgf("trying to forcibly clean up : %+v", path) // Try normal unmount first err := f.system.Unmount(path, 0) if err != nil { @@ -158,14 +158,12 @@ func (f *flistModule) forceUnmountAndRemove(path string) error { // Try lazy unmount (MNT_DETACH) err = syscall.Unmount(path, syscall.MNT_DETACH) if err != nil { - log.Error().Err(err).Msgf("lazy unmount also failed for %s", path) - return err + return errors.Wrapf(err, "lazy unmount also failed for %s", path) } } // Now try to remove the directory if err := os.RemoveAll(path); err != nil { - log.Error().Err(err).Msgf("failed to remove mountpoint %s", path) - return err + return errors.Wrapf(err, "failed to remove mountpoint %s", path) } return nil } From 9c702e7c373b0260e8c8d943621fc336a93f6936 Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Sun, 13 Jul 2025 12:30:17 +0300 Subject: [PATCH 3/4] use f.system instead of syscall --- pkg/flist/cleanup.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/flist/cleanup.go b/pkg/flist/cleanup.go index 06bfc2f8..40d1aef7 100644 --- a/pkg/flist/cleanup.go +++ b/pkg/flist/cleanup.go @@ -149,19 +149,20 @@ func (f *flistModule) cleanUnusedMounts() error { return nil } +// forceUnmountAndRemove tries to fix the clean up of broken mounts by attempting to unmount them first func (f *flistModule) forceUnmountAndRemove(path string) error { - // Try normal unmount first + // try normal unmount first err := f.system.Unmount(path, 0) if err != nil { log.Warn().Err(err).Msgf("normal unmount failed for %s, trying lazy unmount", path) - // Try lazy unmount (MNT_DETACH) - err = syscall.Unmount(path, syscall.MNT_DETACH) + // try lazy unmount + err = f.system.Unmount(path, syscall.MNT_DETACH) if err != nil { return errors.Wrapf(err, "lazy unmount also failed for %s", path) } } - // Now try to remove the directory + // try to remove the path after unmount if err := os.RemoveAll(path); err != nil { return errors.Wrapf(err, "failed to remove mountpoint %s", path) } From ca2c822d33608de536d6273db38a1962040412ce Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Sun, 13 Jul 2025 14:06:24 +0300 Subject: [PATCH 4/4] rename force clean up function --- pkg/flist/cleanup.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/flist/cleanup.go b/pkg/flist/cleanup.go index 40d1aef7..fb1a9fa1 100644 --- a/pkg/flist/cleanup.go +++ b/pkg/flist/cleanup.go @@ -140,7 +140,7 @@ func (f *flistModule) cleanUnusedMounts() error { if err := os.Remove(path); err != nil { log.Debug().Msgf("failed to remove path, trying to forcibly clean up : %+v", path) - if err := f.forceUnmountAndRemove(path); err != nil { + if err := f.forceRemoveBrokenMounts(path); err != nil { log.Error().Err(err).Msgf("failed to clean mountpoint %s", path) } } @@ -149,8 +149,8 @@ func (f *flistModule) cleanUnusedMounts() error { return nil } -// forceUnmountAndRemove tries to fix the clean up of broken mounts by attempting to unmount them first -func (f *flistModule) forceUnmountAndRemove(path string) error { +// forceRemoveBrokenMounts tries to force the clean up of broken mounts by attempting to unmount them first +func (f *flistModule) forceRemoveBrokenMounts(path string) error { // try normal unmount first err := f.system.Unmount(path, 0) if err != nil {