From 103b5343d6606687257875185aed3873777bb912 Mon Sep 17 00:00:00 2001 From: Chantara Tith Date: Wed, 8 Jun 2016 22:50:40 +0700 Subject: [PATCH] Initial work on LvmDevice --- src/core/CMakeLists.txt | 8 +- src/core/devicescanner.cpp | 31 +++++++- src/core/devicescanner.h | 3 + src/core/lvmdevice.cpp | 122 +++++++++++++++++++++++++++++++ src/core/lvmdevice.h | 83 +++++++++++++++++++++ src/core/volumemanagerdevice.cpp | 36 +++++++++ src/core/volumemanagerdevice.h | 55 ++++++++++++++ 7 files changed, 335 insertions(+), 3 deletions(-) create mode 100644 src/core/lvmdevice.cpp create mode 100644 src/core/lvmdevice.h create mode 100644 src/core/volumemanagerdevice.cpp create mode 100644 src/core/volumemanagerdevice.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 60b420b..5f2e6a2 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -15,8 +15,10 @@ set(CORE_SRC core/devicescanner.cpp core/partitionnode.cpp core/partitionalignment.cpp - core/diskdevice.cpp core/device.cpp + core/diskdevice.cpp + core/volumemanagerdevice.cpp + core/lvmdevice.cpp core/operationstack.cpp core/partitionrole.cpp ) @@ -26,8 +28,10 @@ set(CORE_LIB_HDRS core/copysourcedevice.h core/copytarget.h core/copytargetdevice.h - core/diskdevice.h core/device.h + core/diskdevice.h + core/volumemanagerdevice.h + core/lvmdevice.h core/devicescanner.h core/mountentry.h core/operationrunner.h diff --git a/src/core/devicescanner.cpp b/src/core/devicescanner.cpp index 9e67a3b..1e4da1d 100644 --- a/src/core/devicescanner.cpp +++ b/src/core/devicescanner.cpp @@ -23,6 +23,12 @@ #include "core/operationstack.h" #include "core/device.h" +#include "core/lvmdevice.h" +#include "core/diskdevice.h" + +#include "util/externalcommand.h" +#include +#include /** Constructs a DeviceScanner @param ostack the OperationStack where the devices will be created @@ -57,10 +63,33 @@ void DeviceScanner::scan() clear(); QList deviceList = CoreBackendManager::self()->backend()->scanDevices(); - //TODO: Scan for LVM here and add to the list. + QList lvmList = scanLvmDevices(); foreach(Device * d, deviceList) operationStack().addDevice(d); + foreach(Device * d, lvmList) + operationStack().addDevice(d); + operationStack().sortDevices(); } + +/* Return list of VG (LvmDevice) on the system */ +QList DeviceScanner::scanLvmDevices() const +{ + QList lvmList; + + ExternalCommand scanLvm(QStringLiteral("lvm"), + { QStringLiteral("vgdisplay")}); + + if (scanLvm.run(-1) && scanLvm.exitCode() == 0) { + QRegularExpression re(QStringLiteral("VG Name\\h+(\\w+)")); + QRegularExpressionMatchIterator i = re.globalMatch(scanLvm.output()); + while(i.hasNext()) { + QRegularExpressionMatch vgName = i.next(); + LvmDevice* temp = new LvmDevice(vgName.captured(1)); + lvmList.append(temp); + } + } + return lvmList; +} diff --git a/src/core/devicescanner.h b/src/core/devicescanner.h index 741c406..deec515 100644 --- a/src/core/devicescanner.h +++ b/src/core/devicescanner.h @@ -23,6 +23,7 @@ #include class OperationStack; +class LvmDevice; /** Thread to scan for all available Devices on this computer. @@ -54,6 +55,8 @@ protected: return m_OperationStack; } + QList scanLvmDevices() const; + private: OperationStack& m_OperationStack; }; diff --git a/src/core/lvmdevice.cpp b/src/core/lvmdevice.cpp new file mode 100644 index 0000000..6f31f2d --- /dev/null +++ b/src/core/lvmdevice.cpp @@ -0,0 +1,122 @@ +/************************************************************************* + * Copyright (C) 2008 by Volker Lanz * + * Copyright (C) 2016 by Andrius Štikonas * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of * + * the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see .* + *************************************************************************/ + +#include "core/lvmdevice.h" + +#include "core/partitiontable.h" +#include "util/externalcommand.h" + +#include + +/** Constructs a representation of LVM device with functionning LV as Partition + @param name Volume Group name +*/ +LvmDevice::LvmDevice(const QString& name, const QString& iconname) + : VolumeManagerDevice(name, + (QStringLiteral("/dev/") + name), + getPeSize(name), + getTotalPE(name), + iconname, + Device::LVM_Device) + , m_peSize(getPeSize(name)) + , m_totalPE(getTotalPE(name)) + , m_allocPE(getAllocatedPE(name)) + , m_freePE(getFreePE(name)) +{ + //called to initialize member variables + //refresh(); + initPartitions(); +} +/* +void LvmDevice::refresh() const +{ +} +*/ + +void LvmDevice::initPartitions() const +{ + //PartitionTable* pTable = new PartitionTable(PartitionTable::TableType::vmd,0, ) +} + +qint32 LvmDevice::getPeSize(const QString& vgname) +{ + ExternalCommand cmd(QStringLiteral("lvm"), + { QStringLiteral("vgdisplay"), + QStringLiteral("--units"), + QStringLiteral("B"), + vgname}); + if (cmd.run(-1) && cmd.exitCode() == 0) { + QRegularExpression re(QStringLiteral("PE Size\\h+(\\d+)")); + QRegularExpressionMatch match = re.match(cmd.output()); + if (match.hasMatch()) { + return match.captured(1).toInt(); + } + } + return -1; +} + +qint32 LvmDevice::getTotalPE(const QString& vgname) +{ + ExternalCommand cmd(QStringLiteral("lvm"), + { QStringLiteral("vgdisplay"), + QStringLiteral("--units"), + QStringLiteral("B"), + vgname}); + if (cmd.run(-1) && cmd.exitCode() == 0) { + QRegularExpression re(QStringLiteral("Total PE\\h+(\\d+)")); + QRegularExpressionMatch match = re.match(cmd.output()); + if (match.hasMatch()) { + return match.captured(1).toInt(); + } + } + return -1; +} + +qint32 LvmDevice::getAllocatedPE(const QString& vgname) +{ + ExternalCommand cmd(QStringLiteral("lvm"), + { QStringLiteral("vgdisplay"), + QStringLiteral("--units"), + QStringLiteral("B"), + vgname}); + if (cmd.run(-1) && cmd.exitCode() == 0) { + QRegularExpression re(QStringLiteral("Alloc PE.*\\h+(\\d+)")); + QRegularExpressionMatch match = re.match(cmd.output()); + if (match.hasMatch()) { + return match.captured(1).toInt(); + } + } + return -1; +} + +qint32 LvmDevice::getFreePE(const QString& vgname) +{ + ExternalCommand cmd(QStringLiteral("lvm"), + { QStringLiteral("vgdisplay"), + QStringLiteral("--units"), + QStringLiteral("B"), + vgname}); + if (cmd.run(-1) && cmd.exitCode() == 0) { + QRegularExpression re(QStringLiteral("Free PE.*\\h+(\\d+)")); + QRegularExpressionMatch match = re.match(cmd.output()); + if (match.hasMatch()) { + return match.captured(1).toInt(); + } + } + return -1; +} diff --git a/src/core/lvmdevice.h b/src/core/lvmdevice.h new file mode 100644 index 0000000..524e821 --- /dev/null +++ b/src/core/lvmdevice.h @@ -0,0 +1,83 @@ +/************************************************************************* + * Copyright (C) 2016 by Chantara Tith * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of * + * the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see .* + *************************************************************************/ + +#if !defined(LVMDEVICE__H) + +#define LVMDEVICE__H + +#include "core/volumemanagerdevice.h" + +#include "util/libpartitionmanagerexport.h" + +#include +#include +#include + +class PartitionTable; +class CreatePartitionTableOperation; +class SmartStatus; + +/** A device. + + Represents a device like /dev/sda. + + Devices are the outermost entity; they contain a PartitionTable that itself contains Partitions. + + @see PartitionTable, Partition + @author Volker Lanz +*/ +class LIBKPMCORE_EXPORT LvmDevice : public VolumeManagerDevice +{ + Q_DISABLE_COPY(LvmDevice) + +public: + LvmDevice(const QString& name, const QString& iconname = QString()); +// void refresh() const override; + +public: + qint32 peSize() const { + return m_peSize; + } + qint32 totalPE() const { + return m_totalPE; + } + qint32 allocatedPE() const { + return m_allocPE; + } + qint32 freePE() const { + return m_freePE; + } + +public: + static qint32 getPeSize(const QString& name); + static qint32 getTotalPE(const QString& name); + static qint32 getAllocatedPE(const QString& name); + static qint32 getFreePE(const QString& name); +protected: +// void init() const; + void initPartitions() const; + + +private: + qint32 m_peSize; + qint32 m_totalPE; + qint32 m_allocPE; + qint32 m_freePE; +}; + +#endif + diff --git a/src/core/volumemanagerdevice.cpp b/src/core/volumemanagerdevice.cpp new file mode 100644 index 0000000..89136d1 --- /dev/null +++ b/src/core/volumemanagerdevice.cpp @@ -0,0 +1,36 @@ +/************************************************************************* + * Copyright (C) 2008 by Volker Lanz * + * Copyright (C) 2016 by Andrius Štikonas * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of * + * the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see .* + *************************************************************************/ + +#include "core/volumemanagerdevice.h" + +#include "core/partitiontable.h" +#include "core/smartstatus.h" + +#include "util/capacity.h" + +/** Constructs a Device with an empty PartitionTable. +*/ +VolumeManagerDevice::VolumeManagerDevice(const QString& name, + const QString& devicenode, + const qint32 logicalSize, + const qint64 totalLogical, + const QString& iconname, + Device::Type type) + : Device(name, devicenode, logicalSize, totalLogical, iconname, type) +{ +} diff --git a/src/core/volumemanagerdevice.h b/src/core/volumemanagerdevice.h new file mode 100644 index 0000000..5bdf0f7 --- /dev/null +++ b/src/core/volumemanagerdevice.h @@ -0,0 +1,55 @@ +/************************************************************************* + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of * + * the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see .* + *************************************************************************/ + +#if !defined(VOLUMEMANAGERDEVICE__H) + +#define VOLUMEMANAGERDEVICE__H + +#include "util/libpartitionmanagerexport.h" +#include "core/device.h" + +#include +#include +#include + +class PartitionTable; +class CreatePartitionTableOperation; +class CoreBackend; +class SmartStatus; +class Partition; + +/** A device. + + Represents a device like /dev/sda. + + Devices are the outermost entity; they contain a PartitionTable that itself contains Partitions. + + @see PartitionTable, Partition + @author Volker Lanz +*/ +class LIBKPMCORE_EXPORT VolumeManagerDevice : public Device +{ + Q_DISABLE_COPY(VolumeManagerDevice) + +protected: + VolumeManagerDevice(const QString& name, const QString& devicenode, const qint32 logicalSize, const qint64 totalLogical, const QString& iconname = QString(), Device::Type type = Device::Unknown_Device); + +public: + //virtual void refresh() const = 0; /* VG infos can be changed, unlike disk_device */ + //virtual Qlist listDevices() const = 0; +}; + +#endif +