-
Notifications
You must be signed in to change notification settings - Fork 597
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] added container export functionality.
Signed-off-by: Yasin Turan <[email protected]>
- Loading branch information
Showing
7 changed files
with
353 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/containerd/nerdctl/pkg/clientutil" | ||
"github.com/containerd/nerdctl/pkg/cmd/container" | ||
"github.com/mattn/go-isatty" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newExportCommand() *cobra.Command { | ||
var exportCommand = &cobra.Command{ | ||
Use: "export CONTAINER", | ||
Args: cobra.MinimumNArgs(1), | ||
Short: "Export a containers filesystem as a tar archive", | ||
Long: "Export a containers filesystem as a tar archive", | ||
RunE: exportAction, | ||
ValidArgsFunction: exportShellComplete, | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
} | ||
exportCommand.Flags().StringP("output", "o", "", "Write to a file, instead of STDOUT") | ||
|
||
return exportCommand | ||
} | ||
|
||
func exportAction(cmd *cobra.Command, args []string) error { | ||
globalOptions, err := processRootCmdFlags(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
if len(args) == 0 { | ||
return fmt.Errorf("requires at least 1 argument") | ||
} | ||
|
||
output, err := cmd.Flags().GetString("output") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address) | ||
if err != nil { | ||
return err | ||
} | ||
defer cancel() | ||
|
||
writer := cmd.OutOrStdout() | ||
if output != "" { | ||
f, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
writer = f | ||
} else { | ||
if isatty.IsTerminal(os.Stdout.Fd()) { | ||
return fmt.Errorf("cowardly refusing to save to a terminal. Use the -o flag or redirect") | ||
} | ||
} | ||
return container.Export(ctx, client, args, writer) | ||
|
||
} | ||
|
||
func exportShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
// show container names | ||
return shellCompleteContainerNames(cmd, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package container | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/containerd/containerd" | ||
"github.com/containerd/containerd/containers" | ||
"github.com/containerd/containerd/mount" | ||
"github.com/containerd/nerdctl/pkg/idutil/containerwalker" | ||
"github.com/containerd/nerdctl/pkg/tarutil" | ||
) | ||
|
||
func Export(ctx context.Context, client *containerd.Client, args []string, w io.Writer) error { | ||
walker := &containerwalker.ContainerWalker{ | ||
Client: client, | ||
OnFound: func(ctx context.Context, found containerwalker.Found) error { | ||
if found.MatchCount > 1 { | ||
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req) | ||
} | ||
container := found.Container | ||
c, err := container.Info(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
return performWithBaseFS(ctx, client, c, func(root string) error { | ||
tb := tarutil.NewTarballer(w) | ||
return tb.Tar(root) | ||
}) | ||
|
||
}, | ||
} | ||
req := args[0] | ||
n, err := walker.Walk(ctx, req) | ||
if err != nil { | ||
return fmt.Errorf("failed to export container %s: %w", req, err) | ||
} else if n == 0 { | ||
return fmt.Errorf("no such container %s", req) | ||
} | ||
return nil | ||
} | ||
|
||
// performWithBaseFS will execute a given function with respect to the root filesystem of a container. | ||
// copied over from: https://github.com/moby/moby/blob/master/daemon/containerd/image_exporter.go#L24 | ||
func performWithBaseFS(ctx context.Context, client *containerd.Client, c containers.Container, fn func(root string) error) error { | ||
mounts, err := client.SnapshotService(c.Snapshotter).Mounts(ctx, c.SnapshotKey) | ||
if err != nil { | ||
return err | ||
} | ||
return mount.WithTempMount(ctx, mounts, fn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//go:build freebsd || linux | ||
|
||
/* | ||
Copyright The containerd Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package tarutil | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/containerd/continuity/sysx" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func chmodTarEntry(perm os.FileMode) os.FileMode { | ||
return perm | ||
} | ||
|
||
func getxattr(path, attr string) ([]byte, error) { | ||
b, err := sysx.LGetxattr(path, attr) | ||
if err == unix.ENOTSUP || err == sysx.ENODATA { | ||
return nil, nil | ||
} | ||
return b, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package tarutil | ||
|
||
import "os" | ||
|
||
func chmodTarEntry(perm os.FileMode) os.FileMode { | ||
perm &= 0755 | ||
// Add the x bit: make everything +x from windows | ||
perm |= 0111 | ||
|
||
return perm | ||
} | ||
|
||
func getxattr(path, attr string) ([]byte, error) { | ||
return nil, nil | ||
} |