Discussion:
[lxc-devel] [lxd/master] lxd/init: Verify if ZFS pool exists
dnegreira on Github
2018-12-10 23:42:37 UTC
Permalink
From 3ef4c886719bff210d938e02b7895ac28a68e31e Mon Sep 17 00:00:00 2001
From: David Negreira <***@otherreality.net>
Date: Tue, 11 Dec 2018 00:12:25 +0100
Subject: [PATCH 1/2] Add zfsPoolExists func

Signed-off-by: David Negreira <***@otherreality.net>
---
lxd/storage_zfs_utils.go | 17 +++++++++++++++++
1 file changed, 17 insertions(+)

diff --git a/lxd/storage_zfs_utils.go b/lxd/storage_zfs_utils.go
index f6583a8b46..fa3f26a400 100644
--- a/lxd/storage_zfs_utils.go
+++ b/lxd/storage_zfs_utils.go
@@ -89,6 +89,23 @@ func zfsPoolCheck(pool string) error {
return nil
}

+// zfsPoolExists verifies if a specific ZFS pool exists.
+func zfsPoolExists(pool string) (bool, error) {
+ output, err := shared.RunCommand(
+ "zpool", "list", "-Ho", "name")
+
+ if err != nil {
+ return false, err
+ }
+
+ for _, name := range strings.Split(output, "\n") {
+ if name == pool {
+ return true, nil
+ }
+ }
+ return false, nil
+}
+
func zfsPoolCreate(pool string, vdev string) error {
var output string
var err error

From f32f2c6b422666e8c56ee7e552e546a35e70e15a Mon Sep 17 00:00:00 2001
From: David Negreira <***@otherreality.net>
Date: Tue, 11 Dec 2018 00:12:29 +0100
Subject: [PATCH 2/2] Add check on lxd init to verify if zfs storage pool
already exists

Signed-off-by: David Negreira <***@otherreality.net>
---
lxd/main_init_interactive.go | 9 +++++++++
1 file changed, 9 insertions(+)

diff --git a/lxd/main_init_interactive.go b/lxd/main_init_interactive.go
index 623b0bfe2e..12e9626373 100644
--- a/lxd/main_init_interactive.go
+++ b/lxd/main_init_interactive.go
@@ -495,6 +495,15 @@ func (c *cmdInit) askStoragePool(config *cmdInitData, d lxd.ContainerServer, poo

// Ask for the number of placement groups
pool.Config["ceph.osd.pg_num"] = cli.AskString("Number of placement groups [default=32]: ", "32", nil)
+ }
+ if pool.Driver == "zfs" {
+ poolexists, err := zfsPoolExists(pool.Name)
+ if err != nil {
+ return err
+ }
+ if poolexists == true {
+ return fmt.Errorf("'%s' ZFS pool already exists", pool.Name)
+ }
} else if cli.AskBool("Would you like to use an existing block device? (yes/no) [default=no]: ", "no") {
deviceExists := func(path string) error {
if !shared.IsBlockdevPath(path) {

Loading...