[lxc-devel] [lxd/master] Fix a number of lxd-p2c issues

stgraber on Github lxc-bot at linuxcontainers.org
Wed May 23 18:40:56 UTC 2018


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/20180523/4dc80f79/attachment.bin>
-------------- next part --------------
From c0566d1d35519304b685d832f3739634a314a575 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 23 May 2018 11:37:46 -0400
Subject: [PATCH 1/4] lxd-p2c: Ignore missing arg errors
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>
---
 lxd-p2c/transfer.go | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lxd-p2c/transfer.go b/lxd-p2c/transfer.go
index 45a4eadcb..f1b6737bc 100644
--- a/lxd-p2c/transfer.go
+++ b/lxd-p2c/transfer.go
@@ -71,6 +71,7 @@ func rsyncSendSetup(path string) (*exec.Cmd, net.Conn, io.ReadCloser, error) {
 		"--numeric-ids",
 		"--partial",
 		"--sparse",
+		"--ignore-missing-args",
 		path,
 		"localhost:/tmp/foo",
 		"-e",

From 1b0fa784373fc8b8c93076cff631ec3f1fcd97ff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 23 May 2018 12:18:07 -0400
Subject: [PATCH 2/4] lxd-p2c: Delete containers on failure
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>
---
 lxd-p2c/main_migrate.go | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/lxd-p2c/main_migrate.go b/lxd-p2c/main_migrate.go
index 69555d701..7da4b3bef 100644
--- a/lxd-p2c/main_migrate.go
+++ b/lxd-p2c/main_migrate.go
@@ -180,12 +180,25 @@ func (c *cmdMigrate) Run(cmd *cobra.Command, args []string) error {
 		}
 	}
 
+	// Check if the container already exists
+	_, _, err = dst.GetContainer(apiArgs.Name)
+	if err == nil {
+		return fmt.Errorf("Container '%s' already exists", apiArgs.Name)
+	}
+
 	// Create the container
+	success := false
 	op, err := dst.CreateContainer(apiArgs)
 	if err != nil {
 		return err
 	}
 
+	defer func() {
+		if !success {
+			dst.DeleteContainer(apiArgs.Name)
+		}
+	}()
+
 	progress := utils.ProgressRenderer{Format: "Transferring container: %s"}
 	_, err = op.AddHandler(progress.UpdateOp)
 	if err != nil {
@@ -199,6 +212,7 @@ func (c *cmdMigrate) Run(cmd *cobra.Command, args []string) error {
 	}
 
 	progress.Done(fmt.Sprintf("Container %s successfully created", apiArgs.Name))
+	success = true
 
 	return nil
 }

