Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/nvme list #7

Merged
merged 4 commits into from
Feb 14, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions gonvme_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,70 @@ func (nvme *NVMeTCP) nvmeDisonnect(target NVMeTarget) error {
return err
}

// ListNamespaceDevices returns the NVMe namespace Device Paths and each output content
func (nvme *NVMeTCP) ListNamespaceDevices() map[string][]string {
exe := nvme.buildNVMeCommand([]string{"nvme", "list", "-o", "json"})
cmd := exec.Command(exe[0], exe[1:]...)

output, _ := cmd.Output()
str := string(output)
lines := strings.Split(str, "\n")

var devicePaths []string
for _, line := range lines {
line = strings.ReplaceAll(strings.TrimSpace(line), ",", "")

if strings.HasPrefix(line, "\"DevicePath\"") {
devicePath := strings.ReplaceAll(strings.TrimSpace(strings.Split(line, ":")[1]), "\"", "")
francis-nijay marked this conversation as resolved.
Show resolved Hide resolved
devicePaths = append(devicePaths, devicePath)
}
}

namespaceDevices := make(map[string][]string)

for _, devicePath := range devicePaths {

exe := nvme.buildNVMeCommand([]string{"nvme", "list-ns", devicePath})
cmd := exec.Command(exe[0], exe[1:]...)
output, _ := cmd.Output()

str := string(output)
lines := strings.Split(str, "\n")

var namespaceDevice []string
for _, line := range lines {
line = strings.TrimSpace(line)
if line != "" {
nsDevice := strings.Split(line, ":")[1]
namespaceDevice = append(namespaceDevice, nsDevice)
}
}
namespaceDevices[devicePath] = namespaceDevice
}
return namespaceDevices
}

// GetNamespaceData returns the information of namespace specific to the namespace Id
func (nvme *NVMeTCP) GetNamespaceData(path string, namespaceID string) (string, error) {

var nguid string

exe := nvme.buildNVMeCommand([]string{"nvme", "id-ns", path, "--namespace", namespaceID})
cmd := exec.Command(exe[0], exe[1:]...)

output, error := cmd.Output()
str := string(output)
lines := strings.Split(str, "\n")

for _, line := range lines {
if strings.HasPrefix(line, "nguid") {
nguid = strings.TrimSpace(strings.Split(line, ":")[1])
return nguid, nil
}
}
return nguid, error
}

// GetSessions queries information about NVMe sessions
func (nvme *NVMeTCP) GetSessions() ([]NVMESession, error) {
exe := nvme.buildNVMeCommand([]string{"nvme", "list-subsys", "-o", "json"})
Expand Down