您的位置:首页 > 运维架构 > Linux

linux c 查看块设备的大小

2014-02-20 13:41 260 查看
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <stdint.h>
#include <unistd.h>

int main(void)
{
int fd;
//off_t size
uint64_t size;
int len;
int r;

if ((fd = open("/dev/sda", O_RDONLY)) < 0)
{
printf("open error %d\n", errno);
exit(-1);
}

#if 0
//会报错
if ((size = lseek(fd, 0, SEEK_END)) < 0)
{
printf("lseek errno %d\n", errno);
exit(-1);
}
#endif
if ((r = ioctl(fd, BLKGETSIZE64, &size)) < 0)
{
printf("ioctl error %d\n", errno);
exit(-1);
}

len = (size>>30);
printf("size of sda = %d G\n", len);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: