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
<?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 has contributed to dozens of enterprise Node.js services and has worked for a company dedicated to securing Node.js. He has spoken at several conferences on Node.js and JavaScript, published a book on microservices, and is an organizer of Nodeschool SF.