2

I'm booting Linux from a USB flash drive on my laptop. Everything runs fine until I put the laptop to sleep. When it wakes up, suddenly the USB flash drive gets a different device name, and everything starts failing, because the already mounted file system is now on a device that no longer exists. How do I make Linux keep the device at the same device name when it wakes up?

2 Answers 2

2

I cannot tell you how to keep it from changing its device name -- but still there might be a solution to your problem. Before doing the following changes, just for safety make a backup of your /etc/fstab file and make sure you've got some other bootable device available in case of failure (which can happen easily by mistyping a single letter here).

First, check your /etc/fstab. Probably your entries look like this:

/dev/sde1 /   ext4    errors=remount-ro 0       1

And after wakeup, your drive changed to sdf, just as example, so this entry fails now. As these device names are given sequentially on detection, you might prefer some consistent name not affected by this. So your second step is:

ls -l /dev/disk/by-uuid

Now check which of the entries there is pointing to /dev/sde1 (or course, replace sde1 by what your drive is currently mapped to). It might look like this:

lrwxrwxrwx 1 root root 10 Jun 30 20:53 b207-e035ddd09600 -> ../../sde1

Now you can tell the device's unique id. Go back to your /etc/fstab and replace /dev/sde1 by /dev/disk/by-uuid/b207-e035ddd09600 (again, replace my example values by your real ones). The line should now read like:

/dev/disk/by-uuid/b207-e035ddd09600  /   ext4    errors=remount-ro 0       1

Now it should no longer matter which "device name" your USB drive has, as it is identified by its unique id which is not subject to change.

UPDATE:

Using a LiveCD image on an USB stick, things may look a little different, as /etc/fstab may only contain virtual file systems (such as tmpfs) -- but misses the entries for disk partitions. In this case, check with /etc/mtab (which contains all currently mounted partitions), check for the entry matching your flash drive, copy that and add it to /etc/fstab. Then adjust it (in /etc/fstab of course!) as described above.

(hint for the names: fstab = File System table, contains definitions on file systems to mount and what options shall be used mounting them; mtab: Mounted table, i.e. what file systems are currently mounted and which options were used. Remember you also can mount file systems manually, passing all required options to the mount command: Hence, /etc/mtab may contain entries not found in /etc/fstab. Also the other way around, as a device can have the noauto flag set in /etc/fstab, so the system does not mount it automatically.)

5
  • Sounds good! I'll accept as soon as I have a chance to test this out.
    – Michael
    Jul 21, 2012 at 23:06
  • It even works for sd-cards. That's how I managed to have my "removables" (smartphones, cards from my photo devices, etc.) automatically mounted to always the same location.
    – Izzy
    Jul 21, 2012 at 23:20
  • Unfortunately /etc/fstab has only two entries, one for aufs and one for tmpfs. I am having trouble making heads/tail out of how this is working. This is an Ubuntu live "CD" USB image. I uncompressed the initrd and I don't see where it is doing the mounting. I was hoping I could start from a live CD image since a lot of stuff seems to work out of the box on my laptop, but I may have to create my own initrd system from scratch I guess...
    – Michael
    Jul 26, 2012 at 3:30
  • OK, a live CD cannot have many entries in fstab, as that file usually is read-only and the hardware not known in advance, so you simply have to add missing stuff. For the stick itself (using UUID) that should be save, as it has the same UUID regardless of what computer it is attached to. As the device is mounted, you should find it in /etc/mtab to see the options used. Just copy the fitting line over to /etc/fstab and adjust it there.
    – Izzy
    Jul 26, 2012 at 5:56
  • @Michael I updated my answer with the mentioned details and a short explanation. You may want to check the 2 new paragraphs at its end.
    – Izzy
    Jul 26, 2012 at 6:06
0

Depending on what Linux you're running you may find that simply naming the USB flash drive does what you're after. For instance, if you give the drive the file system label MYDISK then it will mount on /media/MYDISK.

If that doesn't work then you can use udev rules to achieve what you're after, though it is more complicated.

You must log in to answer this question.

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