2020-09-20 20:33:16 +01:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
|
|
|
|
SPDX-FileCopyrightText: 2008 Laurent Montel <montel@kde.org>
|
2020-10-03 12:41:18 +01:00
|
|
|
SPDX-FileCopyrightText: 2013-2020 Andrius Štikonas <andrius@stikonas.eu>
|
2020-09-20 20:33:16 +01:00
|
|
|
SPDX-FileCopyrightText: 2015 Chris Campbell <c.j.campbell@ed.ac.uk>
|
|
|
|
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
|
|
|
|
2018-04-09 12:32:46 +01:00
|
|
|
#ifndef KPMCORE_PARTITION_H
|
2017-09-10 20:12:52 +01:00
|
|
|
#define KPMCORE_PARTITION_H
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-05-06 22:36:24 +01:00
|
|
|
#include "core/partitionnode.h"
|
|
|
|
#include "core/partitionrole.h"
|
|
|
|
#include "core/partitiontable.h"
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-05-06 22:36:24 +01:00
|
|
|
#include "util/libpartitionmanagerexport.h"
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-04-18 17:14:31 +01:00
|
|
|
#include <QtGlobal>
|
2016-04-27 15:52:37 +01:00
|
|
|
#include <QPointer>
|
2015-06-04 01:29:22 +01:00
|
|
|
|
|
|
|
class Device;
|
|
|
|
class OperationStack;
|
|
|
|
class CoreBackendPartitionTable;
|
|
|
|
class PartitionAlignment;
|
|
|
|
|
|
|
|
class PartResizerWidget;
|
|
|
|
class ResizeDialog;
|
|
|
|
class InsertDialog;
|
|
|
|
class NewDialog;
|
|
|
|
class EditMountPointDialog;
|
|
|
|
class PartPropsDialog;
|
|
|
|
class SizeDialogBase;
|
|
|
|
|
|
|
|
class CreateFileSystemOperation;
|
|
|
|
class RestoreOperation;
|
|
|
|
class SetPartFlagsOperation;
|
|
|
|
class CopyOperation;
|
|
|
|
class NewOperation;
|
|
|
|
class ResizeOperation;
|
|
|
|
|
|
|
|
class SetPartGeometryJob;
|
|
|
|
class CreatePartitionJob;
|
|
|
|
class SetPartFlagsJob;
|
|
|
|
class RestoreFileSystemJob;
|
|
|
|
|
|
|
|
class FileSystem;
|
|
|
|
|
|
|
|
class Report;
|
|
|
|
|
|
|
|
class QString;
|
|
|
|
class QTextStream;
|
|
|
|
|
|
|
|
/** A partition or some unallocated space on a Device.
|
|
|
|
|
2017-10-09 17:37:16 +01:00
|
|
|
Represent partitions in a PartitionTable on a Device. Partitions can be unallocated, thus not all
|
2015-07-13 15:16:36 +01:00
|
|
|
instances really are partitions in the way the user would see them.
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
Extended partitions have child objects that represent the logicals inside them.
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
@see PartitionTable, Device, FileSystem
|
|
|
|
@author Volker Lanz <vl@fidra.de>
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
class LIBKPMCORE_EXPORT Partition : public PartitionNode
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
/** A Partition state -- where did it come from? */
|
|
|
|
enum State {
|
2018-04-09 03:46:05 +01:00
|
|
|
None, /**< exists on disk */
|
|
|
|
New, /**< from a NewOperation */
|
|
|
|
Copy, /**< from a CopyOperation */
|
|
|
|
Restore, /**< from a RestoreOperation */
|
2018-09-01 18:52:15 +01:00
|
|
|
StateNone [[deprecated("Use Partition::State::None")]] = None,
|
|
|
|
StateNew [[deprecated("Use Partition::State::New")]] = New,
|
|
|
|
StateCopy [[deprecated("Use Partition::State::Copy")]] = Copy,
|
|
|
|
StateRestore [[deprecated("Use Partition::State::Restore")]] = Restore
|
2015-07-13 15:16:36 +01:00
|
|
|
};
|
|
|
|
|
2019-02-09 18:04:44 +00:00
|
|
|
Partition(PartitionNode* parent, const Device& device, const PartitionRole& role, FileSystem* fs, qint64 sectorStart, qint64 sectorEnd, QString partitionPath, PartitionTable::Flags availableFlags = PartitionTable::Flag::None, const QString& mountPoint = QString(), bool mounted = false, PartitionTable::Flags activeFlags = PartitionTable::Flag::None, State state = State::None);
|
2017-09-11 00:29:30 +01:00
|
|
|
~Partition() override;
|
2015-07-13 15:16:36 +01:00
|
|
|
|
2016-12-01 18:00:15 +00:00
|
|
|
Partition(const Partition& other, PartitionNode* parent = nullptr);
|
2015-07-13 15:16:36 +01:00
|
|
|
Partition& operator=(const Partition&);
|
|
|
|
|
|
|
|
bool operator==(const Partition& other) const;
|
|
|
|
bool operator!=(const Partition& other) const;
|
|
|
|
|
|
|
|
qint32 number() const {
|
|
|
|
return m_Number; /**< @return the Partition's device number, e.g. 7 for /dev/sdd7 */
|
|
|
|
}
|
|
|
|
|
2016-04-27 21:54:02 +01:00
|
|
|
bool isRoot() const override {
|
2015-07-13 15:16:36 +01:00
|
|
|
return false; /**< @return always false for Partition */
|
|
|
|
}
|
|
|
|
|
2020-10-03 12:41:18 +01:00
|
|
|
const PartitionTable* partitionTable() const;
|
|
|
|
|
2016-04-27 21:54:02 +01:00
|
|
|
PartitionNode* parent() override {
|
2015-07-13 15:16:36 +01:00
|
|
|
return m_Parent; /**< @return the Partition's parent PartitionNode */
|
|
|
|
}
|
2016-04-27 21:54:02 +01:00
|
|
|
const PartitionNode* parent() const override {
|
2015-07-13 15:16:36 +01:00
|
|
|
return m_Parent; /**< @return the Partition's parent PartitionNode */
|
|
|
|
}
|
|
|
|
|
2016-04-27 21:54:02 +01:00
|
|
|
Partitions& children() override {
|
2015-07-13 15:16:36 +01:00
|
|
|
return m_Children; /**< @return the Partition's children. empty for non-extended. */
|
|
|
|
}
|
2016-04-27 21:54:02 +01:00
|
|
|
const Partitions& children() const override {
|
2015-07-13 15:16:36 +01:00
|
|
|
return m_Children; /**< @return the Partition's children. empty for non-extended. */
|
|
|
|
}
|
|
|
|
const QString& devicePath() const {
|
|
|
|
return m_DevicePath; /**< @return the Partition's device path, e.g. /dev/sdd */
|
|
|
|
}
|
|
|
|
const QString& partitionPath() const {
|
|
|
|
return m_PartitionPath; /**< @return the Partition's path, e.g. /dev/sdd1 */
|
|
|
|
}
|
2016-09-24 22:15:15 +01:00
|
|
|
const QString& label() const {
|
|
|
|
return m_Label; /**< @return the GPT Partition label */
|
|
|
|
}
|
2020-03-19 12:15:13 +00:00
|
|
|
const QString& type() const {
|
|
|
|
return m_Type; /**< @return the GPT Partition type */
|
|
|
|
}
|
2016-09-24 22:15:15 +01:00
|
|
|
const QString& uuid() const {
|
|
|
|
return m_UUID; /**< @return the GPT Partition UUID */
|
|
|
|
}
|
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
|
|
|
quint64 attributes() const {
|
|
|
|
return m_Attributes; /**< @return the GPT Partition attributes */
|
|
|
|
}
|
2015-07-13 15:16:36 +01:00
|
|
|
qint64 firstSector() const {
|
|
|
|
return m_FirstSector; /**< @return the Partition's first sector on the Device */
|
|
|
|
}
|
|
|
|
qint64 lastSector() const {
|
|
|
|
return m_LastSector; /**< @return the Partition's last sector on the Device */
|
|
|
|
}
|
2017-10-26 16:20:21 +01:00
|
|
|
qint64 firstByte() const {
|
|
|
|
return firstSector() * sectorSize(); /**< @return the Partition's first byte on the Device */
|
|
|
|
}
|
|
|
|
qint64 lastByte() const {
|
|
|
|
return firstByte() + length() * sectorSize() - 1; /**< @return the Partition's last byte on the Device */
|
|
|
|
}
|
2015-07-13 15:16:36 +01:00
|
|
|
qint64 sectorsUsed() const;
|
2017-09-11 13:41:03 +01:00
|
|
|
qint64 sectorSize() const {
|
2015-07-13 15:16:36 +01:00
|
|
|
return m_SectorSize; /**< @return the sector size on the Partition's Device */
|
|
|
|
}
|
|
|
|
qint64 length() const {
|
|
|
|
return lastSector() - firstSector() + 1; /**< @return the length of the Partition */
|
|
|
|
}
|
|
|
|
qint64 capacity() const {
|
|
|
|
return length() * sectorSize(); /**< @return the capacity of the Partition in bytes */
|
|
|
|
}
|
|
|
|
qint64 used() const {
|
|
|
|
return sectorsUsed() < 0 ? -1 : sectorsUsed() * sectorSize(); /**< @return the number of used sectors in the Partition's FileSystem */
|
|
|
|
}
|
|
|
|
qint64 available() const {
|
|
|
|
return sectorsUsed() < 0 ? -1 : capacity() - used(); /**< @return the number of free sectors in the Partition's FileSystem */
|
|
|
|
}
|
|
|
|
qint64 minimumSectors() const;
|
|
|
|
qint64 maximumSectors() const;
|
|
|
|
qint64 maxFirstSector() const;
|
|
|
|
qint64 minLastSector() const;
|
|
|
|
|
|
|
|
QString deviceNode() const;
|
|
|
|
|
|
|
|
const PartitionRole& roles() const {
|
|
|
|
return m_Roles; /**< @return the Partition's role(s) */
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString& mountPoint() const {
|
|
|
|
return m_MountPoint; /**< @return the Partition's mount point */
|
|
|
|
}
|
|
|
|
|
|
|
|
PartitionTable::Flags activeFlags() const {
|
|
|
|
return m_ActiveFlags; /**< @return the flags currently set for this Partition */
|
|
|
|
}
|
|
|
|
PartitionTable::Flags availableFlags() const {
|
|
|
|
return m_AvailableFlags; /**< @return the flags available for this Partition */
|
|
|
|
}
|
|
|
|
bool isMounted() const {
|
|
|
|
return m_IsMounted; /**< @return true if Partition is mounted */
|
|
|
|
}
|
|
|
|
FileSystem& fileSystem() {
|
|
|
|
return *m_FileSystem; /**< @return the Partition's FileSystem */
|
|
|
|
}
|
|
|
|
const FileSystem& fileSystem() const {
|
|
|
|
return *m_FileSystem; /**< @return the Partition's FileSystem */
|
|
|
|
}
|
|
|
|
State state() const {
|
|
|
|
return m_State; /**< @return the Partition's state */
|
|
|
|
}
|
|
|
|
bool hasChildren() const;
|
|
|
|
|
|
|
|
bool mount(Report& report);
|
|
|
|
bool unmount(Report& report);
|
|
|
|
|
|
|
|
bool canMount() const;
|
|
|
|
bool canUnmount() const;
|
|
|
|
|
2016-08-11 14:26:54 +01:00
|
|
|
void adjustLogicalNumbers(qint32 deletedNumber, qint32 insertedNumber) const;
|
2015-07-13 15:16:36 +01:00
|
|
|
void checkChildrenMounted();
|
|
|
|
|
|
|
|
void setFirstSector(qint64 s) {
|
|
|
|
m_FirstSector = s;
|
|
|
|
}
|
|
|
|
void setLastSector(qint64 s) {
|
|
|
|
m_LastSector = s;
|
|
|
|
}
|
|
|
|
|
2016-09-24 22:15:15 +01:00
|
|
|
void setLabel(const QString& s) {
|
|
|
|
m_Label = s; /**< @param s the new label */
|
|
|
|
}
|
2020-03-19 12:15:13 +00:00
|
|
|
void setType(const QString& s) {
|
|
|
|
m_Type = s; /**< @param s the new type */
|
|
|
|
}
|
2016-09-24 22:15:15 +01:00
|
|
|
void setUUID(const QString& s) {
|
|
|
|
m_UUID = s; /**< @param s the new UUID */
|
|
|
|
}
|
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
|
|
|
void setAttributes(quint64 f) {
|
|
|
|
m_Attributes = f; /**< @param f the new attributes */
|
|
|
|
}
|
2016-09-24 22:15:15 +01:00
|
|
|
|
2016-04-27 21:54:02 +01:00
|
|
|
void append(Partition* p) override {
|
2015-07-13 15:16:36 +01:00
|
|
|
m_Children.append(p);
|
2017-12-21 23:16:12 +00:00
|
|
|
std::sort(m_Children.begin(), m_Children.end(), [] (const Partition *a, const Partition *b) -> bool {return a->firstSector() < b->firstSector();});
|
2015-07-13 15:16:36 +01:00
|
|
|
}
|
|
|
|
void setDevicePath(const QString& s) {
|
|
|
|
m_DevicePath = s;
|
|
|
|
}
|
|
|
|
void setPartitionPath(const QString& s);
|
|
|
|
void setRoles(const PartitionRole& r) {
|
|
|
|
m_Roles = r;
|
|
|
|
}
|
|
|
|
void setMountPoint(const QString& s) {
|
|
|
|
m_MountPoint = s;
|
|
|
|
}
|
|
|
|
void setFlags(PartitionTable::Flags f) {
|
|
|
|
m_ActiveFlags = f;
|
|
|
|
}
|
|
|
|
void setSectorSize(qint32 s) {
|
|
|
|
m_SectorSize = s;
|
|
|
|
}
|
|
|
|
void move(qint64 newStartSector);
|
2016-10-30 02:31:46 +00:00
|
|
|
void setMounted(bool b);
|
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
void setFlag(PartitionTable::Flag f) {
|
2019-02-09 18:04:44 +00:00
|
|
|
m_ActiveFlags = m_ActiveFlags.setFlag(f);
|
2015-07-13 15:16:36 +01:00
|
|
|
}
|
|
|
|
void unsetFlag(PartitionTable::Flag f) {
|
2019-02-09 18:04:44 +00:00
|
|
|
m_ActiveFlags = m_ActiveFlags.setFlag(f, false);
|
2015-07-13 15:16:36 +01:00
|
|
|
}
|
|
|
|
void setParent(PartitionNode* p) {
|
|
|
|
m_Parent = p;
|
|
|
|
}
|
|
|
|
void setFileSystem(FileSystem* fs);
|
|
|
|
void setState(State s) {
|
|
|
|
m_State = s;
|
|
|
|
}
|
|
|
|
void deleteFileSystem();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void setNumber(qint32 n) {
|
|
|
|
m_Number = n;
|
|
|
|
}
|
|
|
|
|
2021-08-28 19:53:19 +01:00
|
|
|
qint32 m_Number = 0;
|
2015-07-13 15:16:36 +01:00
|
|
|
Partitions m_Children;
|
2021-08-28 19:53:19 +01:00
|
|
|
QPointer< PartitionNode > m_Parent = nullptr;
|
|
|
|
FileSystem* m_FileSystem = nullptr;
|
2015-07-13 15:16:36 +01:00
|
|
|
PartitionRole m_Roles;
|
2021-08-28 19:53:19 +01:00
|
|
|
qint64 m_FirstSector = 0;
|
|
|
|
qint64 m_LastSector = 0;
|
2015-07-13 15:16:36 +01:00
|
|
|
QString m_DevicePath;
|
2016-09-24 22:15:15 +01:00
|
|
|
QString m_Label;
|
2020-03-19 12:15:13 +00:00
|
|
|
QString m_Type;
|
2016-09-24 22:15:15 +01:00
|
|
|
QString m_UUID;
|
2021-08-28 19:53:19 +01:00
|
|
|
quint64 m_Attributes = 0;
|
2015-07-13 15:16:36 +01:00
|
|
|
QString m_PartitionPath;
|
|
|
|
QString m_MountPoint;
|
|
|
|
PartitionTable::Flags m_AvailableFlags;
|
|
|
|
PartitionTable::Flags m_ActiveFlags;
|
2021-08-28 19:53:19 +01:00
|
|
|
bool m_IsMounted = false;
|
|
|
|
qint64 m_SectorSize = 0;
|
|
|
|
State m_State = None;
|
2015-06-04 01:29:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
QTextStream& operator<<(QTextStream& stream, const Partition& p);
|
|
|
|
|
|
|
|
#endif
|