1

Is it possible to make the device filesystem name , /dev/sdi1 , shown by lsblk -f stay constant every time we disconnect and reconnect the SanDisk USB Cruzer drive? I am running Ubuntu Linux 16.04 on an Lenovo Thinkstation desktop computer.

I believe the SanDisk USB Cruzer drive automatically mounts on insertion.

If it is possible, what are the steps necessary to make this possible?

2
  • Most applications dont care what /dev/sd? is used, as it can always be found (eg findmnt) if you use a disc LABEL or UUID for example. Can you say why you need to fix the device?
    – meuh
    May 25, 2016 at 17:50
  • @meuh, Thank you for your comment. Currently we are looking at how to make usb device filename persist across reboots or unplugging and replugging in the usb for Ubuntu Linux 16.04. When I keep plugging them in and pulling them out from the USB ports and they keep getting names like /dev/sdg1 or /dev/sdi1 . However, the device names are not persistent — whether the BusPirate pops up as /dev/sdg1 or /dev/sdi1 depends on the order in which are the devices discovered by the kernel.
    – Frank
    May 25, 2016 at 18:00

3 Answers 3

2

You would need to specify the mounting point based on the device's UUID.

You should refer to the official Ubuntu documentation page on the subject for usage details.

Here are examples (your output will be different, but similarly formatted). The command:

sudo blkid

produces an output similar to:

/dev/sda1: TYPE="ntfs" UUID="72C0DE8EC0DE57C5" LABEL="windows" 
/dev/sda2: UUID="30fcb748-ad1e-4228-af2f-951e8e7b56df" TYPE="ext4" 
/dev/sda5: TYPE="swap" UUID="8c4e69f8-5074-42c0-8134-0b2429c4c02c" 
/dev/sdb1: SEC_TYPE="msdos" UUID="4848-E35A" TYPE="vfat"

you would then have to change the entry in fstab from:

/dev/sda2 /mnt/Mint ext4 defaults 0 0

to something like:

UUID=30fcb748-ad1e-4228-af2f-951e8e7b56df /mnt/Mint ext4 defaults 0 0
15
  • Thank you for your nice answer. Thank you for your comment. Currently we are looking at how to make usb device filename persist across reboots or unplugging and replugging in the usb for Ubuntu Linux 16.04. When I keep plugging them in and pulling them out from the USB ports and they keep getting names like /dev/sdg1 or /dev/sdi1 . However, the device names are not persistent because it depends on the order in which are the devices discovered by the kernel. May I ask why does specifying the mounting point based on the USB device's UUID solve our requirement?
    – Frank
    May 25, 2016 at 18:04
  • Yes, that's exactly the point. May 25, 2016 at 19:40
  • @Fergus-Incoronato: Nice edit! May 26, 2016 at 0:07
  • I enjoyed reading your answer. If I change your fstab example to: /dev/usbhd1 /mnt/Mint ext4 defaults 0 0 where /dev/usbhd1 is Serge's symbolic link, what should the lsblk -f output look like? My architect manager would like to find out what the significance of your nice answer is on preserving mount points across reboots and USB reinsertions?
    – Frank
    May 26, 2016 at 0:21
  • @Frank: lsblk -f's output is not affected by mounts. What will change is the actual device associated (/dev/sda2) but the UUID will always match that actual USB key. (with the only exception would be if you would have cloned the key.) May 26, 2016 at 0:51
2

I am almost sure it's not possible to assign the # of sd, but you might work this around with udev rule that would set up links to the actual sdX and sdXY upon disk drive connection:

KERNEL=="sd*", SUBSYSTEMS=="scsi", ATTRS{model}=="USB 2.0 Storage Device", SYMLINK+="usbhd%n"

would create these links:

  • /dev/usbhd - The fdiskable node
  • /dev/usbhd1 - The first partition (mountable)
  • /dev/usbhd2 - The second partition (mountable)

Source

12
  • Thank you for your nice answer. Once I create your rule using sudo nano /etc/udev/rules.d/10-local.rules, how do I make sure the 3 links you mentioned in your answer get created properly? What should I do after: sudo /etc/init.d/udev restart?
    – Frank
    May 25, 2016 at 18:10
  • Just reconnect the device. Alternatively, use udevadm trigger command (see man udevadm)
    – Serge
    May 25, 2016 at 18:35
  • Please note, that you might have to change the model string in the rule. Also you could use serial no of the device to match. See udev docs for details and samples
    – Serge
    May 25, 2016 at 18:37
  • Thank you for your 2 great comments. I just followed your recommendation to just reconnect the USB device and I can now see the links /dev/usbhd and /dev/usbhd1. Could I ask why it's not possible to assign the # of the sd?
    – Frank
    May 25, 2016 at 18:41
  • @Fergus-Incoronato, Nice edit of Julie Pelletier's answer. How do I use Serge's answer with /etc/fstab? Thank you very much.
    – Frank
    May 26, 2016 at 0:35
1

Another way of finding the /dev/sd device that corresponds to a disc that has no LABEL nor UUID is to use its serial id. For example, my SSD says

$ sudo hdparm -I /dev/sda
    Model Number:       KINGSTON SH103S3240G                    
    Serial Number:      50999B9999926F99    

And I can find which /dev/sd device it is by following the link in /dev/disk/by-id/ using the serial id built from this information. Eg

$ ls -l /dev/disk/by-id/ata-KINGSTON_SH103S3240G_50999B9999926F99
lrwxrwxrwx 1 root root 9 Apr 28 17:22 /dev/disk/by-id/ata-KINGSTON_SH103S3240G_50999B9999926F99 -> ../../sda

Usually, we set a UUID on a partition then we can find the device simply with findmnt. Eg

$ findmnt -n -o source UUID=e7522030-f6e3-472d-8013-3a3316fd15a3
/dev/sda3
11
  • Thank you for your nice answer and comment. Here is my udev rule using your serial id suggestion: SUBSYSTEMS=="usb",ATTRS{serial}=="4C530001041030118021",KERNEL=="sd*", SYMLINK+="usbhd%n". After I restart the udev service using sudo /etc/init.d/udev restart I still cannot find the links /dev/usbhd , /dev/usbhd1 or/dev/usbhd2. May I ask what steps I forgot to do or what mistakes I made?
    – Frank
    May 25, 2016 at 18:31
  • Did you unplug/replug the device? You might want to add ACTION=="add",and put your file last in the rules (ie rename it 99-...)
    – meuh
    May 25, 2016 at 18:37
  • @mueh, Thank you for your comment. Do I need to unplug and replug the device after I add ACTION=="add" to the udev rule?
    – Frank
    May 25, 2016 at 18:43
  • I see from the other answer you have created the links ok. The ACTION=="add", is for the future, to only create the links when the device is plugged in (added). I'm not sure if you need to create another rule to remove the link when you unplug the device (ACTION=="remove",).
    – meuh
    May 25, 2016 at 18:48
  • Thank you for your comments and answer today. Why does not the /dev/usbhd1 , the symbolic link to the first partition, show up in the lsblk -f output? What happens if change the mount point from /home/frank/data1 to /home/frank/data59 corresponding to the symbolic link /dev/usbhd1?
    – Frank
    May 25, 2016 at 19:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .