Blob Blame History Raw
From 97add8b77aebc8032b7d186f36e264e1d9ffa733 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Fri, 24 Jun 2016 15:28:46 +0100
Subject: [PATCH] p2v: Display serial number of fixed disks (RHBZ#855058).

Use "lsblk -o serial" to get the serial number of each fixed disk, and
if it is available display that in the GUI.

(cherry picked from commit 9270a27650abaf586070c51cb90b6a77ec13e0cf)
---
 p2v/dependencies.m4 |  8 ++++----
 p2v/gui.c           |  8 ++++++--
 p2v/p2v.h           |  1 +
 p2v/utils.c         | 34 ++++++++++++++++++++++++++++++++++
 p2v/virt-p2v.pod    |  2 ++
 5 files changed, 47 insertions(+), 6 deletions(-)

diff --git a/p2v/dependencies.m4 b/p2v/dependencies.m4
index 6cf2961..80d6c92 100644
--- a/p2v/dependencies.m4
+++ b/p2v/dependencies.m4
@@ -30,6 +30,7 @@ ifelse(REDHAT,1,
   /usr/bin/qemu-nbd
   curl
   ethtool
+  util-linux
 
   dnl The hwdata package contains PCI IDs, used by virt-p2v to display
   dnl network vendor information (RHBZ#855059).
@@ -39,7 +40,6 @@ ifelse(REDHAT,1,
   pciutils
   hdparm
   smartmontools
-  util-linux
 
   dnl X11 environment
   xterm
@@ -72,11 +72,11 @@ ifelse(DEBIAN,1,
   qemu-utils
   curl
   ethtool
+  util-linux
   hwdata
   pciutils
   hdparm
   smartmontools
-  util-linux
   xorg
   xterm
   xserver-xorg-video-all
@@ -97,11 +97,11 @@ ifelse(ARCHLINUX,1,
   qemu
   curl
   ethtool
+  util-linux
   hwdata
   pciutils
   hdparm
   smartmontools
-  util-linux
   xterm
   xorg-xinit
   xorg-server
@@ -123,11 +123,11 @@ ifelse(SUSE,1,
   openssh
   curl
   ethtool
+  util-linux
   hwdata
   pciutils
   hdparm
   smartmontools
-  util-linux
   xinit
   xorg-x11-server
   xterm
diff --git a/p2v/gui.c b/p2v/gui.c
index 49c703d..58e9ea3 100644
--- a/p2v/gui.c
+++ b/p2v/gui.c
@@ -868,6 +868,7 @@ populate_disks (GtkTreeView *disks_list)
       uint64_t size;
       CLEANUP_FREE char *size_gb = NULL;
       CLEANUP_FREE char *model = NULL;
+      CLEANUP_FREE char *serial = NULL;
       CLEANUP_FREE char *device_descr = NULL;
 
       if (all_disks[i][0] != '/') { /* not using --test-disk */
@@ -875,15 +876,18 @@ populate_disks (GtkTreeView *disks_list)
         if (asprintf (&size_gb, "%" PRIu64 "G", size) == -1)
           error (EXIT_FAILURE, errno, "asprintf");
         model = get_blockdev_model (all_disks[i]);
+        serial = get_blockdev_serial (all_disks[i]);
       }
 
       if (asprintf (&device_descr,
                     "<b>%s</b>\n"
                     "<small>"
-                    "%s %s"
+                    "%s %s\n"
+                    "%s%s"
                     "</small>\n",
                     all_disks[i],
-                    size_gb ? size_gb : "", model ? model : "") == -1)
+                    size_gb ? size_gb : "", model ? model : "",
+                    serial ? "s/n " : "", serial ? serial : "") == -1)
         error (EXIT_FAILURE, errno, "asprintf");
 
       gtk_list_store_append (disks_store, &iter);
diff --git a/p2v/p2v.h b/p2v/p2v.h
index 40b05b8..3e75690 100644
--- a/p2v/p2v.h
+++ b/p2v/p2v.h
@@ -133,6 +133,7 @@ extern const char *get_ssh_error (void);
 /* utils.c */
 extern uint64_t get_blockdev_size (const char *dev);
 extern char *get_blockdev_model (const char *dev);
+extern char *get_blockdev_serial (const char *dev);
 extern char *get_if_addr (const char *if_name);
 extern char *get_if_vendor (const char *if_name, int truncate);
 extern void wait_network_online (const struct config *);
diff --git a/p2v/utils.c b/p2v/utils.c
index 18c2b7c..c9fbd2f 100644
--- a/p2v/utils.c
+++ b/p2v/utils.c
@@ -103,6 +103,40 @@ get_blockdev_model (const char *dev)
   return model;
 }
 
+/**
+ * Return the serial number of a block device.
+ *
+ * This is found using the lsblk command.
+ *
+ * Returns C<NULL> if we could not get the serial number.  The caller
+ * must free the returned string.
+ */
+char *
+get_blockdev_serial (const char *dev)
+{
+  CLEANUP_PCLOSE FILE *fp = NULL;
+  CLEANUP_FREE char *cmd = NULL;
+  char *serial = NULL;
+  size_t len = 0;
+  ssize_t n;
+
+  if (asprintf (&cmd, "lsblk -o serial /dev/%s --nodeps --noheadings",
+                dev) == -1)
+    error (EXIT_FAILURE, errno, "asprintf");
+  fp = popen (cmd, "r");
+  if (fp == NULL) {
+    perror (cmd);
+    return NULL;
+  }
+  if ((n = getline (&serial, &len, fp)) == -1) {
+    perror (cmd);
+    free (serial);
+    return NULL;
+  }
+  CHOMP (serial, n);
+  return serial;
+}
+
 /* Return contents of /sys/class/net/<if_name>/address (if found). */
 char *
 get_if_addr (const char *if_name)
diff --git a/p2v/virt-p2v.pod b/p2v/virt-p2v.pod
index 032a480..421925f 100644
--- a/p2v/virt-p2v.pod
+++ b/p2v/virt-p2v.pod
@@ -197,8 +197,10 @@ settings is fine.
      Convert  Device                        │
      [✔]      sda                           │
               1024G HITACHI                 │
+              s/n 12345                     │
      [✔]      sdb                           │
               119G HITACHI                  │
+              s/n 12346                     │
                                             │
 
 Normally you would want to convert all hard disks.  If you want
-- 
1.8.3.1