| 1 |
#include <glib.h> |
| 2 |
#include <glib-object.h> |
| 3 |
#include <blockdev/utils.h> |
| 4 |
|
| 5 |
#ifndef BD_PART_API |
| 6 |
#define BD_PART_API |
| 7 |
|
| 8 |
GQuark bd_part_error_quark (void) { |
| 9 |
return g_quark_from_static_string ("g-bd-part-error-quark"); |
| 10 |
} |
| 11 |
|
| 12 |
#define BD_PART_ERROR bd_part_error_quark () |
| 13 |
typedef enum { |
| 14 |
BD_PART_ERROR_TECH_UNAVAIL, |
| 15 |
BD_PART_ERROR_FAIL, |
| 16 |
BD_PART_ERROR_INVAL, |
| 17 |
BD_PART_ERROR_EXISTS, |
| 18 |
} BDPartError; |
| 19 |
|
| 20 |
typedef enum { |
| 21 |
BD_PART_TABLE_MSDOS, |
| 22 |
BD_PART_TABLE_GPT, |
| 23 |
BD_PART_TABLE_UNDEF, |
| 24 |
} BDPartTableType; |
| 25 |
|
| 26 |
typedef enum { |
| 27 |
BD_PART_TYPE_NORMAL = 0x00, |
| 28 |
BD_PART_TYPE_LOGICAL = 0x01, |
| 29 |
BD_PART_TYPE_EXTENDED = 0x02, |
| 30 |
BD_PART_TYPE_FREESPACE = 0x04, |
| 31 |
BD_PART_TYPE_METADATA = 0x08, |
| 32 |
BD_PART_TYPE_PROTECTED = 0x10 |
| 33 |
} BDPartType; |
| 34 |
|
| 35 |
typedef enum { |
| 36 |
BD_PART_TYPE_REQ_NORMAL = 0x00, |
| 37 |
BD_PART_TYPE_REQ_LOGICAL = 0x01, |
| 38 |
BD_PART_TYPE_REQ_EXTENDED = 0x02, |
| 39 |
BD_PART_TYPE_REQ_NEXT = 0x04 |
| 40 |
} BDPartTypeReq; |
| 41 |
|
| 42 |
typedef enum { |
| 43 |
BD_PART_ALIGN_NONE, |
| 44 |
BD_PART_ALIGN_MINIMAL, |
| 45 |
BD_PART_ALIGN_OPTIMAL, |
| 46 |
} BDPartAlign; |
| 47 |
|
| 48 |
#define BD_PART_TYPE_SPEC (bd_part_spec_get_type ()) |
| 49 |
GType bd_part_spec_get_type(); |
| 50 |
|
| 51 |
/** |
| 52 |
* BDPartSpec: |
| 53 |
* @path: path of the partition (block device) |
| 54 |
* @name: name of the partition (for GPT partitions) |
| 55 |
* @uuid: UUID of the partition (for GPT partitions) |
| 56 |
* @id: id of the partition (for MSDOS partitions) |
| 57 |
* @type_guid: GUID of the partition's type (GPT) |
| 58 |
* @type: bit combination of partition's types (#BDPartType) |
| 59 |
* @start: start of the partition |
| 60 |
* @size: size of the partition |
| 61 |
* @bootable: whether the bootable flag is set or not (for MSDOS partitions) |
| 62 |
* @attrs: partition GPT attributes |
| 63 |
* @type_name: human readable representation of @type_guid |
| 64 |
*/ |
| 65 |
typedef struct BDPartSpec { |
| 66 |
gchar *path; |
| 67 |
gchar *name; |
| 68 |
gchar *uuid; |
| 69 |
gchar *id; |
| 70 |
gchar *type_guid; |
| 71 |
guint64 type; |
| 72 |
guint64 start; |
| 73 |
guint64 size; |
| 74 |
gboolean bootable; |
| 75 |
guint64 attrs; |
| 76 |
gchar *type_name; |
| 77 |
} BDPartSpec; |
| 78 |
|
| 79 |
BDPartSpec* bd_part_spec_copy (BDPartSpec *data) { |
| 80 |
if (data == NULL) |
| 81 |
return NULL; |
| 82 |
|
| 83 |
BDPartSpec *ret = g_new0 (BDPartSpec, 1); |
| 84 |
|
| 85 |
ret->path = g_strdup (data->path); |
| 86 |
ret->name = g_strdup (data->name); |
| 87 |
ret->uuid = g_strdup (data->uuid); |
| 88 |
ret->id = g_strdup (data->id); |
| 89 |
ret->type_guid = g_strdup (data->type_guid); |
| 90 |
ret->type_name = g_strdup (data->type_name); |
| 91 |
ret->type = data->type; |
| 92 |
ret->start = data->start; |
| 93 |
ret->size = data->size; |
| 94 |
ret->bootable = data->bootable; |
| 95 |
ret->attrs = data->attrs; |
| 96 |
|
| 97 |
return ret; |
| 98 |
} |
| 99 |
|
| 100 |
void bd_part_spec_free (BDPartSpec *data) { |
| 101 |
if (data == NULL) |
| 102 |
return; |
| 103 |
|
| 104 |
g_free (data->path); |
| 105 |
g_free (data->name); |
| 106 |
g_free (data->uuid); |
| 107 |
g_free (data->id); |
| 108 |
g_free (data->type_guid); |
| 109 |
g_free (data->type_name); |
| 110 |
g_free (data); |
| 111 |
} |
| 112 |
|
| 113 |
GType bd_part_spec_get_type () { |
| 114 |
static GType type = 0; |
| 115 |
|
| 116 |
if (G_UNLIKELY(type == 0)) { |
| 117 |
type = g_boxed_type_register_static("BDPartSpec", |
| 118 |
(GBoxedCopyFunc) bd_part_spec_copy, |
| 119 |
(GBoxedFreeFunc) bd_part_spec_free); |
| 120 |
} |
| 121 |
|
| 122 |
return type; |
| 123 |
} |
| 124 |
|
| 125 |
#define BD_PART_TYPE_DISK_SPEC (bd_part_disk_spec_get_type ()) |
| 126 |
GType bd_part_disk_spec_get_type(); |
| 127 |
|
| 128 |
/** |
| 129 |
* BDPartDiskSpec: |
| 130 |
* @path: path of the disk (block device) |
| 131 |
* @table_type: type of the disk's partition table |
| 132 |
* @size: size of the disk |
| 133 |
* @sector_size: disk's sector size |
| 134 |
*/ |
| 135 |
typedef struct BDPartDiskSpec { |
| 136 |
gchar *path; |
| 137 |
BDPartTableType table_type; |
| 138 |
guint64 size; |
| 139 |
guint64 sector_size; |
| 140 |
} BDPartDiskSpec; |
| 141 |
|
| 142 |
BDPartDiskSpec* bd_part_disk_spec_copy (BDPartDiskSpec *data) { |
| 143 |
if (data == NULL) |
| 144 |
return NULL; |
| 145 |
|
| 146 |
BDPartDiskSpec *ret = g_new0 (BDPartDiskSpec, 1); |
| 147 |
|
| 148 |
ret->path = g_strdup (data->path); |
| 149 |
ret->table_type = data->table_type; |
| 150 |
ret->size = data->size; |
| 151 |
ret->sector_size = data->sector_size; |
| 152 |
|
| 153 |
return ret; |
| 154 |
} |
| 155 |
|
| 156 |
void bd_part_disk_spec_free (BDPartDiskSpec *data) { |
| 157 |
if (data == NULL) |
| 158 |
return; |
| 159 |
|
| 160 |
g_free (data->path); |
| 161 |
g_free (data); |
| 162 |
} |
| 163 |
|
| 164 |
GType bd_part_disk_spec_get_type () { |
| 165 |
static GType type = 0; |
| 166 |
|
| 167 |
if (G_UNLIKELY(type == 0)) { |
| 168 |
type = g_boxed_type_register_static("BDPartDiskSpec", |
| 169 |
(GBoxedCopyFunc) bd_part_disk_spec_copy, |
| 170 |
(GBoxedFreeFunc) bd_part_disk_spec_free); |
| 171 |
} |
| 172 |
|
| 173 |
return type; |
| 174 |
} |
| 175 |
|
| 176 |
typedef enum { |
| 177 |
BD_PART_TECH_MBR = 0, |
| 178 |
BD_PART_TECH_GPT, |
| 179 |
} BDPartTech; |
| 180 |
|
| 181 |
typedef enum { |
| 182 |
BD_PART_TECH_MODE_CREATE_TABLE = 1 << 0, |
| 183 |
BD_PART_TECH_MODE_MODIFY_TABLE = 1 << 1, |
| 184 |
BD_PART_TECH_MODE_QUERY_TABLE = 1 << 2, |
| 185 |
BD_PART_TECH_MODE_MODIFY_PART = 1 << 3, |
| 186 |
BD_PART_TECH_MODE_QUERY_PART = 1 << 4, |
| 187 |
} BDPartTechMode; |
| 188 |
|
| 189 |
/** |
| 190 |
* bd_part_is_tech_avail: |
| 191 |
* @tech: the queried tech |
| 192 |
* @mode: a bit mask of queried modes of operation (#BDPartTechMode) for @tech |
| 193 |
* @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available) |
| 194 |
* |
| 195 |
* Returns: whether the @tech-@mode combination is available -- supported by the |
| 196 |
* plugin implementation and having all the runtime dependencies available |
| 197 |
*/ |
| 198 |
gboolean bd_part_is_tech_avail (BDPartTech tech, guint64 mode, GError **error); |
| 199 |
|
| 200 |
/** |
| 201 |
* bd_part_create_table: |
| 202 |
* @disk: path of the disk block device to create partition table on |
| 203 |
* @type: type of the partition table to create |
| 204 |
* @ignore_existing: whether to ignore/overwrite the existing table or not |
| 205 |
* (reports an error if %FALSE and there's some table on @disk) |
| 206 |
* @error: (out) (optional): place to store error (if any) |
| 207 |
* |
| 208 |
* Returns: whether the partition table was successfully created or not |
| 209 |
* |
| 210 |
* Tech category: %BD_PART_TECH_MODE_CREATE_TABLE + the tech according to @type |
| 211 |
*/ |
| 212 |
gboolean bd_part_create_table (const gchar *disk, BDPartTableType type, gboolean ignore_existing, GError **error); |
| 213 |
|
| 214 |
/** |
| 215 |
* bd_part_get_part_spec: |
| 216 |
* @disk: disk to remove the partition from |
| 217 |
* @part: partition to get spec for |
| 218 |
* @error: (out) (optional): place to store error (if any) |
| 219 |
* |
| 220 |
* Returns: (transfer full): spec of the @part partition from @disk or %NULL in case of error |
| 221 |
* |
| 222 |
* Tech category: %BD_PART_TECH_MODE_QUERY_PART + the tech according to the partition table type |
| 223 |
*/ |
| 224 |
BDPartSpec* bd_part_get_part_spec (const gchar *disk, const gchar *part, GError **error); |
| 225 |
|
| 226 |
/** |
| 227 |
* bd_part_get_part_by_pos: |
| 228 |
* @disk: disk to remove the partition from |
| 229 |
* @position: position (in bytes) determining the partition |
| 230 |
* @error: (out) (optional): place to store error (if any) |
| 231 |
* |
| 232 |
* Returns: (transfer full): spec of the partition from @disk spanning over the @position or %NULL if no such |
| 233 |
* partition exists or in case of error (@error is set) |
| 234 |
* |
| 235 |
* Tech category: %BD_PART_TECH_MODE_QUERY_PART + the tech according to the partition table type |
| 236 |
*/ |
| 237 |
BDPartSpec* bd_part_get_part_by_pos (const gchar *disk, guint64 position, GError **error); |
| 238 |
|
| 239 |
/** |
| 240 |
* bd_part_get_disk_spec: |
| 241 |
* @disk: disk to get information about |
| 242 |
* @error: (out) (optional): place to store error (if any) |
| 243 |
* |
| 244 |
* Returns: (transfer full): information about the given @disk or %NULL (in case of error) |
| 245 |
* |
| 246 |
* Tech category: %BD_PART_TECH_MODE_QUERY_TABLE + the tech according to the partition table type |
| 247 |
*/ |
| 248 |
BDPartDiskSpec* bd_part_get_disk_spec (const gchar *disk, GError **error); |
| 249 |
|
| 250 |
/** |
| 251 |
* bd_part_get_disk_parts: |
| 252 |
* @disk: disk to get information about partitions for |
| 253 |
* @error: (out) (optional): place to store error (if any) |
| 254 |
* |
| 255 |
* Returns: (transfer full) (array zero-terminated=1): specs of the partitions from @disk or %NULL in case of error |
| 256 |
* |
| 257 |
* Tech category: %BD_PART_TECH_MODE_QUERY_TABLE + the tech according to the partition table type |
| 258 |
*/ |
| 259 |
BDPartSpec** bd_part_get_disk_parts (const gchar *disk, GError **error); |
| 260 |
|
| 261 |
/** |
| 262 |
* bd_part_get_disk_free_regions: |
| 263 |
* @disk: disk to get free regions for |
| 264 |
* @error: (out) (optional): place to store error (if any) |
| 265 |
* |
| 266 |
* Returns: (transfer full) (array zero-terminated=1): specs of the free regions from @disk or %NULL in case of error |
| 267 |
* |
| 268 |
* Tech category: %BD_PART_TECH_MODE_QUERY_TABLE + the tech according to the partition table type |
| 269 |
*/ |
| 270 |
BDPartSpec** bd_part_get_disk_free_regions (const gchar *disk, GError **error); |
| 271 |
|
| 272 |
/** |
| 273 |
* bd_part_get_best_free_region: |
| 274 |
* @disk: disk to get the best free region for |
| 275 |
* @type: type of the partition that is planned to be added |
| 276 |
* @size: size of the partition to be added |
| 277 |
* @error: (out) (optional): place to store error (if any) |
| 278 |
* |
| 279 |
* Returns: (transfer full): spec of the best free region on @disk for a new partition of type @type |
| 280 |
* with the size of @size or %NULL if there is none such region or if |
| 281 |
* there was an error (@error gets populated) |
| 282 |
* |
| 283 |
* Note: For the @type %BD_PART_TYPE_NORMAL, the smallest possible space that *is not* in an extended partition |
| 284 |
* is found. For the @type %BD_PART_TYPE_LOGICAL, the smallest possible space that *is* in an extended |
| 285 |
* partition is found. For %BD_PART_TYPE_EXTENDED, the biggest possible space is found as long as there |
| 286 |
* is no other extended partition (there can only be one). |
| 287 |
* |
| 288 |
* Tech category: %BD_PART_TECH_MODE_QUERY_TABLE + the tech according to the partition table type |
| 289 |
*/ |
| 290 |
BDPartSpec* bd_part_get_best_free_region (const gchar *disk, BDPartType type, guint64 size, GError **error); |
| 291 |
|
| 292 |
/** |
| 293 |
* bd_part_create_part: |
| 294 |
* @disk: disk to create partition on |
| 295 |
* @type: type of the partition to create (if %BD_PART_TYPE_REQ_NEXT, the |
| 296 |
* partition type will be determined automatically based on the existing |
| 297 |
* partitions) |
| 298 |
* @start: where the partition should start (i.e. offset from the disk start) |
| 299 |
* @size: desired size of the partition (if 0, a max-sized partition is created) |
| 300 |
* @align: alignment to use for the partition |
| 301 |
* @error: (out) (optional): place to store error (if any) |
| 302 |
* |
| 303 |
* Returns: (transfer full): specification of the created partition or %NULL in case of error |
| 304 |
* |
| 305 |
* NOTE: The resulting partition may start at a different position than given by |
| 306 |
* @start and can have different size than @size due to alignment. |
| 307 |
* |
| 308 |
* Tech category: %BD_PART_TECH_MODE_MODIFY_TABLE + the tech according to the partition table type |
| 309 |
*/ |
| 310 |
BDPartSpec* bd_part_create_part (const gchar *disk, BDPartTypeReq type, guint64 start, guint64 size, BDPartAlign align, GError **error); |
| 311 |
|
| 312 |
/** |
| 313 |
* bd_part_delete_part: |
| 314 |
* @disk: disk to remove the partition from |
| 315 |
* @part: partition to remove |
| 316 |
* @error: (out) (optional): place to store error (if any) |
| 317 |
* |
| 318 |
* Returns: whether the @part partition was successfully deleted from @disk |
| 319 |
* |
| 320 |
* Tech category: %BD_PART_TECH_MODE_MODIFY_TABLE + the tech according to the partition table type |
| 321 |
*/ |
| 322 |
gboolean bd_part_delete_part (const gchar *disk, const gchar *part, GError **error); |
| 323 |
|
| 324 |
/** |
| 325 |
* bd_part_resize_part: |
| 326 |
* @disk: disk containing the partition |
| 327 |
* @part: partition to resize |
| 328 |
* @size: new partition size, 0 for maximal size |
| 329 |
* @align: alignment to use for the partition end |
| 330 |
* @error: (out) (optional): place to store error (if any) |
| 331 |
* |
| 332 |
* Returns: whether the @part partition was successfully resized on @disk to @size |
| 333 |
* |
| 334 |
* NOTE: The resulting partition may be slightly bigger than requested due to alignment. |
| 335 |
* |
| 336 |
* Tech category: %BD_PART_TECH_MODE_MODIFY_TABLE + the tech according to the partition table type |
| 337 |
*/ |
| 338 |
gboolean bd_part_resize_part (const gchar *disk, const gchar *part, guint64 size, BDPartAlign align, GError **error); |
| 339 |
|
| 340 |
/** |
| 341 |
* bd_part_set_part_name: |
| 342 |
* @disk: device the partition belongs to |
| 343 |
* @part: partition the name should be set for |
| 344 |
* @name: name to set |
| 345 |
* @error: (out) (optional): place to store error (if any) |
| 346 |
* |
| 347 |
* Returns: whether the name was successfully set or not |
| 348 |
* |
| 349 |
* Tech category: %BD_PART_TECH_GPT-%BD_PART_TECH_MODE_MODIFY_PART |
| 350 |
*/ |
| 351 |
gboolean bd_part_set_part_name (const gchar *disk, const gchar *part, const gchar *name, GError **error); |
| 352 |
|
| 353 |
/** |
| 354 |
* bd_part_set_part_type: |
| 355 |
* @disk: device the partition belongs to |
| 356 |
* @part: partition the type should be set for |
| 357 |
* @type_guid: GUID of the type |
| 358 |
* @error: (out) (optional): place to store error (if any) |
| 359 |
* |
| 360 |
* Returns: whether the @type_guid type was successfully set for @part or not |
| 361 |
* |
| 362 |
* Tech category: %BD_PART_TECH_GPT-%BD_PART_TECH_MODE_MODIFY_PART |
| 363 |
*/ |
| 364 |
gboolean bd_part_set_part_type (const gchar *disk, const gchar *part, const gchar *type_guid, GError **error); |
| 365 |
|
| 366 |
/** |
| 367 |
* bd_part_set_part_id: |
| 368 |
* @disk: device the partition belongs to |
| 369 |
* @part: partition the ID should be set for |
| 370 |
* @part_id: partition Id |
| 371 |
* @error: (out) (optional): place to store error (if any) |
| 372 |
* |
| 373 |
* Returns: whether the @part_id type was successfully set for @part or not |
| 374 |
* |
| 375 |
* Tech category: %BD_PART_TECH_MBR-%BD_PART_TECH_MODE_MODIFY_PART |
| 376 |
*/ |
| 377 |
gboolean bd_part_set_part_id (const gchar *disk, const gchar *part, const gchar *part_id, GError **error); |
| 378 |
|
| 379 |
/** |
| 380 |
* bd_part_set_part_uuid: |
| 381 |
* @disk: device the partition belongs to |
| 382 |
* @part: partition the UUID should be set for |
| 383 |
* @uuid: partition UUID to set |
| 384 |
* @error: (out) (optional): place to store error (if any) |
| 385 |
* |
| 386 |
* Returns: whether the @uuid type was successfully set for @part or not |
| 387 |
* |
| 388 |
* Tech category: %BD_PART_TECH_GPT-%BD_PART_TECH_MODE_MODIFY_PART |
| 389 |
*/ |
| 390 |
gboolean bd_part_set_part_uuid (const gchar *disk, const gchar *part, const gchar *uuid, GError **error); |
| 391 |
|
| 392 |
/** |
| 393 |
* bd_part_set_part_bootable: |
| 394 |
* @disk: device the partition belongs to |
| 395 |
* @part: partition the bootable flag should be set for |
| 396 |
* @bootable: whether to set or unset the bootable flag |
| 397 |
* @error: (out) (optional): place to store error (if any) |
| 398 |
* |
| 399 |
* Returns: whether the @bootable flag was successfully set for @part or not |
| 400 |
* |
| 401 |
* Tech category: %BD_PART_TECH_MBR-%BD_PART_TECH_MODE_MODIFY_PART |
| 402 |
*/ |
| 403 |
gboolean bd_part_set_part_bootable (const gchar *disk, const gchar *part, gboolean bootable, GError **error); |
| 404 |
|
| 405 |
/** |
| 406 |
* bd_part_set_part_attributes: |
| 407 |
* @disk: device the partition belongs to |
| 408 |
* @part: partition the attributes should be set for |
| 409 |
* @attrs: GPT attributes to set on @part |
| 410 |
* @error: (out) (optional): place to store error (if any) |
| 411 |
* |
| 412 |
* Returns: whether the @attrs GPT attributes were successfully set for @part or not |
| 413 |
* |
| 414 |
* Tech category: %BD_PART_TECH_GPT-%BD_PART_TECH_MODE_MODIFY_PART |
| 415 |
*/ |
| 416 |
gboolean bd_part_set_part_attributes (const gchar *disk, const gchar *part, guint64 attrs, GError **error); |
| 417 |
|
| 418 |
/** |
| 419 |
* bd_part_get_part_table_type_str: |
| 420 |
* @type: table type to get string representation for |
| 421 |
* @error: (out) (optional): place to store error (if any) |
| 422 |
* |
| 423 |
* Returns: (transfer none): string representation of @table_type |
| 424 |
* |
| 425 |
* Tech category: the tech according to @type |
| 426 |
*/ |
| 427 |
const gchar* bd_part_get_part_table_type_str (BDPartTableType type, GError **error); |
| 428 |
|
| 429 |
/** |
| 430 |
* bd_part_get_type_str: |
| 431 |
* @type: type to get string representation for |
| 432 |
* @error: (out) (optional): place to store error (if any) |
| 433 |
* |
| 434 |
* Returns: (transfer none): string representation of @type |
| 435 |
* |
| 436 |
* Tech category: always available |
| 437 |
*/ |
| 438 |
const gchar* bd_part_get_type_str (BDPartType type, GError **error); |
| 439 |
|
| 440 |
#endif /* BD_PART_API */ |