From 3ccf18b7d72ef484093e8a6f578ef9381418bc54 Mon Sep 17 00:00:00 2001
From: xiadanni1 <xiadanni1@huawei.com>
Date: Fri, 17 Jan 2020 07:07:34 +0800
Subject: [PATCH] containerd: modify runtime root if container is created by
 1.11.2

reason:if container is created by 1.11.2, runtime root is /run/runc,
so we need to modify the root dir when this container stops first time.

Change-Id: If30e26a719ed61be0a08344860a066ab77b4cb40
Signed-off-by: xiadanni1 <xiadanni1@huawei.com>
---
 runtime/v1/linux/runtime.go                        | 14 ++++---
 .../github.com/containerd/go-runc/command_linux.go |  4 +-
 .../github.com/containerd/go-runc/command_other.go |  2 +-
 vendor/github.com/containerd/go-runc/runc.go       | 45 ++++++++++++----------
 4 files changed, 37 insertions(+), 28 deletions(-)

diff --git a/runtime/v1/linux/runtime.go b/runtime/v1/linux/runtime.go
index c334bf4..08e563d 100644
--- a/runtime/v1/linux/runtime.go
+++ b/runtime/v1/linux/runtime.go
@@ -35,6 +35,7 @@ import (
 	"github.com/containerd/containerd/events"
 	"github.com/containerd/containerd/events/exchange"
 	"github.com/containerd/containerd/identifiers"
+	"github.com/containerd/containerd/legacy"
 	"github.com/containerd/containerd/log"
 	"github.com/containerd/containerd/metadata"
 	"github.com/containerd/containerd/mount"
@@ -521,11 +522,14 @@ func (r *Runtime) terminate(ctx context.Context, bundle *bundle, ns, id string)
 	}); err != nil {
 		log.G(ctx).WithError(err).Warnf("delete runtime state %s", id)
 	}
-	if err := mount.Unmount(filepath.Join(bundle.path, "rootfs"), 0); err != nil {
-		log.G(ctx).WithError(err).WithFields(logrus.Fields{
-			"path": bundle.path,
-			"id":   id,
-		}).Warnf("unmount task rootfs")
+
+	if !legacy.IsLegacy(id) {
+		if err := mount.Unmount(filepath.Join(bundle.path, "rootfs"), 0); err != nil {
+			log.G(ctx).WithError(err).WithFields(logrus.Fields{
+				"path": bundle.path,
+				"id":   id,
+			}).Warnf("unmount task rootfs")
+		}
 	}
 	return nil
 }
diff --git a/vendor/github.com/containerd/go-runc/command_linux.go b/vendor/github.com/containerd/go-runc/command_linux.go
index 6ad27be..0aa6040 100644
--- a/vendor/github.com/containerd/go-runc/command_linux.go
+++ b/vendor/github.com/containerd/go-runc/command_linux.go
@@ -31,12 +31,12 @@ func (r *Runc) isrunv() bool {
 	return false
 }
 
