How to get the Serial Number of a disk by corresponding file in linux?












0















I know that for a disk in linux, we can use commands such as hdparm or smartctl to get its basic information.
But for the implementation of these commands, I think all of them are implemented by reading the corresponding file of a disk in linux. For example, to get the size of a device /dev/sda, we can use cat /sys/block/sda/size instead of using command such hdparm or smartctl. So the question is how to get the serial number of a disk by reading its corresponding file directly.










share|improve this question


















  • 1





    grep SERIAL_SHORT /run/udev/data/b8* (your first drive would be b8:0, the second b8:16, the third b8:32 etc)

    – don_crissti
    Nov 30 '15 at 3:06


















0















I know that for a disk in linux, we can use commands such as hdparm or smartctl to get its basic information.
But for the implementation of these commands, I think all of them are implemented by reading the corresponding file of a disk in linux. For example, to get the size of a device /dev/sda, we can use cat /sys/block/sda/size instead of using command such hdparm or smartctl. So the question is how to get the serial number of a disk by reading its corresponding file directly.










share|improve this question


















  • 1





    grep SERIAL_SHORT /run/udev/data/b8* (your first drive would be b8:0, the second b8:16, the third b8:32 etc)

    – don_crissti
    Nov 30 '15 at 3:06
















0












0








0








I know that for a disk in linux, we can use commands such as hdparm or smartctl to get its basic information.
But for the implementation of these commands, I think all of them are implemented by reading the corresponding file of a disk in linux. For example, to get the size of a device /dev/sda, we can use cat /sys/block/sda/size instead of using command such hdparm or smartctl. So the question is how to get the serial number of a disk by reading its corresponding file directly.










share|improve this question














I know that for a disk in linux, we can use commands such as hdparm or smartctl to get its basic information.
But for the implementation of these commands, I think all of them are implemented by reading the corresponding file of a disk in linux. For example, to get the size of a device /dev/sda, we can use cat /sys/block/sda/size instead of using command such hdparm or smartctl. So the question is how to get the serial number of a disk by reading its corresponding file directly.







linux command io disk






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 30 '15 at 2:41









Jin CaiJin Cai

62




62








  • 1





    grep SERIAL_SHORT /run/udev/data/b8* (your first drive would be b8:0, the second b8:16, the third b8:32 etc)

    – don_crissti
    Nov 30 '15 at 3:06
















  • 1





    grep SERIAL_SHORT /run/udev/data/b8* (your first drive would be b8:0, the second b8:16, the third b8:32 etc)

    – don_crissti
    Nov 30 '15 at 3:06










1




1





grep SERIAL_SHORT /run/udev/data/b8* (your first drive would be b8:0, the second b8:16, the third b8:32 etc)

– don_crissti
Nov 30 '15 at 3:06







grep SERIAL_SHORT /run/udev/data/b8* (your first drive would be b8:0, the second b8:16, the third b8:32 etc)

– don_crissti
Nov 30 '15 at 3:06












3 Answers
3






active

oldest

votes


















0














You could also use /proc/partitions to find that info.



That file gives you the size in number of blocks that all your partitions have, But also gives you the same information for your entire disks.






share|improve this answer


























  • I don't need information of number of blocks but the serial number.

    – Jin Cai
    Nov 30 '15 at 5:57



















0














You could use strace to find how smartctl get the serial number of a disk. CMD line likes this: strace smartctl -a /dev/sda. Maybe it just get the serial by ioctl.






