Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kevpar committed Nov 15, 2023
1 parent ab22a61 commit 1e9dc5a
Show file tree
Hide file tree
Showing 31 changed files with 1,381 additions and 175 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ out/delta.tar.gz: bin/init bin/vsockexec bin/cmd/gcs bin/cmd/gcstools bin/cmd/ho
cp bin/cmd/gcs rootfs/bin/
cp bin/cmd/gcstools rootfs/bin/
cp bin/cmd/hooks/wait-paths rootfs/bin/
cp /mnt/c/Users/kevpar/src/test/rebuf rootfs/bin/
for tool in $(GCS_TOOLS); do ln -s gcstools rootfs/bin/$$tool; done
git -C $(SRCROOT) rev-parse HEAD > rootfs/info/gcs.commit && \
git -C $(SRCROOT) rev-parse --abbrev-ref HEAD > rootfs/info/gcs.branch && \
Expand Down
3 changes: 2 additions & 1 deletion Protobuild.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ generators = ["go", "go-grpc"]

# defaults are "/usr/local/include" and "/usr/include", which don't exist on Windows.
# override defaults to supress errors about non-existant directories.
after = []
# after = []

# This section maps protobuf imports to Go packages.
[packages]
Expand All @@ -21,5 +21,6 @@ prefixes = [
"github.com/Microsoft/hcsshim/internal/computeagent",
"github.com/Microsoft/hcsshim/internal/ncproxyttrpc",
"github.com/Microsoft/hcsshim/internal/vmservice",
"github.com/Microsoft/hcsshim/internal/save",
]
generators = ["go", "go-ttrpc"]
106 changes: 58 additions & 48 deletions cmd/containerd-shim-runhcs-v1/options/runhcs.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cmd/containerd-shim-runhcs-v1/options/runhcs.proto
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ message Options {

// scrub_logs enables removing environment variables and other potentially sensitive information from logs
bool scrub_logs = 20;

string restore_path = 21;
}

// ProcessDetails contains additional information about a process. This is the additional
Expand Down
67 changes: 67 additions & 0 deletions cmd/containerd-shim-runhcs-v1/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/oci"
"github.com/Microsoft/hcsshim/internal/state"
"github.com/Microsoft/hcsshim/internal/uvm"
"github.com/Microsoft/hcsshim/osversion"
"github.com/Microsoft/hcsshim/pkg/annotations"
Expand Down Expand Up @@ -66,6 +67,9 @@ type shimPod interface {
// return `errdefs.ErrFailedPrecondition`. Deleting the pod's sandbox task
// is a no-op.
DeleteTask(ctx context.Context, tid string) error

StartSave(ctx context.Context, path string) error
CompleteSave(ctx context.Context, path string) error
}

