-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig-iscsi-storage.sh
executable file
·331 lines (248 loc) · 9.63 KB
/
config-iscsi-storage.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/bin/bash
echo
echo "$(tput setaf 5)####### VERSIONING & ATTRIBUTION #######$(tput sgr 0)"
echo
echo '# Script Author: Terrence Houlahan, Linux & Network Engineer F1Linux.com'
echo '# Author Site: http://www.F1Linux.com'
echo
echo '# Script Version: 1.11.05'
echo '# Script Date: 20240408'
echo
echo '# These scripts and others by the author can be found at:'
echo
echo ' https://github.com/f1linux'
echo
echo
echo "$(tput setaf 5)####### LICENSE: GPL Version 3 #######$(tput sgr 0)"
echo "$(tput setaf 5)# Copyright (C) 2021 Terrence Houlahan$(tput sgr 0)"
echo
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, [see](https://www.gnu.org/licenses/)
# Full LICENSE found [HERE](./LICENSE)
####### INSTRUCTIONS #######
# NOTE: This Script MUST be executed *BEFORE* "config-iscsi-storage-mounts.sh" which is
# used to configure the auto-mounting of the iSCSI disks configured by THIS script!
# If loading multiple LUNs, then change the "IQNTARGET" variable for each LUN and re-execute this script.
# Since Open-iSCSI only allows one CHAP Username & CHAP Password, those variables should not require changing
# for other LUNs you're configuring with this script.
# STEP 1: Edit "Variables" section below.
# STEP 2: Execute script as 'ubuntu' user:
# cd /home/ubuntu/
# sudo ./config-iscsi-storage.sh
# STEP 3: Partition and format the iSCSI disk after LUN connected.
# This is a manual task however not done with any of the scripts.
# The Instructions on how to accomplish this are printed at the
# completion of this script in 'NEXT STEPS'
####### SET VARIABLES #######
# Host exposing the LUNs:
STORAGEIP='192.168.1.27'
# Get this value from the storage host exposing the LUN:
IQNTARGET=''
# WARNING SYNOLOGY USERS: Uname must be between 1-12 characters or connections will fail with an auth error despite matching on both sides
CHAPUSERNAME='HOST3'
# WARNING SYNOLOGY USERS: Passwd must be between 12-16 characters or connections will fail with an auth error despite matching on both sides
CHAPPASSWORD='CHAPpasswd'
################################
# Exit if script not executed with sudo
if [ `id -u` -ne 0 ]; then
echo "Please execute script as root or using sudo!"
exit
fi
echo
echo "$(tput setaf 5)####### CHECK OS: #######$(tput sgr 0)"
echo
if [[ $(lsb_release -d | awk '{print $2}') != 'Ubuntu' ]]; then
echo 'This script optimized for Ubuntu.'
echo 'Inconsistent results might be achieved using another Debian derivative,'
echo 'or if using RHEL or a derivative of it might break in places.'
else
echo 'NOTE: This script was developed on Ubuntu 20.04 LTS.'
echo 'Using earlier- or later- versions could produce inconsistent results.'
fi
echo
echo "$(tput setaf 5)####### INSTALL OPEN-ISCSI #######$(tput sgr 0)"
echo
if [[ $(dpkg -l | grep "^ii open-iscsi[[:space:]]") = '' ]]; then
until apt-get -y install open-iscsi
do
echo
echo "Package $(tput setaf 3)open-iscsi $(tput sgr 0)not found"
echo
echo "Have you executed this script with sudo?"
echo
break
done
elif [[ $(dpkg -l | grep "^ii $i[[:space:]]") = $(dpkg -l | grep "^ii $i[[:space:]]") ]]; then
echo "Package open-iscsi already installed"
fi
if [[ $(lsb_release -r | awk '{print $2}' | cut -d '.' -f1)='22' ]]; then
until apt-get -y install linux-modules-extra-$(uname -r)
do
echo
echo "Package $(tput setaf 3)linux-modules-extra-$(uname -r) $(tput sgr 0)not found"
echo
echo "Have you executed this script with sudo?"
echo
break
done
fi
echo
echo "$(tput setaf 5)####### CONFIG OPEN-ISCSI #######$(tput sgr 0)"
echo
sed -i 's/#node.session.auth.authmethod = CHAP/node.session.auth.authmethod = CHAP/' /etc/iscsi/iscsid.conf
sed -i "s/#node.session.auth.username = username/node.session.auth.username = $CHAPUSERNAME/" /etc/iscsi/iscsid.conf
sed -i "s/#node.session.auth.password = password/node.session.auth.password = $CHAPPASSWORD/" /etc/iscsi/iscsid.conf
systemctl restart iscsid.service
systemctl enable iscsid.service
echo
echo "$(tput setaf 5)####### CREATE SYSTEMD SERVICE TO CONNECT/DISCONNECT LUN #######$(tput sgr 0)"
echo
if [ ! -d /root/scripts ]; then
mkdir /root/scripts
fi
echo "$(tput setaf 5)# CREATE CONNECTION SCRIPT: connect-luns.sh$(tput sgr 0)"
echo
if [ ! -f /root/scripts/connect-luns.sh ]; then
cat <<EOF> /root/scripts/connect-luns.sh
#!/bin/bash
# Use of "sendtargets" necessary to wake up the Synology Storage Host:
iscsiadm -m discovery -t sendtargets -p $STORAGEIP
# The iscsiadm command to CONNECT the LUN lives in this file
iscsiadm -m node -T $IQNTARGET -p $STORAGEIP:3260 --login
EOF
chmod 700 /root/scripts/connect-luns.sh
chown root:root /root/scripts/connect-luns.sh
else
echo "iscsiadm -m node -T $IQNTARGET -p $STORAGEIP:3260 --login" >> /root/scripts/connect-luns.sh
fi
echo "$(tput setaf 5)# CREATE DISCONNECTION SCRIPT: disconnect-luns.sh$(tput sgr 0)"
echo
if [ ! -f /root/scripts/disconnect-luns.sh ]; then
cat <<EOF> /root/scripts/disconnect-luns.sh
#!/bin/bash
# The iscsiadm command to DISCONNECT the LUN lives in this file
iscsiadm -m node -T $IQNTARGET -p $STORAGEIP:3260, 1 -u
EOF
chmod 700 /root/scripts/disconnect-luns.sh
chown root:root /root/scripts/disconnect-luns.sh
else
echo "iscsiadm -m node -T $IQNTARGET -p $STORAGEIP:3260, 1 -u" >> /root/scripts/disconnect-luns.sh
fi
echo "$(tput setaf 5)# CREATE SYSTEMD SERVICE: connect-luns.service$(tput sgr 0)"
echo
if [ ! -f /etc/systemd/system/connect-luns.service ]; then
cat <<EOF> /etc/systemd/system/connect-luns.service
[Unit]
Description=Connect iSCSI LUN
Documentation=https://github.com/f1linux/iscsi-automount
Requires=network-online.target
#After=
DefaultDependencies=no
[Service]
User=root
Group=root
Type=oneshot
RemainAfterExit=true
ExecStart=/root/scripts/connect-luns.sh "Connecting LUN"
StandardOutput=journal
[Install]
WantedBy=multi-user.target
EOF
chmod 644 /etc/systemd/system/connect-luns.service
systemctl daemon-reload
systemctl enable connect-luns.service
systemctl start connect-luns.service
fi
echo
echo "$(tput setaf 5)####### LOCATE iSCSI TARGETS #######$(tput sgr 0)"
echo
iscsiadm -m discovery -t sendtargets -p $STORAGEIP
echo
echo
echo
echo "$(tput setaf 5)####### LOCATE ISCSI DISK #######$(tput sgr 0)"
echo
echo "$(tput setaf 5)# Output of 'fdisk-l'$(tput sgr 0)"
echo
fdisk -l
echo
echo
echo "$(tput setaf 5)####### NEXT STEPS: #######$(tput sgr 0)"
echo
echo 'STEP 1: sudo systemctl reboot'
echo
echo 'STEP 2: Check the LUNs are connect to this host:'
echo
echo ' iscsiadm -m session'
echo
echo 'STEP 3: Find the iSCSCI disk in the output above and then partition it,'
echo ' ie: fdisk /dev/sdX where "X" is the letter of the iSCSI disk'
echo
echo 'If iSCSI block device not present in output of fdisk -l then skip to *TROUBLESHOOTING* section below'
echo 'Otherwise then proceed to STEP 2'
echo
echo 'STEP 4: Partition each new LUN'
echo ' sudo fdisk /dev/sdX'
echo ' (n) Create new partition'
echo ' (p) Choose Primary partition'
echo ' Accept all remaining default values'
echo ' (w) Write to save changes and exit'
echo ' Note: Default partition type is Linux- no need to set the value'
echo
echo 'STEP 5: Format the iSCSI disk with a filesystem'
echo ' ie: mkfs.ext4 /dev/sdX1 where the iSCSI disk is /dev/sdX'
echo ' Pls note that there is a "1" appended to the block device /dev/sdX1'
echo
echo 'STEP 6: Execute script config-iscsi-storage-mounts.sh which configures the'
echo ' auto-mounting the newly formatted iSCSI disks on boot'
echo
echo
echo "$(tput setaf 5)####### TROUBLESHOOTING: #######$(tput sgr 0)"
echo
echo
echo 'If you do NOT see the expected iSCSI block device check to see if the LUN is connected:'
echo
echo 'Output of * iscsiadm -m session * command below'
echo
iscsiadm -m session
echo
echo 'If no output above or the LUN is missing then the block device is not connected to system'
echo
echo
echo 'Review output of * systemctl status connect-luns.service * below for errors'
echo
systemctl status connect-luns.service
echo
echo 'If Error * iscsiadm: initiator reported error (24 - iSCSI login failed due to authorization failure) * then:'
echo
echo 'STEP 1: Clear Open-iSCSI cached login credentials:'
echo
echo ' sudo rm -rf /etc/iscsi/nodes;sudo rm -rf /etc/iscsi/send_targets'
echo
echo 'STEP 2: Verify CHAP username & Password in storage device both match Open-iSCSI credentials and do not exceed the allowed chars in:'
echo
echo ' /etc/iscsi/iscsid.conf'
echo ' /etc/iscsi/initiatorname.iscsi'
echo
echo 'SYNOLOGY USERS: Check CHAP uname between 1-12 chars and CHAP passwd between 12-16 chars or will fail despite matching on both sides'
echo
echo 'STEP 3: Fix any errors and execute:'
echo
echo ' sudo systemctl restart iscsid.service'
echo ' sudo systemctl restart connect-luns.service'
echo ' sudo iscsiadm -m session'
echo
echo 'STEP 4: If your LUN is now connected successfully then execute:'
echo
echo ' sudo fdisk -l'
echo
echo 'Find your iSCSI block device and complete STEPS 1-3 in the * NEXT STEPS * section above this troubleshooting section'
echo