Skip to content

Commit 74441bf

Browse files
authored
Update linux_userData.sh
use systemctl instead of service for iscsi operations
1 parent 79306fc commit 74441bf

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

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

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ VOL_SIZE=$(echo $VOLUME_SIZE | sed 's/.$//')
2929
LUN_SIZE=$(bc -l <<< "0.85*$VOL_SIZE" )
3030

3131
echo "# Uninstall file" >> uninstall.sh
32-
sudo chmod u+x uninstall.sh
32+
chmod u+x uninstall.sh
3333

3434
function getSecretValue() {
3535
secret_name=$1
@@ -69,26 +69,27 @@ logMessage "Secret data retrieved successfully"
6969

7070
commandDescription="Install linux iSCSI packages"
7171
logMessage "${commandDescription}"
72-
sudo yum install -y device-mapper-multipath iscsi-initiator-utils
72+
yum install -y device-mapper-multipath iscsi-initiator-utils
7373
checkCommand "${commandDescription}"
74-
addUndoCommand "sudo yum remove -y device-mapper-multipath iscsi-initiator-utils"
74+
addUndoCommand "yum remove -y device-mapper-multipath iscsi-initiator-utils"
7575

7676
commandDescription="Set multisession replacment time from default 120 sec to 5 sec"
7777
logMessage "${commandDescription}"
78-
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
78+
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
7979
checkCommand "${commandDescription}"
80-
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"
80+
addUndoCommand "sed -i 's/node.session.timeo.replacement_timeout = .*/node.session.timeo.replacement_timeout = 120/' /etc/iscsi/iscsid.conf; cat /etc/iscsi/iscsid.conf | grep node.session.timeo.replacement_timeout"
8181

8282
commandDescription="Start iscsi service"
8383
logMessage "${commandDescription}"
84-
sudo service iscsid start
84+
systemctl enable iscsid
85+
systemctl start iscsid
8586
checkCommand "${commandDescription}"
8687

8788
# check if the service is running
88-
isIscsciServiceRunning=$(sudo service iscsid status | grep "Active: active (running)" | wc -l)
89+
isIscsciServiceRunning=$(systemctl is-active --quiet iscsid.service && echo "1")
8990
if [ "$isIscsciServiceRunning" -eq 1 ]; then
9091
logMessage "iscsi service is running"
91-
addUndoCommand "sudo service iscsid stop"
92+
addUndoCommand "systemctl --now disable iscsid.service"
9293
else
9394
logMessage "iscsi service is not running, aborting"
9495
# now we have to rollback and exit
@@ -97,21 +98,21 @@ fi
9798

9899
commandDescription="Set multipath configuration which allow automatic failover between yours file servers"
99100
logMessage "${commandDescription}"
100-
sudo mpathconf --enable --with_multipathd y
101+
mpathconf --enable --with_multipathd y
101102
checkCommand "${commandDescription}"
102-
addUndoCommand "sudo mpathconf --disable"
103+
addUndoCommand "mpathconf --disable"
103104

104105
# set the initiator name of your Linux host
105-
name=$(sudo cat /etc/iscsi/initiatorname.iscsi)
106+
name=$(cat /etc/iscsi/initiatorname.iscsi)
106107
initiatorName="${name:14}"
107108
logMessage "initiatorName is: ${initiatorName}"
108109

109110
# Configure iSCSI on the FSx for ONTAP file system
110111
commandDescription="Install sshpass which will allow to connect FSXn using SSH"
111112
logMessage "${commandDescription}"
112-
sudo yum install -y sshpass
113+
yum install -y sshpass
113114
checkCommand "${commandDescription}"
114-
addUndoCommand "sudo yum remove -y sshpass"
115+
addUndoCommand "yum remove -y sshpass"
115116

116117
# Test connection to ONTAP
117118
commandDescription="Testing connection to ONTAP."
@@ -124,7 +125,7 @@ groupName=$(hostname)
124125

125126
commandDescription="Create initiator group for vserver: ${SVM_NAME} group name: ${groupName} and intiator name: ${initiatorName}"
126127

127-
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"}
128+
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")
128129
if [[ "$lunGroupresult" == *"There are no entries matching your query."* ]]; then
129130
logMessage "Initiator ${initiatorName} with group ${groupName} does not exist, creating it."
130131
logMessage "${commandDescription}"
@@ -194,25 +195,25 @@ fi
194195

195196
commandDescription="Discover the target iSCSI nodes, iscsi IP: ${iscsi1IP}"
196197
logMessage "${commandDescription}"
197-
sudo iscsiadm --mode discovery --op update --type sendtargets --portal $iscsi1IP
198+
iscsiadm --mode discovery --op update --type sendtargets --portal $iscsi1IP
198199
checkCommand "${commandDescription}"
199-
addUndoCommand "sudo iscsiadm --mode discovery --op delete --type sendtargets --portal $iscsi1IP"
200-
addUndoCommand "sudo iscsiadm --mode discovery --op delete --type sendtargets --portal $iscsi2IP"
200+
addUndoCommand "iscsiadm --mode discovery --op delete --type sendtargets --portal $iscsi1IP"
201+
addUndoCommand "iscsiadm --mode discovery --op delete --type sendtargets --portal $iscsi2IP"
201202

202203
logMessage "Getting target initiator"
203-
targetInitiator=$(sudo iscsiadm --mode discovery --op update --type sendtargets --portal $iscsi1IP | awk '{print $2}' | head -n 1)
204+
targetInitiator=$(iscsiadm --mode discovery --op update --type sendtargets --portal $iscsi1IP | awk '{print $2}' | head -n 1)
204205
logMessage "Target initiator is: ${targetInitiator}"
205206

206207
# update the number of sessions to 8 (optional step)
207-
#sudo iscsiadm --mode node -T $targetInitiator --op update -n node.session.nr_sessions -v 8
208+
#iscsiadm --mode node -T $targetInitiator --op update -n node.session.nr_sessions -v 8
208209

209210
# Log into the target initiators. Your iSCSI LUNs are presented as available disks
210211
logMessage "Log into target initiator: ${targetInitiator}"
211-
sudo iscsiadm --mode node -T $targetInitiator --login
212-
addUndoCommand "sudo iscsiadm --mode node -T $targetInitiator --logout"
212+
iscsiadm --mode node -T $targetInitiator --login
213+
addUndoCommand "iscsiadm --mode node -T $targetInitiator --logout"
213214

214215
# verify that dm-multipath has identified and merged the iSCSI sessions
215-
sudo multipath -ll
216+
multipath -ll
216217
device_name=fsxontap
217218

218219
# Add the following section to the /etc/multipath.conf file:
@@ -225,29 +226,29 @@ device_name=fsxontap
225226
# Assign name to block device, this should be function that will get serial hex and device name
226227
commandDescription="Update /etc/multipath.conf file, Assign name to block device."
227228
logMessage "${commandDescription}"
228-
sudo cp /etc/multipath.conf /etc/multipath.conf_backup
229+
cp /etc/multipath.conf /etc/multipath.conf_backup
229230

230231
SERIAL_HEX=$serialHex
231232
#ALIAS=$device_name
232233
ALIAS=$VOLUME_NAME
233234
CONF=/etc/multipath.conf
234-
sudo chmod o+rw $CONF
235-
sudo grep -q '^multipaths {' $CONF
235+
chmod o+rw $CONF
236+
grep -q '^multipaths {' $CONF
236237
UNCOMMENTED=$?
237238
if [ $UNCOMMENTED -eq 0 ]; then
238-
sudo sed -i '/^multipaths {/a\\tmultipath {\n\t\twwid 3600a0980'"${SERIAL_HEX}"'\n\t\talias '"${ALIAS}"'\n\t}\n' $CONF
239+
sed -i '/^multipaths {/a\\tmultipath {\n\t\twwid 3600a0980'"${SERIAL_HEX}"'\n\t\talias '"${ALIAS}"'\n\t}\n' $CONF
239240
else
240-
sudo printf "multipaths {\n\tmultipath {\n\t\twwid 3600a0980$SERIAL_HEX\n\t\talias $ALIAS\n\t}\n}" >> $CONF
241+
printf "multipaths {\n\tmultipath {\n\t\twwid 3600a0980$SERIAL_HEX\n\t\talias $ALIAS\n\t}\n}" >> $CONF
241242
fi
242243

243244
fileContent=$(cat $CONF)
244245
logMessage "Updated /etc/multipath.conf file content: $fileContent"
245246

246247
commandDescription="Restart the multipathd service for the changes at: /etc/multipathd.conf will take effect."
247248
logMessage "${commandDescription}"
248-
sudo systemctl restart multipathd.service
249+
systemctl restart multipathd.service
249250
checkCommand "${commandDescription}"
250-
addUndoCommand "sudo cp /etc/multipath.conf_backup /etc/multipath.conf"
251+
addUndoCommand "cp /etc/multipath.conf_backup /etc/multipath.conf"
251252
addUndoCommand "systemctl restart multipathd.service"
252253

253254
logMessage "Checking if the new partition exists."
@@ -278,25 +279,25 @@ mount_point=$VOLUME_NAME
278279

279280
commandDescription="Create a directory /${directory_path}/${mount_point} as the mount point for your file system"
280281
logMessage "${commandDescription}"
281-
sudo mkdir /$directory_path/$mount_point
282+
mkdir /$directory_path/$mount_point
282283
checkCommand "${commandDescription}"
283-
addUndoCommand "sudo rm -rf /$directory_path/$mount_point"
284+
addUndoCommand "rm -rf /$directory_path/$mount_point"
284285

285286
#check this command
286287
# volume_name=the frindly device name as we set it in the multipath.conf file
287288
commandDescription="Creating the file system for the new partition: /dev/mapper/${ALIAS}"
288289
logMessage "${commandDescription}"
289-
sudo mkfs.ext4 /dev/mapper/$ALIAS
290+
mkfs.ext4 /dev/mapper/$ALIAS
290291
checkCommand "${commandDescription}"
291292

292293
commandDescription="Mount the file system using the following command."
293294
logMessage "${commandDescription}"
294-
sudo mount -t ext4 /dev/mapper/$ALIAS /$directory_path/$mount_point
295+
mount -t ext4 /dev/mapper/$ALIAS /$directory_path/$mount_point
295296
checkCommand "${commandDescription}"
296-
addUndoCommand "sudo umount /$directory_path/$mount_point"
297+
addUndoCommand "umount /$directory_path/$mount_point"
297298

298299
username=$(whoami)
299-
sudo chown $username:$username /$directory_path/$mount_point
300+
chown $username:$username /$directory_path/$mount_point
300301

301302
# verify read write
302303
# example: echo "test mount iscsci" > /mnt/myIscsi/testIscsi.txt

0 commit comments

Comments
 (0)