blob: d0322c76b30516b2e5d5f65a8ee98a8ff108ed4d [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
Daniel Genind4708672014-10-31 15:01:29 -0400111 start_service tgtd
112 fi
113
Attila Fazekas380d92c2015-02-18 16:22:06 +0100114 # Start with a clean volume group
115 _create_lvm_volume_group $vg $size
116
Daniel Genind4708672014-10-31 15:01:29 -0400117 # Remove iscsi targets
118 sudo tgtadm --op show --mode target | grep Target | cut -f3 -d ' ' | sudo xargs -n1 tgt-admin --delete || true
119
120 _clean_lvm_volume_group $vg
121}
122
Maru Newbyc070a3d2015-01-27 17:44:44 +0000123# Sentinal value to ensure that init of default lvm volume group is
124# only performed once across calls of init_default_lvm_volume_group.
125_DEFAULT_LVM_INIT=${_DEFAULT_LVM_INIT:-0}
126
127# init_default_lvm_volume_group() initializes a default volume group
128# intended to be shared between cinder and nova. It is idempotent;
129# the init of the default volume group is guaranteed to be performed
130# only once so that either or both of the dependent services can
131# safely call this function.
132#
133# Usage: init_default_lvm_volume_group()
134function init_default_lvm_volume_group {
135 if [[ "$_DEFAULT_LVM_INIT" = "0" ]]; then
136 init_lvm_volume_group $DEFAULT_VOLUME_GROUP_NAME $VOLUME_BACKING_FILE_SIZE
137 _DEFAULT_LVM_INIT=1
138 fi
139}
140
John Griffith4bf861c2015-03-17 21:07:39 -0600141# set_lvm_filter() Gather all devices configured for LVM and
142# use them to build a global device filter
143# set_lvm_filter() Create a device filter
144# and add to /etc/lvm.conf. Note this uses
145# all current PV's in use by LVM on the
146# system to build it's filter.
147#
148# Usage: set_lvm_filter()
149function set_lvm_filter {
150 local filter_suffix='"r|.*|" ]'
151 local filter_string="global_filter = [ "
152 local pv
153 local vg
154 local line
155
156 for pv_info in $(sudo pvs --noheadings -o name); do
157 pv=$(echo -e "${pv_info}" | sed 's/ //g' | sed 's/\/dev\///g')
158 new="\"a|$pv|\", "
159 filter_string=$filter_string$new
160 done
161 filter_string=$filter_string$filter_suffix
162
163 sudo sed -i "/# global_filter = \[*\]/a\ $global_filter$filter_string" /etc/lvm/lvm.conf
164 echo_summary "set lvm.conf device global_filter to: $filter_string"
165}
Maru Newbyc070a3d2015-01-27 17:44:44 +0000166
Daniel Genind4708672014-10-31 15:01:29 -0400167# Restore xtrace
168$MY_XTRACE
169
170# mode: shell-script
171# End: