diff --git a/pkg/hostman/diskutils/nbd/driver.go b/pkg/hostman/diskutils/nbd/driver.go index 88916638a8c..47692d729bd 100644 --- a/pkg/hostman/diskutils/nbd/driver.go +++ b/pkg/hostman/diskutils/nbd/driver.go @@ -20,6 +20,7 @@ import ( "path" "path/filepath" "strings" + "sync" "time" "yunion.io/x/log" @@ -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 { @@ -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 @@ -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 } @@ -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) }