File indexing completed on 2024-05-05 05:48:52

0001 /*
0002     SPDX-FileCopyrightText: 2016 Chantara Tith <tith.chantara@gmail.com>
0003     SPDX-FileCopyrightText: 2016-2018 Andrius Štikonas <andrius@stikonas.eu>
0004     SPDX-FileCopyrightText: 2016 Teo Mrnjavac <teo@kde.org>
0005     SPDX-FileCopyrightText: 2018 Caio Jordão Carvalho <caiojcarvalho@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-3.0-or-later
0008 */
0009 
0010 #include "ops/removevolumegroupoperation.h"
0011 #include "jobs/removevolumegroupjob.h"
0012 
0013 #include "core/lvmdevice.h"
0014 #include "core/partition.h"
0015 #include "core/partitiontable.h"
0016 #include "core/volumemanagerdevice.h"
0017 
0018 #include <QString>
0019 
0020 #include <KLocalizedString>
0021 
0022 /** Creates a new RemoveVolumeGroupOperation.
0023 */
0024 RemoveVolumeGroupOperation::RemoveVolumeGroupOperation(VolumeManagerDevice& d) :
0025     Operation(),
0026     m_RemoveVolumeGroupJob(new RemoveVolumeGroupJob(d)),
0027     m_Device(d),
0028     m_PartitionTable(nullptr)
0029 {
0030     addJob(removeVolumeGroupJob());
0031 }
0032 
0033 QString RemoveVolumeGroupOperation::description() const
0034 {
0035     return xi18nc("@info/plain", "Remove a LVM volume group.");
0036 }
0037 
0038 void RemoveVolumeGroupOperation::preview()
0039 {
0040     m_PartitionTable = device().partitionTable();
0041 
0042     if (device().type() == Device::Type::LVM_Device) {
0043         LvmDevice& lvm = static_cast<LvmDevice&>(device());
0044 
0045         LvmDevice::s_OrphanPVs << lvm.physicalVolumes();
0046     }
0047 
0048     device().setPartitionTable(new PartitionTable(PartitionTable::vmd, 0, device().totalLogical() - 1));
0049 }
0050 
0051 void RemoveVolumeGroupOperation::undo()
0052 {
0053     if (device().type() == Device::Type::LVM_Device) {
0054         LvmDevice& lvm = static_cast<LvmDevice&>(device());
0055 
0056         const QVector<const Partition*> constOrphanList = LvmDevice::s_OrphanPVs;
0057 
0058         for (const Partition* p : constOrphanList)
0059             if (lvm.physicalVolumes().contains(p))
0060                 LvmDevice::s_OrphanPVs.removeAll(p);
0061     }
0062 
0063     device().setPartitionTable(m_PartitionTable);
0064 }
0065 
0066 /** Check if Volume Group can be safely removed
0067  *
0068  *  @param dev VolumeManagerDevice with initialized partitions
0069  *  @return true if there are no LVM partitions.
0070  */
0071 bool RemoveVolumeGroupOperation::isRemovable(const VolumeManagerDevice* dev)
0072 {
0073     // TODO: allow removal when LVs are inactive.
0074     if (dev->type() == Device::Type::LVM_Device) {
0075         if (dev->partitionTable()->children().count() == 0) // This is necessary to prevent a crash during applying of operations
0076             return true;
0077         else if (dev->partitionTable()->children().count() > 1)
0078             return false;
0079         else
0080             if (dev->partitionTable()->children().first()->fileSystem().type() == FileSystem::Type::Unknown)
0081                 return true;
0082     }
0083 
0084     return false;
0085 }