Created
August 10, 2015 18:52
-
-
Save rootfs/b899b4bbd8ed667c424a to your computer and use it in GitHub Desktop.
given wwn and lun, find dm and disk path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"os" | |
"path/filepath" | |
"strings" | |
) | |
func findDM(disk string) string { | |
sys_path := "/sys/block/" | |
if dirs, err := ioutil.ReadDir(sys_path); err == nil { | |
for _, f := range dirs { | |
name := f.Name() | |
if strings.HasPrefix(name, "dm-") { | |
if _, err1 := os.Lstat(sys_path + name + "/slaves/" + disk); err1 == nil { | |
return name | |
} | |
} | |
} | |
} | |
return "" | |
} | |
func main() { | |
if len(os.Args) < 3 { | |
fmt.Fprintf(os.Stdout, "usage: %s wwn lun_number\n", os.Args[0]) | |
os.Exit(1) | |
} | |
wwn := os.Args[1] | |
lun := os.Args[2] | |
fc_path := "-fc-0x" + wwn + "-lun-" + lun | |
dev_path := "/dev/disk/by-path/" | |
if dirs, err := ioutil.ReadDir(dev_path); err == nil { | |
for _, f := range dirs { | |
name := f.Name() | |
if strings.Contains(name, fc_path) { | |
if disk, err1 := filepath.EvalSymlinks(dev_path + name); err1 == nil { | |
arr := strings.Split(disk, "/") | |
l := len(arr) - 1 | |
dev := arr[l] | |
dm := findDM(dev) | |
fmt.Fprintf(os.Stdout, "found %s, disk is %s, dm is %s\n", dev_path+name, disk, dm) | |
} | |
return | |
} | |
} | |
fmt.Fprintf(os.Stderr, "cannot find %s\n", os.Args[1]) | |
} else { | |
fmt.Fprintf(os.Stderr, "failed to open: %v\n", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment