How Do I use autofs to map a USB drive by its ID?
Solution 1
In your constellation /MOUNT_FOLDER is the base directory where subdirectories will be mounted by the indirect mount-map auto.ext-usb.
See man 5 autofs
for further details.
Example:
usbdisk -fstype=vfat,uid=yourworkingusername :/dev/disk/by-id/thediskid
If you cd /MOUNT_FOLDER/usbdisk
your usbdisk will be mounted there (I assume it is vfat formatted).
You can use /etc/fstab instead - but then you will to have to mount "by hand".
The entry in /etc/fstab looks like this:
/dev/disk/by-id/thediskid /MOUNT_FOLDER vfat defaults,user,noauto 0 0
After that you can mount the USB-Disk as ordinary user with mount /MOUNT_FOLDER
.
Solution 2
add to
/etc/auto.master
/var/autofs/removable /etc/auto.removable --timeout=2
add to
/etc/auto.removable
theDiskLabel -fstype=ext4,rw,gid=1000,umask=002 :/dev/disk/by-uuid/thediskid
restart autofs service
sudo service autofs restart
Access your directory, thats it!
cd /var/autofs/removable/theDiskLabel
Related videos on Youtube
![SeanKilleen](https://i.stack.imgur.com/jAkO8.jpg?s=256&g=1)
SeanKilleen
Updated on September 18, 2022Comments
-
SeanKilleen almost 2 years
Goal
- I have a USB drive -- let's say the drive's ID is /dev/disk/by-id/thediskid
- I would like to mount the drive -- by ID -- to a folder (let's call it /MOUNT_FOLDER)
Question
- What is the best way to do this using autofs?
Current Attempt
/etc/auto.master:
+auto.master /localam auto.linux /[another mount] [auto.othermount] --timeout=5 -- ghost /MOUNT_FOLDER auto.ext-usb --timeout=5 / -
/etc/auto.ext-usb:
/MOUNT_FOLDER /dev/disk/by-id/thediskid
I know I'm missing something but I can't seem to get a good lead on what the proper syntax is.
New to Linux; appreciate a pass if I'm overlooking something simple. Thanks!
-
Nils almost 12 yearsIs this the only usb-drive you ever want to mount, or do you want to do something more generic?
-
SeanKilleen almost 12 yearsNils, not sure I follow you completely, though what you're saying sounds possible. In this case, how would you recommend accomplishing mounting the contents of the USB disk into the /MOUNT_FOLDER location? Also, am I using the wrong tool in autofs (is it recommended that I just use /etc/fstab instead, for example?)
-
Nils almost 12 years@goober if you want to use the automounter-mechanism using indirect maps is the "recommended" way. If you really want to use the main directory as mountpoint you can define your entry directly in the direct map auto.master - but changes in auto.master will only be activated by restarting autofs (as opposed to indirect maps). I'll update my answer about /etc/fstab
-
SeanKilleen almost 12 yearsI'm not attempting to argue your solution that it's the recommended way. My issue is that I'm getting the syntax wrong. In order to mount a usb drive by id using autofs, could you show me the proper way to configure the files given the information I've provided initially? I understand the concept generally when it's explained but implementing your suggestions seems to be going wrong somewhere.
-
Nils almost 12 years@goober I tried this myselv and updated my answer.
-
Nils about 10 years@SeanKilleen The problem with indirect maps is that you will have to use a subdirectory. There are two ways around this: a) use a direct map b) use a soft-link /MOUNT_FOLDER that points to /MOUNT_FOLDER/usbdisk.