Skip to content

Commit 0e8849b

Browse files
committed
feat(shim): clean up rootfs views on dead-shim delete path
Wrap the runc shim manager so containerd shim delete subcommand removes rootfs view resources before the bundle is torn down. Signed-off-by: sidneychang <2190206983@qq.com>
1 parent 1989568 commit 0e8849b

2 files changed

Lines changed: 81 additions & 2 deletions

File tree

cmd/containerd-shim-urunc-v2/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ package main
1717
import (
1818
"context"
1919

20-
"github.com/containerd/containerd/runtime/v2/runc/manager"
2120
"github.com/containerd/containerd/runtime/v2/shim"
2221
_ "github.com/urunc-dev/urunc/pkg/containerd-shim"
22+
containerdshim "github.com/urunc-dev/urunc/pkg/containerd-shim"
2323
)
2424

2525
func main() {
26-
shim.RunManager(context.Background(), manager.NewShimManager("io.containerd.urunc.v2"))
26+
shim.RunManager(context.Background(), containerdshim.NewShimManager("io.containerd.urunc.v2"))
2727
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) 2023-2026, Nubificus LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package containerdshim
16+
17+
import (
18+
"context"
19+
"os"
20+
21+
"github.com/containerd/containerd/runtime/v2/runc/manager"
22+
"github.com/containerd/containerd/runtime/v2/shim"
23+
"github.com/containerd/log"
24+
containerdShim "github.com/urunc-dev/urunc/pkg/containerd-shim/containerd"
25+
)
26+
27+
const containerdGRPCAddressEnv = "GRPC_ADDRESS"
28+
29+
func containerdGRPCAddress() string {
30+
return os.Getenv(containerdGRPCAddressEnv)
31+
}
32+
33+
type shimManager struct {
34+
shim.Manager
35+
}
36+
37+
func NewShimManager(runtime string) shim.Manager {
38+
return &shimManager{Manager: manager.NewShimManager(runtime)}
39+
}
40+
41+
func (m *shimManager) Stop(ctx context.Context, id string) (shim.StopStatus, error) {
42+
bundle, err := os.Getwd()
43+
if err != nil {
44+
log.G(ctx).WithError(err).Warn("urunc(shim): getwd during delete failed")
45+
return m.Manager.Stop(ctx, id)
46+
}
47+
48+
shouldCleanup, snapshotter, err := containerdShim.ShouldCleanupRootfsView(bundle)
49+
if err != nil {
50+
log.G(ctx).WithError(err).Warn("urunc(shim): read rootfs view cleanup state from bundle during delete failed")
51+
return m.Manager.Stop(ctx, id)
52+
}
53+
if !shouldCleanup {
54+
return m.Manager.Stop(ctx, id)
55+
}
56+
57+
address := containerdGRPCAddress()
58+
if address == "" {
59+
log.G(ctx).Warn("urunc(shim): containerd gRPC address unset during delete; rootfs view cleanup skipped")
60+
return m.Manager.Stop(ctx, id)
61+
}
62+
63+
session, err := containerdShim.OpenSession(ctx, address, id)
64+
if err != nil {
65+
log.G(ctx).WithError(err).Warn("urunc(shim): open containerd session for rootfs view cleanup failed")
66+
return m.Manager.Stop(ctx, id)
67+
}
68+
defer func() {
69+
if err := session.Close(); err != nil {
70+
log.G(ctx).WithError(err).Warn("urunc(shim): failed to close containerd session after rootfs view cleanup")
71+
}
72+
}()
73+
74+
if err := containerdShim.NewRootfsViewAccessor(session).Cleanup(ctx, snapshotter); err != nil {
75+
log.G(ctx).WithError(err).Warn("urunc(shim): rootfs view cleanup during delete failed")
76+
}
77+
78+
return m.Manager.Stop(ctx, id)
79+
}

0 commit comments

Comments
 (0)