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.