-func (r *Runc) command(context context.Context, args ...string) *exec.Cmd {
+func (r *Runc) command(id string, context context.Context, args ...string) *exec.Cmd {
 	command := r.Command
 	if command == "" {
 		command = DefaultCommand
 	}
-	cmd := exec.CommandContext(context, command, append(r.args(), args...)...)
+	cmd := exec.CommandContext(context, command, append(r.args(id), args...)...)
 	cmd.SysProcAttr = &syscall.SysProcAttr{
 		Setpgid: r.Setpgid,
 	}
diff --git a/vendor/github.com/containerd/go-runc/command_other.go b/vendor/github.com/containerd/go-runc/command_other.go
index b8fd4b8..21bb699 100644
--- a/vendor/github.com/containerd/go-runc/command_other.go
+++ b/vendor/github.com/containerd/go-runc/command_other.go
@@ -29,7 +29,7 @@ func (r *Runc) command(context context.Context, args ...string) *exec.Cmd {
 	if command == "" {
 		command = DefaultCommand
 	}
-	cmd := exec.CommandContext(context, command, append(r.args(), args...)...)
+	cmd := exec.CommandContext(context, command, append(r.args(""), args...)...)
 	cmd.Env = os.Environ()
 	return cmd
 }
diff --git a/vendor/github.com/containerd/go-runc/runc.go b/vendor/github.com/containerd/go-runc/runc.go
index 430648d..c1748ff 100644
--- a/vendor/github.com/containerd/go-runc/runc.go
+++ b/vendor/github.com/containerd/go-runc/runc.go
@@ -31,6 +31,7 @@ import (
 	"syscall"
 	"time"
 
+	"github.com/containerd/containerd/legacy"
 	specs "github.com/opencontainers/runtime-spec/specs-go"
 	"github.com/sirupsen/logrus"
 )
@@ -88,7 +89,7 @@ func init() {
 
 // List returns all containers created inside the provided runc root directory
 func (r *Runc) List(context context.Context) ([]*Container, error) {
-	data, err := cmdOutput(r.command(context, "list", "--format=json"), false)
+	data, err := cmdOutput(r.command("", context, "list", "--format=json"), false)
 	if err != nil {
 		return nil, err
 	}
@@ -101,7 +102,7 @@ func (r *Runc) List(context context.Context) ([]*Container, error) {
 
 // State returns the state for the container provided by id
 func (r *Runc) State(context context.Context, id string) (*Container, error) {
-	data, err := cmdOutputTimeout(r.command(context, "state", id), true, defaultTimeout)
+	data, err := cmdOutputTimeout(r.command(id, context, "state", id), true, defaultTimeout)
 	if err != nil {
 		return nil, fmt.Errorf("%s: %s", err, data)
 	}
@@ -168,7 +169,7 @@ func (r *Runc) Create(context context.Context, id, bundle string, opts *CreateOp
 		}
 		args = append(args, oargs...)
 	}
-	cmd := r.command(context, append(args, id)...)
+	cmd := r.command(id, context, append(args, id)...)
 	if opts != nil && opts.IO != nil {
 		opts.Set(cmd)
 	}
@@ -201,7 +202,7 @@ func (r *Runc) Create(context context.Context, id, bundle string, opts *CreateOp
 
 // Start will start an already created container
 func (r *Runc) Start(context context.Context, id string) error {
-	return r.runOrErrorTimeout(r.command(context, "start", id), startTimeout)
+	return r.runOrErrorTimeout(r.command(id, context, "start", id), startTimeout)
 }
 
 type ExecOpts struct {
@@ -249,7 +250,7 @@ func (r *Runc) Exec(context context.Context, id string, spec specs.Process, opts
 		}
 		args = append(args, oargs...)
 	}
-	cmd := r.command(context, append(args, id)...)
+	cmd := r.command(id, context, append(args, id)...)
 	if opts != nil && opts.IO != nil {
 		opts.Set(cmd)
 	}
@@ -289,7 +290,7 @@ func (r *Runc) Run(context context.Context, id, bundle string, opts *CreateOpts)
 		}
 		args = append(args, oargs...)
 	}
-	cmd := r.command(context, append(args, id)...)
+	cmd := r.command(id, context, append(args, id)...)
 	if opts != nil && opts.IO != nil {
 		opts.Set(cmd)
 	}
@@ -317,7 +318,7 @@ func (r *Runc) Delete(context context.Context, id string, opts *DeleteOpts) erro
 	if opts != nil {
 		args = append(args, opts.args()...)
 	}
-	return r.runOrError(r.command(context, append(args, id)...))
+	return r.runOrError(r.command(id, context, append(args, id)...))
 }
 
 // KillOpts specifies options for killing a container and its processes
@@ -340,12 +341,12 @@ func (r *Runc) Kill(context context.Context, id string, sig int, opts *KillOpts)
 	if opts != nil {
 		args = append(args, opts.args()...)
 	}
-	return r.runOrErrorTimeout(r.command(context, append(args, id, strconv.Itoa(sig))...), defaultTimeout)
+	return r.runOrErrorTimeout(r.command(id, context, append(args, id, strconv.Itoa(sig))...), defaultTimeout)
 }
 
 // Stats return the stats for a container like cpu, memory, and io
 func (r *Runc) Stats(context context.Context, id string) (*Stats, error) {
-	cmd := r.command(context, "events", "--stats", id)
+	cmd := r.command(id, context, "events", "--stats", id)
 	rd, err := cmd.StdoutPipe()
 	if err != nil {
 		return nil, err
@@ -367,7 +368,7 @@ func (r *Runc) Stats(context context.Context, id string) (*Stats, error) {
 
 // Events returns an event stream from runc for a container with stats and OOM notifications
 func (r *Runc) Events(context context.Context, id string, interval time.Duration) (chan *Event, error) {
-	cmd := r.command(context, "events", fmt.Sprintf("--interval=%ds", int(interval.Seconds())), id)
+	cmd := r.command(id, context, "events", fmt.Sprintf("--interval=%ds", int(interval.Seconds())), id)
 	rd, err := cmd.StdoutPipe()
 	if err != nil {
 		return nil, err
@@ -406,17 +407,17 @@ func (r *Runc) Events(context context.Context, id string, interval time.Duration
 
 // Pause the container with the provided id
 func (r *Runc) Pause(context context.Context, id string) error {
-	return r.runOrError(r.command(context, "pause", id))
+	return r.runOrError(r.command(id, context, "pause", id))
 }
 
 // Resume the container with the provided id
 func (r *Runc) Resume(context context.Context, id string) error {
-	return r.runOrError(r.command(context, "resume", id))
+	return r.runOrError(r.command(id, context, "resume", id))
 }
 
 // Ps lists all the processes inside the container returning their pids
 func (r *Runc) Ps(context context.Context, id string) ([]int, error) {
-	data, err := cmdOutputTimeout(r.command(context, "ps", "--format", "json", id), true, defaultTimeout)
+	data, err := cmdOutputTimeout(r.command(id, context, "ps", "--format", "json", id), true, defaultTimeout)
 	if err != nil {
 		return nil, fmt.Errorf("%s: %s", err, data)
 	}
@@ -429,7 +430,7 @@ func (r *Runc) Ps(context context.Context, id string) ([]int, error) {
 
 // Top lists all the processes inside the container returning the full ps data
 func (r *Runc) Top(context context.Context, id string, psOptions string) (*TopResults, error) {
-	data, err := cmdOutput(r.command(context, "ps", "--format", "table", id, psOptions), true)
+	data, err := cmdOutput(r.command(id, context, "ps", "--format", "table", id, psOptions), true)
 	if err != nil {
 		return nil, fmt.Errorf("%s: %s", err, data)
 	}
@@ -528,7 +529,7 @@ func (r *Runc) Checkpoint(context context.Context, id string, opts *CheckpointOp
 	for _, a := range actions {
 		args = a(args)
 	}
-	return r.runOrError(r.command(context, append(args, id)...))
+	return r.runOrError(r.command(id, context, append(args, id)...))
 }
 
 type RestoreOpts struct {
@@ -577,7 +578,7 @@ func (r *Runc) Restore(context context.Context, id, bundle string, opts *Restore
 		args = append(args, oargs...)
 	}
 	args = append(args, "--bundle", bundle)
-	cmd := r.command(context, append(args, id)...)
+	cmd := r.command(id, context, append(args, id)...)
 	if opts != nil && opts.IO != nil {
 		opts.Set(cmd)
 	}
@@ -604,7 +605,7 @@ func (r *Runc) Update(context context.Context, id string, resources *specs.Linux
 		return err
 	}
 	args := []string{"update", "--resources", "-", id}
-	cmd := r.command(context, args...)
+	cmd := r.command(id, context, args...)
 	cmd.Stdin = buf
 	return r.runOrErrorTimeout(cmd, updateTimeout)
 }
@@ -619,7 +620,7 @@ type Version struct {
 
 // Version returns the runc and runtime-spec versions
 func (r *Runc) Version(context context.Context) (Version, error) {
-	data, err := cmdOutput(r.command(context, "--version"), false)
+	data, err := cmdOutput(r.command("", context, "--version"), false)
 	if err != nil {
 		return Version{}, err
 	}
@@ -658,9 +659,13 @@ func parseVersion(data []byte) (Version, error) {
 	return v, nil
 }
 
-func (r *Runc) args() (out []string) {
+func (r *Runc) args(id string) (out []string) {
 	if r.Root != "" {
-		out = append(out, "--root", r.Root)
+		if id != "" && legacy.IsLegacy(id) {
+			out = append(out, "--root", "/run/runc")
+		} else {
+			out = append(out, "--root", r.Root)
+		}
 	}
 	if r.Debug {
 		out = append(out, "--debug")
-- 
1.8.3.1

