Currently trying to find a BULLET proof way to find the sector size of a FLASH based device (testing with USB thumb drive), not getting a convincing reply anywhere else

using stat gives me the size = 4096 bytes

struct stat info;
const char* device = "/dev/sdc";
if (stat(device, &info))
    std::cout << "stat() error" << strerror(errno) << "\r\n";

std::cout << "Prefered block size for '" << device << "' is " << info.st_blksize << " byte" << std::endl;

but fdisk says 512 bytes

Disk /dev/sdc: 3.8 GiB, 4047503360 bytes, 7905280 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos

Is there any other way I can trust?

  • 1
    The erase block / page size might be of more interest to you, since flash memory's only erasable by the block, and they could be huge compared to 512-bytes or 4k, if I recall up to 1M, 2M, 4M... – Xen2050 Jan 15 '19 at 7:43
  • See superuser.com/questions/1311708/… – sawdust Jan 15 '19 at 8:16

Most modern disks use Advanced Format.

Advanced Format disks translate each 4,096-byte physical sector into eight 512-byte logical sectors. To the firmware, operating system, and all disk utilities, the disk appears to have 512-byte sectors, even though the underlying physical sector size is 4,096 bytes.

Fdisk in the latest Linux versions now reports the logical sector size, rather than the physical one, which is the reason for the inconsistency that you noticed.

| improve this answer | |

stat() is reporting the preferred block size of the filesystem, not of the underlying device.

Most USB Mass Storage devices -- especially inexpensive ones, like flash drives -- will report 512-byte sectors for compatibility reasons. The actual block size of the underlying flash memory is likely to be much larger, but is not exposed by the device, so there's no way to detect it from software.

| improve this answer | |

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.