blob: 92265f2af222572967327c00f8a511453ff1205d [file] [log] [blame]
Dean Troyerdc97cb72015-03-28 08:20:50 -05001#!/bin/bash
2#
Daniel Genind4708672014-10-31 15:01:29 -04003# lib/lvm
4# Configure the default LVM volume group used by Cinder and Nova
5
6# Dependencies:
7#
8# - ``functions`` file
9# - ``cinder`` configurations
10
11# DATA_DIR
12
13# clean_default_volume_group - called from clean()
14# configure_default_volume_group - called from configure()
15# init_default_volume_group - called from init()
16
17
18# Save trace setting
Ian Wienand523f4882015-10-13 11:03:03 +110019_XTRACE_LVM=$(set +o | grep xtrace)
Daniel Genind4708672014-10-31 15:01:29 -040020set +o xtrace
21
22
23# Defaults
24# --------
25# Name of the lvm volume groups to use/create for iscsi volumes
Jordan Pittierf5069f32016-11-08 12:10:12 +010026VOLUME_GROUP_NAME=${VOLUME_GROUP_NAME:-stack-volumes}
Daniel Genind4708672014-10-31 15:01:29 -040027DEFAULT_VOLUME_GROUP_NAME=$VOLUME_GROUP_NAME-default
28
29# Backing file name is of the form $VOLUME_GROUP$BACKING_FILE_SUFFIX
30BACKING_FILE_SUFFIX=-backing-file
31
32
Dean Troyerdc97cb72015-03-28 08:20:50 -050033# Functions
34# ---------
Daniel Genind4708672014-10-31 15:01:29 -040035
36# _clean_lvm_volume_group removes all default LVM volumes
37#
Bogdan Katynski8175df12017-06-28 12:34:52 +010038# Usage: _clean_lvm_volume_group $vg
Daniel Genind4708672014-10-31 15:01:29 -040039function _clean_lvm_volume_group {
40 local vg=$1
41
42 # Clean out existing volumes
43 sudo lvremove -f $vg
44}
45
Bogdan Katynski8175df12017-06-28 12:34:52 +010046# _remove_lvm_volume_group removes the volume group
47#
48# Usage: _remove_lvm_volume_group $vg
49function _remove_lvm_volume_group {
50 local vg=$1
51
52 # Remove the volume group
53 sudo vgremove -f $vg
54}
55
Daniel Genind4708672014-10-31 15:01:29 -040056# _clean_lvm_backing_file() removes the backing file of the
57# volume group
58#
59# Usage: _clean_lvm_backing_file() $backing_file
60function _clean_lvm_backing_file {
61 local backing_file=$1
62
Dean Troyerdc97cb72015-03-28 08:20:50 -050063 # If the backing physical device is a loop device, it was probably setup by DevStack
Daniel Genind4708672014-10-31 15:01:29 -040064 if [[ -n "$backing_file" ]] && [[ -e "$backing_file" ]]; then
Ian Wienandada886d2015-10-07 14:06:26 +110065 local vg_dev
66 vg_dev=$(sudo losetup -j $backing_file | awk -F':' '/'$BACKING_FILE_SUFFIX'/ { print $1}')
Eric Harney5237d162016-08-30 10:59:52 -040067 if [[ -n "$vg_dev" ]]; then
68 sudo losetup -d $vg_dev
69 fi
Daniel Genind4708672014-10-31 15:01:29 -040070 rm -f $backing_file
71 fi
72}
73
74# clean_lvm_volume_group() cleans up the volume group and removes the
75# backing file
76#
77# Usage: clean_lvm_volume_group $vg
78function clean_lvm_volume_group {
79 local vg=$1
80
81 _clean_lvm_volume_group $vg
Bogdan Katynski8175df12017-06-28 12:34:52 +010082 _remove_lvm_volume_group $vg
Daniel Genind4708672014-10-31 15:01:29 -040083 # if there is no logical volume left, it's safe to attempt a cleanup
84 # of the backing file
85 if [[ -z "$(sudo lvs --noheadings -o lv_name $vg 2>/dev/null)" ]]; then
86 _clean_lvm_backing_file $DATA_DIR/$vg$BACKING_FILE_SUFFIX
87 fi
88}
89
90
Shin Sato9ee1ef62015-05-28 13:56:58 +090091# _create_lvm_volume_group creates default volume group
Daniel Genind4708672014-10-31 15:01:29 -040092#
93# Usage: _create_lvm_volume_group() $vg $size
94function _create_lvm_volume_group {
95 local vg=$1
96 local size=$2
97
98 local backing_file=$DATA_DIR/$vg$BACKING_FILE_SUFFIX
99 if ! sudo vgs $vg; then
100 # Only create if the file doesn't already exists
Chris Dent4c206072015-02-16 21:56:29 +0000101 [[ -f $backing_file ]] || truncate -s $size $backing_file
Dan Smith93a94f52018-12-14 06:39:59 -0800102
103 local directio=""
104 # Check to see if we can do direct-io
105 if losetup -h | grep -q direct-io; then
106 directio="--direct-io=on"
107 fi
108
Ian Wienandada886d2015-10-07 14:06:26 +1100109 local vg_dev
Dan Smith93a94f52018-12-14 06:39:59 -0800110 vg_dev=$(sudo losetup -f --show $directio $backing_file)
Daniel Genind4708672014-10-31 15:01:29 -0400111
112 # Only create volume group if it doesn't already exist
113 if ! sudo vgs $vg; then
114 sudo vgcreate $vg $vg_dev
115 fi
116 fi
117}
118
119# init_lvm_volume_group() initializes the volume group creating the backing
120# file if necessary
121#
jeckxie8a834942016-12-05 15:32:25 +0800122# Usage: init_lvm_volume_group() $vg $size
Daniel Genind4708672014-10-31 15:01:29 -0400123function init_lvm_volume_group {
124 local vg=$1
125 local size=$2
Daniel Genind4708672014-10-31 15:01:29 -0400126
Lee Yarwoode16e9252020-04-09 11:00:28 +0100127 # Start the lvmetad on f30 (dropped from f31) or SUSE
128 if [[ $DISTRO =~ f30 ]] || is_suse; then
Attila Fazekas380d92c2015-02-18 16:22:06 +0100129 # services is not started by default
130 start_service lvm2-lvmetad
Lee Yarwoode16e9252020-04-09 11:00:28 +0100131 fi
132
133 # Start the tgtd service on Fedora and SUSE if tgtadm is used
134 if is_fedora || is_suse && [[ "$CINDER_ISCSI_HELPER" = "tgtadm" ]]; then
135 start_service tgtd
Daniel Genind4708672014-10-31 15:01:29 -0400136 fi
137
Attila Fazekas380d92c2015-02-18 16:22:06 +0100138 # Start with a clean volume group
139 _create_lvm_volume_group $vg $size
140
Daniel Genind4708672014-10-31 15:01:29 -0400141 # Remove iscsi targets
Attila Fazekasc70605d2015-01-26 15:44:47 +0100142 if [ "$CINDER_ISCSI_HELPER" = "lioadm" ]; then
143 sudo cinder-rtstool get-targets | sudo xargs -rn 1 cinder-rtstool delete
144 else
Waldemar Znoinski084efc72016-03-09 12:18:51 +0000145 sudo tgtadm --op show --mode target | awk '/Target/ {print $3}' | sudo xargs -r -n1 tgt-admin --delete
Attila Fazekasc70605d2015-01-26 15:44:47 +0100146 fi
Daniel Genind4708672014-10-31 15:01:29 -0400147 _clean_lvm_volume_group $vg
148}
149
Maru Newbyc070a3d2015-01-27 17:44:44 +0000150# Sentinal value to ensure that init of default lvm volume group is
151# only performed once across calls of init_default_lvm_volume_group.
152_DEFAULT_LVM_INIT=${_DEFAULT_LVM_INIT:-0}
153
154# init_default_lvm_volume_group() initializes a default volume group
155# intended to be shared between cinder and nova. It is idempotent;
156# the init of the default volume group is guaranteed to be performed
157# only once so that either or both of the dependent services can
158# safely call this function.
159#
160# Usage: init_default_lvm_volume_group()
161function init_default_lvm_volume_group {
162 if [[ "$_DEFAULT_LVM_INIT" = "0" ]]; then
163 init_lvm_volume_group $DEFAULT_VOLUME_GROUP_NAME $VOLUME_BACKING_FILE_SIZE
164 _DEFAULT_LVM_INIT=1
165 fi
166}
167
Dan Smith6cdb2e02015-04-23 09:12:59 -0700168# clean_lvm_filter() Remove the filter rule set in set_lvm_filter()
169#
170# Usage: clean_lvm_filter()
171function clean_lvm_filter {
172 sudo sed -i "s/^.*# from devstack$//" /etc/lvm/lvm.conf
173}
174
John Griffith4bf861c2015-03-17 21:07:39 -0600175# set_lvm_filter() Gather all devices configured for LVM and
176# use them to build a global device filter
177# set_lvm_filter() Create a device filter
178# and add to /etc/lvm.conf. Note this uses
179# all current PV's in use by LVM on the
180# system to build it's filter.
181#
182# Usage: set_lvm_filter()
183function set_lvm_filter {
Dan Smith6cdb2e02015-04-23 09:12:59 -0700184 local filter_suffix='"r|.*|" ] # from devstack'
John Griffith4bf861c2015-03-17 21:07:39 -0600185 local filter_string="global_filter = [ "
186 local pv
187 local vg
188 local line
189
190 for pv_info in $(sudo pvs --noheadings -o name); do
191 pv=$(echo -e "${pv_info}" | sed 's/ //g' | sed 's/\/dev\///g')
192 new="\"a|$pv|\", "
193 filter_string=$filter_string$new
194 done
195 filter_string=$filter_string$filter_suffix
196
Dan Smith6cdb2e02015-04-23 09:12:59 -0700197 clean_lvm_filter
John Griffith4bf861c2015-03-17 21:07:39 -0600198 sudo sed -i "/# global_filter = \[*\]/a\ $global_filter$filter_string" /etc/lvm/lvm.conf
199 echo_summary "set lvm.conf device global_filter to: $filter_string"
200}
Maru Newbyc070a3d2015-01-27 17:44:44 +0000201
Daniel Genind4708672014-10-31 15:01:29 -0400202# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100203$_XTRACE_LVM
Daniel Genind4708672014-10-31 15:01:29 -0400204
205# mode: shell-script
206# End: