Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linuxkit/build: implement --modified-only option #4038

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/cmd/linuxkit/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func buildCmd() *cobra.Command {
noSbom bool
sbomOutputFilename string
inputTar string
modifiedOnly bool
sbomCurrentTime bool
dryRun bool
)
Expand Down Expand Up @@ -117,6 +118,9 @@ The generated image can be in one of multiple formats which can be run on variou
if inputTar != "" && pull {
return fmt.Errorf("cannot use --input-tar and --pull together")
}
if inputTar == "" && modifiedOnly {
return fmt.Errorf("--modified-only can be used together with the --input-tar")
}

var outfile *os.File
if outputFile != "" {
Expand Down Expand Up @@ -235,7 +239,7 @@ The generated image can be in one of multiple formats which can be run on variou
return fmt.Errorf("error creating sbom generator: %v", err)
}
}
err = moby.Build(m, w, moby.BuildOpts{Pull: pull, BuilderType: tp, DecompressKernel: decompressKernel, CacheDir: cacheDir.String(), DockerCache: docker, Arch: arch, SbomGenerator: sbomGenerator, InputTar: inputTar})
err = moby.Build(m, w, moby.BuildOpts{Pull: pull, BuilderType: tp, DecompressKernel: decompressKernel, CacheDir: cacheDir.String(), DockerCache: docker, Arch: arch, SbomGenerator: sbomGenerator, InputTar: inputTar, ModifiedOnly: modifiedOnly})
if err != nil {
return fmt.Errorf("%v", err)
}
Expand Down Expand Up @@ -266,6 +270,7 @@ The generated image can be in one of multiple formats which can be run on variou
cmd.Flags().StringVar(&arch, "arch", runtime.GOARCH, "target architecture for which to build")
cmd.Flags().VarP(&buildFormats, "format", "f", "Formats to create [ "+strings.Join(outputTypes, " ")+" ]")
cmd.Flags().StringVar(&inputTar, "input-tar", "", "path to tar from previous linuxkit build to use as input; if provided, will take files from images from this tar, using OCI images only to replace or update files. Always copies to a temporary working directory to avoid overwriting. Only works if input-tar file has the linuxkit.yaml used to build it in the exact same location. Incompatible with --pull")
cmd.Flags().BoolVar(&modifiedOnly, "modified-only", false, "Output tarball contains modified files only. Works only with the --input-tar option.")
cacheDir = flagOverEnvVarOverDefaultString{def: defaultLinuxkitCache(), envVar: envVarCacheDir}
cmd.Flags().Var(&cacheDir, "cache", fmt.Sprintf("Directory for caching and finding cached image, overrides env var %s", envVarCacheDir))
cmd.Flags().BoolVar(&noSbom, "no-sbom", false, "suppress consolidation of sboms on input container images to a single sbom and saving in the output filesystem")
Expand Down
30 changes: 20 additions & 10 deletions src/cmd/linuxkit/moby/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,10 @@ func Build(m Moby, w io.Writer, opts BuildOpts) error {
// first check if the existing one had it
//if config != nil && len(oldConfig.initRefs) > index+1 && oldConfig.initRefs[index].String() == image {
if oldConfig != nil && oldConfig.Kernel.ref != nil && oldConfig.Kernel.ref.String() == m.Kernel.ref.String() {
if err := extractPackageFilesFromTar(in, iw, m.Kernel.ref.String(), "kernel"); err != nil {
return err
if !opts.ModifiedOnly {
if err := extractPackageFilesFromTar(in, iw, m.Kernel.ref.String(), "kernel"); err != nil {
return err
}
}
} else {
// get kernel and initrd tarball and ucode cpio archive from container
Expand All @@ -231,8 +233,10 @@ func Build(m Moby, w io.Writer, opts BuildOpts) error {
apkTar := newAPKTarWriter(iw, "init")
for i, ii := range m.initRefs {
if oldConfig != nil && len(oldConfig.initRefs) > i && oldConfig.initRefs[i].String() == ii.String() {
if err := extractPackageFilesFromTar(in, apkTar, ii.String(), fmt.Sprintf("init[%d]", i)); err != nil {
return err
if !opts.ModifiedOnly {
if err := extractPackageFilesFromTar(in, apkTar, ii.String(), fmt.Sprintf("init[%d]", i)); err != nil {
return err
}
}
} else {
log.Infof("Process init image: %s", ii)
Expand All @@ -251,8 +255,10 @@ func Build(m Moby, w io.Writer, opts BuildOpts) error {
}
for i, image := range m.Onboot {
if oldConfig != nil && len(oldConfig.Onboot) > i && oldConfig.Onboot[i].Equal(image) {
if err := extractPackageFilesFromTar(in, iw, image.Image, fmt.Sprintf("onboot[%d]", i)); err != nil {
return err
if !opts.ModifiedOnly {
if err := extractPackageFilesFromTar(in, iw, image.Image, fmt.Sprintf("onboot[%d]", i)); err != nil {
return err
}
}
} else {
so := fmt.Sprintf("%03d", i)
Expand All @@ -267,8 +273,10 @@ func Build(m Moby, w io.Writer, opts BuildOpts) error {
}
for i, image := range m.Onshutdown {
if oldConfig != nil && len(oldConfig.Onshutdown) > i && oldConfig.Onshutdown[i].Equal(image) {
if err := extractPackageFilesFromTar(in, iw, image.Image, fmt.Sprintf("onshutdown[%d]", i)); err != nil {
return err
if !opts.ModifiedOnly {
if err := extractPackageFilesFromTar(in, iw, image.Image, fmt.Sprintf("onshutdown[%d]", i)); err != nil {
return err
}
}
} else {
so := fmt.Sprintf("%03d", i)
Expand All @@ -283,8 +291,10 @@ func Build(m Moby, w io.Writer, opts BuildOpts) error {
}
for i, image := range m.Services {
if oldConfig != nil && len(oldConfig.Services) > i && oldConfig.Services[i].Equal(image) {
if err := extractPackageFilesFromTar(in, iw, image.Image, fmt.Sprintf("services[%d]", i)); err != nil {
return err
if !opts.ModifiedOnly {
if err := extractPackageFilesFromTar(in, iw, image.Image, fmt.Sprintf("services[%d]", i)); err != nil {
return err
}
}
} else {
if err := outputImage(image, "services", i, "", m, idMap, dupMap, iw, opts); err != nil {
Expand Down
1 change: 1 addition & 0 deletions src/cmd/linuxkit/moby/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ type BuildOpts struct {
Arch string
SbomGenerator *SbomGenerator
InputTar string
ModifiedOnly bool
}