1 /*
2 libparted - a library for manipulating disk partitions
3 Copyright (C) 1999, 2000, 2001, 2002, 2007 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20 * \addtogroup PedDisk
21 * @{
22 */
23
24 /** \file disk.h */
25
26 #ifndef PED_DISK_H_INCLUDED
27 #define PED_DISK_H_INCLUDED
28
29 /**
30 * Partition types
31 */
32 enum _PedPartitionType {
33 PED_PARTITION_NORMAL = 0x00,
34 PED_PARTITION_LOGICAL = 0x01,
35 PED_PARTITION_EXTENDED = 0x02,
36 PED_PARTITION_FREESPACE = 0x04,
37 PED_PARTITION_METADATA = 0x08,
38 PED_PARTITION_PROTECTED = 0x10
39 };
40
41 /**
42 * Partition flags.
43 */
44 enum _PedPartitionFlag {
45 PED_PARTITION_BOOT=1,
46 PED_PARTITION_ROOT=2,
47 PED_PARTITION_SWAP=3,
48 PED_PARTITION_HIDDEN=4,
49 PED_PARTITION_RAID=5,
50 PED_PARTITION_LVM=6,
51 PED_PARTITION_LBA=7,
52 PED_PARTITION_HPSERVICE=8,
53 PED_PARTITION_PALO=9,
54 PED_PARTITION_PREP=10,
55 PED_PARTITION_MSFT_RESERVED=11
56 };
57 #define PED_PARTITION_FIRST_FLAG PED_PARTITION_BOOT
58 #define PED_PARTITION_LAST_FLAG PED_PARTITION_MSFT_RESERVED
59
60 enum _PedDiskTypeFeature {
61 PED_DISK_TYPE_EXTENDED=1, /**< supports extended partitions */
62 PED_DISK_TYPE_PARTITION_NAME=2 /**< supports partition names */
63 };
64 #define PED_DISK_TYPE_FIRST_FEATURE PED_DISK_TYPE_EXTENDED
65 #define PED_DISK_TYPE_LAST_FEATURE PED_DISK_TYPE_PARTITION_NAME
66
67 struct _PedDisk;
68 struct _PedPartition;
69 struct _PedDiskOps;
70 struct _PedDiskType;
71 struct _PedDiskArchOps;
72
73 typedef enum _PedPartitionType PedPartitionType;
74 typedef enum _PedPartitionFlag PedPartitionFlag;
75 typedef enum _PedDiskTypeFeature PedDiskTypeFeature;
76 typedef struct _PedDisk PedDisk;
77 typedef struct _PedPartition PedPartition;
78 typedef const struct _PedDiskOps PedDiskOps;
79 typedef struct _PedDiskType PedDiskType;
80 typedef const struct _PedDiskArchOps PedDiskArchOps;
81
82 #include <parted/device.h>
83 #include <parted/filesys.h>
84 #include <parted/natmath.h>
85 #include <parted/geom.h>
86
87 /** @} */
88
89 /**
90 * \addtogroup PedPartition
91 *
92 * @{
93 */
94
95 /** \file disk.h */
96
97 /**
98 * PedPartition structure represents a partition.
99 */
100 struct _PedPartition {
101 PedPartition* prev;
102 PedPartition* next;
103
104 /**< the partition table of the partition */
105 PedDisk* disk;
106 PedGeometry geom; /**< geometry of the partition */
107
108 /**< the partition number: In Linux, this is the
109 same as the minor number. No assumption
110 should be made about "num" and "type"
111 - different disk labels have different rules. */
112
113 int num;
114 PedPartitionType type; /**< the type of partition: a bit field of
115 PED_PARTITION_LOGICAL, PED_PARTITION_EXTENDED,
116 PED_PARTITION_METADATA
117 and PED_PARTITION_FREESPACE.
118 Both the first two, and the last two are
119 mutually exclusive.
120 An extended partition is a primary
121 partition that may contain logical partitions.
122 There is at most one extended partition on
123 a disk.
124 A logical partition is like a primary
125 partition, except it's inside an extended
126 partition. Internally, pseudo partitions are
127 allocated to represent free space, or disk
128 label meta-data. These have the
129 PED_PARTITION_FREESPACE or
130 PED_PARTITION_METADATA bit set. */
131
132 /**< The type of file system on the partition. NULL if unknown. */
133 const PedFileSystemType* fs_type;
134
135 /**< Only used for an extended partition. The list of logical
136 partitions (and free space and metadata within the extended
137 partition). */
138 PedPartition* part_list;
139
140 void* disk_specific;
141 };
142
143 /** @} */
144
145 /**
146 * \addtogroup PedDisk
147 * @{
148 */
149
150 /**
151 * Represents a disk label (partition table).
152 */
153 struct _PedDisk {
154 PedDevice* dev; /**< the device where the
155 partition table lies */
156 const PedDiskType* type; /**< type of disk label */
157 const int* block_sizes; /**< block sizes supported
158 by this label */
159 PedPartition* part_list; /**< list of partitions. Access with
160 ped_disk_next_partition() */
161
162 void* disk_specific;
163
164 /* office use only ;-) */
165 int needs_clobber; /**< clobber before write? */
166 int update_mode; /**< mode without free/metadata
167 partitions, for easier
168 update */
169 };
170
171 struct _PedDiskOps {
172 /* disk label operations */
173 int (*probe) (const PedDevice *dev);
174 int (*clobber) (PedDevice* dev);
175 PedDisk* (*alloc) (const PedDevice* dev);
176 PedDisk* (*duplicate) (const PedDisk* disk);
177 void (*free) (PedDisk* disk);
178 int (*read) (PedDisk* disk);
179 int (*write) (const PedDisk* disk);
180 /** \todo add label guessing op here */
181
182 /* partition operations */
183 PedPartition* (*partition_new) (
184 const PedDisk* disk,
185 PedPartitionType part_type,
186 const PedFileSystemType* fs_type,
187 PedSector start,
188 PedSector end);
189 PedPartition* (*partition_duplicate) (const PedPartition* part);
190 void (*partition_destroy) (PedPartition* part);
191 int (*partition_set_system) (PedPartition* part,
192 const PedFileSystemType* fs_type);
193 int (*partition_set_flag) (
194 PedPartition* part,
195 PedPartitionFlag flag,
196 int state);
197 int (*partition_get_flag) (
198 const PedPartition* part,
199 PedPartitionFlag flag);
200 int (*partition_is_flag_available) (
201 const PedPartition* part,
202 PedPartitionFlag flag);
203 void (*partition_set_name) (PedPartition* part, const char* name);
204 const char* (*partition_get_name) (const PedPartition* part);
205 int (*partition_align) (PedPartition* part,
206 const PedConstraint* constraint);
207 int (*partition_enumerate) (PedPartition* part);
208
209 /* other */
210 int (*alloc_metadata) (PedDisk* disk);
211 int (*get_max_primary_partition_count) (const PedDisk* disk);
212 };
213
214 struct _PedDiskType {
215 PedDiskType* next;
216 const char* name; /**< the name of the partition table type.
217 \todo not very intuitive name */
218 PedDiskOps* const ops;
219
220 PedDiskTypeFeature features; /**< bitmap of supported features */
221 };
222
223 /**
224 * Architecture-specific operations. i.e. communication with kernel (or
225 * whatever) about changes, etc.
226 */
227 struct _PedDiskArchOps {
228 char* (*partition_get_path) (const PedPartition* part);
229 int (*partition_is_busy) (const PedPartition* part);
230 int (*disk_commit) (PedDisk* disk);
231 };
232
233 extern void ped_disk_type_register (PedDiskType* type);
234 extern void ped_disk_type_unregister (PedDiskType* type);
235
236 extern PedDiskType* ped_disk_type_get_next (PedDiskType* type);
237 extern PedDiskType* ped_disk_type_get (const char* name);
238 extern int ped_disk_type_check_feature (const PedDiskType* disk_type,
239 PedDiskTypeFeature feature);
240
241 extern PedDiskType* ped_disk_probe (PedDevice* dev);
242 extern int ped_disk_clobber (PedDevice* dev);
243 extern int ped_disk_clobber_exclude (PedDevice* dev,
244 const PedDiskType* exclude);
245 extern PedDisk* ped_disk_new (PedDevice* dev);
246 extern PedDisk* ped_disk_new_fresh (PedDevice* dev,
247 const PedDiskType* disk_type);
248 extern PedDisk* ped_disk_duplicate (const PedDisk* old_disk);
249 extern void ped_disk_destroy (PedDisk* disk);
250 extern int ped_disk_commit (PedDisk* disk);
251 extern int ped_disk_commit_to_dev (PedDisk* disk);
252 extern int ped_disk_commit_to_os (PedDisk* disk);
253 extern int ped_disk_check (const PedDisk* disk);
254 extern void ped_disk_print (const PedDisk* disk);
255
256 extern int ped_disk_get_primary_partition_count (const PedDisk* disk);
257 extern int ped_disk_get_last_partition_num (const PedDisk* disk);
258 extern int ped_disk_get_max_primary_partition_count (const PedDisk* disk);
259
260 /** @} */
261
262 /**
263 * \addtogroup PedPartition
264 *
265 * @{
266 */
267
268 extern PedPartition* ped_partition_new (const PedDisk* disk,
269 PedPartitionType type,
270 const PedFileSystemType* fs_type,
271 PedSector start,
272 PedSector end);
273 extern void ped_partition_destroy (PedPartition* part);
274 extern int ped_partition_is_active (const PedPartition* part);
275 extern int ped_partition_set_flag (PedPartition* part, PedPartitionFlag flag,
276 int state);
277 extern int ped_partition_get_flag (const PedPartition* part,
278 PedPartitionFlag flag);
279 extern int ped_partition_is_flag_available (const PedPartition* part,
280 PedPartitionFlag flag);
281 extern int ped_partition_set_system (PedPartition* part,
282 const PedFileSystemType* fs_type);
283 extern int ped_partition_set_name (PedPartition* part, const char* name);
284 extern const char* ped_partition_get_name (const PedPartition* part);
285 extern int ped_partition_is_busy (const PedPartition* part);
286 extern char* ped_partition_get_path (const PedPartition* part);
287
288 extern const char* ped_partition_type_get_name (PedPartitionType part_type);
289 extern const char* ped_partition_flag_get_name (PedPartitionFlag flag);
290 extern PedPartitionFlag ped_partition_flag_get_by_name (const char* name);
291 extern PedPartitionFlag ped_partition_flag_next (PedPartitionFlag flag);
292
293 /** @} */
294
295 /**
296 * \addtogroup PedDisk
297 * @{
298 */
299
300 extern int ped_disk_add_partition (PedDisk* disk, PedPartition* part,
301 const PedConstraint* constraint);
302 extern int ped_disk_remove_partition (PedDisk* disk, PedPartition* part);
303 extern int ped_disk_delete_partition (PedDisk* disk, PedPartition* part);
304 extern int ped_disk_delete_all (PedDisk* disk);
305 extern int ped_disk_set_partition_geom (PedDisk* disk, PedPartition* part,
306 const PedConstraint* constraint,
307 PedSector start, PedSector end);
308 extern int ped_disk_maximize_partition (PedDisk* disk, PedPartition* part,
309 const PedConstraint* constraint);
310 extern PedGeometry* ped_disk_get_max_partition_geometry (PedDisk* disk,
311 PedPartition* part, const PedConstraint* constraint);
312 extern int ped_disk_minimize_extended_partition (PedDisk* disk);
313
314 extern PedPartition* ped_disk_next_partition (const PedDisk* disk,
315 const PedPartition* part);
316 extern PedPartition* ped_disk_get_partition (const PedDisk* disk, int num);
317 extern PedPartition* ped_disk_get_partition_by_sector (const PedDisk* disk,
318 PedSector sect);
319 extern PedPartition* ped_disk_extended_partition (const PedDisk* disk);
320
321 /* internal functions */
322 extern PedDisk* _ped_disk_alloc (const PedDevice* dev, const PedDiskType* type);
323 extern void _ped_disk_free (PedDisk* disk);
324
325
326 /** @} */
327
328 /**
329 * \addtogroup PedPartition
330 *
331 * @{
332 */
333
334 extern PedPartition* _ped_partition_alloc (const PedDisk* disk,
335 PedPartitionType type,
336 const PedFileSystemType* fs_type,
337 PedSector start,
338 PedSector end);
339 extern void _ped_partition_free (PedPartition* part);
340
341 extern int _ped_partition_attempt_align (
342 PedPartition* part, const PedConstraint* external,
343 PedConstraint* internal);
344
345 #endif /* PED_DISK_H_INCLUDED */
346
347 /** @} */
348