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).
This commit is contained in:
parent
df4e2332f4
commit
1dde035705
|
@ -114,6 +114,17 @@ public:
|
||||||
*/
|
*/
|
||||||
virtual bool setPartitionLabel(Report& report, const Partition& partition, const QString& label) = 0;
|
virtual bool setPartitionLabel(Report& report, const Partition& partition, const QString& label) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the UUID of a partition in the partition table (GPT only).
|
||||||
|
* The partition UUID is known as PARTUUID by several utilities. The device-manager links
|
||||||
|
* the device under /dev/disk/by-partuuid/<uuid>.
|
||||||
|
* @param report the report to write information to
|
||||||
|
* @param partition the partition to set the UUID for
|
||||||
|
* @param uuid the new UUID for the partition
|
||||||
|
* @return true on success
|
||||||
|
*/
|
||||||
|
virtual bool setPartitionUUID(Report& report, const Partition& partition, const QString& uuid) = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the system type (e.g. 83 for Linux) of a partition. The type to set is taken from
|
* Set the system type (e.g. 83 for Linux) of a partition. The type to set is taken from
|
||||||
* the partition's file system.
|
* the partition's file system.
|
||||||
|
|
|
@ -7,6 +7,7 @@ set(JOBS_SRC
|
||||||
jobs/createpartitionjob.cpp
|
jobs/createpartitionjob.cpp
|
||||||
jobs/createpartitiontablejob.cpp
|
jobs/createpartitiontablejob.cpp
|
||||||
jobs/setpartitionlabeljob.cpp
|
jobs/setpartitionlabeljob.cpp
|
||||||
|
jobs/setpartitionuuidjob.cpp
|
||||||
jobs/createvolumegroupjob.cpp
|
jobs/createvolumegroupjob.cpp
|
||||||
jobs/removevolumegroupjob.cpp
|
jobs/removevolumegroupjob.cpp
|
||||||
jobs/deactivatevolumegroupjob.cpp
|
jobs/deactivatevolumegroupjob.cpp
|
||||||
|
|
|
@ -0,0 +1,86 @@
|
||||||
|
/*************************************************************************
|
||||||
|
* Copyright (C) 2020 by Gaël PORTAY <gael.portay@collabora.com> *
|
||||||
|
* *
|
||||||
|
* This program is free software; you can redistribute it and/or *
|
||||||
|
* modify it under the terms of the GNU General Public License as *
|
||||||
|
* published by the Free Software Foundation; either version 3 of *
|
||||||
|
* the License, or (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* This program is distributed in the hope that it will be useful, *
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||||
|
* GNU General Public License for more details. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU General Public License *
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
|
||||||
|
*************************************************************************/
|
||||||
|
|
||||||
|
#include "jobs/setpartitionuuidjob.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 SetPartitionUUIDJob (GPT only)
|
||||||
|
@param d the Device the Partition to be created will be on
|
||||||
|
@param p the Partition whose UUID is to be set is on
|
||||||
|
@param newUUID the new UUID
|
||||||
|
*/
|
||||||
|
SetPartitionUUIDJob::SetPartitionUUIDJob(Device& d, Partition& p, const QString& newUUID) :
|
||||||
|
Job(),
|
||||||
|
m_Device(d),
|
||||||
|
m_Partition(p),
|
||||||
|
m_UUID(newUUID)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SetPartitionUUIDJob::run(Report& parent)
|
||||||
|
{
|
||||||
|
Q_ASSERT(partition().devicePath() == device().deviceNode());
|
||||||
|
|
||||||
|
bool rval = true;
|
||||||
|
|
||||||
|
Report* report = jobStarted(parent);
|
||||||
|
|
||||||
|
// The UUID is 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 UUIDs. 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->setPartitionUUID(*report, partition(), m_UUID)) {
|
||||||
|
rval = true;
|
||||||
|
partition().setUUID(m_UUID);
|
||||||
|
backendPartitionTable->commit();
|
||||||
|
} else
|
||||||
|
report->line() << xi18nc("@info:progress", "Failed to set the UUID 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 UUID 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 UUID for partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
jobFinished(*report, rval);
|
||||||
|
|
||||||
|
return rval;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString SetPartitionUUIDJob::description() const
|
||||||
|
{
|
||||||
|
return xi18nc("@info:progress", "Set the UUID on partition <filename>%1</filename> to \"%2\"", partition().deviceNode(), uuid());
|
||||||
|
}
|
|
@ -0,0 +1,70 @@
|
||||||
|
/*************************************************************************
|
||||||
|
* Copyright (C) 2020 by Gaël PORTAY <gael.portay@collabora.com> *
|
||||||
|
* *
|
||||||
|
* This program is free software; you can redistribute it and/or *
|
||||||
|
* modify it under the terms of the GNU General Public License as *
|
||||||
|
* published by the Free Software Foundation; either version 3 of *
|
||||||
|
* the License, or (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* This program is distributed in the hope that it will be useful, *
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||||
|
* GNU General Public License for more details. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU General Public License *
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
|
||||||
|
*************************************************************************/
|
||||||
|
|
||||||
|
#if !defined(KPMCORE_SETPARTITIONUUIDJOB_H)
|
||||||
|
|
||||||
|
#define KPMCORE_SETPARTITIONUUIDJOB_H
|
||||||
|
|
||||||
|
#include "jobs/job.h"
|
||||||
|
|
||||||
|
class Partition;
|
||||||
|
class Device;
|
||||||
|
class Report;
|
||||||
|
|
||||||
|
class QString;
|
||||||
|
|
||||||
|
/** Set a Partition UUID (GPT only).
|
||||||
|
@author Gaël PORTAY <gael.portay@collabora.com>
|
||||||
|
*/
|
||||||
|
class SetPartitionUUIDJob : public Job
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SetPartitionUUIDJob(Device& d, Partition& p, const QString& newUUID);
|
||||||
|
|
||||||
|
public:
|
||||||
|
bool run(Report& parent) override;
|
||||||
|
QString description() const override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Partition& partition() {
|
||||||
|
return m_Partition;
|
||||||
|
}
|
||||||
|
const Partition& partition() const {
|
||||||
|
return m_Partition;
|
||||||
|
}
|
||||||
|
|
||||||
|
Device& device() {
|
||||||
|
return m_Device;
|
||||||
|
}
|
||||||
|
const Device& device() const {
|
||||||
|
return m_Device;
|
||||||
|
}
|
||||||
|
|
||||||
|
const QString& uuid() const {
|
||||||
|
return m_UUID;
|
||||||
|
}
|
||||||
|
void setUUID(const QString& u) {
|
||||||
|
m_UUID = u;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Device& m_Device;
|
||||||
|
Partition& m_Partition;
|
||||||
|
QString m_UUID;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -25,6 +25,7 @@
|
||||||
#include "jobs/createpartitionjob.h"
|
#include "jobs/createpartitionjob.h"
|
||||||
#include "jobs/createfilesystemjob.h"
|
#include "jobs/createfilesystemjob.h"
|
||||||
#include "jobs/setpartitionlabeljob.h"
|
#include "jobs/setpartitionlabeljob.h"
|
||||||
|
#include "jobs/setpartitionuuidjob.h"
|
||||||
#include "jobs/setfilesystemlabeljob.h"
|
#include "jobs/setfilesystemlabeljob.h"
|
||||||
#include "jobs/setpartflagsjob.h"
|
#include "jobs/setpartflagsjob.h"
|
||||||
#include "jobs/checkfilesystemjob.h"
|
#include "jobs/checkfilesystemjob.h"
|
||||||
|
@ -48,6 +49,7 @@ NewOperation::NewOperation(Device& d, Partition* p) :
|
||||||
m_NewPartition(p),
|
m_NewPartition(p),
|
||||||
m_CreatePartitionJob(new CreatePartitionJob(targetDevice(), newPartition())),
|
m_CreatePartitionJob(new CreatePartitionJob(targetDevice(), newPartition())),
|
||||||
m_SetPartitionLabelJob(nullptr),
|
m_SetPartitionLabelJob(nullptr),
|
||||||
|
m_SetPartitionUUIDJob(nullptr),
|
||||||
m_CreateFileSystemJob(nullptr),
|
m_CreateFileSystemJob(nullptr),
|
||||||
m_SetPartFlagsJob(nullptr),
|
m_SetPartFlagsJob(nullptr),
|
||||||
m_SetFileSystemLabelJob(nullptr),
|
m_SetFileSystemLabelJob(nullptr),
|
||||||
|
@ -60,6 +62,11 @@ NewOperation::NewOperation(Device& d, Partition* p) :
|
||||||
addJob(setPartitionLabelJob());
|
addJob(setPartitionLabelJob());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!p->uuid().isEmpty()) {
|
||||||
|
m_SetPartitionUUIDJob = new SetPartitionUUIDJob(targetDevice(), newPartition(), p->uuid());
|
||||||
|
addJob(setPartitionUUIDJob());
|
||||||
|
}
|
||||||
|
|
||||||
const FileSystem& fs = newPartition().fileSystem();
|
const FileSystem& fs = newPartition().fileSystem();
|
||||||
|
|
||||||
if (fs.type() != FileSystem::Type::Extended) {
|
if (fs.type() != FileSystem::Type::Extended) {
|
||||||
|
|
|
@ -31,6 +31,7 @@ class OperationStack;
|
||||||
|
|
||||||
class CreatePartitionJob;
|
class CreatePartitionJob;
|
||||||
class SetPartitionLabelJob;
|
class SetPartitionLabelJob;
|
||||||
|
class SetPartitionUUIDJob;
|
||||||
class CreateFileSystemJob;
|
class CreateFileSystemJob;
|
||||||
class SetFileSystemLabelJob;
|
class SetFileSystemLabelJob;
|
||||||
class SetPartFlagsJob;
|
class SetPartFlagsJob;
|
||||||
|
@ -87,6 +88,9 @@ protected:
|
||||||
SetPartitionLabelJob* setPartitionLabelJob() {
|
SetPartitionLabelJob* setPartitionLabelJob() {
|
||||||
return m_SetPartitionLabelJob;
|
return m_SetPartitionLabelJob;
|
||||||
}
|
}
|
||||||
|
SetPartitionUUIDJob* setPartitionUUIDJob() {
|
||||||
|
return m_SetPartitionUUIDJob;
|
||||||
|
}
|
||||||
CreateFileSystemJob* createFileSystemJob() {
|
CreateFileSystemJob* createFileSystemJob() {
|
||||||
return m_CreateFileSystemJob;
|
return m_CreateFileSystemJob;
|
||||||
}
|
}
|
||||||
|
@ -105,6 +109,7 @@ private:
|
||||||
Partition* m_NewPartition;
|
Partition* m_NewPartition;
|
||||||
CreatePartitionJob* m_CreatePartitionJob;
|
CreatePartitionJob* m_CreatePartitionJob;
|
||||||
SetPartitionLabelJob* m_SetPartitionLabelJob;
|
SetPartitionLabelJob* m_SetPartitionLabelJob;
|
||||||
|
SetPartitionUUIDJob* m_SetPartitionUUIDJob;
|
||||||
CreateFileSystemJob* m_CreateFileSystemJob;
|
CreateFileSystemJob* m_CreateFileSystemJob;
|
||||||
SetPartFlagsJob* m_SetPartFlagsJob;
|
SetPartFlagsJob* m_SetPartFlagsJob;
|
||||||
SetFileSystemLabelJob* m_SetFileSystemLabelJob;
|
SetFileSystemLabelJob* m_SetFileSystemLabelJob;
|
||||||
|
|
|
@ -117,6 +117,15 @@ bool DummyPartitionTable::setPartitionLabel(Report& report, const Partition& par
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool DummyPartitionTable::setPartitionUUID(Report& report, const Partition& partition, const QString& uuid)
|
||||||
|
{
|
||||||
|
Q_UNUSED(report)
|
||||||
|
Q_UNUSED(partition)
|
||||||
|
Q_UNUSED(uuid)
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool DummyPartitionTable::setFlag(Report& report, const Partition& partition, PartitionTable::Flag partitionManagerFlag, bool state)
|
bool DummyPartitionTable::setFlag(Report& report, const Partition& partition, PartitionTable::Flag partitionManagerFlag, bool state)
|
||||||
{
|
{
|
||||||
Q_UNUSED(report)
|
Q_UNUSED(report)
|
||||||
|
|
|
@ -47,6 +47,7 @@ public:
|
||||||
bool resizeFileSystem(Report& report, const Partition& partition, qint64 newLength) override;
|
bool resizeFileSystem(Report& report, const Partition& partition, qint64 newLength) override;
|
||||||
FileSystem::Type detectFileSystemBySector(Report& report, const Device& device, qint64 sector) override;
|
FileSystem::Type detectFileSystemBySector(Report& report, const Device& device, qint64 sector) override;
|
||||||
bool setPartitionLabel(Report& report, const Partition& partition, const QString& label) override;
|
bool setPartitionLabel(Report& report, const Partition& partition, const QString& label) override;
|
||||||
|
bool setPartitionUUID(Report& report, const Partition& partition, const QString& uuid) override;
|
||||||
bool setPartitionSystemType(Report& report, const Partition& partition) override;
|
bool setPartitionSystemType(Report& report, const Partition& partition) override;
|
||||||
bool setFlag(Report& report, const Partition& partition, PartitionTable::Flag partitionManagerFlag, bool state) override;
|
bool setFlag(Report& report, const Partition& partition, PartitionTable::Flag partitionManagerFlag, bool state) override;
|
||||||
};
|
};
|
||||||
|
|
|
@ -222,6 +222,15 @@ bool SfdiskPartitionTable::setPartitionLabel(Report& report, const Partition& pa
|
||||||
return sfdiskCommand.run(-1) && sfdiskCommand.exitCode() == 0;
|
return sfdiskCommand.run(-1) && sfdiskCommand.exitCode() == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
bool SfdiskPartitionTable::setPartitionSystemType(Report& report, const Partition& partition)
|
bool SfdiskPartitionTable::setPartitionSystemType(Report& report, const Partition& partition)
|
||||||
{
|
{
|
||||||
QString partitionType = partition.type();
|
QString partitionType = partition.type();
|
||||||
|
|
|
@ -47,6 +47,7 @@ public:
|
||||||
bool resizeFileSystem(Report& report, const Partition& partition, qint64 newLength) override;
|
bool resizeFileSystem(Report& report, const Partition& partition, qint64 newLength) override;
|
||||||
FileSystem::Type detectFileSystemBySector(Report& report, const Device& device, qint64 sector) override;
|
FileSystem::Type detectFileSystemBySector(Report& report, const Device& device, qint64 sector) override;
|
||||||
bool setPartitionLabel(Report& report, const Partition& partition, const QString& label) override;
|
bool setPartitionLabel(Report& report, const Partition& partition, const QString& label) override;
|
||||||
|
bool setPartitionUUID(Report& report, const Partition& partition, const QString& uuid) override;
|
||||||
bool setPartitionSystemType(Report& report, const Partition& partition) override;
|
bool setPartitionSystemType(Report& report, const Partition& partition) override;
|
||||||
bool setFlag(Report& report, const Partition& partition, PartitionTable::Flag flag, bool state) override;
|
bool setFlag(Report& report, const Partition& partition, PartitionTable::Flag flag, bool state) override;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue