github.com/afein/docker@v1.8.2/daemon/graphdriver/devmapper/mount.go (about)

     1  // +build linux
     2  
     3  package devmapper
     4  
     5  import (
     6  	"bytes"
     7  	"fmt"
     8  	"os"
     9  	"path/filepath"
    10  	"syscall"
    11  )
    12  
    13  // FIXME: this is copy-pasted from the aufs driver.
    14  // It should be moved into the core.
    15  
    16  func Mounted(mountpoint string) (bool, error) {
    17  	mntpoint, err := os.Stat(mountpoint)
    18  	if err != nil {
    19  		if os.IsNotExist(err) {
    20  			return false, nil
    21  		}
    22  		return false, err
    23  	}
    24  	parent, err := os.Stat(filepath.Join(mountpoint, ".."))
    25  	if err != nil {
    26  		return false, err
    27  	}
    28  	mntpointSt := mntpoint.Sys().(*syscall.Stat_t)
    29  	parentSt := parent.Sys().(*syscall.Stat_t)
    30  	return mntpointSt.Dev != parentSt.Dev, nil
    31  }
    32  
    33  type probeData struct {
    34  	fsName string
    35  	magic  string
    36  	offset uint64
    37  }
    38  
    39  func ProbeFsType(device string) (string, error) {
    40  	probes := []probeData{
    41  		{"btrfs", "_BHRfS_M", 0x10040},
    42  		{"ext4", "\123\357", 0x438},
    43  		{"xfs", "XFSB", 0},
    44  	}
    45  
    46  	maxLen := uint64(0)
    47  	for _, p := range probes {
    48  		l := p.offset + uint64(len(p.magic))
    49  		if l > maxLen {
    50  			maxLen = l
    51  		}
    52  	}
    53  
    54  	file, err := os.Open(device)
    55  	if err != nil {
    56  		return "", err
    57  	}
    58  	defer file.Close()
    59  
    60  	buffer := make([]byte, maxLen)
    61  	l, err := file.Read(buffer)
    62  	if err != nil {
    63  		return "", err
    64  	}
    65  
    66  	if uint64(l) != maxLen {
    67  		return "", fmt.Errorf("unable to detect filesystem type of %s, short read", device)
    68  	}
    69  
    70  	for _, p := range probes {
    71  		if bytes.Equal([]byte(p.magic), buffer[p.offset:p.offset+uint64(len(p.magic))]) {
    72  			return p.fsName, nil
    73  		}
    74  	}
    75  
    76  	return "", fmt.Errorf("Unknown filesystem type on %s", device)
    77  }
    78  
    79  func joinMountOptions(a, b string) string {
    80  	if a == "" {
    81  		return b
    82  	}
    83  	if b == "" {
    84  		return a
    85  	}
    86  	return a + "," + b
    87  }