diff --git a/src/fs/lvm2_pv.cpp b/src/fs/lvm2_pv.cpp index 5af3c7c..99fc26e 100644 --- a/src/fs/lvm2_pv.cpp +++ b/src/fs/lvm2_pv.cpp @@ -22,6 +22,7 @@ #include "util/capacity.h" #include +#include namespace FS { @@ -46,7 +47,9 @@ lvm2_pv::lvm2_pv(qint64 firstsector, qint64 lastsector, qint64 sectorsused, cons void lvm2_pv::init() { m_Create = findExternal(QStringLiteral("lvm")) ? cmdSupportFileSystem : cmdSupportNone; - m_Check = findExternal(QStringLiteral("lvm")) ? cmdSupportFileSystem : cmdSupportNone; + m_Check = findExternal(QStringLiteral("lvm")) ? cmdSupportFileSystem : cmdSupportNone; + m_Grow = findExternal(QStringLiteral("lvm")) ? cmdSupportFileSystem : cmdSupportNone; + m_Shrink = findExternal(QStringLiteral("lvm")) ? cmdSupportFileSystem : cmdSupportNone; m_GetLabel = cmdSupportCore; m_UpdateUUID = findExternal(QStringLiteral("lvm")) ? cmdSupportFileSystem : cmdSupportNone; @@ -68,8 +71,8 @@ bool lvm2_pv::supportToolFound() const m_Create != cmdSupportNone && m_Check != cmdSupportNone && m_UpdateUUID != cmdSupportNone && -// m_Grow != cmdSupportNone && -// m_Shrink != cmdSupportNone && + m_Grow != cmdSupportNone && + m_Shrink != cmdSupportNone && // m_Copy != cmdSupportNone && m_Move != cmdSupportNone && m_Backup != cmdSupportNone && @@ -105,9 +108,93 @@ bool lvm2_pv::remove(Report& report, const QString& deviceNode) const return cmd.run(-1) && cmd.exitCode() == 0; } +bool lvm2_pv::resize(Report& report, const QString& deviceNode, qint64 length) const +{ + // TODO: check if the it is legal to resize + const QString len = QString::number(length / 512) + QStringLiteral("s"); + + ExternalCommand cmd(report, QStringLiteral("lvm"), { QStringLiteral("pvresize"), QStringLiteral("--setphysicalvolumesize"), len, deviceNode }); + return cmd.run(-1) && cmd.exitCode() == 0; +} + bool lvm2_pv::updateUUID(Report& report, const QString& deviceNode) const { ExternalCommand cmd(report, QStringLiteral("lvm"), { QStringLiteral("pvchange"), QStringLiteral("--uuid"), deviceNode }); return cmd.run(-1) && cmd.exitCode() == 0; } + +QString lvm2_pv::getVGName(const QString& deviceNode) //PV node +{ + ExternalCommand cmd( QStringLiteral("lvm"), + { QStringLiteral("pvdisplay"), QStringLiteral("--verbose"), deviceNode }); + if (cmd.run(-1) && cmd.exitCode() == 0) { + QRegularExpression re(QStringLiteral("VG Name\\h+(\\w+)")); + QRegularExpressionMatch vgName = re.match(cmd.output()); + if (vgName.hasMatch()) + return vgName.captured(1); + } + return QString(); +} + +qint64 lvm2_pv::getTotalPE(const QString& deviceNode) const { + ExternalCommand cmd( QStringLiteral("lvm"), + { QStringLiteral("pvdisplay"), QStringLiteral("--verbose"), deviceNode }); + if (cmd.run(-1) && cmd.exitCode() == 0) { + QRegularExpression re(QStringLiteral("Total PE\\h+(\\w+)")); + QRegularExpressionMatch totalPE = re.match(cmd.output()); + if (totalPE.hasMatch()) + return totalPE.captured(1).toLongLong(); + } + return -1; + +} + +qint64 lvm2_pv::getFreePE(const QString& deviceNode) const { + ExternalCommand cmd( QStringLiteral("lvm"), + { QStringLiteral("pvdisplay"), QStringLiteral("--verbose"), deviceNode }); + if (cmd.run(-1) && cmd.exitCode() == 0) { + QRegularExpression re(QStringLiteral("Free PE\\h+(\\w+)")); + QRegularExpressionMatch freePE = re.match(cmd.output()); + if (freePE.hasMatch()) + return freePE.captured(1).toLongLong(); + } + return -1; +} + +qint64 lvm2_pv::getAllocatedPE(const QString& deviceNode) const { + ExternalCommand cmd( QStringLiteral("lvm"), + { QStringLiteral("pvdisplay"), QStringLiteral("--verbose"), deviceNode }); + if (cmd.run(-1) && cmd.exitCode() == 0) { + QRegularExpression re(QStringLiteral("Allocated PE\\h+(\\d+)")); + QRegularExpressionMatch allocatedPE = re.match(cmd.output()); + if (allocatedPE.hasMatch()) + return allocatedPE.captured(1).toLongLong(); + } + return -1; +} + +qint64 lvm2_pv::getPVSize(const QString& deviceNode) const { + ExternalCommand cmd( QStringLiteral("lvm"), + { QStringLiteral("pvdisplay"), QStringLiteral("--verbose"), QStringLiteral("--units"), QStringLiteral("B"), deviceNode }); + if (cmd.run(-1) && cmd.exitCode() == 0) { + QRegularExpression re(QStringLiteral("PV Size\\h+(\\d+)")); + QRegularExpressionMatch PVSize = re.match(cmd.output()); + if (PVSize.hasMatch()) + return PVSize.captured(1).toLongLong(); + } + return -1; +} + +qint64 lvm2_pv::getPESize(const QString& deviceNode) const { + ExternalCommand cmd( QStringLiteral("lvm"), + { QStringLiteral("pvdisplay"), QStringLiteral("--verbose"), QStringLiteral("--units"), QStringLiteral("B"), deviceNode }); + if (cmd.run(-1) && cmd.exitCode() == 0) { + QRegularExpression re(QStringLiteral("PE Size\\h+(\\d+)")); + QRegularExpressionMatch PESize = re.match(cmd.output()); + if (PESize.hasMatch()) + return PESize.captured(1).toLongLong(); + } + return -1; +} + } diff --git a/src/fs/lvm2_pv.h b/src/fs/lvm2_pv.h index 398fce5..029881f 100644 --- a/src/fs/lvm2_pv.h +++ b/src/fs/lvm2_pv.h @@ -47,10 +47,11 @@ public: bool check(Report& report, const QString& deviceNode) const override; bool create(Report& report, const QString& deviceNode) const override; bool remove(Report& report, const QString& deviceNode) const override; -// bool resize(Report& report, const QString& deviceNode, qint64 length) const override; + bool resize(Report& report, const QString& deviceNode, qint64 length) const override; // bool writeLabel(Report& report, const QString& deviceNode, const QString& newLabel) override; bool updateUUID(Report& report, const QString& deviceNode) const override; + CommandSupportType supportGetUsed() const override { return m_GetUsed; } @@ -92,6 +93,14 @@ public: SupportTool supportToolName() const override; bool supportToolFound() const override; + qint64 getTotalPE(const QString& deviceNode) const; + qint64 getFreePE(const QString& deviceNode) const; + qint64 getAllocatedPE(const QString& deviceNode) const; + qint64 getPESize(const QString& deviceNode) const; // return PE size in bytes + qint64 getPVSize(const QString& deviceNode) const; // return PV size in bytes + + static QString getVGName(const QString& deviceNode); + public: static CommandSupportType m_GetUsed; static CommandSupportType m_GetLabel; diff --git a/src/plugins/libparted/libpartedbackend.cpp b/src/plugins/libparted/libpartedbackend.cpp index a6fa3c4..2f4eec4 100644 --- a/src/plugins/libparted/libpartedbackend.cpp +++ b/src/plugins/libparted/libpartedbackend.cpp @@ -35,6 +35,7 @@ #include "fs/hfs.h" #include "fs/hfsplus.h" #include "fs/luks.h" +#include "fs/lvm2_pv.h" #include "util/globallog.h" #include "util/helpers.h" @@ -369,12 +370,18 @@ void LibPartedBackend::scanDevicePartitions(Device& d, PedDisk* pedDisk) if (isCryptOpen) { luksFs->loadInnerFileSystem(node, mapperNode); - mountPoint = mountPoints.findByDevice(mapperNode) ? - mountPoints.findByDevice(mapperNode)->mountPoint() : - QString(); - // We cannot use libparted to check the mounted status because - // we don't have a PedPartition for the mapper device, so we use lsblk - mounted = isMounted(mapperNode); + if (luksFs->type() == FileSystem::Lvm2_PV) { + mountPoint = FS::lvm2_pv::getVGName(mapperNode); + mounted = false; + } else { + + mountPoint = mountPoints.findByDevice(mapperNode) ? + mountPoints.findByDevice(mapperNode)->mountPoint() : + QString(); + // We cannot use libparted to check the mounted status because + // we don't have a PedPartition for the mapper device, so we use lsblk + mounted = isMounted(mapperNode); + } if (mounted) { const KDiskFreeSpaceInfo freeSpaceInfo = KDiskFreeSpaceInfo::freeSpaceInfo(mountPoint); if (freeSpaceInfo.isValid() && mountPoint != QString()) @@ -385,6 +392,10 @@ void LibPartedBackend::scanDevicePartitions(Device& d, PedDisk* pedDisk) } luksFs->setMounted(mounted); + } else if (type == FileSystem::Lvm2_PV) { + //TODO: adding PartitionRole + mountPoint = FS::lvm2_pv::getVGName(node); + mounted = false; } else { mountPoint = mountPoints.findByDevice(node) ? mountPoints.findByDevice(node)->mountPoint() :