From 53b154fe167dfaff19302050e8708744fea7ce11 Mon Sep 17 00:00:00 2001 From: Chantara Tith Date: Mon, 8 Aug 2016 03:30:47 +0700 Subject: [PATCH] Add internal storage to LVM Device to keep track of its LVs and PVs. --- src/core/lvmdevice.cpp | 39 ++++++++++++++++++++++------------ src/core/lvmdevice.h | 22 +++++++++++++++++-- src/core/volumemanagerdevice.h | 13 +++++++----- 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/src/core/lvmdevice.cpp b/src/core/lvmdevice.cpp index 4762411..1a5b311 100644 --- a/src/core/lvmdevice.cpp +++ b/src/core/lvmdevice.cpp @@ -27,7 +27,6 @@ #include "util/helpers.h" #include -#include #include #include @@ -50,10 +49,20 @@ LvmDevice::LvmDevice(const QString& name, const QString& iconname) m_freePE = getFreePE(name); m_allocPE = m_totalPE - m_freePE; m_UUID = getUUID(name); + m_PVPathList = new QStringList(getPVs(name)); + m_LVPathList = new QStringList(getLVs(name)); + m_LVSizeMap = new QMap(); initPartitions(); } +LvmDevice::~LvmDevice() +{ + delete m_PVPathList; + delete m_LVPathList; + delete m_LVSizeMap; +} + void LvmDevice::initPartitions() { qint64 firstUsable = 0; @@ -61,6 +70,7 @@ void LvmDevice::initPartitions() PartitionTable* pTable = new PartitionTable(PartitionTable::vmd, firstUsable, lastusable); foreach (Partition* p, scanPartitions(pTable)) { + LVSizeMap()->insert(p->partitionPath(), p->length()); pTable->append(p); } @@ -178,7 +188,6 @@ QList LvmDevice::scanSystemLVM() lvmList.append(new LvmDevice(vgname.trimmed())); } } - return lvmList; } @@ -190,14 +199,26 @@ qint64 LvmDevice::mappedSector(const QString& lvpath, qint64 sector) const if (devIndex) { for (int i = 0; i < devIndex; i++) { - //TODO: currently going over the same LV again and again is wasteful. Could use some more optimization - mSector += getTotalLE(lvpathList[i]); + mSector += LVSizeMap()->value(lvpathList[i]); } mSector += sector; } return mSector; } +QStringList LvmDevice::deviceNodeList() const +{ + return *PVPathList(); +} + +QStringList LvmDevice::lvPathList() const +{ + return *LVPathList(); +} + + + + QStringList LvmDevice::getPVs(const QString& vgname) { QStringList devPathList; @@ -212,11 +233,6 @@ QStringList LvmDevice::getPVs(const QString& vgname) return devPathList; } -QList LvmDevice::deviceNodeList() const -{ - return getPVs(name()); -} - QStringList LvmDevice::getLVs(const QString& vgname) { QStringList lvPathList; @@ -231,11 +247,6 @@ QStringList LvmDevice::getLVs(const QString& vgname) return lvPathList; } -QList LvmDevice::lvPathList() const -{ - return getLVs(name()); -} - qint64 LvmDevice::getPeSize(const QString& vgname) { QString val = getField(QStringLiteral("vg_extent_size"), vgname); diff --git a/src/core/lvmdevice.h b/src/core/lvmdevice.h index a59935d..6c71fa9 100644 --- a/src/core/lvmdevice.h +++ b/src/core/lvmdevice.h @@ -27,6 +27,7 @@ #include #include #include +#include class PartitionTable; class CreatePartitionTableOperation; @@ -45,11 +46,13 @@ class LIBKPMCORE_EXPORT LvmDevice : public VolumeManagerDevice public: LvmDevice(const QString& name, const QString& iconname = QString()); + ~LvmDevice(); public: QList scanPartitions(PartitionTable* pTable) const; Partition* scanPartition(const QString& lvPath, PartitionTable* pTable) const; +public: static QList scanSystemLVM(); static qint64 getPeSize(const QString& vgname); @@ -78,10 +81,10 @@ public: protected: void initPartitions(); - QList deviceNodeList() const override; + QStringList deviceNodeList() const override; qint64 mappedSector(const QString& lvpath, qint64 sector) const override; - QList lvPathList() const; + QStringList lvPathList() const; public: qint64 peSize() const { @@ -100,6 +103,18 @@ public: return m_UUID; } + QStringList* LVPathList() const { + return m_LVPathList; + } + + QStringList* PVPathList() const { + return m_PVPathList; + } + + QMap* LVSizeMap() const { + return m_LVSizeMap; + } + private: qint64 m_peSize; qint64 m_totalPE; @@ -107,6 +122,9 @@ private: qint64 m_freePE; QString m_UUID; + mutable QStringList* m_LVPathList; + mutable QStringList* m_PVPathList; + mutable QMap* m_LVSizeMap; }; #endif diff --git a/src/core/volumemanagerdevice.h b/src/core/volumemanagerdevice.h index 89ae87c..1b5f354 100644 --- a/src/core/volumemanagerdevice.h +++ b/src/core/volumemanagerdevice.h @@ -23,6 +23,7 @@ #include "core/device.h" #include +#include #include #include @@ -44,18 +45,20 @@ class LIBKPMCORE_EXPORT VolumeManagerDevice : public Device { Q_DISABLE_COPY(VolumeManagerDevice) -protected: +public: VolumeManagerDevice(const QString& name, const QString& devicenode, const qint32 logicalSize, const qint64 totalLogical, const QString& iconname = QString(), Device::Type type = Device::Unknown_Device); - virtual QList deviceNodeList() const = 0; /** Return list of physical device or partitions that makes up volumeManagerDevice */ + virtual void initPartitions() = 0; + virtual QStringList deviceNodeList() const = 0; /** Return list of physical device or partitions that makes up volumeManagerDevice */ virtual qint64 mappedSector(const QString& devNode, qint64 sector) const = 0; public: /** string deviceNodeList together into comma-sperated list */ virtual QString prettyDeviceNodeList() const; - /** Mapper return absolute sector representing the VG */ -private: - //QMap deviceSizeMapper; + void setTotalLogical(qint64 num) { + Q_ASSERT(num > 0); + m_TotalLogical = num; + } }; #endif