|
| 1 | +#!/bin/bash |
| 2 | +# setup-kaminario-mpath.sh - script for configuring the multipath.conf file with Kaminario devices |
| 3 | +# Author: flashdba (http://flashdba.com) |
| 4 | +# The output can be cut and pasted into the "multipaths {}" section of /etc/multipath.conf |
| 5 | +# Alternatively the -l option can be used to print a list of devices |
| 6 | +# |
| 7 | +# For educational purposes only - no warranty is provided |
| 8 | +# Test thoroughly - use at your own risk |
| 9 | + |
| 10 | +# TODO: Currently only written for use with Red Hat 6 / Oracle Linux 6 / SLES 11 |
| 11 | + |
| 12 | +# Setup variables and arrays |
| 13 | +VERBOSE=0 |
| 14 | +LISTMODE=0 |
| 15 | +MULTIPATH_DEVICES=() |
| 16 | + |
| 17 | +# Setup print functions |
| 18 | +echoerr() { echo "Error: $@" 1>&2; } |
| 19 | +echovrb() { [[ "$VERBOSE" = 1 ]] && echo "Info : ${@}" 1>&2; } |
| 20 | +echoout() { echo "$@"; } |
| 21 | +echolst() { echo "$@" | tr '±' '\t' | expand -t 5 1>&2; } |
| 22 | + |
| 23 | +# Function for printing usage information |
| 24 | +usage() { |
| 25 | + echo "" 1>&2 |
| 26 | + if [ "$#" -gt 0 ]; then |
| 27 | + echo "Error: $@" 1>&2 |
| 28 | + echo "" 1>&2 |
| 29 | + fi |
| 30 | + echo "Usage: $0 [-v ]" 1>&2 |
| 31 | + echo "" 1>&2 |
| 32 | + echo " Script for configuring the /etc/multipath.conf file" 1>&2 |
| 33 | + echo " Creates entries for the \"multipath \{\}\" section" 1>&2 |
| 34 | + echo " Requires the sg3_utils package to be present" 1>&2 |
| 35 | + echo " Errors and info are printed to stderr" 1>&2 |
| 36 | + echo "" 1>&2 |
| 37 | + echo " Options:" 1>&2 |
| 38 | + echo " -h Help (print help and version information)" 1>&2 |
| 39 | + echo " -l List (print a list of devices and their details)" 1>&2 |
| 40 | + echo " -v Verbose (show processing details)" 1>&2 |
| 41 | + echo "" 1>&2 |
| 42 | + exit 1 |
| 43 | +} |
| 44 | + |
| 45 | +while getopts ":hvl" opt; do |
| 46 | + case $opt in |
| 47 | + h) |
| 48 | + usage |
| 49 | + ;; |
| 50 | + v) |
| 51 | + VERBOSE=1 |
| 52 | + echovrb "Running in verbose mode" |
| 53 | + ;; |
| 54 | + l) |
| 55 | + LISTMODE=1 |
| 56 | + echovrb "Running in list mode" |
| 57 | + ;; |
| 58 | + \?) |
| 59 | + echo "Invalid option: -$OPTARG" >&2 |
| 60 | + usage |
| 61 | + ;; |
| 62 | + esac |
| 63 | +done |
| 64 | + |
| 65 | +# Check that the s3_utils package has been installed and is in the path |
| 66 | +if hash sg_inq 2> /dev/null; then |
| 67 | + echovrb "Using sg_inq `sg_inq -V 2>&1`" |
| 68 | +else |
| 69 | + echoerr "sg3_utils package not installed - exiting..." |
| 70 | + exit 1 |
| 71 | +fi |
| 72 | + |
| 73 | +# Build a list of multipath devices to scan |
| 74 | +MULTIPATH_FILELIST=$( ls -1 /dev/dm-* ) |
| 75 | + |
| 76 | +# Iterate through the list |
| 77 | +for MPDEVICE in $MULTIPATH_FILELIST; do |
| 78 | + |
| 79 | + echovrb "Issuing inquiry to device $MPDEVICE" |
| 80 | + |
| 81 | + # Issue sg3 inquiry to device |
| 82 | + SG3_OUTPUT=`sg_inq -i $MPDEVICE 2> /dev/null` |
| 83 | + SG3_RETVAL=$? |
| 84 | + |
| 85 | + # If inquiry returned error code then skip |
| 86 | + if [ "$SG3_RETVAL" -ne 0 ]; then |
| 87 | + echovrb "Skipping device $MPDEVICE" |
| 88 | + continue |
| 89 | + fi |
| 90 | + |
| 91 | + # Scan output to find vendor id |
| 92 | + SG3_VENDORID=`echo "$SG3_OUTPUT" | grep "vendor id:" | cut -d':' -f2- | sed 's/^ *//g' | sed 's/ *$//g' ` 2> /dev/null |
| 93 | + |
| 94 | + # Check the vendor is KMNRIO otherwise skip |
| 95 | + if [ "$SG3_VENDORID" != "KMNRIO" ]; then |
| 96 | + echovrb "Ignoring device on $MPDEVICE with vendor id = $SG3_VENDORID" |
| 97 | + continue |
| 98 | + fi |
| 99 | + |
| 100 | + # Get the sysfs device location (required for udevinfo) |
| 101 | + MPATH_DEVBASE=`basename $MPDEVICE` |
| 102 | + MPATH_SYSFS="/block/$MPATH_DEVBASE" |
| 103 | + |
| 104 | + # Process device specific details |
| 105 | + LUN_NAME=`echo "$SG3_OUTPUT" | grep "vendor specific:" | awk '{print ($3)}'` |
| 106 | + LUN_UUID=`udevadm info --query=property --path=$MPATH_SYSFS 2> /dev/null | grep "DM_UUID=" | sed 's/^DM_UUID=mpath-*//g'` |
| 107 | + echovrb "Found Kaminario device on $MPDEVICE: LUN Name = $LUN_NAME UUID = $LUN_UUID" |
| 108 | + |
| 109 | + # Add details to an array variable of Kaminario devices |
| 110 | + MULTIPATH_DEVICES+=(`echo "$LUN_NAME:$LUN_UUID:$MPDEVICE"`) |
| 111 | +done |
| 112 | + |
| 113 | +# Sort the array into alphabetical order based on LUN name |
| 114 | +echovrb "Sorting discovered devices into alphabetical order..." |
| 115 | +MULTIPATH_DEVICES=($(for MPDEVICE in ${MULTIPATH_DEVICES[@]}; do |
| 116 | + echo $MPDEVICE |
| 117 | +done | sort)) |
| 118 | +echovrb "Sort complete" |
| 119 | + |
| 120 | +if [ "$LISTMODE" = 1 ]; then |
| 121 | + echolst "Device Name±LUN Name±WWID" |
| 122 | + echolst "-----------±--------±----------------------------------" |
| 123 | +else |
| 124 | + if [ -r /etc/multipath.conf ]; then |
| 125 | + echovrb "Backup up /etc/multipath.conf to /tmp" |
| 126 | + cp /etc/multipath.conf /tmp |
| 127 | + fi |
| 128 | +fi |
| 129 | + |
| 130 | +echovrb "Printing multipath.conf configuration details..." |
| 131 | +echovrb "" |
| 132 | + |
| 133 | +# Now print the multipath.conf output for each device, converting the LUN name to lowercase |
| 134 | +for MPDEVICE in ${MULTIPATH_DEVICES[@]}; do |
| 135 | + MP_WWID=`echo $MPDEVICE | cut -d':' -f2` |
| 136 | + MP_ALIAS=`echo $MPDEVICE | cut -d':' -f1 | tr '[:upper:]' '[:lower:]'` |
| 137 | + MP_DEVNAME=`echo $MPDEVICE | cut -d':' -f3` |
| 138 | + |
| 139 | + if [ "$LISTMODE" = 1 ]; then |
| 140 | + echolst "$MP_DEVNAME ±$MP_ALIAS±$MP_WWID" |
| 141 | + else |
| 142 | + echoout " multipath {" |
| 143 | + echoout " wwid $MP_WWID" |
| 144 | + echoout " alias $MP_ALIAS" |
| 145 | + echoout " }" |
| 146 | + fi |
| 147 | +done |
| 148 | + |
| 149 | +echovrb "Successful completion" |
| 150 | +exit 0 |
0 commit comments