版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34870631/article/details/83993851
通过loop_info中字段去确认是否mount
/* Backwards compatibility version */
struct loop_info {
int lo_number; /* ioctl r/o */
__kernel_old_dev_t lo_device; /* ioctl r/o */
unsigned long lo_inode; /* ioctl r/o */
__kernel_old_dev_t lo_rdevice; /* ioctl r/o */
int lo_offset;
int lo_encrypt_type;
int lo_encrypt_key_size; /* ioctl w/o */
int lo_flags; /* ioctl r/o */
char lo_name[LO_NAME_SIZE];
unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
unsigned long lo_init[2];
char reserved[4];
};
struct loop_info64 {
__u64 lo_device; /* ioctl r/o */
__u64 lo_inode; /* ioctl r/o */
__u64 lo_rdevice; /* ioctl r/o */
__u64 lo_offset;
__u64 lo_sizelimit;/* bytes, 0 == max available */
__u32 lo_number; /* ioctl r/o */
__u32 lo_encrypt_type;
__u32 lo_encrypt_key_size; /* ioctl w/o */
__u32 lo_flags; /* ioctl r/o */
__u8 lo_file_name[LO_NAME_SIZE];
__u8 lo_crypt_name[LO_NAME_SIZE];
__u8 lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
__u64 lo_init[2];
};
#include <fcntl.h>
#include <linux/loop.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)
int main(int argc, char *argv[])
{
int ret = -1;
int fd = -1;
int i = 0;
long devnr;
char loopname[4096];
char test_name[]="/home/user/test.img";
struct loop_info64 test = {0};
for(i=0 ; i < 8; i++)
{
sprintf(loopname, "/dev/loop%d", i);
fd = open(loopname, O_RDONLY);
if(fd == -1){
errExit("open: loopname");
}
ret = ioctl(fd, LOOP_GET_STATUS64, &test);
close(fd);
if(ret != 0)
{
errExit("ret");
}
if(strcmp(test.lo_file_name, test_name) == 0)
{
printf("lo_file_name:%s test_name:%s\n", test.lo_file_name, test_name);
exit(EXIT_SUCCESS);
}
}
exit(EXIT_SUCCESS);
}