| Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 1 | #!/bin/bash | 
|  | 2 | # | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 3 | # 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 Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 19 | _XTRACE_LVM=$(set +o | grep xtrace) | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 20 | set +o xtrace | 
|  | 21 |  | 
|  | 22 |  | 
|  | 23 | # Defaults | 
|  | 24 | # -------- | 
|  | 25 | # Name of the lvm volume groups to use/create for iscsi volumes | 
|  | 26 | # This monkey-motion is for compatibility with icehouse-generation Grenade | 
|  | 27 | # If ``VOLUME_GROUP`` is set, use it, otherwise we'll build a VG name based | 
|  | 28 | # on ``VOLUME_GROUP_NAME`` that includes the backend name | 
|  | 29 | # Grenade doesn't use ``VOLUME_GROUP2`` so it is left out | 
|  | 30 | VOLUME_GROUP_NAME=${VOLUME_GROUP:-${VOLUME_GROUP_NAME:-stack-volumes}} | 
|  | 31 | DEFAULT_VOLUME_GROUP_NAME=$VOLUME_GROUP_NAME-default | 
|  | 32 |  | 
|  | 33 | # Backing file name is of the form $VOLUME_GROUP$BACKING_FILE_SUFFIX | 
|  | 34 | BACKING_FILE_SUFFIX=-backing-file | 
|  | 35 |  | 
|  | 36 |  | 
| Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 37 | # Functions | 
|  | 38 | # --------- | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 39 |  | 
|  | 40 | # _clean_lvm_volume_group removes all default LVM volumes | 
|  | 41 | # | 
|  | 42 | # Usage: clean_lvm_volume_group $vg | 
|  | 43 | function _clean_lvm_volume_group { | 
|  | 44 | local vg=$1 | 
|  | 45 |  | 
|  | 46 | # Clean out existing volumes | 
|  | 47 | sudo lvremove -f $vg | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | # _clean_lvm_backing_file() removes the backing file of the | 
|  | 51 | # volume group | 
|  | 52 | # | 
|  | 53 | # Usage: _clean_lvm_backing_file() $backing_file | 
|  | 54 | function _clean_lvm_backing_file { | 
|  | 55 | local backing_file=$1 | 
|  | 56 |  | 
| Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 57 | # If the backing physical device is a loop device, it was probably setup by DevStack | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 58 | if [[ -n "$backing_file" ]] && [[ -e "$backing_file" ]]; then | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 59 | local vg_dev | 
|  | 60 | vg_dev=$(sudo losetup -j $backing_file | awk -F':' '/'$BACKING_FILE_SUFFIX'/ { print $1}') | 
| Eric Harney | 5237d16 | 2016-08-30 10:59:52 -0400 | [diff] [blame] | 61 | if [[ -n "$vg_dev" ]]; then | 
|  | 62 | sudo losetup -d $vg_dev | 
|  | 63 | fi | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 64 | rm -f $backing_file | 
|  | 65 | fi | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | # clean_lvm_volume_group() cleans up the volume group and removes the | 
|  | 69 | # backing file | 
|  | 70 | # | 
|  | 71 | # Usage: clean_lvm_volume_group $vg | 
|  | 72 | function clean_lvm_volume_group { | 
|  | 73 | local vg=$1 | 
|  | 74 |  | 
|  | 75 | _clean_lvm_volume_group $vg | 
|  | 76 | # if there is no logical volume left, it's safe to attempt a cleanup | 
|  | 77 | # of the backing file | 
|  | 78 | if [[ -z "$(sudo lvs --noheadings -o lv_name $vg 2>/dev/null)" ]]; then | 
|  | 79 | _clean_lvm_backing_file $DATA_DIR/$vg$BACKING_FILE_SUFFIX | 
|  | 80 | fi | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 |  | 
| Shin Sato | 9ee1ef6 | 2015-05-28 13:56:58 +0900 | [diff] [blame] | 84 | # _create_lvm_volume_group creates default volume group | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 85 | # | 
|  | 86 | # Usage: _create_lvm_volume_group() $vg $size | 
|  | 87 | function _create_lvm_volume_group { | 
|  | 88 | local vg=$1 | 
|  | 89 | local size=$2 | 
|  | 90 |  | 
|  | 91 | local backing_file=$DATA_DIR/$vg$BACKING_FILE_SUFFIX | 
|  | 92 | if ! sudo vgs $vg; then | 
|  | 93 | # Only create if the file doesn't already exists | 
| Chris Dent | 4c20607 | 2015-02-16 21:56:29 +0000 | [diff] [blame] | 94 | [[ -f $backing_file ]] || truncate -s $size $backing_file | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 95 | local vg_dev | 
|  | 96 | vg_dev=`sudo losetup -f --show $backing_file` | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 97 |  | 
|  | 98 | # Only create volume group if it doesn't already exist | 
|  | 99 | if ! sudo vgs $vg; then | 
|  | 100 | sudo vgcreate $vg $vg_dev | 
|  | 101 | fi | 
|  | 102 | fi | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | # init_lvm_volume_group() initializes the volume group creating the backing | 
|  | 106 | # file if necessary | 
|  | 107 | # | 
|  | 108 | # Usage: init_lvm_volume_group() $vg | 
|  | 109 | function init_lvm_volume_group { | 
|  | 110 | local vg=$1 | 
|  | 111 | local size=$2 | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 112 |  | 
| Attila Fazekas | 380d92c | 2015-02-18 16:22:06 +0100 | [diff] [blame] | 113 | # Start the lvmetad and tgtd services | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 114 | if is_fedora || is_suse; then | 
| Attila Fazekas | 380d92c | 2015-02-18 16:22:06 +0100 | [diff] [blame] | 115 | # services is not started by default | 
|  | 116 | start_service lvm2-lvmetad | 
| Attila Fazekas | c70605d | 2015-01-26 15:44:47 +0100 | [diff] [blame] | 117 | if [ "$CINDER_ISCSI_HELPER" = "tgtadm" ]; then | 
|  | 118 | start_service tgtd | 
|  | 119 | fi | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 120 | fi | 
|  | 121 |  | 
| Attila Fazekas | 380d92c | 2015-02-18 16:22:06 +0100 | [diff] [blame] | 122 | # Start with a clean volume group | 
|  | 123 | _create_lvm_volume_group $vg $size | 
|  | 124 |  | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 125 | # Remove iscsi targets | 
| Attila Fazekas | c70605d | 2015-01-26 15:44:47 +0100 | [diff] [blame] | 126 | if [ "$CINDER_ISCSI_HELPER" = "lioadm" ]; then | 
|  | 127 | sudo cinder-rtstool get-targets | sudo xargs -rn 1 cinder-rtstool delete | 
|  | 128 | else | 
| Waldemar Znoinski | 084efc7 | 2016-03-09 12:18:51 +0000 | [diff] [blame] | 129 | sudo tgtadm --op show --mode target | awk '/Target/ {print $3}' | sudo xargs -r -n1 tgt-admin --delete | 
| Attila Fazekas | c70605d | 2015-01-26 15:44:47 +0100 | [diff] [blame] | 130 | fi | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 131 | _clean_lvm_volume_group $vg | 
|  | 132 | } | 
|  | 133 |  | 
| Maru Newby | c070a3d | 2015-01-27 17:44:44 +0000 | [diff] [blame] | 134 | # Sentinal value to ensure that init of default lvm volume group is | 
|  | 135 | # only performed once across calls of init_default_lvm_volume_group. | 
|  | 136 | _DEFAULT_LVM_INIT=${_DEFAULT_LVM_INIT:-0} | 
|  | 137 |  | 
|  | 138 | # init_default_lvm_volume_group() initializes a default volume group | 
|  | 139 | # intended to be shared between cinder and nova.  It is idempotent; | 
|  | 140 | # the init of the default volume group is guaranteed to be performed | 
|  | 141 | # only once so that either or both of the dependent services can | 
|  | 142 | # safely call this function. | 
|  | 143 | # | 
|  | 144 | # Usage: init_default_lvm_volume_group() | 
|  | 145 | function init_default_lvm_volume_group { | 
|  | 146 | if [[ "$_DEFAULT_LVM_INIT" = "0" ]]; then | 
|  | 147 | init_lvm_volume_group $DEFAULT_VOLUME_GROUP_NAME $VOLUME_BACKING_FILE_SIZE | 
|  | 148 | _DEFAULT_LVM_INIT=1 | 
|  | 149 | fi | 
|  | 150 | } | 
|  | 151 |  | 
| Dan Smith | 6cdb2e0 | 2015-04-23 09:12:59 -0700 | [diff] [blame] | 152 | # clean_lvm_filter() Remove the filter rule set in set_lvm_filter() | 
|  | 153 | # | 
|  | 154 | # Usage: clean_lvm_filter() | 
|  | 155 | function clean_lvm_filter { | 
|  | 156 | sudo sed -i "s/^.*# from devstack$//" /etc/lvm/lvm.conf | 
|  | 157 | } | 
|  | 158 |  | 
| John Griffith | 4bf861c | 2015-03-17 21:07:39 -0600 | [diff] [blame] | 159 | # set_lvm_filter() Gather all devices configured for LVM and | 
|  | 160 | # use them to build a global device filter | 
|  | 161 | # set_lvm_filter() Create a device filter | 
|  | 162 | # and add to /etc/lvm.conf.  Note this uses | 
|  | 163 | # all current PV's in use by LVM on the | 
|  | 164 | # system to build it's filter. | 
|  | 165 | # | 
|  | 166 | # Usage: set_lvm_filter() | 
|  | 167 | function set_lvm_filter { | 
| Dan Smith | 6cdb2e0 | 2015-04-23 09:12:59 -0700 | [diff] [blame] | 168 | local filter_suffix='"r|.*|" ]  # from devstack' | 
| John Griffith | 4bf861c | 2015-03-17 21:07:39 -0600 | [diff] [blame] | 169 | local filter_string="global_filter = [ " | 
|  | 170 | local pv | 
|  | 171 | local vg | 
|  | 172 | local line | 
|  | 173 |  | 
|  | 174 | for pv_info in $(sudo pvs --noheadings -o name); do | 
|  | 175 | pv=$(echo -e "${pv_info}" | sed 's/ //g' | sed 's/\/dev\///g') | 
|  | 176 | new="\"a|$pv|\", " | 
|  | 177 | filter_string=$filter_string$new | 
|  | 178 | done | 
|  | 179 | filter_string=$filter_string$filter_suffix | 
|  | 180 |  | 
| Dan Smith | 6cdb2e0 | 2015-04-23 09:12:59 -0700 | [diff] [blame] | 181 | clean_lvm_filter | 
| John Griffith | 4bf861c | 2015-03-17 21:07:39 -0600 | [diff] [blame] | 182 | sudo sed -i "/# global_filter = \[*\]/a\    $global_filter$filter_string" /etc/lvm/lvm.conf | 
|  | 183 | echo_summary "set lvm.conf device global_filter to: $filter_string" | 
|  | 184 | } | 
| Maru Newby | c070a3d | 2015-01-27 17:44:44 +0000 | [diff] [blame] | 185 |  | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 186 | # Restore xtrace | 
| Ian Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 187 | $_XTRACE_LVM | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 188 |  | 
|  | 189 | # mode: shell-script | 
|  | 190 | # End: |