github.com/cloudfoundry-attic/garden-linux@v0.333.2-candidate/containerizer/system/mount_linux.go (about) 1 package system 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "syscall" 8 ) 9 10 type Mount struct { 11 Type MountType 12 SourcePath string 13 TargetPath string 14 Flags int 15 Data string 16 } 17 18 type MountType string 19 20 const ( 21 Tmpfs MountType = "tmpfs" 22 Proc = "proc" 23 Devpts = "devpts" 24 Bind = "bind" 25 Sys = "sysfs" 26 ) 27 28 func (m Mount) Mount() error { 29 isDir := true 30 sourcePath := m.SourcePath 31 32 if m.SourcePath != "" { 33 if info, err := os.Stat(m.SourcePath); err == nil { 34 isDir = info.IsDir() 35 } else { 36 return fmt.Errorf("system: source path stat: %s", err) 37 } 38 } else { 39 sourcePath = string(m.Type) 40 } 41 42 rootDir := m.TargetPath 43 rootDirPermissions := os.FileMode(0700) 44 45 if !isDir { 46 rootDir = filepath.Dir(m.TargetPath) 47 rootDirPermissions = os.FileMode(0755) 48 } 49 50 if err := os.MkdirAll(rootDir, rootDirPermissions); err != nil { 51 return fmt.Errorf("system: create mount point directory %s: %s", rootDir, err) 52 } 53 54 if !isDir { 55 if _, err := os.OpenFile(m.TargetPath, os.O_CREATE|os.O_RDONLY, 0700); err != nil { 56 return fmt.Errorf("system: create mount point file %s: %s", m.TargetPath, err) 57 } 58 } 59 60 if err := syscall.Mount(sourcePath, m.TargetPath, string(m.Type), uintptr(m.Flags), m.Data); err != nil { 61 return fmt.Errorf("system: mount %s on %s: %s", m.Type, m.TargetPath, err) 62 } 63 64 return nil 65 }