[HowTo] Automount ANY disk device (fstab)

Mounting

Mounting devices is an everyday part of the maintenance of a Linux system.

This guide is not recommended for #newbies as the slightest error will prevent your system from booting.

If you are new to Linux or less experienced the recommended way is to use systemd mount units. Such units will - in case of errors - not render your system unbootable. Instead use this guide - much safer - systemwise.

If you are comfortable with editing your fstab and otherwise proficient in *nix administration - please continue

Using terminal

Locate your device name and partition

lsblk -o PATH,SIZE,HOTPLUG,MODEL

If will be something like /dev/sdy1 - setup a variable - replace sdy1 with your actual partition

PARTITION=/dev/sdy1

Create a variable with the path (no spaces) where you want to mount the partition (outside the home folder)

MOUNTPOINT=/data/mydevice

Create the mountpoint

sudo mkdir -p ${MOUNTPOINT}

Set permissions on mount point

sudo chmod ugo+rwx ${MOUNTPOINT}

Create a merge file - in case the device is not present at boot time we set some mount options
nofail noauto device-timeout automount. man systemd mount (5)

echo "UUID=$(lsblk -no UUID ${PARTITION}) ${MOUNTPOINT} $(lsblk -no FSTYPE ${PARTITION}) rw,noatime,nofail,auto,x-systemd.automount,x-systemd.device-timeout=1ms 0 2" > automount.txt

Verify content (example)

❯ cat automount.txt
UUID=1f3d1a6e-b4b5-46da-909a-8a87a45dd18b /data/mydevice ext4 rw,noatime,nofail,auto,x-systemd.automount,x-systemd.device-timeout=1ms 0 2

Merge with your fstab

sudo cat automount.txt >> /etc/fstab

Mount the partition

sudo mount -a

You can of course create a script - add it to your toolbox - e.g. add-mount.sh and make it executable chmod +x add-mount.sh

#!/bin/bash
if [[ $(whoami) != "root" ]]; then
    echo "Please run as root!"
    exit
fi
PARTITION=$1
MOUNTPOINT=$2
if [[ -z ${PARTITION} ]]; then
    echo "Partition empty -> exit"
    exit 1
fi

if [[ -z ${MOUNTPOINT} ]]; then
    echo "Mountpoint empty -> exit"
    exit 1
fi

sudo mkdir -p ${MOUNTPOINT}
sudo chmod ugo+rwx ${MOUNTPOINT}
echo "UUID=$(lsblk -no UUID ${PARTITION}) ${MOUNTPOINT} $(lsblk -no FSTYPE ${PARTITION}) rw,noatime,nofail,auto,x-systemd.automount,x-systemd.device-timeout=1ms 0 2" > ~/automount.txt
echo "..."
echo "Adding line to fstab"
cat ~/automount.txt
sudo cat ~/automount.txt >> /etc/fstab
sudo mount -a

Use it like this

./add-mount.sh /dev/sdy1 /data/my-device

And yes - you can use Gnome Disks - but where the fun in that :slight_smile:

6 Likes

Or you could install udiskie :stuck_out_tongue_winking_eye:

Still - where's the fun?

:keyboard:

Thank's for a concise and easy to understand guide!

A minor correction if I may; the noauto flag means the mount will not happen automatically (at boot or with mount -a), rather you' ll have to mount the device specifically (for example with sudo mount ${MOUNTPOINT}).

So, to actually automount the device you'll have to ditch the noauto flag :slight_smile:

1 Like

Thank you - that was an oversight

I have fixed it.

Forum kindly sponsored by