we want to capture the disk device that belong to the OS ( Linux )

since each linux machine have list of disks that are not the OS , we want to capture the disk that belong to the OS

so by fdisk we can see that boot is on sda1

# sfdisk -l   | grep Linux
/dev/sda1   *      0+     63-     64-    512000   83  Linux
/dev/sda2         63+  19581-  19518- 156773376   8e  Linux LVM

so according to that I created the following command , in order to capture the disk that belong to the OS ( linux )

# OS_DISK=` sfdisk -l   | grep Linux | awk '$2 == "*" {print $1}' | sed s'/\// /g' | awk '{print $2}' | sed 's/[0-9]*//g' `

# echo $OS_DISK    

seems the command do the Job

but Ifeel that this cli is too long and little clumsy

closed as unclear what you're asking by n.st, Thomas, Rui F Ribeiro, Jeff Schaller, JigglyNaga Nov 26 '18 at 9:58

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    What's your defintion of "belonging to the OS"? The root partition? /boot//usr//var/… partitions (in case they're separate)? /home (if it is local)? All local partitions that are mounted? Or all local partitions, even if they're not mounted? There are many special cases (e.g. multiboot systems) and it isn't clear how you would like to handle those. – n.st Nov 25 '18 at 15:42
  • yes boot partitions – yael Nov 25 '18 at 16:38
  • Regardless of the problematic definition of "the OS", there is no single, general, simple, command to get the physical device that hosts a mount point. In this specific case, supposing that you are looking for the physical disk that hosts the partition mounted as /boot, lsblk -n -o PKNAME "$(findmnt -n -T /boot -o SOURCE)" will probably work (PKNAME for lsblk is the "internal parent kernel device name"). – fra-san Nov 25 '18 at 18:47
  1. I find the simplest command to identify the operating system disk to be df /.  Unfortunately, it produces a lot of output (by which I mean a header line and many fields), so you would still need to do some filtering to get just the device name.
  2. You're right; your command is overly long and somewhat clumsy.  awk is a very powerful program; you rarely need to combine it with grep and/or sed, and having multiple awk commands in the same pipeline is almost never necessary.  Your pipeline can be replaced with

    sfdisk -l | awk '/Linux/ && $2 == "*" { gsub("[0-9]", "", $1); split($1, a, "/"); print a[3]; }'
    OK, it's only about a dozen characters shorter, but it's one command instead of five.

    P.S. sed 's/[0-9]*//g' is a slightly dangerous command.  Because of the g, it doesn't really make sense to have the * also.  To see what I mean, try sed 's/[0-9]*/X/g' with various inputs, and compare to s/[0-9]/X/g and s/[0-9]\+/X/g.

  3. OS_DISK=` command ` can be changed to OS_DISK=$(command), and the second form (with the parentheses) is preferred.

Commands fdisk and sfdisk are included in package util-linux. In the same package there is also findmnt command, that you can use like bellow:

findmnt / -no SOURCE

Not the answer you're looking for? Browse other questions tagged or ask your own question.