Sinan Kurtulmuş

Mounting Removable Drives on UNIX

There are numerous disk management and automounting programs and daemons that are available and widely used on Linux systems. However, in most cases, these are unnecessary and a few lines of shell scripting work just as fine.

Here is a very simple POSIX compliant shell script that I use for mounting removable drives on Linux and OpenBSD desktops.


We open up a text editor and start typing. First, we get a list of the mountable drives, and prompt the user to choose one.

  • For OpenBSD:

sysctl hw.disknames
echo "Disk: "
read DISK
doas disklabel -h $DISK
echo "Partition: "
read PART
DEV="$DISK$PART"

Optionally, we can run the following command to see the removable disks only.

xinnna @ obsd ~ $:
dmesg | grep removable

  • For Linux:

lsblk
echo "Device: "
read DEV

Then we check if the device was already mounted. If not, we assign the directories of the device and the mountpoint. Then, we make the mount directory if it does not exist, and change its ownership and group to the current user. Additionally, we check to make sure it is empty.

The sudo command can be used if doas is not available.

[ -n "$(df | $DEV)" ] && \
echo "Device already mounted!" && exit 1

MOUNTDEV=/dev/$DEV
MOUNTPNT=/mnt/$DEV

[ -d "$MOUNTPNT" ] || doas mkdir $MOUNTPNT && \
doas chown $USER:$USER $MOUNTPNT

[ -n "$(ls -A $MOUNTPNT)" ] && \
echo "The directory is not empty!" && exit 1

Finally, we mount the drive. You can read more about mount options in the manual page of the mount command.

  • For OpenBSD:

doas mount -o noexec,nodev,nosuid $MOUNTDEV $MOUNTPOINT && \ echo "Device mounted."

  • For Linux:

doas mount -o noexec,nodev,nosuid,\
uid=$(id -u),gid=$(id -g),dmask=0022,fmask=0133 \
$MOUNTDEV $MOUNTPNT && echo "Device mounted."

Note: The FAT filesystem has no real support for file permissions. On OpenBSD, the mount command invokes a different program for each filesystem type. FAT filesystems are mounted by the mount_msdos program, which imitates file permissions according to the mount directory. This is documented in the OpenBSD manual pages.

When mounting FAT filesystems on Linux, passing the uid (user ID) and the gid (group ID) options to the mount command makes sure that everything in the mount directory is owned by the user. Also, by passing the dmask and the fmask options, it is possible to manage the permissions.

That's it. Here is the final script.

#!/bin/sh

##### OPENBSD #####
sysctl hw.disknames
echo "Disk: "
read DISK
doas disklabel -h $DISK
echo "Partition: "
read PART
DEV="$DISK$PART"

##### LINUX #####
# lsblk
# echo "Device: "
# read DEV

[ -n "$(df | $MOUNTDEV)" ] && \
echo "Device already mounted!" && exit 1

MOUNTDEV=/dev/$DEV
MOUNTPNT=/mnt/$DEV

[ -d "$MOUNTPNT" ] || doas mkdir $MOUNTPNT && \
doas chown $USER:$USER $MOUNTPNT

[ -n "$(ls -A $MOUNTPNT)" ] && \
echo "The directory is not empty!" && exit 1

##### OPENBSD #####
doas mount -o noexec,nodev,nosuid \
$MOUNTDEV $MOUNTPOINT && echo "Device mounted."

##### LINUX #####
# doas mount -o noexec,nodev,nosuid,\
# uid=$(id -u),gid=$(id -g),dmask=0022,fmask=0133 \
# $MOUNTDEV $MOUNTPNT && echo "Device mounted."