ioctl 操作 读取磁盘信息
/// @file main.cpp
/// @brief
/// @author EastonWoo
/// 0.01
/// @date 2012-12-19
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h> //ioctl
#include <sys/mount.h> //BLKSSZGET //BLKGETSIZE
#include <linux/fd.h> //struct floppy_struct //FDGETPRM
#include <linux/hdreg.h> //struct hd_geometry //HDIO_GETGEO
int main(int argc, const char *argv[])
{
int dev = 0;
if((dev = open("/dev/sdc1", O_EXCL | O_RDWR)) == 0)
// if((dev = open("/dev/sdc", O_EXCL | O_RDWR)) == 0)
{
printf("open error!\n");
return -1;
}
int min_sector_size = 0;
if (ioctl(dev, BLKSSZGET, &min_sector_size) >= 0)
printf("successful : min_sector_size = %d\n",min_sector_size);
long loop_size = 0;
if(ioctl(dev, BLKGETSIZE, &loop_size) >= 0)
printf("successful : loop_size = %d\n",loop_size);
//#define BLKRRPART _IO(0x12, 95) /* Re-read partition table. */
int retVal = 0;
if(ioctl(dev,BLKRRPART,&retVal) >= 0); //重读分区表
printf("successful : retVal = %d\n",retVal);
struct floppy_struct param;
memset(¶m,0,sizeof(param));
if(ioctl(dev, FDGETPRM, ¶m) >= 0)
printf("\tsize = %u\n\
\tsect = %u\n", \
param.size, \
param.sect \
);
struct hd_geometry geometry;
if(ioctl(dev, HDIO_GETGEO, &geometry) >=0)
printf("\theads = %u\n \
\tsectors = %u\n \
\tcylinders = %u\n \
\tstart = %lu\n", \
geometry.heads, \
geometry.sectors, \
geometry.cylinders, \
geometry.start \
);
if(dev != 0)
{
close(dev);
}
return 0;
}
结果:
[user:test] ./a.out
successful : min_sector_size = 512
successful : loop_size = 8177022
successful : retVal = 0
heads = 255
sectors = 63
cylinders = 509
start = 63
fdisk结果对比:
[user:dosfstools-3.0.13] sudo fdisk -l /dev/sdc1
[sudo] password for user:
Disk /dev/sdc1: 4186 MB, 4186635264 bytes
255 heads, 63 sectors/track, 508 cylinders, total 8177022 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
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
[user:dosfstools-3.0.13] sudo fdisk -l /dev/sdc
Disk /dev/sdc: 4194 MB, 4194304000 bytes
255 heads, 63sectors/track, 509 cylinders, total 8192000 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
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sdc1 * 63 8177084 4088511 83 Linux
[user:dosfstools-3.0.13]