2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux
|
2014-08-02 03:46:15 -04:00
|
|
|
// +build linux
|
2013-11-27 22:12:51 -05:00
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package devmapper // import "github.com/docker/docker/daemon/graphdriver/devmapper"
|
2013-11-07 17:37:33 -05:00
|
|
|
|
|
|
|
import (
|
2014-03-18 07:26:42 -04:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2014-05-16 08:10:02 -04:00
|
|
|
"os"
|
2013-11-07 17:37:33 -05:00
|
|
|
)
|
|
|
|
|
2014-03-18 07:26:42 -04:00
|
|
|
type probeData struct {
|
|
|
|
fsName string
|
|
|
|
magic string
|
|
|
|
offset uint64
|
|
|
|
}
|
|
|
|
|
2015-07-22 20:42:28 -04:00
|
|
|
// ProbeFsType returns the filesystem name for the given device id.
|
2014-03-18 07:26:42 -04:00
|
|
|
func ProbeFsType(device string) (string, error) {
|
|
|
|
probes := []probeData{
|
|
|
|
{"btrfs", "_BHRfS_M", 0x10040},
|
|
|
|
{"ext4", "\123\357", 0x438},
|
|
|
|
{"xfs", "XFSB", 0},
|
|
|
|
}
|
|
|
|
|
|
|
|
maxLen := uint64(0)
|
|
|
|
for _, p := range probes {
|
|
|
|
l := p.offset + uint64(len(p.magic))
|
|
|
|
if l > maxLen {
|
|
|
|
maxLen = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(device)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-02-26 21:08:33 -05:00
|
|
|
defer file.Close()
|
2014-03-18 07:26:42 -04:00
|
|
|
|
|
|
|
buffer := make([]byte, maxLen)
|
|
|
|
l, err := file.Read(buffer)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-02-26 21:08:33 -05:00
|
|
|
|
2014-03-18 07:26:42 -04:00
|
|
|
if uint64(l) != maxLen {
|
2015-12-14 16:03:33 -05:00
|
|
|
return "", fmt.Errorf("devmapper: unable to detect filesystem type of %s, short read", device)
|
2014-03-18 07:26:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range probes {
|
|
|
|
if bytes.Equal([]byte(p.magic), buffer[p.offset:p.offset+uint64(len(p.magic))]) {
|
|
|
|
return p.fsName, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-14 16:03:33 -05:00
|
|
|
return "", fmt.Errorf("devmapper: Unknown filesystem type on %s", device)
|
2014-03-18 07:26:42 -04:00
|
|
|
}
|
2014-03-18 09:23:43 -04:00
|
|
|
|
|
|
|
func joinMountOptions(a, b string) string {
|
|
|
|
if a == "" {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
if b == "" {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return a + "," + b
|
|
|
|
}
|