Skip to content

Commit 187382c

Browse files
committed
Add create-layer command to create filesystem change set
Signed-off-by: Lei Jitang <[email protected]>
1 parent 721ba58 commit 187382c

File tree

281 files changed

+115420
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

281 files changed

+115420
-3
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ tools:
2222
go build ./cmd/oci-create-runtime-bundle
2323
go build ./cmd/oci-unpack
2424
go build ./cmd/oci-image-validate
25+
go build ./cmd/oci-create-layer
2526

2627
lint:
2728
@echo "checking lint"

cmd/oci-create-layer/main.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2016 The Linux Foundation
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 main
16+
17+
import (
18+
"log"
19+
"os"
20+
21+
"github.com/opencontainers/image-tools/image"
22+
"github.com/spf13/cobra"
23+
)
24+
25+
type layerCmd struct {
26+
stdout *log.Logger
27+
stderr *log.Logger
28+
dest string
29+
}
30+
31+
func main() {
32+
stdout := log.New(os.Stdout, "", 0)
33+
stderr := log.New(os.Stderr, "", 0)
34+
35+
cmd := newLayerCmd(stdout, stderr)
36+
if err := cmd.Execute(); err != nil {
37+
stderr.Println(err)
38+
os.Exit(1)
39+
}
40+
}
41+
42+
func newLayerCmd(stdout, stderr *log.Logger) *cobra.Command {
43+
v := &layerCmd{
44+
stdout: stdout,
45+
stderr: stderr,
46+
}
47+
48+
cmd := &cobra.Command{
49+
Use: "create-layer [child] [parent]",
50+
Short: "Create an OCI layer",
51+
Long: `Create an OCI layer based on the changeset between filesystems.`,
52+
Run: v.Run,
53+
}
54+
cmd.Flags().StringVar(
55+
&v.dest, "dest", "",
56+
`The dest specify a particular filename where the layer write to`,
57+
)
58+
return cmd
59+
}
60+
61+
func (v *layerCmd) Run(cmd *cobra.Command, args []string) {
62+
if len(args) != 1 && len(args) != 2 {
63+
v.stderr.Print("One or two filesystems are required")
64+
if err := cmd.Usage(); err != nil {
65+
v.stderr.Println(err)
66+
}
67+
os.Exit(1)
68+
}
69+
var err error
70+
if len(args) == 1 {
71+
err = image.CreateLayer(args[0], "", v.dest)
72+
} else {
73+
err = image.CreateLayer(args[0], args[1], v.dest)
74+
}
75+
if err != nil {
76+
v.stderr.Printf("create layer failed: %v", err)
77+
os.Exit(1)
78+
}
79+
os.Exit(0)
80+
}

glide.lock

Lines changed: 26 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ import:
1212
- package: github.com/pkg/errors
1313
version: ~0.7.1
1414
- package: github.com/spf13/cobra
15+
- package: github.com/containers/storage
16+

image/layer.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2016 The Linux Foundation
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 image
16+
17+
import (
18+
"fmt"
19+
"io"
20+
"os"
21+
"path/filepath"
22+
23+
"github.com/containers/storage/pkg/archive"
24+
)
25+
26+
func CreateLayer(child, parent, dest string) error {
27+
arch, err := Diff(child, parent)
28+
if err != nil {
29+
return err
30+
}
31+
defer arch.Close()
32+
filename := fmt.Sprintf("%s.tar", filepath.Clean(child))
33+
if dest != "" {
34+
filename = filepath.Clean(dest)
35+
}
36+
f, err := os.Create(filename)
37+
if err != nil {
38+
return err
39+
}
40+
defer f.Close()
41+
_, err = io.Copy(f, arch)
42+
return err
43+
}
44+
45+
// Diff produces an archive of the changes between the specified
46+
// layer and its parent layer which may be "".
47+
func Diff(child, parent string) (arch archive.Archive, err error) {
48+
if parent == "" {
49+
archive, err := archive.Tar(child, archive.Uncompressed)
50+
if err != nil {
51+
return nil, err
52+
}
53+
return archive, nil
54+
}
55+
56+
changes, err := archive.ChangesDirs(child, parent)
57+
if err != nil {
58+
return nil, err
59+
}
60+
archive, err := archive.ExportChanges(child, changes, nil, nil)
61+
if err != nil {
62+
return nil, err
63+
}
64+
return archive, nil
65+
}

vendor/github.com/Sirupsen/logrus/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/Sirupsen/logrus/alt_exit.go

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/Sirupsen/logrus/doc.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)