From 78080ff321fd3390aba76fbb76cc3b351102743c Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Wed, 1 Nov 2023 20:42:09 +0800 Subject: [PATCH] fix(host-deployer): clean nbd mount on host-deployer start --- pkg/hostman/diskutils/nbd/driver.go | 13 ++++++++++ pkg/hostman/diskutils/nbd/nbdman.go | 38 ++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/pkg/hostman/diskutils/nbd/driver.go b/pkg/hostman/diskutils/nbd/driver.go index 42b6fb47501..88916638a8c 100644 --- a/pkg/hostman/diskutils/nbd/driver.go +++ b/pkg/hostman/diskutils/nbd/driver.go @@ -193,6 +193,19 @@ func (d *NBDDriver) findLVMPartitions(partDev string) string { return findVgname(partDev) } +func (nbdDriver *NBDDriver) setupAndPutdownLVMS() error { + _, err := nbdDriver.setupLVMS() + if err != nil { + return err + } + + if !nbdDriver.putdownLVMs() { + return errors.Errorf("failed putdown lvms") + } + + return nil +} + func (d *NBDDriver) Disconnect() error { if len(d.nbdDev) > 0 { pathType, lock := lvmTool.Acquire(d.rootImagePath()) diff --git a/pkg/hostman/diskutils/nbd/nbdman.go b/pkg/hostman/diskutils/nbd/nbdman.go index 5b732d01f3b..e6a6afbf72b 100644 --- a/pkg/hostman/diskutils/nbd/nbdman.go +++ b/pkg/hostman/diskutils/nbd/nbdman.go @@ -65,11 +65,13 @@ func tryDetachNbd(nbddev string) error { var errs []error for tried < MaxTries && fileutils2.IsBlockDeviceUsed(nbddev) { tried++ - err := QemuNbdDisconnect(nbddev) - if err != nil { + if err := putdownNbdDevice(nbddev); err != nil { errs = append(errs, err) + time.Sleep(time.Second) + continue } - time.Sleep(time.Second) + break + } if tried < MaxTries { return nil @@ -80,17 +82,47 @@ func tryDetachNbd(nbddev string) error { return nil } +func putdownNbdDevice(nbddev string) error { + nbdDriver := &NBDDriver{nbdDev: nbddev} + + if err := nbdDriver.findPartitions(); err != nil { + return err + } + + if _, err := nbdDriver.setupLVMS(); err != nil { + return err + } + for i := range nbdDriver.partitions { + if nbdDriver.partitions[i].IsMounted() { + if err := nbdDriver.partitions[i].Umount(); err != nil { + return errors.Wrapf(err, "umount %s", nbdDriver.partitions[i].GetPartDev()) + } + } + } + + if !nbdDriver.putdownLVMs() { + return errors.Errorf("failed putdown lvms") + } + if err := QemuNbdDisconnect(nbddev); err != nil { + return err + } + return nil + +} + func (m *SNBDManager) cleanupNbdDevices() { var i = 0 for { nbddev := fmt.Sprintf("/dev/nbd%d", i) if fileutils2.Exists(nbddev) { if fileutils2.IsBlockDeviceUsed(nbddev) { + log.Infof("nbd device %s is used", nbddev) err := tryDetachNbd(nbddev) if err != nil { log.Errorf("tryDetachNbd fail %s", err) } } + i++ } else { break }