How do I prevent USB flash drive from getting a new Linux device name after wake from sleep?

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?


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.)