#include <lustre_ver.h>
#include <sys/stat.h>
#include <sys/utsname.h>
-#include <linux/loop.h>
extern char *progname;
extern int verbose;
return strscat(dst, src, buflen);
}
-static int check_losetup(char *device, char *file_path)
-{
- struct loop_info64 loop_info;
- int fd;
- int rc;
-
- fd = open(device, O_RDONLY);
- if (fd < 0)
- return errno;
-
- rc = ioctl(fd, LOOP_GET_STATUS64, (void *)&loop_info);
- close(fd);
- if (rc < 0)
- return errno;
-
- return strcmp(file_path, (char *)(loop_info.lo_file_name));
-}
-
int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
{
FILE *fp;
struct mntent *mnt;
- char lo_dev[] = "/dev/loop";
- int lo_dev_len = strlen(lo_dev);
- int ret = 0;
fp = setmntent(MOUNTED, "r");
if (fp == NULL)
return 0;
while ((mnt = getmntent(fp)) != NULL) {
- char *fsname = mnt->mnt_fsname;
-
- if ((strcmp(fsname, spec1) == 0 ||
- strcmp(fsname, spec2) == 0) &&
+ if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
+ strcmp(mnt->mnt_fsname, spec2) == 0) &&
(mtpt == NULL || strcmp(mnt->mnt_dir, mtpt) == 0) &&
(type == NULL || strcmp(mnt->mnt_type, type) == 0)) {
- ret = EEXIST;
- break;
- }
- /* Check if the loop device is already mounted */
- if (strncmp(fsname, lo_dev, lo_dev_len) != 0)
- continue;
- if (check_losetup(fsname, spec1) == 0 ||
- check_losetup(fsname, spec2) == 0) {
- ret = EEXIST;
- break;
+ endmntent(fp);
+ return(EEXIST);
}
}
endmntent(fp);
- return ret;
+ return 0;
}
#define PROC_DIR "/proc/"
int loop_cleanup(struct mkfs_opts *mop)
{
char cmd[150];
- int ret = 1;
+ int ret = 0;
+
if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
+ int tries;
+
sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
- ret = run_command(cmd, sizeof(cmd));
+ for (tries = 0; tries < 3; tries++) {
+ ret = run_command(cmd, sizeof(cmd));
+ if (ret == 0)
+ break;
+ sleep(1);
+ }
}
+
+ if (ret != 0)
+ fprintf(stderr, "cannot cleanup %s: rc = %d\n",
+ mop->mo_loopdev, ret);
return ret;
}
return 0;
}
-
-/**
- * Try to get the real path to the device, in case it is a
- * symbolic link or dm device for instance
- */
-int get_realpath(char *path, char **device)
-{
- FILE *f;
- size_t sz;
- char *ptr;
- char name[256];
- char real_path[PATH_MAX] = {'\0'};
-
- if (realpath(path, real_path) == NULL)
- return errno;
-
- ptr = strrchr(real_path, '/');
- if (ptr && strncmp(ptr, "/dm-", 4) == 0 && isdigit(*(ptr + 4))) {
- snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptr+1);
- f = fopen(path, "r");
- if (f != NULL) {
- /* read "<name>\n" from sysfs */
- if (fgets(name, sizeof(name), f) != NULL) {
- sz = strlen(name);
- if (sz > 1) {
- name[sz - 1] = '\0';
- snprintf(real_path, sizeof(real_path),
- "/dev/mapper/%s", name);
- }
- }
- fclose(f);
- }
- }
- *device = strdup(real_path);
-
- return 0;
-}