share|improve this answer































    0














    I just have a try. It do get the serial by ioctl. This is some output of strace:



    open("/dev/sda", O_RDONLY|O_NONBLOCK)   = 3
    fcntl(3, F_SETFD, FD_CLOEXEC) = 0
    ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[6]=[12, 00, 00, 00, 24, 00], mx_sb_len=32, iovec_count=0, dxfer_len=36, timeout=20000, flags=0, data[36]=["52[ATA WDC WD5000LPVX-0"...], status=00, masked_status=00, sb[0]=, host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0
    ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[16]=[85, 08, 0e, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, ec, 00], mx_sb_len=32, iovec_count=0, dxfer_len=512, timeout=20000, flags=0, data[512]=["zB377?731020? W -DXW11"...], status=00, masked_status=00, sb[0]=, host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0
    ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[16]=[85, 08, 0e, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, ec, 00], mx_sb_len=32, iovec_count=0, dxfer_len=512, timeout=20000, flags=0, data[512]=["zB377?731020? W -DXW11"...], status=00, masked_status=00, sb[0]=, host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0





    share|improve this answer

























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f246307%2fhow-to-get-the-serial-number-of-a-disk-by-corresponding-file-in-linux%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You could also use /proc/partitions to find that info.



      That file gives you the size in number of blocks that all your partitions have, But also gives you the same information for your entire disks.






      share|improve this answer


























      • I don't need information of number of blocks but the serial number.

        – Jin Cai
        Nov 30 '15 at 5:57
















      0














      You could also use /proc/partitions to find that info.



      That file gives you the size in number of blocks that all your partitions have, But also gives you the same information for your entire disks.






      share|improve this answer


























      • I don't need information of number of blocks but the serial number.

        – Jin Cai
        Nov 30 '15 at 5:57














      0












      0








      0







      You could also use /proc/partitions to find that info.



      That file gives you the size in number of blocks that all your partitions have, But also gives you the same information for your entire disks.






      share|improve this answer















      You could also use /proc/partitions to find that info.



      That file gives you the size in number of blocks that all your partitions have, But also gives you the same information for your entire disks.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 30 '15 at 5:37









      Tejas

      1,83621940




      1,83621940










      answered Nov 30 '15 at 4:56









      VictorVictor

      112




      112













      • I don't need information of number of blocks but the serial number.

        – Jin Cai
        Nov 30 '15 at 5:57



















      • I don't need information of number of blocks but the serial number.

        – Jin Cai
        Nov 30 '15 at 5:57

















      I don't need information of number of blocks but the serial number.

      – Jin Cai
      Nov 30 '15 at 5:57





      I don't need information of number of blocks but the serial number.

      – Jin Cai
      Nov 30 '15 at 5:57













      0














      You could use strace to find how smartctl get the serial number of a disk. CMD line likes this: strace smartctl -a /dev/sda. Maybe it just get the serial by ioctl.






      share|improve this answer




























        0














        You could use strace to find how smartctl get the serial number of a disk. CMD line likes this: strace smartctl -a /dev/sda. Maybe it just get the serial by ioctl.






        share|improve this answer


























          0












          0








          0







          You could use strace to find how smartctl get the serial number of a disk. CMD line likes this: strace smartctl -a /dev/sda. Maybe it just get the serial by ioctl.






          share|improve this answer













          You could use strace to find how smartctl get the serial number of a disk. CMD line likes this: strace smartctl -a /dev/sda. Maybe it just get the serial by ioctl.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 30 '15 at 8:28









          Zhengyuan LiuZhengyuan Liu

          315




          315























              0














              I just have a try. It do get the serial by ioctl. This is some output of strace:



              open("/dev/sda", O_RDONLY|O_NONBLOCK)   = 3
              fcntl(3, F_SETFD, FD_CLOEXEC) = 0
              ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[6]=[12, 00, 00, 00, 24, 00], mx_sb_len=32, iovec_count=0, dxfer_len=36, timeout=20000, flags=0, data[36]=["52[ATA WDC WD5000LPVX-0"...], status=00, masked_status=00, sb[0]=, host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0
              ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[16]=[85, 08, 0e, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, ec, 00], mx_sb_len=32, iovec_count=0, dxfer_len=512, timeout=20000, flags=0, data[512]=["zB377?731020? W -DXW11"...], status=00, masked_status=00, sb[0]=, host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0
              ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[16]=[85, 08, 0e, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, ec, 00], mx_sb_len=32, iovec_count=0, dxfer_len=512, timeout=20000, flags=0, data[512]=["zB377?731020? W -DXW11"...], status=00, masked_status=00, sb[0]=, host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0





              share|improve this answer






























                0














                I just have a try. It do get the serial by ioctl. This is some output of strace:



                open("/dev/sda", O_RDONLY|O_NONBLOCK)   = 3
                fcntl(3, F_SETFD, FD_CLOEXEC) = 0
                ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[6]=[12, 00, 00, 00, 24, 00], mx_sb_len=32, iovec_count=0, dxfer_len=36, timeout=20000, flags=0, data[36]=["52[ATA WDC WD5000LPVX-0"...], status=00, masked_status=00, sb[0]=, host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0
                ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[16]=[85, 08, 0e, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, ec, 00], mx_sb_len=32, iovec_count=0, dxfer_len=512, timeout=20000, flags=0, data[512]=["zB377?731020? W -DXW11"...], status=00, masked_status=00, sb[0]=, host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0
                ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[16]=[85, 08, 0e, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, ec, 00], mx_sb_len=32, iovec_count=0, dxfer_len=512, timeout=20000, flags=0, data[512]=["zB377?731020? W -DXW11"...], status=00, masked_status=00, sb[0]=, host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0





                share|improve this answer




























                  0












                  0








                  0







                  I just have a try. It do get the serial by ioctl. This is some output of strace:



                  open("/dev/sda", O_RDONLY|O_NONBLOCK)   = 3
                  fcntl(3, F_SETFD, FD_CLOEXEC) = 0
                  ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[6]=[12, 00, 00, 00, 24, 00], mx_sb_len=32, iovec_count=0, dxfer_len=36, timeout=20000, flags=0, data[36]=["52[ATA WDC WD5000LPVX-0"...], status=00, masked_status=00, sb[0]=, host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0
                  ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[16]=[85, 08, 0e, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, ec, 00], mx_sb_len=32, iovec_count=0, dxfer_len=512, timeout=20000, flags=0, data[512]=["zB377?731020? W -DXW11"...], status=00, masked_status=00, sb[0]=, host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0
                  ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[16]=[85, 08, 0e, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, ec, 00], mx_sb_len=32, iovec_count=0, dxfer_len=512, timeout=20000, flags=0, data[512]=["zB377?731020? W -DXW11"...], status=00, masked_status=00, sb[0]=, host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0





                  share|improve this answer















                  I just have a try. It do get the serial by ioctl. This is some output of strace:



                  open("/dev/sda", O_RDONLY|O_NONBLOCK)   = 3
                  fcntl(3, F_SETFD, FD_CLOEXEC) = 0
                  ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[6]=[12, 00, 00, 00, 24, 00], mx_sb_len=32, iovec_count=0, dxfer_len=36, timeout=20000, flags=0, data[36]=["52[ATA WDC WD5000LPVX-0"...], status=00, masked_status=00, sb[0]=, host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0
                  ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[16]=[85, 08, 0e, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, ec, 00], mx_sb_len=32, iovec_count=0, dxfer_len=512, timeout=20000, flags=0, data[512]=["zB377?731020? W -DXW11"...], status=00, masked_status=00, sb[0]=, host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0
                  ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[16]=[85, 08, 0e, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, ec, 00], mx_sb_len=32, iovec_count=0, dxfer_len=512, timeout=20000, flags=0, data[512]=["zB377?731020? W -DXW11"...], status=00, masked_status=00, sb[0]=, host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 30 '15 at 8:35









                  Archemar

                  20.2k93773




                  20.2k93773










                  answered Nov 30 '15 at 8:33









                  Zhengyuan LiuZhengyuan Liu

                  315




                  315






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f246307%2fhow-to-get-the-serial-number-of-a-disk-by-corresponding-file-in-linux%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

                      is 'sed' thread safe

                      How to make a Squid Proxy server?