go fmt and aufs support removed

This commit is contained in:
Victor Vieux 2013-09-26 15:40:13 +00:00
parent 5e1d540209
commit ebfa24acb0
11 changed files with 124 additions and 396 deletions

View File

@ -566,7 +566,6 @@ func TestPostCommit(t *testing.T) {
srv := &Server{runtime: runtime} srv := &Server{runtime: runtime}
// Create a container and remove a file // Create a container and remove a file
container, err := runtime.Create( container, err := runtime.Create(
&Config{ &Config{

View File

@ -84,7 +84,7 @@ func Tar(path string, compression Compression) (io.Reader, error) {
} }
func escapeName(name string) string { func escapeName(name string) string {
escaped := make([]byte,0) escaped := make([]byte, 0)
for i, c := range []byte(name) { for i, c := range []byte(name) {
if i == 0 && c == '/' { if i == 0 && c == '/' {
continue continue
@ -102,7 +102,7 @@ func escapeName(name string) string {
// Tar creates an archive from the directory at `path`, only including files whose relative // Tar creates an archive from the directory at `path`, only including files whose relative
// paths are included in `filter`. If `filter` is nil, then all files are included. // paths are included in `filter`. If `filter` is nil, then all files are included.
func TarFilter(path string, compression Compression, filter []string, recursive bool, createFiles []string) (io.Reader, error) { func TarFilter(path string, compression Compression, filter []string, recursive bool, createFiles []string) (io.Reader, error) {
args := []string{"tar", "--numeric-owner", "-f", "-", "-C", path, "-T", "-",} args := []string{"tar", "--numeric-owner", "-f", "-", "-C", path, "-T", "-"}
if filter == nil { if filter == nil {
filter = []string{"."} filter = []string{"."}
} }
@ -142,7 +142,7 @@ func TarFilter(path string, compression Compression, filter []string, recursive
} }
} }
return CmdStream(exec.Command(args[0], args[1:]...), &files, func () { return CmdStream(exec.Command(args[0], args[1:]...), &files, func() {
if tmpDir != "" { if tmpDir != "" {
_ = os.RemoveAll(tmpDir) _ = os.RemoveAll(tmpDir)
} }

View File

@ -34,78 +34,6 @@ func (change *Change) String() string {
return fmt.Sprintf("%s %s", kind, change.Path) return fmt.Sprintf("%s %s", kind, change.Path)
} }
func ChangesAUFS(layers []string, rw string) ([]Change, error) {
var changes []Change
err := filepath.Walk(rw, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
// Rebase path
path, err = filepath.Rel(rw, path)
if err != nil {
return err
}
path = filepath.Join("/", path)
// Skip root
if path == "/" {
return nil
}
// Skip AUFS metadata
if matched, err := filepath.Match("/.wh..wh.*", path); err != nil || matched {
return err
}
change := Change{
Path: path,
}
// Find out what kind of modification happened
file := filepath.Base(path)
// If there is a whiteout, then the file was removed
if strings.HasPrefix(file, ".wh.") {
originalFile := file[len(".wh."):]
change.Path = filepath.Join(filepath.Dir(path), originalFile)
change.Kind = ChangeDelete
} else {
// Otherwise, the file was added
change.Kind = ChangeAdd
// ...Unless it already existed in a top layer, in which case, it's a modification
for _, layer := range layers {
stat, err := os.Stat(filepath.Join(layer, path))
if err != nil && !os.IsNotExist(err) {
return err
}
if err == nil {
// The file existed in the top layer, so that's a modification
// However, if it's a directory, maybe it wasn't actually modified.
// If you modify /foo/bar/baz, then /foo will be part of the changed files only because it's the parent of bar
if stat.IsDir() && f.IsDir() {
if f.Size() == stat.Size() && f.Mode() == stat.Mode() && f.ModTime() == stat.ModTime() {
// Both directories are the same, don't record the change
return nil
}
}
change.Kind = ChangeModify
break
}
}
}
// Record change
changes = append(changes, change)
return nil
})
if err != nil && !os.IsNotExist(err) {
return nil, err
}
return changes, nil
}
type FileInfo struct { type FileInfo struct {
parent *FileInfo parent *FileInfo
name string name string
@ -132,20 +60,20 @@ func (root *FileInfo) LookUp(path string) *FileInfo {
return parent return parent
} }
func (info *FileInfo)path() string { func (info *FileInfo) path() string {
if info.parent == nil { if info.parent == nil {
return "/" return "/"
} }
return filepath.Join(info.parent.path(), info.name) return filepath.Join(info.parent.path(), info.name)
} }
func (info *FileInfo)unlink() { func (info *FileInfo) unlink() {
if info.parent != nil { if info.parent != nil {
delete(info.parent.children, info.name) delete(info.parent.children, info.name)
} }
} }
func (info *FileInfo)Remove(path string) bool { func (info *FileInfo) Remove(path string) bool {
child := info.LookUp(path) child := info.LookUp(path)
if child != nil { if child != nil {
child.unlink() child.unlink()
@ -154,12 +82,11 @@ func (info *FileInfo)Remove(path string) bool {
return false return false
} }
func (info *FileInfo)isDir() bool { func (info *FileInfo) isDir() bool {
return info.parent == nil || info.stat.Mode&syscall.S_IFDIR == syscall.S_IFDIR return info.parent == nil || info.stat.Mode&syscall.S_IFDIR == syscall.S_IFDIR
} }
func (info *FileInfo) addChanges(oldInfo *FileInfo, changes *[]Change) {
func (info *FileInfo)addChanges(oldInfo *FileInfo, changes *[]Change) {
if oldInfo == nil { if oldInfo == nil {
// add // add
change := Change{ change := Change{
@ -198,7 +125,7 @@ func (info *FileInfo)addChanges(oldInfo *FileInfo, changes *[]Change) {
oldStat.Gid != newStat.Gid || oldStat.Gid != newStat.Gid ||
oldStat.Rdev != newStat.Rdev || oldStat.Rdev != newStat.Rdev ||
// Don't look at size for dirs, its not a good measure of change // Don't look at size for dirs, its not a good measure of change
(oldStat.Size != newStat.Size && oldStat.Mode &syscall.S_IFDIR != syscall.S_IFDIR) || (oldStat.Size != newStat.Size && oldStat.Mode&syscall.S_IFDIR != syscall.S_IFDIR) ||
oldMtime.Sec != newMtime.Sec || oldMtime.Sec != newMtime.Sec ||
oldMtime.Usec != newMtime.Usec { oldMtime.Usec != newMtime.Usec {
change := Change{ change := Change{
@ -223,10 +150,9 @@ func (info *FileInfo)addChanges(oldInfo *FileInfo, changes *[]Change) {
*changes = append(*changes, change) *changes = append(*changes, change)
} }
} }
func (info *FileInfo)Changes(oldInfo *FileInfo) []Change { func (info *FileInfo) Changes(oldInfo *FileInfo) []Change {
var changes []Change var changes []Change
info.addChanges(oldInfo, &changes) info.addChanges(oldInfo, &changes)
@ -234,9 +160,8 @@ func (info *FileInfo)Changes(oldInfo *FileInfo) []Change {
return changes return changes
} }
func newRootFileInfo() *FileInfo { func newRootFileInfo() *FileInfo {
root := &FileInfo { root := &FileInfo{
name: "/", name: "/",
children: make(map[string]*FileInfo), children: make(map[string]*FileInfo),
} }
@ -299,7 +224,7 @@ func applyLayer(root *FileInfo, layer string) error {
return fmt.Errorf("collectFileInfo: Unexpectedly no parent for %s", relPath) return fmt.Errorf("collectFileInfo: Unexpectedly no parent for %s", relPath)
} }
info := &FileInfo { info := &FileInfo{
name: filepath.Base(relPath), name: filepath.Base(relPath),
children: make(map[string]*FileInfo), children: make(map[string]*FileInfo),
parent: parent, parent: parent,
@ -314,7 +239,6 @@ func applyLayer(root *FileInfo, layer string) error {
return err return err
} }
func collectFileInfo(sourceDir string) (*FileInfo, error) { func collectFileInfo(sourceDir string) (*FileInfo, error) {
root := newRootFileInfo() root := newRootFileInfo()
@ -339,7 +263,7 @@ func collectFileInfo(sourceDir string) (*FileInfo, error) {
return fmt.Errorf("collectFileInfo: Unexpectedly no parent for %s", relPath) return fmt.Errorf("collectFileInfo: Unexpectedly no parent for %s", relPath)
} }
info := &FileInfo { info := &FileInfo{
name: filepath.Base(relPath), name: filepath.Base(relPath),
children: make(map[string]*FileInfo), children: make(map[string]*FileInfo),
parent: parent, parent: parent,
@ -365,7 +289,7 @@ func ChangesLayers(newDir string, layers []string) ([]Change, error) {
return nil, err return nil, err
} }
oldRoot := newRootFileInfo() oldRoot := newRootFileInfo()
for i := len(layers)-1; i >= 0; i-- { for i := len(layers) - 1; i >= 0; i-- {
layer := layers[i] layer := layers[i]
if err = applyLayer(oldRoot, layer); err != nil { if err = applyLayer(oldRoot, layer); err != nil {
return nil, err return nil, err

View File

@ -25,7 +25,6 @@ func (wrapper *DeviceSetWrapper) wrap(hash string) string {
return hash return hash
} }
func (wrapper *DeviceSetWrapper) AddDevice(hash, baseHash string) error { func (wrapper *DeviceSetWrapper) AddDevice(hash, baseHash string) error {
return wrapper.wrapped.AddDevice(wrapper.wrap(hash), wrapper.wrap(baseHash)) return wrapper.wrapped.AddDevice(wrapper.wrap(hash), wrapper.wrap(baseHash))
} }

140
image.go
View File

@ -8,9 +8,7 @@ import (
"github.com/dotcloud/docker/utils" "github.com/dotcloud/docker/utils"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"os/exec"
"path" "path"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -141,31 +139,6 @@ func mountPath(root string) string {
return path.Join(root, "mount") return path.Join(root, "mount")
} }
func MountAUFS(ro []string, rw string, target string) error {
// FIXME: Now mount the layers
rwBranch := fmt.Sprintf("%v=rw", rw)
roBranches := ""
for _, layer := range ro {
roBranches += fmt.Sprintf("%v=ro+wh:", layer)
}
branches := fmt.Sprintf("br:%v:%v", rwBranch, roBranches)
branches += ",xino=/dev/shm/aufs.xino"
//if error, try to load aufs kernel module
if err := mount("none", target, "aufs", 0, branches); err != nil {
log.Printf("Kernel does not support AUFS, trying to load the AUFS module with modprobe...")
if err := exec.Command("modprobe", "aufs").Run(); err != nil {
return fmt.Errorf("Unable to load the AUFS module")
}
log.Printf("...module loaded.")
if err := mount("none", target, "aufs", 0, branches); err != nil {
return fmt.Errorf("Unable to mount using aufs")
}
}
return nil
}
// TarLayer returns a tar archive of the image's filesystem layer. // TarLayer returns a tar archive of the image's filesystem layer.
func (image *Image) TarLayer(compression Compression) (Archive, error) { func (image *Image) TarLayer(compression Compression) (Archive, error) {
layerPath, err := image.layer() layerPath, err := image.layer()
@ -315,7 +288,7 @@ func (image *Image) applyLayer(layer, target string) error {
syscall.NsecToTimeval(srcStat.Mtim.Nano()), syscall.NsecToTimeval(srcStat.Mtim.Nano()),
} }
u := TimeUpdate { u := TimeUpdate{
path: targetPath, path: targetPath,
time: ts, time: ts,
} }
@ -335,7 +308,7 @@ func (image *Image) applyLayer(layer, target string) error {
update := updateTimes[i] update := updateTimes[i]
O_PATH := 010000000 // Not in syscall yet O_PATH := 010000000 // Not in syscall yet
fd, err := syscall.Open(update.path, syscall.O_RDWR | O_PATH | syscall.O_NOFOLLOW, 0600) fd, err := syscall.Open(update.path, syscall.O_RDWR|O_PATH|syscall.O_NOFOLLOW, 0600)
if err == syscall.EISDIR || err == syscall.ELOOP { if err == syscall.EISDIR || err == syscall.ELOOP {
// O_PATH not supported, use Utimes except on symlinks where Utimes doesn't work // O_PATH not supported, use Utimes except on symlinks where Utimes doesn't work
if err != syscall.ELOOP { if err != syscall.ELOOP {
@ -411,7 +384,6 @@ func (image *Image) ensureImageDevice(devices DeviceSet) error {
return err return err
} }
err = ioutil.WriteFile(path.Join(mountDir, ".docker-id"), []byte(image.ID), 0600) err = ioutil.WriteFile(path.Join(mountDir, ".docker-id"), []byte(image.ID), 0600)
if err != nil { if err != nil {
_ = devices.UnmountDevice(image.ID, mountDir) _ = devices.UnmountDevice(image.ID, mountDir)
@ -461,25 +433,7 @@ func (image *Image) ensureImageDevice(devices DeviceSet) error {
} }
func (image *Image) Mounted(runtime *Runtime, root, rw string) (bool, error) { func (image *Image) Mounted(runtime *Runtime, root, rw string) (bool, error) {
method := runtime.GetMountMethod()
if method == MountMethodFilesystem {
if _, err := os.Stat(rw); err != nil {
if os.IsNotExist(err) {
err = nil
}
return false, err
}
mountedPath := path.Join(rw, ".fs-mounted")
if _, err := os.Stat(mountedPath); err != nil {
if os.IsNotExist(err) {
err = nil
}
return false, err
}
return true, nil
} else {
return Mounted(root) return Mounted(root)
}
} }
func (image *Image) Mount(runtime *Runtime, root, rw string, id string) error { func (image *Image) Mount(runtime *Runtime, root, rw string, id string) error {
@ -492,23 +446,6 @@ func (image *Image) Mount(runtime *Runtime, root, rw string, id string) error {
return err return err
} }
switch runtime.GetMountMethod() {
case MountMethodNone:
return fmt.Errorf("No supported Mount implementation")
case MountMethodAUFS:
if err := os.Mkdir(rw, 0755); err != nil && !os.IsExist(err) {
return err
}
layers, err := image.layers()
if err != nil {
return err
}
if err := MountAUFS(layers, rw, root); err != nil {
return err
}
case MountMethodDeviceMapper:
devices, err := runtime.GetDeviceSet() devices, err := runtime.GetDeviceSet()
if err != nil { if err != nil {
return err return err
@ -541,47 +478,14 @@ func (image *Image) Mount(runtime *Runtime, root, rw string, id string) error {
return err return err
} }
} }
case MountMethodFilesystem:
if err := os.Mkdir(rw, 0755); err != nil && !os.IsExist(err) {
return err
}
layers, err := image.layers()
if err != nil {
return err
}
for i := len(layers)-1; i >= 0; i-- {
layer := layers[i]
if err = image.applyLayer(layer, root); err != nil {
return err
}
}
mountedPath := path.Join(rw, ".fs-mounted")
fo, err := os.Create(mountedPath)
if err != nil {
return err
}
fo.Close()
}
return nil return nil
} }
func (image *Image) Unmount(runtime *Runtime, root string, id string) error { func (image *Image) Unmount(runtime *Runtime, root string, id string) error {
switch runtime.GetMountMethod() {
case MountMethodNone:
return fmt.Errorf("No supported Unmount implementation")
case MountMethodAUFS:
return Unmount(root)
case MountMethodDeviceMapper:
// Try to deactivate the device as generally there is no use for it anymore // Try to deactivate the device as generally there is no use for it anymore
devices, err := runtime.GetDeviceSet() devices, err := runtime.GetDeviceSet()
if err != nil { if err != nil {
return err; return err
} }
err = devices.UnmountDevice(id, root) err = devices.UnmountDevice(id, root)
@ -590,24 +494,9 @@ func (image *Image) Unmount(runtime *Runtime, root string, id string) error {
} }
return devices.DeactivateDevice(id) return devices.DeactivateDevice(id)
case MountMethodFilesystem:
return nil
}
return nil
} }
func (image *Image) Changes(runtime *Runtime, root, rw, id string) ([]Change, error) { func (image *Image) Changes(runtime *Runtime, root, rw, id string) ([]Change, error) {
switch runtime.GetMountMethod() {
case MountMethodAUFS:
layers, err := image.layers()
if err != nil {
return nil, err
}
return ChangesAUFS(layers, rw)
case MountMethodDeviceMapper:
devices, err := runtime.GetDeviceSet() devices, err := runtime.GetDeviceSet()
if err != nil { if err != nil {
return nil, err return nil, err
@ -635,28 +524,9 @@ func (image *Image) Changes(runtime *Runtime, root, rw, id string) ([]Change, er
return nil, err return nil, err
} }
return changes, nil return changes, nil
case MountMethodFilesystem:
layers, err := image.layers()
if err != nil {
return nil, err
}
changes, err := ChangesLayers(root, layers)
if err != nil {
return nil, err
}
return changes, nil
}
return nil, fmt.Errorf("No supported Changes implementation")
} }
func (image *Image) ExportChanges(runtime *Runtime, root, rw, id string) (Archive, error) { func (image *Image) ExportChanges(runtime *Runtime, root, rw, id string) (Archive, error) {
switch runtime.GetMountMethod() {
case MountMethodAUFS:
return Tar(rw, Uncompressed)
case MountMethodFilesystem, MountMethodDeviceMapper:
changes, err := image.Changes(runtime, root, rw, id) changes, err := image.Changes(runtime, root, rw, id)
if err != nil { if err != nil {
return nil, err return nil, err
@ -676,12 +546,8 @@ func (image *Image) ExportChanges(runtime *Runtime, root, rw, id string) (Archiv
} }
return TarFilter(root, Uncompressed, files, false, deletions) return TarFilter(root, Uncompressed, files, false, deletions)
}
return nil, fmt.Errorf("No supported Changes implementation")
} }
func (image *Image) ShortID() string { func (image *Image) ShortID() string {
return utils.TruncateID(image.ID) return utils.TruncateID(image.ID)
} }

View File

@ -1,40 +1,11 @@
package docker package docker
import ( import (
"fmt"
"github.com/dotcloud/docker/utils"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"syscall" "syscall"
"time"
) )
func Unmount(target string) error {
if err := exec.Command("auplink", target, "flush").Run(); err != nil {
utils.Debugf("[warning]: couldn't run auplink before unmount: %s", err)
}
if err := syscall.Unmount(target, 0); err != nil {
return err
}
// Even though we just unmounted the filesystem, AUFS will prevent deleting the mntpoint
// for some time. We'll just keep retrying until it succeeds.
for retries := 0; retries < 1000; retries++ {
err := os.Remove(target)
if err == nil {
// rm mntpoint succeeded
return nil
}
if os.IsNotExist(err) {
// mntpoint doesn't exist anymore. Success.
return nil
}
// fmt.Printf("(%v) Remove %v returned: %v\n", retries, target, err)
time.Sleep(10 * time.Millisecond)
}
return fmt.Errorf("Umount: Failed to umount %v", target)
}
func Mounted(mountpoint string) (bool, error) { func Mounted(mountpoint string) (bool, error) {
mntpoint, err := os.Stat(mountpoint) mntpoint, err := os.Stat(mountpoint)
if err != nil { if err != nil {

View File

@ -17,14 +17,6 @@ import (
) )
var defaultDns = []string{"8.8.8.8", "8.8.4.4"} var defaultDns = []string{"8.8.8.8", "8.8.4.4"}
type MountMethod int
const (
MountMethodNone MountMethod = iota
MountMethodAUFS
MountMethodDeviceMapper
MountMethodFilesystem
)
type Capabilities struct { type Capabilities struct {
MemoryLimit bool MemoryLimit bool
@ -47,7 +39,6 @@ type Runtime struct {
srv *Server srv *Server
Dns []string Dns []string
deviceSet DeviceSet deviceSet DeviceSet
mountMethod MountMethod
} }
var sysInitPath string var sysInitPath string
@ -109,27 +100,6 @@ func hasFilesystemSupport(fstype string) bool {
return false return false
} }
func (runtime *Runtime) GetMountMethod() MountMethod {
if runtime.mountMethod == MountMethodNone {
// Try to automatically pick a method
if hasFilesystemSupport("aufs") {
utils.Debugf("Using AUFS backend.")
runtime.mountMethod = MountMethodAUFS
} else {
_ = exec.Command("modprobe", "aufs").Run()
if hasFilesystemSupport("aufs") {
utils.Debugf("Using AUFS backend.")
runtime.mountMethod = MountMethodAUFS
} else {
utils.Debugf("Using device-mapper backend.")
runtime.mountMethod = MountMethodDeviceMapper
}
}
}
return runtime.mountMethod
}
func (runtime *Runtime) GetDeviceSet() (DeviceSet, error) { func (runtime *Runtime) GetDeviceSet() (DeviceSet, error) {
if runtime.deviceSet == nil { if runtime.deviceSet == nil {
return nil, fmt.Errorf("No device set available") return nil, fmt.Errorf("No device set available")
@ -288,7 +258,7 @@ func (runtime *Runtime) Destroy(container *Container) error {
if err := os.RemoveAll(container.root); err != nil { if err := os.RemoveAll(container.root); err != nil {
return fmt.Errorf("Unable to remove filesystem for %v: %v", container.ID, err) return fmt.Errorf("Unable to remove filesystem for %v: %v", container.ID, err)
} }
if runtime.GetMountMethod() == MountMethodDeviceMapper && runtime.deviceSet.HasDevice(container.ID) { if runtime.deviceSet.HasDevice(container.ID) {
if err := runtime.deviceSet.RemoveDevice(container.ID); err != nil { if err := runtime.deviceSet.RemoveDevice(container.ID); err != nil {
return fmt.Errorf("Unable to remove device for %v: %v", container.ID, err) return fmt.Errorf("Unable to remove device for %v: %v", container.ID, err)
} }
@ -301,7 +271,7 @@ func (runtime *Runtime) DeleteImage(id string) error {
if err != nil { if err != nil {
return err return err
} }
if runtime.GetMountMethod() == MountMethodDeviceMapper && runtime.deviceSet.HasDevice(id) { if runtime.deviceSet.HasDevice(id) {
if err := runtime.deviceSet.RemoveDevice(id); err != nil { if err := runtime.deviceSet.RemoveDevice(id); err != nil {
return fmt.Errorf("Unable to remove device for %v: %v", id, err) return fmt.Errorf("Unable to remove device for %v: %v", id, err)
} }

View File

@ -3,8 +3,8 @@ package docker
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/dotcloud/docker/utils"
"github.com/dotcloud/docker/devmapper" "github.com/dotcloud/docker/devmapper"
"github.com/dotcloud/docker/utils"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@ -75,7 +75,6 @@ func cleanupLast(runtime *Runtime) error {
return nil return nil
} }
func layerArchive(tarfile string) (io.Reader, error) { func layerArchive(tarfile string) (io.Reader, error) {
// FIXME: need to close f somewhere // FIXME: need to close f somewhere
f, err := os.Open(tarfile) f, err := os.Open(tarfile)

View File

@ -2,11 +2,11 @@ package docker
import ( import (
"github.com/dotcloud/docker/utils" "github.com/dotcloud/docker/utils"
"path/filepath"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"path/filepath"
"strings" "strings"
"testing" "testing"
) )