From 8943710092219509a7e0a59ca736a4593f1f9a8f Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Fri, 29 Jul 2016 16:21:05 +0900 Subject: [PATCH] fs inspector support for namazu container Fix #161 --- README.md | 2 ++ nmz/cli/container/run/run.go | 7 ++++++- nmz/container/fs.go | 38 ++++++++++++++++++++++++++++++++++++ nmz/container/start.go | 32 +++++++++++++++++++++++++++++- nmz/util/config/config.go | 1 + 5 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 nmz/container/fs.go diff --git a/README.md b/README.md index 1ab4167..c82ec24 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,8 @@ explorePolicy = "random" # Default: true enableProcInspector = true procWatchInterval = "1s" + # Default: true (for volumes (`-v /foo:/bar`)) + enableFSInspector = true ``` For other parameters, please refer to [`config.go`](nmz/util/config/config.go) and [`randompolicy.go`](nmz/explorepolicy/random/randompolicy.go). diff --git a/nmz/cli/container/run/run.go b/nmz/cli/container/run/run.go index 761c8e1..5786088 100644 --- a/nmz/cli/container/run/run.go +++ b/nmz/cli/container/run/run.go @@ -89,6 +89,11 @@ func Run(args []string) int { return 1 } + dockerOpt, err = container.StartNamazuRoutinesPre(dockerOpt, nmzCfg) + if err != nil { + panic(log.Critical(err)) + } + client, err := ns.NewDockerClient() if err != nil { panic(log.Critical(err)) @@ -113,7 +118,7 @@ func Run(args []string) int { log.Info("Namazu container is running the container in background, but Namazu itself keeps running in foreground.") } - err = container.StartNamazuRoutines(c, nmzCfg) + err = container.StartNamazuRoutinesPost(c, nmzCfg) if err != nil { panic(log.Critical(err)) } diff --git a/nmz/container/fs.go b/nmz/container/fs.go new file mode 100644 index 0000000..8174687 --- /dev/null +++ b/nmz/container/fs.go @@ -0,0 +1,38 @@ +// Copyright (C) 2015 Nippon Telegraph and Telephone Corporation. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +// implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package container + +import ( + "github.com/osrg/hookfs/hookfs" + "github.com/osrg/namazu/nmz/inspector/fs" + ocutil "github.com/osrg/namazu/nmz/util/orchestrator" +) + +func ServeFSInspector(orig, mountpoint string) error { + hook := fs.FilesystemInspector{ + OrchestratorURL: ocutil.LocalOrchestratorURL, + EntityID: "_namazu_container_fs_inspector_" + orig, + } + fs, err := hookfs.NewHookFs(orig, mountpoint, &hook) + if err != nil { + return err + } + if err = fs.Serve(); err != nil { + return err + } + // NOTREACHED + return nil +} diff --git a/nmz/container/start.go b/nmz/container/start.go index 37e2ae7..6f3b226 100644 --- a/nmz/container/start.go +++ b/nmz/container/start.go @@ -17,13 +17,15 @@ package container import ( "fmt" + "io/ioutil" + "strings" log "github.com/cihub/seelog" docker "github.com/fsouza/go-dockerclient" "github.com/osrg/namazu/nmz/util/config" ) -func StartNamazuRoutines(c *docker.Container, cfg config.Config) error { +func StartNamazuRoutinesPre(dockerOpt *docker.CreateContainerOptions, cfg config.Config) (*docker.CreateContainerOptions, error) { log.Debugf("Starting Orchestrator") go func() { oerr := StartOrchestrator(cfg) @@ -32,6 +34,34 @@ func StartNamazuRoutines(c *docker.Container, cfg config.Config) error { } }() + var newBinds []string + for _, bind := range dockerOpt.HostConfig.Binds { + split := strings.Split(bind, ":") + if len(split) != 2 { + return dockerOpt, fmt.Errorf("bind is expected to be :, got %s", bind) + } + bindSrc, bindDst := split[0], split[1] + mountpoint, err := ioutil.TempDir("", "nmz-container-fs-inspector") + if err != nil { + return dockerOpt, err + } + if cfg.GetBool("container.enableFSInspector") { + log.Debugf("Starting Filesystem Inspector for %s (on %s)", bindSrc, mountpoint) + log.Warnf("Please run `fusermount -i %s` manually on exit", mountpoint) + go func() { + ierr := ServeFSInspector(bindSrc, mountpoint) + if ierr != nil { + panic(log.Critical(ierr)) + } + }() + } + newBinds = append(newBinds, fmt.Sprintf("%s:%s", mountpoint, bindDst)) + } + dockerOpt.HostConfig.Binds = newBinds + return dockerOpt, nil +} + +func StartNamazuRoutinesPost(c *docker.Container, cfg config.Config) error { if cfg.GetBool("container.enableEthernetInspector") { nfqNum := cfg.GetInt("container.ethernetNFQNumber") if nfqNum <= 0 { diff --git a/nmz/util/config/config.go b/nmz/util/config/config.go index 1697f9e..aa94a29 100644 --- a/nmz/util/config/config.go +++ b/nmz/util/config/config.go @@ -90,6 +90,7 @@ func New() Config { cfg.SetDefault("container", map[string]interface{}{ "enableEthernetInspector": false, "enableProcInspector": true, + "enableFSInspector": true, "ethernetNFQNumber": 42, "procWatchInterval": time.Second, })