| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 1 | # 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 | 
 | 17 | MY_XTRACE=$(set +o | grep xtrace) | 
 | 18 | set +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 | 
 | 28 | VOLUME_GROUP_NAME=${VOLUME_GROUP:-${VOLUME_GROUP_NAME:-stack-volumes}} | 
 | 29 | DEFAULT_VOLUME_GROUP_NAME=$VOLUME_GROUP_NAME-default | 
 | 30 |  | 
 | 31 | # Backing file name is of the form $VOLUME_GROUP$BACKING_FILE_SUFFIX | 
 | 32 | BACKING_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 | 
 | 41 | function _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 | 
 | 52 | function _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 | 
 | 67 | function 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 | 
 | 82 | function _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 Dent | 4c20607 | 2015-02-16 21:56:29 +0000 | [diff] [blame] | 89 |         [[ -f $backing_file ]] || truncate -s $size $backing_file | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 90 |         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 | 
 | 103 | function init_lvm_volume_group { | 
 | 104 |     local vg=$1 | 
 | 105 |     local size=$2 | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 106 |  | 
| Attila Fazekas | 380d92c | 2015-02-18 16:22:06 +0100 | [diff] [blame] | 107 |     # Start the lvmetad and tgtd services | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 108 |     if is_fedora || is_suse; then | 
| Attila Fazekas | 380d92c | 2015-02-18 16:22:06 +0100 | [diff] [blame] | 109 |         # services is not started by default | 
 | 110 |         start_service lvm2-lvmetad | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 111 |         start_service tgtd | 
 | 112 |     fi | 
 | 113 |  | 
| Attila Fazekas | 380d92c | 2015-02-18 16:22:06 +0100 | [diff] [blame] | 114 |     # Start with a clean volume group | 
 | 115 |     _create_lvm_volume_group $vg $size | 
 | 116 |  | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 117 |     # 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 Newby | c070a3d | 2015-01-27 17:44:44 +0000 | [diff] [blame] | 123 | # 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() | 
 | 134 | function 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 |  | 
 | 141 |  | 
| Daniel Genin | d470867 | 2014-10-31 15:01:29 -0400 | [diff] [blame] | 142 | # Restore xtrace | 
 | 143 | $MY_XTRACE | 
 | 144 |  | 
 | 145 | # mode: shell-script | 
 | 146 | # End: |