blob: 324c3234da7ddcfd186387ad1550d06a93a7b510 [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
115 for lv in $(sudo lvs --noheadings -o lv_name $vg 2>/dev/null); do
116 # lv_prefix prefixes the LVs we want
117 if [[ "${lv#$lv_prefix}" != "$lv" ]]; then
118 sudo lvremove -f $vg/$lv
119 fi
120 done
121}
122
123# _clean_lvm_backing_file() removes the backing file of the
124# volume group used by cinder
125#
126# Usage: _clean_lvm_backing_file() volume-group-name backing-file-name
127function _clean_lvm_backing_file {
128 local vg=$1
129 local backing_file=$2
130
131 # if there is no logical volume left, it's safe to attempt a cleanup
132 # of the backing file
133 if [[ -z "$(sudo lvs --noheadings -o lv_name $vg 2>/dev/null)" ]]; then
134 # if the backing physical device is a loop device, it was probably setup by devstack
135 VG_DEV=$(sudo losetup -j $backing_file | awk -F':' '/backing-file/ { print $1}')
136 if [[ -n "$VG_DEV" ]] && [[ -e "$VG_DEV" ]]; then
137 sudo losetup -d $VG_DEV
138 rm -f $backing_file
139 fi
140 fi
141}
142
143# _create_cinder_volume_group volume-group-name backing-file-name
144function _create_cinder_volume_group {
145 # According to the ``CINDER_MULTI_LVM_BACKEND`` value, configure one or two default volumes
146 # group called ``stack-volumes`` (and ``stack-volumes2``) for the volume
147 # service if it (they) does (do) not yet exist. If you don't wish to use a
148 # file backed volume group, create your own volume group called ``stack-volumes``
149 # and ``stack-volumes2`` before invoking ``stack.sh``.
150 #
151 # The two backing files are ``VOLUME_BACKING_FILE_SIZE`` in size, and they are stored in
152 # the ``DATA_DIR``.
153
154 local vg_name=$1
155 local backing_file=$2
156
157 if ! sudo vgs $vg_name; then
158 # TODO: fix device handling
159 if [ -z "$VOLUME_BACKING_DEVICE" ]; then
160 # Only create if the file doesn't already exists
161 [[ -f $backing_file ]] || truncate -s $VOLUME_BACKING_FILE_SIZE $backing_file
162 DEV=`sudo losetup -f --show $backing_file`
163
164 # Only create if the loopback device doesn't contain $VOLUME_GROUP
165 if ! sudo vgs $vg_name; then
166 sudo vgcreate $vg_name $DEV
167 fi
168 else
169 sudo vgcreate $vg_name $VOLUME_BACKING_DEVICE
170 fi
171 fi
172}
173
174
175# Restore xtrace
176$MY_XTRACE
177
178# mode: shell-script
179# End: