fix: get hostid&hostnqn from Host File

Signed-off-by: Meinhard Zhou <zhouenhua@bytedance.com>
This commit is contained in:
Meinhard Zhou
2025-09-18 14:29:35 +08:00
parent e351c0ef98
commit ac848b7ec2
2 changed files with 61 additions and 36 deletions

View File

@@ -36,11 +36,24 @@ type Connector struct {
TargetPort string TargetPort string
Transport string Transport string
HostNqn string HostNqn string
HostId string
RetryCount int32 RetryCount int32
CheckInterval int32 CheckInterval int32
} }
func getNvmfConnector(nvmfInfo *nvmfDiskInfo, hostnqn string) *Connector { func getNvmfConnector(nvmfInfo *nvmfDiskInfo) *Connector {
hostnqnData, err := os.ReadFile("/etc/nvme/hostnqn")
hostnqn := strings.TrimSpace(string(hostnqnData))
if err != nil {
hostnqn = ""
}
hostidData, err := os.ReadFile("/etc/nvme/hostid")
hostid := strings.TrimSpace(string(hostidData))
if err != nil {
hostid = ""
}
return &Connector{ return &Connector{
VolumeID: nvmfInfo.VolName, VolumeID: nvmfInfo.VolName,
DeviceUUID: nvmfInfo.DeviceUUID, DeviceUUID: nvmfInfo.DeviceUUID,
@@ -49,6 +62,7 @@ func getNvmfConnector(nvmfInfo *nvmfDiskInfo, hostnqn string) *Connector {
TargetPort: nvmfInfo.Port, TargetPort: nvmfInfo.Port,
Transport: nvmfInfo.Transport, Transport: nvmfInfo.Transport,
HostNqn: hostnqn, HostNqn: hostnqn,
HostId: hostid,
} }
} }
@@ -176,17 +190,19 @@ func disconnectByNqn(nqn, hostnqn string) int {
} }
// delete hostnqn file // delete hostnqn file
hostnqnPath := filepath.Join(RUN_NVMF, nqn, b64.StdEncoding.EncodeToString([]byte(hostnqn))) if hostnqn != "" {
os.Remove(hostnqnPath) hostnqnPath := filepath.Join(RUN_NVMF, nqn, b64.StdEncoding.EncodeToString([]byte(hostnqn)))
os.Remove(hostnqnPath)
}
// delete nqn directory if has no hostnqn files // delete nqn directory if has no hostnqn files
nqnPath := filepath.Join(RUN_NVMF, nqn) nqnPath := filepath.Join(RUN_NVMF, nqn)
hostnqns, err := os.ReadDir(nqnPath) hostnqnFiles, err := os.ReadDir(nqnPath)
if err != nil { if err != nil {
klog.Errorf("Disconnect: readdir %s err: %v", nqnPath, err) klog.Errorf("Disconnect: readdir %s err: %v", nqnPath, err)
return -ENOENT return -ENOENT
} }
if len(hostnqns) <= 0 { if len(hostnqnFiles) <= 0 {
os.RemoveAll(nqnPath) os.RemoveAll(nqnPath)
} }
@@ -197,29 +213,26 @@ func disconnectByNqn(nqn, hostnqn string) int {
} }
for _, device := range devices { for _, device := range devices {
if err := disconnectSubsysWithHostNqn(nqn, hostnqn, device.Name()); err != nil { if hostnqn != "" {
if _, ok := err.(*UnsupportedHostnqnError); ok { if err := disconnectSubsysWithHostNqn(nqn, hostnqn, device.Name()); err == nil {
klog.Infof("Fallback because you have no hostnqn supports!") klog.Infof("Fallback because you have no hostnqn supports!")
ret++
// disconnect all controllers if has no hostnqn files
if len(hostnqns) <= 0 {
devices, err := os.ReadDir(SYS_NVMF)
if err != nil {
klog.Errorf("Disconnect: readdir %s err: %s", SYS_NVMF, err)
return -ENOENT
}
for _, device := range devices {
if err := disconnectSubsys(nqn, device.Name()); err == nil {
ret++
}
}
}
return ret
} }
} else { } else {
ret++ // disconnect all controllers if has no hostnqn files
if len(hostnqnFiles) <= 0 {
devices, err := os.ReadDir(SYS_NVMF)
if err != nil {
klog.Errorf("Disconnect: readdir %s err: %s", SYS_NVMF, err)
return -ENOENT
}
for _, device := range devices {
if err := disconnectSubsys(nqn, device.Name()); err == nil {
ret++
}
}
}
} }
} }
@@ -244,7 +257,17 @@ func (c *Connector) Connect() (string, error) {
return "", fmt.Errorf("csi transport only support tcp/rdma ") return "", fmt.Errorf("csi transport only support tcp/rdma ")
} }
baseString := fmt.Sprintf("nqn=%s,transport=%s,traddr=%s,trsvcid=%s,hostnqn=%s", c.TargetNqn, c.Transport, c.TargetAddr, c.TargetPort, c.HostNqn) var builder strings.Builder
builder.WriteString(fmt.Sprintf("nqn=%s,transport=%s,traddr=%s,trsvcid=%s", c.TargetNqn, c.Transport, c.TargetAddr, c.TargetPort))
if c.HostNqn != "" {
builder.WriteString(fmt.Sprintf(",hostnqn=%s", c.HostNqn))
}
if c.HostId != "" {
builder.WriteString(fmt.Sprintf(",hostid=%s", c.HostId))
}
baseString := builder.String()
devicePath := strings.Join([]string{"/dev/disk/by-id/nvme-uuid", c.DeviceUUID}, ".") devicePath := strings.Join([]string{"/dev/disk/by-id/nvme-uuid", c.DeviceUUID}, ".")
// connect to nvmf disk // connect to nvmf disk
@@ -275,17 +298,19 @@ func (c *Connector) Connect() (string, error) {
} }
// create hostnqn file // create hostnqn file
hostnqnPath := filepath.Join(RUN_NVMF, c.TargetNqn, b64.StdEncoding.EncodeToString([]byte(c.HostNqn))) if c.HostNqn != "" {
file, err := os.Create(hostnqnPath) hostnqnPath := filepath.Join(RUN_NVMF, c.TargetNqn, b64.StdEncoding.EncodeToString([]byte(c.HostNqn)))
if err != nil { file, err := os.Create(hostnqnPath)
klog.Errorf("create hostnqn file %s:%s error %v, rollback!!!", c.TargetNqn, c.HostNqn, err) if err != nil {
ret := disconnectByNqn(c.TargetNqn, c.HostNqn) klog.Errorf("create hostnqn file %s:%s error %v, rollback!!!", c.TargetNqn, c.HostNqn, err)
if ret < 0 { ret := disconnectByNqn(c.TargetNqn, c.HostNqn)
klog.Errorf("rollback error !!!") if ret < 0 {
klog.Errorf("rollback error !!!")
}
return "", err
} }
return "", err defer file.Close()
} }
defer file.Close()
klog.Infof("After connect we're returning devicePath: %s", devicePath) klog.Infof("After connect we're returning devicePath: %s", devicePath)
return devicePath, nil return devicePath, nil

View File

@@ -88,7 +88,7 @@ func getNVMfDiskMounter(nvmfInfo *nvmfDiskInfo, req *csi.NodePublishVolumeReques
mounter: &mount.SafeFormatAndMount{Interface: mount.New(""), Exec: exec.New()}, mounter: &mount.SafeFormatAndMount{Interface: mount.New(""), Exec: exec.New()},
exec: exec.New(), exec: exec.New(),
targetPath: req.GetTargetPath(), targetPath: req.GetTargetPath(),
connector: getNvmfConnector(nvmfInfo, req.GetTargetPath()), connector: getNvmfConnector(nvmfInfo),
} }
} }