0

In Ubuntu, even if different block devices (e.g. an external hard drive and a flash drive) are represented by the same device file (e.g. /dev/sdb1) one after another, they are automatically mounted to different mounting points (e.g. /media/b/mydrive and /media/b/myflashdrive). How is that achieved? How does Ubuntu know the "name" of each device and use that "name" to name its mounting point? (/etc/fstab doesn't cover these plug-in devices.)

How can I simulate that? When manually mounting a block device, I run

sudo mkdir /media/b/mountingpoint1
sudo mount -t ntfs /dev/sdb1 /media/b/mountingpoint1
sudo umount /media/b/mountingpoint1
sudo rmdir /media/b/mountingpoint1

If I detach the device and then attach a different block device, the new device will be again represented by the same device file /dev/sdb1, and the above command will mount it to the same mounting point /media/b/mountingpoint1. I need a way to name the mounting points differently for different devices.

1
  • Where it gets the names is anyone guess, but there are filesystem/volume labels.
    – Ricky
    Oct 22, 2021 at 9:41

2 Answers 2

3

are represented by the same device file (e.g. /dev/sdb1)

The trick is that they are not represented by the same device file: In addition to that primary device file, which contains letters and numbers managed by the kernel, you also have symlinks in /dev/disk/by-* which, for example, contain the volume name of the filesystem, the vendor and model of the harddrive, or the device path.

And it's those symlinks that you should use inside /etc/fstab, not /dev/sdb1 etc.

2

(e.g. /media/b/mydrive and /media/b/myflashdrive). How is that achieved? How does Ubuntu know the "name" of each device and use that "name" to name its mounting point? (/etc/fstab doesn't cover these plug-in devices.)

Mounting of removable devices is done by UDisks which also creates the mountpoint for them which is always (on Ubuntu) in format /media/<user>/<unique_id>. The last part is either filesystem label or UUID if the filesystem doesn't have a label. Removable disks and drives are usually preformatted and the partition labeled by the vendor (e.g. my Kingston flash drive has the VFAT partition labeled as KINGSTON).

You can get the label (and UUID) for your devices with lsblk -f.

If you want to mount a device manually (not from GUI) and achieve the same effect you can use udisksctl mount -b /dev/sdb1.

And if you (in the future) use fstab for automounting you should always use UUID (UUID=) instead of /dev/sdb1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.