blob: a3ab5bf3f9868bd8fe24309190487e22c1c744c4 [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Dean Troyer09718332014-07-03 10:46:57 -05003# lib/cinder_backends/lvm
4# Configure the LVM backend
5
6# Enable with:
7#
8# CINDER_ENABLED_BACKENDS+=,lvm:lvmname
9
10# Dependencies:
11#
12# - ``functions`` file
13# - ``cinder`` configurations
14
15# CINDER_CONF
16# DATA_DIR
17
18# clean_cinder_backend_lvm - called from clean_cinder()
19# configure_cinder_backend_lvm - called from configure_cinder()
20# init_cinder_backend_lvm - called from init_cinder()
21
22
23# Save trace setting
24MY_XTRACE=$(set +o | grep xtrace)
25set +o xtrace
26
27
28# Defaults
29# --------
30
31# Name of the lvm volume groups to use/create for iscsi volumes
32# This monkey-motion is for compatibility with icehouse-generation Grenade
33# If ``VOLUME_GROUP`` is set, use it, otherwise we'll build a VG name based
34# on ``VOLUME_GROUP_NAME`` that includes the backend name
35# Grenade doesn't use ``VOLUME_GROUP2`` so it is left out
36VOLUME_GROUP_NAME=${VOLUME_GROUP:-${VOLUME_GROUP_NAME:-stack-volumes}}
37
38# TODO: resurrect backing device...need to know how to set values
39#VOLUME_BACKING_DEVICE=${VOLUME_BACKING_DEVICE:-}
40
41VOLUME_NAME_PREFIX=${VOLUME_NAME_PREFIX:-volume-}
42
43
44# Entry Points
45# ------------
46
47# Compatibility for getting a volume group name from either ``VOLUME_GROUP``
48# or from ``VOLUME_GROUP_NAME`` plus the backend name
49function get_volume_group_name {
50 local be_name=$1
51
52 # Again with the icehouse-generation compatibility
53 local volume_group_name=$VOLUME_GROUP_NAME
54 if [[ -z $VOLUME_GROUP ]]; then
55 volume_group_name+="-$be_name"
56 fi
57 echo $volume_group_name
58}
59
60function cleanup_cinder_backend_lvm {
61 local be_name=$1
62
63 # Again with the icehouse-generation compatibility
64 local volume_group_name=$(get_volume_group_name $be_name)
65
66 # Campsite rule: leave behind a volume group at least as clean as we found it
67 _clean_lvm_lv ${volume_group_name} $VOLUME_NAME_PREFIX
68 _clean_lvm_backing_file ${volume_group_name} $DATA_DIR/${volume_group_name}-backing-file
69}
70
71# configure_cinder_backend_lvm - Set config files, create data dirs, etc
72# configure_cinder_backend_lvm $name
73function configure_cinder_backend_lvm {
74 local be_name=$1
75
76 # Again with the icehouse-generation compatibility
77 local volume_group_name=$(get_volume_group_name $be_name)
78
79 iniset $CINDER_CONF $be_name volume_backend_name $be_name
80 iniset $CINDER_CONF $be_name volume_driver "cinder.volume.drivers.lvm.LVMISCSIDriver"
81 iniset $CINDER_CONF $be_name volume_group $volume_group_name
82
83 if [[ "$CINDER_SECURE_DELETE" == "False" ]]; then
84 iniset $CINDER_CONF $be_name volume_clear none
85 fi
86}
87
88
89function init_cinder_backend_lvm {
90 local be_name=$1
91
92 # Again with the icehouse-generation compatibility
93 local volume_group_name=$(get_volume_group_name $be_name)
94
95 # Start with a clean volume group
96 _create_cinder_volume_group ${volume_group_name} $DATA_DIR/${volume_group_name}-backing-file
97
98 if is_fedora || is_suse; then
99 # service is not started by default
100 start_service tgtd
101 fi
102
103 # Remove iscsi targets
104 sudo tgtadm --op show --mode target | grep $VOLUME_NAME_PREFIX | grep Target | cut -f3 -d ' ' | sudo xargs -n1 tgt-admin --delete || true
105 _clean_lvm_lv ${volume_group_name} $VOLUME_NAME_PREFIX
106}
107
108
109# _clean_lvm_lv removes all cinder LVM volumes
110#
111# Usage: _clean_lvm_lv volume-group-name $VOLUME_NAME_PREFIX
112function _clean_lvm_lv {
113 local vg=$1
114 local lv_prefix=$2
115
116 # Clean out existing volumes
Dean Troyere8a35ac2014-07-25 12:37:41 -0500117 local lv
Dean Troyer09718332014-07-03 10:46:57 -0500118 for lv in $(sudo lvs --noheadings -o lv_name $vg 2>/dev/null); do
119 # lv_prefix prefixes the LVs we want
120 if [[ "${lv#$lv_prefix}" != "$lv" ]]; then
121 sudo lvremove -f $vg/$lv
122 fi
123 done
124}
125
126# _clean_lvm_backing_file() removes the backing file of the
127# volume group used by cinder
128#
129# Usage: _clean_lvm_backing_file() volume-group-name backing-file-name
130function _clean_lvm_backing_file {
131 local vg=$1
132 local backing_file=$2
133
134 # if there is no logical volume left, it's safe to attempt a cleanup
135 # of the backing file
136 if [[ -z "$(sudo lvs --noheadings -o lv_name $vg 2>/dev/null)" ]]; then
137 # if the backing physical device is a loop device, it was probably setup by devstack
Dean Troyere8a35ac2014-07-25 12:37:41 -0500138 local vg_dev=$(sudo losetup -j $backing_file | awk -F':' '/backing-file/ { print $1}')
139 if [[ -n "$vg_dev" ]] && [[ -e "$vg_dev" ]]; then
140 sudo losetup -d $vg_dev
Dean Troyer09718332014-07-03 10:46:57 -0500141 rm -f $backing_file
142 fi
143 fi
144}
145
146# _create_cinder_volume_group volume-group-name backing-file-name
147function _create_cinder_volume_group {
148 # According to the ``CINDER_MULTI_LVM_BACKEND`` value, configure one or two default volumes
149 # group called ``stack-volumes`` (and ``stack-volumes2``) for the volume
150 # service if it (they) does (do) not yet exist. If you don't wish to use a
151 # file backed volume group, create your own volume group called ``stack-volumes``
152 # and ``stack-volumes2`` before invoking ``stack.sh``.
153 #
154 # The two backing files are ``VOLUME_BACKING_FILE_SIZE`` in size, and they are stored in
155 # the ``DATA_DIR``.
156
157 local vg_name=$1
158 local backing_file=$2
159
160 if ! sudo vgs $vg_name; then
161 # TODO: fix device handling
162 if [ -z "$VOLUME_BACKING_DEVICE" ]; then
163 # Only create if the file doesn't already exists
164 [[ -f $backing_file ]] || truncate -s $VOLUME_BACKING_FILE_SIZE $backing_file
Dean Troyere8a35ac2014-07-25 12:37:41 -0500165 local vg_dev=`sudo losetup -f --show $backing_file`
Dean Troyer09718332014-07-03 10:46:57 -0500166
167 # Only create if the loopback device doesn't contain $VOLUME_GROUP
168 if ! sudo vgs $vg_name; then
Dean Troyere8a35ac2014-07-25 12:37:41 -0500169 sudo vgcreate $vg_name $vg_dev
Dean Troyer09718332014-07-03 10:46:57 -0500170 fi
171 else
172 sudo vgcreate $vg_name $VOLUME_BACKING_DEVICE
173 fi
174 fi
175}
176
177
178# Restore xtrace
179$MY_XTRACE
180
181# mode: shell-script
182# End: