github.com/daaku/docker@v1.5.0/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  
    59  	buffer := make([]byte, maxLen)
    60  	l, err := file.Read(buffer)
    61  	if err != nil {
    62  		return "", err
    63  	}
    64  	file.Close()
    65  	if uint64(l) != maxLen {
    66  		return "", fmt.Errorf("unable to detect filesystem type of %s, short read", device)
    67  	}
    68  
    69  	for _, p := range probes {
    70  		if bytes.Equal([]byte(p.magic), buffer[p.offset:p.offset+uint64(len(p.magic))]) {
    71  			return p.fsName, nil
    72  		}
    73  	}
    74  
    75  	return "", fmt.Errorf("Unknown filesystem type on %s", device)
    76  }
    77  
    78  func joinMountOptions(a, b string) string {
    79  	if a == "" {
    80  		return b
    81  	}
    82  	if b == "" {
    83  		return a
    84  	}
    85  	return a + "," + b
    86  }