Skip to content

Commit 11b7825

Browse files
committed
cmd: add syncfs option to ctr command
Signed-off-by: Qiyuan Liang <[email protected]>
1 parent 2c042ae commit 11b7825

File tree

7 files changed

+39
-11
lines changed

7 files changed

+39
-11
lines changed

cmd/ctr/commands/images/import.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
containerd "github.com/containerd/containerd/v2/client"
3030
"github.com/containerd/containerd/v2/cmd/ctr/commands"
31+
"github.com/containerd/containerd/v2/core/diff"
3132
"github.com/containerd/containerd/v2/core/images/archive"
3233
"github.com/containerd/containerd/v2/core/transfer"
3334
tarchive "github.com/containerd/containerd/v2/core/transfer/archive"
@@ -99,6 +100,10 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
99100
Name: "discard-unpacked-layers",
100101
Usage: "Allow the garbage collector to clean layers up from the content store after unpacking, cannot be used with --no-unpack, false by default",
101102
},
103+
&cli.BoolFlag{
104+
Name: "sync-fs",
105+
Usage: "Synchronize the underlying filesystem containing files when unpack images, false by default",
106+
},
102107
}, append(commands.SnapshotterFlags, commands.LabelFlag)...),
103108

104109
Action: func(cliContext *cli.Context) error {
@@ -290,7 +295,7 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb
290295

291296
// TODO: Show unpack status
292297
fmt.Printf("unpacking %s (%s)...", img.Name, img.Target.Digest)
293-
err = image.Unpack(ctx, cliContext.String("snapshotter"))
298+
err = image.Unpack(ctx, cliContext.String("snapshotter"), containerd.WithUnpackApplyOpts(diff.WithSyncFs(cliContext.Bool("sync-fs"))))
294299
if err != nil {
295300
return err
296301
}

cmd/ctr/commands/images/mount.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
containerd "github.com/containerd/containerd/v2/client"
2525
"github.com/containerd/containerd/v2/cmd/ctr/commands"
26+
"github.com/containerd/containerd/v2/core/diff"
2627
"github.com/containerd/containerd/v2/core/leases"
2728
"github.com/containerd/containerd/v2/core/mount"
2829
"github.com/containerd/containerd/v2/defaults"
@@ -50,6 +51,10 @@ When you are done, use the unmount command.
5051
Usage: "Mount the image for the specified platform",
5152
Value: platforms.DefaultString(),
5253
},
54+
&cli.BoolFlag{
55+
Name: "sync-fs",
56+
Usage: "Synchronize the underlying filesystem containing files when unpack images, false by default",
57+
},
5358
),
5459
Action: func(cliContext *cli.Context) (retErr error) {
5560
var (
@@ -101,7 +106,7 @@ When you are done, use the unmount command.
101106
}
102107

103108
i := containerd.NewImageWithPlatform(client, img, platforms.Only(p))
104-
if err := i.Unpack(ctx, snapshotter); err != nil {
109+
if err := i.Unpack(ctx, snapshotter, containerd.WithUnpackApplyOpts(diff.WithSyncFs(cliContext.Bool("sync-fs")))); err != nil {
105110
return fmt.Errorf("error unpacking image: %w", err)
106111
}
107112

cmd/ctr/commands/images/pull.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
containerd "github.com/containerd/containerd/v2/client"
2929
"github.com/containerd/containerd/v2/cmd/ctr/commands"
3030
"github.com/containerd/containerd/v2/cmd/ctr/commands/content"
31+
"github.com/containerd/containerd/v2/core/diff"
3132
"github.com/containerd/containerd/v2/core/images"
3233
"github.com/containerd/containerd/v2/core/transfer"
3334
"github.com/containerd/containerd/v2/core/transfer/image"
@@ -84,6 +85,10 @@ command. As part of this process, we do the following:
8485
Name: "local",
8586
Usage: "Fetch content from local client rather than using transfer service",
8687
},
88+
&cli.BoolFlag{
89+
Name: "sync-fs",
90+
Usage: "Synchronize the underlying filesystem containing files when unpack images, false by default",
91+
},
8792
),
8893
Action: func(cliContext *cli.Context) error {
8994
var (
@@ -203,7 +208,7 @@ command. As part of this process, we do the following:
203208
for _, platform := range p {
204209
fmt.Printf("unpacking %s %s...\n", platforms.Format(platform), img.Target.Digest)
205210
i := containerd.NewImageWithPlatform(client, img, platforms.Only(platform))
206-
err = i.Unpack(ctx, cliContext.String("snapshotter"))
211+
err = i.Unpack(ctx, cliContext.String("snapshotter"), containerd.WithUnpackApplyOpts(diff.WithSyncFs(cliContext.Bool("sync-fs"))))
207212
if err != nil {
208213
return err
209214
}

cmd/ctr/commands/run/run.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ var Command = &cli.Command{
127127
Name: "cni",
128128
Usage: "Enable cni networking for the container",
129129
},
130+
&cli.BoolFlag{
131+
Name: "sync-fs",
132+
Usage: "Synchronize the underlying filesystem containing files when unpack images, false by default",
133+
},
130134
}, append(platformRunFlags,
131135
append(commands.RuntimeFlags,
132136
append(append(commands.SnapshotterFlags, []cli.Flag{commands.SnapshotterLabels}...),

cmd/ctr/commands/run/run_unix.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/containerd/containerd/v2/contrib/nvidia"
3434
"github.com/containerd/containerd/v2/contrib/seccomp"
3535
"github.com/containerd/containerd/v2/core/containers"
36+
"github.com/containerd/containerd/v2/core/diff"
3637
"github.com/containerd/containerd/v2/core/snapshots"
3738
"github.com/containerd/containerd/v2/pkg/oci"
3839
"github.com/containerd/log"
@@ -149,7 +150,7 @@ func NewContainer(ctx context.Context, client *containerd.Client, cliContext *cl
149150
return nil, err
150151
}
151152
if !unpacked {
152-
if err := image.Unpack(ctx, snapshotter); err != nil {
153+
if err := image.Unpack(ctx, snapshotter, containerd.WithUnpackApplyOpts(diff.WithSyncFs(cliContext.Bool("sync-fs")))); err != nil {
153154
return nil, err
154155
}
155156
}

cmd/ctr/commands/run/run_windows.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/containerd/console"
2626
containerd "github.com/containerd/containerd/v2/client"
2727
"github.com/containerd/containerd/v2/cmd/ctr/commands"
28+
"github.com/containerd/containerd/v2/core/diff"
2829
"github.com/containerd/containerd/v2/core/snapshots"
2930
"github.com/containerd/containerd/v2/pkg/netns"
3031
"github.com/containerd/containerd/v2/pkg/oci"
@@ -91,7 +92,7 @@ func NewContainer(ctx context.Context, client *containerd.Client, cliContext *cl
9192
return nil, err
9293
}
9394
if !unpacked {
94-
if err := image.Unpack(ctx, snapshotter); err != nil {
95+
if err := image.Unpack(ctx, snapshotter, containerd.WithUnpackApplyOpts(diff.WithSyncFs(cliContext.Bool("sync-fs")))); err != nil {
9596
return nil, err
9697
}
9798
}

cmd/ctr/commands/snapshots/snapshots.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,19 @@ import (
2828
"text/tabwriter"
2929
"time"
3030

31+
"github.com/containerd/log"
32+
digest "github.com/opencontainers/go-digest"
33+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
34+
"github.com/urfave/cli/v2"
35+
36+
containerd "github.com/containerd/containerd/v2/client"
3137
"github.com/containerd/containerd/v2/cmd/ctr/commands"
3238
"github.com/containerd/containerd/v2/core/content"
3339
"github.com/containerd/containerd/v2/core/diff"
3440
"github.com/containerd/containerd/v2/core/mount"
3541
"github.com/containerd/containerd/v2/core/snapshots"
3642
"github.com/containerd/containerd/v2/pkg/progress"
3743
"github.com/containerd/containerd/v2/pkg/rootfs"
38-
"github.com/containerd/log"
39-
digest "github.com/opencontainers/go-digest"
40-
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
41-
"github.com/urfave/cli/v2"
4244
)
4345

4446
// Command is the cli command for managing snapshots
@@ -536,7 +538,12 @@ var unpackCommand = &cli.Command{
536538
Name: "unpack",
537539
Usage: "Unpack applies layers from a manifest to a snapshot",
538540
ArgsUsage: "[flags] <digest>",
539-
Flags: commands.SnapshotterFlags,
541+
Flags: append([]cli.Flag{
542+
&cli.BoolFlag{
543+
Name: "sync-fs",
544+
Usage: "Synchronize the underlying filesystem containing files when unpack images, false by default",
545+
},
546+
}, commands.SnapshotterFlags...),
540547
Action: func(cliContext *cli.Context) error {
541548
dgst, err := digest.Parse(cliContext.Args().First())
542549
if err != nil {
@@ -557,7 +564,7 @@ var unpackCommand = &cli.Command{
557564
for _, image := range images {
558565
if image.Target().Digest == dgst {
559566
fmt.Printf("unpacking %s (%s)...", dgst, image.Target().MediaType)
560-
if err := image.Unpack(ctx, cliContext.String("snapshotter")); err != nil {
567+
if err := image.Unpack(ctx, cliContext.String("snapshotter"), containerd.WithUnpackApplyOpts(diff.WithSyncFs(cliContext.Bool("sync-fs")))); err != nil {
561568
fmt.Println()
562569
return err
563570
}

0 commit comments

Comments
 (0)