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.


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.