Skip to contentSkip to conversation

main - device_id: look for serial number in other locations

PATCH David Teigland 1 participant 1 message Nov 7, 2022

lore
Share & tools
lore
#0 lore raw
Gitweb:        https://sourceware.org/git/?p=lvm2.git;a=commitdiff;h=bdab36cf3f059e597371bb504646f4dfb7a89f50
Commit:        bdab36cf3f059e597371bb504646f4dfb7a89f50
Parent:        36a923926c2c27c1a8a5ac262387d2a4d3e620f8
Author:        David Teigland <[email protected]>
AuthorDate:    Mon Oct 24 16:23:36 2022 -0500
Committer:     David Teigland <[email protected]>
CommitterDate: Mon Nov 7 08:56:02 2022 -0600

device_id: look for serial number in other locations

Only /sys/dev/block/major:minor/device/serial was read to find a disk serial number, but a serial number seems to be reported more often in other locations, so check these also:
/sys/dev/block/major:minor/device/vpd_pg80 /sys/class/block/vda/serial (for virtio disks only)

---
 lib/device/device.h    |  1 +
 lib/device/device_id.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++--
 lib/device/parse_vpd.c | 40 ++++++++++++++++++++++++++++++
 3 files changed, 105 insertions(+), 2 deletions(-)
lib/device/device.h +1 -0
index ca46490ce..519754e41 100644
@@ -228,5 +228,6 @@ int dev_mpath_init(const char *config_wwids_file);
 void dev_mpath_exit(void);
 int parse_vpd_ids(const unsigned char *vpd_data, int vpd_datalen, struct dm_list *ids);
 int format_t10_id(const unsigned char *in, int in_bytes, unsigned char *out, int out_bytes);
+int parse_vpd_serial(const unsigned char *in, char *out, int outsize);
 
 #endif
lib/device/device_id.c +64 -2

78 lines not on this page · raw message

lib/device/parse_vpd.c +40 -0
index 99e8c0ec2..23b0c6efa 100644
@@ -211,3 +211,43 @@ int parse_vpd_ids(const unsigned char *vpd_data, int vpd_datalen, struct dm_list
 
 	return id_size;
 }
+
+int parse_vpd_serial(const unsigned char *in, char *out, int outsize)
+{
+	uint8_t len_buf[2] __attribute__((aligned(8))) = { 0 };;
+	size_t len;
+
+	/* parsing code from multipath tools */
+	/* ignore in[0] and in[1] */
+	/* len is in[2] and in[3] */
+	/* serial begins@in[4] */
+
+	len_buf[0] = in[2];
+	len_buf[1] = in[3];
+	len = len_buf[0] << 8 | len_buf[1];
+
+	if (outsize == 0)
+		return 0;
+
+	if (len > DEV_WWID_SIZE)
+		len = DEV_WWID_SIZE;
+	/*
+	 * Strip leading and trailing whitespace
+	 */
+	while (len > 0 && in[len + 3] == ' ')
+		--len;
+	while (len > 0 && in[4] == ' ') {
+		++in;
+		--len;
+	}
+
+	if (len >= outsize)
+                len = outsize - 1;
+
+	if (len > 0) {
+		memcpy(out, in + 4, len);
+		out[len] = '\0';
+	}
+	return len;
+}
+