40

I answered this question, assuming that the *.img file had a sector size of 512.

How do I query a device, or the image of a device, to find the correct sector size?

5 Answers 5

43

fdisk -l (that's lower L in the parameter) will show you, among other information, the sector size too.

$ sudo fdisk -l

Disk /dev/sda: 150.3 GB, 150323855360 bytes
255 heads, 63 sectors/track, 18275 cylinders, total 293601280 sectors
Units = sectors of 1 * 512 = 512 bytes

   Device Boot      Start         End      Blocks   Id  System
   /dev/sda1   *          63      208844      104391   83  Linux
   /dev/sda2          208845   209712509   104751832+  83  Linux

This shows that the sector size is 512 bytes.

EDIT: Newer versions of fdisk e.g., fdisk (from package util-linux 2.20.1), will also show you the logical and physical sector sizes. For example, relevant output from a "WDC WD10EFRX 1TB drive":

Disk /dev/sdn: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders, total 1953525168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
1
  • So it was 4096 bytes, not 512 bytes.
    – Hibou57
    Commented Aug 22, 2017 at 21:23
31

According to the documentation for the queue sysfs files:

# cat /sys/block/sda/queue/hw_sector_size
512
4
  • 2
    This will not work for arbitrary image files. How should the kernel know about them? Commented Jul 22, 2014 at 11:26
  • 4
    @ChristianPietsch arbitrary image files are normally mounted via loop devices, so this will work for them. Just be sure to use loopN instead of sdX. Still, it's rather pointless in a sense, since image files don't really have any sectors, and this info will just tell you the sector size of currently-mounted device.
    – Ruslan
    Commented Jul 22, 2018 at 12:26
  • 1
    strange, it shows 512 for SSD, but I am mostly certain that it is 4096 Commented May 25, 2023 at 21:33
  • @ChristianPietsch is right: I can mount a file entirely in user space and the kernel will never know about it: nbdfuse dev/nbd0 --socket-activation qemu-nbd --nocache --aio=threads -f qcow2 $image and then run fdisk ~/dev/nbd0 -l
    – user247243
    Commented Feb 11 at 4:26
6

One can just use file(1) command and do the math himself. This works even if you only have an image of the FAT system.

For example:

$ sudo file -s /dev/sda5 | tr , '\n'
/dev/sda5: DOS/MBR boot sector
 code offset 0x58+2
 OEM-ID "mkfs.fat"
 sectors/cluster 64
 reserved sectors 64
 Media descriptor 0xf8
 sectors/track 63
 heads 255
 hidden sectors 147632128
 sectors 536870912 (volumes > 32 MB)
 FAT (32 bit)
 sectors/FAT 65536
 serial number 0x9f981691
 unlabeled

This partition has 64 sectors per cluster. The number of bytes in each sector is 512, as it is the default, therefore, the cluster size is 32KB.

Notice that the sector size can be greater than 512 (FAT specification limits it to 4096 but you can go higher). When that happens, you will see an additional "Bytes/sector" field in the output.

$ sudo mkfs.fat -S 4096 -s 16 /dev/sda5
mkfs.fat 4.1 (2017-01-24)

$ sudo file -s /dev/sda5 | tr , '\n'
/dev/sda5: DOS/MBR boot sector
 code offset 0x58+2
 OEM-ID "mkfs.fat"
 Bytes/sector 4096
 sectors/cluster 16
 Media descriptor 0xf8
 sectors/track 63
 heads 255
 hidden sectors 147632128
 sectors 67108864 (volumes > 32 MB)
 FAT (32 bit)
 sectors/FAT 4096
 serial number 0xb059d826
 label: unlabeled

Do the math again, 4096 bytes × 16 sectors = 64 KiB per cluster.

4

thanks above answers, about fdisk showing physical sector size, and about /sys path... but one note... by Lucas in book Absolute FreeBSD.... almost every hdd will lie about sector size, so in case of linux

/sys/block/sda/queue/hw_sector_size

will always, as far as I can understand, show 512 for better performance it's good to take in account physical sector size, real, that really used in hardware logic. It showed by fdisk, and it present in

cat /sys/block/sdc/queue/physical_block_size 
4096
3
  • /sys/block/sda/queue/hw_sector_size shows 4096 on many non-spinning-rust disks.
    – jbg
    Commented Jul 27, 2022 at 12:38
  • @jbg with SSDs all bets are off and "sector size" is useless metric there - it actually refers more to an IO unit size than to a sector. I don't understand how you fail to see, that it has nothing with the question at hand, whatsoever. The fact is majority that of spindles these days are so called AD/AFD (Advanced Format Drive) and these will always lie about having 512 byte sectors, and present 4096 only to layers capable to use them. This is by design, so that these drives can be used in old equipment and whatnot. Of course 512b sectors are completely emulated here.
    – etosan
    Commented Dec 26, 2022 at 14:43
  • Only most modern spindle drives, most consumers never saw yet, will not advertise 512b sectors and will advertise only 4k sectors (despite support for 4K being present in almost all living OSes). These are so called 4Kn (4K native) drives, and even to this day many consumer level products (like their BIOSes and UEFI firmwares) might caca themselves completely and totally, when dealing with such a drive as a primary boot device, because they don't even talk to you in 512b way. See en.wikipedia.org/wiki/Advanced_Format for more.
    – etosan
    Commented Dec 26, 2022 at 14:49
-1

try this

disk_sector_num=$(cat /sys/block/sdc/size)
disk_size_gb=$(expr $disk_sector_num / 2097152)
echo "sdc = ${disk_size_gb} GB"

sdc = 28 GB

2
  • 2
    You should perhaps explain that this code block is hard-coded for disk "sdc", and I'd personally be curious what the "magic" number of 2097152 means. (Please edit your answer to provide any clarifications, then feel free to flag this comment as no longer needed). Thank you!
    – Jeff Schaller
    Commented Sep 17, 2021 at 17:43
  • @JeffSchaller: It's just plain old wrong. It assumes 512 byte sector size.
    – Joshua
    Commented Mar 25 at 2:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .