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?

migrated from stackoverflow.com Aug 3 '15 at 14:51

This question came from our site for professional and enthusiast programmers.

  • 2
    The answer is no, but you can work around that with udev rules. See here for a better but slightly outdated explanation. – Satō Katsura Aug 3 '15 at 8:13
  • @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. – ikrabbe Aug 3 '15 at 8:42
  • 1
    @ikrabbe, I know, that's why I posted an answer based on label identification (these can be managed a bit easier). – Alex Tartan Aug 3 '15 at 8:47

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

  • 1
    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). – Toby Speight Aug 3 '15 at 13:31

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.

  • 1
    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. – Gudrun Aug 3 '15 at 10:10

Just to add to @alextartan's answer.

I rolled my own backup system which uses USB drives for targets. I need to be sure what's going where. Since UUID's are not particularly user friendly, I rely on disk labels.

UUID's are also a bit problematic because whenever you reformat a partition, it gets a new unique UUID, so any scripts using that UUID have to be modified. I can assign the same label as before to the reformatted partition (or even to a partition on a new/replacement drive) and leave my scripts alone.

I came up with my own unique (human friendly) labelling convention and use gparted to label the various partitions when I create them.

When my scripts need to access a particular partition, I look up the device name using the label.

function get_dev {
  ## Return the device name of a partition
  ## In DEV
  ## Given its label
  ## Partition does not need to be mounted
  ## Usage get_dev <partition_label>
  ## Copyleft 04/28/2014 JPmicrosystems
  local LINE
  local VERBOSE=1  ## Verbose version
  local USAGE="ERROR Usage is: get_dev <partition_label>"
  unset DEV

  if [ -z "${1}" ]
    (( VERBOSE )) && echo $USAGE
    return 1

  LINE=$(ls -l /dev/disk/by-label | grep " ${1} ")
  if (( $? ))
    (( VERBOSE )) && echo "ERROR Partition [${1}] not found"
    return 1

  return 0

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.