[lxc-devel] [lxd/master] Add support for arguments to compressors
stgraber on Github
lxc-bot at linuxcontainers.org
Mon Jun 22 22:17:28 UTC 2020
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200622/17bbdb8a/attachment.bin>
-------------- next part --------------
From 78b5f55cf62446973993ca76e20f0ca4289eadaf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 22 Jun 2020 18:13:07 -0400
Subject: [PATCH 1/2] lxd: More flexible compression algorithms
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This allows for arguments to be passed.
Closes #7558
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
lxd/cluster/config.go | 9 ++++++++-
lxd/images.go | 22 ++++++++++++++++++----
2 files changed, 26 insertions(+), 5 deletions(-)
diff --git a/lxd/cluster/config.go b/lxd/cluster/config.go
index 1bb8ac8a09..479f28836e 100644
--- a/lxd/cluster/config.go
+++ b/lxd/cluster/config.go
@@ -9,6 +9,7 @@ import (
"strconv"
"time"
+ "github.com/kballard/go-shellquote"
"golang.org/x/crypto/scrypt"
"github.com/lxc/lxd/lxd/config"
@@ -373,7 +374,13 @@ func validateCompression(value string) error {
value = "tar2sqfs"
}
- _, err := exec.LookPath(value)
+ // Parse the command.
+ fields, err := shellquote.Split(value)
+ if err != nil {
+ return err
+ }
+
+ _, err = exec.LookPath(fields[0])
return err
}
diff --git a/lxd/images.go b/lxd/images.go
index 7e278ca1b5..3bcc3819f7 100644
--- a/lxd/images.go
+++ b/lxd/images.go
@@ -22,6 +22,7 @@ import (
"time"
"github.com/gorilla/mux"
+ "github.com/kballard/go-shellquote"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
@@ -113,7 +114,13 @@ func compressFile(compress string, infile io.Reader, outfile io.Writer) error {
reproducible := []string{"gzip"}
var cmd *exec.Cmd
- if compress == "squashfs" {
+ // Parse the command.
+ fields, err := shellquote.Split(compress)
+ if err != nil {
+ return err
+ }
+
+ if fields[0] == "squashfs" {
// 'tar2sqfs' do not support writing to stdout. So write to a temporary
// file first and then replay the compressed content to outfile.
tempfile, err := ioutil.TempFile("", "lxd_compress_")
@@ -124,7 +131,11 @@ func compressFile(compress string, infile io.Reader, outfile io.Writer) error {
defer os.Remove(tempfile.Name())
// Prepare 'tar2sqfs' arguments
- args := []string{"tar2sqfs", "--no-skip", "--force", "--compressor", "xz", tempfile.Name()}
+ args := []string{"tar2sqfs"}
+ if len(fields) > 1 {
+ args = append(args, fields[1:]...)
+ }
+ args = append(args, "--no-skip", "--force", "--compressor", "xz", tempfile.Name())
cmd = exec.Command(args[0], args[1:]...)
cmd.Stdin = infile
@@ -140,11 +151,14 @@ func compressFile(compress string, infile io.Reader, outfile io.Writer) error {
}
} else {
args := []string{"-c"}
- if shared.StringInSlice(compress, reproducible) {
+ if len(fields) > 1 {
+ args = append(args, fields[1:]...)
+ }
+ if shared.StringInSlice(fields[0], reproducible) {
args = append(args, "-n")
}
- cmd := exec.Command(compress, args...)
+ cmd := exec.Command(fields[0], args...)
cmd.Stdin = infile
cmd.Stdout = outfile
err := cmd.Run()
From 35ef63ab84ed65ac5c1493baee3f2a421aa08aa0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 22 Jun 2020 18:16:40 -0400
Subject: [PATCH 2/2] tests: Add test for compression options
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
test/suites/basic.sh | 3 +++
1 file changed, 3 insertions(+)
diff --git a/test/suites/basic.sh b/test/suites/basic.sh
index 37ea97c518..95f7256327 100644
--- a/test/suites/basic.sh
+++ b/test/suites/basic.sh
@@ -146,6 +146,9 @@ test_basic_usage() {
curl -k -s --cert "${LXD_CONF}/client3.crt" --key "${LXD_CONF}/client3.key" -X GET "https://${LXD_ADDR}/1.0/images" | grep "/1.0/images/" && false
lxc image delete foo-image-compressed
+ # Test compression options
+ lxc publish bar --alias=foo-image-compressed --compression="gzip --rsyncable" prop=val1
+ lxc image delete foo-image-compressed
# Test privileged container publish
lxc profile create priv
More information about the lxc-devel
mailing list