-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig-iscsi-storage-mounts.sh
executable file
·203 lines (143 loc) · 6.04 KB
/
config-iscsi-storage-mounts.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
#!/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 #######
# If auto-mounting multiple LUNs, then change- at a minimum- the "ISCSIDEVICE" variable for each LUN and re-execute this script.
# This script mounts an already formatted iSCSI LUN to a folder in the path '/mnt/' per FHS guidance
# with an arbitrary name which is specified in a variable.
# STEP 1: First execute 'sudo ./config-iscsi-storage.sh' to connect the formatted iSCSI block device to the host.
# STEP 2: Modify variables in "SET VARIABLES" section below
# STEP 3: Execute this script as the 'ubuntu' user:
# sudo ./config-iscsi-storage-mounts.sh
####### SET VARIABLES #######
# Default value 'sda1' works if you load a single LUN on a Raspberry Pi partitioned by accepting fdisk default options.
# The first LUN appears in 'fdisk -l' as DISK 'sda' and 2nd LUN as DISK 'sdb' on your Pi.
# If you chose default values when partitioning the iSCSI * DISK * then 'fdisk -l' shows
# * DEVICES * '/dev/sda1' for LUN1 and '/dev/sdb1' for LUN2.
# Do 'fdisk -l' if unsure how the device is specified
ISCSIDEVICE='sda1'
# The script will create the folder with the name supplied in "ISCSIDISKMOUNTFOLDER" variable
# in the path /mnt. Therefore do NOT manually create the folder the LUN is mounted to.
#
# NAMING REQUIREMENTS: Do NOT specify a folder name with a hypnen in folder name that the LUN will be mounted on.
# This will cause the SystemD mount to fail.
# ie: "logsproxy" is a valid name and WILL work but "logs-proxy" will NOT and cause the mount to fail.
#
ISCSIDISKMOUNTFOLDER='logs'
# Filesystem type the LUN was formatted for which is supplied in the 'Type' field below
FILESYSTEM='ext4'
# SystemD mount file comment:
# Below variable sets "Description" field in the file that starts the mount.
MOUNTDESCRIPTION='Persistent Data living on iSCSI LUN'
####### EDIT BELOW WITH CAUTION #######
## NOTE: Most settings below show work out of the box
# Exit if script not executed with sudo
if [ `id -u` -ne 0 ]; then
echo
echo " Please re-execute script using sudo!"
echo
exit
fi
# Test if block device has a filesystem and if not exit (no uuid found)
if [ "$(ls -al /dev/disk/by-uuid | grep $ISCSIDEVICE | awk '{print $9}')" = '' ]; then
echo
echo " Please format block device: 'mkfs.ext4 /dev/$ISCSIDEVICE'"
echo " Script will now exit"
echo
exit
fi
echo "$(tput setaf 5)####### CHECK IF LUN CONNECTED #######$(tput sgr 0)"
echo
echo "$# Check if a LUN is connected and if not exit the script with a notification to connect it first:$(tput sgr 0)"
echo
# NOTE: 'iscsiadm' directs output to stderr so we need to redirect to stdout '2>&1' or our test will fail
if [[ $(iscsiadm -m session 2>&1) = 'iscsiadm: No active sessions.' ]]; then
echo
echo 'No LUNs connected.'
echo 'Execute "config-iscsi-storage.sh" script '
echo 'Script exiting'
exit
else
echo 'Connected LUN found: Script will continue'
echo
fi
echo "$(tput setaf 5)####### CREATE SYSTEMD MOUNT FOR LUN #######$(tput sgr 0)"
echo
echo "$(tput setaf 5)# Create directory /mnt/$ISCSIDISKMOUNTFOLDER for the iSCSI device to mount to$(tput sgr 0)"
if [ ! -d /mnt/$ISCSIDISKMOUNTFOLDER ]; then
mkdir /mnt/$ISCSIDISKMOUNTFOLDER
chmod 770 /mnt/$ISCSIDISKMOUNTFOLDER
fi
echo "$(tput setaf 5)# Create mnt-$ISCSIDISKMOUNTFOLDER.mount$(tput sgr 0)"
echo
if [ ! -f /etc/systemd/system/mnt-$ISCSIDISKMOUNTFOLDER.mount ]; then
cat <<EOF> /etc/systemd/system/mnt-$ISCSIDISKMOUNTFOLDER.mount
[Unit]
Description=$MOUNTDESCRIPTION
After=connect-luns.service
DefaultDependencies=no
[Mount]
What=/dev/disk/by-uuid/$(ls -al /dev/disk/by-uuid | grep $ISCSIDEVICE | awk '{print $9}')
Where=/mnt/$ISCSIDISKMOUNTFOLDER
Type=$FILESYSTEM
StandardOutput=journal
[Install]
WantedBy=multi-user.target
EOF
chown root:root /etc/systemd/system/mnt-$ISCSIDISKMOUNTFOLDER.mount
chmod 644 /etc/systemd/system/mnt-$ISCSIDISKMOUNTFOLDER.mount
systemctl daemon-reload
systemctl enable mnt-$ISCSIDISKMOUNTFOLDER.mount
sudo systemctl start mnt-$ISCSIDISKMOUNTFOLDER.mount
else
echo "'/etc/systemd/system/mnt-$ISCSIDISKMOUNTFOLDER.mount' already exists"
fi
echo "$(tput setaf 5)# Create mnt-$ISCSIDISKMOUNTFOLDER.automount$(tput sgr 0)"
echo
if [ ! -f /etc/systemd/system/mnt-$ISCSIDISKMOUNTFOLDER.automount ]; then
cat <<EOF> /etc/systemd/system/mnt-$ISCSIDISKMOUNTFOLDER.automount
[Unit]
Description=$MOUNTDESCRIPTION
Requires=network-online.target
#After=
[Automount]
Where=/mnt/$ISCSIDISKMOUNTFOLDER
[Install]
WantedBy=multi-user.target
EOF
chown root:root /etc/systemd/system/mnt-$ISCSIDISKMOUNTFOLDER.automount
chmod 644 /etc/systemd/system/mnt-$ISCSIDISKMOUNTFOLDER.automount
else
echo "'/etc/systemd/system/mnt-$ISCSIDISKMOUNTFOLDER.automount' already exists"
fi
echo
echo "$(tput setaf 5)####### NEXT STEPS: #######$(tput sgr 0)"
echo
echo 'Reboot and verify that the iscsi disk automatically mounted on boot using the "mount" command'
echo