Auto Mount External Drives in Debian

Looking for a better way to manage your org's technical proposals?

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 Hunter II is a Software Engineer with 18+ years building scalable, production-grade systems. Deep expertise in Node.js, observability, and developer tooling, with a strong track record of driving architectural decisions, mentoring engineers, and improving customer facing technical documentation. Author of 5 books (including O'Reilly), certified by the OpenJS Foundation, and frequent international conference speaker.