How do commands like fdisk -l find the sector size?

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











up vote
4
down vote

favorite












Does the MBR of the disk contain this information and therefore when i call a command like fdisk, a kernel level code eventually runs and reads it from a specific part in MBR? If so, which part of it? What offset?



If it's not in the MBR, then how can these types of commands find it? They can't be reading it from the beginning of a partition considering they need to calculate the starting address of that partition and they need the sector size to do so, don't they?



How are commands like fdisk implemented to find this information? Where do they read it from?










share|improve this question



























    up vote
    4
    down vote

    favorite












    Does the MBR of the disk contain this information and therefore when i call a command like fdisk, a kernel level code eventually runs and reads it from a specific part in MBR? If so, which part of it? What offset?



    If it's not in the MBR, then how can these types of commands find it? They can't be reading it from the beginning of a partition considering they need to calculate the starting address of that partition and they need the sector size to do so, don't they?



    How are commands like fdisk implemented to find this information? Where do they read it from?










    share|improve this question

























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      Does the MBR of the disk contain this information and therefore when i call a command like fdisk, a kernel level code eventually runs and reads it from a specific part in MBR? If so, which part of it? What offset?



      If it's not in the MBR, then how can these types of commands find it? They can't be reading it from the beginning of a partition considering they need to calculate the starting address of that partition and they need the sector size to do so, don't they?



      How are commands like fdisk implemented to find this information? Where do they read it from?










      share|improve this question















      Does the MBR of the disk contain this information and therefore when i call a command like fdisk, a kernel level code eventually runs and reads it from a specific part in MBR? If so, which part of it? What offset?



      If it's not in the MBR, then how can these types of commands find it? They can't be reading it from the beginning of a partition considering they need to calculate the starting address of that partition and they need the sector size to do so, don't they?



      How are commands like fdisk implemented to find this information? Where do they read it from?







      linux hard-disk command disk cluster






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago

























      asked 1 hour ago









      John P

      1898




      1898




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote













          A device’s sector size isn’t stored in the MBR.



          User space commands such as fdisk use the BLKBSZGET and BLKSSZGET ioctls to retrieve the sector sizes from disks. Those ioctls are handled by drivers in the kernel, which retrieve the relevant information from the drives themselves.



          (There isn’t much documentation about the relevant ioctls; you need to check the kernel source code.)



          You can see the relevant information using other tools which query drives directly, for example hdparm. On a small SSD, hdparm -I tells me



          [...]
          Logical Sector size: 512 bytes
          Physical Sector size: 512 bytes
          Logical Sector-0 offset: 0 bytes
          [...]
          cache/buffer size = unknown
          Form Factor: 2.5 inch
          Nominal Media Rotation Rate: Solid State Device
          [...]


          On a large spinning disk with 4K sectors, I get instead



          [...]
          Logical Sector size: 512 bytes
          Physical Sector size: 4096 bytes
          Logical Sector-0 offset: 0 bytes
          [...]
          cache/buffer size = unknown
          Form Factor: 3.5 inch
          Nominal Media Rotation Rate: 5400
          [...]





          share|improve this answer






















          • Does this mean that hdparm sends ATA commands to the device itself? I would have thought you'd have to go through the kernel to get to the device.
            – mjb2kmn
            1 hour ago










          • So if we are talking about the lowest level, where on the disk this sector size is stored and "who" retrieves this information first and pass it to upper levels when i call something like fdisk ? a kernel code? I asked this question because i was looking at the MBR of my hard disk and i was wondering if the sector size is stored there and a kernel level code reads it from there or not?
            – John P
            1 hour ago











          • @mjb2kmn the two aren’t exclusive ;-). hdparm it sends ATA commands to the device itself, using the SG_IO ioctl (so with the help of the kernel).
            – Stephen Kitt
            42 mins ago










          • @JohnP the kernel asks the device, which typically stores the information in its firmware (but that’s really up to the device).
            – Stephen Kitt
            41 mins ago










          • @StephenKitt so it is not stored in the MBR, correct?
            – John P
            37 mins ago


















          up vote
          2
          down vote













          There are a few questions in there, I tried to track down the answer to "Where does fdisk find the sector size of a disk?" The best I've found is that it gets the info from ioctl.



          ioctl info on Wikipedia and Linux man page.



          fdisk source code link






          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',
            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%2f479199%2fhow-do-commands-like-fdisk-l-find-the-sector-size%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote













            A device’s sector size isn’t stored in the MBR.



            User space commands such as fdisk use the BLKBSZGET and BLKSSZGET ioctls to retrieve the sector sizes from disks. Those ioctls are handled by drivers in the kernel, which retrieve the relevant information from the drives themselves.



            (There isn’t much documentation about the relevant ioctls; you need to check the kernel source code.)



            You can see the relevant information using other tools which query drives directly, for example hdparm. On a small SSD, hdparm -I tells me



            [...]
            Logical Sector size: 512 bytes
            Physical Sector size: 512 bytes
            Logical Sector-0 offset: 0 bytes
            [...]
            cache/buffer size = unknown
            Form Factor: 2.5 inch
            Nominal Media Rotation Rate: Solid State Device
            [...]


            On a large spinning disk with 4K sectors, I get instead



            [...]
            Logical Sector size: 512 bytes
            Physical Sector size: 4096 bytes
            Logical Sector-0 offset: 0 bytes
            [...]
            cache/buffer size = unknown
            Form Factor: 3.5 inch
            Nominal Media Rotation Rate: 5400
            [...]





            share|improve this answer






















            • Does this mean that hdparm sends ATA commands to the device itself? I would have thought you'd have to go through the kernel to get to the device.
              – mjb2kmn
              1 hour ago










            • So if we are talking about the lowest level, where on the disk this sector size is stored and "who" retrieves this information first and pass it to upper levels when i call something like fdisk ? a kernel code? I asked this question because i was looking at the MBR of my hard disk and i was wondering if the sector size is stored there and a kernel level code reads it from there or not?
              – John P
              1 hour ago











            • @mjb2kmn the two aren’t exclusive ;-). hdparm it sends ATA commands to the device itself, using the SG_IO ioctl (so with the help of the kernel).
              – Stephen Kitt
              42 mins ago










            • @JohnP the kernel asks the device, which typically stores the information in its firmware (but that’s really up to the device).
              – Stephen Kitt
              41 mins ago










            • @StephenKitt so it is not stored in the MBR, correct?
              – John P
              37 mins ago















            up vote
            5
            down vote













            A device’s sector size isn’t stored in the MBR.



            User space commands such as fdisk use the BLKBSZGET and BLKSSZGET ioctls to retrieve the sector sizes from disks. Those ioctls are handled by drivers in the kernel, which retrieve the relevant information from the drives themselves.



            (There isn’t much documentation about the relevant ioctls; you need to check the kernel source code.)



            You can see the relevant information using other tools which query drives directly, for example hdparm. On a small SSD, hdparm -I tells me



            [...]
            Logical Sector size: 512 bytes
            Physical Sector size: 512 bytes
            Logical Sector-0 offset: 0 bytes
            [...]
            cache/buffer size = unknown
            Form Factor: 2.5 inch
            Nominal Media Rotation Rate: Solid State Device
            [...]


            On a large spinning disk with 4K sectors, I get instead



            [...]
            Logical Sector size: 512 bytes
            Physical Sector size: 4096 bytes
            Logical Sector-0 offset: 0 bytes
            [...]
            cache/buffer size = unknown
            Form Factor: 3.5 inch
            Nominal Media Rotation Rate: 5400
            [...]





            share|improve this answer






















            • Does this mean that hdparm sends ATA commands to the device itself? I would have thought you'd have to go through the kernel to get to the device.
              – mjb2kmn
              1 hour ago










            • So if we are talking about the lowest level, where on the disk this sector size is stored and "who" retrieves this information first and pass it to upper levels when i call something like fdisk ? a kernel code? I asked this question because i was looking at the MBR of my hard disk and i was wondering if the sector size is stored there and a kernel level code reads it from there or not?
              – John P
              1 hour ago











            • @mjb2kmn the two aren’t exclusive ;-). hdparm it sends ATA commands to the device itself, using the SG_IO ioctl (so with the help of the kernel).
              – Stephen Kitt
              42 mins ago










            • @JohnP the kernel asks the device, which typically stores the information in its firmware (but that’s really up to the device).
              – Stephen Kitt
              41 mins ago










            • @StephenKitt so it is not stored in the MBR, correct?
              – John P
              37 mins ago













            up vote
            5
            down vote










            up vote
            5
            down vote









            A device’s sector size isn’t stored in the MBR.



            User space commands such as fdisk use the BLKBSZGET and BLKSSZGET ioctls to retrieve the sector sizes from disks. Those ioctls are handled by drivers in the kernel, which retrieve the relevant information from the drives themselves.



            (There isn’t much documentation about the relevant ioctls; you need to check the kernel source code.)



            You can see the relevant information using other tools which query drives directly, for example hdparm. On a small SSD, hdparm -I tells me



            [...]
            Logical Sector size: 512 bytes
            Physical Sector size: 512 bytes
            Logical Sector-0 offset: 0 bytes
            [...]
            cache/buffer size = unknown
            Form Factor: 2.5 inch
            Nominal Media Rotation Rate: Solid State Device
            [...]


            On a large spinning disk with 4K sectors, I get instead



            [...]
            Logical Sector size: 512 bytes
            Physical Sector size: 4096 bytes
            Logical Sector-0 offset: 0 bytes
            [...]
            cache/buffer size = unknown
            Form Factor: 3.5 inch
            Nominal Media Rotation Rate: 5400
            [...]





            share|improve this answer














            A device’s sector size isn’t stored in the MBR.



            User space commands such as fdisk use the BLKBSZGET and BLKSSZGET ioctls to retrieve the sector sizes from disks. Those ioctls are handled by drivers in the kernel, which retrieve the relevant information from the drives themselves.



            (There isn’t much documentation about the relevant ioctls; you need to check the kernel source code.)



            You can see the relevant information using other tools which query drives directly, for example hdparm. On a small SSD, hdparm -I tells me



            [...]
            Logical Sector size: 512 bytes
            Physical Sector size: 512 bytes
            Logical Sector-0 offset: 0 bytes
            [...]
            cache/buffer size = unknown
            Form Factor: 2.5 inch
            Nominal Media Rotation Rate: Solid State Device
            [...]


            On a large spinning disk with 4K sectors, I get instead



            [...]
            Logical Sector size: 512 bytes
            Physical Sector size: 4096 bytes
            Logical Sector-0 offset: 0 bytes
            [...]
            cache/buffer size = unknown
            Form Factor: 3.5 inch
            Nominal Media Rotation Rate: 5400
            [...]






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 32 mins ago

























            answered 1 hour ago









            Stephen Kitt

            154k23338407




            154k23338407











            • Does this mean that hdparm sends ATA commands to the device itself? I would have thought you'd have to go through the kernel to get to the device.
              – mjb2kmn
              1 hour ago










            • So if we are talking about the lowest level, where on the disk this sector size is stored and "who" retrieves this information first and pass it to upper levels when i call something like fdisk ? a kernel code? I asked this question because i was looking at the MBR of my hard disk and i was wondering if the sector size is stored there and a kernel level code reads it from there or not?
              – John P
              1 hour ago











            • @mjb2kmn the two aren’t exclusive ;-). hdparm it sends ATA commands to the device itself, using the SG_IO ioctl (so with the help of the kernel).
              – Stephen Kitt
              42 mins ago










            • @JohnP the kernel asks the device, which typically stores the information in its firmware (but that’s really up to the device).
              – Stephen Kitt
              41 mins ago










            • @StephenKitt so it is not stored in the MBR, correct?
              – John P
              37 mins ago

















            • Does this mean that hdparm sends ATA commands to the device itself? I would have thought you'd have to go through the kernel to get to the device.
              – mjb2kmn
              1 hour ago










            • So if we are talking about the lowest level, where on the disk this sector size is stored and "who" retrieves this information first and pass it to upper levels when i call something like fdisk ? a kernel code? I asked this question because i was looking at the MBR of my hard disk and i was wondering if the sector size is stored there and a kernel level code reads it from there or not?
              – John P
              1 hour ago











            • @mjb2kmn the two aren’t exclusive ;-). hdparm it sends ATA commands to the device itself, using the SG_IO ioctl (so with the help of the kernel).
              – Stephen Kitt
              42 mins ago










            • @JohnP the kernel asks the device, which typically stores the information in its firmware (but that’s really up to the device).
              – Stephen Kitt
              41 mins ago










            • @StephenKitt so it is not stored in the MBR, correct?
              – John P
              37 mins ago
















            Does this mean that hdparm sends ATA commands to the device itself? I would have thought you'd have to go through the kernel to get to the device.
            – mjb2kmn
            1 hour ago




            Does this mean that hdparm sends ATA commands to the device itself? I would have thought you'd have to go through the kernel to get to the device.
            – mjb2kmn
            1 hour ago












            So if we are talking about the lowest level, where on the disk this sector size is stored and "who" retrieves this information first and pass it to upper levels when i call something like fdisk ? a kernel code? I asked this question because i was looking at the MBR of my hard disk and i was wondering if the sector size is stored there and a kernel level code reads it from there or not?
            – John P
            1 hour ago





            So if we are talking about the lowest level, where on the disk this sector size is stored and "who" retrieves this information first and pass it to upper levels when i call something like fdisk ? a kernel code? I asked this question because i was looking at the MBR of my hard disk and i was wondering if the sector size is stored there and a kernel level code reads it from there or not?
            – John P
            1 hour ago













            @mjb2kmn the two aren’t exclusive ;-). hdparm it sends ATA commands to the device itself, using the SG_IO ioctl (so with the help of the kernel).
            – Stephen Kitt
            42 mins ago




            @mjb2kmn the two aren’t exclusive ;-). hdparm it sends ATA commands to the device itself, using the SG_IO ioctl (so with the help of the kernel).
            – Stephen Kitt
            42 mins ago












            @JohnP the kernel asks the device, which typically stores the information in its firmware (but that’s really up to the device).
            – Stephen Kitt
            41 mins ago




            @JohnP the kernel asks the device, which typically stores the information in its firmware (but that’s really up to the device).
            – Stephen Kitt
            41 mins ago












            @StephenKitt so it is not stored in the MBR, correct?
            – John P
            37 mins ago





            @StephenKitt so it is not stored in the MBR, correct?
            – John P
            37 mins ago













            up vote
            2
            down vote













            There are a few questions in there, I tried to track down the answer to "Where does fdisk find the sector size of a disk?" The best I've found is that it gets the info from ioctl.



            ioctl info on Wikipedia and Linux man page.



            fdisk source code link






            share|improve this answer
























              up vote
              2
              down vote













              There are a few questions in there, I tried to track down the answer to "Where does fdisk find the sector size of a disk?" The best I've found is that it gets the info from ioctl.



              ioctl info on Wikipedia and Linux man page.



              fdisk source code link






              share|improve this answer






















                up vote
                2
                down vote










                up vote
                2
                down vote









                There are a few questions in there, I tried to track down the answer to "Where does fdisk find the sector size of a disk?" The best I've found is that it gets the info from ioctl.



                ioctl info on Wikipedia and Linux man page.



                fdisk source code link






                share|improve this answer












                There are a few questions in there, I tried to track down the answer to "Where does fdisk find the sector size of a disk?" The best I've found is that it gets the info from ioctl.



                ioctl info on Wikipedia and Linux man page.



                fdisk source code link







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 1 hour ago









                mjb2kmn

                336113




                336113



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f479199%2fhow-do-commands-like-fdisk-l-find-the-sector-size%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery