From: Sage Weil Date: Thu, 20 May 2021 20:12:11 +0000 (-0500) Subject: common/blkdev: remove double _'s from device_id X-Git-Tag: v17.1.0~1846^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=5ee787944b0d00ce3bb6766f5b176e971eff91f9;p=ceph.git common/blkdev: remove double _'s from device_id For some unknown reason some devices have double spaces in their ID_MODEL_ENC string from udev: cpach:build (devid-underscores) 03:10 PM $ udevadm info /dev/sdt | grep ID_MODEL_ENC E: ID_MODEL_ENC=WDC\x20\x20WDS400T2B0A-00SM50\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20 cpach:build (devid-underscores) 03:10 PM $ udevadm info /dev/sdn | grep ID_MODEL_ENC E: ID_MODEL_ENC=WDC\x20WDS200T2B0A-00SM50\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20 It's not clear why this only happens on some devices and not others, especially when they are the same model of device. Regardless, removing the duplicate ' '/'_' is straightforward. Signed-off-by: Sage Weil --- diff --git a/src/common/blkdev.cc b/src/common/blkdev.cc index 3fbffe65ae71..ca5e9eb0c596 100644 --- a/src/common/blkdev.cc +++ b/src/common/blkdev.cc @@ -429,6 +429,14 @@ std::string _decode_model_enc(const std::string& in) v.erase(found + 1); } std::replace(v.begin(), v.end(), ' ', '_'); + + // remove "__", which seems to come up on by ubuntu box for some reason. + while (true) { + auto p = v.find("__"); + if (p == std::string::npos) break; + v.replace(p, 2, "_"); + } + return v; } @@ -498,6 +506,12 @@ std::string get_device_id(const std::string& devname, udev_device_unref(dev); udev_unref(udev); + cout << " vendor '" << id_vendor << "'" << std::endl; + cout << " model '" << id_model << "'" << std::endl; + cout << " serial_short '" << id_serial_short << "'" << std::endl; + cout << " scsi_serial '" << id_scsi_serial << "'" << std::endl; + cout << " serial '" << id_serial << "'" << std::endl; + // ID_SERIAL is usually $vendor_$model_$serial, but not always // ID_SERIAL_SHORT is mostly always just the serial // ID_MODEL is sometimes $vendor_$model, but diff --git a/src/test/common/test_blkdev.cc b/src/test/common/test_blkdev.cc index 952fb84d1974..733273ad41ee 100644 --- a/src/test/common/test_blkdev.cc +++ b/src/test/common/test_blkdev.cc @@ -99,3 +99,16 @@ TEST(blkdev, _decode_model_enc) ASSERT_EQ(std::string(foo[i][1]), d); } } + +TEST(blkdev, get_device_id) +{ + // this doesn't really test anything; it's just a way to exercise the + // get_device_id() code. + for (char c = 'a'; c < 'z'; ++c) { + char devname[4] = {'s', 'd', c, 0}; + std::string err; + auto i = get_device_id(devname, &err); + cout << "devname " << devname << " -> '" << i + << "' (" << err << ")" << std::endl; + } +}