Ubuntu – How to get Ubuntu Server to recognize and automount a hotplugged SATA drive


AHCI is on and I can manually mount but as this device is destined to be a headless device used by people with no clue about the terminal so I need something as idiotproof as possible.

I expect I need to make some udev rules but I've got no idea how to start.

I've managed to get it to automount using fstab but this is unsuitable for hotplugging.

For now all I need is for it to recognize a particular drive with one primary NTFS partition but I'll be ramping this up to include a bunch of drives and even 'never seen by the server before' drives so if there's someone can also help me with that then even better.

Best Answer

On Linux, you can tell the system to rescan an individual SATA port.So to rescan the SATA3 port, just do:

echo "- - -" | sudo tee -a /sys/class/scsi_host/host2/scan

The system will scan the drive and add entries to /dev/ for the drive and its individual partitions (e.g /dev/sdb, /dev/sdb1, /dev/sdb2). Ubuntu will automount any mountable partitions under /media and put an icon on the desktop.

This is a script that may help you in scanning the board.

export SCAN_TEMP=$(mktemp -d)
ls /dev/sd? > ${SCAN_TEMP}/before.txt
for i in /sys/class/scsi_host/host*/scan
  echo "- - -" >> $i
ls /dev/sd? > ${SCAN_TEMP}/after.txt
for i in $(diff ${SCAN_TEMP}/before.txt ${SCAN_TEMP}/after.txt | grep \> | awk -F/ '{print $NF}'); do
  echo Added /dev/$i:
  /sbin/sfdisk -uM -l /dev/$i | grep -E "^/dev/$i" | awk '{print "    "$1" "$4" MiB"}'
rm -rf ${SCAN_TEMP}

Source: http://www.makestuff.eu/wordpress/sata-hotplug-in-ubuntu/