Skip to content

Commit f35a634

Browse files
authored
Add files via upload
add 2 script, one for windows EC2 user data and one for Linux EC2 user data
1 parent 76b49da commit f35a634

File tree

2 files changed

+652
-0
lines changed

2 files changed

+652
-0
lines changed
Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
#!/bin/bash
2+
3+
# user data
4+
# Set the secret name and region
5+
SECRET_NAME=[Secret name has it been saved in AWS secret manager]
6+
AWS_REGION=[AWS region]
7+
8+
FSXN_PASSWORD=[Fsx admin password, e.g. fsxadmin123]
9+
FSXN_ADMIN_IP=[Fsx admin ip, e.g. 172.25.45.32]
10+
# Volume name
11+
VOLUME_NAME=[Fsx volume name, e.g. iscsiVol]
12+
# Volume size in GB
13+
VOLUME_SIZE=[volume size in GB, e.g 100g]
14+
# Default value is fsx, but you can change it to any other value according to yours FSx for ONTAP SVM name
15+
SVM_NAME=fsx
16+
# end - user data
17+
18+
19+
min=100
20+
max=999
21+
LUN_NAME=${VOLUME_NAME}_$(($RANDOM%($max-$min+1)+$min))
22+
23+
# defaults
24+
ONTAP_USER=fsxadmin
25+
LOG_FILE=/home/ec2-user/install.log
26+
27+
VOL_SIZE=$(echo $VOLUME_SIZE | sed 's/.$//')
28+
LUN_SIZE=$(bc -l <<< "0.85*$VOL_SIZE" )
29+
30+
echo "# Uninstall file" >> uninstall.sh
31+
sudo chmod u+x uninstall.sh
32+
33+
function getSecretValue() {
34+
secret_name=$1
35+
aws_region=$2
36+
SECRET_VALUE=$(aws secretsmanager get-secret-value \
37+
--secret-id "$secret_name" \
38+
--region "$aws_region" \
39+
--query 'SecretString' \
40+
--output text)
41+
42+
if [ $? -ne 0 ]; then
43+
echo "Failed to retrieve the secret: $secret_name, Aborting."
44+
exit 1
45+
fi
46+
}
47+
48+
function fsxnSshCommand(){
49+
command = $1
50+
sshpass -p $FSXN_PASSWORD ssh -o StrictHostKeyChecking=no fsxadmin@$FSXN_ADMIN_IP $command
51+
}
52+
53+
logMessage() {
54+
echo "$(date) - $1" >> $LOG_FILE
55+
}
56+
57+
checkCommand() {
58+
if [ $? -ne 0 ]; then
59+
logMessage "$1 failed. Aborting."
60+
./uninstall.sh
61+
exit 1
62+
fi
63+
}
64+
65+
addUndoCommand() {
66+
sed -i "1i$1" uninstall.sh
67+
}
68+
69+
logMessage "Get secret data"
70+
getSecretValue "${SECRET_NAME}" "${AWS_REGION}"
71+
FSXN_PASSWORD=$SECRET_VALUE
72+
logMessage "Secret data retrieved successfully"
73+
74+
commandDescription="Install linux iSCSI packages"
75+
logMessage "${commandDescription}"
76+
sudo yum install -y device-mapper-multipath iscsi-initiator-utils
77+
checkCommand "${commandDescription}"
78+
addUndoCommand "sudo yum remove -y device-mapper-multipath iscsi-initiator-utils"
79+
80+
commandDescription="Set multisession replacment time from default 120 sec to 5 sec"
81+
logMessage "${commandDescription}"
82+
sudo sed -i 's/node.session.timeo.replacement_timeout = .*/node.session.timeo.replacement_timeout = 5/' /etc/iscsi/iscsid.conf; sudo cat /etc/iscsi/iscsid.conf | grep node.session.timeo.replacement_timeout
83+
checkCommand "${commandDescription}"
84+
addUndoCommand "sudo sed -i 's/node.session.timeo.replacement_timeout = .*/node.session.timeo.replacement_timeout = 120/' /etc/iscsi/iscsid.conf; sudo cat /etc/iscsi/iscsid.conf | grep node.session.timeo.replacement_timeout"
85+
86+
commandDescription="Start iscsi service"
87+
logMessage "${commandDescription}"
88+
sudo service iscsid start
89+
checkCommand "${commandDescription}"
90+
91+
# check if the service is running
92+
isIscsciServiceRunning=$(sudo service iscsid status | grep "Active: active (running)" | wc -l)
93+
if [ "$isIscsciServiceRunning" -eq 1 ]; then
94+
logMessage "iscsi service is running"
95+
addUndoCommand "sudo service iscsid stop"
96+
else
97+
logMessage "iscsi service is not running, aborting"
98+
# now we have to rollback and exit
99+
./uninstall.sh
100+
fi
101+
102+
commandDescription="Set multipath configuration which allow automatic failover between yours file servers"
103+
logMessage "${commandDescription}"
104+
sudo mpathconf --enable --with_multipathd y
105+
checkCommand "${commandDescription}"
106+
addUndoCommand "sudo mpathconf --disable"
107+
108+
# set the initiator name of your Linux host
109+
name=$(sudo cat /etc/iscsi/initiatorname.iscsi)
110+
initiatorName="${name:14}"
111+
logMessage "initiatorName is: ${initiatorName}"
112+
113+
# Configure iSCSI on the FSx for ONTAP file system
114+
commandDescription="Install sshpass which will allow to connect FSXn using SSH"
115+
logMessage "${commandDescription}"
116+
sudo yum install -y sshpass
117+
checkCommand "${commandDescription}"
118+
addUndoCommand "sudo yum remove -y sshpass"
119+
120+
# Test connection to ONTAP
121+
commandDescription="Testing connection to ONTAP."
122+
logMessage "${commandDescription}"
123+
sshpass -p $FSXN_PASSWORD ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "version"
124+
checkCommand "${commandDescription}"
125+
126+
# group name should be the hostname of the linux host
127+
groupName=$(hostname)
128+
129+
commandDescription="Create initiator group for vserver: ${SVM_NAME} group name: ${groupName} and intiator name: ${initiatorName}"
130+
131+
lunGroupresult=${sshpass -p $FSXN_PASSWORD ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "lun igroup show -vserver $SVM_NAME -igroup $groupName -initiator $initiatorName -protocol iscsi -ostype linux"}
132+
if [[ "$lunGroupresult" == *"There are no entries matching your query."* ]]; then
133+
logMessage "Initiator ${initiatorName} with group ${groupName} does not exist, creating it."
134+
logMessage "${commandDescription}"
135+
sshpass -p $FSXN_PASSWORD ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "lun igroup create -vserver $SVM_NAME -igroup $groupName -initiator $initiatorName -protocol iscsi -ostype linux"
136+
checkCommand "${commandDescription}"
137+
addUndoCommand "sshpass -p ${FSXN_PASSWORD} ssh -o StrictHostKeyChecking=no ${ONTAP_USER}@${FSXN_ADMIN_IP} lun igroup delete -vserver ${SVM_NAME} -igroup ${groupName} -force"
138+
else
139+
logMessage "Initiator ${initiatorName} with group ${groupName} already exists, skipping creation."
140+
fi
141+
142+
# confirm that igroup was created
143+
isInitiatorGroupCreadted=$(sshpass -p $FSXN_PASSWORD ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "lun igroup show -igroup $groupName -protocol iscsi" | grep $groupName | wc -l)
144+
if [ "$isInitiatorGroupCreadted" -eq 1 ]; then
145+
logMessage "Initiator group ${groupName} was created"
146+
else
147+
logMessage "Initiator group ${groupName} was not created, aborting"
148+
# now we have to rollback and exit
149+
./uninstall.sh
150+
fi
151+
152+
commandDescription="Create volume for vserver: ${SVM_NAME} volume name: ${VOLUME_NAME} and size: ${VOLUME_SIZE}"
153+
logMessage "${commandDescription}"
154+
sshpass -p $FSXN_PASSWORD ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "volume create -vserver $SVM_NAME -volume $VOLUME_NAME -aggregate aggr1 -size $VOLUME_SIZE -state online"
155+
checkCommand "${commandDescription}"
156+
addUndoCommand "sshpass -p ${FSXN_PASSWORD} ssh -o StrictHostKeyChecking=no ${ONTAP_USER}@${FSXN_ADMIN_IP} volume delete -vserver ${SVM_NAME} -volume ${VOLUME_NAME} -force"
157+
158+
commandDescription="Create iscsi lun for vserver: ${SVM_NAME} volume name: ${VOLUME_NAME} and lun name: ${LUN_NAME} and size: ${LUN_SIZE}g"
159+
logMessage "${commandDescription}"
160+
sshpass -p $FSXN_PASSWORD ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "lun create -vserver $SVM_NAME -path /vol/$VOLUME_NAME/$LUN_NAME -size "${LUN_SIZE}g" -ostype linux -space-allocation enabled"
161+
checkCommand "${commandDescription}"
162+
addUndoCommand "sshpass -p ${FSXN_PASSWORD} ssh -o StrictHostKeyChecking=no ${ONTAP_USER}@${FSXN_ADMIN_IP} lun delete -vserver ${SVM_NAME} -path /vol/${VOLUME_NAME}/${LUN_NAME} -force"
163+
164+
# Create a mapping from the LUN you created to the igroup you created
165+
# The LUN ID integer is specific to the mapping, not to the LUN itself.
166+
# This is used by the initiators in the igroup as the Logical Unit Number use this value for the initiator when accessing the storage.
167+
commandDescription="Create a mapping from the LUN you created to the igroup you created"
168+
logMessage "${commandDescription}"
169+
lun_id=0
170+
sshpass -p $FSXN_PASSWORD ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "lun mapping create -vserver $SVM_NAME -path /vol/$VOLUME_NAME/$LUN_NAME -igroup $groupName -lun-id 0"
171+
checkCommand "${commandDescription}"
172+
173+
commandDescription="Validate the lun mapping was created"
174+
logMessage "${commandDescription}"
175+
serialHex=$(sshpass -p $FSXN_PASSWORD ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "lun show -path /vol/$VOLUME_NAME/$LUN_NAME -fields state,mapped,serial-hex" | grep $SVM_NAME | awk '{print $3}')
176+
if [ -n "$serialHex" ]; then
177+
logMessage "Lun mapping was created"
178+
else
179+
logMessage "Lun mapping was not created, aborting"
180+
addUndoCommand "sshpass -p ${FSXN_PASSWORD} ssh -o StrictHostKeyChecking=no ${ONTAP_USER}@${FSXN_ADMIN_IP} lun mapping delete -vserver ${SVM_NAME} -path /vol/${VOLUME_NAME}/${LUN_NAME} -igroup ${groupName}"
181+
fi
182+
183+
# The serail hex in needed for creating readable name for the block device.
184+
commandDescription="Get the iscsi interface addresses for the svm ${SVM_NAME}"
185+
logMessage "${commandDescription}"
186+
iscsi1IP=$(sshpass -p $FSXN_PASSWORD ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "network interface show -vserver $SVM_NAME" | grep -e iscsi_1 | awk '{print $3}')
187+
iscsi2IP=$(sshpass -p $FSXN_PASSWORD ssh -o StrictHostKeyChecking=no $ONTAP_USER@$FSXN_ADMIN_IP "network interface show -vserver $SVM_NAME" | grep -e iscsi_2 | awk '{print $3}')
188+
189+
if [ -n "$i$iscsi1IP" ] && [ -n "$iscsi2IP" ]; then
190+
iscsi1IP=$(echo ${iscsi1IP%/*})
191+
iscsi2IP=$(echo ${iscsi2IP%/*})
192+
logMessage "iscsi interface addresses for the svm ${SVM_NAME} are: ${iscsi1IP} and ${iscsi2IP}"
193+
else
194+
logMessage "iscsi interface addresses for the svm ${SVM_NAME} are not available, aborting"
195+
# now we have to rollback and exit
196+
./uninstall.sh
197+
fi
198+
199+
commandDescription="Discover the target iSCSI nodes, iscsi IP: ${iscsi1IP}"
200+
logMessage "${commandDescription}"
201+
sudo iscsiadm --mode discovery --op update --type sendtargets --portal $iscsi1IP
202+
checkCommand "${commandDescription}"
203+
addUndoCommand "sudo iscsiadm --mode discovery --op delete --type sendtargets --portal $iscsi1IP"
204+
addUndoCommand "sudo iscsiadm --mode discovery --op delete --type sendtargets --portal $iscsi2IP"
205+
206+
logMessage "Getting target initiator"
207+
targetInitiator=$(sudo iscsiadm --mode discovery --op update --type sendtargets --portal $iscsi1IP | awk '{print $2}' | head -n 1)
208+
logMessage "Target initiator is: ${targetInitiator}"
209+
210+
# update the number of sessions to 8 (optional step)
211+
#sudo iscsiadm --mode node -T $targetInitiator --op update -n node.session.nr_sessions -v 8
212+
213+
# Log into the target initiators. Your iSCSI LUNs are presented as available disks
214+
logMessage "Log into target initiator: ${targetInitiator}"
215+
sudo iscsiadm --mode node -T $targetInitiator --login
216+
addUndoCommand "sudo iscsiadm --mode node -T $targetInitiator --logout"
217+
218+
# verify that dm-multipath has identified and merged the iSCSI sessions
219+
sudo multipath -ll
220+
device_name=fsxontap
221+
222+
# Add the following section to the /etc/multipath.conf file:
223+
# multipaths {
224+
# multipath {
225+
# wwid 3600a0980${serialHex}
226+
# alias ${device_name}
227+
# }
228+
# }
229+
# Assign name to block device, this should be function that will get serial hex and device name
230+
commandDescription="Update /etc/multipath.conf file, Assign name to block device."
231+
logMessage "${commandDescription}"
232+
sudo cp /etc/multipath.conf /etc/multipath.conf_backup
233+
234+
SERIAL_HEX=$serialHex
235+
#ALIAS=$device_name
236+
ALIAS=$VOLUME_NAME
237+
CONF=/etc/multipath.conf
238+
sudo chmod o+rw $CONF
239+
sudo grep -q '^multipaths {' $CONF
240+
UNCOMMENTED=$?
241+
if [ $UNCOMMENTED -eq 0 ]; then
242+
sudo sed -i '/^multipaths {/a\\tmultipath {\n\t\twwid 3600a0980'"${SERIAL_HEX}"'\n\t\talias '"${ALIAS}"'\n\t}\n' $CONF
243+
else
244+
sudo printf "multipaths {\n\tmultipath {\n\t\twwid 3600a0980$SERIAL_HEX\n\t\talias $ALIAS\n\t}\n}" >> $CONF
245+
fi
246+
247+
fileContent=$(cat $CONF)
248+
logMessage "Updated /etc/multipath.conf file content: $fileContent"
249+
250+
commandDescription="Restart the multipathd service for the changes at: /etc/multipathd.conf will take effect."
251+
logMessage "${commandDescription}"
252+
sudo systemctl restart multipathd.service
253+
checkCommand "${commandDescription}"
254+
addUndoCommand "sudo cp /etc/multipath.conf_backup /etc/multipath.conf"
255+
addUndoCommand "systemctl restart multipathd.service"
256+
257+
logMessage "Checking if the new partition exists."
258+
timeout=90
259+
interval=5
260+
elapsed=0
261+
262+
while [ $elapsed -lt $timeout ]; do
263+
if [ -e "/dev/mapper/$VOLUME_NAME" ]; then
264+
logMessage "The device $VOLUME_NAME exists."
265+
break
266+
fi
267+
sleep $interval
268+
elapsed=$((elapsed + interval))
269+
done
270+
if [ ! -e "/dev/mapper/$VOLUME_NAME" ]; then
271+
logMessage "The device $VOLUME_NAME does not exists. Exiting."
272+
./uninstall.sh
273+
exit 1
274+
fi
275+
276+
# Partition the LUN
277+
# mount the LUN on the Linux client
278+
279+
# Create a directory directory_path as the mount point for your file system.
280+
directory_path=mnt
281+
mount_point=$VOLUME_NAME
282+
283+
commandDescription="Create a directory /${directory_path}/${mount_point} as the mount point for your file system"
284+
logMessage "${commandDescription}"
285+
sudo mkdir /$directory_path/$mount_point
286+
checkCommand "${commandDescription}"
287+
addUndoCommand "sudo rm -rf /$directory_path/$mount_point"
288+
289+
#check this command
290+
# volume_name=the frindly device name as we set it in the multipath.conf file
291+
commandDescription="Creating the file system for the new partition: /dev/mapper/${ALIAS}"
292+
logMessage "${commandDescription}"
293+
sudo mkfs.ext4 /dev/mapper/$ALIAS
294+
checkCommand "${commandDescription}"
295+
296+
commandDescription="Mount the file system using the following command."
297+
logMessage "${commandDescription}"
298+
sudo mount -t ext4 /dev/mapper/$ALIAS /$directory_path/$mount_point
299+
checkCommand "${commandDescription}"
300+
addUndoCommand "sudo umount /$directory_path/$mount_point"
301+
302+
username=$(whoami)
303+
sudo chown $username:$username /$directory_path/$mount_point
304+
305+
# verify read write
306+
# example: echo "test mount iscsci" > /mnt/myIscsi/testIscsi.txt
307+
echo "test mount iscsci" > /$directory_path/$mount_point/testIscsi.txt
308+
cat /$directory_path/$mount_point/testIscsci.txt
309+
310+
logMessage "Mounting the FSXn iSCSI volume was successful."
311+
312+
rm -f uninstall.sh

0 commit comments

Comments
 (0)