Skip to content

Commit

Permalink
Merge pull request containerd#11118 from tboevil/ctr-syncfs
Browse files Browse the repository at this point in the history
cmd/ctr: allow user to syncfs during unpacking image locally
  • Loading branch information
mxpv authored Dec 11, 2024
2 parents 41bc049 + 11b7825 commit ff4f5cb
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 11 deletions.
7 changes: 6 additions & 1 deletion cmd/ctr/commands/images/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

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

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

// TODO: Show unpack status
fmt.Printf("unpacking %s (%s)...", img.Name, img.Target.Digest)
err = image.Unpack(ctx, cliContext.String("snapshotter"))
err = image.Unpack(ctx, cliContext.String("snapshotter"), containerd.WithUnpackApplyOpts(diff.WithSyncFs(cliContext.Bool("sync-fs"))))
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/ctr/commands/images/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

containerd "github.com/containerd/containerd/v2/client"
"github.com/containerd/containerd/v2/cmd/ctr/commands"
"github.com/containerd/containerd/v2/core/diff"
"github.com/containerd/containerd/v2/core/leases"
"github.com/containerd/containerd/v2/core/mount"
"github.com/containerd/containerd/v2/defaults"
Expand Down Expand Up @@ -50,6 +51,10 @@ When you are done, use the unmount command.
Usage: "Mount the image for the specified platform",
Value: platforms.DefaultString(),
},
&cli.BoolFlag{
Name: "sync-fs",
Usage: "Synchronize the underlying filesystem containing files when unpack images, false by default",
},
),
Action: func(cliContext *cli.Context) (retErr error) {
var (
Expand Down Expand Up @@ -101,7 +106,7 @@ When you are done, use the unmount command.
}

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

Expand Down
7 changes: 6 additions & 1 deletion cmd/ctr/commands/images/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
containerd "github.com/containerd/containerd/v2/client"
"github.com/containerd/containerd/v2/cmd/ctr/commands"
"github.com/containerd/containerd/v2/cmd/ctr/commands/content"
"github.com/containerd/containerd/v2/core/diff"
"github.com/containerd/containerd/v2/core/images"
"github.com/containerd/containerd/v2/core/transfer"
"github.com/containerd/containerd/v2/core/transfer/image"
Expand Down Expand Up @@ -84,6 +85,10 @@ command. As part of this process, we do the following:
Name: "local",
Usage: "Fetch content from local client rather than using transfer service",
},
&cli.BoolFlag{
Name: "sync-fs",
Usage: "Synchronize the underlying filesystem containing files when unpack images, false by default",
},
),
Action: func(cliContext *cli.Context) error {
var (
Expand Down Expand Up @@ -204,7 +209,7 @@ command. As part of this process, we do the following:
for _, platform := range p {
fmt.Printf("unpacking %s %s...\n", platforms.Format(platform), img.Target.Digest)
i := containerd.NewImageWithPlatform(client, img, platforms.Only(platform))
err = i.Unpack(ctx, cliContext.String("snapshotter"))
err = i.Unpack(ctx, cliContext.String("snapshotter"), containerd.WithUnpackApplyOpts(diff.WithSyncFs(cliContext.Bool("sync-fs"))))
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/ctr/commands/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ var Command = &cli.Command{
Name: "cni",
Usage: "Enable cni networking for the container",
},
&cli.BoolFlag{
Name: "sync-fs",
Usage: "Synchronize the underlying filesystem containing files when unpack images, false by default",
},
}, append(platformRunFlags,
append(commands.RuntimeFlags,
append(append(commands.SnapshotterFlags, []cli.Flag{commands.SnapshotterLabels}...),
Expand Down
3 changes: 2 additions & 1 deletion cmd/ctr/commands/run/run_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/containerd/containerd/v2/contrib/nvidia"
"github.com/containerd/containerd/v2/contrib/seccomp"
"github.com/containerd/containerd/v2/core/containers"
"github.com/containerd/containerd/v2/core/diff"
"github.com/containerd/containerd/v2/core/snapshots"
"github.com/containerd/containerd/v2/pkg/oci"
"github.com/containerd/log"
Expand Down Expand Up @@ -149,7 +150,7 @@ func NewContainer(ctx context.Context, client *containerd.Client, cliContext *cl
return nil, err
}
if !unpacked {
if err := image.Unpack(ctx, snapshotter); err != nil {
if err := image.Unpack(ctx, snapshotter, containerd.WithUnpackApplyOpts(diff.WithSyncFs(cliContext.Bool("sync-fs")))); err != nil {
return nil, err
}
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/ctr/commands/run/run_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/containerd/console"
containerd "github.com/containerd/containerd/v2/client"
"github.com/containerd/containerd/v2/cmd/ctr/commands"
"github.com/containerd/containerd/v2/core/diff"
"github.com/containerd/containerd/v2/core/snapshots"
"github.com/containerd/containerd/v2/pkg/netns"
"github.com/containerd/containerd/v2/pkg/oci"
Expand Down Expand Up @@ -91,7 +92,7 @@ func NewContainer(ctx context.Context, client *containerd.Client, cliContext *cl
return nil, err
}
if !unpacked {
if err := image.Unpack(ctx, snapshotter); err != nil {
if err := image.Unpack(ctx, snapshotter, containerd.WithUnpackApplyOpts(diff.WithSyncFs(cliContext.Bool("sync-fs")))); err != nil {
return nil, err
}
}
Expand Down
19 changes: 13 additions & 6 deletions cmd/ctr/commands/snapshots/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ import (
"text/tabwriter"
"time"

"github.com/containerd/log"
digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/urfave/cli/v2"

containerd "github.com/containerd/containerd/v2/client"
"github.com/containerd/containerd/v2/cmd/ctr/commands"
"github.com/containerd/containerd/v2/core/content"
"github.com/containerd/containerd/v2/core/diff"
"github.com/containerd/containerd/v2/core/mount"
"github.com/containerd/containerd/v2/core/snapshots"
"github.com/containerd/containerd/v2/pkg/progress"
"github.com/containerd/containerd/v2/pkg/rootfs"
"github.com/containerd/log"
digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/urfave/cli/v2"
)

// Command is the cli command for managing snapshots
Expand Down Expand Up @@ -536,7 +538,12 @@ var unpackCommand = &cli.Command{
Name: "unpack",
Usage: "Unpack applies layers from a manifest to a snapshot",
ArgsUsage: "[flags] <digest>",
Flags: commands.SnapshotterFlags,
Flags: append([]cli.Flag{
&cli.BoolFlag{
Name: "sync-fs",
Usage: "Synchronize the underlying filesystem containing files when unpack images, false by default",
},
}, commands.SnapshotterFlags...),
Action: func(cliContext *cli.Context) error {
dgst, err := digest.Parse(cliContext.Args().First())
if err != nil {
Expand All @@ -557,7 +564,7 @@ var unpackCommand = &cli.Command{
for _, image := range images {
if image.Target().Digest == dgst {
fmt.Printf("unpacking %s (%s)...", dgst, image.Target().MediaType)
if err := image.Unpack(ctx, cliContext.String("snapshotter")); err != nil {
if err := image.Unpack(ctx, cliContext.String("snapshotter"), containerd.WithUnpackApplyOpts(diff.WithSyncFs(cliContext.Bool("sync-fs")))); err != nil {
fmt.Println()
return err
}
Expand Down

0 comments on commit ff4f5cb

Please sign in to comment.