blob: 280baf7b3e3fb86f99311d828ee0a4acd5319804 [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
Sean Dague53753292014-12-04 19:38:15 -050036VOLUME_GROUP=${VOLUME_GROUP:-}
Dean Troyer09718332014-07-03 10:46:57 -050037VOLUME_GROUP_NAME=${VOLUME_GROUP:-${VOLUME_GROUP_NAME:-stack-volumes}}
38
39# TODO: resurrect backing device...need to know how to set values
40#VOLUME_BACKING_DEVICE=${VOLUME_BACKING_DEVICE:-}
41
42VOLUME_NAME_PREFIX=${VOLUME_NAME_PREFIX:-volume-}
43
44
45# Entry Points
46# ------------
47
48# Compatibility for getting a volume group name from either ``VOLUME_GROUP``
49# or from ``VOLUME_GROUP_NAME`` plus the backend name
50function get_volume_group_name {
51 local be_name=$1
52
53 # Again with the icehouse-generation compatibility
54 local volume_group_name=$VOLUME_GROUP_NAME
55 if [[ -z $VOLUME_GROUP ]]; then
56 volume_group_name+="-$be_name"
57 fi
58 echo $volume_group_name
59}
60
61function cleanup_cinder_backend_lvm {
62 local be_name=$1
63
64 # Again with the icehouse-generation compatibility
65 local volume_group_name=$(get_volume_group_name $be_name)
66
67 # Campsite rule: leave behind a volume group at least as clean as we found it
68 _clean_lvm_lv ${volume_group_name} $VOLUME_NAME_PREFIX
69 _clean_lvm_backing_file ${volume_group_name} $DATA_DIR/${volume_group_name}-backing-file
70}
71
72# configure_cinder_backend_lvm - Set config files, create data dirs, etc
73# configure_cinder_backend_lvm $name
74function configure_cinder_backend_lvm {
75 local be_name=$1
76
77 # Again with the icehouse-generation compatibility
78 local volume_group_name=$(get_volume_group_name $be_name)
79
80 iniset $CINDER_CONF $be_name volume_backend_name $be_name
81 iniset $CINDER_CONF $be_name volume_driver "cinder.volume.drivers.lvm.LVMISCSIDriver"
82 iniset $CINDER_CONF $be_name volume_group $volume_group_name
83
84 if [[ "$CINDER_SECURE_DELETE" == "False" ]]; then
85 iniset $CINDER_CONF $be_name volume_clear none
86 fi
87}
88
89
90function init_cinder_backend_lvm {
91 local be_name=$1
92
93 # Again with the icehouse-generation compatibility
94 local volume_group_name=$(get_volume_group_name $be_name)
95
96 # Start with a clean volume group
97 _create_cinder_volume_group ${volume_group_name} $DATA_DIR/${volume_group_name}-backing-file
98
99 if is_fedora || is_suse; then
100 # service is not started by default
101 start_service tgtd
102 fi
103
104 # Remove iscsi targets
105 sudo tgtadm --op show --mode target | grep $VOLUME_NAME_PREFIX | grep Target | cut -f3 -d ' ' | sudo xargs -n1 tgt-admin --delete || true
106 _clean_lvm_lv ${volume_group_name} $VOLUME_NAME_PREFIX
107}
108
109
110# _clean_lvm_lv removes all cinder LVM volumes
111#
112# Usage: _clean_lvm_lv volume-group-name $VOLUME_NAME_PREFIX
113function _clean_lvm_lv {
114 local vg=$1
115 local lv_prefix=$2
116
117 # Clean out existing volumes
Dean Troyere8a35ac2014-07-25 12:37:41 -0500118 local lv
Dean Troyer09718332014-07-03 10:46:57 -0500119 for lv in $(sudo lvs --noheadings -o lv_name $vg 2>/dev/null); do
120 # lv_prefix prefixes the LVs we want
121 if [[ "${lv#$lv_prefix}" != "$lv" ]]; then
122 sudo lvremove -f $vg/$lv
123 fi
124 done
125}
126
127# _clean_lvm_backing_file() removes the backing file of the
128# volume group used by cinder
129#
130# Usage: _clean_lvm_backing_file() volume-group-name backing-file-name
131function _clean_lvm_backing_file {
132 local vg=$1
133 local backing_file=$2
134
135 # if there is no logical volume left, it's safe to attempt a cleanup
136 # of the backing file
137 if [[ -z "$(sudo lvs --noheadings -o lv_name $vg 2>/dev/null)" ]]; then
138 # if the backing physical device is a loop device, it was probably setup by devstack
Dean Troyere8a35ac2014-07-25 12:37:41 -0500139 local vg_dev=$(sudo losetup -j $backing_file | awk -F':' '/backing-file/ { print $1}')
140 if [[ -n "$vg_dev" ]] && [[ -e "$vg_dev" ]]; then
141 sudo losetup -d $vg_dev
Dean Troyer09718332014-07-03 10:46:57 -0500142 rm -f $backing_file
143 fi
144 fi
145}
146
147# _create_cinder_volume_group volume-group-name backing-file-name
148function _create_cinder_volume_group {
149 # According to the ``CINDER_MULTI_LVM_BACKEND`` value, configure one or two default volumes
150 # group called ``stack-volumes`` (and ``stack-volumes2``) for the volume
151 # service if it (they) does (do) not yet exist. If you don't wish to use a
152 # file backed volume group, create your own volume group called ``stack-volumes``
153 # and ``stack-volumes2`` before invoking ``stack.sh``.
154 #
155 # The two backing files are ``VOLUME_BACKING_FILE_SIZE`` in size, and they are stored in
156 # the ``DATA_DIR``.
157
158 local vg_name=$1
159 local backing_file=$2
160
161 if ! sudo vgs $vg_name; then
162 # TODO: fix device handling
163 if [ -z "$VOLUME_BACKING_DEVICE" ]; then
164 # Only create if the file doesn't already exists
165 [[ -f $backing_file ]] || truncate -s $VOLUME_BACKING_FILE_SIZE $backing_file
Dean Troyere8a35ac2014-07-25 12:37:41 -0500166 local vg_dev=`sudo losetup -f --show $backing_file`
Dean Troyer09718332014-07-03 10:46:57 -0500167
168 # Only create if the loopback device doesn't contain $VOLUME_GROUP
169 if ! sudo vgs $vg_name; then
Dean Troyere8a35ac2014-07-25 12:37:41 -0500170 sudo vgcreate $vg_name $vg_dev
Dean Troyer09718332014-07-03 10:46:57 -0500171 fi
172 else
173 sudo vgcreate $vg_name $VOLUME_BACKING_DEVICE
174 fi
175 fi
176}
177
178
179# Restore xtrace
180$MY_XTRACE
181
182# mode: shell-script
183# End: