First, use disk UUIDs like @grawity said.
If you want to know the UUIDs of everything that looks like a block device to Linux, use the blkid
command. I think you can do something like blkid /dev/sda
to find out the UUID of sda
as well.
Another thing you can do is use the symlinks in /dev/disk/by-id
which are created for each disk based on the bus it's connected to and its reported model and serial number.
Technically, Linux doesn't know or care about the device it was loaded from, because
- that's the bootloader's job - which runs before the kernel is running - to load the kernel into memory.
- anything that's not in the kernel needed at boot is in the initramfs (initrd), such as drivers, early userland utilities - loading this is also the bootloader's job.
Now, after Linux loads, boot scripts or whatever other mechanism running under the kernel tries to mount a root file system so you have other things to run besides the kernel, as well as swap, etc. Basically all the stuff in your /etc/fstab
. This is what you really care about and that file will have the information you need. You can use UUIDs in /etc/fstab
(and I believe most distributions already use them) - so with a couple grep
's and cut
's you can get UUIDs out of here that you want.
Example (this probably can be done better): cat /etc/fstab | grep "/ " | cut -f 1 -d " "
And you can use the output of that for blkid
to find the UUID of your root file system or whatever other partition.
Best Answer
eg: