[lxc-devel] [lxd/master] storage: bugfixes

brauner on Github lxc-bot at linuxcontainers.org
Thu Feb 16 23:06:24 UTC 2017


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/20170216/135c3aed/attachment.bin>
-------------- next part --------------
From fb06eec959cb5a89ca5cf00083965350aed31543 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Thu, 16 Feb 2017 21:13:19 +0100
Subject: [PATCH 1/4] lxd/main: fix lxd init
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/main.go      |  2 +-
 lxd/main_init.go | 25 ++++++++++++++-----------
 2 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/lxd/main.go b/lxd/main.go
index 52f4972..6d4bd47 100644
--- a/lxd/main.go
+++ b/lxd/main.go
@@ -26,7 +26,7 @@ var argPrintGoroutinesEvery = gnuflag.Int("print-goroutines-every", -1, "")
 var argStorageBackend = gnuflag.String("storage-backend", "", "")
 var argStorageCreateDevice = gnuflag.String("storage-create-device", "", "")
 var argStorageCreateLoop = gnuflag.Int64("storage-create-loop", -1, "")
-var argStoragePool = gnuflag.String("storage-pool", "", "")
+var argStorageDataset = gnuflag.String("storage-pool", "", "")
 var argSyslog = gnuflag.Bool("syslog", false, "")
 var argTimeout = gnuflag.Int("timeout", -1, "")
 var argTrustPassword = gnuflag.String("trust-password", "", "")
diff --git a/lxd/main_init.go b/lxd/main_init.go
index 0063bd3..d831113 100644
--- a/lxd/main_init.go
+++ b/lxd/main_init.go
@@ -23,6 +23,7 @@ func cmdInit() error {
 	var storageLoopSize int64 // Size in GB
 	var storageDevice string  // Path
 	var storagePool string    // pool name
+	var storageDataset string // existing ZFS pool name
 	var networkAddress string // Address
 	var networkPort int64     // Port
 	var trustPassword string  // Trust password
@@ -192,7 +193,7 @@ func cmdInit() error {
 		}
 
 		if *argStorageBackend == "dir" {
-			if *argStorageCreateLoop != -1 || *argStorageCreateDevice != "" || *argStoragePool != "" {
+			if *argStorageCreateLoop != -1 || *argStorageCreateDevice != "" || *argStorageDataset != "" {
 				return fmt.Errorf("None of --storage-pool, --storage-create-device or --storage-create-loop may be used with the 'dir' backend.")
 			}
 		}
@@ -202,7 +203,7 @@ func cmdInit() error {
 				return fmt.Errorf("Only one of --storage-create-device or --storage-create-loop can be specified with the 'zfs' backend.")
 			}
 
-			if *argStoragePool == "" {
+			if *argStorageDataset == "" {
 				return fmt.Errorf("--storage-pool must be specified with the 'zfs' backend.")
 			}
 		}
@@ -219,13 +220,14 @@ func cmdInit() error {
 		storageBackend = *argStorageBackend
 		storageLoopSize = *argStorageCreateLoop
 		storageDevice = *argStorageCreateDevice
-		storagePool = *argStoragePool
+		storageDataset = *argStorageDataset
 		networkAddress = *argNetworkAddress
 		networkPort = *argNetworkPort
 		trustPassword = *argTrustPassword
+		storagePool = "default"
 		storageSetup = true
 	} else {
-		if *argStorageBackend != "" || *argStorageCreateDevice != "" || *argStorageCreateLoop != -1 || *argStoragePool != "" || *argNetworkAddress != "" || *argNetworkPort != -1 || *argTrustPassword != "" {
+		if *argStorageBackend != "" || *argStorageCreateDevice != "" || *argStorageCreateLoop != -1 || *argStorageDataset != "" || *argNetworkAddress != "" || *argNetworkPort != -1 || *argTrustPassword != "" {
 			return fmt.Errorf("Init configuration is only valid with --auto")
 		}
 
@@ -278,7 +280,7 @@ func cmdInit() error {
 					storageLoopSize = askInt(q, 1, -1, fmt.Sprintf("%d", def))
 				}
 			} else {
-				storagePool = askString("Name of the existing ZFS pool or dataset: ", "", nil)
+				storageDataset = askString("Name of the existing ZFS pool or dataset: ", "", nil)
 			}
 		}
 
@@ -360,14 +362,15 @@ they otherwise would.
 			}
 		}
 
