Add direct unpack support for overlay and aufs

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu
2019-01-03 18:07:56 -08:00
committed by Derek McGowan
parent f06e605f1a
commit 81386df917
9 changed files with 333 additions and 91 deletions

View File

@@ -19,10 +19,8 @@ package apply
import (
"context"
"io"
"io/ioutil"
"time"
"github.com/containerd/containerd/archive"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/diff"
"github.com/containerd/containerd/log"
@@ -94,15 +92,8 @@ func (s *fsApplier) Apply(ctx context.Context, desc ocispec.Descriptor, mounts [
rc := &readCounter{
r: io.TeeReader(processor, digester.Hash()),
}
if err := mount.WithTempMount(ctx, mounts, func(root string) error {
if _, err := archive.Apply(ctx, root, rc); err != nil {
return err
}
// Read any trailing data
_, err := io.Copy(ioutil.Discard, rc)
return err
}); err != nil {
if err := apply(ctx, mounts, rc); err != nil {
return emptyDesc, err
}

90
diff/apply/apply_linux.go Normal file
View File

@@ -0,0 +1,90 @@
// +build 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 apply
import (
"context"
"io"
"strings"
"github.com/containerd/containerd/archive"
"github.com/containerd/containerd/mount"
"github.com/pkg/errors"
)
func apply(ctx context.Context, mounts []mount.Mount, r io.Reader) error {
switch {
case len(mounts) == 1 && mounts[0].Type == "overlay":
path, err := getOverlayPath(mounts[0].Options)
if err != nil {
return err
}
_, err = archive.Apply(ctx, path, r,
archive.WithConvertWhiteout(archive.OverlayConvertWhiteout))
return err
case len(mounts) == 1 && mounts[0].Type == "aufs":
path, err := getAufsPath(mounts[0].Options)
if err != nil {
return err
}
_, err = archive.Apply(ctx, path, r,
archive.WithConvertWhiteout(archive.AufsConvertWhiteout))
return err
default:
return mount.WithTempMount(ctx, mounts, func(root string) error {
_, err := archive.Apply(ctx, root, r)
return err
})
}
}
func getOverlayPath(options []string) (string, error) {
const upperdirPrefix = "upperdir="
for _, o := range options {
if strings.HasPrefix(o, upperdirPrefix) {
return strings.TrimPrefix(o, upperdirPrefix), nil
}
}
return "", errors.New("upperdir not found")
}
func getAufsPath(options []string) (string, error) {
const (
sep = ":"
brPrefix1 = "br:"
brPrefix2 = "br="
rwSuffix = "=rw"
)
for _, o := range options {
if strings.HasPrefix(o, brPrefix1) {
o = strings.TrimPrefix(o, brPrefix1)
} else if strings.HasPrefix(o, brPrefix2) {
o = strings.TrimPrefix(o, brPrefix2)
} else {
continue
}
for _, b := range strings.Split(o, sep) {
if strings.HasSuffix(b, rwSuffix) {
return strings.TrimSuffix(b, rwSuffix), nil
}
}
break
}
return "", errors.New("rw branch not found")
}

View File

@@ -0,0 +1,79 @@
// +build 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 apply
import (
"testing"
)
func TestGetOverlayPath(t *testing.T) {
good := []string{"upperdir=/test/upper", "lowerdir=/test/lower", "workdir=/test/work"}
path, err := getOverlayPath(good)
if err != nil {
t.Fatalf("Get overlay path failed: %v", err)
}
if path != "/test/upper" {
t.Fatalf("Unexpected upperdir: %q", path)
}
bad := []string{"lowerdir=/test/lower"}
_, err = getOverlayPath(bad)
if err == nil {
t.Fatalf("An error is expected")
}
}
func TestGetAufsPath(t *testing.T) {
rwDir := "/test/rw"
for _, test := range []struct {
options []string
expectErr bool
}{
{
options: []string{"random:option", "br:" + rwDir + "=rw:/test/ro=ro+wh"},
expectErr: false,
},
{
options: []string{"random:option", "br=" + rwDir + "=rw:/test/ro=ro+wh"},
expectErr: false,
},
{
options: []string{"random:option"},
expectErr: true,
},
{
options: []string{"br:/test/ro=ro+wh"},
expectErr: true,
},
} {
path, err := getAufsPath(test.options)
if test.expectErr {
if err == nil {
t.Fatalf("An error is expected")
}
continue
}
if err != nil {
t.Fatalf("Get aufs path failed: %v", err)
}
if path != rwDir {
t.Fatalf("Unexpected rw dir: %q", path)
}
}
}

34
diff/apply/apply_other.go Normal file
View File

@@ -0,0 +1,34 @@
// +build !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 apply
import (
"context"
"io"
"github.com/containerd/containerd/archive"
"github.com/containerd/containerd/mount"
)
func apply(ctx context.Context, mounts []mount.Mount, r io.Reader) error {
return mount.WithTempMount(ctx, mounts, func(root string) error {
_, err := archive.Apply(ctx, root, r)
return err
})
}