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