From d50357f83487fc4534bc01d6e4db6a29b69c7209 Mon Sep 17 00:00:00 2001 From: Shuo Wu Date: Wed, 26 Oct 2022 21:27:54 +0800 Subject: [PATCH] server: Check the work directory in the monitor Longhorn 4738 Signed-off-by: Shuo Wu --- pkg/server/manager.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/server/manager.go b/pkg/server/manager.go index 251a26ef..0be478bb 100644 --- a/pkg/server/manager.go +++ b/pkg/server/manager.go @@ -31,6 +31,7 @@ import ( type Manager struct { diskUUID string diskPath string + workDir string backingImages map[string]*BackingImage portRangeMin int32 @@ -65,6 +66,7 @@ func NewManager(diskUUID, diskPath, portRange string, shutdownCh chan error) (*M m := &Manager{ diskUUID: diskUUID, diskPath: diskPath, + workDir: workDir, backingImages: map[string]*BackingImage{}, portRangeMin: start, @@ -129,6 +131,17 @@ func (m *Manager) startMonitoring() { } } m.lock.RUnlock() + + if workDirStat, err := os.Stat(m.workDir); os.IsNotExist(err) || (workDirStat != nil && !workDirStat.IsDir()) { + if err := os.RemoveAll(m.workDir); err != nil { + m.log.WithError(err).Error("Backing Image Manager: detected the backing image work directory becoming invalid but failed to do cleanup") + continue + } + if err := os.Mkdir(m.workDir, 0666); err != nil && !os.IsExist(err) { + m.log.WithError(err).Error("Backing Image Manager: failed to recreate the backing image work directory after cleaning up the invalid one") + continue + } + } } if done { break