Linux + how to capture the OS disk device [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












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
sda


seems the command do the Job



but Ifeel that this cli is too long and little clumsy










share|improve this question















closed as unclear what you're asking by n.st, Thomas, Rui F Ribeiro, Jeff Schaller, JigglyNaga Nov 26 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 at 15:42










  • yes boot partitions
    – yael
    Nov 25 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 at 18:47















up vote
-1
down vote

favorite












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
sda


seems the command do the Job



but Ifeel that this cli is too long and little clumsy










share|improve this question















closed as unclear what you're asking by n.st, Thomas, Rui F Ribeiro, Jeff Schaller, JigglyNaga Nov 26 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 at 15:42










  • yes boot partitions
    – yael
    Nov 25 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 at 18:47













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











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
sda


seems the command do the Job



but Ifeel that this cli is too long and little clumsy










share|improve this question















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
sda


seems the command do the Job



but Ifeel that this cli is too long and little clumsy







linux scripting disk fdisk lsblk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 at 15:31









Jeff Schaller

37k1052121




37k1052121










asked Nov 25 at 14:29









yael

2,3341955




2,3341955




closed as unclear what you're asking by n.st, Thomas, Rui F Ribeiro, Jeff Schaller, JigglyNaga Nov 26 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.






closed as unclear what you're asking by n.st, Thomas, Rui F Ribeiro, Jeff Schaller, JigglyNaga Nov 26 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 at 15:42










  • yes boot partitions
    – yael
    Nov 25 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 at 18:47













  • 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 at 15:42










  • yes boot partitions
    – yael
    Nov 25 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 at 18:47








2




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 at 15:42




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 at 15:42












yes boot partitions
– yael
Nov 25 at 16:38




yes boot partitions
– yael
Nov 25 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 at 18:47





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 at 18:47











2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










  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.





share|improve this answer





























    up vote
    2
    down vote













    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
    /dev/sda6





    share|improve this answer





























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      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.





      share|improve this answer


























        up vote
        1
        down vote



        accepted










        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.





        share|improve this answer
























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          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.





          share|improve this answer














          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.






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 25 at 23:53

























          answered Nov 25 at 23:20









          G-Man

          12.3k92961




          12.3k92961






















              up vote
              2
              down vote













              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
              /dev/sda6





              share|improve this answer


























                up vote
                2
                down vote













                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
                /dev/sda6





                share|improve this answer
























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  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
                  /dev/sda6





                  share|improve this answer














                  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
                  /dev/sda6






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 25 at 19:11

























                  answered Nov 25 at 18:16









                  George Vasiliou

                  5,57531028




                  5,57531028












                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?