2020-09-30 23:28:47 +01:00
/*
SPDX - FileCopyrightText : 2016 Chantara Tith < tith . chantara @ gmail . com >
SPDX - FileCopyrightText : 2016 - 2018 Andrius Š tikonas < andrius @ stikonas . eu >
SPDX - FileCopyrightText : 2016 Teo Mrnjavac < teo @ kde . org >
2020-10-01 00:33:19 +01:00
SPDX - FileCopyrightText : 2018 Caio Jordão Carvalho < caiojcarvalho @ gmail . com >
2020-09-30 23:28:47 +01:00
SPDX - License - Identifier : GPL - 3.0 - or - later
*/
2016-07-03 14:45:34 +01:00
# include "ops/removevolumegroupoperation.h"
# include "jobs/removevolumegroupjob.h"
2018-07-23 17:34:40 +01:00
# include "core/lvmdevice.h"
2016-11-05 15:33:20 +00:00
# include "core/partition.h"
2016-11-05 13:10:30 +00:00
# include "core/partitiontable.h"
2016-07-03 14:45:34 +01:00
# include "core/volumemanagerdevice.h"
2018-10-11 21:51:25 +01:00
# include "core/raid/softwareraid.h"
2016-07-03 14:45:34 +01:00
# include <QString>
# include <KLocalizedString>
/** Creates a new RemoveVolumeGroupOperation.
*/
2016-08-12 01:57:34 +01:00
RemoveVolumeGroupOperation : : RemoveVolumeGroupOperation ( VolumeManagerDevice & d ) :
2016-07-03 14:45:34 +01:00
Operation ( ) ,
2016-08-12 01:57:34 +01:00
m_RemoveVolumeGroupJob ( new RemoveVolumeGroupJob ( d ) ) ,
2016-12-13 15:36:36 +00:00
m_Device ( d ) ,
m_PartitionTable ( nullptr )
2016-07-03 14:45:34 +01:00
{
addJob ( removeVolumeGroupJob ( ) ) ;
}
QString RemoveVolumeGroupOperation : : description ( ) const
{
2018-10-11 21:51:25 +01:00
return xi18nc ( " @info/plain " , " Remove a %1 volume group. " , ( device ( ) . type ( ) = = Device : : Type : : SoftwareRAID_Device ? QStringLiteral ( " RAID " ) : QStringLiteral ( " LVM " ) ) ) ;
2016-07-03 14:45:34 +01:00
}
void RemoveVolumeGroupOperation : : preview ( )
{
2016-11-05 13:10:30 +00:00
m_PartitionTable = device ( ) . partitionTable ( ) ;
2018-07-23 17:34:40 +01:00
if ( device ( ) . type ( ) = = Device : : Type : : LVM_Device ) {
LvmDevice & lvm = static_cast < LvmDevice & > ( device ( ) ) ;
LvmDevice : : s_OrphanPVs < < lvm . physicalVolumes ( ) ;
}
2016-11-05 13:10:30 +00:00
device ( ) . setPartitionTable ( new PartitionTable ( PartitionTable : : vmd , 0 , device ( ) . totalLogical ( ) - 1 ) ) ;
2016-07-03 14:45:34 +01:00
}
void RemoveVolumeGroupOperation : : undo ( )
{
2018-07-23 17:34:40 +01:00
if ( device ( ) . type ( ) = = Device : : Type : : LVM_Device ) {
LvmDevice & lvm = static_cast < LvmDevice & > ( device ( ) ) ;
const QVector < const Partition * > constOrphanList = LvmDevice : : s_OrphanPVs ;
for ( const Partition * p : constOrphanList )
if ( lvm . physicalVolumes ( ) . contains ( p ) )
LvmDevice : : s_OrphanPVs . removeAll ( p ) ;
}
2016-11-05 13:10:30 +00:00
device ( ) . setPartitionTable ( m_PartitionTable ) ;
2016-07-03 14:45:34 +01:00
}
2016-11-05 15:33:20 +00:00
/** Check if Volume Group can be safely removed
*
* @ param dev VolumeManagerDevice with initialized partitions
* @ return true if there are no LVM partitions .
*/
bool RemoveVolumeGroupOperation : : isRemovable ( const VolumeManagerDevice * dev )
{
// TODO: allow removal when LVs are inactive.
2018-04-09 02:57:45 +01:00
if ( dev - > type ( ) = = Device : : Type : : LVM_Device ) {
2016-11-05 15:33:20 +00:00
if ( dev - > partitionTable ( ) - > children ( ) . count ( ) = = 0 ) // This is necessary to prevent a crash during applying of operations
return true ;
else if ( dev - > partitionTable ( ) - > children ( ) . count ( ) > 1 )
return false ;
else
2018-04-07 19:54:30 +01:00
if ( dev - > partitionTable ( ) - > children ( ) . first ( ) - > fileSystem ( ) . type ( ) = = FileSystem : : Type : : Unknown )
2016-11-05 15:33:20 +00:00
return true ;
}
2018-10-11 21:51:25 +01:00
else if ( dev - > type ( ) = = Device : : Type : : SoftwareRAID_Device ) {
const SoftwareRAID * raid = static_cast < const SoftwareRAID * > ( dev ) ;
return raid - > status ( ) = = SoftwareRAID : : Status : : Inactive ;
}
2016-11-05 15:33:20 +00:00
return false ;
}