gopkg.in/dotcloud/docker.v1@v1.13.1/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  // Mounted returns true if a mount point exists.
    17  func Mounted(mountpoint string) (bool, error) {
    18  	mntpoint, err := os.Stat(mountpoint)
    19  	if err != nil {
    20  		if os.IsNotExist(err) {
    21  			return false, nil
    22  		}
    23  		return false, err
    24  	}
    25  	parent, err := os.Stat(filepath.Join(mountpoint, ".."))
    26  	if err != nil {
    27  		return false, err
    28  	}
    29  	mntpointSt := mntpoint.Sys().(*syscall.Stat_t)
    30  	parentSt := parent.Sys().(*syscall.Stat_t)
    31  	return mntpointSt.Dev != parentSt.Dev, nil
    32  }
    33  
    34  type probeData struct {
    35  	fsName string
    36  	magic  string
    37  	offset uint64
    38  }
    39  
    40  // ProbeFsType returns the filesystem name for the given device id.
    41  func ProbeFsType(device string) (string, error) {
    42  	probes := []probeData{
    43  		{"btrfs", "_BHRfS_M", 0x10040},
    44  		{"ext4", "\123\357", 0x438},
    45  		{"xfs", "XFSB", 0},
    46  	}
    47  
    48  	maxLen := uint64(0)
    49  	for _, p := range probes {
    50  		l := p.offset + uint64(len(p.magic))
    51  		if l > maxLen {
    52  			maxLen = l
    53  		}
    54  	}
    55  
    56  	file, err := os.Open(device)
    57  	if err != nil {
    58  		return "", err
    59  	}
    60  	defer file.Close()
    61  
    62  	buffer := make([]byte, maxLen)
    63  	l, err := file.Read(buffer)
    64  	if err != nil {
    65  		return "", err
    66  	}
    67  
    68  	if uint64(l) != maxLen {
    69  		return "", fmt.Errorf("devmapper: unable to detect filesystem type of %s, short read", device)
    70  	}
    71  
    72  	for _, p := range probes {
    73  		if bytes.Equal([]byte(p.magic), buffer[p.offset:p.offset+uint64(len(p.magic))]) {
    74  			return p.fsName, nil
    75  		}
    76  	}
    77  
    78  	return "", fmt.Errorf("devmapper: Unknown filesystem type on %s", device)
    79  }
    80  
    81  func joinMountOptions(a, b string) string {
    82  	if a == "" {
    83  		return b
    84  	}
    85  	if b == "" {
    86  		return a
    87  	}
    88  	return a + "," + b
    89  }