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

0001 /*
0002     SPDX-FileCopyrightText: 2016 Chantara Tith <tith.chantara@gmail.com>
0003     SPDX-FileCopyrightText: 2016-2018 Andrius Štikonas <andrius@stikonas.eu>
0004 
0005     SPDX-License-Identifier: GPL-3.0-or-later
0006 */
0007 
0008 #include "ops/deactivatevolumegroupoperation.h"
0009 #include "jobs/deactivatevolumegroupjob.h"
0010 #include "jobs/deactivatelogicalvolumejob.h"
0011 
0012 #include "core/volumemanagerdevice.h"
0013 #include "core/partitiontable.h"
0014 #include "core/partition.h"
0015 
0016 #include <QString>
0017 
0018 #include <KLocalizedString>
0019 
0020 /** Creates a new RemoveVolumeGroupOperation.
0021 */
0022 DeactivateVolumeGroupOperation::DeactivateVolumeGroupOperation(VolumeManagerDevice& d) :
0023     Operation(),
0024     m_DeactivateVolumeGroupJob(new DeactivateVolumeGroupJob(d)),
0025     m_DeactivateLogicalVolumeJob(new DeactivateLogicalVolumeJob(d)),
0026     m_Device(d),
0027     m_PartitionTable(d.partitionTable())
0028 {
0029     addJob(deactivateLogicalVolumeJob());
0030     addJob(deactivateVolumeGroupJob());
0031 }
0032 
0033 QString DeactivateVolumeGroupOperation::description() const
0034 {
0035     return xi18nc("@info/plain", "Deactivate volume group.");
0036 }
0037 
0038 void DeactivateVolumeGroupOperation::preview()
0039 {
0040     m_PartitionTable = device().partitionTable();
0041     device().setPartitionTable(new PartitionTable(PartitionTable::vmd, 0, device().totalLogical() - 1));
0042 }
0043 
0044 void DeactivateVolumeGroupOperation::undo()
0045 {
0046     device().setPartitionTable(m_PartitionTable);
0047 }
0048 
0049 /** loop through given device's partitions to check if any is mounted.
0050  *
0051  *  @param dev VolumeManagerDevice with initialized partitions
0052  *  @return false if any of the device's partition is mounted.
0053  */
0054 bool DeactivateVolumeGroupOperation::isDeactivatable(const VolumeManagerDevice* dev)
0055 {
0056     if (dev->type() == Device::Type::LVM_Device) {
0057         for (const auto &p : dev->partitionTable()->children()) {
0058             if (p->isMounted()) {
0059                 return false;
0060             }
0061         }
0062         return true;
0063     }
0064 
0065     return false;
0066 }