libstorage-ng
Partition.h
1 /*
2  * Copyright (c) [2014-2015] Novell, Inc.
3  * Copyright (c) [2016-2024] 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 
128  ID_ESP = 0xef,
129 
131  ID_UNKNOWN = 0x100,
132 
136 
139 
142 
145 
148 
151 
154 
157 
160 
163 
166 
169 
172 
175 
178 
181 
184 
187 
190 
193 
196 
199 
202 
205 
208 
211 
214 
215  };
216 
217 
225  std::string get_partition_id_name(IdNum partition_id);
226 
227 
235  bool is_linux_partition_id(IdNum id);
236 
237 
242  {
243  ROOT, USR
244  };
245 
246 
252  bool is_linux_partition_id(IdNum id, LinuxPartitionIdCategory linux_partition_id_category);
253 
254 
263  IdNum get_linux_partition_id(LinuxPartitionIdCategory linux_partition_id_category, const Arch& arch);
264 
265 
274  IdNum get_linux_partition_id(LinuxPartitionIdCategory linux_partition_id_category, SystemInfo& system_info);
275 
276 
280  class Partition : public BlkDevice
281  {
282  public:
283 
291  static Partition* create(Devicegraph* devicegraph, const std::string& name,
292  const Region& region, PartitionType type);
293 
294  static Partition* load(Devicegraph* devicegraph, const xmlNode* node);
295 
299  unsigned int get_number() const;
300 
306  PartitionType get_type() const;
307 
315  void set_type(PartitionType type);
316 
322  unsigned int get_id() const;
323 
331  void set_id(unsigned int id);
332 
336  bool is_boot() const;
337 
356  void set_boot(bool boot);
357 
361  bool is_legacy_boot() const;
362 
370  void set_legacy_boot(bool legacy_boot);
371 
375  bool is_no_automount() const;
376 
388  void set_no_automount(bool no_automount);
389 
396  const std::string& get_label() const;
397 
403  void set_label(const std::string& label);
404 
408  const std::string& get_uuid() const;
409 
416 
420  const PartitionTable* get_partition_table() const;
421 
428 
432  const Partitionable* get_partitionable() const;
433 
441 
447  static Partition* find_by_name(Devicegraph* devicegraph, const std::string& name);
448 
452  static const Partition* find_by_name(const Devicegraph* devicegraph, const std::string& name);
453 
459  static bool compare_by_number(const Partition* lhs, const Partition* rhs);
460 
461  public:
462 
463  class Impl;
464 
465  Impl& get_impl();
466  const Impl& get_impl() const;
467 
468  virtual Partition* clone() const override;
469 
470  Partition(Impl* impl);
471 
472  };
473 
474 
480  bool is_partition(const Device* device);
481 
488  Partition* to_partition(Device* device);
489 
493  const Partition* to_partition(const Device* device);
494 
495 }
496 
497 #endif
bool is_linux_partition_id(IdNum id)
Check whether the id is a Linux partition id.
Only for MS-DOS.
Definition: Partition.h:97
Partition * to_partition(Device *device)
Converts pointer to Device to pointer to Partition.
BIOS boot partition (https://en.wikipedia.org/wiki/BIOS_boot_partition), only for GPT...
Definition: Partition.h:135
Only set during probing.
Definition: Partition.h:131
Linux USR Partition (x86), only for GPT.
Definition: Partition.h:210
Linux Root Partition (x86), only for GPT.
Definition: Partition.h:177
Linux Root Partition (s390x), only for GPT.
Definition: Partition.h:174
For MS-DOS, GPT, DASD and implicit.
Definition: Partition.h:116
void set_type(PartitionType type)
Set the partition type.
std::string get_partition_id_name(IdNum partition_id)
Convert the IdNum partition_id to a string.
For MS-DOS and GPT.
Definition: Partition.h:106
Linux USR Partition (s390), only for GPT.
Definition: Partition.h:204
unsigned int get_id() const
Get the partition id.
void set_legacy_boot(bool legacy_boot)
Set the legacy boot flag of the partition.
PartitionTable * get_partition_table()
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.
void set_no_automount(bool no_automount)
Set the no-automount flag of the partition.
Linux Root Partition (aarch64), only for GPT.
Definition: Partition.h:153
Linux Server Data, only for GPT.
Definition: Partition.h:147
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, ...) depending on the architecture.
Linux USR Partition (ppc), only for GPT.
Definition: Partition.h:189
Only for MS-DOS.
Definition: Partition.h:94
PartitionType
Enum with partition types.
Definition: Partition.h:43
Only for MS-DOS.
Definition: Partition.h:91
void set_label(const std::string &label)
Set the partition label.
LVM partition, for MS-DOS, GPT and DASD.
Definition: Partition.h:122
Linux Root Partition (ppc64be), only for GPT.
Definition: Partition.h:159
LinuxPartitionIdCategory
Enum with categories for Linux partitions.
Definition: Partition.h:241
Linux USR Partition (riscv32), only for GPT.
Definition: Partition.h:198
Linux USR Partition (x86_64), only for GPT.
Definition: Partition.h:213
Linux Root Partition (arm), only for GPT.
Definition: Partition.h:150
std::string get_partition_type_name(PartitionType partition_type)
Convert the PartitionType partition_type to a string.
A start/length pair with a block size.
Definition: Region.h:84
Linux Root Partition (x86_64), only for GPT.
Definition: Partition.h:180
Region get_unused_surrounding_region() const
Returns the unused region surrounding the partition (including the partition itself).
PPC PReP Boot partition, for MS-DOS and GPT.
Definition: Partition.h:109
Linux USR Partition (s390x), only for GPT.
Definition: Partition.h:207
Windows basic data partition, only for GPT.
Definition: Partition.h:138
Linux USR Partition (ppc64le), only for GPT.
Definition: Partition.h:195
bool is_partition(const Device *device)
Checks whether device points to a Partition.
Intel Rapid Start Technology, for MS-DOS and GPT.
Definition: Partition.h:119
EFI System Partition, for MS-DOS and GPT.
Definition: Partition.h:128
The main container of the libstorage-ng.
Definition: Devicegraph.h:169
Swap partition, for MS-DOS, GPT and DASD.
Definition: Partition.h:112
Only for MS-DOS.
Definition: Partition.h:100
const std::string & get_label() const
Get the partition label.
An abstract Block Device.
Definition: BlkDevice.h:48
unsigned int get_number() const
Get the partition number.
const std::string & get_uuid() const
Get the partition UUID.
void set_boot(bool boot)
Set the boot flag of the partition.
Linux Root Partition (s390), only for GPT.
Definition: Partition.h:171
Linux USR Partition (arm), only for GPT.
Definition: Partition.h:183
An abstract base class for storage devices.
Definition: Device.h:81
PartitionType get_type() const
Get the partition type.
Partitionable * get_partitionable()
Return the partitionable the partition belongs to.
bool is_no_automount() const
Query the no-automount flag of the partition.
Microsoft reserved partition, only for GPT.
Definition: Partition.h:141
Only for MS-DOS.
Definition: Partition.h:103
Definition: Partitionable.h:39
Linux USR Partition (ppc64be), only for GPT.
Definition: Partition.h:192
bool is_legacy_boot() const
Query the legacy boot flag of the partition.
Linux Root Partition (ppc64le), only for GPT.
Definition: Partition.h:162
A partition of a Partitionable, e.g.
Definition: Partition.h:280
Linux USR Partition (riscv64), only for GPT.
Definition: Partition.h:201
RAID partition, for MS-DOS, GPT and DASD.
Definition: Partition.h:125
Linux Root Partition (riscv64), only for GPT.
Definition: Partition.h:168
Linux Home, only for GPT.
Definition: Partition.h:144
The storage namespace.
Definition: Actiongraph.h:39
Linux Root Partition (riscv32, only for GPT.
Definition: Partition.h:165
static Partition * find_by_name(Devicegraph *devicegraph, const std::string &name)
Find a Partition by its name.
Definition: PartitionTable.h:87
Linux Root Partition (ppc), only for GPT.
Definition: Partition.h:156
Linux USR Partition (aarch64), only for GPT.
Definition: Partition.h:186
bool is_boot() const
Query the boot flag of the partition.
IdNum
Enum with values used as partition ids.
Definition: Partition.h:88
static Partition * create(Devicegraph *devicegraph, const std::string &name, const Region &region, PartitionType type)
Create a device of type Partition.
void set_id(unsigned int id)
Set the partition id.