blob: c183f09d04c49e39e3c6a1f55f8cd4bb812b3ad8 [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
106 # Start with a clean volume group
107 _create_lvm_volume_group $vg $size
108
109 if is_fedora || is_suse; then
110 # service is not started by default
111 start_service tgtd
112 fi
113
114 # Remove iscsi targets
115 sudo tgtadm --op show --mode target | grep Target | cut -f3 -d ' ' | sudo xargs -n1 tgt-admin --delete || true
116
117 _clean_lvm_volume_group $vg
118}
119
Maru Newbyc070a3d2015-01-27 17:44:44 +0000120# Sentinal value to ensure that init of default lvm volume group is
121# only performed once across calls of init_default_lvm_volume_group.
122_DEFAULT_LVM_INIT=${_DEFAULT_LVM_INIT:-0}
123
124# init_default_lvm_volume_group() initializes a default volume group
125# intended to be shared between cinder and nova. It is idempotent;
126# the init of the default volume group is guaranteed to be performed
127# only once so that either or both of the dependent services can
128# safely call this function.
129#
130# Usage: init_default_lvm_volume_group()
131function init_default_lvm_volume_group {
132 if [[ "$_DEFAULT_LVM_INIT" = "0" ]]; then
133 init_lvm_volume_group $DEFAULT_VOLUME_GROUP_NAME $VOLUME_BACKING_FILE_SIZE
134 _DEFAULT_LVM_INIT=1
135 fi
136}
137
138
Daniel Genind4708672014-10-31 15:01:29 -0400139# Restore xtrace
140$MY_XTRACE
141
142# mode: shell-script
143# End: