Skip to content

Instantly share code, notes, and snippets.

@rafaelfoster
Forked from skarllot/rescan.sh
Last active October 8, 2024 12:20
Show Gist options
  • Save rafaelfoster/8741314 to your computer and use it in GitHub Desktop.
Save rafaelfoster/8741314 to your computer and use it in GitHub Desktop.
Rescan the devices (can detect new space when disk is resized OR new devices that were attached)
# Reference: http://blog.gurudelleccelsopicco.org/2009/09/online-lun-expansion-and-partition-resizing-without-reboot-under-linux/
echo 1 > /sys/block/[DEVICE]/device/rescan
# DETECT IF NEW DISKS ARE ATTACHED TO THE HOST
# Reference: http://www.cyberciti.biz/tips/vmware-add-a-new-hard-disk-without-rebooting-guest.html
ls /sys/class/scsi_host
# Output:
# host0 host1 ...
# Now type the following to send a rescan request:
echo "- - -" > /sys/class/scsi_host/host0/scan
fdisk -l
# OR
ls /sys/class/scsi_host/ | while read host ; do echo "- - -" > /sys/class/scsi_host/$host/scan ; done
@sturlan
Copy link

sturlan commented Oct 8, 2024

this is not working for me, here's a rewrite:

#!/bin/bash

Directory where scsi hosts are listed

SCSI_HOST_PATH="/sys/class/scsi_host/"

Check if the directory exists

if [ ! -d "$SCSI_HOST_PATH" ]; then
echo "SCSI host path not found: $SCSI_HOST_PATH"
exit 1
fi

Loop over all the scsi_host directories

for host in "$SCSI_HOST_PATH"host*; do
# Extract the host number from the directory name (e.g., host0, host1, etc.)
host_name=$(basename "$host")

# Construct the path to the scan file
scan_file="$SCSI_HOST_PATH$host_name/scan"

# Check if the scan file exists
if [ -f "$scan_file" ]; then
    echo "Rescanning $host_name..."
    # Rescan the SCSI host by echoing '0 0 0' into the scan file
    echo "0 0 0" > "$scan_file"
    echo "$host_name rescan complete."
else
    echo "Scan file not found for $host_name"
fi

done

echo "All SCSI hosts have been rescanned."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment