Skip to content

Commit

Permalink
Merge pull request #18584 from wanyaoqi/fix/host-deployer-connect-lock
Browse files Browse the repository at this point in the history
fix(host-deployer): use sync.lock on nbd connect/disconnect
  • Loading branch information
zexi authored Nov 7, 2023
2 parents d5292e3 + b1ef71f commit e2969ee
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions pkg/hostman/diskutils/nbd/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"path"
"path/filepath"
"strings"
"sync"
"time"

"yunion.io/x/log"
Expand Down Expand Up @@ -51,10 +52,10 @@ func NewNBDDriver(imageInfo qemuimg.SImageInfo) *NBDDriver {
}
}

var lvmTool *SLVMImageConnectUniqueToolSet
var lock *sync.Mutex

func init() {
lvmTool = NewLVMImageConnectUniqueToolSet()
lock = new(sync.Mutex)
}

func (d *NBDDriver) Connect() error {
Expand All @@ -64,11 +65,10 @@ func (d *NBDDriver) Connect() error {
}

rootPath := d.rootImagePath()
pathType, lock := lvmTool.Acquire(rootPath)
if pathType != NON_LVM_PATH {
lock.Lock()
defer lock.Unlock()
}
log.Infof("lock root image path %s", rootPath)
lock.Lock()
defer lock.Unlock()
defer log.Infof("unlock root image path %s", rootPath)

if err := QemuNbdConnect(d.imageInfo, d.nbdDev); err != nil {
return err
Expand All @@ -84,22 +84,10 @@ func (d *NBDDriver) Connect() error {
tried += 1
}

log.Infof("path type %s: %v", d.nbdDev, pathType)
if pathType == LVM_PATH {
if _, err := d.setupLVMS(); err != nil {
return err
}
} else if pathType == PATH_TYPE_UNKNOWN {
hasLVM, err := d.setupLVMS()
log.Infof("%s hasLVM %v err %v", d.nbdDev, hasLVM, err)
if err != nil {
return err
}

// no lvm partition found and has partitions
if !hasLVM && len(d.partitions) > 0 {
lvmTool.CacheNonLvmImagePath(rootPath)
}
hasLVM, err := d.setupLVMS()
log.Infof("%s hasLVM %v err %v", d.nbdDev, hasLVM, err)
if err != nil {
return err
}
return nil
}
Expand Down Expand Up @@ -208,11 +196,11 @@ func (nbdDriver *NBDDriver) setupAndPutdownLVMS() error {

func (d *NBDDriver) Disconnect() error {
if len(d.nbdDev) > 0 {
pathType, lock := lvmTool.Acquire(d.rootImagePath())
if pathType != NON_LVM_PATH {
lock.Lock()
defer lock.Unlock()
}
rootPath := d.rootImagePath()
log.Infof("Disconnect lock root image path %s", rootPath)
lock.Lock()
defer lock.Unlock()
defer log.Infof("Disconnect unlock root image path %s", rootPath)
if !d.putdownLVMs() {
return fmt.Errorf("failed putdown lvm devices %s", d.nbdDev)
}
Expand Down

0 comments on commit e2969ee

Please sign in to comment.