From e239f6559093939cd1a2753d714dafecc21915e2 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Mon, 25 Jun 2018 14:10:17 -0400 Subject: [PATCH] Handle abs path for rootfs in oci hook Fixes #2412 Signed-off-by: Michael Crosby --- cmd/containerd/command/oci-hook.go | 34 +++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/cmd/containerd/command/oci-hook.go b/cmd/containerd/command/oci-hook.go index 1ddc2c7c0..f47f8e3ac 100644 --- a/cmd/containerd/command/oci-hook.go +++ b/cmd/containerd/command/oci-hook.go @@ -37,8 +37,12 @@ var ociHook = cli.Command{ if err != nil { return err } + spec, err := loadSpec(state.Bundle) + if err != nil { + return err + } var ( - ctx = newTemplateContext(state) + ctx = newTemplateContext(state, spec) args = []string(context.Args()) env = os.Environ() ) @@ -52,6 +56,25 @@ var ociHook = cli.Command{ }, } +type hookSpec struct { + Root struct { + Path string `json:"path"` + } `json:"root"` +} + +func loadSpec(bundle string) (*hookSpec, error) { + f, err := os.Open(filepath.Join(bundle, "config.json")) + if err != nil { + return nil, err + } + defer f.Close() + var s hookSpec + if err := json.NewDecoder(f).Decode(&s); err != nil { + return nil, err + } + return &s, nil +} + func loadHookState(r io.Reader) (*specs.State, error) { var s specs.State if err := json.NewDecoder(r).Decode(&s); err != nil { @@ -60,9 +83,10 @@ func loadHookState(r io.Reader) (*specs.State, error) { return &s, nil } -func newTemplateContext(state *specs.State) *templateContext { +func newTemplateContext(state *specs.State, spec *hookSpec) *templateContext { t := &templateContext{ state: state, + root: spec.Root.Path, } t.funcs = template.FuncMap{ "id": t.id, @@ -77,6 +101,7 @@ func newTemplateContext(state *specs.State) *templateContext { type templateContext struct { state *specs.State + root string funcs template.FuncMap } @@ -89,7 +114,10 @@ func (t *templateContext) bundle() string { } func (t *templateContext) rootfs() string { - return filepath.Join(t.state.Bundle, "rootfs") + if filepath.IsAbs(t.root) { + return t.root + } + return filepath.Join(t.state.Bundle, t.root) } func (t *templateContext) pid() int {