From fd11f4a6272b4f1c7c519b10ad798d335c281105 Mon Sep 17 00:00:00 2001 From: Clayton Craft Date: Tue, 21 Feb 2023 11:47:38 -0800 Subject: [PATCH] archive.New: accept compression level (MR 25) I went with a simpler implementation that uses Go compression packages to do the work. The downside of this is that the compression Level is a bit weird to set, since most libraries discourage setting the numeric compression level directly. This is configured by setting `deviceinfo_initfs_compression`, the value it expects is a string in the form: `FORMAT[:LEVEL]`, where `[:LEVEL]` is optional. Actually setting the variable at all is optional... if nothing is specified, or it can't parse the format/level from the string value, it defaults to using gzip with the "default" level for the package (which tries to mirror gzip's default, or something). The level can be one of `default`, `fast`, `best`. To configure gzip with the fastest compression (so, bigger size): deviceinfo_initfs_compression="gzip:fast"` To configure zstd with the most compression: `deviceinfo_initfs_compression="zstd:best"` To configure zstd with default compression: `deviceinfo_initfs_compression="zstd"` (or `deviceinfo_initfs_compression="zstd:default"`) In this case, `gzip:default` is assumed: deviceinfo_initfs_compression="bananas:mmmm"` --- cmd/mkinitfs/main.go | 11 +++++++---- internal/archive/archive.go | 4 +++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/cmd/mkinitfs/main.go b/cmd/mkinitfs/main.go index 37de407..0236a49 100644 --- a/cmd/mkinitfs/main.go +++ b/cmd/mkinitfs/main.go @@ -63,6 +63,9 @@ func main() { return } + // deviceinfo.InitfsCompression needs a little more post-processing + compressionFormat, compressionLevel := archive.ExtractFormatLevel(devinfo.InitfsCompression) + defer misc.TimeFunc(time.Now(), "mkinitfs") kernVer, err := osutil.GetKernelVersion() @@ -91,7 +94,7 @@ func main() { log.Print("Generating for kernel version: ", kernVer) log.Print("Output directory: ", *outDir) - if err := generateArchive("initramfs", devinfo.InitfsCompression, workDir, []filelist.FileLister{ + if err := generateArchive("initramfs", compressionFormat, compressionLevel, workDir, []filelist.FileLister{ hookdirs.New("/usr/share/mkinitfs/dirs"), hookdirs.New("/etc/mkinitfs/dirs"), hookfiles.New("/usr/share/mkinitfs/files"), @@ -107,7 +110,7 @@ func main() { return } - if err := generateArchive("initramfs-extra", devinfo.InitfsCompression, workDir, []filelist.FileLister{ + if err := generateArchive("initramfs-extra", compressionFormat, compressionLevel, workDir, []filelist.FileLister{ hookfiles.New("/usr/share/mkinitfs/files-extra"), hookfiles.New("/etc/mkinitfs/files-extra"), hookscripts.New("/usr/share/mkinitfs/hooks-extra"), @@ -141,10 +144,10 @@ func bootDeploy(workDir, outDir, ubootBoardname string) error { return bd.Run() } -func generateArchive(name string, compressionFormat string, path string, features []filelist.FileLister) error { +func generateArchive(name string, format archive.CompressFormat, level archive.CompressLevel, path string, features []filelist.FileLister) error { log.Printf("== Generating %s ==\n", name) defer misc.TimeFunc(time.Now(), name) - a, err := archive.New(archive.CompressFormat(compressionFormat)) + a, err := archive.New(format, level) if err != nil { return err } diff --git a/internal/archive/archive.go b/internal/archive/archive.go index 0eb4554..b22b2a9 100644 --- a/internal/archive/archive.go +++ b/internal/archive/archive.go @@ -45,14 +45,16 @@ type Archive struct { cpioWriter *cpio.Writer buf *bytes.Buffer compress_format CompressFormat + compress_level CompressLevel } -func New(format CompressFormat) (*Archive, error) { +func New(format CompressFormat, level CompressLevel) (*Archive, error) { buf := new(bytes.Buffer) archive := &Archive{ cpioWriter: cpio.NewWriter(buf), buf: buf, compress_format: format, + compress_level: level, } return archive, nil