libstorage-ng
Partition.h
1 /*
2  * Copyright (c) [2014-2015] Novell, Inc.
3  * Copyright (c) [2016-2023] 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 
38 
42  enum class PartitionType {
43 
50  PRIMARY,
51 
55  EXTENDED,
56 
60  LOGICAL
61 
62  };
63 
64 
70  std::string get_partition_type_name(PartitionType partition_type);
71 
72 
87  enum IdNum : unsigned int {
88 
90  ID_DOS12 = 0x01,
91 
93  ID_DOS16 = 0x06,
94 
96  ID_NTFS = 0x07,
97 
99  ID_DOS32 = 0x0c,
100 
102  ID_EXTENDED = 0x0f,
103 
105  ID_DIAG = 0x12,
106 
108  ID_PREP = 0x41,
109 
111  ID_SWAP = 0x82,
112 
115  ID_LINUX = 0x83,
116 
118  ID_IRST = 0x84,
119 
121  ID_LVM = 0x8e,
122 
124  ID_RAID = 0xfd,
125 
127  ID_ESP = 0xef,
128 
130  ID_UNKNOWN = 0x100,
131 
135 
138 
141 
144 
147 
150 
153 
156 
159 
162 
165 
168 
171 
174 
177 
180 
183 
186 
189 
192 
195 
198 
201 
204 
207 
210 
213 
214  };
215 
216 
224  std::string get_partition_id_name(IdNum partition_id);
225 
226 
234  bool is_linux_partition_id(IdNum id);
235 
236 
241  {
242  ROOT, USR
243  };
244 
245 
251  bool is_linux_partition_id(IdNum id, LinuxPartitionIdCategory linux_partition_id_category);
252 
253 
262  IdNum get_linux_partition_id(LinuxPartitionIdCategory linux_partition_id_category, SystemInfo& system_info);
263 
264 
268  class Partition : public BlkDevice
269  {
270  public:
271 
279  static Partition* create(Devicegraph* devicegraph, const std::string& name,
280  const Region& region, PartitionType type);
281 
282  static Partition* load(Devicegraph* devicegraph, const xmlNode* node);
283 
287  unsigned int get_number() const;
288 
294  PartitionType get_type() const;
295 
303  void set_type(PartitionType type);
304 
310  unsigned int get_id() const;
311 
319  void set_id(unsigned int id);
320 
324  bool is_boot() const;
325 
344  void set_boot(bool boot);
345 
349  bool is_legacy_boot() const;
350 
358  void set_legacy_boot(bool legacy_boot);
359 
363  bool is_no_automount() const;
364 
376  void set_no_automount(bool no_automount);
377 
384  const std::string& get_label() const;
385 
391  void set_label(const std::string& label);
392 
396  const std::string& get_uuid() const;
397 
404 
408  const PartitionTable* get_partition_table() const;
409 
416 
420  const Partitionable* get_partitionable() const;
421 
429 
435  static Partition* find_by_name(Devicegraph* devicegraph, const std::string& name);
436 
440  static const Partition* find_by_name(const Devicegraph* devicegraph, const std::string& name);
441 
447  static bool compare_by_number(const Partition* lhs, const Partition* rhs);
448 
449  public:
450 
451  class Impl;
452 
453  Impl& get_impl();
454  const Impl& get_impl() const;
455 
456  virtual Partition* clone() const override;
457 
458  Partition(Impl* impl);
459 
460  };
461 
462 
468  bool is_partition(const Device* device);
469 
476  Partition* to_partition(Device* device);
477 
481  const Partition* to_partition(const Device* device);
482 
483 }
484 
485 #endif
bool is_linux_partition_id(IdNum id)
Check whether the id is a Linux partition id.
Only for MS-DOS.
Definition: Partition.h:96
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:134
Only set during probing.
Definition: Partition.h:130
Linux USR Partition (x86), only for GPT.
Definition: Partition.h:209
Linux Root Partition (x86), only for GPT.
Definition: Partition.h:176
Linux Root Partition (s390x), only for GPT.
Definition: Partition.h:173
For MS-DOS, GPT, DASD and implicit.
Definition: Partition.h:115
void set_type(PartitionType type)
Set the partiton 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:105
Linux USR Partition (s390), only for GPT.
Definition: Partition.h:203
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:152
Linux Server Data, only for GPT.
Definition: Partition.h:146
Linux USR Partition (ppc), only for GPT.
Definition: Partition.h:188
Only for MS-DOS.
Definition: Partition.h:93
PartitionType
Enum with partition types.
Definition: Partition.h:42
Only for MS-DOS.
Definition: Partition.h:90
void set_label(const std::string &label)
Set the partition label.
LVM partition, for MS-DOS, GPT and DASD.
Definition: Partition.h:121
Linux Root Partition (ppc64be), only for GPT.
Definition: Partition.h:158
LinuxPartitionIdCategory
Enum with categories for Linux partitions.
Definition: Partition.h:240
Linux USR Partition (riscv32), only for GPT.
Definition: Partition.h:197
Linux USR Partition (x86_64), only for GPT.
Definition: Partition.h:212
IdNum get_linux_partition_id(LinuxPartitionIdCategory linux_partition_id_category, SystemInfo &system_info)
Get the partition id for the Linux partition of the given category (root, usr, ...) depending on the architecture.
Linux Root Partition (arm), only for GPT.
Definition: Partition.h:149
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:179
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:108
Linux USR Partition (s390x), only for GPT.
Definition: Partition.h:206
Windows basic data partition, only for GPT.
Definition: Partition.h:137
Linux USR Partition (ppc64le), only for GPT.
Definition: Partition.h:194
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:118
EFI System Partition, for MS-DOS and GPT.
Definition: Partition.h:127
The main container of the libstorage-ng.
Definition: Devicegraph.h:169
Swap partition, for MS-DOS, GPT and DASD.
Definition: Partition.h:111
Only for MS-DOS.
Definition: Partition.h:99
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:170
Linux USR Partition (arm), only for GPT.
Definition: Partition.h:182
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:140
Only for MS-DOS.
Definition: Partition.h:102
Definition: Partitionable.h:39
Linux USR Partition (ppc64be), only for GPT.
Definition: Partition.h:191
bool is_legacy_boot() const
Query the legacy boot flag of the partition.
Linux Root Partition (ppc64le), only for GPT.
Definition: Partition.h:161
A partition of a Partitionable, e.g.
Definition: Partition.h:268
Linux USR Partition (riscv64), only for GPT.
Definition: Partition.h:200
RAID partition, for MS-DOS, GPT and DASD.
Definition: Partition.h:124
Linux Root Partition (riscv64), only for GPT.
Definition: Partition.h:167
Linux Home, only for GPT.
Definition: Partition.h:143
The storage namespace.
Definition: Actiongraph.h:39
Linux Root Partition (riscv32, only for GPT.
Definition: Partition.h:164
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:155
Linux USR Partition (aarch64), only for GPT.
Definition: Partition.h:185
bool is_boot() const
Query the boot flag of the partition.
IdNum
Enum with values used as partition ids.
Definition: Partition.h:87
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.