From 76fe0267f91c711838f52b004a1b2c657cd6cd5f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 23 May 2018 13:49:00 -0400
Subject: [PATCH 3/4] lxd-p2c: Better report rsync errors
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes #4571

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd-p2c/transfer.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lxd-p2c/transfer.go b/lxd-p2c/transfer.go
index f1b6737bc..8287060f1 100644
--- a/lxd-p2c/transfer.go
+++ b/lxd-p2c/transfer.go
@@ -40,7 +40,7 @@ func rsyncSend(conn *websocket.Conn, path string) error {
 	<-writeDone
 
 	if err != nil {
-		return err
+		return fmt.Errorf("Failed to rsync: %v\n%s", err, output)
 	}
 
 	return nil

From 38fa1bb708fc02231d18c1986aea6208df8ab9e1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 23 May 2018 13:57:50 -0400
Subject: [PATCH 4/4] lxd-p2c: Allow overriding rsync args
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes #4571

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd-p2c/main_migrate.go |  4 +++-
 lxd-p2c/transfer.go     | 23 +++++++++++++++--------
 lxd-p2c/utils.go        |  4 ++--
 3 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/lxd-p2c/main_migrate.go b/lxd-p2c/main_migrate.go
index 7da4b3bef..239d9e357 100644
--- a/lxd-p2c/main_migrate.go
+++ b/lxd-p2c/main_migrate.go
@@ -24,6 +24,7 @@ type cmdMigrate struct {
 	flagProfile    []string
 	flagStorage    string
 	flagType       string
+	flagRsyncArgs  string
 	flagNoProfiles bool
 }
 
@@ -49,6 +50,7 @@ func (c *cmdMigrate) Command() *cobra.Command {
 	cmd.Flags().StringArrayVarP(&c.flagProfile, "profile", "p", nil, "Profile to apply to the container"+"``")
 	cmd.Flags().StringVarP(&c.flagStorage, "storage", "s", "", "Storage pool to use for the container"+"``")
 	cmd.Flags().StringVarP(&c.flagType, "type", "t", "", "Instance type to use for the container"+"``")
+	cmd.Flags().StringVar(&c.flagRsyncArgs, "rsync-args", "", "Extra arguments to pass to rsync"+"``")
 	cmd.Flags().BoolVar(&c.flagNoProfiles, "no-profiles", false, "Create the container with no profiles applied")
 
 	return cmd
@@ -206,7 +208,7 @@ func (c *cmdMigrate) Run(cmd *cobra.Command, args []string) error {
 		return err
 	}
 
-	err = transferRootfs(dst, op, fullPath)
+	err = transferRootfs(dst, op, fullPath, c.flagRsyncArgs)
 	if err != nil {
 		return err
 	}
diff --git a/lxd-p2c/transfer.go b/lxd-p2c/transfer.go
index 8287060f1..b60fe6e67 100644
--- a/lxd-p2c/transfer.go
+++ b/lxd-p2c/transfer.go
@@ -7,6 +7,7 @@ import (
 	"net"
 	"os"
 	"os/exec"
+	"strings"
 
 	"github.com/gorilla/websocket"
 	"github.com/pborman/uuid"
@@ -16,8 +17,8 @@ import (
 )
 
 // Send an rsync stream of a path over a websocket
-func rsyncSend(conn *websocket.Conn, path string) error {
-	cmd, dataSocket, stderr, err := rsyncSendSetup(path)
+func rsyncSend(conn *websocket.Conn, path string, rsyncArgs string) error {
+	cmd, dataSocket, stderr, err := rsyncSendSetup(path, rsyncArgs)
 	if err != nil {
 		return err
 	}
@@ -47,7 +48,7 @@ func rsyncSend(conn *websocket.Conn, path string) error {
 }
 
 // Spawn the rsync process
-func rsyncSendSetup(path string) (*exec.Cmd, net.Conn, io.ReadCloser, error) {
+func rsyncSendSetup(path string, rsyncArgs string) (*exec.Cmd, net.Conn, io.ReadCloser, error) {
 	auds := fmt.Sprintf("@lxd-p2c/%s", uuid.NewRandom().String())
 	if len(auds) > shared.ABSTRACT_UNIX_SOCK_LEN-1 {
 		auds = auds[:shared.ABSTRACT_UNIX_SOCK_LEN-1]
@@ -65,17 +66,23 @@ func rsyncSendSetup(path string) (*exec.Cmd, net.Conn, io.ReadCloser, error) {
 
 	rsyncCmd := fmt.Sprintf("sh -c \"%s netcat %s\"", execPath, auds)
 
-	cmd := exec.Command("rsync",
+	args := []string{
 		"-arvP",
 		"--devices",
 		"--numeric-ids",
 		"--partial",
 		"--sparse",
 		"--ignore-missing-args",
-		path,
-		"localhost:/tmp/foo",
-		"-e",
-		rsyncCmd)
+	}
+
+	if rsyncArgs != "" {
+		args = append(args, strings.Split(rsyncArgs, " ")...)
+	}
+
+	args = append(args, []string{path, "localhost:/tmp/foo"}...)
+	args = append(args, []string{"-e", rsyncCmd}...)
+
+	cmd := exec.Command("rsync", args...)
 
 	stderr, err := cmd.StderrPipe()
 	if err != nil {
diff --git a/lxd-p2c/utils.go b/lxd-p2c/utils.go
index 5268508ab..0b5b388ec 100644
--- a/lxd-p2c/utils.go
+++ b/lxd-p2c/utils.go
@@ -16,7 +16,7 @@ import (
 	"github.com/lxc/lxd/shared/api"
 )
 
-func transferRootfs(dst lxd.ContainerServer, op lxd.Operation, rootfs string) error {
+func transferRootfs(dst lxd.ContainerServer, op lxd.Operation, rootfs string, rsyncArgs string) error {
 	opAPI := op.Get()
 
 	// Connect to the websockets
@@ -54,7 +54,7 @@ func transferRootfs(dst lxd.ContainerServer, op lxd.Operation, rootfs string) er
 		return err
 	}
 
-	err = rsyncSend(wsFs, rootfs)
+	err = rsyncSend(wsFs, rootfs, rsyncArgs)
 	if err != nil {
 		return abort(err)
 	}


More information about the lxc-devel mailing list