#include <inttypes.h>
#include <string.h>
#include <unistd.h>
+#include <sys/stat.h>
#include "daemon.h"
#include "c-ctype.h"
free (err);
return 0;
}
+
+/* Test if a device is a logical volume (RHBZ#619793).
+ *
+ * This is harder than it should be. A LV device like /dev/VG/LV is
+ * really a symlink to a device-mapper device like /dev/dm-0. However
+ * at the device-mapper (kernel) level, nothing is really known about
+ * LVM (a userspace concept). Therefore we use a convoluted method to
+ * determine this, by listing out known LVs and checking whether the
+ * rdev (major/minor) of the device we are passed matches any of them.
+ *
+ * Note use of 'stat' instead of 'lstat' so that symlinks are fully
+ * resolved.
+ */
+int
+do_is_lv (const char *device)
+{
+ struct stat stat1, stat2;
+
+ int r = stat (device, &stat1);
+ if (r == -1) {
+ reply_with_perror ("stat: %s", device);
+ return -1;
+ }
+
+ char **lvs = do_lvs ();
+ if (lvs == NULL)
+ return -1;
+
+ size_t i;
+ for (i = 0; lvs[i] != NULL; ++i) {
+ r = stat (lvs[i], &stat2);
+ if (r == -1) {
+ reply_with_perror ("stat: %s", lvs[i]);
+ free_strings (lvs);
+ return -1;
+ }
+ if (stat1.st_rdev == stat2.st_rdev) { /* found it */
+ free_strings (lvs);
+ return 1;
+ }
+ }
+
+ /* not found */
+ free_strings (lvs);
+ return 0;
+}
+
+/* Similar to is_lv above (RHBZ#638899). */
+char *
+do_lvm_canonical_lv_name (const char *device)
+{
+ struct stat stat1, stat2;
+
+ int r = stat (device, &stat1);
+ if (r == -1) {
+ reply_with_perror ("stat: %s", device);
+ return NULL;
+ }
+
+ char **lvs = do_lvs ();
+ if (lvs == NULL)
+ return NULL;
+
+ size_t i;
+ for (i = 0; lvs[i] != NULL; ++i) {
+ r = stat (lvs[i], &stat2);
+ if (r == -1) {
+ reply_with_perror ("stat: %s", lvs[i]);
+ free_strings (lvs);
+ return NULL;
+ }
+ if (stat1.st_rdev == stat2.st_rdev) { /* found it */
+ char *r = strdup (lvs[i]);
+ if (r == NULL) {
+ reply_with_perror ("strdup");
+ free_strings (lvs);
+ }
+ free_strings (lvs);
+ return r;
+ }
+ }
+
+ free_strings (lvs);
+
+ /* not found */
+ reply_with_error ("%s: not a logical volume", device);
+ return NULL;
+}