+
+/**
+ * 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;
+}