[Ocfs2-tools-devel] [PATCH] adding code to mounted.c to translate /dev/dm-[N] names to /dev/mapper/xxx names.
XiaoweiHu
xiaowei.hu at oracle.com
Tue Dec 1 23:20:17 PST 2009
Signed-off-by: XiaoweiHu <xiaowei.hu at oracle.com>
---
mounted.ocfs2/mounted.c | 76 +++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 74 insertions(+), 2 deletions(-)
diff --git a/mounted.ocfs2/mounted.c b/mounted.ocfs2/mounted.c
index e4c5877..c691a3d 100644
--- a/mounted.ocfs2/mounted.c
+++ b/mounted.ocfs2/mounted.c
@@ -33,6 +33,7 @@
#include <linux/fd.h>
#include <string.h>
#include <sys/stat.h>
+#include <dirent.h>
#include <uuid/uuid.h>
@@ -155,12 +156,70 @@ static void ocfs2_print_quick_detect(struct list_head *dev_list)
}
}
+char *devname_strndup(const char *s, int length)
+{
+ char *ret;
+
+ if (!s)
+ return NULL;
+
+ if (!length)
+ length = strlen(s);
+
+ ret = malloc(length + 1);
+ if (ret) {
+ strncpy(ret, s, length);
+ ret[length] = '\0';
+ }
+ return ret;
+}
+
+char *devname_strdup(const char *s)
+{
+ return devname_strndup(s, 0);
+}
+
+void scan_dir_for_dev(char *dirname, dev_t devno, char **devname)
+{
+ DIR *dir;
+ struct dirent *dp;
+ char path[1024];
+ int dirlen;
+ struct stat st;
+
+ if ((dir = opendir(dirname)) == NULL)
+ return;
+ dirlen = strlen(dirname) + 2;
+ while ((dp = readdir(dir)) != 0) {
+ if (dirlen + strlen(dp->d_name) >= sizeof(path))
+ continue;
+
+ if (dp->d_name[0] == '.' &&
+ ((dp->d_name[1] == 0) ||
+ ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
+ continue;
+
+ sprintf(path, "%s/%s", dirname, dp->d_name);
+ if (stat(path, &st) < 0)
+ continue;
+
+ if (S_ISBLK(st.st_mode) && st.st_rdev == devno) {
+ *devname = devname_strdup(path);
+ break;
+ }
+ }
+ closedir(dir);
+ return;
+}
+
static errcode_t ocfs2_partition_list (struct list_head *dev_list)
{
errcode_t ret = 0;
FILE *proc;
char line[256];
char name[256];
+ char *devname = NULL;
+ int major,minor;
ocfs2_devices *dev;
proc = fopen ("/proc/partitions", "r");
@@ -170,14 +229,27 @@ static errcode_t ocfs2_partition_list (struct list_head *dev_list)
}
while (fgets (line, sizeof(line), proc) != NULL) {
- if (sscanf(line, "%*d %*d %*d %99[^ \t\n]", name) != 1)
+ if (sscanf(line, "%d %d %*d %99[^ \t\n]", &major, &minor, name) != 3)
continue;
ret = ocfs2_malloc0(sizeof(ocfs2_devices), &dev);
if (ret)
goto bail;
- snprintf(dev->dev_name, sizeof(dev->dev_name), "/dev/%s", name);
+ /* Try to translate private device-mapper dm-<N> names
+ * to standard /dev/mapper/<name>.
+ */
+ if (!strncmp(name, "dm-", 3) && isdigit(name[3])) {
+ scan_dir_for_dev("/dev/mapper", makedev(major, minor), &devname);
+ if (devname)
+ snprintf(dev->dev_name, sizeof(dev->dev_name), "%s", devname);
+ else
+ snprintf(dev->dev_name, sizeof(dev->dev_name), "/dev/%s", name);
+
+ } else {
+ snprintf(dev->dev_name, sizeof(dev->dev_name), "/dev/%s", name);
+ }
+
list_add_tail(&(dev->list), dev_list);
}
--
1.6.3.3
More information about the Ocfs2-tools-devel
mailing list