Will a USB key have the same name every time I mount it?

5,815

Solution 1

While accessing it by /dev/sdXy is risky, a more accurate identification may be done by UUID.

Since you mention (at some point) changing the usb stick, to maintain compatibility, you may want to identify your usb stick by a label. To do so, you can: /dev/disk/by-label/YourLabelHere.

Note that you need to set the label to a new usb stick before running the script, and also, /dev/disk/by-label is only available if (at least) one usb stick is connected (otherwise it gets removed).

Details on how to setup the usb label can be found here: https://askubuntu.com/questions/194510/how-to-edit-label-of-usb-drive

Solution 2

This is how I address this problem, but generally as Sato Katsura told you, you need to write a udev rule.

  1. Plug in your device, check which device it got (for example by watching dmesg).
  2. As superuser call udevadm info --query all /dev/sdc (or whatever).
  3. Setup a udev rule, here is an example for my pocketbook. The fields ID_SERIAL_SHORT and ID_FS_UUID I read from the info in step 2:

    SUBSYSTEM=="block", ENV{ID_SERIAL_SHORT}=="YT440900877400W000Y0", ENV{ID_FS_UUID}=="3878-D432", ENV{DEVTYPE}=="disk", SYMLINK+="pocketbook", OWNER="ingo"
    SUBSYSTEM=="block", ENV{ID_SERIAL_SHORT}=="YT440900877400W000Y0", ENV{ID_FS_UUID}=="9016-4EF8", ENV{DEVTYPE}=="disk", SYMLINK+="pocketbook-sd", OWNER="ingo"
    

    I set OWNER in here, that I can mount it with my main user account. Also with my pocketbook I don't have partitions but mount the plain disks. If you have partitions change:

    ENV{ID_PART_ENTRY_NUMBER}=="1", ENV{DEVTYPE}=="partition",
    

    for partition Number 1, on the udev rules lines.

The udev rules are found in /etc/udev/rules.d, if you have a sane system.

  1. Now you can

    udevadm control --reload
    

    to reload the rules and

    udevadm monitor
    

    to monitor what happens during step 5.

  2. Plugout and Plugin your usb device (of course after unmounting before if you mounted that). The monitor should inform you about the new device when it is initialized.

  3. Check if /dev/ contains the right symlinks. In my example I got this:

    brw-rw---- 1 ingo disk    8,  48 Aug  3 10:32 sdd
    brw-rw---- 1 ingo disk    8,  32 Aug  3 10:32 sdc
    lrwxrwxrwx 1 root root         3 Aug  3 10:32 pocketbook-sd -> sdd
    lrwxrwxrwx 1 root root         3 Aug  3 10:32 pocketbook -> sdc
    

Now you can define persistent rules with the symlinks /dev/pocketbook and /dev/pocketbook-sd, or whatever are your names in the SYMLINK field of the udev rules file.

Share:
5,815

Related videos on Youtube

Gudrun
Author by

Gudrun

Updated on September 18, 2022

Comments

  • Gudrun
    Gudrun over 1 year

    I have a USB key which will be used to save data on it daily via a bash script. I managed to mount it for a specific user to be able to write folders in it using the command:

    mount /dev/sdc1 /media/usb -o rw,uid=sysop,gid=sysop
    

    The script can run and create directories as it wants and it's perfect. At some point, the USB key will be unplugged and replaced with a new one (same model). Will the new USB key still have the name /dev/sdc1 or will it be a different one?

    If it's a different one, how can I include the mounting part in my script for it to be made automatically?

    • Satō Katsura
      Satō Katsura almost 9 years
      The answer is no, but you can work around that with udev rules. See here for a better but slightly outdated explanation.
    • ikrabbe
      ikrabbe almost 9 years
      @AlexTartan If you plug in any other usb disk in any other usb port before it might get sdc. So your constraint "and nothing else is assigend to sdc1" is true. In general this is a very dangerous assumption that can never be made. It's never a good idea to use sd[a-z] as persistent device names. This is a legacy from times when we had IDE controllers with four static ports that could be set by setting hardware jumpers and selecting the right cable plug.
  • Gudrun
    Gudrun almost 9 years
    Thank you for your answer. However I won't be able to use it. The device I'm working on is installed on a volcano and the usb stick is used to store the data acquired there. We'll only go there once every three mounths to change the usb stick. I only have a remote acces to it, without any "screen" interface. As the usb will be different all the times, but plugged on the same usb ports, I think the easier way is keeping the sdc1 path. I'll see with udev rules what I can do.
  • Toby Speight
    Toby Speight almost 9 years
    If your filesystems don't have labels, then /dev/disk/by-id/ may be of use instead (it's what I use for my filesystems).