libstorage-ng
Partition.h
1 /*
2  * Copyright (c) [2014-2015] Novell, Inc.
3  * Copyright (c) [2016-2026] SUSE LLC
4  *
5  * All Rights Reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of version 2 of the GNU General Public License as published
9  * by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, contact Novell, Inc.
18  *
19  * To contact Novell about this file by physical or electronic mail, you may
20  * find current contact information at www.novell.com.
21  */
22 
23 
24 #ifndef STORAGE_PARTITION_H
25 #define STORAGE_PARTITION_H
26 
27 
28 #include "storage/Devices/BlkDevice.h"
29 
30 
31 namespace storage
32 {
33 
34  class PartitionTable;
35  class Partitionable;
36  class SystemInfo;
37  class Arch;
38 
39 
43  enum class PartitionType {
44 
51  PRIMARY,
52 
56  EXTENDED,
57 
61  LOGICAL
62 
63  };
64 
65 
71  std::string get_partition_type_name(PartitionType partition_type);
72 
73 
88  enum IdNum : unsigned int {
89 
91  ID_DOS12 = 0x01,
92 
94  ID_DOS16 = 0x06,
95 
97  ID_NTFS = 0x07,
98 
100  ID_DOS32 = 0x0c,
101 
103  ID_EXTENDED = 0x0f,
104 
106  ID_DIAG = 0x12,
107 
109  ID_PREP = 0x41,
110 
112  ID_SWAP = 0x82,
113 
116  ID_LINUX = 0x83,
117 
119  ID_IRST = 0x84,
120 
122  ID_LVM = 0x8e,
123 
125  ID_RAID = 0xfd,
126 
129  ID_XBOOTLDR = 0xea,
130 
132  ID_ESP = 0xef,
133 
135  ID_UNKNOWN = 0x100,
136 
140 
143 
146 
149 
152 
155 
158 
161 
164 
167 
170 
173 
176 
179 
182 
185 
188 
191 
194 
197 
200 
203 
206 
209 
212 
215 
218 
219  };
220 
221 
229  std::string get_partition_id_name(IdNum partition_id);
230 
231 
240 
241 
246  {
247  ROOT, USR
248  };
249 
250 
256  bool is_linux_partition_id(IdNum id, LinuxPartitionIdCategory linux_partition_id_category);
257 
258 
267  IdNum get_linux_partition_id(LinuxPartitionIdCategory linux_partition_id_category, const Arch& arch);
268 
269 
278  IdNum get_linux_partition_id(LinuxPartitionIdCategory linux_partition_id_category, SystemInfo& system_info);
279 
280 
284  class Partition : public BlkDevice
285  {
286  public:
287 
295  static Partition* create(Devicegraph* devicegraph, const std::string& name,
296  const Region& region, PartitionType type);
297 
298  static Partition* load(Devicegraph* devicegraph, const xmlNode* node);
299 
303  unsigned int get_number() const;
304 
311 
320 
326  unsigned int get_id() const;
327 
335  void set_id(unsigned int id);
336 
340  bool is_boot() const;
341 
360  void set_boot(bool boot);
361 
365  bool is_legacy_boot() const;
366 
374  void set_legacy_boot(bool legacy_boot);
375 
379  bool is_no_automount() const;
380 
392  void set_no_automount(bool no_automount);
393 
400  const std::string& get_label() const;
401 
407  void set_label(const std::string& label);
408 
412  const std::string& get_uuid() const;
413 
420 
425 
432 
437 
445 
451  static Partition* find_by_name(Devicegraph* devicegraph, const std::string& name);
452 
456  static const Partition* find_by_name(const Devicegraph* devicegraph, const std::string& name);
457 
463  static bool compare_by_number(const Partition* lhs, const Partition* rhs);
464 
465  public:
466 
467  class Impl;
468 
469  Impl& get_impl();
470  const Impl& get_impl() const;
471 
472  virtual Partition* clone() const override;
473  virtual std::unique_ptr<Device> clone_v2() const override;
474 
475  Partition(Impl* impl);
476  ST_NO_SWIG Partition(std::unique_ptr<Device::Impl>&& impl);
477 
478  };
479 
480 
486  bool is_partition(const Device* device);
487 
495 
499  const Partition* to_partition(const Device* device);
500 
501 }
502 
503 #endif
Definition: Arch.h:36
An abstract Block Device.
Definition: BlkDevice.h:50
An abstract base class for storage devices.
Definition: Device.h:82
The main container of the libstorage-ng.
Definition: Devicegraph.h:170
Definition: PartitionTable.h:88
A partition of a Partitionable, e.g.
Definition: Partition.h:285
PartitionTable * get_partition_table()
Return the partition table the partition belongs to.
const std::string & get_uuid() const
Get the partition UUID.
void set_legacy_boot(bool legacy_boot)
Set the legacy boot flag of the partition.
void set_id(unsigned int id)
Set the partition id.
unsigned int get_number() const
Get the partition number.
void set_boot(bool boot)
Set the boot flag of the partition.
PartitionType get_type() const
Get the partition type.
void set_no_automount(bool no_automount)
Set the no-automount flag of the partition.
void set_type(PartitionType type)
Set the partition type.
bool is_legacy_boot() const
Query the legacy boot flag of the partition.
static Partition * create(Devicegraph *devicegraph, const std::string &name, const Region &region, PartitionType type)
Create a device of type Partition.
unsigned int get_id() const
Get the partition id.
bool is_no_automount() const
Query the no-automount flag of the partition.
static Partition * find_by_name(Devicegraph *devicegraph, const std::string &name)
Find a Partition by its name.
const Partitionable * get_partitionable() const
Return the partitionable the partition belongs to.
Partitionable * get_partitionable()
Return the partitionable the partition belongs to.
const std::string & get_label() const
Get the partition label.
Region get_unused_surrounding_region() const
Returns the unused region surrounding the partition (including the partition itself).
static const Partition * find_by_name(const Devicegraph *devicegraph, const std::string &name)
Find a Partition by its name.
bool is_boot() const
Query the boot flag of the partition.
void set_label(const std::string &label)
Set the partition label.
const PartitionTable * get_partition_table() const
Return the partition table the partition belongs to.
static bool compare_by_number(const Partition *lhs, const Partition *rhs)
Compare (less than) two Partitions by number.
Definition: Partitionable.h:40
A start/length pair with a block size.
Definition: Region.h:85
The SystemInfo class keeps various system information.
Definition: SystemInfo.h:49
The storage namespace.
Definition: Actiongraph.h:40
PartitionType
Enum with partition types.
Definition: Partition.h:43
@ EXTENDED
Extended partition.
@ LOGICAL
Logical partition.
@ PRIMARY
Primary partition.
LinuxPartitionIdCategory
Enum with categories for Linux partitions.
Definition: Partition.h:246
IdNum
Enum with values used as partition ids.
Definition: Partition.h:88
@ ID_LINUX_ROOT_PPC64LE
Linux Root Partition (ppc64le), only for GPT.
Definition: Partition.h:166
@ ID_SWAP
Swap partition, for MS-DOS, GPT and DASD.
Definition: Partition.h:112
@ ID_LINUX_USR_ARM
Linux USR Partition (arm), only for GPT.
Definition: Partition.h:187
@ ID_LINUX
For MS-DOS, GPT, DASD and implicit.
Definition: Partition.h:116
@ ID_UNKNOWN
Only set during probing.
Definition: Partition.h:135
@ ID_LINUX_USR_PPC32
Linux USR Partition (ppc), only for GPT.
Definition: Partition.h:193
@ ID_BIOS_BOOT
BIOS boot partition (https://en.wikipedia.org/wiki/BIOS_boot_partition), only for GPT.
Definition: Partition.h:139
@ ID_LINUX_USR_S390X
Linux USR Partition (s390x), only for GPT.
Definition: Partition.h:211
@ ID_PREP
PPC PReP Boot partition, for MS-DOS and GPT.
Definition: Partition.h:109
@ ID_LINUX_USR_PPC64LE
Linux USR Partition (ppc64le), only for GPT.
Definition: Partition.h:199
@ ID_LINUX_ROOT_X86_64
Linux Root Partition (x86_64), only for GPT.
Definition: Partition.h:184
@ ID_LINUX_ROOT_AARCH64
Linux Root Partition (aarch64), only for GPT.
Definition: Partition.h:157
@ ID_LINUX_ROOT_S390X
Linux Root Partition (s390x), only for GPT.
Definition: Partition.h:178
@ ID_LINUX_ROOT_PPC32
Linux Root Partition (ppc), only for GPT.
Definition: Partition.h:160
@ ID_LINUX_HOME
Linux Home, only for GPT.
Definition: Partition.h:148
@ ID_RAID
RAID partition, for MS-DOS, GPT and DASD.
Definition: Partition.h:125
@ ID_LINUX_SERVER_DATA
Linux Server Data, only for GPT.
Definition: Partition.h:151
@ ID_LINUX_ROOT_PPC64BE
Linux Root Partition (ppc64be), only for GPT.
Definition: Partition.h:163
@ ID_LINUX_ROOT_ARM
Linux Root Partition (arm), only for GPT.
Definition: Partition.h:154
@ ID_LINUX_USR_X86
Linux USR Partition (x86), only for GPT.
Definition: Partition.h:214
@ ID_WINDOWS_BASIC_DATA
Windows basic data partition, only for GPT.
Definition: Partition.h:142
@ ID_DOS32
Only for MS-DOS.
Definition: Partition.h:100
@ ID_ESP
EFI System Partition, for MS-DOS and GPT.
Definition: Partition.h:132
@ ID_DIAG
For MS-DOS and GPT.
Definition: Partition.h:106
@ ID_DOS16
Only for MS-DOS.
Definition: Partition.h:94
@ ID_LINUX_USR_X86_64
Linux USR Partition (x86_64), only for GPT.
Definition: Partition.h:217
@ ID_EXTENDED
Only for MS-DOS.
Definition: Partition.h:103
@ ID_LINUX_USR_S390
Linux USR Partition (s390), only for GPT.
Definition: Partition.h:208
@ ID_NTFS
Only for MS-DOS.
Definition: Partition.h:97
@ ID_MICROSOFT_RESERVED
Microsoft reserved partition, only for GPT.
Definition: Partition.h:145
@ ID_LINUX_USR_AARCH64
Linux USR Partition (aarch64), only for GPT.
Definition: Partition.h:190
@ ID_LINUX_ROOT_S390
Linux Root Partition (s390), only for GPT.
Definition: Partition.h:175
@ ID_LINUX_USR_RISCV32
Linux USR Partition (riscv32), only for GPT.
Definition: Partition.h:202
@ ID_LINUX_ROOT_X86
Linux Root Partition (x86), only for GPT.
Definition: Partition.h:181
@ ID_LINUX_USR_PPC64BE
Linux USR Partition (ppc64be), only for GPT.
Definition: Partition.h:196
@ ID_LINUX_ROOT_RISCV32
Linux Root Partition (riscv32, only for GPT.
Definition: Partition.h:169
@ ID_LINUX_ROOT_RISCV64
Linux Root Partition (riscv64), only for GPT.
Definition: Partition.h:172
@ ID_LVM
LVM partition, for MS-DOS, GPT and DASD.
Definition: Partition.h:122
@ ID_DOS12
Only for MS-DOS.
Definition: Partition.h:91
@ ID_LINUX_USR_RISCV64
Linux USR Partition (riscv64), only for GPT.
Definition: Partition.h:205
@ ID_XBOOTLDR
Boot Loader Specification (https://uapi-group.org/specifications/specs/boot_loader_specification/),...
Definition: Partition.h:129
@ ID_IRST
Intel Rapid Start Technology, for MS-DOS and GPT.
Definition: Partition.h:119
bool is_linux_partition_id(IdNum id)
Check whether the id is a Linux partition id.
bool is_partition(const Device *device)
Checks whether device points to a Partition.
std::string get_partition_id_name(IdNum partition_id)
Convert the IdNum partition_id to a string.
std::string get_partition_type_name(PartitionType partition_type)
Convert the PartitionType partition_type to a string.
IdNum get_linux_partition_id(LinuxPartitionIdCategory linux_partition_id_category, const Arch &arch)
Get the partition id for the Linux partition of the given category (root, usr, ......
Partition * to_partition(Device *device)
Converts pointer to Device to pointer to Partition.