Saturday, 26 October 2019

hard drive - Linux: get the current boot disk's device name


When booting a machine by USB stick,


sudo fdisk -l

outputs you two devices marked with an asterisk, and you'll never know which was the boot device.


I need a list of all disks attached to the current machine where


sed -ne 's/.*\([sh]d[a-zA-Z]\+$\)/\/dev\/\1/p' /proc/partitions 

is a good way, it outputs


/dev/sda
/dev/sdb

That does give me a clue which is the boot device, which is essential as a want to boot from the one and secure delete the other - automatically (stick in, boot, let it run...) as I do that on a bunch of machines at one time, making sure that no data remains on customer computer hard disks.


awk '$2 == "/"' /proc/self/mounts 

That gives you the UUID-number - but how to find the /dev/sd? belonging to it?


(Hint about "use UUID" isn't helpful - I don't want to reconfigure the system, I want to clean it. Of course I can assign new UUIDs when doing that, it costs a second, but that is definitely not the goal, and still misses the fact that I don't know which device /dev/sd? to give a new one.)


dban did the job, but won't be free to use in future.



Answer



You almost have it with your awk invocation. A few small tweaks should be enough.


awk '$1 ~ /^\/dev\// && $2 == "/" { print $1 }' /proc/self/mounts

The $1 ~ /^\/dev\// part requires that any satisfying line names a device somewhere under /dev. Now all we need is a mapping from the device UUID name to the kernel-assigned name, which is easy since all names listed will either be that name directly, or a symbolic link (ultimately) pointing to that name, so we can just feel them to ls -l like so:


ls -l $(awk '$1 ~ /^\/dev\// && $2 == "/" { print $1 }' /proc/self/mounts)

This should give you a single line of output which tells you which physical device the root file system is currently mounted from. It won't be foolproof, so I wouldn't automate anything potentially destructive based on this alone, but for a human who just wants some help in figuring out which device to point at, it should be enough.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...