summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA. Wilcox <AWilcox@Wilcox-Tech.com>2023-10-18 22:37:22 -0500
committerA. Wilcox <AWilcox@Wilcox-Tech.com>2023-10-18 22:37:22 -0500
commit93499086c1293c07433d5e86e7423e21f1415e17 (patch)
tree5e28cdd41dad156c3a59bc5f44cce6127c7b46bb
parente6036a34eafdc8705c886ddb5d79517845a984b0 (diff)
downloadhorizon-93499086c1293c07433d5e86e7423e21f1415e17.tar.gz
horizon-93499086c1293c07433d5e86e7423e21f1415e17.tar.bz2
horizon-93499086c1293c07433d5e86e7423e21f1415e17.tar.xz
horizon-93499086c1293c07433d5e86e7423e21f1415e17.zip
hscript: diskid: Refactor how we check disk IDs
Now we use ID_MODEL_ENC in addition to ID_SERIAL, because sometimes users might want to actually use spaces in addition to just serial numbers. Fixes: #348
-rw-r--r--hscript/disk.cc33
1 files changed, 18 insertions, 15 deletions
diff --git a/hscript/disk.cc b/hscript/disk.cc
index 446714e..3fdeb2a 100644
--- a/hscript/disk.cc
+++ b/hscript/disk.cc
@@ -97,25 +97,23 @@ bool DiskId::execute() const {
if(!script->options().test(InstallEnvironment)) return true;
#ifdef HAS_INSTALL_ENV
- struct udev *udev;
- struct udev_device *device;
- const char *serial;
struct stat blk_stat;
- const char *block_c = _block.c_str();
- if(stat(block_c, &blk_stat) != 0) {
+ if(stat(_block.c_str(), &blk_stat) != 0) {
output_error(pos, "diskid: error opening device " + _block,
strerror(errno));
return false;
}
assert(S_ISBLK(blk_stat.st_mode));
- udev = udev_new();
+ struct udev *udev = udev_new();
if(!udev) {
output_error(pos, "diskid: failed to communicate with udevd",
"cannot read disk information");
return false;
}
- device = udev_device_new_from_devnum(udev, 'b', blk_stat.st_rdev);
+
+ struct udev_device *device =
+ udev_device_new_from_devnum(udev, 'b', blk_stat.st_rdev);
if(!device) {
udev_unref(udev);
output_error(pos, "diskid: failed to retrieve disk from udevd",
@@ -123,14 +121,19 @@ bool DiskId::execute() const {
return false;
}
- serial = udev_device_get_property_value(device, "ID_SERIAL");
- /* If we can't get the serial for this device, it's not a disk */
- if(serial) {
- std::string full_str(serial);
- match = (full_str.find(_ident) != std::string::npos);
- } else {
- output_error(pos, "diskid: failed to retrieve disk identification",
- "cannot read disk information");
+ const char *value;
+ for(const auto &prop : {"ID_SERIAL", "ID_MODEL_ENC"}) {
+ value = udev_device_get_property_value(device, prop);
+ /* If we can't get the serial for this device, it's not a disk */
+ if(value) {
+ std::string full_str(value);
+ match = (full_str.find(_ident) != std::string::npos);
+ } else {
+ output_error(pos, "diskid: failed to retrieve disk identification",
+ "cannot read disk information");
+ }
+
+ if(match) break;
}
if(!match) {