Skip to content

Instantly share code, notes, and snippets.

@troykelly
Last active January 15, 2024 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troykelly/9e55afc26f6295eb3b4202c0f6fa084e to your computer and use it in GitHub Desktop.
Save troykelly/9e55afc26f6295eb3b4202c0f6fa084e to your computer and use it in GitHub Desktop.
iSCSI Cleanup if you are using TrueNAS

iSCSI Cleanup for Proxmox VE

This script identifies and removes invalid iSCSI targets from a Proxmox VE (PVE) server. It obtains the IPv4/IPv6 addresses of the iSCSI servers, compares them against the IP addresses of iSCSI targets on the PVE server, and removes any invalid targets.

This script is important for maintaining the efficiency and health of iSCSI links in a PVE environment, thereby ensuring optimum storage performance.

Installation

You should have root access to the PVE server to install this script.

Follow the below instructions to install and set up this script:

  1. Download the cleanup script and make it executable.
# Fetch the script
wget -O /usr/local/sbin/iscsi-cleanup https://gist.githubusercontent.com/troykelly/9e55afc26f6295eb3b4202c0f6fa084e/raw/iscsi-cleanup

# Make the script executable
chmod +x /usr/local/sbin/iscsi-cleanup
  1. Download and move the cron file to /etc/cron.d.
# Fetch the cron file
wget -O /etc/cron.d/iscsicleanup https://gist.githubusercontent.com/troykelly/9e55afc26f6295eb3b4202c0f6fa084e/raw/iscsicleanup

# Set appropriate permissions and ownership
chown root:root /etc/cron.d/iscsicleanup
chmod 644 /etc/cron.d/iscsicleanup
  1. Restart the cron daemon to apply the changes.
service cron restart
  1. Verify that everything is in place.
ls -l /usr/local/sbin/iscsi-cleanup
ls -l /etc/cron.d/iscsicleanup

Execution

This script will automatically run at every system startup and every hour, thereby regularly cleaning up any invalid iSCSI targets.

You can also manually run the script using the following command:

/usr/local/sbin/iscsi-cleanup

Logging

The output of this script will be logged to /var/log/iscsi-cleanup.log.

You can view the logs with the following command:

tail -f /var/log/iscsi-cleanup.log

This command will display the recent log contents and update the display as new entries are added.

#!/bin/bash
# This script identifies and removes invalid iSCSI targets from a Proxmox server.
# It retrieves the IPv4/IPv6 addresses of the servers and compares them against
# the IP addresses of iSCSI targets from Proxmox server.
# List of valid servers
declare -a VALID_SERVERS=("nas001.internal.sy3.aperim.net" "nas002.internal.sy3.aperim.net")
# Fetch the IPv4 and IPv6 addresses of servers and store them in an array
declare -a VALID_IPs=()
for server in "${VALID_SERVERS[@]}"; do
IPs=$(dig A $server +short; dig AAAA $server +short)
for ip in $IPs; do
VALID_IPs+=("$ip")
done
done
echo "Valid IPs: ${VALID_IPs[*]}" >&2
# Function to delete invalid target
delete_invalid_target() {
local ip="$1"
local targetIQN="$2"
echo "Deleting invalid iSCSI target: $targetIQN at $ip" >&2
# The following command should delete the target
# Uncomment it when you are ready to do so
iscsiadm -m node -T "$targetIQN" -p "$ip" -o delete
}
# Regex pattern to match IPv4, IPv6, and port
IPv4pattern='([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+):([0-9]+)'
IPv6pattern='\[([0-9a-f:]+)\]:([0-9]+)'
# Loop to check each valid IP running iSCSI target discovery and delete any invalid targets.
for ip in "${VALID_IPs[@]}"; do
# Discover iSCSI targets on each valid IP
target_discovery=$(iscsiadm -m discovery -t sendtargets -p "$ip")
# Iterate over the targets from the target discovery result.
while read -r target; do
# Check IPv4 targets
if [[ $target =~ $IPv4pattern ]]; then
targetIP="${BASH_REMATCH[1]}"
targetPort="${BASH_REMATCH[2]}"
# Check IPv6 targets
elif [[ $target =~ $IPv6pattern ]]; then
targetIP="${BASH_REMATCH[1]}"
targetPort="${BASH_REMATCH[2]}"
fi
targetIQN=$(echo "$target" | cut -d " " -f2)
# If the target's IP isn't in the array of VALID_IPs, it's invalid
if [[ ! "${VALID_IPs[@]}" =~ "$targetIP" ]]; then
delete_invalid_target "${targetIP}:${targetPort}" "$targetIQN"
fi
done <<< "$target_discovery"
done
echo "Invalid targets have been deleted." >&2
# /etc/cron.d/iscsicleanup
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# Run the iscsi-cleanup script at startup.
@reboot root /usr/local/sbin/iscsi-cleanup >> /var/log/iscsi-cleanup.log 2>&1
# Run the iscsi-cleanup script every hour.
0 * * * * root /usr/local/sbin/iscsi-cleanup >> /var/log/iscsi-cleanup.log 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment