From a35702e9113d655ffcc4b2ffc06e791f256d0beb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Thu, 22 Jun 2017 03:08:05 +0100 Subject: [PATCH] Use lsblk's TYPE=disk when scanning for devices. The previous way relied on kernel device numbers but this gets ugly, especially in cases when device number is not assigned such as virtio disks. BUG: 378607 --- src/backend/corebackend.cpp | 4 +-- src/plugins/libparted/libpartedbackend.cpp | 35 ++++++++++------------ 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/backend/corebackend.cpp b/src/backend/corebackend.cpp index e4918c2..9ce45c0 100644 --- a/src/backend/corebackend.cpp +++ b/src/backend/corebackend.cpp @@ -45,9 +45,9 @@ void CoreBackend::emitProgress(int i) emit progress(i); } -void CoreBackend::emitScanProgress(const QString& device_node, int i) +void CoreBackend::emitScanProgress(const QString& deviceNode, int i) { - emit scanProgress(device_node, i); + emit scanProgress(deviceNode, i); } void CoreBackend::setPartitionTableForDevice(Device& d, PartitionTable* p) diff --git a/src/plugins/libparted/libpartedbackend.cpp b/src/plugins/libparted/libpartedbackend.cpp index 48528aa..b6e1bb3 100644 --- a/src/plugins/libparted/libpartedbackend.cpp +++ b/src/plugins/libparted/libpartedbackend.cpp @@ -45,6 +45,8 @@ #include #include +#include +#include #include #include @@ -411,37 +413,32 @@ Device* LibPartedBackend::scanDevice(const QString& deviceNode) QList LibPartedBackend::scanDevices(bool excludeReadOnly) { +// TODO: add another bool option for loopDevices QList result; - // linux.git/tree/Documentation/devices.txt - QString blockDeviceMajorNumbers = QStringLiteral( - "3,22,33,34,56,57,88,89,90,91,128,129,130,131,132,133,134,135," // MFM, RLL and IDE hard disk/CD-ROM interface - // "7," // loop devices TODO: add another bool option for loopDevices - "8,65,66,67,68,69,70,71," // SCSI disk devices - "80,81,82,83,84,85,86,87," // I2O hard disk - "179," // MMC block devices - "240,241,242,243,244,245,246,247,248,249,250,251,252,253,254," // Virtio KVM devices (e.g. /dev/vda) - "259" // Block Extended Major (include NVMe) - ); - ExternalCommand cmd(QStringLiteral("lsblk"), { + + ExternalCommand cmd(QStringLiteral("lsblk"), + { QStringLiteral("--list"), QStringLiteral("--nodeps"), QStringLiteral("--noheadings"), - QStringLiteral("--output"), QString::fromLatin1("name"), QStringLiteral("--paths"), - QStringLiteral("--include"), blockDeviceMajorNumbers}); + QStringLiteral("--sort"), QStringLiteral("name"), + QStringLiteral("--output"), + QStringLiteral("type,name") }); + if (cmd.run(-1) && cmd.exitCode() == 0) { - QStringList devices = cmd.output().split(QString::fromLatin1("\n")); - devices.removeLast(); - quint32 totalDevices = devices.length(); + const QStringList output=cmd.output().split(QStringLiteral("\n")).filter(QRegularExpression(QStringLiteral("^disk "))); + quint32 totalDevices = output.length(); for (quint32 i = 0; i < totalDevices; ++i) { + QString deviceNode = output[i].split(QStringLiteral(" ")).last(); if (excludeReadOnly) { - QFile f(QStringLiteral("/sys/block/%1/ro").arg(QString(devices[i]).remove(QStringLiteral("/dev/")))); + QFile f(QStringLiteral("/sys/block/%1/ro").arg(deviceNode.remove(QStringLiteral("/dev/")))); if (f.open(QIODevice::ReadOnly)) if (f.readLine().trimmed().toInt() == 1) continue; } - emitScanProgress(devices[i], i * 100 / totalDevices); - Device* device = scanDevice(devices[i]); + emitScanProgress(deviceNode, i * 100 / totalDevices); + Device* device = scanDevice(deviceNode); if(device != nullptr) { result.append(device); }