Skip to content

Commit a889c69

Browse files
authored
Merge branch 'main' into cloud-formation-script
2 parents 9f9d3dc + bb38496 commit a889c69

File tree

1 file changed

+64
-60
lines changed

1 file changed

+64
-60
lines changed

Management-Utilities/ec2-user-data-iscsi-create-and-mount/linux_userData.sh

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
#!/bin/bash
22

33
# user data
4-
# Secret name has it been saved in AWS secret manager
4+
# Set the secret name and region
55
SECRET_NAME=
66
AWS_REGION=
7-
# Fsx admin ip, e.g. 172.25.45.32
7+
# Set the FSx admin IP
88
FSXN_ADMIN_IP=
9-
# FSxN Volume name , e.g. iscsiVol
9+
# Volume name
1010
VOLUME_NAME=
11-
# Volume size in GB e.g 100
11+
# Volume size in GB
1212
VOLUME_SIZE=
13-
# Default value is fsx, but you can change it to any other value according to yours FSx for ONTAP SVM name
13+
# SVM name (default: fsx)
1414
SVM_NAME=fsx
15-
# Default value is fsxadmin, but you can change it to any other value according to yours FSx for ONTAP admin user name
15+
# ONTAP admin user (default: fsxadmin)
1616
ONTAP_USER=fsxadmin
1717
# end - user data
18-
1918
SECRET_NAME="${SECRET_NAME:=$1}"
2019
AWS_REGION="${AWS_REGION:=$2}"
2120
FSXN_ADMIN_IP="${FSXN_ADMIN_IP:=$3}"
@@ -26,14 +25,12 @@ SVM_NAME="${6:-$SVM_NAME}"
2625
min=100
2726
max=999
2827
LUN_NAME=${VOLUME_NAME}_$(($RANDOM%($max-$min+1)+$min))
29-
3028
# defaults
31-
# The script will create a log file in the ec2-user home directory
29+
# Log file in ec2-user home
3230
LOG_FILE=/home/ec2-user/install.log
3331
TIMEOUT=5
3432

3533
LUN_SIZE=$(bc -l <<< "0.90*$VOLUME_SIZE" )
36-
3734
echo "# Uninstall file" >> uninstall.sh
3835
chmod u+x uninstall.sh
3936

