2020-09-29 23:55:11 +01:00
/*
SPDX - FileCopyrightText : 2008 - 2010 Volker Lanz < vl @ fidra . de >
SPDX - FileCopyrightText : 2013 - 2018 Andrius Š tikonas < andrius @ stikonas . eu >
SPDX - FileCopyrightText : 2016 Chantara Tith < tith . chantara @ gmail . com >
2020-10-01 00:33:19 +01:00
SPDX - FileCopyrightText : 2018 Caio Jordão Carvalho < caiojcarvalho @ gmail . com >
2020-09-29 23:55:11 +01:00
SPDX - FileCopyrightText : 2019 Yuri Chornoivan < yurchor @ ukr . net >
SPDX - FileCopyrightText : 2020 Gaël PORTAY < gael . portay @ collabora . com >
SPDX - License - Identifier : GPL - 3.0 - or - later
*/
2015-06-04 01:29:22 +01:00
# include "jobs/createpartitionjob.h"
# include "backend/corebackend.h"
# include "backend/corebackendmanager.h"
# include "backend/corebackenddevice.h"
# include "backend/corebackendpartitiontable.h"
# include "core/partition.h"
# include "core/device.h"
2016-06-23 19:41:55 +01:00
# include "core/lvmdevice.h"
2015-06-04 01:29:22 +01:00
# include "util/report.h"
# include <KLocalizedString>
/** Creates a new CreatePartitionJob
2015-07-13 15:16:36 +01:00
@ param d the Device the Partition to be created will be on
@ param p the Partition to create
2015-06-04 01:29:22 +01:00
*/
CreatePartitionJob : : CreatePartitionJob ( Device & d , Partition & p ) :
2015-07-13 15:16:36 +01:00
Job ( ) ,
m_Device ( d ) ,
m_Partition ( p )
2015-06-04 01:29:22 +01:00
{
}
bool CreatePartitionJob : : run ( Report & parent )
{
2015-07-13 15:16:36 +01:00
Q_ASSERT ( partition ( ) . devicePath ( ) = = device ( ) . deviceNode ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
bool rval = false ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
Report * report = jobStarted ( parent ) ;
2015-06-04 01:29:22 +01:00
2018-07-11 00:03:59 +01:00
if ( device ( ) . type ( ) = = Device : : Type : : Disk_Device | | device ( ) . type ( ) = = Device : : Type : : SoftwareRAID_Device ) {
2018-03-31 14:44:40 +01:00
std : : unique_ptr < CoreBackendDevice > backendDevice = CoreBackendManager : : self ( ) - > backend ( ) - > openDevice ( device ( ) ) ;
2015-06-04 01:29:22 +01:00
2016-05-31 19:28:31 +01:00
if ( backendDevice ) {
2018-03-31 21:49:20 +01:00
std : : unique_ptr < CoreBackendPartitionTable > backendPartitionTable = backendDevice - > openPartitionTable ( ) ;
2015-06-04 01:29:22 +01:00
2016-05-31 19:28:31 +01:00
if ( backendPartitionTable ) {
QString partitionPath = backendPartitionTable - > createPartition ( * report , partition ( ) ) ;
2015-06-04 01:29:22 +01:00
2019-12-09 16:09:53 +00:00
if ( ! partitionPath . isEmpty ( ) ) {
2016-05-31 19:28:31 +01:00
rval = true ;
partition ( ) . setPartitionPath ( partitionPath ) ;
2018-04-09 03:46:05 +01:00
partition ( ) . setState ( Partition : : State : : None ) ;
2016-05-31 19:28:31 +01:00
backendPartitionTable - > commit ( ) ;
2020-05-15 16:22:57 +01:00
// The UUID is supported by GPT only; it is generated automatically once the creation of a partition.
// Store the generated UUID to the partition object if no UUID is set.
if ( m_Device . partitionTable ( ) - > type ( ) = = PartitionTable : : gpt & & partition ( ) . uuid ( ) . isEmpty ( ) )
partition ( ) . setUUID ( backendPartitionTable - > getPartitionUUID ( * report , partition ( ) ) ) ;
2016-05-31 19:28:31 +01:00
} else
report - > line ( ) < < xi18nc ( " @info/plain " , " Failed to add partition <filename>%1</filename> to device <filename>%2</filename>. " , partition ( ) . deviceNode ( ) , device ( ) . deviceNode ( ) ) ;
2015-07-13 15:16:36 +01:00
} else
2016-05-31 19:28:31 +01:00
report - > line ( ) < < xi18nc ( " @info:progress " , " Could not open partition table on device <filename>%1</filename> to create new partition <filename>%2</filename>. " , device ( ) . deviceNode ( ) , partition ( ) . deviceNode ( ) ) ;
2015-07-13 15:16:36 +01:00
} else
2016-05-31 19:28:31 +01:00
report - > line ( ) < < xi18nc ( " @info:progress " , " Could not open device <filename>%1</filename> to create new partition <filename>%2</filename>. " , device ( ) . deviceNode ( ) , partition ( ) . deviceNode ( ) ) ;
2018-04-09 02:57:45 +01:00
} else if ( device ( ) . type ( ) = = Device : : Type : : LVM_Device ) {
2016-06-23 19:41:55 +01:00
LvmDevice & dev = dynamic_cast < LvmDevice & > ( device ( ) ) ;
2018-04-09 03:46:05 +01:00
partition ( ) . setState ( Partition : : State : : None ) ;
2016-06-25 12:01:32 +01:00
QString partPath = partition ( ) . partitionPath ( ) ;
QString lvname = partPath . right ( partPath . length ( ) - partPath . lastIndexOf ( QStringLiteral ( " / " ) ) - 1 ) ;
rval = LvmDevice : : createLV ( * report , dev , partition ( ) , lvname ) ;
2016-05-31 19:28:31 +01:00
}
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
jobFinished ( * report , rval ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
return rval ;
2015-06-04 01:29:22 +01:00
}
QString CreatePartitionJob : : description ( ) const
{
2015-07-13 15:16:36 +01:00
if ( partition ( ) . number ( ) > 0 )
2016-07-17 23:41:00 +01:00
return xi18nc ( " @info:progress " , " Create new partition <filename>%1</filename> " , partition ( ) . deviceNode ( ) ) ;
2015-06-04 01:29:22 +01:00
2016-07-17 23:41:00 +01:00
return xi18nc ( " @info:progress " , " Create new partition on device <filename>%1</filename> " , device ( ) . deviceNode ( ) ) ;
2015-06-04 01:29:22 +01:00
}