func createPod(ctx context.Context, events publisher, req *task.CreateTaskRequest, s *specs.Spec) (_ shimPod, err error) {
Expand Down Expand Up @@ -449,3 +453,66 @@ func (p *pod) DeleteTask(ctx context.Context, tid string) error {

return nil
}

type podState struct {
ID string
Spec *specs.Spec
}

func (p *pod) StartSave(ctx context.Context, path string) error {
if err := os.MkdirAll(path, 0755); err != nil {
return err
}
if p.host == nil {
return fmt.Errorf("can only save VM-isolated pods")
}
if err := p.host.StartSave(ctx, filepath.Join(path, "uvm")); err != nil {
return fmt.Errorf("save UVM: %w", err)
}
if err := p.sandboxTask.Save(ctx, filepath.Join(path, "sandboxTask")); err != nil {
return err
}
if err := state.Write(filepath.Join(path, "state.json"),
&podState{
ID: p.id,
Spec: p.spec,
}); err != nil {
return err
}
return nil
}

func (p *pod) CompleteSave(ctx context.Context, path string) error {
if err := os.MkdirAll(path, 0755); err != nil {
return err
}
if p.host == nil {
return fmt.Errorf("can only save VM-isolated pods")
}
if err := p.host.CompleteSave(ctx, filepath.Join(path, "uvm")); err != nil {
return fmt.Errorf("save UVM: %w", err)
}
return nil
}

type standbyPod struct {
state *podState
host *uvm.UtilityVM
}

func restorePod(ctx context.Context, path string, netNS string, scratchPath string, events publisher, req *task.CreateTaskRequest) (_ shimPod, err error) {
p := &pod{
events: events,
id: req.ID,
}
p.host, err = uvm.RestoreUVM(ctx, filepath.Join(path, "uvm"), netNS, scratchPath, req.ID)
if err != nil {
return nil, err
}
state, err := state.Read[podState](filepath.Join(path, "state.json"))
if err != nil {
return nil, err
}
p.spec = state.Spec
return p, nil
}
8 changes: 8 additions & 0 deletions cmd/containerd-shim-runhcs-v1/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ func (tsp *testShimPod) DeleteTask(ctx context.Context, tid string) error {
return nil
}

func (tsp *testShimPod) StartSave(ctx context.Context, path string) error {
return fmt.Errorf("not implemented")
}

func (tsp *testShimPod) CompleteSave(ctx context.Context, path string) error {
return fmt.Errorf("not implemented")
}

// Pod tests

func setupTestPodWithFakes(t *testing.T) (*pod, *testShimTask) {
Expand Down
52 changes: 52 additions & 0 deletions cmd/containerd-shim-runhcs-v1/saverestore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"context"
"fmt"
"os"
)

func (s *service) startSave(ctx context.Context, path string) error {
if err := os.MkdirAll(path, 0755); err != nil {
return err
}
v := s.taskOrPod.Load()
if v == nil {
return fmt.Errorf("invalid state: no pod")
}
p, ok := v.(shimPod)
if !ok {
return fmt.Errorf("only works with pod, not standalone task")
}
if err := p.StartSave(ctx, path); err != nil {
return err
}
return nil
}

func (s *service) completeSave(ctx context.Context, path string) error {
if err := os.MkdirAll(path, 0755); err != nil {
return err
}
v := s.taskOrPod.Load()
if v == nil {
return fmt.Errorf("invalid state: no pod")
}
p, ok := v.(shimPod)
if !ok {
return fmt.Errorf("only works with pod, not standalone task")
}
if err := p.CompleteSave(ctx, path); err != nil {
return err
}
return nil
}

func (s *service) restore(ctx context.Context, path string) error {
// p, err := restorePod(ctx, filepath.Join(path, "pod"))
// if err != nil {
// return err
// }
// s.standbyPod = p
return nil
}
8 changes: 8 additions & 0 deletions cmd/containerd-shim-runhcs-v1/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
runhcsopts "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
"github.com/Microsoft/hcsshim/internal/extendedtask"
hcslog "github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/save"
"github.com/Microsoft/hcsshim/internal/shimdiag"
"github.com/Microsoft/hcsshim/pkg/octtrpc"
)
Expand Down Expand Up @@ -193,6 +194,12 @@ var serveCommand = cli.Command{
return fmt.Errorf("failed to create new service: %w", err)
}

if p := newShimOpts.RestorePath; p != "" {
if err := svc.restore(context.Background(), p); err != nil {
return fmt.Errorf("restore shim from state %q: %w", p, err)
}
}

s, err := ttrpc.NewServer(ttrpc.WithUnaryServerInterceptor(octtrpc.ServerInterceptor()))
if err != nil {
return err
Expand All @@ -201,6 +208,7 @@ var serveCommand = cli.Command{
task.RegisterTaskService(s, svc)
shimdiag.RegisterShimDiagService(s, svc)
extendedtask.RegisterExtendedTaskService(s, svc)
save.RegisterSaveService(s, svc)

sl, err := winio.ListenPipe(socket, nil)
if err != nil {
Expand Down
Loading

0 comments on commit 1e9dc5a

Please sign in to comment.