| 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 | 
|  | 19 | MY_XTRACE=$(set +o | grep xtrace) | 
|  | 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 | 
|  | 59 | local vg_dev=$(sudo losetup -j $backing_file | awk -F':' '/'$BACKING_FILE_SUFFIX'/ { print $1}') | 
|  | 60 | sudo losetup -d $vg_dev | 
|  | 61 | rm -f $backing_file | 
|  | 62 | fi | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | # clean_lvm_volume_group() cleans up the volume group and removes the | 
|  | 66 | # backing file | 
|  | 67 | # | 
|  | 68 | # Usage: clean_lvm_volume_group $vg | 
|  | 69 | function clean_lvm_volume_group { | 
|  | 70 | local vg=$1 | 
|  | 71 |  | 
|  | 72 | _clean_lvm_volume_group $vg | 
|  | 73 | # if there is no logical volume left, it's safe to attempt a cleanup | 
|  | 74 | # of the backing file | 
|  | 75 | if [[ -z "$(sudo lvs --noheadings -o lv_name $vg 2>/dev/null)" ]]; then | 
|  | 76 | _clean_lvm_backing_file $DATA_DIR/$vg$BACKING_FILE_SUFFIX | 
|  | 77 | fi | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 |  | 
|  | 81 | # _create_volume_group creates default volume group | 
|  | 82 | # | 
|  | 83 | # Usage: _create_lvm_volume_group() $vg $size | 
|  | 84 | function _create_lvm_volume_group { | 
|  | 85 | local vg=$1 | 
|  | 86 | local size=$2 | 
|  | 87 |  | 
|  | 88 | local backing_file=$DATA_DIR/$vg$BACKING_FILE_SUFFIX | 
|  | 89 | if ! sudo vgs $vg; then | 
|  | 90 | # Only create if the file doesn't already exists | 
| Chris Dent | 4c20607 | 2015-02-16 21:56:29 +0000 | [diff] [blame] | 91 | [[ -f $backing_file ]] || truncate -s $size $backing_file | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 92 | local vg_dev=`sudo losetup -f --show $backing_file` | 
|  | 93 |  | 
|  | 94 | # Only create volume group if it doesn't already exist | 
|  | 95 | if ! sudo vgs $vg; then | 
|  | 96 | sudo vgcreate $vg $vg_dev | 
|  | 97 | fi | 
|  | 98 | fi | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | # init_lvm_volume_group() initializes the volume group creating the backing | 
|  | 102 | # file if necessary | 
|  | 103 | # | 
|  | 104 | # Usage: init_lvm_volume_group() $vg | 
|  | 105 | function init_lvm_volume_group { | 
|  | 106 | local vg=$1 | 
|  | 107 | local size=$2 | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 108 |  | 
| Attila Fazekas | 380d92c | 2015-02-18 16:22:06 +0100 | [diff] [blame] | 109 | # Start the lvmetad and tgtd services | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 110 | if is_fedora || is_suse; then | 
| Attila Fazekas | 380d92c | 2015-02-18 16:22:06 +0100 | [diff] [blame] | 111 | # services is not started by default | 
|  | 112 | start_service lvm2-lvmetad | 
| Attila Fazekas | c70605d | 2015-01-26 15:44:47 +0100 | [diff] [blame] | 113 | if [ "$CINDER_ISCSI_HELPER" = "tgtadm" ]; then | 
|  | 114 | start_service tgtd | 
|  | 115 | fi | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 116 | fi | 
|  | 117 |  | 
| Attila Fazekas | 380d92c | 2015-02-18 16:22:06 +0100 | [diff] [blame] | 118 | # Start with a clean volume group | 
|  | 119 | _create_lvm_volume_group $vg $size | 
|  | 120 |  | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 121 | # Remove iscsi targets | 
| Attila Fazekas | c70605d | 2015-01-26 15:44:47 +0100 | [diff] [blame] | 122 | if [ "$CINDER_ISCSI_HELPER" = "lioadm" ]; then | 
|  | 123 | sudo cinder-rtstool get-targets | sudo xargs -rn 1 cinder-rtstool delete | 
|  | 124 | else | 
|  | 125 | sudo tgtadm --op show --mode target | grep Target | cut -f3 -d ' ' | sudo xargs -n1 tgt-admin --delete || true | 
|  | 126 | fi | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 127 | _clean_lvm_volume_group $vg | 
|  | 128 | } | 
|  | 129 |  | 
| Maru Newby | c070a3d | 2015-01-27 17:44:44 +0000 | [diff] [blame] | 130 | # Sentinal value to ensure that init of default lvm volume group is | 
|  | 131 | # only performed once across calls of init_default_lvm_volume_group. | 
|  | 132 | _DEFAULT_LVM_INIT=${_DEFAULT_LVM_INIT:-0} | 
|  | 133 |  | 
|  | 134 | # init_default_lvm_volume_group() initializes a default volume group | 
|  | 135 | # intended to be shared between cinder and nova.  It is idempotent; | 
|  | 136 | # the init of the default volume group is guaranteed to be performed | 
|  | 137 | # only once so that either or both of the dependent services can | 
|  | 138 | # safely call this function. | 
|  | 139 | # | 
|  | 140 | # Usage: init_default_lvm_volume_group() | 
|  | 141 | function init_default_lvm_volume_group { | 
|  | 142 | if [[ "$_DEFAULT_LVM_INIT" = "0" ]]; then | 
|  | 143 | init_lvm_volume_group $DEFAULT_VOLUME_GROUP_NAME $VOLUME_BACKING_FILE_SIZE | 
|  | 144 | _DEFAULT_LVM_INIT=1 | 
|  | 145 | fi | 
|  | 146 | } | 
|  | 147 |  | 
| John Griffith | 4bf861c | 2015-03-17 21:07:39 -0600 | [diff] [blame] | 148 | # set_lvm_filter() Gather all devices configured for LVM and | 
|  | 149 | # use them to build a global device filter | 
|  | 150 | # set_lvm_filter() Create a device filter | 
|  | 151 | # and add to /etc/lvm.conf.  Note this uses | 
|  | 152 | # all current PV's in use by LVM on the | 
|  | 153 | # system to build it's filter. | 
|  | 154 | # | 
|  | 155 | # Usage: set_lvm_filter() | 
|  | 156 | function set_lvm_filter { | 
|  | 157 | local filter_suffix='"r|.*|" ]' | 
|  | 158 | local filter_string="global_filter = [ " | 
|  | 159 | local pv | 
|  | 160 | local vg | 
|  | 161 | local line | 
|  | 162 |  | 
|  | 163 | for pv_info in $(sudo pvs --noheadings -o name); do | 
|  | 164 | pv=$(echo -e "${pv_info}" | sed 's/ //g' | sed 's/\/dev\///g') | 
|  | 165 | new="\"a|$pv|\", " | 
|  | 166 | filter_string=$filter_string$new | 
|  | 167 | done | 
|  | 168 | filter_string=$filter_string$filter_suffix | 
|  | 169 |  | 
|  | 170 | sudo sed -i "/# global_filter = \[*\]/a\    $global_filter$filter_string" /etc/lvm/lvm.conf | 
|  | 171 | echo_summary "set lvm.conf device global_filter to: $filter_string" | 
|  | 172 | } | 
| Maru Newby | c070a3d | 2015-01-27 17:44:44 +0000 | [diff] [blame] | 173 |  | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 174 | # Restore xtrace | 
|  | 175 | $MY_XTRACE | 
|  | 176 |  | 
|  | 177 | # mode: shell-script | 
|  | 178 | # End: |