diff --git a/pkg/converter/convert_unix.go b/pkg/converter/convert_unix.go index 2583cd0542..cff87c067e 100644 --- a/pkg/converter/convert_unix.go +++ b/pkg/converter/convert_unix.go @@ -25,6 +25,7 @@ import ( "github.com/containerd/containerd/archive" "github.com/containerd/containerd/archive/compression" "github.com/containerd/containerd/content" + "github.com/containerd/containerd/content/local" "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/images" "github.com/containerd/containerd/images/converter" @@ -624,13 +625,23 @@ func Merge(ctx context.Context, layers []Layer, dest io.Writer, opt MergeOption) return nil, errors.Wrap(err, "merge bootstrap") } + bootstrapRa, err := local.OpenReader(targetBootstrapPath) + if err != nil { + return nil, errors.Wrap(err, "open bootstrap reader") + } + defer bootstrapRa.Close() + + files := append([]File{ + { + Name: EntryBootstrap, + Reader: content.NewReader(bootstrapRa), + Size: bootstrapRa.Size(), + }, + }, opt.AppendFiles...) var rc io.ReadCloser if opt.WithTar { - rc, err = packToTar(targetBootstrapPath, fmt.Sprintf("image/%s", EntryBootstrap), false) - if err != nil { - return nil, errors.Wrap(err, "pack bootstrap to tar") - } + rc = packToTar(files, false) } else { rc, err = os.Open(targetBootstrapPath) if err != nil { diff --git a/pkg/converter/types.go b/pkg/converter/types.go index 94a68041a1..6bd54a6afc 100644 --- a/pkg/converter/types.go +++ b/pkg/converter/types.go @@ -125,6 +125,8 @@ type MergeOption struct { Timeout *time.Duration // Encrypt encrypts the bootstrap layer if it's specified. Encrypt Encrypter + // AppendFiles specifies the files that need to be appended to the bootstrap layer. + AppendFiles []File } type UnpackOption struct { diff --git a/pkg/converter/utils.go b/pkg/converter/utils.go index b0b04d8b5f..7c09f76d0b 100644 --- a/pkg/converter/utils.go +++ b/pkg/converter/utils.go @@ -14,7 +14,6 @@ import ( "encoding/json" "fmt" "io" - "os" "path/filepath" "github.com/containerd/containerd/content" @@ -22,6 +21,12 @@ import ( ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) +type File struct { + Name string + Reader io.Reader + Size int64 +} + type writeCloser struct { closed bool io.WriteCloser @@ -83,39 +88,27 @@ func newSeekReader(ra io.ReaderAt) *seekReader { } } -// packToTar makes .tar(.gz) stream of file named `name` and return reader. -func packToTar(src string, name string, compress bool) (io.ReadCloser, error) { - fi, err := os.Stat(src) - if err != nil { - return nil, err - } - +// packToTar packs files to .tar(.gz) stream then return reader. +func packToTar(files []File, compress bool) io.ReadCloser { dirHdr := &tar.Header{ - Name: filepath.Dir(name), + Name: "image", Mode: 0755, Typeflag: tar.TypeDir, } - hdr := &tar.Header{ - Name: name, - Mode: 0444, - Size: fi.Size(), - } - - reader, writer := io.Pipe() + pr, pw := io.Pipe() go func() { // Prepare targz writer var tw *tar.Writer var gw *gzip.Writer var err error - var file *os.File if compress { - gw = gzip.NewWriter(writer) + gw = gzip.NewWriter(pw) tw = tar.NewWriter(gw) } else { - tw = tar.NewWriter(writer) + tw = tar.NewWriter(pw) } defer func() { @@ -137,30 +130,30 @@ func packToTar(src string, name string, compress bool) (io.ReadCloser, error) { finalErr = err2 } - writer.CloseWithError(finalErr) + pw.CloseWithError(finalErr) }() - file, err = os.Open(src) - if err != nil { - return - } - defer file.Close() - // Write targz stream if err = tw.WriteHeader(dirHdr); err != nil { return } - if err = tw.WriteHeader(hdr); err != nil { - return - } - - if _, err = io.Copy(tw, file); err != nil { - return + for _, file := range files { + hdr := tar.Header{ + Name: filepath.Join("image", file.Name), + Mode: 0444, + Size: file.Size, + } + if err = tw.WriteHeader(&hdr); err != nil { + return + } + if _, err = io.Copy(tw, file.Reader); err != nil { + return + } } }() - return reader, nil + return pr } // Copied from containerd/containerd project, copyright The containerd Authors. diff --git a/tools/optimizer-server/Cargo.lock b/tools/optimizer-server/Cargo.lock index 0a100f32a2..d0d4dc4102 100644 --- a/tools/optimizer-server/Cargo.lock +++ b/tools/optimizer-server/Cargo.lock @@ -171,18 +171,18 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "proc-macro2" -version = "1.0.52" +version = "1.0.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d0e1ae9e836cc3beddd63db0df682593d7e2d3d891ae8c9083d2113e1744224" +checksum = "3d1597b0c024618f09a9c3b8655b7e430397a36d23fdafec26d6965e9eec3eba" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.25" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5308e8208729c3e1504a6cfad0d5daacc4614c9a2e65d1ea312a34b5cb00fe84" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] @@ -209,18 +209,18 @@ checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" [[package]] name = "serde" -version = "1.0.155" +version = "1.0.198" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71f2b4817415c6d4210bfe1c7bfcf4801b2d904cb4d0e1a8fdb651013c9e86b8" +checksum = "9846a40c979031340571da2545a4e5b7c4163bdae79b301d5f86d03979451fcc" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.155" +version = "1.0.198" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d071a94a3fac4aff69d023a7f411e33f40f3483f8c5190b1953822b6b76d7630" +checksum = "e88edab869b01783ba905e7d0153f9fc1a6505a96e4ad3018011eedb838566d9" dependencies = [ "proc-macro2", "quote", @@ -229,9 +229,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.94" +version = "1.0.116" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c533a59c9d8a93a09c6ab31f0fd5e5f4dd1b8fc9434804029839884765d04ea" +checksum = "3e17db7126d17feb94eb3fad46bf1a96b034e8aacbc2e775fe81505f8b0b2813" dependencies = [ "itoa", "ryu", @@ -271,9 +271,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "1.0.109" +version = "2.0.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +checksum = "909518bc7b1c9b779f1bbf07f2929d35af9f0f37e47c6e9ef7f9dddc1e1821f3" dependencies = [ "proc-macro2", "quote", diff --git a/tools/optimizer-server/Cargo.toml b/tools/optimizer-server/Cargo.toml index c0bff4abaa..caa026ec6b 100644 --- a/tools/optimizer-server/Cargo.toml +++ b/tools/optimizer-server/Cargo.toml @@ -11,6 +11,6 @@ clap = "4.1.8" lazy_static = "1.4.0" libc = "0.2.140" nix = "0.26.2" -serde = { version="1.0.152", features = ["derive"] } -serde_json = "1.0.93" +serde = { version="1.0.198", features = ["derive"] } +serde_json = "1.0.116" signal-hook = "0.3.15" diff --git a/tools/optimizer-server/src/main.rs b/tools/optimizer-server/src/main.rs index 60ba162282..cd97c93511 100644 --- a/tools/optimizer-server/src/main.rs +++ b/tools/optimizer-server/src/main.rs @@ -74,12 +74,14 @@ const FAN_OPEN: u64 = 0x0000_0020; const FAN_OPEN_EXEC: u64 = 0x00001000; const AT_FDCWD: i32 = -100; +#[allow(dead_code)] #[derive(Debug)] enum SetnsError { IO(io::Error), Nix(nix::Error), } +#[allow(dead_code)] #[derive(Debug)] enum SendError { IO(io::Error),