Skip to content

Commit

Permalink
Merge pull request #18548 from wanyaoqi/automated-cherry-pick-of-#185…
Browse files Browse the repository at this point in the history
…46-upstream-master

Automated cherry pick of #18546: fix(host-deployer): clean nbd mount on host-deployer start
  • Loading branch information
zexi authored Nov 2, 2023
2 parents b29db95 + 78080ff commit 8e81bf4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
13 changes: 13 additions & 0 deletions pkg/hostman/diskutils/nbd/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
38 changes: 35 additions & 3 deletions pkg/hostman/diskutils/nbd/nbdman.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 8e81bf4

Please sign in to comment.