|
| 1 | +#!/bin/bash |
| 2 | +# iscsiattach.sh - Scan and automatically attach new iSCSI targets |
| 3 | +# |
| 4 | +# Author: Steven B. Nelson, Sr. Solutions Architect |
| 5 | +# Oracle Bare Metal Cloud Services |
| 6 | +# |
| 7 | +# 20 April 2017 |
| 8 | +# Copyright Oracle, Inc. All rights reserved. |
| 9 | + |
| 10 | +# Make FIFO pipes for the two loops below |
| 11 | +mkfifo discpipe |
| 12 | +mkfifo sesspipe |
| 13 | + |
| 14 | +# Set the address ranges based on the Block Storage version |
| 15 | +V1ADDR="169.254.0.2" |
| 16 | +V2ADDR="169.254.2.0" |
| 17 | + |
| 18 | +# Set the block storage version |
| 19 | +BSV="v1" |
| 20 | + |
| 21 | +# If the BSV is v2, we need to scan all 254 addresses, otherwise, |
| 22 | +# we scan 1. :-( |
| 23 | + |
| 24 | +if [ ${BSV} = "v2" ] |
| 25 | +then |
| 26 | + numAddrs=254 |
| 27 | + BASEADDR=${V2ADDR} |
| 28 | +else |
| 29 | + numAddrs=3 |
| 30 | + BASEADDR=${V1ADDR} |
| 31 | +fi |
| 32 | + |
| 33 | +# Set a base address incrementor so we can loop through all the |
| 34 | +# addresses. |
| 35 | +addrCount=0 |
| 36 | + |
| 37 | +echo "Scanning "${numAddrs}" for new targets. Stand by." |
| 38 | +while [ ${addrCount} -le ${numAddrs} ] |
| 39 | +do |
| 40 | + # Set the current address to attempt to attach. |
| 41 | + if [ ${BSV} = "v2" ] |
| 42 | + then |
| 43 | + CURRADDR=`echo ${BASEADDR} | awk -F\. '{ |
| 44 | +last=$4+'${addrCount}' |
| 45 | +print $1"."$2"."$3"."last |
| 46 | +}'` |
| 47 | + else |
| 48 | + CURRADDR=`echo ${BASEADDR} | awk -F\. '{ |
| 49 | +last=$3+'${addrCount}' |
| 50 | +print $1"."$2"."last"."$4 |
| 51 | +}'` |
| 52 | + fi |
| 53 | + |
| 54 | + # We use ping to see if the target is even there. |
| 55 | + # Skip to the next address if we cant ping it. |
| 56 | + ping -q -c 1 -W 1 ${CURRADDR} > /dev/null 2>&1 |
| 57 | + result=$? |
| 58 | + if [ ${result} -ne 0 ] |
| 59 | + then |
| 60 | + (( addrCount = addrCount + 1 )) |
| 61 | + continue |
| 62 | + fi |
| 63 | + |
| 64 | + echo "Connecting to "${CURRADDR} |
| 65 | + # Find all the iSCSI Block Storage volumes attached to the instance but |
| 66 | + # not configured for use on the instance. Basically, get a list of the |
| 67 | + # volumes that the instance can see, the loop through the ones it has, |
| 68 | + # and add volumes not already configured on the instance. |
| 69 | + # |
| 70 | + # First get the list of volumes visible (attached) to the instance |
| 71 | + |
| 72 | + iscsiadm -m discovery -t st -p ${CURRADDR}:3260 | grep -v uefi | awk '{print $2}' > discpipe 2> /dev/null & |
| 73 | + |
| 74 | + # If the result is non-zero, that generally means that there are no targets available or |
| 75 | + # that the portal is reachable but not active. We make no distinction between the two |
| 76 | + # and simply skip ahead. |
| 77 | + result=$? |
| 78 | + if [ ${result} -ne 0 ] |
| 79 | + then |
| 80 | + (( addrCount = addrCount + 1 )) |
| 81 | + continue |
| 82 | + fi |
| 83 | + |
| 84 | + # Loop through the list (via the named FIFO pipe below) |
| 85 | + while read target |
| 86 | + do |
| 87 | + # Get the list of the currently attached Block Storage volumes |
| 88 | + iscsiadm -m session -P 0 | grep -v uefi | awk '{print $4}' > sesspipe 2> /dev/null & |
| 89 | + |
| 90 | + # Set a flag, and loop through the sessions (attached, but not configured) |
| 91 | + # and see if the volumes match. If so, skip to the next until we get |
| 92 | + # through the list. Session list is via the pipe. |
| 93 | + found="false" |
| 94 | + while read session |
| 95 | + do |
| 96 | + if [ ${target} = ${session} ] |
| 97 | + then |
| 98 | + found="true" |
| 99 | + break |
| 100 | + fi |
| 101 | + done < sesspipe |
| 102 | + |
| 103 | + # If the volume is not found, configure it. Get the resulting device file. |
| 104 | + if [ ${found} = "false" ] |
| 105 | + then |
| 106 | + iscsiadm -m node -o new -T ${target} -p ${CURRADDR}:3260 |
| 107 | + iscsiadm -m node -o update -T ${target} -n node.startup -v automatic |
| 108 | + iscsiadm -m node -T ${target} -p ${CURRADDR}:3260 -l |
| 109 | + sleep 10 |
| 110 | + fi |
| 111 | + done < discpipe |
| 112 | + (( addrCount = addrCount + 1 )) |
| 113 | +done |
| 114 | +echo "Scan Complete." |
| 115 | + |
| 116 | +# Remove the FIFOs |
| 117 | +find . -maxdepth 1 -type p -exec rm {} \; |
0 commit comments