@@ -51,48 +48,47 @@ function getSecretValue() {
5148
exit 1
5249
fi
5350
}
54-
5551
logMessage() {
5652
echo "$(date) - $1" >> $LOG_FILE
5753
}
58-
5954
checkCommand() {
6055
if [ $? -ne 0 ]; then
6156
logMessage "$1 failed. Aborting."
6257
./uninstall.sh
6358
exit 1
6459
fi
6560
}
66-
6761
addUndoCommand() {
6862
sed -i "1i$1" uninstall.sh
6963
}
70-
64+
invokeLambda() {
65+
aws lambda invoke \
66+
--function-name "arn:aws:lambda:${AWS_REGION}:718273455463:function:reporting-monitoring-dashboard-usage" \
67+
--payload "$LAMBDA_PAYLOAD" \
68+
--cli-binary-format raw-in-base64-out \
69+
/home/ec2-user/lambda_response.json 2>/home/ec2-user/lambda_error.log
70+
}
7171
logMessage "Get secret data"
7272
getSecretValue "${SECRET_NAME}" "${AWS_REGION}"
7373
FSXN_PASSWORD="${SECRET_VALUE}"
7474
logMessage "Secret data retrieved successfully"
75-
7675
commandDescription="Install linux iSCSI packages"
7776
logMessage "${commandDescription}"
7877
yum install -y device-mapper-multipath iscsi-initiator-utils
7978
checkCommand "${commandDescription}"
8079
addUndoCommand "yum remove -y device-mapper-multipath iscsi-initiator-utils"
81-
82-
commandDescription="Set multisession replacment time from default 120 sec to 5 sec"
80+
commandDescription="Set multisession timeout from 120s to 5s"
8381
logMessage "${commandDescription}"
8482
sed -i 's/node.session.timeo.replacement_timeout = .*/node.session.timeo.replacement_timeout = 5/' /etc/iscsi/iscsid.conf; cat /etc/iscsi/iscsid.conf | grep node.session.timeo.replacement_timeout
8583
cat /etc/iscsi/iscsid.conf | grep "node.session.timeo.replacement_timeout = 5"
8684
checkCommand "${commandDescription}"
8785
addUndoCommand "sed -i 's/node.session.timeo.replacement_timeout = .*/node.session.timeo.replacement_timeout = 120/' /etc/iscsi/iscsid.conf;"
88-
8986
commandDescription="Start iscsi service"
9087
logMessage "${commandDescription}"
9188
systemctl enable iscsid
9289
systemctl start iscsid
9390
checkCommand "${commandDescription}"
94-
95-
# check if the service is running
91+
# check service status
9692
isIscsciServiceRunning=$(systemctl is-active --quiet iscsid.service && echo "1" || echo "0")
9793
if [ "$isIscsciServiceRunning" -eq 1 ]; then
9894
logMessage "iscsi service is running"
@@ -101,38 +97,31 @@ else
10197
logMessage "iscsi service is not running, aborting"
10298
./uninstall.sh
10399
fi
104-
105-
commandDescription="Set multipath configuration which allow automatic failover between yours file servers"
100+
commandDescription="Set multipath config for automatic failover"
106101
logMessage "${commandDescription}"
107102
mpathconf --enable --with_multipathd y
108103
checkCommand "${commandDescription}"
109104
addUndoCommand "mpathconf --disable"
110-
111-
# set the initiator name of your Linux host
105+
# set Linux host initiator name
112106
name=$(cat /etc/iscsi/initiatorname.iscsi)
113107
initiatorName="${name:14}"
114108
logMessage "initiatorName is: ${initiatorName}"
115-
116109
# Test connection to ONTAP
117110
logMessage "Testing connection to ONTAP."
118-
119111
versionResponse=$(curl -m $TIMEOUT -X GET -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/cluster?fields=version")
120112
if [[ "$versionResponse" == *"version"* ]]; then
121113
logMessage "Connection to ONTAP is successful."
122114
else
123115
logMessage "Connection to ONTAP failed, aborting."
124116
./uninstall.sh
125117
fi
126-
127-
# group name should be the hostname of the linux host
118+
# group name = hostname
128119
groupName=$(hostname)
129-
130120
iGroupResult=$(curl -m $TIMEOUT -X GET -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/protocols/san/igroups?svm.name=$SVM_NAME&name=$groupName&initiators.name=$initiatorName&protocol=iscsi&os_type=linux")
131121
initiatorExists=$(echo "${iGroupResult}" | jq '.num_records')
132-
133122
if [ "$initiatorExists" -eq 0 ]; then
134123
logMessage "Initiator ${initiatorName} with group ${groupName} does not exist, creating it."
135-
logMessage "Create initiator group for vserver: ${SVM_NAME} group name: ${groupName} and intiator name: ${initiatorName}"
124+
logMessage "Create initiator group for vserver: ${SVM_NAME} group: ${groupName} initiator: ${initiatorName}"
136125
createGroupResult=$(curl -m $TIMEOUT -X POST -u "$ONTAP_USER":"$FSXN_PASSWORD" -H "Content-Type: application/json" -k "https://$FSXN_ADMIN_IP/api/protocols/san/igroups" -d '{
137126
"protocol": "iscsi",
138127
"initiators": [
@@ -155,7 +144,7 @@ if [ "$initiatorExists" -eq 0 ]; then
155144
logMessage "Initiator group ${groupName} was not created, aborting"
156145
./uninstall.sh
157146
fi
158-
# Add undo command for iGroup creation
147+
# Add undo for iGroup
159148
addUndoCommand "curl -m $TIMEOUT -X DELETE -u \"$ONTAP_USER\":\"$FSXN_PASSWORD\" -k \"https://$FSXN_ADMIN_IP/api/protocols/san/igroups/$iGroupUuid\""
160149
else
161150
logMessage "Initiator ${initiatorName} with group ${groupName} already exists, skipping creation."
@@ -166,7 +155,7 @@ if [ -z "$instance_id" ]; then
166155
instance_id="unknown"
167156
fi
168157

169-
logMessage "Create volume for vserver: ${SVM_NAME} volume name: ${VOLUME_NAME} and size: ${VOLUME_SIZE}g"
158+
logMessage "Create volume: ${SVM_NAME} vol: ${VOLUME_NAME} size: ${VOLUME_SIZE}g"
170159
createVolumeResult=$(curl -m $TIMEOUT -X POST -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/storage/volumes" -d '{
171160
"name": "'$VOLUME_NAME'",
172161
"size": "'$VOLUME_SIZE'g",
@@ -194,7 +183,7 @@ if [ "$jobState" != "success" ]; then
194183
./uninstall.sh
195184
fi
196185

197-
# validate if volume was created successfully
186+
# validate volume creation
198187
volumeResult=$(curl -m $TIMEOUT -X GET -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/storage/volumes?name=${VOLUME_NAME}&svm.name=${SVM_NAME}")
199188
volumeUUid=$(echo "${volumeResult}" | jq -r '.records[] | select(.name == "'$VOLUME_NAME'" ) | .uuid')
200189
if [ -n "$volumeUUid" ]; then
@@ -205,7 +194,7 @@ else
205194
fi
206195
addUndoCommand "curl -m $TIMEOUT -X DELETE -u \"$ONTAP_USER\":\"$FSXN_PASSWORD\" -k \"https://$FSXN_ADMIN_IP/api/storage/volumes/${volumeUUid}\""
207196

208-
logMessage "Create iscsi lun for vserver: ${SVM_NAME} volume name: ${VOLUME_NAME} and lun name: ${LUN_NAME} and size: ${LUN_SIZE}g which is 90% of the volume size"
197+
logMessage "Create iscsi lun: ${SVM_NAME} vol: ${VOLUME_NAME} lun: ${LUN_NAME} size: ${LUN_SIZE}g (90% of volume)"
209198
createLunResult=$(curl -m $TIMEOUT -X POST -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/storage/luns" -d '{
210199
"name": "'/vol/${VOLUME_NAME}/$LUN_NAME'",
211200
"space": {
@@ -218,7 +207,7 @@ createLunResult=$(curl -m $TIMEOUT -X POST -u "$ONTAP_USER":"$FSXN_PASSWORD" -k
218207
"os_type": "linux"
219208
}')
220209
lunResult=$(curl -X GET -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/storage/luns?fields=uuid&name=/vol/${VOLUME_NAME}/$LUN_NAME")
221-
# Validate if LUN was created successfully
210+
# Validate LUN creation
222211
lunUuid=$(echo "${lunResult}" | jq -r '.records[] | select(.name == "'/vol/${VOLUME_NAME}/$LUN_NAME'" ) | .uuid')
223212
if [ -n "$lunUuid" ]; then
224213
logMessage "LUN ${LUN_NAME} was created successfully with UUID: ${lunUuid}"
@@ -229,8 +218,7 @@ fi
229218

230219
addUndoCommand "curl -m $TIMEOUT -X DELETE -u \"$ONTAP_USER\":\"$FSXN_PASSWORD\" -k \"https://$FSXN_ADMIN_IP/api/storage/luns/${lunUuid}\""
231220

232-
# The LUN ID integer is specific to the mapping, not to the LUN itself.
233-
# This is used by the initiators in the igroup as the Logical Unit Number. Use this value for the initiator when accessing the storage.
221+
# LUN ID is mapping-specific, used by initiators as Logical Unit Number
234222
logMessage "Create a mapping from the LUN you created to the igroup you created"
235223

236224
lunMapResult=$(curl -m $TIMEOUT -X POST -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/protocols/san/lun-maps" -d '{
@@ -258,7 +246,7 @@ fi
258246

259247
addUndoCommand "curl -m $TIMEOUT -X DELETE -u \"$ONTAP_USER\":\"$FSXN_PASSWORD\" -k \"https://$FSXN_ADMIN_IP/api/protocols/san/lun-maps?lun.name=/vol/${VOLUME_NAME}/${LUN_NAME}&igroup.name=${groupName}&svm.name=${SVM_NAME}\""
260248

261-
# The serial hex in needed for creating readable name for the block device.
249+
# Serial hex needed for readable block device name
262250
getLunSerialNumberResult=$(curl -m $TIMEOUT -X GET -u "$ONTAP_USER":"$FSXN_PASSWORD" -k "https://$FSXN_ADMIN_IP/api/storage/luns?fields=serial_number")
263251
serialNumber=$(echo "${getLunSerialNumberResult}" | jq -r '.records[] | select(.name == "'/vol/$VOLUME_NAME/$LUN_NAME'" ) | .serial_number')
264252
serialHex=$(echo -n "${serialNumber}" | xxd -p)
@@ -291,10 +279,9 @@ logMessage "Getting target initiator"
291279
targetInitiator=$(iscsiadm --mode discovery --op update --type sendtargets --portal $iscsi1IP | awk '{print $2}' | head -n 1)
292280
logMessage "Target initiator is: ${targetInitiator}"
293281

294-
# update the number of sessions to 8 (optional step)
282+
# update sessions to 8 (optional)
295283
#iscsiadm --mode node -T $targetInitiator --op update -n node.session.nr_sessions -v 8
296-
297-
# Log into the target initiators. Your iSCSI LUNs are presented as available disks
284+
# Login to target initiators - iSCSI LUNs presented as disks
298285
logMessage "Log into target initiator: ${targetInitiator}"
299286
iscsiadm --mode node -T $targetInitiator --login
300287
addUndoCommand "iscsiadm --mode node -T $targetInitiator --logout"
@@ -306,7 +293,7 @@ addUndoCommand "iscsiadm --mode node -T $targetInitiator --logout"
306293
# alias ${VOLUME_NAME}
307294
# }
308295
# }
309-
# Assign name to block device, this should be function that will get serial hex and device name
296+
# Assign block device name
310297
logMessage "Update /etc/multipath.conf file, Assign name to block device."
311298
cp /etc/multipath.conf /etc/multipath.conf_backup
312299

@@ -325,7 +312,7 @@ fi
325312
fileContent="$(cat $CONF)"
326313
logMessage "Updated /etc/multipath.conf file content: $fileContent"
327314

328-
commandDescription="Restart the multipathd service for the changes at: /etc/multipathd.conf will take effect."
315+
commandDescription="Restart multipathd for /etc/multipathd.conf changes"
329316
logMessage "${commandDescription}"
330317
systemctl restart multipathd.service
331318
checkCommand "${commandDescription}"
@@ -352,47 +339,64 @@ if [ ! -e "/dev/mapper/$VOLUME_NAME" ]; then
352339
fi
353340

354341
# Partition the LUN
355-
# mount the LUN on the Linux client
356-
# Create a directory directory_path as the mount point for your file system.
342+
# mount the LUN on Linux client
343+
# Create mount point directory
357344
directory_path=mnt
358345
mount_point=$VOLUME_NAME
359346

360-
commandDescription="Create a directory /${directory_path}/${mount_point} as the mount point for your file system"
347+
commandDescription="Create mount point /${directory_path}/${mount_point}"
361348
logMessage "${commandDescription}"
362349
mkdir /$directory_path/$mount_point
363350
checkCommand "${commandDescription}"
364351
addUndoCommand "rm -rf /$directory_path/$mount_point"
365-
366-
# volume_name = the friendly device name as we set it in the multipath.conf file
367-
commandDescription="Creating the file system for the new partition: /dev/mapper/${ALIAS}"
352+
# volume_name = friendly device name from multipath.conf
353+
commandDescription="Create file system for /dev/mapper/${ALIAS}"
368354
logMessage "${commandDescription}"
369355
mkfs.ext4 /dev/mapper/$ALIAS
370356
checkCommand "${commandDescription}"
371357

372-
commandDescription="Mount the file system using the following command."
358+
commandDescription="Mount the file system"
373359
logMessage "${commandDescription}"
374360
mount -t ext4 /dev/mapper/$ALIAS /$directory_path/$mount_point
375361
checkCommand "${commandDescription}"
376362
addUndoCommand "umount /$directory_path/$mount_point"
377-
378-
# verify read write
379-
# example: echo "test mount iscsci" > /mnt/myIscsi/testIscsi.txt
380-
commandDescription="Verify read write on the mounted file system"
363+
# verify read/write access
364+
commandDescription="Verify read/write access"
381365
logMessage "${commandDescription}"
382366
echo "test mount iscsci" > /$directory_path/$mount_point/testIscsi.txt
383367
cat /$directory_path/$mount_point/testIscsi.txt
384368
checkCommand "${commandDescription}"
385369
rm /$directory_path/$mount_point/testIscsi.txt
386370

387-
logMessage "Mounting the FSXn iSCSI volume was successful."
388-
389-
# Add the mount entry to /etc/fstab
390-
commandDescription="Add the mount entry to /etc/fstab"
371+
logMessage "FSXn iSCSI volume mount successful."
372+
# Add mount to /etc/fstab
373+
commandDescription="Add mount to /etc/fstab"
391374
logMessage "${commandDescription}"
392375
echo "/dev/mapper/$ALIAS /$directory_path/$mount_point ext4 defaults,_netdev 0 0" >> /etc/fstab
393376
checkCommand "${commandDescription}"
394377
addUndoCommand "sed -i '/\/dev\/mapper\/$ALIAS \/mnt\/$mount_point ext4 defaults,_netdev 0 0/d' /etc/fstab"
395-
# End of script
378+
379+
# Report usage
380+
logMessage "Report usage"
381+
logMessage "Attempting Lambda invoke"
382+
LAMBDA_PAYLOAD='{"ResourceProperties":{"Source":"Deploy_EC2_Wizard","Region":"'$AWS_REGION'"},"RequestType":"CLI"}'
383+
384+
# Try Lambda invoke
385+
invokeLambda
386+
if [ $? -ne 0 ] && grep -q "initializing" /home/ec2-user/lambda_error.log 2>/dev/null; then
387+
logMessage "Lambda initializing, retrying in 10s..."
388+
sleep 10
389+
invokeLambda
390+
fi
391+
392+
# Check final result
393+
if [ $? -eq 0 ]; then
394+
logMessage "Usage reporting completed successfully"
395+
else
396+
logMessage "Usage reporting failed"
397+
fi
398+
399+
# End
396400
logMessage "Script completed successfully."
397401

398402
rm -f uninstall.sh

0 commit comments

Comments
 (0)