Store the generated partitition UUID after creation

The GPT UUID are generated by the backends automatically once a
partition is created.

This reads the UUID that was generated after the creation of a partition
and stores it back to the object Partition.
This commit is contained in:
Gaël PORTAY 2020-05-15 11:22:57 -04:00
parent 1dde035705
commit e12f0ec391
6 changed files with 39 additions and 0 deletions

View File

@ -103,6 +103,16 @@ public:
*/
virtual bool updateGeometry(Report& report, const Partition& partition, qint64 sector_start, qint64 sector_end) = 0;
/**
* Get 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 get the UUID for
* @return the partition UUID
*/
virtual QString getPartitionUUID(Report& report, const Partition& partition) = 0;
/**
* Set the label of a partition in the partition table (GPT only).
* The label is set in the GPT partition name entry. The partition name is known as PARTLABEL by

View File

@ -64,6 +64,10 @@ bool CreatePartitionJob::run(Report& parent)
partition().setPartitionPath(partitionPath);
partition().setState(Partition::State::None);
backendPartitionTable->commit();
// 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()));
} else
report->line() << xi18nc("@info/plain", "Failed to add partition <filename>%1</filename> to device <filename>%2</filename>.", partition().deviceNode(), device().deviceNode());
} else

View File

@ -117,6 +117,14 @@ bool DummyPartitionTable::setPartitionLabel(Report& report, const Partition& par
return true;
}
QString DummyPartitionTable::getPartitionUUID(Report& report, const Partition& partition)
{
Q_UNUSED(report)
Q_UNUSED(partition)
return QString();
}
bool DummyPartitionTable::setPartitionUUID(Report& report, const Partition& partition, const QString& uuid)
{
Q_UNUSED(report)

View File

@ -47,6 +47,7 @@ public:
bool resizeFileSystem(Report& report, const Partition& partition, qint64 newLength) override;
FileSystem::Type detectFileSystemBySector(Report& report, const Device& device, qint64 sector) override;
bool setPartitionLabel(Report& report, const Partition& partition, const QString& label) override;
QString getPartitionUUID(Report& report, const Partition& partition) override;
bool setPartitionUUID(Report& report, const Partition& partition, const QString& uuid) override;
bool setPartitionSystemType(Report& report, const Partition& partition) override;
bool setFlag(Report& report, const Partition& partition, PartitionTable::Flag partitionManagerFlag, bool state) override;

View File

@ -222,6 +222,21 @@ bool SfdiskPartitionTable::setPartitionLabel(Report& report, const Partition& pa
return sfdiskCommand.run(-1) && sfdiskCommand.exitCode() == 0;
}
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();
}
bool SfdiskPartitionTable::setPartitionUUID(Report& report, const Partition& partition, const QString& uuid)
{
if (uuid.isEmpty())

View File

@ -47,6 +47,7 @@ public:
bool resizeFileSystem(Report& report, const Partition& partition, qint64 newLength) override;
FileSystem::Type detectFileSystemBySector(Report& report, const Device& device, qint64 sector) override;
bool setPartitionLabel(Report& report, const Partition& partition, const QString& label) override;
QString getPartitionUUID(Report& report, const Partition& partition) override;
bool setPartitionUUID(Report& report, const Partition& partition, const QString& uuid) override;
bool setPartitionSystemType(Report& report, const Partition& partition) override;
bool setFlag(Report& report, const Partition& partition, PartitionTable::Flag flag, bool state) override;