Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sohooo/15a1e84362c8e2a3c459fc53fdda45f7 to your computer and use it in GitHub Desktop.
Save sohooo/15a1e84362c8e2a3c459fc53fdda45f7 to your computer and use it in GitHub Desktop.
Mounting external usb drives automatically to its label on Ubuntu
sudo apt-get install -y usbmount
sudo bash -c "cat <<EOT > /etc/usbmount/mount.d/01_create_label_symlink
#!/bin/sh
# This script creates the volume label symlink in /var/run/usbmount.
# Copyright (C) 2014 Oliver Sauder
#
# This file is free software; the copyright holder gives unlimited
# permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
#
set -e
# Exit if device or mountpoint is empty.
test -z \"\\\$UM_DEVICE\" && test -z \"\\\$UM_MOUNTPOINT\" && exit 0
# get volume label name
label=\\\`blkid -o udev \\\$UM_DEVICE | grep ID_FS_LABEL= | cut -d'=' -f2\\\`
# If the symlink does not yet exist, create it.
test -e \"/var/run/usbmount/\\\$label\" || ln -sf \"\\\$UM_MOUNTPOINT\" \"/var/run/usbmount/\\\$label\"
exit 0
EOT"
sudo chmod +x /etc/usbmount/mount.d/01_create_label_symlink
sudo bash -c "cat <<EOT > /etc/usbmount/umount.d/00_remove_model_symlink
#!/bin/sh
# This script removes the model name symlink in /var/run/usbmount.
# Copyright (C) 2005 Martin Dickopp
#
# This file is free software; the copyright holder gives unlimited
# permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
#
set -e
ls /var/run/usbmount | while read name; do
if test \"\\\`readlink \"/var/run/usbmount/\\\$name\" || :\\\`\" = \"\\\$UM_MOUNTPOINT\"; then
rm -f \"/var/run/usbmount/\\\$name\"
fi
done
exit 0
EOT"
sudo chmod +x /etc/usbmount/umount.d/00_remove_model_symlink
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment