2020-09-29 23:55:11 +01:00
/*
SPDX - FileCopyrightText : 2020 Gaël PORTAY < gael . portay @ collabora . com >
SPDX - License - Identifier : GPL - 3.0 - or - later
*/
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"
# include "backend/corebackend.h"
# include "backend/corebackendmanager.h"
# include "backend/corebackenddevice.h"
# include "backend/corebackendpartitiontable.h"
# include "core/partition.h"
# include "core/device.h"
# include "util/report.h"
# include <KLocalizedString>
# include <memory>
/** Creates a new SetPartitionAttributesJob (GPT only)
@ param d the Device the Partition to be created will be on
@ param p the Partition whose attributes is to be set is on
@ param newAttrs the new attributes
*/
SetPartitionAttributesJob : : SetPartitionAttributesJob ( Device & d , Partition & p , quint64 newAttrs ) :
Job ( ) ,
m_Device ( d ) ,
m_Partition ( p ) ,
m_Attributes ( newAttrs )
{
}
bool SetPartitionAttributesJob : : run ( Report & parent )
{
Q_ASSERT ( partition ( ) . devicePath ( ) = = device ( ) . deviceNode ( ) ) ;
bool rval = true ;
Report * report = jobStarted ( parent ) ;
// The attributes are supported by GPT only, if the partition table is not GPT, just ignore the
// request and say all is well. This helps in operations because we don't have to check for
// support to avoid having a failed job.
if ( m_Device . partitionTable ( ) - > type ( ) ! = PartitionTable : : gpt )
report - > line ( ) < < xi18nc ( " @info:progress " , " Partition table of partition <filename>%1</filename> does not support setting attributes. Job ignored. " , partition ( ) . deviceNode ( ) ) ;
else {
std : : unique_ptr < CoreBackendDevice > backendDevice = CoreBackendManager : : self ( ) - > backend ( ) - > openDevice ( m_Device ) ;
if ( backendDevice ) {
std : : unique_ptr < CoreBackendPartitionTable > backendPartitionTable = backendDevice - > openPartitionTable ( ) ;
if ( backendPartitionTable ) {
if ( backendPartitionTable - > setPartitionAttributes ( * report , partition ( ) , m_Attributes ) ) {
rval = true ;
partition ( ) . setAttributes ( m_Attributes ) ;
backendPartitionTable - > commit ( ) ;
} else
report - > line ( ) < < xi18nc ( " @info:progress " , " Failed to set the attributes for the partition <filename>%1</filename>. " , partition ( ) . deviceNode ( ) ) ;
} else
report - > line ( ) < < xi18nc ( " @info:progress " , " Could not open partition table on device <filename>%1</filename> to set the attributes for the partition <filename>%2</filename>. " , device ( ) . deviceNode ( ) , partition ( ) . deviceNode ( ) ) ;
} else
report - > line ( ) < < xi18nc ( " @info:progress " , " Could not open device <filename>%1</filename> to set the attributes for partition <filename>%2</filename>. " , device ( ) . deviceNode ( ) , partition ( ) . deviceNode ( ) ) ;
}
jobFinished ( * report , rval ) ;
return rval ;
}
QString SetPartitionAttributesJob : : description ( ) const
{
return xi18nc ( " @info:progress " , " Set the attributes on partition <filename>%1</filename> to \" %2 \" " , partition ( ) . deviceNode ( ) , QString : : number ( attributes ( ) , 0x10 ) ) ;
}