[lxc-devel] [lxd/master] lxd/db/cluster: Fix incorrect storage volume node IDs

monstermunchkin on Github lxc-bot at linuxcontainers.org
Mon Aug 31 12:24:41 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/20200831/7271f816/attachment.bin>
-------------- next part --------------
From 024e87531934c3125f5a28eaa1d1cf5390debc09 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Mon, 31 Aug 2020 13:35:45 +0200
Subject: [PATCH] lxd/db/cluster: Fix incorrect storage volume node IDs

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 lxd/db/cluster/schema.go |  2 +-
 lxd/db/cluster/update.go | 28 ++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/lxd/db/cluster/schema.go b/lxd/db/cluster/schema.go
index 688575a342..de1981674d 100644
--- a/lxd/db/cluster/schema.go
+++ b/lxd/db/cluster/schema.go
@@ -573,5 +573,5 @@ CREATE TABLE storage_volumes_snapshots_config (
     UNIQUE (storage_volume_snapshot_id, key)
 );
 
-INSERT INTO schema (version, updated_at) VALUES (35, strftime("%s"))
+INSERT INTO schema (version, updated_at) VALUES (36, strftime("%s"))
 `
diff --git a/lxd/db/cluster/update.go b/lxd/db/cluster/update.go
index cf7bf3f008..b6bce4a2dc 100644
--- a/lxd/db/cluster/update.go
+++ b/lxd/db/cluster/update.go
@@ -72,6 +72,34 @@ var updates = map[int]schema.Update{
 	33: updateFromV32,
 	34: updateFromV33,
 	35: updateFromV34,
+	36: updateFromV35,
+}
+
+// This fixes node IDs of storage volumes on non-remote pools which were
+// wrongly set to NULL.
+func updateFromV35(tx *sql.Tx) error {
+	stmts := `
+WITH storage_volumes_tmp (id, node_id)
+AS (
+  SELECT storage_volumes.id, storage_pools_nodes.node_id
+  FROM storage_volumes
+	JOIN storage_pools_nodes ON storage_pools_nodes.storage_pool_id=storage_volumes.storage_pool_id
+	JOIN storage_pools ON storage_pools.id=storage_volumes.storage_pool_id
+  WHERE storage_pools.driver NOT IN ("ceph", "cephfs"))
+UPDATE storage_volumes
+SET node_id=(
+  SELECT storage_volumes_tmp.node_id
+  FROM storage_volumes_tmp
+  WHERE storage_volumes.id=storage_volumes_tmp.id)
+WHERE id IN (SELECT id FROM storage_volumes_tmp) AND node_id IS NULL
+`
+
+	_, err := tx.Exec(stmts)
+	if err != nil {
+		return err
+	}
+
+	return nil
 }
 
 // Remove multiple entries of the same volume when using remote storage.


More information about the lxc-devel mailing list