ioctl 操作 读取磁盘信息

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(&param,0,sizeof(param));
    if(ioctl(dev, FDGETPRM, &param) >= 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]

 


 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值