Created
August 11, 2015 20:12
-
-
Save rootfs/2b11bd00218fe44d7b0e to your computer and use it in GitHub Desktop.
complete FC matching: rescan, create multipath.conf, etc
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 rescan() { | |
scsi_path := "/sys/class/scsi_host/" | |
if dirs, err := ioutil.ReadDir(scsi_path); err == nil { | |
for _, f := range dirs { | |
name := scsi_path + f.Name() + "/scan" | |
data := []byte("- - -") | |
ioutil.WriteFile(name, data, 0666) | |
} | |
} | |
} | |
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 "/dev/" + name | |
} | |
} | |
} | |
} | |
return "" | |
} | |
func createDMConf(path string) { | |
if _, err := os.Lstat(path); err != nil { | |
data := []byte(`defaults { | |
find_multipaths yes | |
user_friendly_names yes | |
} | |
blacklist { | |
} | |
`) | |
ioutil.WriteFile(path, data, 0664) | |
} | |
} | |
func findDisk(wwn, lun string) (string, string) { | |
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) | |
return disk, dm | |
} | |
} | |
} | |
} | |
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] | |
createDMConf("/etc/multipath.conf") | |
rescan() | |
disk, dm := findDisk(wwn, lun) | |
fmt.Fprintf(os.Stdout, "disk is %s, dm is %s\n", disk, dm) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment