Auto Mount External Drives in Debian

Here's an old script I came up with for delaying the mounting external drives (they were eSATA) to persistent locations on my Debian based server, regardless of the order in which the OS detects the drives. It uses the blkid identifiers of the drives, instead of the /dev drive locations (which can change each boot). You can run sudo blkid to get a list of the blkid of mounted drives (e.g. manually mount it, get the id, edit the script).

This script requires PHP, and can be setup as an rc.d script. fstab will need to be aware of the disks. Since the mounting is done in a side process, the system won't hang when it tries to mount the drives and they aren't present.

There are better methods (read; less hacky) for automatically mounting devices. If you know them, please mention how in the comments below.

#!/usr/bin/env php
define('CHELSEY',       '30AC5B8EAC5B4E8A'); # Put blkid here
define('DELTA',         '4A841A75821A63AB'); # Put blkid here
$devices = `/sbin/blkid`;

if (strpos($devices, DELTA) !== FALSE) {
    $output = `mount /storage/delta`;
    $output = trim($output);
    echo "Mounting /storage/delta... $output\n";
} else {
    echo "Not Mounting: /storage/delta\n";

if (strpos($devices, CHELSEY) !== FALSE) {
    $output = `mount /storage/chelsey`;
    $output = trim($output);
    echo "Mounting /storage/chelsey... $output\n";
} else {
    echo "Not Mounting: /storage/chelsey\n";
Thomas Hunter II Avatar

Thomas is the author of Advanced Microservices and is a prolific public speaker with a passion for reducing complex problems into simple language and diagrams. His career includes working at Fortune 50's in the Midwest, co-founding a successful startup, and everything in between.