From 8083306d6610babc76fcdde7b11cb62878a4c8a0 Mon Sep 17 00:00:00 2001 From: Caio Carvalho Date: Fri, 24 Aug 2018 12:06:35 -0300 Subject: [PATCH] Updating mdadm config file through a application helper. --- src/core/raid/softwareraid.cpp | 46 +++++++++++----------------- src/core/raid/softwareraid.h | 2 +- src/util/CMakeLists.txt | 9 ++++++ src/util/externalcommand_whitelist.h | 5 +-- src/util/mdadmupdateconf.cpp | 20 ++++++++++++ 5 files changed, 51 insertions(+), 31 deletions(-) create mode 100644 src/util/mdadmupdateconf.cpp diff --git a/src/core/raid/softwareraid.cpp b/src/core/raid/softwareraid.cpp index a3efff1..f5e5b2c 100644 --- a/src/core/raid/softwareraid.cpp +++ b/src/core/raid/softwareraid.cpp @@ -247,7 +247,7 @@ qint32 SoftwareRAID::getRaidLevel(const QString &path) QRegularExpression re(QStringLiteral("Raid Level :\\s+\\w+(\\d+)")); QRegularExpressionMatch reMatch = re.match(output); if (reMatch.hasMatch()) - return reMatch.captured(1).toLongLong(); + return reMatch.captured(1).toInt(); } return -1; @@ -263,10 +263,8 @@ qint64 SoftwareRAID::getChunkSize(const QString &path) // Look sector size for the first device/partition on the list, as RAID 1 is composed by mirrored devices ExternalCommand sectorSize(QStringLiteral("blockdev"), { QStringLiteral("--getss"), device }); - if (sectorSize.run(-1) && sectorSize.exitCode() == 0) { - int sectors = sectorSize.output().trimmed().toLongLong(); - return sectors; - } + if (sectorSize.run(-1) && sectorSize.exitCode() == 0) + return sectorSize.output().trimmed().toLongLong(); } } else { @@ -279,7 +277,6 @@ qint64 SoftwareRAID::getChunkSize(const QString &path) } } return -1; - } qint64 SoftwareRAID::getTotalChunk(const QString &path) @@ -382,16 +379,18 @@ bool SoftwareRAID::createSoftwareRAID(Report &report, const qint32 raidLevel, const qint32 chunkSize) { + QString path = QStringLiteral("/dev/") + name; + QStringList args; - args << QStringLiteral("--create") << name; + args << QStringLiteral("--create") << path; args << QStringLiteral("--level=") + QString::number(raidLevel); args << QStringLiteral("--chunk=") + QString::number(chunkSize); args << QStringLiteral("--raid-devices=") + QString::number(devicePathList.size()); - for (const QString path : qAsConst(devicePathList)) { - eraseDeviceMDSuperblock(path); + for (const QString &p : qAsConst(devicePathList)) { + eraseDeviceMDSuperblock(p); - args << path; + args << p; } ExternalCommand cmd(report, QStringLiteral("mdadm"), args); @@ -399,8 +398,9 @@ bool SoftwareRAID::createSoftwareRAID(Report &report, if (!cmd.run(-1) || cmd.exitCode() != 0) return false; - // TODO: Support custom config files. - return updateConfigurationFile(name); + updateConfigurationFile(path); + + return true; } bool SoftwareRAID::deleteSoftwareRAID(Report &report, @@ -462,6 +462,8 @@ void SoftwareRAID::initPartitions() qint64 SoftwareRAID::mappedSector(const QString &partitionPath, qint64 sector) const { + Q_UNUSED(partitionPath); + Q_UNUSED(sector); return -1; } @@ -473,24 +475,12 @@ bool SoftwareRAID::eraseDeviceMDSuperblock(const QString &path) return cmd.run(-1) && cmd.exitCode() == 0; } -bool SoftwareRAID::updateConfigurationFile(const QString &deviceName) +bool SoftwareRAID::updateConfigurationFile(const QString &path) { - // TODO: Don't use QFile, it is not authenticated and will fail without sudo - QFile config(raidConfigurationFilePath()); + ExternalCommand cmd(QStringLiteral("/usr/") + QStringLiteral(LIBEXECDIRPATH) + QStringLiteral("/kpmcore_mdadmupdateconf"), + { path, raidConfigurationFilePath() }); - if (!config.open(QIODevice::WriteOnly | QIODevice::Append)) - return false; - - QTextStream out(&config); - - QString info = getDeviceInformation(deviceName); - - if (!info.isEmpty()) - out << info << QLatin1Char('\n'); - - config.close(); - - return true; + return cmd.run(-1) && cmd.exitCode() == 0; } QString SoftwareRAID::getDetail(const QString &path) diff --git a/src/core/raid/softwareraid.h b/src/core/raid/softwareraid.h index b1b342f..8fec188 100644 --- a/src/core/raid/softwareraid.h +++ b/src/core/raid/softwareraid.h @@ -103,7 +103,7 @@ protected: private: static bool eraseDeviceMDSuperblock(const QString& path); - static bool updateConfigurationFile(const QString& deviceName); + static bool updateConfigurationFile(const QString& path); static QString getDetail(const QString& path); diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 1106ba3..34dcbb9 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -41,6 +41,14 @@ add_executable(kpmcore_externalcommand util/externalcommandhelper.cpp ) +add_executable(kpmcore_mdadmupdateconf + util/mdadmupdateconf.cpp +) + +add_definitions( + -DLIBEXECDIRPATH="${LIBEXEC_INSTALL_DIR}" +) + target_link_libraries(kpmcore_externalcommand qca-qt5 Qt5::Core @@ -49,6 +57,7 @@ target_link_libraries(kpmcore_externalcommand KF5::I18n ) +install(TARGETS kpmcore_mdadmupdateconf DESTINATION ${LIBEXEC_INSTALL_DIR}) install(TARGETS kpmcore_externalcommand DESTINATION ${KAUTH_HELPER_INSTALL_DIR}) install( FILES util/org.kde.kpmcore.helperinterface.conf DESTINATION ${SYSCONF_INSTALL_DIR}/dbus-1/system.d ) install( FILES util/org.kde.kpmcore.applicationinterface.conf DESTINATION ${SYSCONF_INSTALL_DIR}/dbus-1/system.d ) diff --git a/src/util/externalcommand_whitelist.h b/src/util/externalcommand_whitelist.h index 79160aa..04aea3f 100644 --- a/src/util/externalcommand_whitelist.h +++ b/src/util/externalcommand_whitelist.h @@ -18,7 +18,7 @@ #ifndef KPMCORE_EXTERNALCOMMAND_WHITELIST_H #define KPMCORE_EXTERNALCOMMAND_WHITELIST_H -QString allowedCommands[] = { +const QString allowedCommands[] = { // TODO try to remove these later QStringLiteral("mv"), QStringLiteral("dd"), @@ -101,7 +101,8 @@ QStringLiteral("xfs_repair"), QStringLiteral("mkfs.xfs"), QStringLiteral("xfs_copy"), QStringLiteral("xfs_growfs"), -QStringLiteral("zpool") +QStringLiteral("zpool"), +QStringLiteral("kpmcore_mdadmupdateconf") }; #endif diff --git a/src/util/mdadmupdateconf.cpp b/src/util/mdadmupdateconf.cpp new file mode 100644 index 0000000..10f345a --- /dev/null +++ b/src/util/mdadmupdateconf.cpp @@ -0,0 +1,20 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + if (argc != 3) + return 1; + + char command[] = "mdadm --detail --scan "; + + strcat(command, argv[1]); + + strcat(command, " >> "); + + strcat(command, argv[2]); + + system(command); + + return 0; +}