-		// Destroy any existing loop device
-		for _, file := range []string{"zfs.img"} {
-			os.Remove(shared.VarPath(file))
+		// Pool configuration
+		storageConfig := map[string]string{}
+		if storageDevice != "" {
+			storageConfig["source"] = storageDevice
+		} else if storageDataset != "" {
+			storageConfig["source"] = storageDataset
 		}
 
-		storageConfig := map[string]string{}
-		storageConfig["source"] = storageDevice
-		if storageBackend != "dir" {
+		if storageBackend != "dir" && storageLoopSize != 0 {
 			storageConfig["size"] = strconv.FormatInt(storageLoopSize, 10) + "GB"
 		}
 

From 0c1c7f5b42b70eeac16558c030f6734e05c48816 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Thu, 16 Feb 2017 21:21:40 +0100
Subject: [PATCH 2/4] lxd/main_init: small fixes

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 lxd/main_init.go | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/lxd/main_init.go b/lxd/main_init.go
index d831113..6fe86c4 100644
--- a/lxd/main_init.go
+++ b/lxd/main_init.go
@@ -202,10 +202,6 @@ func cmdInit() error {
 			if *argStorageCreateLoop != -1 && *argStorageCreateDevice != "" {
 				return fmt.Errorf("Only one of --storage-create-device or --storage-create-loop can be specified with the 'zfs' backend.")
 			}
-
-			if *argStorageDataset == "" {
-				return fmt.Errorf("--storage-pool must be specified with the 'zfs' backend.")
-			}
 		}
 
 		if *argNetworkAddress == "" {
@@ -251,6 +247,7 @@ func cmdInit() error {
 		}
 
 		if storageSetup && storageBackend == "zfs" {
+			storageLoopSize = -1
 			if askBool("Create a new ZFS pool (yes/no) [default=yes]? ", "yes") {
 				if askBool("Would you like to use an existing block device (yes/no) [default=no]? ", "no") {
 					deviceExists := func(path string) error {
@@ -366,11 +363,21 @@ they otherwise would.
 		storageConfig := map[string]string{}
 		if storageDevice != "" {
 			storageConfig["source"] = storageDevice
-		} else if storageDataset != "" {
+			// The user probably wants to give the storage pool a
+			// custom name.
+			if storageDataset != "" {
+				storagePool = storageDataset
+			}
+		} else if storageDataset != "" && storageBackend == "zfs" && storageLoopSize < 0 {
 			storageConfig["source"] = storageDataset
 		}
 
-		if storageBackend != "dir" && storageLoopSize != 0 {
+		if storageBackend != "dir" && storageLoopSize > 0 {
+			// The user probably wants to give the storage pool a
+			// custom name.
+			if storageDataset != "" {
+				storagePool = storageDataset
+			}
 			storageConfig["size"] = strconv.FormatInt(storageLoopSize, 10) + "GB"
 		}
 

From 5fe80755841f408c61a8c30d686d28d80e79c176 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Thu, 16 Feb 2017 23:11:55 +0100
Subject: [PATCH 3/4] test: add lxd init --auto tests

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 test/main.sh           | 108 ++++++++++++++++++++++++++++++++++
 test/suites/init.sh    |  81 ++++++++++++++++++++++++++
 test/suites/storage.sh | 153 ++++++++-----------------------------------------
 3 files changed, 212 insertions(+), 130 deletions(-)
 create mode 100644 test/suites/init.sh

diff --git a/test/main.sh b/test/main.sh
index d2e176e..be3c592 100755
--- a/test/main.sh
+++ b/test/main.sh
@@ -397,6 +397,113 @@ wipe() {
   rm -Rf "${1}"
 }
 
+configure_lvm_loop_device() {
+  lv_loop_file=$(mktemp -p "${TEST_DIR}" XXXX.lvm)
+  truncate -s 4G "${lv_loop_file}"
+  pvloopdev=$(losetup --show -f "${lv_loop_file}")
+  if [ ! -e "${pvloopdev}" ]; then
+    echo "failed to setup loop"
+    false
+  fi
+
+  pvcreate "${pvloopdev}"
+
+  # The following code enables to return a value from a shell function by
+  # calling the function as: fun VAR1
+
+  # shellcheck disable=2039
+  local  __tmp1="${1}"
+  # shellcheck disable=2039
+  local  res1="${lv_loop_file}"
+  if [ "${__tmp1}" ]; then
+      eval "${__tmp1}='${res1}'"
+  fi
+
+  # shellcheck disable=2039
+  local  __tmp2="${2}"
+  # shellcheck disable=2039
+  local  res2="${pvloopdev}"
+  if [ "${__tmp2}" ]; then
+      eval "${__tmp2}='${res2}'"
+  fi
+}
+
+deconfigure_lvm_loop_device() {
+  lv_loop_file="${1}"
+  loopdev="${2}"
+
+  SUCCESS=0
+  # shellcheck disable=SC2034
+  for i in $(seq 10); do
+    pvremove -f "${loopdev}" > /dev/null 2>&1 || true
+    if losetup -d "${loopdev}"; then
+      SUCCESS=1
+      break
+    fi
+
+    sleep 0.5
+  done
+
+  if [ "${SUCCESS}" = "0" ]; then
+    echo "Failed to tear down loop device."
+    false
+  fi
+
+  rm -f "${lv_loop_file}"
+}
+
+configure_loop_device() {
+  lv_loop_file=$(mktemp -p "${TEST_DIR}" XXXX.img)
+  truncate -s 10G "${lv_loop_file}"
+  pvloopdev=$(losetup --show -f "${lv_loop_file}")
+  if [ ! -e "${pvloopdev}" ]; then
+    echo "failed to setup loop"
+    false
+  fi
+
+  # The following code enables to return a value from a shell function by
+  # calling the function as: fun VAR1
+
+  # shellcheck disable=2039
+  local  __tmp1="${1}"
+  # shellcheck disable=2039
+  local  res1="${lv_loop_file}"
+  if [ "${__tmp1}" ]; then
+      eval "${__tmp1}='${res1}'"
+  fi
+
+  # shellcheck disable=2039
+  local  __tmp2="${2}"
+  # shellcheck disable=2039
+  local  res2="${pvloopdev}"
+  if [ "${__tmp2}" ]; then
+      eval "${__tmp2}='${res2}'"
+  fi
+}
+
+deconfigure_loop_device() {
+  lv_loop_file="${1}"
+  loopdev="${2}"
+
+  SUCCESS=0
+  # shellcheck disable=SC2034
+  for i in $(seq 10); do
+    if losetup -d "${loopdev}"; then
+      SUCCESS=1
+      break
+    fi
+
+    sleep 0.5
+  done
+
+  if [ "${SUCCESS}" = "0" ]; then
+    echo "Failed to tear down loop device"
+    false
+  fi
+
+  rm -f "${lv_loop_file}"
+}
+
 # Must be set before cleanup()
 TEST_CURRENT=setup
 TEST_RESULT=failure
@@ -479,5 +586,6 @@ run_test test_fdleak "fd leak"
 run_test test_cpu_profiling "CPU profiling"
 run_test test_mem_profiling "memory profiling"
 run_test test_storage "storage"
+run_test test_lxd_autoinit "lxd init auto"
 
 TEST_RESULT=success
diff --git a/test/suites/init.sh b/test/suites/init.sh
new file mode 100644
index 0000000..8a41846
--- /dev/null
+++ b/test/suites/init.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+
+test_lxd_autoinit() {
+  # lxd init --auto
+  LXD_INIT_DIR=$(mktemp -d -p "${TEST_DIR}" XXX)
+  chmod +x "${LXD_INIT_DIR}"
+  spawn_lxd "${LXD_INIT_DIR}" false
+
+  ZFS_POOL="lxdtest-$(basename "${LXD_DIR}")-init"
+  LXD_DIR=${LXD_INIT_DIR} lxd init --auto
+
+  kill_lxd "${LXD_INIT_DIR}"
+
+  # lxd init --auto --storage-backend zfs
+  if [ "${LXD_BACKEND}" = "zfs" ]; then
+    LXD_INIT_DIR=$(mktemp -d -p "${TEST_DIR}" XXX)
+    chmod +x "${LXD_INIT_DIR}"
+    spawn_lxd "${LXD_INIT_DIR}" false
+
+    LXD_DIR=${LXD_INIT_DIR} lxd init --auto --storage-backend zfs
+
+    kill_lxd "${LXD_INIT_DIR}"
+  fi
+
+  # lxd init --auto --storage-backend zfs --storage-pool <name>
+  if [ "${LXD_BACKEND}" = "zfs" ]; then
+    LXD_INIT_DIR=$(mktemp -d -p "${TEST_DIR}" XXX)
+    chmod +x "${LXD_INIT_DIR}"
+    spawn_lxd "${LXD_INIT_DIR}" false
+
+    configure_loop_device loop_file_1 loop_device_1
+    zpool create "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool" "${loop_device_1}" -m none -O compression=on
+    LXD_DIR=${LXD_INIT_DIR} lxd init --auto --storage-backend zfs --storage-pool "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool"
+
+    kill_lxd "${LXD_INIT_DIR}"
+    deconfigure_loop_device "${loop_file_1}" "${loop_device_1}"
+  fi
+
+  # lxd init --auto --storage-backend zfs --storage-pool <name>/<non-existing-dataset>
+  if [ "${LXD_BACKEND}" = "zfs" ]; then
+    LXD_INIT_DIR=$(mktemp -d -p "${TEST_DIR}" XXX)
+    chmod +x "${LXD_INIT_DIR}"
+    spawn_lxd "${LXD_INIT_DIR}" false
+
+    configure_loop_device loop_file_1 loop_device_1
+    zpool create "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool" "${loop_device_1}" -m none -O compression=on
+    LXD_DIR=${LXD_INIT_DIR} lxd init --auto --storage-backend zfs --storage-pool "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool/non-existing-dataset"
+
+    kill_lxd "${LXD_INIT_DIR}"
+    deconfigure_loop_device "${loop_file_1}" "${loop_device_1}"
+    zpool destroy -f "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool"
+  fi
+
+  # lxd init --auto --storage-backend zfs --storage-pool <name>/<existing-dataset>
+  if [ "${LXD_BACKEND}" = "zfs" ]; then
+    LXD_INIT_DIR=$(mktemp -d -p "${TEST_DIR}" XXX)
+    chmod +x "${LXD_INIT_DIR}"
+    spawn_lxd "${LXD_INIT_DIR}" false
+
+    configure_loop_device loop_file_1 loop_device_1
+    zpool create "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool" "${loop_device_1}" -f -m none -O compression=on
+    zfs create -p -o mountpoint=none "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool/existing-dataset"
+    LXD_DIR=${LXD_INIT_DIR} lxd init --auto --storage-backend zfs --storage-pool "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool/existing-dataset"
+
+    kill_lxd "${LXD_INIT_DIR}"
+    deconfigure_loop_device "${loop_file_1}" "${loop_device_1}"
+    zpool destroy -f "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool"
+  fi
+
+ # lxd init --storage-backend zfs --storage-create-loop 1 --storage-pool <name> --auto
+  if [ "${LXD_BACKEND}" = "zfs" ]; then
+    LXD_INIT_DIR=$(mktemp -d -p "${TEST_DIR}" XXX)
+    chmod +x "${LXD_INIT_DIR}"
+    spawn_lxd "${LXD_INIT_DIR}" false
+
+    ZFS_POOL="lxdtest-$(basename "${LXD_DIR}")-init"
+    LXD_DIR=${LXD_INIT_DIR} lxd init --storage-backend zfs --storage-create-loop 1 --storage-pool "${ZFS_POOL}" --auto
+
+    kill_lxd "${LXD_INIT_DIR}"
+  fi
+}
diff --git a/test/suites/storage.sh b/test/suites/storage.sh
index 20d2c5e..b4a6f24 100644
--- a/test/suites/storage.sh
+++ b/test/suites/storage.sh
@@ -1,112 +1,5 @@
 #!/bin/sh
 
-configure_lvm_loop_device() {
-  lv_loop_file=$(mktemp -p "${TEST_DIR}" XXXX.lvm)
-  truncate -s 4G "${lv_loop_file}"
-  pvloopdev=$(losetup --show -f "${lv_loop_file}")
-  if [ ! -e "${pvloopdev}" ]; then
-    echo "failed to setup loop"
-    false
-  fi
-
-  pvcreate "${pvloopdev}"
-
-  # The following code enables to return a value from a shell function by
-  # calling the function as: fun VAR1
-
-  # shellcheck disable=2039
-  local  __tmp1="${1}"
-  # shellcheck disable=2039
-  local  res1="${lv_loop_file}"
-  if [ "${__tmp1}" ]; then
-      eval "${__tmp1}='${res1}'"
-  fi
-
-  # shellcheck disable=2039
-  local  __tmp2="${2}"
-  # shellcheck disable=2039
-  local  res2="${pvloopdev}"
-  if [ "${__tmp2}" ]; then
-      eval "${__tmp2}='${res2}'"
-  fi
-}
-
-deconfigure_lvm_loop_device() {
-  lv_loop_file="${1}"
-  loopdev="${2}"
-
-  SUCCESS=0
-  # shellcheck disable=SC2034
-  for i in $(seq 10); do
-    pvremove -f "${loopdev}" > /dev/null 2>&1 || true
-    if losetup -d "${loopdev}"; then
-      SUCCESS=1
-      break
-    fi
-
-    sleep 0.5
-  done
-
-  if [ "${SUCCESS}" = "0" ]; then
-    echo "Failed to tear down loop device."
-    false
-  fi
-
-  rm -f "${lv_loop_file}"
-}
-
-configure_loop_device() {
-  lv_loop_file=$(mktemp -p "${TEST_DIR}" XXXX.img)
-  truncate -s 10G "${lv_loop_file}"
-  pvloopdev=$(losetup --show -f "${lv_loop_file}")
-  if [ ! -e "${pvloopdev}" ]; then
-    echo "failed to setup loop"
-    false
-  fi
-
-  # The following code enables to return a value from a shell function by
-  # calling the function as: fun VAR1
-
-  # shellcheck disable=2039
-  local  __tmp1="${1}"
-  # shellcheck disable=2039
-  local  res1="${lv_loop_file}"
-  if [ "${__tmp1}" ]; then
-      eval "${__tmp1}='${res1}'"
-  fi
-
-  # shellcheck disable=2039
-  local  __tmp2="${2}"
-  # shellcheck disable=2039
-  local  res2="${pvloopdev}"
-  if [ "${__tmp2}" ]; then
-      eval "${__tmp2}='${res2}'"
-  fi
-}
-
-deconfigure_loop_device() {
-  lv_loop_file="${1}"
-  loopdev="${2}"
-
-  SUCCESS=0
-  # shellcheck disable=SC2034
-  for i in $(seq 10); do
-    if losetup -d "${loopdev}"; then
-      SUCCESS=1
-      break
-    fi
-
-    sleep 0.5
-  done
-
-  if [ "${SUCCESS}" = "0" ]; then
-    echo "Failed to tear down loop device"
-    false
-  fi
-
-  rm -f "${lv_loop_file}"
-}
-
 test_storage() {
   LXD_STORAGE_DIR=$(mktemp -d -p "${TEST_DIR}" XXXXXXXXX)
   chmod +x "${LXD_STORAGE_DIR}"
@@ -128,18 +21,18 @@ test_storage() {
     lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool1" "${BACKEND}"
 
     if [ "${BACKEND}" = "zfs" ]; then
-	# Let LXD use an already existing dataset.
-	zfs create -p -o mountpoint=none "lxdtest-$(basename "${LXD_DIR}")-pool1/existing-dataset-as-pool"
-	lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool7" "${BACKEND}" source="lxdtest-$(basename "${LXD_DIR}")-pool1/existing-dataset-as-pool"
-
-	# Let LXD use an already existing storage pool.
-	configure_loop_device loop_file_4 loop_device_4
-	# shellcheck disable=SC2154
-	zpool create "lxdtest-$(basename "${LXD_DIR}")-pool9-existing-pool" "${loop_device_4}" -f -m none -O compression=on
-	lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool9" "${BACKEND}" source="lxdtest-$(basename "${LXD_DIR}")-pool9-existing-pool"
-
-	# Let LXD create a new dataset and use as pool.
-	lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool8" "${BACKEND}" source="lxdtest-$(basename "${LXD_DIR}")-pool1/non-existing-dataset-as-pool"
+      # Let LXD use an already existing dataset.
+      zfs create -p -o mountpoint=none "lxdtest-$(basename "${LXD_DIR}")-pool1/existing-dataset-as-pool"
+      lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool7" "${BACKEND}" source="lxdtest-$(basename "${LXD_DIR}")-pool1/existing-dataset-as-pool"
+
+      # Let LXD use an already existing storage pool.
+      configure_loop_device loop_file_4 loop_device_4
+      # shellcheck disable=SC2154
+      zpool create "lxdtest-$(basename "${LXD_DIR}")-pool9-existing-pool" "${loop_device_4}" -f -m none -O compression=on
+      lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool9" "${BACKEND}" source="lxdtest-$(basename "${LXD_DIR}")-pool9-existing-pool"
+
+      # Let LXD create a new dataset and use as pool.
+      lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool8" "${BACKEND}" source="lxdtest-$(basename "${LXD_DIR}")-pool1/non-existing-dataset-as-pool"
     fi
 
     # Create device backed zfs pool
@@ -230,24 +123,24 @@ test_storage() {
     lxc delete -f c12pool6
 
     if [ "${BACKEND}" = "zfs" ]; then
-	lxc delete -f c13pool7
-	lxc delete -f c14pool7
+      lxc delete -f c13pool7
+      lxc delete -f c14pool7
 
-	lxc delete -f c15pool8
-	lxc delete -f c16pool8
+      lxc delete -f c15pool8
+      lxc delete -f c16pool8
 
-	lxc delete -f c17pool9
-	lxc delete -f c18pool9
+      lxc delete -f c17pool9
+      lxc delete -f c18pool9
     fi
 
     lxc image delete testimage
 
     if [ "${BACKEND}" = "zfs" ]; then
-	lxc storage delete "lxdtest-$(basename "${LXD_DIR}")-pool7"
-	lxc storage delete "lxdtest-$(basename "${LXD_DIR}")-pool8"
-	lxc storage delete "lxdtest-$(basename "${LXD_DIR}")-pool9"
-	# shellcheck disable=SC2154
-	deconfigure_loop_device "${loop_file_4}" "${loop_device_4}"
+      lxc storage delete "lxdtest-$(basename "${LXD_DIR}")-pool7"
+      lxc storage delete "lxdtest-$(basename "${LXD_DIR}")-pool8"
+      lxc storage delete "lxdtest-$(basename "${LXD_DIR}")-pool9"
+      # shellcheck disable=SC2154
+      deconfigure_loop_device "${loop_file_4}" "${loop_device_4}"
     fi
 
     lxc storage delete "lxdtest-$(basename "${LXD_DIR}")-pool2"

From 67910e98005ed4d9b198b3b1bd6b91957192d775 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Thu, 16 Feb 2017 23:35:13 +0100
Subject: [PATCH 4/4] test: execute tests based on available tools

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 test/suites/init.sh    |  17 +++--
 test/suites/storage.sh | 169 ++++++++++++++++++++++++++++---------------------
 2 files changed, 110 insertions(+), 76 deletions(-)

diff --git a/test/suites/init.sh b/test/suites/init.sh
index 8a41846..3887e82 100644
--- a/test/suites/init.sh
+++ b/test/suites/init.sh
@@ -12,7 +12,7 @@ test_lxd_autoinit() {
   kill_lxd "${LXD_INIT_DIR}"
 
   # lxd init --auto --storage-backend zfs
-  if [ "${LXD_BACKEND}" = "zfs" ]; then
+  if [ "${LXD_BACKEND}" = "zfs" ] && command -v zfs >/dev/null 2>&1; then
     LXD_INIT_DIR=$(mktemp -d -p "${TEST_DIR}" XXX)
     chmod +x "${LXD_INIT_DIR}"
     spawn_lxd "${LXD_INIT_DIR}" false
@@ -23,52 +23,59 @@ test_lxd_autoinit() {
   fi
 
   # lxd init --auto --storage-backend zfs --storage-pool <name>
-  if [ "${LXD_BACKEND}" = "zfs" ]; then
+  if [ "${LXD_BACKEND}" = "zfs" ] && command -v zfs >/dev/null 2>&1; then
     LXD_INIT_DIR=$(mktemp -d -p "${TEST_DIR}" XXX)
     chmod +x "${LXD_INIT_DIR}"
     spawn_lxd "${LXD_INIT_DIR}" false
 
+    # shellcheck disable=SC2154
     configure_loop_device loop_file_1 loop_device_1
+    # shellcheck disable=SC2154
     zpool create "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool" "${loop_device_1}" -m none -O compression=on
     LXD_DIR=${LXD_INIT_DIR} lxd init --auto --storage-backend zfs --storage-pool "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool"
 
     kill_lxd "${LXD_INIT_DIR}"
+    # shellcheck disable=SC2154
     deconfigure_loop_device "${loop_file_1}" "${loop_device_1}"
   fi
 
   # lxd init --auto --storage-backend zfs --storage-pool <name>/<non-existing-dataset>
-  if [ "${LXD_BACKEND}" = "zfs" ]; then
+  if [ "${LXD_BACKEND}" = "zfs" ] && command -v zfs >/dev/null 2>&1; then
     LXD_INIT_DIR=$(mktemp -d -p "${TEST_DIR}" XXX)
     chmod +x "${LXD_INIT_DIR}"
     spawn_lxd "${LXD_INIT_DIR}" false
 
     configure_loop_device loop_file_1 loop_device_1
+    # shellcheck disable=SC2154
     zpool create "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool" "${loop_device_1}" -m none -O compression=on
     LXD_DIR=${LXD_INIT_DIR} lxd init --auto --storage-backend zfs --storage-pool "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool/non-existing-dataset"
 
     kill_lxd "${LXD_INIT_DIR}"
     deconfigure_loop_device "${loop_file_1}" "${loop_device_1}"
+    # shellcheck disable=SC2154
     zpool destroy -f "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool"
   fi
 
   # lxd init --auto --storage-backend zfs --storage-pool <name>/<existing-dataset>
-  if [ "${LXD_BACKEND}" = "zfs" ]; then
+  if [ "${LXD_BACKEND}" = "zfs" ] && command -v zfs >/dev/null 2>&1; then
     LXD_INIT_DIR=$(mktemp -d -p "${TEST_DIR}" XXX)
     chmod +x "${LXD_INIT_DIR}"
     spawn_lxd "${LXD_INIT_DIR}" false
 
     configure_loop_device loop_file_1 loop_device_1
+    # shellcheck disable=SC2154
     zpool create "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool" "${loop_device_1}" -f -m none -O compression=on
     zfs create -p -o mountpoint=none "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool/existing-dataset"
     LXD_DIR=${LXD_INIT_DIR} lxd init --auto --storage-backend zfs --storage-pool "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool/existing-dataset"
 
     kill_lxd "${LXD_INIT_DIR}"
     deconfigure_loop_device "${loop_file_1}" "${loop_device_1}"
+    # shellcheck disable=SC2154
     zpool destroy -f "lxdtest-$(basename "${LXD_DIR}")-pool1-existing-pool"
   fi
 
  # lxd init --storage-backend zfs --storage-create-loop 1 --storage-pool <name> --auto
-  if [ "${LXD_BACKEND}" = "zfs" ]; then
+  if [ "${LXD_BACKEND}" = "zfs" ] && command -v zfs >/dev/null 2>&1; then
     LXD_INIT_DIR=$(mktemp -d -p "${TEST_DIR}" XXX)
     chmod +x "${LXD_INIT_DIR}"
     spawn_lxd "${LXD_INIT_DIR}" false
diff --git a/test/suites/storage.sh b/test/suites/storage.sh
index b4a6f24..bf4cec2 100644
--- a/test/suites/storage.sh
+++ b/test/suites/storage.sh
@@ -1,6 +1,8 @@
 #!/bin/sh
 
 test_storage() {
+  # shellcheck disable=2039
+
   LXD_STORAGE_DIR=$(mktemp -d -p "${TEST_DIR}" XXXXXXXXX)
   chmod +x "${LXD_STORAGE_DIR}"
   spawn_lxd "${LXD_STORAGE_DIR}" false
@@ -17,10 +19,11 @@ test_storage() {
       BACKEND=zfs
     fi
 
+    # shellcheck disable=SC1009
+    if [ "${BACKEND}" = "zfs" ] && command -v zfs >/dev/null 2>&1; then
     # Create loop file zfs pool.
-    lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool1" "${BACKEND}"
+      lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool1" "${BACKEND}"
 
-    if [ "${BACKEND}" = "zfs" ]; then
       # Let LXD use an already existing dataset.
       zfs create -p -o mountpoint=none "lxdtest-$(basename "${LXD_DIR}")-pool1/existing-dataset-as-pool"
       lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool7" "${BACKEND}" source="lxdtest-$(basename "${LXD_DIR}")-pool1/existing-dataset-as-pool"
@@ -33,96 +36,116 @@ test_storage() {
 
       # Let LXD create a new dataset and use as pool.
       lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool8" "${BACKEND}" source="lxdtest-$(basename "${LXD_DIR}")-pool1/non-existing-dataset-as-pool"
-    fi
 
-    # Create device backed zfs pool
-    configure_loop_device loop_file_1 loop_device_1
-    # shellcheck disable=SC2154
-    lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool2" "${BACKEND}" source="${loop_device_1}"
+      # Create device backed zfs pool
+      configure_loop_device loop_file_1 loop_device_1
+      # shellcheck disable=SC2154
+      lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool2" zfs source="${loop_device_1}"
+    fi
 
-    # Create loop file btrfs pool.
-    lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool3" btrfs
+    if command -v btrfs >/dev/null 2>&1; then
+      # Create loop file btrfs pool.
+      lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool3" btrfs
 
-    # Create device backed btrfs pool.
-    configure_loop_device loop_file_2 loop_device_2
-    # shellcheck disable=SC2154
-    lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool4" btrfs source="${loop_device_2}"
+      # Create device backed btrfs pool.
+      configure_loop_device loop_file_2 loop_device_2
+      # shellcheck disable=SC2154
+      lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool4" btrfs source="${loop_device_2}"
+    fi
 
     # Create dir pool.
     lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool5" dir
 
-    # Create lvm pool.
-    configure_lvm_loop_device loop_file_3 loop_device_3
-    # shellcheck disable=SC2154
-    lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool6" lvm source="${loop_device_3}"
+    if command -v lvdisplay >/dev/null 2>&1; then
+      # Create lvm pool.
+      configure_lvm_loop_device loop_file_3 loop_device_3
+      # shellcheck disable=SC2154
+      lxc storage create "lxdtest-$(basename "${LXD_DIR}")-pool6" lvm source="${loop_device_3}"
+    fi
 
     # Set default storage pool for image import.
-    lxc profile device add default root disk path="/" pool="lxdtest-$(basename "${LXD_DIR}")-pool1"
+    lxc profile device add default root disk path="/" pool="lxdtest-$(basename "${LXD_DIR}")-pool5"
 
     # Import image into default storage pool.
     ensure_import_testimage
 
     # Muck around with some containers on various pools.
-    lxc init testimage c1pool1 -s "lxdtest-$(basename "${LXD_DIR}")-pool1"
-    lxc list -c b c1pool1 | grep "lxdtest-$(basename "${LXD_DIR}")-pool1"
-    lxc init testimage c2pool2 -s "lxdtest-$(basename "${LXD_DIR}")-pool2"
-    lxc list -c b c2pool2 | grep "lxdtest-$(basename "${LXD_DIR}")-pool2"
-
-    lxc launch testimage c3pool1 -s "lxdtest-$(basename "${LXD_DIR}")-pool1"
-    lxc list -c b c3pool1 | grep "lxdtest-$(basename "${LXD_DIR}")-pool1"
-    lxc launch testimage c4pool2 -s "lxdtest-$(basename "${LXD_DIR}")-pool2"
-    lxc list -c b c4pool2 | grep "lxdtest-$(basename "${LXD_DIR}")-pool2"
-
-    lxc init testimage c5pool3 -s "lxdtest-$(basename "${LXD_DIR}")-pool3"
-    lxc list -c b c5pool3 | grep "lxdtest-$(basename "${LXD_DIR}")-pool3"
-    lxc init testimage c6pool4 -s "lxdtest-$(basename "${LXD_DIR}")-pool4"
-    lxc list -c b c6pool4 | grep "lxdtest-$(basename "${LXD_DIR}")-pool4"
-
-    lxc launch testimage c7pool3 -s "lxdtest-$(basename "${LXD_DIR}")-pool3"
-    lxc list -c b c7pool3 | grep "lxdtest-$(basename "${LXD_DIR}")-pool3"
-    lxc launch testimage c8pool4 -s "lxdtest-$(basename "${LXD_DIR}")-pool4"
-    lxc list -c b c8pool4 | grep "lxdtest-$(basename "${LXD_DIR}")-pool4"
+    if [ "${BACKEND}" = "zfs" ] && command -v zfs >/dev/null 2>&1; then
+      lxc init testimage c1pool1 -s "lxdtest-$(basename "${LXD_DIR}")-pool1"
+      lxc list -c b c1pool1 | grep "lxdtest-$(basename "${LXD_DIR}")-pool1"
+
+      lxc init testimage c2pool2 -s "lxdtest-$(basename "${LXD_DIR}")-pool2"
+      lxc list -c b c2pool2 | grep "lxdtest-$(basename "${LXD_DIR}")-pool2"
+
+      lxc launch testimage c3pool1 -s "lxdtest-$(basename "${LXD_DIR}")-pool1"
+      lxc list -c b c3pool1 | grep "lxdtest-$(basename "${LXD_DIR}")-pool1"
+
+      lxc launch testimage c4pool2 -s "lxdtest-$(basename "${LXD_DIR}")-pool2"
+      lxc list -c b c4pool2 | grep "lxdtest-$(basename "${LXD_DIR}")-pool2"
+    fi
+
+    if command -v btrfs >/dev/null 2>&1; then
+      lxc init testimage c5pool3 -s "lxdtest-$(basename "${LXD_DIR}")-pool3"
+      lxc list -c b c5pool3 | grep "lxdtest-$(basename "${LXD_DIR}")-pool3"
+      lxc init testimage c6pool4 -s "lxdtest-$(basename "${LXD_DIR}")-pool4"
+      lxc list -c b c6pool4 | grep "lxdtest-$(basename "${LXD_DIR}")-pool4"
+
+      lxc launch testimage c7pool3 -s "lxdtest-$(basename "${LXD_DIR}")-pool3"
+      lxc list -c b c7pool3 | grep "lxdtest-$(basename "${LXD_DIR}")-pool3"
+      lxc launch testimage c8pool4 -s "lxdtest-$(basename "${LXD_DIR}")-pool4"
+      lxc list -c b c8pool4 | grep "lxdtest-$(basename "${LXD_DIR}")-pool4"
+    fi
 
     lxc init testimage c9pool5 -s "lxdtest-$(basename "${LXD_DIR}")-pool5"
     lxc list -c b c9pool5 | grep "lxdtest-$(basename "${LXD_DIR}")-pool5"
-    lxc init testimage c10pool6 -s "lxdtest-$(basename "${LXD_DIR}")-pool6"
-    lxc list -c b c10pool6 | grep "lxdtest-$(basename "${LXD_DIR}")-pool6"
 
     lxc launch testimage c11pool5 -s "lxdtest-$(basename "${LXD_DIR}")-pool5"
     lxc list -c b c11pool5 | grep "lxdtest-$(basename "${LXD_DIR}")-pool5"
-    lxc launch testimage c12pool6 -s "lxdtest-$(basename "${LXD_DIR}")-pool6"
-    lxc list -c b c12pool6 | grep "lxdtest-$(basename "${LXD_DIR}")-pool6"
 
-    if [ "${BACKEND}" = "zfs" ]; then
-	lxc launch testimage c13pool7 -s "lxdtest-$(basename "${LXD_DIR}")-pool7"
-	lxc launch testimage c14pool7 -s "lxdtest-$(basename "${LXD_DIR}")-pool7"
+    if command -v lvdisplay >/dev/null 2>&1; then
+      lxc init testimage c10pool6 -s "lxdtest-$(basename "${LXD_DIR}")-pool6"
+      lxc list -c b c10pool6 | grep "lxdtest-$(basename "${LXD_DIR}")-pool6"
 
-	lxc launch testimage c15pool8 -s "lxdtest-$(basename "${LXD_DIR}")-pool8"
-	lxc launch testimage c16pool8 -s "lxdtest-$(basename "${LXD_DIR}")-pool8"
+      lxc launch testimage c12pool6 -s "lxdtest-$(basename "${LXD_DIR}")-pool6"
+      lxc list -c b c12pool6 | grep "lxdtest-$(basename "${LXD_DIR}")-pool6"
+    fi
+
+    if [ "${BACKEND}" = "zfs" ] && command -v zfs >/dev/null 2>&1; then
+      lxc launch testimage c13pool7 -s "lxdtest-$(basename "${LXD_DIR}")-pool7"
+      lxc launch testimage c14pool7 -s "lxdtest-$(basename "${LXD_DIR}")-pool7"
 
-	lxc launch testimage c17pool9 -s "lxdtest-$(basename "${LXD_DIR}")-pool9"
-	lxc launch testimage c18pool9 -s "lxdtest-$(basename "${LXD_DIR}")-pool9"
+      lxc launch testimage c15pool8 -s "lxdtest-$(basename "${LXD_DIR}")-pool8"
+      lxc launch testimage c16pool8 -s "lxdtest-$(basename "${LXD_DIR}")-pool8"
+
+      lxc launch testimage c17pool9 -s "lxdtest-$(basename "${LXD_DIR}")-pool9"
+      lxc launch testimage c18pool9 -s "lxdtest-$(basename "${LXD_DIR}")-pool9"
     fi
 
-    lxc delete -f c1pool1
-    lxc delete -f c2pool2
+    if [ "${BACKEND}" = "zfs" ] && command -v zfs >/dev/null 2>&1; then
+      lxc delete -f c1pool1
+      lxc delete -f c3pool1
 
-    lxc delete -f c3pool1
-    lxc delete -f c4pool2
+      lxc delete -f c4pool2
+      lxc delete -f c2pool2
+    fi
 
-    lxc delete -f c5pool3
-    lxc delete -f c6pool4
+    if command -v btrfs >/dev/null 2>&1; then
+      lxc delete -f c5pool3
+      lxc delete -f c7pool3
 
-    lxc delete -f c7pool3
-    lxc delete -f c8pool4
+      lxc delete -f c8pool4
+      lxc delete -f c6pool4
+    fi
 
     lxc delete -f c9pool5
-    lxc delete -f c10pool6
-
     lxc delete -f c11pool5
-    lxc delete -f c12pool6
 
-    if [ "${BACKEND}" = "zfs" ]; then
+    if command -v lvdisplay >/dev/null 2>&1; then
+      lxc delete -f c10pool6
+      lxc delete -f c12pool6
+    fi
+
+    if [ "${BACKEND}" = "zfs" ] && command -v zfs >/dev/null 2>&1; then
       lxc delete -f c13pool7
       lxc delete -f c14pool7
 
@@ -135,25 +158,29 @@ test_storage() {
 
     lxc image delete testimage
 
-    if [ "${BACKEND}" = "zfs" ]; then
+    if [ "${BACKEND}" = "zfs" ] && command -v zfs >/dev/null 2>&1; then
       lxc storage delete "lxdtest-$(basename "${LXD_DIR}")-pool7"
       lxc storage delete "lxdtest-$(basename "${LXD_DIR}")-pool8"
       lxc storage delete "lxdtest-$(basename "${LXD_DIR}")-pool9"
       # shellcheck disable=SC2154
       deconfigure_loop_device "${loop_file_4}" "${loop_device_4}"
-    fi
 
-    lxc storage delete "lxdtest-$(basename "${LXD_DIR}")-pool2"
-    # shellcheck disable=SC2154
-    deconfigure_loop_device "${loop_file_1}" "${loop_device_1}"
+      lxc storage delete "lxdtest-$(basename "${LXD_DIR}")-pool2"
+      # shellcheck disable=SC2154
+      deconfigure_loop_device "${loop_file_1}" "${loop_device_1}"
+    fi
 
-    lxc storage delete "lxdtest-$(basename "${LXD_DIR}")-pool4"
-    # shellcheck disable=SC2154
-    deconfigure_loop_device "${loop_file_2}" "${loop_device_2}"
+    if command -v btrfs >/dev/null 2>&1; then
+      lxc storage delete "lxdtest-$(basename "${LXD_DIR}")-pool4"
+      # shellcheck disable=SC2154
+      deconfigure_loop_device "${loop_file_2}" "${loop_device_2}"
+    fi
 
-    lxc storage delete "lxdtest-$(basename "${LXD_DIR}")-pool6"
-    # shellcheck disable=SC2154
-    deconfigure_lvm_loop_device "${loop_file_3}" "${loop_device_3}"
+    if command -v lvdisplay >/dev/null 2>&1; then
+      lxc storage delete "lxdtest-$(basename "${LXD_DIR}")-pool6"
+      # shellcheck disable=SC2154
+      deconfigure_lvm_loop_device "${loop_file_3}" "${loop_device_3}"
+    fi
   )
 
   # shellcheck disable=SC2031


More information about the lxc-devel mailing list