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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI 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?
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
According to the documentation for the queue sysfs files:
# cat /sys/block/sda/queue/hw_sector_size
512
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.
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.
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
/sys/block/sda/queue/hw_sector_size
shows 4096 on many non-spinning-rust disks.
try this
disk_sector_num=$(cat /sys/block/sdc/size)
disk_size_gb=$(expr $disk_sector_num / 2097152)
echo "sdc = ${disk_size_gb} GB"