Skip to content

Commit 92d58bf

Browse files
authored
Merge pull request #2670 from jterry75/runhcs_stop_success
runhcs-shim improvements and fixes to ctr
2 parents 2623241 + 547bb94 commit 92d58bf

24 files changed

Lines changed: 456 additions & 221 deletions

File tree

cmd/ctr/commands/run/run.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ var Command = cli.Command{
106106
Name: "fifo-dir",
107107
Usage: "directory used for storing IO FIFOs",
108108
},
109+
cli.BoolFlag{
110+
Name: "isolated",
111+
Usage: "run the container with vm isolation",
112+
},
109113
}, append(commands.SnapshotterFlags, commands.ContainerFlags...)...),
110114
Action: func(context *cli.Context) error {
111115
var (

cmd/ctr/commands/run/run_windows.go

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,57 +31,80 @@ import (
3131
// NewContainer creates a new container
3232
func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli.Context) (containerd.Container, error) {
3333
var (
34-
ref = context.Args().First()
35-
id = context.Args().Get(1)
36-
args = context.Args()[2:]
37-
)
38-
39-
image, err := client.GetImage(ctx, ref)
40-
if err != nil {
41-
return nil, err
42-
}
43-
44-
var (
34+
id string
4535
opts []oci.SpecOpts
4636
cOpts []containerd.NewContainerOpts
4737
spec containerd.NewContainerOpts
38+
39+
config = context.IsSet("config")
4840
)
4941

50-
if context.IsSet("config") {
42+
if config {
43+
id = context.Args().First()
5144
opts = append(opts, oci.WithSpecFromFile(context.String("config")))
5245
} else {
53-
opts = append(opts, oci.WithDefaultSpec())
54-
}
55-
56-
opts = append(opts, oci.WithImageConfig(image))
57-
opts = append(opts, oci.WithEnv(context.StringSlice("env")))
58-
opts = append(opts, withMounts(context))
59-
if context.Bool("tty") {
60-
opts = append(opts, oci.WithTTY)
46+
var (
47+
ref = context.Args().First()
48+
args = context.Args()[2:]
49+
)
50+
51+
id = context.Args().Get(1)
52+
snapshotter := context.String("snapshotter")
53+
if snapshotter == "windows-lcow" {
54+
opts = append(opts, oci.WithDefaultSpecForPlatform("linux/amd64"))
55+
// Clear the rootfs section.
56+
opts = append(opts, oci.WithRootFSPath(""))
57+
} else {
58+
opts = append(opts, oci.WithDefaultSpec())
59+
}
60+
opts = append(opts, oci.WithEnv(context.StringSlice("env")))
61+
opts = append(opts, withMounts(context))
6162

62-
con := console.Current()
63-
size, err := con.Size()
63+
image, err := client.GetImage(ctx, ref)
6464
if err != nil {
65-
logrus.WithError(err).Error("console size")
65+
return nil, err
6666
}
67-
opts = append(opts, oci.WithTTYSize(int(size.Width), int(size.Height)))
68-
}
67+
unpacked, err := image.IsUnpacked(ctx, snapshotter)
68+
if err != nil {
69+
return nil, err
70+
}
71+
if !unpacked {
72+
if err := image.Unpack(ctx, snapshotter); err != nil {
73+
return nil, err
74+
}
75+
}
76+
opts = append(opts, oci.WithImageConfig(image))
77+
cOpts = append(cOpts, containerd.WithImage(image))
78+
cOpts = append(cOpts, containerd.WithSnapshotter(snapshotter))
79+
cOpts = append(cOpts, containerd.WithNewSnapshot(id, image))
6980

70-
if len(args) > 0 {
71-
opts = append(opts, oci.WithProcessArgs(args...))
72-
}
73-
if cwd := context.String("cwd"); cwd != "" {
74-
opts = append(opts, oci.WithProcessCwd(cwd))
81+
if len(args) > 0 {
82+
opts = append(opts, oci.WithProcessArgs(args...))
83+
}
84+
if cwd := context.String("cwd"); cwd != "" {
85+
opts = append(opts, oci.WithProcessCwd(cwd))
86+
}
87+
if context.Bool("tty") {
88+
opts = append(opts, oci.WithTTY)
89+
90+
con := console.Current()
91+
size, err := con.Size()
92+
if err != nil {
93+
logrus.WithError(err).Error("console size")
94+
}
95+
opts = append(opts, oci.WithTTYSize(int(size.Width), int(size.Height)))
96+
}
97+
if context.Bool("isolated") {
98+
opts = append(opts, oci.WithWindowsHyperV)
99+
}
75100
}
76101

102+
cOpts = append(cOpts, containerd.WithContainerLabels(commands.LabelArgs(context.StringSlice("label"))))
103+
cOpts = append(cOpts, containerd.WithRuntime(context.String("runtime"), nil))
104+
77105
var s specs.Spec
78106
spec = containerd.WithSpec(&s, opts...)
79107

80-
cOpts = append(cOpts, containerd.WithContainerLabels(commands.LabelArgs(context.StringSlice("label"))))
81-
cOpts = append(cOpts, containerd.WithImage(image))
82-
cOpts = append(cOpts, containerd.WithSnapshotter(context.String("snapshotter")))
83-
cOpts = append(cOpts, containerd.WithNewSnapshot(id, image))
84-
cOpts = append(cOpts, containerd.WithRuntime(context.String("runtime"), nil))
85108
cOpts = append(cOpts, spec)
86109

87110
return client.NewContainer(ctx, id, cOpts...)

oci/spec_opts.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,3 +1011,14 @@ var WithPrivileged = Compose(
10111011
WithApparmorProfile(""),
10121012
WithSeccompUnconfined,
10131013
)
1014+
1015+
// WithWindowsHyperV sets the Windows.HyperV section for HyperV isolation of containers.
1016+
func WithWindowsHyperV(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
1017+
if s.Windows == nil {
1018+
s.Windows = &specs.Windows{}
1019+
}
1020+
if s.Windows.HyperV == nil {
1021+
s.Windows.HyperV = &specs.WindowsHyperV{}
1022+
}
1023+
return nil
1024+
}

runtime/v2/runhcs/cmdutil.go

Lines changed: 0 additions & 98 deletions
This file was deleted.

runtime/v2/runhcs/process.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ import (
3131
"github.com/containerd/containerd/runtime"
3232
)
3333

34+
type processExit struct {
35+
pid uint32
36+
exitStatus uint32
37+
exitedAt time.Time
38+
exitErr error
39+
}
40+
3441
func newProcess(ctx context.Context, s *service, id string, pid uint32, pr *pipeRelay, bundle, stdin, stdout, stderr string, terminal bool) (*process, error) {
3542
p, err := os.FindProcess(int(pid))
3643
if err != nil {

runtime/v2/runhcs/service.go

Lines changed: 22 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ import (
4949
)
5050

5151
const (
52-
runhcsBinary = "runhcs"
53-
runhcsVersion = "0.0.1"
54-
runhcsDebugLegacy = "--debug" // TODO: JTERRY75 remove when all cmd's are complete in go-runhcs
52+
runhcsShimVersion = "0.0.1"
5553
)
5654

5755
var (
@@ -201,41 +199,9 @@ func (s *service) State(ctx context.Context, r *taskAPI.StateRequest) (*taskAPI.
201199
return nil, err
202200
}
203201

204-
cmd := exec.Command(runhcsBinary, runhcsDebugLegacy, "state", p.id)
205-
sout := getBuffer()
206-
defer putBuffer(sout)
207-
208-
cmd.Stdout = sout
209-
_, stateErr := runCmd(ctx, cmd)
210-
if stateErr != nil {
211-
return nil, stateErr
212-
}
213-
214-
// TODO: JTERRY75 merge this with runhcs declaration
215-
type containerState struct {
216-
// Version is the OCI version for the container
217-
Version string `json:"ociVersion"`
218-
// ID is the container ID
219-
ID string `json:"id"`
220-
// InitProcessPid is the init process id in the parent namespace
221-
InitProcessPid int `json:"pid"`
222-
// Status is the current status of the container, running, paused, ...
223-
Status string `json:"status"`
224-
// Bundle is the path on the filesystem to the bundle
225-
Bundle string `json:"bundle"`
226-
// Rootfs is a path to a directory containing the container's root filesystem.
227-
Rootfs string `json:"rootfs"`
228-
// Created is the unix timestamp for the creation time of the container in UTC
229-
Created time.Time `json:"created"`
230-
// Annotations is the user defined annotations added to the config.
231-
Annotations map[string]string `json:"annotations,omitempty"`
232-
// The owner of the state directory (the owner of the container).
233-
Owner string `json:"owner"`
234-
}
235-
236-
var cs containerState
237-
if err := json.NewDecoder(sout).Decode(&cs); err != nil {
238-
log.G(ctx).WithError(err).Debugf("failed to decode runhcs state output: %s", sout.Bytes())
202+
rhcs := newRunhcs(p.bundle)
203+
cs, err := rhcs.State(ctx, p.id)
204+
if err != nil {
239205
return nil, err
240206
}
241207

@@ -551,13 +517,14 @@ func (s *service) Pids(ctx context.Context, r *taskAPI.PidsRequest) (*taskAPI.Pi
551517
// Pause the container
552518
func (s *service) Pause(ctx context.Context, r *taskAPI.PauseRequest) (*ptypes.Empty, error) {
553519
// TODO: Validate that 'id' is actually a valid parent container ID
554-
if _, err := s.getProcess(r.ID, ""); err != nil {
520+
var p *process
521+
var err error
522+
if p, err = s.getProcess(r.ID, ""); err != nil {
555523
return nil, err
556524
}
557525

558-
cmd := exec.Command(runhcsBinary, runhcsDebugLegacy, "pause", r.ID)
559-
_, err := runCmd(ctx, cmd)
560-
if err != nil {
526+
rhcs := newRunhcs(p.bundle)
527+
if err = rhcs.Pause(ctx, p.id); err != nil {
561528
return nil, err
562529
}
563530

@@ -567,13 +534,14 @@ func (s *service) Pause(ctx context.Context, r *taskAPI.PauseRequest) (*ptypes.E
567534
// Resume the container
568535
func (s *service) Resume(ctx context.Context, r *taskAPI.ResumeRequest) (*ptypes.Empty, error) {
569536
// TODO: Validate that 'id' is actually a valid parent container ID
570-
if _, err := s.getProcess(r.ID, ""); err != nil {
537+
var p *process
538+
var err error
539+
if p, err = s.getProcess(r.ID, ""); err != nil {
571540
return nil, err
572541
}
573542

574-
cmd := exec.Command(runhcsBinary, runhcsDebugLegacy, "resume", r.ID)
575-
_, err := runCmd(ctx, cmd)
576-
if err != nil {
543+
rhcs := newRunhcs(p.bundle)
544+
if err = rhcs.Resume(ctx, p.id); err != nil {
577545
return nil, err
578546
}
579547

@@ -599,7 +567,7 @@ func (s *service) Kill(ctx context.Context, r *taskAPI.KillRequest) (*ptypes.Emp
599567
// TODO: JTERRY75 runhcs support for r.All?
600568
rhcs := newRunhcs(p.bundle)
601569
if err = rhcs.Kill(ctx, p.id, strconv.FormatUint(uint64(r.Signal), 10)); err != nil {
602-
if !strings.Contains(err.Error(), "container is not running") {
570+
if !strings.Contains(err.Error(), "container is stopped") {
603571
return nil, err
604572
}
605573
}
@@ -698,18 +666,12 @@ func (s *service) ResizePty(ctx context.Context, r *taskAPI.ResizePtyRequest) (*
698666
return nil, err
699667
}
700668

701-
cmd := exec.Command(
702-
runhcsBinary,
703-
runhcsDebugLegacy,
704-
"resize-tty",
705-
p.cid,
706-
"-p",
707-
strconv.FormatUint(uint64(p.pid), 10),
708-
strconv.FormatUint(uint64(r.Width), 10),
709-
strconv.FormatUint(uint64(r.Height), 10))
710-
711-
_, err = runCmd(ctx, cmd)
712-
if err != nil {
669+
pid := int(p.pid)
670+
opts := runhcs.ResizeTTYOpts{
671+
Pid: &pid,
672+
}
673+
rhcs := newRunhcs(p.bundle)
674+
if err = rhcs.ResizeTTY(ctx, p.cid, uint16(r.Width), uint16(r.Height), &opts); err != nil {
713675
return nil, err
714676
}
715677

@@ -756,7 +718,7 @@ func (s *service) Connect(ctx context.Context, r *taskAPI.ConnectRequest) (*task
756718
return &taskAPI.ConnectResponse{
757719
ShimPid: uint32(os.Getpid()),
758720
TaskPid: s.processes[s.id].pid,
759-
Version: runhcsVersion,
721+
Version: runhcsShimVersion,
760722
}, nil
761723
}
762724

0 commit comments

Comments
 (0)