以下是一个简单的用C语言编写的判断块设备上是否有分区表的程序示例:
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
int main() {
int fd;
char device_path[20] = "/dev/sda"; // 块设备路径,可以根据实际情况修改
fd = open(device_path, O_RDONLY);
if (fd == -1) {
perror("无法打开设备");
return 1;
}
unsigned long long size;
if (ioctl(fd, BLKGETSIZE64, &size) == -1) {
perror("获取设备大小失败");
close(fd);
return 1;
}
printf("设备大小:%llu 字节\n", size);
struct partition pt;
if (ioctl(fd, BLKPG_GET_PARTITION_TABLE, &pt) == -1) {
printf("该设备没有分区表\n");
close(fd);
return 0;
}
printf("该设备有分区表\n");
close(fd);
return 0;
}
请注意,在运行此程序时需要具有足够的权限。另外,device_path 变量需要根据实际情况修改为你要检测的块设备路径,比如 /dev/sda
。
内容由零声教学AI助手提供,问题来源于学员提问