2020-09-30 23:28:47 +01:00
/*
SPDX - FileCopyrightText : 2008 - 2010 Volker Lanz < vl @ fidra . de >
SPDX - FileCopyrightText : 2012 - 2019 Andrius Š tikonas < andrius @ stikonas . eu >
SPDX - FileCopyrightText : 2015 Teo Mrnjavac < teo @ kde . org >
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 "ops/newoperation.h"
# include "core/partition.h"
# include "core/device.h"
# include "core/partitionnode.h"
# include "jobs/createpartitionjob.h"
# include "jobs/createfilesystemjob.h"
2020-03-18 18:24:27 +00:00
# include "jobs/setpartitionlabeljob.h"
Add new job to set the GPT partition UUID
The GPT partition layout supports unique GUID partitions.
The CLI sfdisk sets the partition UUID using the option --part-uuid.
See the examples below:
$ cat <<EOF | sfdisk disk.img
label: gpt
type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, size=64M
type=0FC63DAF-8483-4772-8E79-3D69D8477DE4
EOF
(...)
$ sfdisk --dump disk.img
(...)
disk.img1 : start= 2048, size= 131072, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=BBE806BB-8567-A843-9FF5-9B6B35D2908E
disk.img2 : start= 133120, size= 1963999, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=7DB39F08-C138-664B-B38E-ED2DEB549AA6
$ sfdisk --part-uuid disk.img 1 00000000^C000-0000-0000-000000000000
(...)
$ sfdisk --part-uuid disk.img 2 11111111-1111-1111-1111-111111111111
(...)
$ sfdisk --dump disk.img
(...)
disk.img1 : start= 2048, size= 131072, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=00000000-0000-0000-0000-000000000000
disk.img2 : start= 133120, size= 1963999, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=11111111-1111-1111-1111-111111111111
This commit introduces the new job set-partition-uuid that is used in
the new-operation to set the UUID of the partition. The job uses the
newly introduced method setPartitionUUID that is implemented by the
sfdisk and dummy backends.
Note: This is a copypaste of what was done for GTP partition label in
commit f585f6c (Add new job to set the GPT partition label).
2020-03-18 18:24:27 +00:00
# include "jobs/setpartitionuuidjob.h"
Add new job to set the GPT partition attributes
The GPT partition layout supports partition attributes.
The CLI sfdisk sets the partition attributes using the option
--part-attrs. See the examples below:
$ cat <<EOF | sfdisk disk.img
label: gpt
type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, size=64M
type=0FC63DAF-8483-4772-8E79-3D69D8477DE4
EOF
(...)
$ sfdisk --part-attrs disk.img 1 RequiredPartition,NoBlockIOProtocol,LegacyBIOSBootable,48
(...)
$ sfdisk --part-attrs disk.img 2 60,61,62,63
(...)
$ sfdisk --dump disk.img
(...)
disk.img1 : start= 2048, size= 131072, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=BBE806BB-8567-A843-9FF5-9B6B35D2908E, attrs="RequiredPartition NoBlockIOProtocol LegacyBIOSBootable GUID:48"
disk.img2 : start= 133120, size= 1963999, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=7DB39F08-C138-664B-B38E-ED2DEB549AA6, attrs="GUID:60,61,62,63"
This commit introduces the new job set-partition-attributes that is used
in the new-operation to set the attributes of the partition. The job
uses the newly introduced method setPartitionAttributes that is
implemented by the sfdisk and dummy backends.
Note: This is a copypaste of what was done for GPT partition label in
commit f585f6c (Add new job to set the GPT partition label) and GPT
partition UUID in commit 1dde035 (Add new job to set the GPT partition
UUID).
Note: RequiredPartition, NoBlockIOProtocol, LegacyBIOSBootable are
key words for 0, 1 and 2.
2020-05-15 17:24:06 +01:00
# include "jobs/setpartitionattributesjob.h"
2015-06-04 01:29:22 +01:00
# include "jobs/setfilesystemlabeljob.h"
# include "jobs/setpartflagsjob.h"
# include "jobs/checkfilesystemjob.h"
2021-12-09 15:54:41 +00:00
# include "jobs/changepermissionsjob.h"
2015-06-04 01:29:22 +01:00
# include "fs/filesystem.h"
# include "fs/filesystemfactory.h"
# include "util/capacity.h"
# include <QString>
# include <KLocalizedString>
2020-10-27 18:00:37 +00:00
struct NewOperationPrivate
{
NewOperationPrivate ( Device & d , Partition * p ) :
m_TargetDevice ( d ) ,
m_NewPartition ( p ) ,
m_CreatePartitionJob ( new CreatePartitionJob ( d , * p ) ) ,
m_SetPartitionLabelJob ( nullptr ) ,
m_SetPartitionUUIDJob ( nullptr ) ,
m_SetPartitionAttributesJob ( nullptr ) ,
m_CreateFileSystemJob ( nullptr ) ,
m_SetPartFlagsJob ( nullptr ) ,
m_SetFileSystemLabelJob ( nullptr ) ,
m_CheckFileSystemJob ( nullptr )
{
}
Device & m_TargetDevice ;
Partition * m_NewPartition ;
CreatePartitionJob * m_CreatePartitionJob ;
SetPartitionLabelJob * m_SetPartitionLabelJob ;
SetPartitionUUIDJob * m_SetPartitionUUIDJob ;
SetPartitionAttributesJob * m_SetPartitionAttributesJob ;
CreateFileSystemJob * m_CreateFileSystemJob ;
SetPartFlagsJob * m_SetPartFlagsJob ;
SetFileSystemLabelJob * m_SetFileSystemLabelJob ;
CheckFileSystemJob * m_CheckFileSystemJob ;
} ;
2015-06-04 01:29:22 +01:00
/** Creates a new NewOperation.
2015-07-13 15:16:36 +01:00
@ param d the Device to create a new Partition on
2015-07-22 14:48:03 +01:00
@ param p pointer to the new Partition to create . May not be nullptr .
2015-06-04 01:29:22 +01:00
*/
NewOperation : : NewOperation ( Device & d , Partition * p ) :
2015-07-13 15:16:36 +01:00
Operation ( ) ,
2020-11-12 01:04:47 +00:00
d_ptr ( std : : make_unique < NewOperationPrivate > ( d , p ) )
2015-06-04 01:29:22 +01:00
{
2015-07-13 15:16:36 +01:00
addJob ( createPartitionJob ( ) ) ;
2015-06-04 01:29:22 +01:00
2020-03-18 18:24:27 +00:00
if ( ! p - > label ( ) . isEmpty ( ) ) {
2020-11-12 01:04:47 +00:00
d_ptr - > m_SetPartitionLabelJob = new SetPartitionLabelJob ( targetDevice ( ) , newPartition ( ) , p - > label ( ) ) ;
2020-03-18 18:24:27 +00:00
addJob ( setPartitionLabelJob ( ) ) ;
}
Add new job to set the GPT partition UUID
The GPT partition layout supports unique GUID partitions.
The CLI sfdisk sets the partition UUID using the option --part-uuid.
See the examples below:
$ cat <<EOF | sfdisk disk.img
label: gpt
type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, size=64M
type=0FC63DAF-8483-4772-8E79-3D69D8477DE4
EOF
(...)
$ sfdisk --dump disk.img
(...)
disk.img1 : start= 2048, size= 131072, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=BBE806BB-8567-A843-9FF5-9B6B35D2908E
disk.img2 : start= 133120, size= 1963999, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=7DB39F08-C138-664B-B38E-ED2DEB549AA6
$ sfdisk --part-uuid disk.img 1 00000000^C000-0000-0000-000000000000
(...)
$ sfdisk --part-uuid disk.img 2 11111111-1111-1111-1111-111111111111
(...)
$ sfdisk --dump disk.img
(...)
disk.img1 : start= 2048, size= 131072, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=00000000-0000-0000-0000-000000000000
disk.img2 : start= 133120, size= 1963999, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=11111111-1111-1111-1111-111111111111
This commit introduces the new job set-partition-uuid that is used in
the new-operation to set the UUID of the partition. The job uses the
newly introduced method setPartitionUUID that is implemented by the
sfdisk and dummy backends.
Note: This is a copypaste of what was done for GTP partition label in
commit f585f6c (Add new job to set the GPT partition label).
2020-03-18 18:24:27 +00:00
if ( ! p - > uuid ( ) . isEmpty ( ) ) {
2020-11-12 01:04:47 +00:00
d_ptr - > m_SetPartitionUUIDJob = new SetPartitionUUIDJob ( targetDevice ( ) , newPartition ( ) , p - > uuid ( ) ) ;
Add new job to set the GPT partition UUID
The GPT partition layout supports unique GUID partitions.
The CLI sfdisk sets the partition UUID using the option --part-uuid.
See the examples below:
$ cat <<EOF | sfdisk disk.img
label: gpt
type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, size=64M
type=0FC63DAF-8483-4772-8E79-3D69D8477DE4
EOF
(...)
$ sfdisk --dump disk.img
(...)
disk.img1 : start= 2048, size= 131072, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=BBE806BB-8567-A843-9FF5-9B6B35D2908E
disk.img2 : start= 133120, size= 1963999, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=7DB39F08-C138-664B-B38E-ED2DEB549AA6
$ sfdisk --part-uuid disk.img 1 00000000^C000-0000-0000-000000000000
(...)
$ sfdisk --part-uuid disk.img 2 11111111-1111-1111-1111-111111111111
(...)
$ sfdisk --dump disk.img
(...)
disk.img1 : start= 2048, size= 131072, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=00000000-0000-0000-0000-000000000000
disk.img2 : start= 133120, size= 1963999, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=11111111-1111-1111-1111-111111111111
This commit introduces the new job set-partition-uuid that is used in
the new-operation to set the UUID of the partition. The job uses the
newly introduced method setPartitionUUID that is implemented by the
sfdisk and dummy backends.
Note: This is a copypaste of what was done for GTP partition label in
commit f585f6c (Add new job to set the GPT partition label).
2020-03-18 18:24:27 +00:00
addJob ( setPartitionUUIDJob ( ) ) ;
}
Add new job to set the GPT partition attributes
The GPT partition layout supports partition attributes.
The CLI sfdisk sets the partition attributes using the option
--part-attrs. See the examples below:
$ cat <<EOF | sfdisk disk.img
label: gpt
type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, size=64M
type=0FC63DAF-8483-4772-8E79-3D69D8477DE4
EOF
(...)
$ sfdisk --part-attrs disk.img 1 RequiredPartition,NoBlockIOProtocol,LegacyBIOSBootable,48
(...)
$ sfdisk --part-attrs disk.img 2 60,61,62,63
(...)
$ sfdisk --dump disk.img
(...)
disk.img1 : start= 2048, size= 131072, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=BBE806BB-8567-A843-9FF5-9B6B35D2908E, attrs="RequiredPartition NoBlockIOProtocol LegacyBIOSBootable GUID:48"
disk.img2 : start= 133120, size= 1963999, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=7DB39F08-C138-664B-B38E-ED2DEB549AA6, attrs="GUID:60,61,62,63"
This commit introduces the new job set-partition-attributes that is used
in the new-operation to set the attributes of the partition. The job
uses the newly introduced method setPartitionAttributes that is
implemented by the sfdisk and dummy backends.
Note: This is a copypaste of what was done for GPT partition label in
commit f585f6c (Add new job to set the GPT partition label) and GPT
partition UUID in commit 1dde035 (Add new job to set the GPT partition
UUID).
Note: RequiredPartition, NoBlockIOProtocol, LegacyBIOSBootable are
key words for 0, 1 and 2.
2020-05-15 17:24:06 +01:00
if ( p - > attributes ( ) ) {
2020-11-12 01:04:47 +00:00
d_ptr - > m_SetPartitionAttributesJob = new SetPartitionAttributesJob ( targetDevice ( ) , newPartition ( ) , p - > attributes ( ) ) ;
Add new job to set the GPT partition attributes
The GPT partition layout supports partition attributes.
The CLI sfdisk sets the partition attributes using the option
--part-attrs. See the examples below:
$ cat <<EOF | sfdisk disk.img
label: gpt
type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, size=64M
type=0FC63DAF-8483-4772-8E79-3D69D8477DE4
EOF
(...)
$ sfdisk --part-attrs disk.img 1 RequiredPartition,NoBlockIOProtocol,LegacyBIOSBootable,48
(...)
$ sfdisk --part-attrs disk.img 2 60,61,62,63
(...)
$ sfdisk --dump disk.img
(...)
disk.img1 : start= 2048, size= 131072, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=BBE806BB-8567-A843-9FF5-9B6B35D2908E, attrs="RequiredPartition NoBlockIOProtocol LegacyBIOSBootable GUID:48"
disk.img2 : start= 133120, size= 1963999, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=7DB39F08-C138-664B-B38E-ED2DEB549AA6, attrs="GUID:60,61,62,63"
This commit introduces the new job set-partition-attributes that is used
in the new-operation to set the attributes of the partition. The job
uses the newly introduced method setPartitionAttributes that is
implemented by the sfdisk and dummy backends.
Note: This is a copypaste of what was done for GPT partition label in
commit f585f6c (Add new job to set the GPT partition label) and GPT
partition UUID in commit 1dde035 (Add new job to set the GPT partition
UUID).
Note: RequiredPartition, NoBlockIOProtocol, LegacyBIOSBootable are
key words for 0, 1 and 2.
2020-05-15 17:24:06 +01:00
addJob ( setPartitionAttributesJob ( ) ) ;
}
2015-07-13 15:16:36 +01:00
const FileSystem & fs = newPartition ( ) . fileSystem ( ) ;
2015-06-04 01:29:22 +01:00
2018-04-07 19:54:30 +01:00
if ( fs . type ( ) ! = FileSystem : : Type : : Extended ) {
2015-07-13 15:16:36 +01:00
// It would seem tempting to skip the CreateFileSystemJob or the
// SetFileSystemLabelJob if either has nothing to do (unformatted FS or
// empty label). However, the user might later on decide to change FS or
// label. The operation stack will merge these operations with this one here
// and if the jobs don't exist things will break.
2015-06-04 01:29:22 +01:00
2020-11-12 01:04:47 +00:00
d_ptr - > m_CreateFileSystemJob = new CreateFileSystemJob ( targetDevice ( ) , newPartition ( ) , fs . label ( ) ) ;
2015-07-13 15:16:36 +01:00
addJob ( createFileSystemJob ( ) ) ;
2015-06-04 01:29:22 +01:00
2018-04-07 19:54:30 +01:00
if ( fs . type ( ) = = FileSystem : : Type : : Lvm2_PV ) {
2020-11-12 01:04:47 +00:00
d_ptr - > m_SetPartFlagsJob = new SetPartFlagsJob ( targetDevice ( ) , newPartition ( ) , PartitionTable : : Flag : : Lvm ) ;
2015-07-13 15:16:36 +01:00
addJob ( setPartFlagsJob ( ) ) ;
}
2015-06-04 01:29:22 +01:00
2020-11-12 01:04:47 +00:00
d_ptr - > m_SetFileSystemLabelJob = new SetFileSystemLabelJob ( newPartition ( ) , fs . label ( ) ) ;
2015-07-13 15:16:36 +01:00
addJob ( setLabelJob ( ) ) ;
2015-06-04 01:29:22 +01:00
2020-11-12 01:04:47 +00:00
d_ptr - > m_CheckFileSystemJob = new CheckFileSystemJob ( newPartition ( ) ) ;
2015-07-13 15:16:36 +01:00
addJob ( checkJob ( ) ) ;
2021-12-09 15:54:41 +00:00
// if the user never configured a new permission, nothing will run, if he did,
// then we change the permissions on the newly created partition.
addJob ( new ChangePermissionJob ( newPartition ( ) ) ) ;
2015-07-13 15:16:36 +01:00
}
2015-06-04 01:29:22 +01:00
}
NewOperation : : ~ NewOperation ( )
{
2015-07-13 15:16:36 +01:00
if ( status ( ) = = StatusPending )
2020-11-12 01:04:47 +00:00
delete d_ptr - > m_NewPartition ;
2020-10-27 18:00:37 +00:00
}
Partition & NewOperation : : newPartition ( )
{
2020-11-12 01:04:47 +00:00
return * d_ptr - > m_NewPartition ;
2020-10-27 18:00:37 +00:00
}
const Partition & NewOperation : : newPartition ( ) const
{
2020-11-12 01:04:47 +00:00
return * d_ptr - > m_NewPartition ;
2020-10-27 18:00:37 +00:00
}
Device & NewOperation : : targetDevice ( )
{
2020-11-12 01:04:47 +00:00
return d_ptr - > m_TargetDevice ;
2020-10-27 18:00:37 +00:00
}
const Device & NewOperation : : targetDevice ( ) const
{
2020-11-12 01:04:47 +00:00
return d_ptr - > m_TargetDevice ;
2020-10-27 18:00:37 +00:00
}
CreatePartitionJob * NewOperation : : createPartitionJob ( )
{
2020-11-12 01:04:47 +00:00
return d_ptr - > m_CreatePartitionJob ;
2020-10-27 18:00:37 +00:00
}
SetPartitionLabelJob * NewOperation : : setPartitionLabelJob ( )
{
2020-11-12 01:04:47 +00:00
return d_ptr - > m_SetPartitionLabelJob ;
2020-10-27 18:00:37 +00:00
}
SetPartitionUUIDJob * NewOperation : : setPartitionUUIDJob ( )
{
2020-11-12 01:04:47 +00:00
return d_ptr - > m_SetPartitionUUIDJob ;
2020-10-27 18:00:37 +00:00
}
SetPartitionAttributesJob * NewOperation : : setPartitionAttributesJob ( )
{
2020-11-12 01:04:47 +00:00
return d_ptr - > m_SetPartitionAttributesJob ;
2020-10-27 18:00:37 +00:00
}
CreateFileSystemJob * NewOperation : : createFileSystemJob ( )
{
2020-11-12 01:04:47 +00:00
return d_ptr - > m_CreateFileSystemJob ;
2020-10-27 18:00:37 +00:00
}
SetPartFlagsJob * NewOperation : : setPartFlagsJob ( )
{
2020-11-12 01:04:47 +00:00
return d_ptr - > m_SetPartFlagsJob ;
2020-10-27 18:00:37 +00:00
}
SetFileSystemLabelJob * NewOperation : : setLabelJob ( )
{
2020-11-12 01:04:47 +00:00
return d_ptr - > m_SetFileSystemLabelJob ;
2020-10-27 18:00:37 +00:00
}
CheckFileSystemJob * NewOperation : : checkJob ( )
{
2020-11-12 01:04:47 +00:00
return d_ptr - > m_CheckFileSystemJob ;
2015-06-04 01:29:22 +01:00
}
bool NewOperation : : targets ( const Device & d ) const
{
2015-07-13 15:16:36 +01:00
return d = = targetDevice ( ) ;
2015-06-04 01:29:22 +01:00
}
bool NewOperation : : targets ( const Partition & p ) const
{
2015-07-13 15:16:36 +01:00
return p = = newPartition ( ) ;
2015-06-04 01:29:22 +01:00
}
void NewOperation : : preview ( )
{
2015-07-13 15:16:36 +01:00
insertPreviewPartition ( targetDevice ( ) , newPartition ( ) ) ;
2015-06-04 01:29:22 +01:00
}
void NewOperation : : undo ( )
{
2015-07-13 15:16:36 +01:00
removePreviewPartition ( targetDevice ( ) , newPartition ( ) ) ;
2015-06-04 01:29:22 +01:00
}
QString NewOperation : : description ( ) const
{
2016-07-17 23:41:00 +01:00
return xi18nc ( " @info:status " , " Create a new partition (%1, %2) on < filename > % 3 < / filename > " , Capacity::formatByteSize(newPartition().capacity()), newPartition().fileSystem().name(), targetDevice().deviceNode()) ;
2015-06-04 01:29:22 +01:00
}
/** Can a Partition be created somewhere?
2015-07-22 14:48:03 +01:00
@ param p the Partition where a new Partition is to be created , may be nullptr
2015-07-13 15:16:36 +01:00
@ return true if a new Partition can be created in @ p p
2015-06-04 01:29:22 +01:00
*/
bool NewOperation : : canCreateNew ( const Partition * p )
{
2015-07-22 14:48:03 +01:00
return p ! = nullptr & & p - > roles ( ) . has ( PartitionRole : : Unallocated ) ;
2015-06-04 01:29:22 +01:00
}
2015-07-13 14:28:56 +01:00
Partition * NewOperation : : createNew ( const Partition & cloneFrom ,
2015-07-13 15:16:36 +01:00
FileSystem : : Type type )
2015-06-04 01:29:22 +01:00
{
2015-07-13 15:16:36 +01:00
Partition * p = new Partition ( cloneFrom ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
p - > deleteFileSystem ( ) ;
p - > setFileSystem ( FileSystemFactory : : create ( type ,
p - > firstSector ( ) ,
2017-09-03 15:37:14 +01:00
p - > lastSector ( ) ,
p - > sectorSize ( ) ) ) ;
2018-04-09 03:46:05 +01:00
p - > setState ( Partition : : State : : New ) ;
2015-07-13 15:16:36 +01:00
p - > setPartitionPath ( QString ( ) ) ;
2021-08-26 23:37:19 +01:00
p - > setAttributes ( 0 ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
return p ;
2015-06-04 01:29:22 +01:00
}