Introduction to External Drive Management
I’ve been using Linux for years, and one thing that’s always been a bit of a pain is managing external drives. Whether it’s a USB flash drive, an external hard drive, or an SSD, keeping track of these devices can be a chore. I’ve seen this go wrong when you’ve got multiple drives connected and you’re not sure which one is which. In practice, this can lead to a lot of confusion and wasted time. That’s why I’ve started using udev rules and automounting to manage my external drives.
Udev Rules
Udev is a device manager for Linux that allows you to create custom rules for device handling. The real trick is to create a rule that automatically mounts a specific external drive to a certain mount point. To create a udev rule, you’ll need to create a new file in the /etc/udev/rules.d/ directory. Don’t bother with trying to figure out the perfect filename - just use a format like XX-udev-rule.rules, where XX is a number that determines the order in which the rules are applied.
Here’s an example of a udev rule that mounts a specific external drive to /media/external:
ACTION=="add", RUN+="/usr/bin/mount /dev/%k /media/external"
This rule will mount the device to /media/external whenever it’s connected. I usually start with a simple rule like this and then modify it as needed.
Automounting
Automounting is a feature that allows your system to automatically mount devices when they’re connected. This can be especially useful for external drives, as it eliminates the need to manually mount them every time you connect them. To enable automounting, you’ll need to install the udev and util-linux packages. This is where people usually get burned - they forget to install these packages and then wonder why automounting isn’t working.
For example, you can create a rule that automounts all external drives to /media/:
ACTION=="add", ENV{ID_BUS}=="usb", RUN+="/usr/bin/mount /dev/%k /media/%k"
This rule will mount all USB devices to /media/ whenever they’re connected. In practice, this can be really useful for keeping your external drives organized.
Practical Examples
Here are a few practical examples of how you can use udev rules and automounting to manage your external drives:
- Create a udev rule that mounts a specific external drive to a certain mount point, such as
/media/external. - Use automounting to mount all external drives to
/media/. - Create a udev rule that runs a custom script whenever a specific external drive is connected.
For more information on udev rules and automounting, you can check out the official udev documentation or the Arch Linux wiki.
Troubleshooting
If you’re having trouble getting your udev rules or automounting to work, there are a few things you can try:
- Check the system logs to see if there are any error messages related to udev or automounting.
- Use the
udevadmcommand to test your udev rules and see if they’re being applied correctly. - Make sure that the
udevandutil-linuxpackages are installed and up-to-date.
See also
- Taming Dependency Chaos with Apt Pinning on a Small Debian Server
- Replacing Ubuntu with Fedora on my Home Server: What Broke and How I Fixed It
- Replacing Ubuntu with Fedora on my Daily Driver Laptop: A Month of Tweaks and Surprises
- What I Would Actually Self-Host Again on Linux
- Introduction to OpenSearch