github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/graphdriver/devmapper/mount.go (about) 1 // +build linux 2 3 package devmapper // import "github.com/demonoid81/moby/daemon/graphdriver/devmapper" 4 5 import ( 6 "bytes" 7 "fmt" 8 "os" 9 ) 10 11 type probeData struct { 12 fsName string 13 magic string 14 offset uint64 15 } 16 17 // ProbeFsType returns the filesystem name for the given device id. 18 func ProbeFsType(device string) (string, error) { 19 probes := []probeData{ 20 {"btrfs", "_BHRfS_M", 0x10040}, 21 {"ext4", "\123\357", 0x438}, 22 {"xfs", "XFSB", 0}, 23 } 24 25 maxLen := uint64(0) 26 for _, p := range probes { 27 l := p.offset + uint64(len(p.magic)) 28 if l > maxLen { 29 maxLen = l 30 } 31 } 32 33 file, err := os.Open(device) 34 if err != nil { 35 return "", err 36 } 37 defer file.Close() 38 39 buffer := make([]byte, maxLen) 40 l, err := file.Read(buffer) 41 if err != nil { 42 return "", err 43 } 44 45 if uint64(l) != maxLen { 46 return "", fmt.Errorf("devmapper: unable to detect filesystem type of %s, short read", device) 47 } 48 49 for _, p := range probes { 50 if bytes.Equal([]byte(p.magic), buffer[p.offset:p.offset+uint64(len(p.magic))]) { 51 return p.fsName, nil 52 } 53 } 54 55 return "", fmt.Errorf("devmapper: Unknown filesystem type on %s", device) 56 } 57 58 func joinMountOptions(a, b string) string { 59 if a == "" { 60 return b 61 } 62 if b == "" { 63 return a 64 } 65 return a + "," + b 66 }