Created
August 10, 2015 14:36
-
-
Save rootfs/f02ac683ca1f4d110c1f to your computer and use it in GitHub Desktop.
given block device sdX, find its dm parent
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" | |
"strings" | |
) | |
func main() { | |
if len(os.Args) < 2 { | |
fmt.Fprintf(os.Stdout, "missing device name(e.g. sda)\n") | |
os.Exit(1) | |
} | |
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/" + os.Args[1]); err1 == nil { | |
fmt.Fprintf(os.Stderr, "found %s, dm is %s\n", os.Args[1], name) | |
return | |
} | |
} | |
} | |
fmt.Fprintf(os.Stderr, "cannot find %s\n", os.Args[1]) | |
} else { | |
fmt.Fprintf(os.Stderr, "failed to open /sys/block: %v\n", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment