[lxc-devel] [lxd/master] Use Truncate instead of qemu-img

stgraber on Github lxc-bot at linuxcontainers.org
Wed Jun 24 21:10:53 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/20200624/130f4fec/attachment.bin>
-------------- next part --------------
From 64f7dab6944ac1d144edc794809c76a8e2a544bf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 24 Jun 2020 17:06:09 -0400
Subject: [PATCH 1/2] lxd/db: Fix UsedBy for profiles on storage pools
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/db/storage_pools.go | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/lxd/db/storage_pools.go b/lxd/db/storage_pools.go
index 10d5f18024..61dcc6aab1 100644
--- a/lxd/db/storage_pools.go
+++ b/lxd/db/storage_pools.go
@@ -148,12 +148,12 @@ func (c *ClusterTx) GetStoragePoolUsedBy(name string) ([]string, error) {
 			if v["pool"] != name {
 				continue
 			}
-		}
 
-		if profile.Project == "default" {
-			usedby = append(usedby, fmt.Sprintf("/1.0/profiles/%s", profile.Name))
-		} else {
-			usedby = append(usedby, fmt.Sprintf("/1.0/profiles/%s?project=%s", profile.Name, profile.Project))
+			if profile.Project == "default" {
+				usedby = append(usedby, fmt.Sprintf("/1.0/profiles/%s", profile.Name))
+			} else {
+				usedby = append(usedby, fmt.Sprintf("/1.0/profiles/%s?project=%s", profile.Name, profile.Project))
+			}
 		}
 	}
 

From 4352efdad4a40afa0005fe9916147a4dd1fe3566 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 24 Jun 2020 17:02:38 -0400
Subject: [PATCH 2/2] lxd/storage: Use Truncate to create/grow VM files
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/storage/drivers/driver_btrfs.go | 2 +-
 lxd/storage/drivers/driver_lvm.go   | 2 +-
 lxd/storage/drivers/driver_zfs.go   | 2 +-
 lxd/storage/drivers/utils.go        | 9 +++++----
 4 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/lxd/storage/drivers/driver_btrfs.go b/lxd/storage/drivers/driver_btrfs.go
index 1247df9f06..baa6418df3 100644
--- a/lxd/storage/drivers/driver_btrfs.go
+++ b/lxd/storage/drivers/driver_btrfs.go
@@ -103,7 +103,7 @@ func (d *btrfs) Create() error {
 			return err
 		}
 
-		err = createSparseFile(d.config["source"], size)
+		err = ensureSparseFile(d.config["source"], size)
 		if err != nil {
 			return errors.Wrap(err, "Failed to create the sparse file")
 		}
diff --git a/lxd/storage/drivers/driver_lvm.go b/lxd/storage/drivers/driver_lvm.go
index 1357ae58b6..24112f1b24 100644
--- a/lxd/storage/drivers/driver_lvm.go
+++ b/lxd/storage/drivers/driver_lvm.go
@@ -129,7 +129,7 @@ func (d *lvm) Create() error {
 			return fmt.Errorf("Source file location already exists")
 		}
 
-		err = createSparseFile(d.config["source"], size)
+		err = ensureSparseFile(d.config["source"], size)
 		if err != nil {
 			return errors.Wrapf(err, "Failed to create sparse file %q", d.config["source"])
 		}
diff --git a/lxd/storage/drivers/driver_zfs.go b/lxd/storage/drivers/driver_zfs.go
index d5b87ad9f9..30e76dc640 100644
--- a/lxd/storage/drivers/driver_zfs.go
+++ b/lxd/storage/drivers/driver_zfs.go
@@ -144,7 +144,7 @@ func (d *zfs) Create() error {
 			return err
 		}
 
-		err = createSparseFile(loopPath, size)
+		err = ensureSparseFile(loopPath, size)
 		if err != nil {
 			return err
 		}
diff --git a/lxd/storage/drivers/utils.go b/lxd/storage/drivers/utils.go
index 5a974baf5f..988bf98208 100644
--- a/lxd/storage/drivers/utils.go
+++ b/lxd/storage/drivers/utils.go
@@ -292,8 +292,9 @@ func deleteParentSnapshotDirIfEmpty(poolName string, volType VolumeType, volName
 	return nil
 }
 
-// createSparseFile creates a sparse empty file at specified location with specified size.
-func createSparseFile(filePath string, sizeBytes int64) error {
+// ensureSparseFile creates a sparse empty file at specified location with specified size.
+// If the path already exists, the file is truncated to the requested size.
+func ensureSparseFile(filePath string, sizeBytes int64) error {
 	f, err := os.Create(filePath)
 	if err != nil {
 		return errors.Wrapf(err, "Failed to open %s", filePath)
@@ -354,7 +355,7 @@ func ensureVolumeBlockFile(path string, sizeBytes int64) (bool, error) {
 			return false, nil
 		}
 
-		_, err = shared.RunCommand("qemu-img", "resize", "-f", "raw", path, fmt.Sprintf("%d", sizeBytes))
+		err = ensureSparseFile(path, sizeBytes)
 		if err != nil {
 			return false, errors.Wrapf(err, "Failed resizing disk image %q to size %d", path, sizeBytes)
 		}
@@ -364,7 +365,7 @@ func ensureVolumeBlockFile(path string, sizeBytes int64) (bool, error) {
 
 	// If path doesn't exist, then there has been no filler function supplied to create it from another source.
 	// So instead create an empty volume (use for PXE booting a VM).
-	_, err = shared.RunCommand("qemu-img", "create", "-f", "raw", path, fmt.Sprintf("%d", sizeBytes))
+	err = ensureSparseFile(path, sizeBytes)
 	if err != nil {
 		return false, errors.Wrapf(err, "Failed creating disk image %q as size %d", path, sizeBytes)
 	}


More information about the lxc-devel mailing list