22

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?

24

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
  • So it was 4096 bytes, not 512 bytes. – Hibou57 Aug 22 '17 at 21:23
24

According to the documentation for the queue sysfs files:

# cat /sys/block/sda/queue/hw_sector_size
512
  • 1
    This will not work for arbitrary image files. How should the kernel know about them? – Christian Pietsch Jul 22 '14 at 11:26
  • 1
    @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 Jul 22 '18 at 12:26
0

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
0

One can just use file(1) command and do the math himself. This is 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 match again, 4096 bytes × 16 sectors = 64 KiB per cluster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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