From c070a3dbb5c52fb9099bd6cec477ba3d0a75760a Mon Sep 17 00:00:00 2001
From: Maru Newby <marun@redhat.com>
Date: Tue, 27 Jan 2015 17:44:44 +0000
Subject: [PATCH] Init default lvm volume group only if required

A recent patch [1] added support for lvm ephemeral storage for nova,
but at the cost of initializing a default lvm volume group even if it
was not required.  This change ensures that init of the default volume
group is only performed when nova and/or cinder are configured to use
lvm.

1: https://review.openstack.org/#/c/132333

Change-Id: I7634ca0ed0ffe1b13464e4d66744918f85149f2e
Closes-Bug: #1414820
---
 lib/cinder |  4 ++++
 lib/lvm    | 19 +++++++++++++++++++
 lib/nova   |  4 ++++
 stack.sh   |  4 ----
 4 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/lib/cinder b/lib/cinder
index 6eee880fef..03e2e28727 100644
--- a/lib/cinder
+++ b/lib/cinder
@@ -399,6 +399,10 @@ function init_cinder {
             be_type=${be%%:*}
             be_name=${be##*:}
             if type init_cinder_backend_${be_type} >/dev/null 2>&1; then
+                # Always init the default volume group for lvm.
+                if [[ "$be_type" == "lvm" ]]; then
+                    init_default_lvm_volume_group
+                fi
                 init_cinder_backend_${be_type} ${be_name}
             fi
         done
diff --git a/lib/lvm b/lib/lvm
index 4ef260d593..ed24487c5f 100644
--- a/lib/lvm
+++ b/lib/lvm
@@ -117,6 +117,25 @@ function init_lvm_volume_group {
     _clean_lvm_volume_group $vg
 }
 
+# Sentinal value to ensure that init of default lvm volume group is
+# only performed once across calls of init_default_lvm_volume_group.
+_DEFAULT_LVM_INIT=${_DEFAULT_LVM_INIT:-0}
+
+# init_default_lvm_volume_group() initializes a default volume group
+# intended to be shared between cinder and nova.  It is idempotent;
+# the init of the default volume group is guaranteed to be performed
+# only once so that either or both of the dependent services can
+# safely call this function.
+#
+# Usage: init_default_lvm_volume_group()
+function init_default_lvm_volume_group {
+    if [[ "$_DEFAULT_LVM_INIT" = "0" ]]; then
+        init_lvm_volume_group $DEFAULT_VOLUME_GROUP_NAME $VOLUME_BACKING_FILE_SIZE
+        _DEFAULT_LVM_INIT=1
+    fi
+}
+
+
 # Restore xtrace
 $MY_XTRACE
 
diff --git a/lib/nova b/lib/nova
index 76212edc96..a4b1bb15dc 100644
--- a/lib/nova
+++ b/lib/nova
@@ -640,6 +640,10 @@ function init_nova {
 
     create_nova_cache_dir
     create_nova_keys_dir
+
+    if [[ "$NOVA_BACKEND" == "LVM" ]]; then
+        init_default_lvm_volume_group
+    fi
 }
 
 # install_novaclient() - Collect source and prepare
diff --git a/stack.sh b/stack.sh
index b03cca8c51..db36367c07 100755
--- a/stack.sh
+++ b/stack.sh
@@ -939,10 +939,6 @@ init_service_check
 # A better kind of sysstat, with the top process per time slice
 start_dstat
 
-# Initialize default LVM volume group
-# -----------------------------------
-init_lvm_volume_group $DEFAULT_VOLUME_GROUP_NAME $VOLUME_BACKING_FILE_SIZE
-
 # Start Services
 # ==============