2020-09-30 23:58:54 +01:00
/*
SPDX - FileCopyrightText : 2017 - 2019 Andrius Š tikonas < andrius @ stikonas . eu >
2020-10-01 00:33:19 +01:00
SPDX - FileCopyrightText : 2018 Caio Jordão Carvalho < caiojcarvalho @ gmail . com >
2020-09-30 23:58:54 +01:00
SPDX - FileCopyrightText : 2020 Gaël PORTAY < gael . portay @ collabora . com >
SPDX - License - Identifier : GPL - 3.0 - or - later
*/
2017-09-17 15:33:25 +01:00
# include "plugins/sfdisk/sfdiskpartitiontable.h"
2020-06-16 22:02:01 +01:00
# include "plugins/sfdisk/sfdiskgptattributes.h"
2017-09-17 15:33:25 +01:00
# include "backend/corebackend.h"
# include "backend/corebackendmanager.h"
# include "core/partition.h"
# include "core/device.h"
2018-07-11 03:03:21 +01:00
# include "core/raid/softwareraid.h"
2017-09-17 15:33:25 +01:00
# include "fs/filesystem.h"
# include "util/report.h"
# include "util/externalcommand.h"
# include <QJsonArray>
# include <QJsonDocument>
# include <QJsonObject>
# include <QRegularExpression>
# include <KLocalizedString>
2017-12-07 20:50:54 +00:00
SfdiskPartitionTable : : SfdiskPartitionTable ( const Device * d ) :
2017-09-17 15:33:25 +01:00
CoreBackendPartitionTable ( ) ,
2017-12-07 20:50:54 +00:00
m_device ( d )
2017-09-17 15:33:25 +01:00
{
}
SfdiskPartitionTable : : ~ SfdiskPartitionTable ( )
{
}
bool SfdiskPartitionTable : : open ( )
{
return true ;
}
bool SfdiskPartitionTable : : commit ( quint32 timeout )
{
2018-07-24 07:12:02 +01:00
if ( m_device - > type ( ) = = Device : : Type : : SoftwareRAID_Device )
ExternalCommand ( QStringLiteral ( " udevadm " ) , { QStringLiteral ( " control " ) , QStringLiteral ( " --stop-exec-queue " ) } ) . run ( ) ;
2018-04-13 13:54:51 +01:00
ExternalCommand ( QStringLiteral ( " udevadm " ) , { QStringLiteral ( " settle " ) , QStringLiteral ( " --timeout= " ) + QString : : number ( timeout ) } ) . run ( ) ;
2019-11-18 18:42:32 +00:00
ExternalCommand ( QStringLiteral ( " partx " ) , { QStringLiteral ( " --update " ) , m_device - > deviceNode ( ) } ) . run ( ) ;
2019-12-02 22:01:10 +00:00
ExternalCommand ( QStringLiteral ( " udevadm " ) , { QStringLiteral ( " trigger " ) , QStringLiteral ( " --subsystem-match=block " ) } ) . run ( ) ;
2017-09-17 15:33:25 +01:00
2018-07-24 07:12:02 +01:00
if ( m_device - > type ( ) = = Device : : Type : : SoftwareRAID_Device )
ExternalCommand ( QStringLiteral ( " udevadm " ) , { QStringLiteral ( " control " ) , QStringLiteral ( " --start-exec-queue " ) } ) . run ( ) ;
2019-09-22 19:24:21 +01:00
ExternalCommand ( QStringLiteral ( " udevadm " ) , { QStringLiteral ( " settle " ) , QStringLiteral ( " --timeout= " ) + QString : : number ( timeout ) } ) . run ( ) ;
2017-09-17 15:33:25 +01:00
return true ;
}
QString SfdiskPartitionTable : : createPartition ( Report & report , const Partition & partition )
{
if ( ! ( partition . roles ( ) . has ( PartitionRole : : Extended ) | | partition . roles ( ) . has ( PartitionRole : : Logical ) | | partition . roles ( ) . has ( PartitionRole : : Primary ) ) ) {
report . line ( ) < < xi18nc ( " @info:progress " , " Unknown partition role for new partition <filename>%1</filename> (roles: %2) " , partition . deviceNode ( ) , partition . roles ( ) . toString ( ) ) ;
return QString ( ) ;
}
2017-12-16 12:55:34 +00:00
QByteArray type = QByteArray ( ) ;
2017-09-17 15:33:25 +01:00
if ( partition . roles ( ) . has ( PartitionRole : : Extended ) )
type = QByteArrayLiteral ( " type=5 " ) ;
// NOTE: at least on GPT partition types "are" partition flags
ExternalCommand createCommand ( report , QStringLiteral ( " sfdisk " ) , { QStringLiteral ( " --force " ) , QStringLiteral ( " --append " ) , partition . devicePath ( ) } ) ;
2017-11-07 22:55:28 +00:00
if ( createCommand . write ( QByteArrayLiteral ( " start= " ) + QByteArray : : number ( partition . firstSector ( ) ) +
2017-09-17 15:33:25 +01:00
type +
2018-07-21 21:19:31 +01:00
QByteArrayLiteral ( " size= " ) + QByteArray : : number ( partition . length ( ) ) + QByteArrayLiteral ( " \n write \n " ) ) & & createCommand . start ( - 1 ) ) {
2018-07-11 23:36:20 +01:00
QRegularExpression re ( QStringLiteral ( " Created a new partition ( \\ d+) " )) ;
2017-09-17 15:33:25 +01:00
QRegularExpressionMatch rem = re . match ( createCommand . output ( ) ) ;
2018-07-11 03:03:21 +01:00
2018-07-11 23:36:20 +01:00
if ( rem . hasMatch ( ) ) {
2018-07-18 23:23:34 +01:00
if ( partition . devicePath ( ) . back ( ) . isDigit ( ) )
2018-07-11 23:36:20 +01:00
return partition . devicePath ( ) + QLatin1Char ( ' p ' ) + rem . captured ( 1 ) ;
else
return partition . devicePath ( ) + rem . captured ( 1 ) ;
}
2017-09-17 15:33:25 +01:00
}
2017-12-07 20:50:54 +00:00
report . line ( ) < < xi18nc ( " @info:progress " , " Failed to add partition <filename>%1</filename> to device <filename>%2</filename>. " , partition . deviceNode ( ) , m_device - > deviceNode ( ) ) ;
2017-09-17 15:33:25 +01:00
return QString ( ) ;
}
bool SfdiskPartitionTable : : deletePartition ( Report & report , const Partition & partition )
{
ExternalCommand deleteCommand ( report , QStringLiteral ( " sfdisk " ) , { QStringLiteral ( " --force " ) , QStringLiteral ( " --delete " ) , partition . devicePath ( ) , QString : : number ( partition . number ( ) ) } ) ;
if ( deleteCommand . run ( - 1 ) & & deleteCommand . exitCode ( ) = = 0 )
return true ;
report . line ( ) < < xi18nc ( " @info:progress " , " Could not delete partition <filename>%1</filename>. " , partition . devicePath ( ) ) ;
return false ;
}
bool SfdiskPartitionTable : : updateGeometry ( Report & report , const Partition & partition , qint64 sectorStart , qint64 sectorEnd )
{
ExternalCommand sfdiskCommand ( report , QStringLiteral ( " sfdisk " ) , { QStringLiteral ( " --force " ) , partition . devicePath ( ) , QStringLiteral ( " -N " ) , QString : : number ( partition . number ( ) ) } ) ;
2017-11-07 22:55:28 +00:00
if ( sfdiskCommand . write ( QByteArrayLiteral ( " start= " ) + QByteArray : : number ( sectorStart ) +
2017-09-17 15:33:25 +01:00
QByteArrayLiteral ( " size= " ) + QByteArray : : number ( sectorEnd - sectorStart + 1 ) +
QByteArrayLiteral ( " \n Y \n " ) )
2018-07-21 21:19:31 +01:00
& & sfdiskCommand . start ( - 1 ) & & sfdiskCommand . exitCode ( ) = = 0 ) {
2017-09-17 15:33:25 +01:00
return true ;
}
report . line ( ) < < xi18nc ( " @info:progress " , " Could not set geometry for partition <filename>%1</filename> while trying to resize/move it. " , partition . devicePath ( ) ) ;
return false ;
}
bool SfdiskPartitionTable : : clobberFileSystem ( Report & report , const Partition & partition )
{
ExternalCommand wipeCommand ( report , QStringLiteral ( " wipefs " ) , { QStringLiteral ( " --all " ) , partition . partitionPath ( ) } ) ;
if ( wipeCommand . run ( - 1 ) & & wipeCommand . exitCode ( ) = = 0 )
return true ;
report . line ( ) < < xi18nc ( " @info:progress " , " Failed to erase filesystem signature on partition <filename>%1</filename>. " , partition . partitionPath ( ) ) ;
return false ;
}
bool SfdiskPartitionTable : : resizeFileSystem ( Report & report , const Partition & partition , qint64 newLength )
{
// sfdisk does not have any partition resize capabilities
2017-11-07 22:55:28 +00:00
Q_UNUSED ( report )
Q_UNUSED ( partition )
Q_UNUSED ( newLength )
2017-09-17 15:33:25 +01:00
return false ;
}
FileSystem : : Type SfdiskPartitionTable : : detectFileSystemBySector ( Report & report , const Device & device , qint64 sector )
{
2018-04-07 19:54:30 +01:00
FileSystem : : Type type = FileSystem : : Type : : Unknown ;
2017-09-17 15:33:25 +01:00
ExternalCommand jsonCommand ( QStringLiteral ( " sfdisk " ) , { QStringLiteral ( " --json " ) , device . deviceNode ( ) } ) ;
if ( jsonCommand . run ( - 1 ) & & jsonCommand . exitCode ( ) = = 0 ) {
2017-12-04 21:18:37 +00:00
const QJsonArray partitionTable = QJsonDocument : : fromJson ( jsonCommand . rawOutput ( ) ) . object ( ) [ QLatin1String ( " partitiontable " ) ] . toObject ( ) [ QLatin1String ( " partitions " ) ] . toArray ( ) ;
2017-09-17 15:33:25 +01:00
for ( const auto & partition : partitionTable ) {
const QJsonObject partitionObject = partition . toObject ( ) ;
const qint64 start = partitionObject [ QLatin1String ( " start " ) ] . toVariant ( ) . toLongLong ( ) ;
if ( start = = sector ) {
const QString deviceNode = partitionObject [ QLatin1String ( " node " ) ] . toString ( ) ;
type = CoreBackendManager : : self ( ) - > backend ( ) - > detectFileSystem ( deviceNode ) ;
return type ;
}
}
}
report . line ( ) < < xi18nc ( " @info:progress " , " Could not determine file system of partition at sector %1 on device <filename>%2</filename>. " , sector , device . deviceNode ( ) ) ;
return type ;
}
2017-12-07 23:24:03 +00:00
static struct {
FileSystem : : Type type ;
QLatin1String partitionType [ 2 ] ; // GPT, MBR
} typemap [ ] = {
2018-04-07 19:54:30 +01:00
{ FileSystem : : Type : : Btrfs , { QLatin1String ( " 0FC63DAF-8483-4772-8E79-3D69D8477DE4 " ) , QLatin1String ( " 83 " ) } } ,
{ FileSystem : : Type : : Ext2 , { QLatin1String ( " 0FC63DAF-8483-4772-8E79-3D69D8477DE4 " ) , QLatin1String ( " 83 " ) } } ,
{ FileSystem : : Type : : Ext3 , { QLatin1String ( " 0FC63DAF-8483-4772-8E79-3D69D8477DE4 " ) , QLatin1String ( " 83 " ) } } ,
{ FileSystem : : Type : : Ext4 , { QLatin1String ( " 0FC63DAF-8483-4772-8E79-3D69D8477DE4 " ) , QLatin1String ( " 83 " ) } } ,
{ FileSystem : : Type : : LinuxSwap , { QLatin1String ( " 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F " ) , QLatin1String ( " 82 " ) } } ,
{ FileSystem : : Type : : Fat12 , { QLatin1String ( " EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 " ) , QLatin1String ( " 6 " ) } } ,
{ FileSystem : : Type : : Fat16 , { QLatin1String ( " EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 " ) , QLatin1String ( " 6 " ) } } ,
2019-10-15 21:19:33 +01:00
{ FileSystem : : Type : : Fat32 , { QLatin1String ( " EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 " ) , QLatin1String ( " c " ) } } ,
2018-04-07 19:54:30 +01:00
{ FileSystem : : Type : : Nilfs2 , { QLatin1String ( " 0FC63DAF-8483-4772-8E79-3D69D8477DE4 " ) , QLatin1String ( " 83 " ) } } ,
{ FileSystem : : Type : : Ntfs , { QLatin1String ( " EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 " ) , QLatin1String ( " 7 " ) } } ,
{ FileSystem : : Type : : Exfat , { QLatin1String ( " EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 " ) , QLatin1String ( " 7 " ) } } ,
{ FileSystem : : Type : : ReiserFS , { QLatin1String ( " 0FC63DAF-8483-4772-8E79-3D69D8477DE4 " ) , QLatin1String ( " 83 " ) } } ,
{ FileSystem : : Type : : Reiser4 , { QLatin1String ( " 0FC63DAF-8483-4772-8E79-3D69D8477DE4 " ) , QLatin1String ( " 83 " ) } } ,
{ FileSystem : : Type : : Xfs , { QLatin1String ( " 0FC63DAF-8483-4772-8E79-3D69D8477DE4 " ) , QLatin1String ( " 83 " ) } } ,
{ FileSystem : : Type : : Jfs , { QLatin1String ( " 0FC63DAF-8483-4772-8E79-3D69D8477DE4 " ) , QLatin1String ( " 83 " ) } } ,
{ FileSystem : : Type : : Hfs , { QLatin1String ( " 48465300-0000-11AA-AA11-00306543ECAC " ) , QLatin1String ( " af " ) } } ,
{ FileSystem : : Type : : HfsPlus , { QLatin1String ( " 48465300-0000-11AA-AA11-00306543ECAC " ) , QLatin1String ( " af " ) } } ,
{ FileSystem : : Type : : Udf , { QLatin1String ( " EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 " ) , QLatin1String ( " 7 " ) } }
2017-12-07 23:24:03 +00:00
// Add ZFS too
} ;
static QLatin1String getPartitionType ( FileSystem : : Type t , PartitionTable : : TableType tableType )
2017-09-17 15:33:25 +01:00
{
2017-12-07 23:24:03 +00:00
quint8 type ;
switch ( tableType ) {
2018-09-01 18:40:10 +01:00
case PartitionTable : : TableType : : gpt :
2017-12-07 23:24:03 +00:00
type = 0 ;
break ;
2018-09-01 18:40:10 +01:00
case PartitionTable : : TableType : : msdos :
case PartitionTable : : TableType : : msdos_sectorbased :
2017-12-07 23:24:03 +00:00
type = 1 ;
break ;
default : ;
2018-01-31 14:58:43 +00:00
return QLatin1String ( ) ;
2017-12-07 23:24:03 +00:00
}
for ( quint32 i = 0 ; i < sizeof ( typemap ) / sizeof ( typemap [ 0 ] ) ; i + + )
if ( typemap [ i ] . type = = t )
return typemap [ i ] . partitionType [ type ] ;
2017-11-07 22:55:28 +00:00
2017-12-07 23:24:03 +00:00
return QLatin1String ( ) ;
}
2020-03-18 18:24:27 +00:00
bool SfdiskPartitionTable : : setPartitionLabel ( Report & report , const Partition & partition , const QString & label )
{
if ( label . isEmpty ( ) )
return true ;
ExternalCommand sfdiskCommand ( report , QStringLiteral ( " sfdisk " ) , { QStringLiteral ( " --part-label " ) , m_device - > deviceNode ( ) , QString : : number ( partition . number ( ) ) ,
label } ) ;
return sfdiskCommand . run ( - 1 ) & & sfdiskCommand . exitCode ( ) = = 0 ;
}
2020-05-15 16:22:57 +01:00
QString SfdiskPartitionTable : : getPartitionUUID ( Report & report , const Partition & partition )
{
ExternalCommand sfdiskCommand ( report , QStringLiteral ( " sfdisk " ) , { QStringLiteral ( " --list " ) , QStringLiteral ( " --output " ) , QStringLiteral ( " Device,UUID " ) ,
m_device - > deviceNode ( ) } ) ;
if ( sfdiskCommand . run ( - 1 ) & & sfdiskCommand . exitCode ( ) = = 0 ) {
QRegularExpression re ( m_device - > deviceNode ( ) + QString : : number ( partition . number ( ) ) + QStringLiteral ( " +(.+) " ) ) ;
QRegularExpressionMatch rem = re . match ( sfdiskCommand . output ( ) ) ;
if ( rem . hasMatch ( ) )
return rem . captured ( 1 ) ;
}
return QString ( ) ;
}
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
bool SfdiskPartitionTable : : setPartitionUUID ( Report & report , const Partition & partition , const QString & uuid )
{
if ( uuid . isEmpty ( ) )
return true ;
ExternalCommand sfdiskCommand ( report , QStringLiteral ( " sfdisk " ) , { QStringLiteral ( " --part-uuid " ) , m_device - > deviceNode ( ) , QString : : number ( partition . number ( ) ) ,
uuid } ) ;
return sfdiskCommand . run ( - 1 ) & & sfdiskCommand . exitCode ( ) = = 0 ;
}
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
bool SfdiskPartitionTable : : setPartitionAttributes ( Report & report , const Partition & partition , quint64 attrs )
{
2020-06-16 22:02:01 +01:00
QStringList attributes = SfdiskGptAttributes : : toStringList ( attrs ) ;
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 ( attributes . isEmpty ( ) )
return true ;
ExternalCommand sfdiskCommand ( report , QStringLiteral ( " sfdisk " ) , { QStringLiteral ( " --part-attrs " ) , m_device - > deviceNode ( ) , QString : : number ( partition . number ( ) ) ,
attributes . join ( QStringLiteral ( " , " ) ) } ) ;
return sfdiskCommand . run ( - 1 ) & & sfdiskCommand . exitCode ( ) = = 0 ;
}
2017-12-07 23:24:03 +00:00
bool SfdiskPartitionTable : : setPartitionSystemType ( Report & report , const Partition & partition )
{
2020-03-19 12:15:13 +00:00
QString partitionType = partition . type ( ) ;
if ( partitionType . isEmpty ( ) )
partitionType = getPartitionType ( partition . fileSystem ( ) . type ( ) , m_device - > partitionTable ( ) - > type ( ) ) ;
2017-12-09 19:19:36 +00:00
if ( partitionType . isEmpty ( ) )
return true ;
2017-12-07 23:24:03 +00:00
ExternalCommand sfdiskCommand ( report , QStringLiteral ( " sfdisk " ) , { QStringLiteral ( " --part-type " ) , m_device - > deviceNode ( ) , QString : : number ( partition . number ( ) ) ,
2017-12-09 19:19:36 +00:00
partitionType } ) ;
2017-12-07 23:24:03 +00:00
return sfdiskCommand . run ( - 1 ) & & sfdiskCommand . exitCode ( ) = = 0 ;
2017-11-07 22:55:28 +00:00
}
2017-11-08 00:01:24 +00:00
bool SfdiskPartitionTable : : setFlag ( Report & report , const Partition & partition , PartitionTable : : Flag flag , bool state )
2017-11-07 22:55:28 +00:00
{
2018-09-01 21:27:05 +01:00
if ( m_device - > partitionTable ( ) - > type ( ) = = PartitionTable : : TableType : : msdos | |
m_device - > partitionTable ( ) - > type ( ) = = PartitionTable : : TableType : : msdos_sectorbased ) {
// We only allow setting one active partition per device
2019-02-09 18:04:44 +00:00
if ( flag = = PartitionTable : : Flag : : Boot & & state = = true ) {
2018-09-01 21:27:05 +01:00
ExternalCommand sfdiskCommand ( report , QStringLiteral ( " sfdisk " ) , { QStringLiteral ( " --activate " ) , m_device - > deviceNode ( ) , QString : : number ( partition . number ( ) ) } ) ;
if ( sfdiskCommand . run ( - 1 ) & & sfdiskCommand . exitCode ( ) = = 0 )
return true ;
else
return false ;
2019-02-09 18:04:44 +00:00
} else if ( flag = = PartitionTable : : Flag : : Boot & & state = = false ) {
2018-09-01 21:27:05 +01:00
ExternalCommand sfdiskCommand ( report , QStringLiteral ( " sfdisk " ) , { QStringLiteral ( " --activate " ) , m_device - > deviceNode ( ) , QStringLiteral ( " - " ) } ) ;
if ( sfdiskCommand . run ( - 1 ) & & sfdiskCommand . exitCode ( ) = = 0 )
return true ;
else
return false ;
}
2017-11-08 00:01:24 +00:00
}
2019-02-09 18:04:44 +00:00
if ( flag = = PartitionTable : : Flag : : Boot & & state = = true ) {
2017-12-07 20:50:54 +00:00
ExternalCommand sfdiskCommand ( report , QStringLiteral ( " sfdisk " ) , { QStringLiteral ( " --part-type " ) , m_device - > deviceNode ( ) , QString : : number ( partition . number ( ) ) ,
2017-11-08 00:01:24 +00:00
QStringLiteral ( " C12A7328-F81F-11D2-BA4B-00A0C93EC93B " ) } ) ;
if ( sfdiskCommand . run ( - 1 ) & & sfdiskCommand . exitCode ( ) = = 0 )
return true ;
else
return false ;
}
2019-02-09 18:04:44 +00:00
if ( flag = = PartitionTable : : Flag : : Boot & & state = = false )
2017-11-08 00:01:24 +00:00
setPartitionSystemType ( report , partition ) ;
2019-02-09 18:04:44 +00:00
if ( flag = = PartitionTable : : Flag : : BiosGrub & & state = = true ) {
2017-12-07 20:50:54 +00:00
ExternalCommand sfdiskCommand ( report , QStringLiteral ( " sfdisk " ) , { QStringLiteral ( " --part-type " ) , m_device - > deviceNode ( ) , QString : : number ( partition . number ( ) ) ,
2017-11-08 00:01:24 +00:00
QStringLiteral ( " 21686148-6449-6E6F-744E-656564454649 " ) } ) ;
if ( sfdiskCommand . run ( - 1 ) & & sfdiskCommand . exitCode ( ) = = 0 )
return true ;
else
return false ;
}
2019-02-09 18:04:44 +00:00
if ( flag = = PartitionTable : : Flag : : BiosGrub & & state = = false )
2017-11-08 00:01:24 +00:00
setPartitionSystemType ( report , partition ) ;
2017-09-17 15:33:25 +01:00
return true ;
}