blob: 519e82c806aec5e5e5bf2e0e53c21e705b94f628 [file] [log] [blame]
Daniel Genind4708672014-10-31 15:01:29 -04001# lib/lvm
2# Configure the default LVM volume group used by Cinder and Nova
3
4# Dependencies:
5#
6# - ``functions`` file
7# - ``cinder`` configurations
8
9# DATA_DIR
10
11# clean_default_volume_group - called from clean()
12# configure_default_volume_group - called from configure()
13# init_default_volume_group - called from init()
14
15
16# Save trace setting
17MY_XTRACE=$(set +o | grep xtrace)
18set +o xtrace
19
20
21# Defaults
22# --------
23# Name of the lvm volume groups to use/create for iscsi volumes
24# This monkey-motion is for compatibility with icehouse-generation Grenade
25# If ``VOLUME_GROUP`` is set, use it, otherwise we'll build a VG name based
26# on ``VOLUME_GROUP_NAME`` that includes the backend name
27# Grenade doesn't use ``VOLUME_GROUP2`` so it is left out
28VOLUME_GROUP_NAME=${VOLUME_GROUP:-${VOLUME_GROUP_NAME:-stack-volumes}}
29DEFAULT_VOLUME_GROUP_NAME=$VOLUME_GROUP_NAME-default
30
31# Backing file name is of the form $VOLUME_GROUP$BACKING_FILE_SUFFIX
32BACKING_FILE_SUFFIX=-backing-file
33
34
35# Entry Points
36# ------------
37
38# _clean_lvm_volume_group removes all default LVM volumes
39#
40# Usage: clean_lvm_volume_group $vg
41function _clean_lvm_volume_group {
42 local vg=$1
43
44 # Clean out existing volumes
45 sudo lvremove -f $vg
46}
47
48# _clean_lvm_backing_file() removes the backing file of the
49# volume group
50#
51# Usage: _clean_lvm_backing_file() $backing_file
52function _clean_lvm_backing_file {
53 local backing_file=$1
54
55 # if the backing physical device is a loop device, it was probably setup by devstack
56 if [[ -n "$backing_file" ]] && [[ -e "$backing_file" ]]; then
57 local vg_dev=$(sudo losetup -j $backing_file | awk -F':' '/'$BACKING_FILE_SUFFIX'/ { print $1}')
58 sudo losetup -d $vg_dev
59 rm -f $backing_file
60 fi
61}
62
63# clean_lvm_volume_group() cleans up the volume group and removes the
64# backing file
65#
66# Usage: clean_lvm_volume_group $vg
67function clean_lvm_volume_group {
68 local vg=$1
69
70 _clean_lvm_volume_group $vg
71 # if there is no logical volume left, it's safe to attempt a cleanup
72 # of the backing file
73 if [[ -z "$(sudo lvs --noheadings -o lv_name $vg 2>/dev/null)" ]]; then
74 _clean_lvm_backing_file $DATA_DIR/$vg$BACKING_FILE_SUFFIX
75 fi
76}
77
78
79# _create_volume_group creates default volume group
80#
81# Usage: _create_lvm_volume_group() $vg $size
82function _create_lvm_volume_group {
83 local vg=$1
84 local size=$2
85
86 local backing_file=$DATA_DIR/$vg$BACKING_FILE_SUFFIX
87 if ! sudo vgs $vg; then
88 # Only create if the file doesn't already exists
Chris Dent4c206072015-02-16 21:56:29 +000089 [[ -f $backing_file ]] || truncate -s $size $backing_file
Daniel Genind4708672014-10-31 15:01:29 -040090 local vg_dev=`sudo losetup -f --show $backing_file`
91
92 # Only create volume group if it doesn't already exist
93 if ! sudo vgs $vg; then
94 sudo vgcreate $vg $vg_dev
95 fi
96 fi
97}
98
99# init_lvm_volume_group() initializes the volume group creating the backing
100# file if necessary
101#
102# Usage: init_lvm_volume_group() $vg
103function init_lvm_volume_group {
104 local vg=$1
105 local size=$2
Daniel Genind4708672014-10-31 15:01:29 -0400106
Attila Fazekas380d92c2015-02-18 16:22:06 +0100107 # Start the lvmetad and tgtd services
Daniel Genind4708672014-10-31 15:01:29 -0400108 if is_fedora || is_suse; then
Attila Fazekas380d92c2015-02-18 16:22:06 +0100109 # services is not started by default
110 start_service lvm2-lvmetad
Attila Fazekasc70605d2015-01-26 15:44:47 +0100111 if [ "$CINDER_ISCSI_HELPER" = "tgtadm" ]; then
112 start_service tgtd
113 fi
Daniel Genind4708672014-10-31 15:01:29 -0400114 fi
115
Attila Fazekas380d92c2015-02-18 16:22:06 +0100116 # Start with a clean volume group
117 _create_lvm_volume_group $vg $size
118
Daniel Genind4708672014-10-31 15:01:29 -0400119 # Remove iscsi targets
Attila Fazekasc70605d2015-01-26 15:44:47 +0100120 if [ "$CINDER_ISCSI_HELPER" = "lioadm" ]; then
121 sudo cinder-rtstool get-targets | sudo xargs -rn 1 cinder-rtstool delete
122 else
123 sudo tgtadm --op show --mode target | grep Target | cut -f3 -d ' ' | sudo xargs -n1 tgt-admin --delete || true
124 fi
Daniel Genind4708672014-10-31 15:01:29 -0400125 _clean_lvm_volume_group $vg
126}
127
Maru Newbyc070a3d2015-01-27 17:44:44 +0000128# Sentinal value to ensure that init of default lvm volume group is
129# only performed once across calls of init_default_lvm_volume_group.
130_DEFAULT_LVM_INIT=${_DEFAULT_LVM_INIT:-0}
131
132# init_default_lvm_volume_group() initializes a default volume group
133# intended to be shared between cinder and nova. It is idempotent;
134# the init of the default volume group is guaranteed to be performed
135# only once so that either or both of the dependent services can
136# safely call this function.
137#
138# Usage: init_default_lvm_volume_group()
139function init_default_lvm_volume_group {
140 if [[ "$_DEFAULT_LVM_INIT" = "0" ]]; then
141 init_lvm_volume_group $DEFAULT_VOLUME_GROUP_NAME $VOLUME_BACKING_FILE_SIZE
142 _DEFAULT_LVM_INIT=1
143 fi
144}
145
John Griffith4bf861c2015-03-17 21:07:39 -0600146# set_lvm_filter() Gather all devices configured for LVM and
147# use them to build a global device filter
148# set_lvm_filter() Create a device filter
149# and add to /etc/lvm.conf. Note this uses
150# all current PV's in use by LVM on the
151# system to build it's filter.
152#
153# Usage: set_lvm_filter()
154function set_lvm_filter {
155 local filter_suffix='"r|.*|" ]'
156 local filter_string="global_filter = [ "
157 local pv
158 local vg
159 local line
160
161 for pv_info in $(sudo pvs --noheadings -o name); do
162 pv=$(echo -e "${pv_info}" | sed 's/ //g' | sed 's/\/dev\///g')
163 new="\"a|$pv|\", "
164 filter_string=$filter_string$new
165 done
166 filter_string=$filter_string$filter_suffix
167
168 sudo sed -i "/# global_filter = \[*\]/a\ $global_filter$filter_string" /etc/lvm/lvm.conf
169 echo_summary "set lvm.conf device global_filter to: $filter_string"
170}
Maru Newbyc070a3d2015-01-27 17:44:44 +0000171
Daniel Genind4708672014-10-31 15:01:29 -0400172# Restore xtrace
173$MY_XTRACE
174
175# mode: shell-script
176# End: