diff --git a/src/jobs/CMakeLists.txt b/src/jobs/CMakeLists.txt index 569454b..a0184ee 100644 --- a/src/jobs/CMakeLists.txt +++ b/src/jobs/CMakeLists.txt @@ -1,24 +1,26 @@ set(JOBS_SRC jobs/resizefilesystemjob.cpp jobs/createfilesystemjob.cpp jobs/job.cpp jobs/checkfilesystemjob.cpp jobs/shredfilesystemjob.cpp jobs/createpartitionjob.cpp jobs/createpartitiontablejob.cpp jobs/createvolumegroupjob.cpp jobs/removevolumegroupjob.cpp + jobs/resizevolumegroupjob.cpp + jobs/movephysicalvolumejob.cpp jobs/setfilesystemlabeljob.cpp jobs/deletepartitionjob.cpp jobs/restorefilesystemjob.cpp jobs/setpartgeometryjob.cpp jobs/deletefilesystemjob.cpp jobs/backupfilesystemjob.cpp jobs/setpartflagsjob.cpp jobs/copyfilesystemjob.cpp jobs/movefilesystemjob.cpp ) set(JOBS_LIB_HDRS jobs/job.h ) diff --git a/src/jobs/movephysicalvolumejob.cpp b/src/jobs/movephysicalvolumejob.cpp new file mode 100644 index 0000000..10c635e --- /dev/null +++ b/src/jobs/movephysicalvolumejob.cpp @@ -0,0 +1,63 @@ +/************************************************************************* + * 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 .* + *************************************************************************/ + +#include "jobs/movephysicalvolumejob.h" + +#include "core/lvmdevice.h" + +#include "util/report.h" + +#include + +/** Creates a new MovePhysicalVolumeJob + @param vgname + @parem pvList +*/ +MovePhysicalVolumeJob::MovePhysicalVolumeJob(LvmDevice& dev, const QStringList partlist) : + Job(), + m_Device(dev), + m_PartList(partlist) +{ +} + +bool MovePhysicalVolumeJob::run(Report& parent) +{ + bool rval = false; + + Report* report = jobStarted(parent); + + //TODO:check that the provided list is legal + foreach (QString partPath, partList()) { + rval = LvmDevice::movePV(*report, device(), partPath); + if (rval == false) { + break; + } + } + + jobFinished(*report, rval); + + return rval; +} + +QString MovePhysicalVolumeJob::description() const +{ + QString tmp = QString(); + foreach (QString path, partList()) { + tmp += path + QStringLiteral(","); + } + return xi18nc("@info/plain", "Move used PE in %1 on %2 to other available Physical Volumes", tmp, device().name()); +} diff --git a/src/jobs/movephysicalvolumejob.h b/src/jobs/movephysicalvolumejob.h new file mode 100644 index 0000000..2293c90 --- /dev/null +++ b/src/jobs/movephysicalvolumejob.h @@ -0,0 +1,55 @@ +/************************************************************************* + * 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(MOVEPHYSICALVOLUMEJOB_H) + +#define MOVEPHYSICALVOLUMEJOB_H + +#include "jobs/job.h" + +class LvmDevice; +class Report; + +class QString; + +class MovePhysicalVolumeJob : public Job +{ +public: + MovePhysicalVolumeJob(LvmDevice& dev, const QStringList partlist); + +public: + bool run(Report& parent) override; + QString description() const override; + + +protected: + LvmDevice& device() { + return m_Device; + } + const LvmDevice& device() const { + return m_Device; + } + const QStringList partList() const { + return m_PartList; + } + +private: + LvmDevice& m_Device; + const QStringList m_PartList; +}; + +#endif diff --git a/src/jobs/resizevolumegroupjob.cpp b/src/jobs/resizevolumegroupjob.cpp new file mode 100644 index 0000000..77dd4a2 --- /dev/null +++ b/src/jobs/resizevolumegroupjob.cpp @@ -0,0 +1,74 @@ +/************************************************************************* + * 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 .* + *************************************************************************/ + +#include "jobs/resizevolumegroupjob.h" + +#include "core/lvmdevice.h" + +#include "util/report.h" + +#include + +/** Creates a new ResizeVolumeGroupJob + @param vgname + @parem pvList +*/ +ResizeVolumeGroupJob::ResizeVolumeGroupJob(LvmDevice& dev, const QStringList partlist, const Type type) : + Job(), + m_Device(dev), + m_PartList(partlist), + m_Type(type) +{ +} + +bool ResizeVolumeGroupJob::run(Report& parent) +{ + bool rval = false; + + Report* report = jobStarted(parent); + + //TODO:check that the provided list is legal + foreach (QString pvpath, partList()) { + if (type() == ResizeVolumeGroupJob::Grow) { + rval = LvmDevice::insertPV(*report, device(), pvpath); + } else if (type() == ResizeVolumeGroupJob::Shrink) { + rval = LvmDevice::removePV(*report, device(), pvpath); + } + if (rval == false) { + break; + } + } + + jobFinished(*report, rval); + + return rval; +} + +QString ResizeVolumeGroupJob::description() const +{ + QString tmp = QString(); + foreach (QString path, partList()) { + tmp += path + QStringLiteral(","); + } + if (type() == ResizeVolumeGroupJob::Grow) { + return xi18nc("@info/plain", "Inserting Volume: %1 to %2.", tmp, device().name()); + } + if (type() == ResizeVolumeGroupJob::Shrink) { + return xi18nc("@info/plain", "Removing Volume: %1 from %2.", tmp, device().name()); + } + return xi18nc("@info/plain", "Resizing Volume: %1 to %2.", device().name(), tmp); +} diff --git a/src/jobs/resizevolumegroupjob.h b/src/jobs/resizevolumegroupjob.h new file mode 100644 index 0000000..bfdc643 --- /dev/null +++ b/src/jobs/resizevolumegroupjob.h @@ -0,0 +1,67 @@ +/************************************************************************* + * 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(RESIZEVOLUMEGROUPJOB_H) + +#define RESIZEVOLUMEGROUPJOB_H + +#include "jobs/job.h" + +class LvmDevice; +class Report; + +class QString; + +class ResizeVolumeGroupJob : public Job +{ + +public: + enum Type { + Grow = 0, + Shrink = 1 + }; + +public: + ResizeVolumeGroupJob(LvmDevice& dev, const QStringList partlist, const Type type); + +public: + bool run(Report& parent) override; + QString description() const override; + +protected: + LvmDevice& device() { + return m_Device; + } + const LvmDevice& device() const { + return m_Device; + } + + const QStringList partList() const { + return m_PartList; + } + + const Type type() const { + return m_Type; + } + +private: + LvmDevice& m_Device; + const QStringList m_PartList; + const Type m_Type; +}; + +#endif diff --git a/src/ops/CMakeLists.txt b/src/ops/CMakeLists.txt index 75bdaff..9ceda6e 100644 --- a/src/ops/CMakeLists.txt +++ b/src/ops/CMakeLists.txt @@ -1,33 +1,35 @@ set(OPS_SRC ops/operation.cpp ops/deleteoperation.cpp ops/restoreoperation.cpp ops/resizeoperation.cpp ops/newoperation.cpp ops/createfilesystemoperation.cpp ops/createpartitiontableoperation.cpp ops/createvolumegroupoperation.cpp ops/removevolumegroupoperation.cpp + ops/resizevolumegroupoperation.cpp ops/setfilesystemlabeloperation.cpp ops/setpartflagsoperation.cpp ops/checkoperation.cpp ops/backupoperation.cpp ops/copyoperation.cpp ) set(OPS_LIB_HDRS ops/backupoperation.h ops/checkoperation.h ops/copyoperation.h ops/createfilesystemoperation.h ops/createpartitiontableoperation.h ops/createvolumegroupoperation.h ops/removevolumegroupoperation.h + ops/resizevolumegroupoperation.h ops/deleteoperation.h ops/newoperation.h ops/operation.h ops/resizeoperation.h ops/restoreoperation.h ops/setfilesystemlabeloperation.h ops/setpartflagsoperation.h ) diff --git a/src/ops/resizevolumegroupoperation.cpp b/src/ops/resizevolumegroupoperation.cpp new file mode 100644 index 0000000..2e9eae6 --- /dev/null +++ b/src/ops/resizevolumegroupoperation.cpp @@ -0,0 +1,118 @@ +/************************************************************************* + * 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 .* + *************************************************************************/ + +#include "ops/resizevolumegroupoperation.h" + +#include "core/lvmdevice.h" +#include "fs/lvm2_pv.h" +#include "core/partition.h" + +#include "jobs/resizevolumegroupjob.h" +#include "jobs/movephysicalvolumejob.h" + +#include + +#include + +/** Creates a new ResizeVolumeGroupOperation. + @param d the Device to create the new PartitionTable on + @param t the type for the new PartitionTable +*/ +ResizeVolumeGroupOperation::ResizeVolumeGroupOperation(LvmDevice& dev, const QStringList partlist) : + Operation(), + m_Device(dev), + m_TargetList(partlist), + m_CurrentList(LvmDevice::getPVs(dev.name())), + m_GrowVolumeGroupJob(nullptr), + m_ShrinkVolumeGroupJob(nullptr), + m_MovePhysicalVolumeJob(nullptr) +{ + const QStringList clist = LvmDevice::getPVs(dev.name()); + + QStringList toRemoveList = clist; + foreach (QString path, partlist) { + if (toRemoveList.contains(path)) { + toRemoveList.removeAll(path); + } + } + + QStringList toInsertList = partlist; + foreach (QString path, clist) { + if (toInsertList.contains(path)) { + toInsertList.removeAll(path); + } + } + + qint64 freePE = FS::lvm2_pv::getFreePE(clist) - FS::lvm2_pv::getFreePE(toRemoveList); + qint64 movePE = FS::lvm2_pv::getAllocatedPE(toRemoveList); + qint64 growPE = FS::lvm2_pv::getPVSize(toInsertList) / LvmDevice::getPeSize(dev.name()); + + if ( movePE > (freePE + growPE)) { + // *ABORT* can't move + } else if (partlist == clist) { + // *DO NOTHING* + } else { + if (!toInsertList.isEmpty()) { + m_GrowVolumeGroupJob = new ResizeVolumeGroupJob(dev, toInsertList, ResizeVolumeGroupJob::Grow); + addJob(growVolumeGroupJob()); + } + if (!toRemoveList.isEmpty()) { + m_MovePhysicalVolumeJob = new MovePhysicalVolumeJob(dev, toRemoveList); + m_ShrinkVolumeGroupJob = new ResizeVolumeGroupJob(dev, toRemoveList, ResizeVolumeGroupJob::Shrink); + addJob(movePhysicalVolumeJob()); + addJob(shrinkvolumegroupjob()); + } + + } + +} + +QString ResizeVolumeGroupOperation::description() const +{ + QString tlist = QString(); + foreach (QString path, targetList()) { + tlist += path + QStringLiteral(","); + } + QString clist = QString(); + foreach (QString path, currentList()) { + clist += path + QStringLiteral(","); + } + return xi18nc("@info/plain", "Resize volume %1 From\n%2 To\n%3", device().name(), clist, tlist); +} + +bool ResizeVolumeGroupOperation::targets(const Device& d) const +{ + return d == device(); +} + +bool ResizeVolumeGroupOperation::targets(const Partition& part) const +{ + foreach (QString partPath, targetList()) { + if (partPath == part.partitionPath()) { + return true; + } + } + return false; +} + +void ResizeVolumeGroupOperation::preview() +{ +} + +void ResizeVolumeGroupOperation::undo() +{ +} diff --git a/src/ops/resizevolumegroupoperation.h b/src/ops/resizevolumegroupoperation.h new file mode 100644 index 0000000..45a5d7d --- /dev/null +++ b/src/ops/resizevolumegroupoperation.h @@ -0,0 +1,98 @@ +/************************************************************************* + * 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(RESIZEVOLUMEGROUPOPERATION_H) + +#define RESIZEVOLUMEGROUPOPERATION_H + +#include "util/libpartitionmanagerexport.h" + +#include "ops/operation.h" + +#include "core/lvmdevice.h" + +#include + +class ResizeVolumeGroupJob; +class MovePhysicalVolumeJob; +class OperationStack; +class LvmDevice; + +class LIBKPMCORE_EXPORT ResizeVolumeGroupOperation : public Operation +{ + Q_DISABLE_COPY(ResizeVolumeGroupOperation) + + friend class OperationStack; + +public: + ResizeVolumeGroupOperation(LvmDevice& dev, const QStringList partlist); + +public: + QString iconName() const override { + return QStringLiteral("arrow-right-double"); + } + + QString description() const override; + + virtual bool targets(const Device&) const override; + virtual bool targets(const Partition&) const override; + + virtual void preview() override; + virtual void undo() override; + + QStringList getToRemoveList(); + QStringList getToInsertList(); + +protected: + LvmDevice& device() { + return m_Device; + } + const LvmDevice& device() const { + return m_Device; + } + const QStringList targetList() const { + return m_TargetList; + } + + const QStringList currentList() const { + return m_CurrentList; + } + + ResizeVolumeGroupJob* growVolumeGroupJob() { + return m_GrowVolumeGroupJob; + } + + ResizeVolumeGroupJob* shrinkvolumegroupjob() { + return m_ShrinkVolumeGroupJob; + } + + MovePhysicalVolumeJob* movePhysicalVolumeJob() { + return m_MovePhysicalVolumeJob; + } + +private: + LvmDevice& m_Device; + + const QStringList m_TargetList; + const QStringList m_CurrentList; + + ResizeVolumeGroupJob *m_GrowVolumeGroupJob; + ResizeVolumeGroupJob *m_ShrinkVolumeGroupJob; + MovePhysicalVolumeJob *m_MovePhysicalVolumeJob; +}; + +#endif