Intro:
This is a SystemD alternative to mount an iSCSI drive at boot.
Mounting an iSCSI disk at boot is a (2) step process:
The LUN must be connected by the host
And then mounted to a point on the local filesystem.
The scripts- developed using Ubuntu 20.04 LTS- pasted in below answer I've made available to save others the donkey work of configuring an iSCI mount; it is indeed fiddly, tedious business. They are known to work correctly and produce consistent, correct results as of the date of this posting.
To download the scripts directly to your host mounting your iSCSI drive:
git clone https://github.com/f1linux/iscsi-automount.git
Please note that there are some Debian-specific commands in the full scripts in the Github repo- ie: dpkg. The abridged scripts below are distribution in-specific and should work on any distro using SystemD.
Pre-Requisites:
open-iscsi installed and its' configuration files /etc/iscsi/iscsid.conf & initiatorname.iscsi configured.
Manually test the connections using the iscsiadm command before running the scripts.
The (2) Scripts:
config-iscsi-storage.sh: Automatically connects the LUN at boot by creating a SystemD service called connect-luns.service. This script must be executed FIRST. After the LUN is connected you must next partition and laydown a filesystem on it before running the next script.
config-iscsi-storage-mounts.sh: Automatically mounts the connected, partitioned iSCSI disk with a filesystem to a mount point on the local filesystem as a SystemD mount at boot.
The Scripts:
NOTE: I've removed the echo'ed feedback, instructions and other debris from the scripts to aid ease of reading for you to assess their operation.
config-iscsi-storage.sh
####### SET VARIABLES #######
# Host exposing the LUNs:
STORAGEIP=''
# Get this value from the storage host exposing the LUN:
IQNTARGET=''
if [ ! -d /root/scripts ]; then
mkdir /root/scripts
fi
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
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
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
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
iscsiadm -m discovery -t sendtargets -p $STORAGEIP
fdisk -l
echo
echo "$(tput setaf 5)####### NEXT STEPS: #######$(tput sgr 0)"
echo
echo 'STEP 1: 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 'STEP 2: Format the iSCSI disk with a filesystem'
echo ' ie: mkfs.ext4 /dev/sdX1 where the iSCSI disk is /dev/sdX'
echo
echo 'STEP 3: Execute script config-iscsi-storage-mounts.sh to configure auto-mounting the iSCSI disk'
echo ' to configure mounting the newly formatted iSCSI disks on boot'
echo
config-iscsi-storage-mounts.sh
####### SET VARIABLES #######
# Use 'fdisk -l' to identify the iSCSI disk
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 hypen 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'
## NOTE: Most settings below show work out of the box
if [ ! -d /mnt/$ISCSIDISKMOUNTFOLDER ]; then
mkdir /mnt/$ISCSIDISKMOUNTFOLDER
chmod 770 /mnt/$ISCSIDISKMOUNTFOLDER
fi
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
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
Conclusion:
As you can see, connecting an iSCSI disk is fiddily work. Hope this saved you some grunt work-