From 7093411a8dd36612ddc636c14395e33ecff47f7f Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sat, 12 Oct 2013 18:47:00 -0700 Subject: [PATCH 1/2] Initialize devicemapper in NewRuntimeFromDIrectory --- docker/docker.go | 3 +-- runtime.go | 9 ++++++--- runtime_test.go | 24 ++++++++++-------------- server.go | 4 ++-- utils_test.go | 3 +-- 5 files changed, 20 insertions(+), 23 deletions(-) diff --git a/docker/docker.go b/docker/docker.go index 8118dbb611..cb1a5020f4 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -4,7 +4,6 @@ import ( "flag" "fmt" "github.com/dotcloud/docker" - "github.com/dotcloud/docker/devmapper" "github.com/dotcloud/docker/utils" "io/ioutil" "log" @@ -134,7 +133,7 @@ func daemon(pidfile string, flGraphPath string, protoAddrs []string, autoRestart if flDns != "" { dns = []string{flDns} } - server, err := docker.NewServer(flGraphPath, devmapper.NewDeviceSetDM(flGraphPath), autoRestart, enableCors, dns) + server, err := docker.NewServer(flGraphPath, autoRestart, enableCors, dns) if err != nil { return err } diff --git a/runtime.go b/runtime.go index dfcb2c1c70..9799c86b34 100644 --- a/runtime.go +++ b/runtime.go @@ -4,6 +4,7 @@ import ( "container/list" "fmt" "github.com/dotcloud/docker/utils" + "github.com/dotcloud/docker/devmapper" "io" "io/ioutil" "log" @@ -555,8 +556,8 @@ func (runtime *Runtime) Commit(container *Container, repository, tag, comment, a } // FIXME: harmonize with NewGraph() -func NewRuntime(flGraphPath string, deviceSet DeviceSet, autoRestart bool, dns []string) (*Runtime, error) { - runtime, err := NewRuntimeFromDirectory(flGraphPath, deviceSet, autoRestart) +func NewRuntime(flGraphPath string, autoRestart bool, dns []string) (*Runtime, error) { + runtime, err := NewRuntimeFromDirectory(flGraphPath, autoRestart) if err != nil { return nil, err } @@ -574,7 +575,7 @@ func NewRuntime(flGraphPath string, deviceSet DeviceSet, autoRestart bool, dns [ return runtime, nil } -func NewRuntimeFromDirectory(root string, deviceSet DeviceSet, autoRestart bool) (*Runtime, error) { +func NewRuntimeFromDirectory(root string, autoRestart bool) (*Runtime, error) { runtimeRepo := path.Join(root, "containers") if err := os.MkdirAll(runtimeRepo, 0700); err != nil && !os.IsExist(err) { @@ -600,6 +601,8 @@ func NewRuntimeFromDirectory(root string, deviceSet DeviceSet, autoRestart bool) if err != nil { return nil, err } + deviceSet := devmapper.NewDeviceSetDM(root) + // Initialize devicemapper deviceSet runtime := &Runtime{ root: root, repository: runtimeRepo, diff --git a/runtime_test.go b/runtime_test.go index dd670f0ce0..cebd16a4fb 100644 --- a/runtime_test.go +++ b/runtime_test.go @@ -25,7 +25,6 @@ const ( unitTestImageID = "83599e29c455eb719f77d799bc7c51521b9551972f5a850d7ad265bc1b5292f6" // 1.0 unitTestNetworkBridge = "testdockbr0" unitTestStoreBase = "/var/lib/docker/unit-tests" - unitTestStoreDevicesBase = "/var/lib/docker/unit-tests-devices" testDaemonAddr = "127.0.0.1:4270" testDaemonProto = "tcp" ) @@ -51,6 +50,9 @@ func nuke(runtime *Runtime) error { for _, container := range runtime.List() { container.EnsureUnmounted() } + if err := runtime.deviceSet.Shutdown(); err != nil { + utils.Debugf("Error shutting down devicemapper for runtime %s", runtime.root) + } return os.RemoveAll(runtime.root) } @@ -166,24 +168,18 @@ func init() { log.Fatalf("Unable to cleanup devmapper: %s", err) } - // Always start from a clean set of loopback mounts - err := os.RemoveAll(unitTestStoreDevicesBase) - if err != nil { - panic(err) - } - - deviceset := devmapper.NewDeviceSetDM(unitTestStoreDevicesBase) - // Create a device, which triggers the initiation of the base FS - // This avoids other tests doing this and timing out - deviceset.AddDevice("init", "") - // Make it our Store root - if runtime, err := NewRuntimeFromDirectory(unitTestStoreBase, deviceset, false); err != nil { + if runtime, err := NewRuntimeFromDirectory(unitTestStoreBase, false); err != nil { log.Fatalf("Unable to create a runtime for tests:", err) } else { globalRuntime = runtime } + // Create a device, which triggers the initiation of the base FS + // This avoids other tests doing this and timing out + deviceset := devmapper.NewDeviceSetDM(unitTestStoreBase) + deviceset.AddDevice("init", "") + // Create the "Server" srv := &Server{ runtime: globalRuntime, @@ -548,7 +544,7 @@ func TestRestore(t *testing.T) { // Here are are simulating a docker restart - that is, reloading all containers // from scratch - runtime2, err := NewRuntimeFromDirectory(runtime1.root, runtime1.deviceSet, false) + runtime2, err := NewRuntimeFromDirectory(runtime1.root, false) if err != nil { t.Fatal(err) } diff --git a/server.go b/server.go index 45d8bbf9cf..7e6c556a3f 100644 --- a/server.go +++ b/server.go @@ -1337,11 +1337,11 @@ func (srv *Server) ContainerCopy(name string, resource string, out io.Writer) er } -func NewServer(flGraphPath string, deviceSet DeviceSet, autoRestart, enableCors bool, dns ListOpts) (*Server, error) { +func NewServer(flGraphPath string, autoRestart, enableCors bool, dns ListOpts) (*Server, error) { if runtime.GOARCH != "amd64" { log.Fatalf("The docker runtime currently only supports amd64 (not %s). This will change in the future. Aborting.", runtime.GOARCH) } - runtime, err := NewRuntime(flGraphPath, deviceSet, autoRestart, dns) + runtime, err := NewRuntime(flGraphPath, autoRestart, dns) if err != nil { return nil, err } diff --git a/utils_test.go b/utils_test.go index 9c7ab2f26c..013626a03a 100644 --- a/utils_test.go +++ b/utils_test.go @@ -6,7 +6,6 @@ import ( "io/ioutil" "os" "path" - "path/filepath" "strings" "testing" ) @@ -43,7 +42,7 @@ func newTestRuntime() (*Runtime, error) { return nil, err } - runtime, err := NewRuntimeFromDirectory(root, NewDeviceSetWrapper(globalRuntime.deviceSet, filepath.Base(root)), false) + runtime, err := NewRuntimeFromDirectory(root, false) if err != nil { return nil, err } From 5ebaca7e55e006083e6e9c2782e4f8b421275579 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Tue, 15 Oct 2013 21:27:47 +0000 Subject: [PATCH 2/2] devmapper: create device nodes 'on create' instead of 'on resume' --- devmapper/devmapper.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/devmapper/devmapper.go b/devmapper/devmapper.go index 97a388e4c9..a925c1a996 100644 --- a/devmapper/devmapper.go +++ b/devmapper/devmapper.go @@ -175,6 +175,11 @@ const ( DeviceSetGeometry ) +const ( + AddNodeOnResume AddNodeType = iota + AddNodeOnCreate +) + var ( ErrTaskRun = errors.New("dm_task_run failed") ErrTaskSetName = errors.New("dm_task_set_name failed") @@ -209,6 +214,7 @@ type ( TargetCount int32 } TaskType int + AddNodeType int ) func (t *Task) destroy() { @@ -274,6 +280,13 @@ func (t *Task) SetCookie(cookie *uint32, flags uint16) error { return nil } +func (t *Task) SetAddNode(add_node AddNodeType) error { + if res := C.dm_task_set_add_node(t.unmanaged, C.dm_add_node_t (add_node)); res != 1 { + return ErrTaskSetAddNode + } + return nil +} + func (t *Task) SetRo() error { if res := C.dm_task_set_ro(t.unmanaged); res != 1 { return ErrTaskSetRO @@ -614,6 +627,9 @@ func activateDevice(poolName string, name string, deviceId int, size uint64) err if err := task.AddTarget(0, size/512, "thin", params); err != nil { return fmt.Errorf("Can't add target") } + if err := task.SetAddNode(AddNodeOnCreate); err != nil { + return fmt.Errorf("Can't add node") + } var cookie uint32 = 0 if err := task.SetCookie(&cookie, 0); err != nil {