Obtain the size of the storage media sector in Linux

Source: Internet
Author: User

Obtained through the proc file system, there is a file named partitions in the proc file system. This file contains the hard disk and partition information of the local disk. According to the device naming rules in Linux, if the last character of the device name is a number, it should be a partition, otherwise it is a hard disk. Based on this file, you can also know the hard disk device name, the number of partitions in each hard disk, and the name of the device in each partition.

Obtain the disk sector size:

/* Get size in bytes */

Int
Blkdev_get_size (int fd, unsigned long * bytes)
{
Unsigned long size;
Int ver = get_linux_version ();
/* Kernels 2.4.15-2.4.17, had a broken BLKGETSIZE64 */
If (ver> = KERNEL_VERSION (2, 6, 0) |
(Ver> = KERNEL_VERSION (2, 4, 18) & ver <KERNEL_VERSION (2, 5, 0 ))){
If (ioctl (fd, BLKGETSIZE64, bytes)> = 0)
Return 0;
}
If (ioctl (fd, BLKGETSIZE, & size)> = 0 ){
* Bytes = (unsigned long) size <9 );
Return 0;
}
Return-1;

}

According to the ATA8-ACS documentation specification, our operating system will have a data structure of ata identify device to indicate a disk DEVICE, either linux or windows, here, I use linux as an example to illustrate that windows can also be obtained through APIS.
In linux:

Unsigned short word106 = 0;
Struct hd_driveid id;
Int fd = open (diskname, O_RDONLY );
Ioctl (fd, HDIO_GET_IDENTITY, & id );
Word106 = id. words1__125 [2];

Obtain the 106th characters of this structure (note that it is a word, not a byte ). This field defines the disk sector size. Let's see how this field defines the sector size.
Bit 15 is fixed to 0.
Bit 14 is fixed to 1.
If bit 13 is 1, it indicates that a logical sector has multiple physical sectors.
If bit 12 is set to 1, the device is formatted into a logical sector with a size of more than 256 characters.
Bit 11-4 is reserved.
Bit 3-0 if bit 13 is 1, these three bits are used to indicate the size of the logical sector.
We will focus on how the first four bits represent the sector size. I use a formula to indicate that sector_size = 2 ^ x * Physical sector size (512b ).
That is to say, the size of the logical sector is the size of the physical sector to the x power of 2, that is, the size of the sector = 2 ^ x * 512. X is the value of the first four digits of the word.
For example, if it is a large slice and the slice size is 4 K, it is 4096 bytes:
Bit 13 = 1
Bit 3-0 = 0011
If the binary number is 0011 = the decimal number 3, the cubic value of 2 is 8, and the slice size is 8*512, that is, 4096 bytes.

Recommended reading:

Linux2.6 direct read/write program for hard disk sectors

Obtain the Disk Physical sector size in Linux

Linux on a 4 kb Disk: actual recommendations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.