| 1 |
/* |
| 2 |
* Copyright (C) 2016 Red Hat, Inc. |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2.1 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 16 |
* |
| 17 |
* Author: Vratislav Podzimek <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <ctype.h> |
| 21 |
#include <sys/file.h> |
| 22 |
#include <fcntl.h> |
| 23 |
#include <blockdev/utils.h> |
| 24 |
#include <libfdisk.h> |
| 25 |
#include <locale.h> |
| 26 |
|
| 27 |
#include "part.h" |
| 28 |
|
| 29 |
/** |
| 30 |
* SECTION: part |
| 31 |
* @short_description: plugin for operations with partition tables |
| 32 |
* @title: Part |
| 33 |
* @include: part.h |
| 34 |
* |
| 35 |
* A plugin for operations with partition tables. Currently supported table |
| 36 |
* (disk label) types are MBR and GPT. See the functions below to get an |
| 37 |
* overview of which operations are supported. If there's anything missing, |
| 38 |
* please don't hesitate to report it as this plugin (just like all the others) |
| 39 |
* is subject to future development and enhancements. |
| 40 |
* |
| 41 |
* This particular implementation of the part plugin uses libfdisk for |
| 42 |
* manipulations of both the MBR and GPT disk label types. |
| 43 |
*/ |
| 44 |
|
| 45 |
/** |
| 46 |
* bd_part_error_quark: (skip) |
| 47 |
*/ |
| 48 |
GQuark bd_part_error_quark (void) |
| 49 |
{ |
| 50 |
return g_quark_from_static_string ("g-bd-part-error-quark"); |
| 51 |
} |
| 52 |
|
| 53 |
BDPartSpec* bd_part_spec_copy (BDPartSpec *data) { |
| 54 |
if (data == NULL) |
| 55 |
return NULL; |
| 56 |
|
| 57 |
BDPartSpec *ret = g_new0 (BDPartSpec, 1); |
| 58 |
|
| 59 |
ret->path = g_strdup (data->path); |
| 60 |
ret->name = g_strdup (data->name); |
| 61 |
ret->id = g_strdup (data->id); |
| 62 |
ret->uuid = g_strdup (data->uuid); |
| 63 |
ret->type_guid = g_strdup (data->type_guid); |
| 64 |
ret->type_name = g_strdup (data->type_name); |
| 65 |
ret->type = data->type; |
| 66 |
ret->start = data->start; |
| 67 |
ret->size = data->size; |
| 68 |
ret->bootable = data->bootable; |
| 69 |
ret->attrs = data->attrs; |
| 70 |
|
| 71 |
return ret; |
| 72 |
} |
| 73 |
|
| 74 |
void bd_part_spec_free (BDPartSpec *data) { |
| 75 |
if (data == NULL) |
| 76 |
return; |
| 77 |
|
| 78 |
g_free (data->path); |
| 79 |
g_free (data->name); |
| 80 |
g_free (data->uuid); |
| 81 |
g_free (data->id); |
| 82 |
g_free (data->type_guid); |
| 83 |
g_free (data->type_name); |
| 84 |
g_free (data); |
| 85 |
} |
| 86 |
|
| 87 |
BDPartDiskSpec* bd_part_disk_spec_copy (BDPartDiskSpec *data) { |
| 88 |
if (data == NULL) |
| 89 |
return NULL; |
| 90 |
|
| 91 |
BDPartDiskSpec *ret = g_new0 (BDPartDiskSpec, 1); |
| 92 |
|
| 93 |
ret->path = g_strdup (data->path); |
| 94 |
ret->table_type = data->table_type; |
| 95 |
ret->size = data->size; |
| 96 |
ret->sector_size = data->sector_size; |
| 97 |
|
| 98 |
return ret; |
| 99 |
} |
| 100 |
|
| 101 |
void bd_part_disk_spec_free (BDPartDiskSpec *data) { |
| 102 |
if (data == NULL) |
| 103 |
return; |
| 104 |
|
| 105 |
g_free (data->path); |
| 106 |
g_free (data); |
| 107 |
} |
| 108 |
|
| 109 |
/* "C" locale to get the locale-agnostic error messages */ |
| 110 |
static locale_t c_locale = (locale_t) 0; |
| 111 |
|
| 112 |
static int fdisk_version = 0; |
| 113 |
|
| 114 |
/* base 2 logarithm of x */ |
| 115 |
static gint log2i (guint x) { |
| 116 |
gint ret = 0; |
| 117 |
|
| 118 |
if (x == 0) |
| 119 |
return -1; |
| 120 |
|
| 121 |
while (x >>= 1) |
| 122 |
ret++; |
| 123 |
|
| 124 |
return ret; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
/** |
| 129 |
* get_part_num: (skip) |
| 130 |
* |
| 131 |
* Extract partition number from it's name (e.g. sda1). |
| 132 |
* |
| 133 |
* Returns: partition number or -1 in case of an error |
| 134 |
*/ |
| 135 |
static gint get_part_num (const gchar *part, GError **error) { |
| 136 |
const gchar *part_num_str = NULL; |
| 137 |
gint part_num = -1; |
| 138 |
gsize i = 0; |
| 139 |
|
| 140 |
if (!part || *part == '\0') { |
| 141 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_INVAL, |
| 142 |
"Invalid partition path given: '%s'", part); |
| 143 |
return -1; |
| 144 |
} |
| 145 |
|
| 146 |
i = strlen (part); |
| 147 |
while (i > 0 && (isdigit ((unsigned char) part[i - 1]) || part[i - 1] == '-')) |
| 148 |
i--; |
| 149 |
part_num_str = part + i; |
| 150 |
|
| 151 |
part_num = atoi (part_num_str); |
| 152 |
if (part_num == 0) { |
| 153 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_INVAL, |
| 154 |
"Invalid partition path given: '%s'. Cannot extract partition number", part); |
| 155 |
return -1; |
| 156 |
} else if (part_num < 0) { |
| 157 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_INVAL, |
| 158 |
"Invalid partition path given: '%s'.", part); |
| 159 |
return -1; |
| 160 |
} |
| 161 |
|
| 162 |
return part_num; |
| 163 |
} |
| 164 |
|
| 165 |
static int fdisk_ask_callback (struct fdisk_context *cxt G_GNUC_UNUSED, struct fdisk_ask *ask, void *data G_GNUC_UNUSED) { |
| 166 |
gint type = 0; |
| 167 |
const gchar *fdisk_msg = NULL; |
| 168 |
gchar *message = NULL; |
| 169 |
|
| 170 |
type = fdisk_ask_get_type (ask); |
| 171 |
fdisk_msg = fdisk_ask_print_get_mesg (ask); |
| 172 |
|
| 173 |
switch (type) { |
| 174 |
case FDISK_ASKTYPE_INFO: |
| 175 |
message = g_strdup_printf ("[fdisk] %s", fdisk_msg); |
| 176 |
bd_utils_log (BD_UTILS_LOG_INFO, message); |
| 177 |
g_free (message); |
| 178 |
break; |
| 179 |
case FDISK_ASKTYPE_WARNX: |
| 180 |
case FDISK_ASKTYPE_WARN: |
| 181 |
message = g_strdup_printf ("[fdisk] %s", fdisk_msg); |
| 182 |
bd_utils_log (BD_UTILS_LOG_WARNING, message); |
| 183 |
g_free (message); |
| 184 |
break; |
| 185 |
default: |
| 186 |
break; |
| 187 |
} |
| 188 |
|
| 189 |
return 0; |
| 190 |
} |
| 191 |
|
| 192 |
static struct fdisk_context* get_device_context (const gchar *disk, gboolean read_only, GError **error) { |
| 193 |
struct fdisk_context *cxt = fdisk_new_context (); |
| 194 |
gint ret = 0; |
| 195 |
|
| 196 |
if (!cxt) { |
| 197 |
g_set_error_literal (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 198 |
"Failed to create a new context"); |
| 199 |
return NULL; |
| 200 |
} |
| 201 |
|
| 202 |
ret = fdisk_assign_device (cxt, disk, read_only); |
| 203 |
if (ret != 0) { |
| 204 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 205 |
"Failed to assign the new context to disk '%s': %s", disk, strerror_l (-ret, c_locale)); |
| 206 |
fdisk_unref_context (cxt); |
| 207 |
return NULL; |
| 208 |
} |
| 209 |
|
| 210 |
fdisk_disable_dialogs (cxt, 1); |
| 211 |
fdisk_set_ask (cxt, fdisk_ask_callback, NULL); |
| 212 |
return cxt; |
| 213 |
} |
| 214 |
|
| 215 |
static void close_context (struct fdisk_context *cxt) { |
| 216 |
gint ret = 0; |
| 217 |
|
| 218 |
ret = fdisk_deassign_device (cxt, 0); /* context, nosync */ |
| 219 |
|
| 220 |
if (ret != 0) |
| 221 |
/* XXX: should report error here? */ |
| 222 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, |
| 223 |
"Failed to close and sync the device: %s", |
| 224 |
strerror_l (-ret, c_locale)); |
| 225 |
|
| 226 |
fdisk_unref_context (cxt); |
| 227 |
} |
| 228 |
|
| 229 |
static gboolean write_label (struct fdisk_context *cxt, struct fdisk_table *orig, const gchar *disk, gboolean force, GError **error) { |
| 230 |
gint ret = 0; |
| 231 |
gint dev_fd = 0; |
| 232 |
guint num_tries = 1; |
| 233 |
|
| 234 |
/* XXX: try to grab a lock for the device so that udev doesn't step in |
| 235 |
between the two operations we need to perform (see below) with its |
| 236 |
BLKRRPART ioctl() call which makes the device busy |
| 237 |
see https://systemd.io/BLOCK_DEVICE_LOCKING */ |
| 238 |
dev_fd = open (disk, O_RDONLY|O_CLOEXEC); |
| 239 |
if (dev_fd >= 0) { |
| 240 |
ret = flock (dev_fd, LOCK_EX|LOCK_NB); |
| 241 |
while ((ret != 0) && (num_tries <= 5)) { |
| 242 |
g_usleep (100 * 1000); /* microseconds */ |
| 243 |
ret = flock (dev_fd, LOCK_EX|LOCK_NB); |
| 244 |
num_tries++; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
/* Just continue even in case we don't get the lock, there's still a |
| 249 |
chance things will just work. If not, an error will be reported |
| 250 |
anyway with no harm. */ |
| 251 |
|
| 252 |
ret = fdisk_write_disklabel (cxt); |
| 253 |
if (ret != 0) { |
| 254 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 255 |
"Failed to write the new disklabel to disk '%s': %s", disk, strerror_l (-ret, c_locale)); |
| 256 |
if (dev_fd >= 0) |
| 257 |
close (dev_fd); |
| 258 |
return FALSE; |
| 259 |
} |
| 260 |
|
| 261 |
if (force) { |
| 262 |
/* force kernel to re-read entire partition table, not only changed partitions */ |
| 263 |
ret = fdisk_reread_partition_table (cxt); |
| 264 |
if (ret != 0) { |
| 265 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 266 |
"Failed to inform kernel about changes on the '%s' device: %s", disk, strerror_l (-ret, c_locale)); |
| 267 |
if (dev_fd >= 0) |
| 268 |
close (dev_fd); |
| 269 |
return FALSE; |
| 270 |
} |
| 271 |
} else if (orig) { |
| 272 |
/* We have original table layout -- reread changed partitions */ |
| 273 |
ret = fdisk_reread_changes (cxt, orig); |
| 274 |
if (ret != 0) { |
| 275 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 276 |
"Failed to inform kernel about changes on the '%s' device: %s", disk, strerror_l (-ret, c_locale)); |
| 277 |
if (dev_fd >= 0) |
| 278 |
close (dev_fd); |
| 279 |
return FALSE; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
if (dev_fd >= 0) |
| 284 |
close (dev_fd); |
| 285 |
|
| 286 |
return TRUE; |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* bd_part_init: |
| 291 |
* |
| 292 |
* Initializes the plugin. **This function is called automatically by the |
| 293 |
* library's initialization functions.** |
| 294 |
* |
| 295 |
*/ |
| 296 |
gboolean bd_part_init (void) { |
| 297 |
c_locale = newlocale (LC_ALL_MASK, "C", c_locale); |
| 298 |
fdisk_init_debug (0); |
| 299 |
fdisk_version = fdisk_get_library_version (NULL); |
| 300 |
return TRUE; |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* bd_part_close: |
| 305 |
* |
| 306 |
* Cleans up after the plugin. **This function is called automatically by the |
| 307 |
* library's functions that unload it.** |
| 308 |
* |
| 309 |
*/ |
| 310 |
void bd_part_close (void) { |
| 311 |
freelocale (c_locale); |
| 312 |
c_locale = (locale_t) 0; |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* bd_part_is_tech_avail: |
| 317 |
* @tech: the queried tech |
| 318 |
* @mode: a bit mask of queried modes of operation (#BDPartTechMode) for @tech |
| 319 |
* @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available) |
| 320 |
* |
| 321 |
* Returns: whether the @tech-@mode combination is available -- supported by the |
| 322 |
* plugin implementation and having all the runtime dependencies available |
| 323 |
*/ |
| 324 |
gboolean bd_part_is_tech_avail (BDPartTech tech, guint64 mode G_GNUC_UNUSED, GError **error) { |
| 325 |
switch (tech) { |
| 326 |
case BD_PART_TECH_MBR: |
| 327 |
case BD_PART_TECH_GPT: |
| 328 |
/* all MBR and GPT-mode combinations are supported by this implementation of the |
| 329 |
* plugin, nothing extra is needed */ |
| 330 |
return TRUE; |
| 331 |
default: |
| 332 |
g_set_error_literal (error, BD_PART_ERROR, BD_PART_ERROR_TECH_UNAVAIL, "Unknown technology"); |
| 333 |
return FALSE; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
static const gchar *table_type_str[BD_PART_TABLE_UNDEF] = {"dos", "gpt"}; |
| 338 |
|
| 339 |
/** |
| 340 |
* bd_part_create_table: |
| 341 |
* @disk: path of the disk block device to create partition table on |
| 342 |
* @type: type of the partition table to create |
| 343 |
* @ignore_existing: whether to ignore/overwrite the existing table or not |
| 344 |
* (reports an error if %FALSE and there's some table on @disk) |
| 345 |
* @error: (out) (optional): place to store error (if any) |
| 346 |
* |
| 347 |
* Returns: whether the partition table was successfully created or not |
| 348 |
* |
| 349 |
* Tech category: %BD_PART_TECH_MODE_CREATE_TABLE + the tech according to @type |
| 350 |
*/ |
| 351 |
gboolean bd_part_create_table (const gchar *disk, BDPartTableType type, gboolean ignore_existing, GError **error) { |
| 352 |
struct fdisk_context *cxt = NULL; |
| 353 |
gint ret = 0; |
| 354 |
guint64 progress_id = 0; |
| 355 |
gchar *msg = NULL; |
| 356 |
GError *l_error = NULL; |
| 357 |
|
| 358 |
msg = g_strdup_printf ("Starting creation of a new partition table on '%s'", disk); |
| 359 |
progress_id = bd_utils_report_started (msg); |
| 360 |
g_free (msg); |
| 361 |
|
| 362 |
cxt = get_device_context (disk, FALSE, &l_error); |
| 363 |
if (!cxt) { |
| 364 |
/* error is already populated */ |
| 365 |
bd_utils_report_finished (progress_id, l_error->message); |
| 366 |
g_propagate_error (error, l_error); |
| 367 |
return FALSE; |
| 368 |
} |
| 369 |
|
| 370 |
if (!ignore_existing && fdisk_has_label (cxt)) { |
| 371 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_EXISTS, |
| 372 |
"Device '%s' already contains a partition table", disk); |
| 373 |
bd_utils_report_finished (progress_id, l_error->message); |
| 374 |
g_propagate_error (error, l_error); |
| 375 |
close_context (cxt); |
| 376 |
return FALSE; |
| 377 |
} |
| 378 |
|
| 379 |
ret = fdisk_create_disklabel (cxt, table_type_str[type]); |
| 380 |
if (ret != 0) { |
| 381 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 382 |
"Failed to create a new disklabel for disk '%s': %s", disk, strerror_l (-ret, c_locale)); |
| 383 |
bd_utils_report_finished (progress_id, l_error->message); |
| 384 |
g_propagate_error (error, l_error); |
| 385 |
close_context (cxt); |
| 386 |
return FALSE; |
| 387 |
} |
| 388 |
|
| 389 |
if (!write_label (cxt, NULL, disk, FALSE, &l_error)) { |
| 390 |
bd_utils_report_finished (progress_id, l_error->message); |
| 391 |
g_propagate_error (error, l_error); |
| 392 |
close_context (cxt); |
| 393 |
return FALSE; |
| 394 |
} |
| 395 |
|
| 396 |
close_context (cxt); |
| 397 |
bd_utils_report_finished (progress_id, "Completed"); |
| 398 |
return TRUE; |
| 399 |
} |
| 400 |
|
| 401 |
static gchar* get_part_type_guid_and_gpt_flags (const gchar *device, int part_num, guint64 *attrs, char **type_name, GError **error) { |
| 402 |
struct fdisk_context *cxt = NULL; |
| 403 |
struct fdisk_label *lb = NULL; |
| 404 |
struct fdisk_partition *pa = NULL; |
| 405 |
struct fdisk_parttype *ptype = NULL; |
| 406 |
const gchar *label_name = NULL; |
| 407 |
const gchar *ptype_string = NULL; |
| 408 |
gchar *ret = NULL; |
| 409 |
gint status = 0; |
| 410 |
|
| 411 |
/* first partition in fdisk is 0 */ |
| 412 |
part_num--; |
| 413 |
|
| 414 |
cxt = get_device_context (device, TRUE, error); |
| 415 |
if (!cxt) { |
| 416 |
/* error is already populated */ |
| 417 |
return NULL; |
| 418 |
} |
| 419 |
|
| 420 |
lb = fdisk_get_label (cxt, NULL); |
| 421 |
if (!lb) { |
| 422 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 423 |
"Failed to read partition table on device '%s'", device); |
| 424 |
close_context (cxt); |
| 425 |
return NULL; |
| 426 |
} |
| 427 |
|
| 428 |
label_name = fdisk_label_get_name (lb); |
| 429 |
if (g_strcmp0 (label_name, table_type_str[BD_PART_TABLE_GPT]) != 0) { |
| 430 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_INVAL, |
| 431 |
"Setting GPT flags is not supported on '%s' partition table", label_name); |
| 432 |
close_context (cxt); |
| 433 |
return NULL; |
| 434 |
} |
| 435 |
|
| 436 |
if (attrs) { |
| 437 |
status = fdisk_gpt_get_partition_attrs (cxt, part_num, attrs); |
| 438 |
if (status < 0) { |
| 439 |
g_set_error_literal (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 440 |
"Failed to read GPT attributes"); |
| 441 |
close_context (cxt); |
| 442 |
return NULL; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
status = fdisk_get_partition (cxt, part_num, &pa); |
| 447 |
if (status != 0) { |
| 448 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 449 |
"Failed to get partition %d on device '%s'", part_num, device); |
| 450 |
close_context (cxt); |
| 451 |
return NULL; |
| 452 |
} |
| 453 |
|
| 454 |
ptype = fdisk_partition_get_type (pa); |
| 455 |
if (!ptype) { |
| 456 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 457 |
"Failed to get partition type for partition %d on device '%s'", part_num, device); |
| 458 |
fdisk_unref_partition (pa); |
| 459 |
close_context (cxt); |
| 460 |
return NULL; |
| 461 |
} |
| 462 |
|
| 463 |
/* part type name -- human readable string, e.g. "Microsoft Reserved Partition" */ |
| 464 |
ptype_string = fdisk_parttype_get_name (ptype); |
| 465 |
if (!ptype_string) { |
| 466 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 467 |
"Failed to get partition type string for partition %d on device '%s'", part_num, device); |
| 468 |
fdisk_unref_partition (pa); |
| 469 |
close_context (cxt); |
| 470 |
return NULL; |
| 471 |
} |
| 472 |
|
| 473 |
*type_name = g_strdup (ptype_string); |
| 474 |
|
| 475 |
/* part type string -- GUID as a string */ |
| 476 |
ptype_string = fdisk_parttype_get_string (ptype); |
| 477 |
if (!ptype_string) { |
| 478 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 479 |
"Failed to get partition type for partition %d on device '%s'", part_num, device); |
| 480 |
g_free (*type_name); |
| 481 |
*type_name = NULL; |
| 482 |
fdisk_unref_partition (pa); |
| 483 |
close_context (cxt); |
| 484 |
return NULL; |
| 485 |
} |
| 486 |
|
| 487 |
ret = g_strdup (ptype_string); |
| 488 |
|
| 489 |
fdisk_unref_partition (pa); |
| 490 |
close_context (cxt); |
| 491 |
return ret; |
| 492 |
} |
| 493 |
|
| 494 |
static BDPartSpec* get_part_spec_fdisk (struct fdisk_context *cxt, struct fdisk_partition *pa, GError **error) { |
| 495 |
struct fdisk_label *lb = NULL; |
| 496 |
struct fdisk_parttype *ptype = NULL; |
| 497 |
BDPartSpec *ret = NULL; |
| 498 |
const gchar *devname = NULL; |
| 499 |
const gchar *partname = NULL; |
| 500 |
const gchar *partuuid = NULL; |
| 501 |
GError *l_error = NULL; |
| 502 |
|
| 503 |
ret = g_new0 (BDPartSpec, 1); |
| 504 |
|
| 505 |
devname = fdisk_get_devname (cxt); |
| 506 |
|
| 507 |
if (fdisk_partition_has_partno (pa)) { |
| 508 |
if (isdigit (devname[strlen (devname) - 1])) |
| 509 |
ret->path = g_strdup_printf ("%sp%zu", devname, fdisk_partition_get_partno (pa) + 1); |
| 510 |
else |
| 511 |
ret->path = g_strdup_printf ("%s%zu", devname, fdisk_partition_get_partno (pa) + 1); |
| 512 |
} |
| 513 |
|
| 514 |
partname = fdisk_partition_get_name (pa); |
| 515 |
if (partname) |
| 516 |
ret->name = g_strdup (partname); |
| 517 |
|
| 518 |
partuuid = fdisk_partition_get_uuid (pa); |
| 519 |
if (partuuid) |
| 520 |
ret->uuid = g_strdup (partuuid); |
| 521 |
|
| 522 |
if (fdisk_partition_is_container (pa)) |
| 523 |
ret->type = BD_PART_TYPE_EXTENDED; |
| 524 |
else if (fdisk_partition_is_nested (pa)) |
| 525 |
ret->type = BD_PART_TYPE_LOGICAL; |
| 526 |
else |
| 527 |
ret->type = BD_PART_TYPE_NORMAL; |
| 528 |
|
| 529 |
if (fdisk_partition_is_freespace (pa)) |
| 530 |
ret->type |= BD_PART_TYPE_FREESPACE; |
| 531 |
|
| 532 |
if (fdisk_partition_has_start (pa)) |
| 533 |
ret->start = (guint64) fdisk_partition_get_start (pa) * fdisk_get_sector_size (cxt); |
| 534 |
|
| 535 |
if (fdisk_partition_has_size (pa)) |
| 536 |
ret->size = (guint64) fdisk_partition_get_size (pa) * fdisk_get_sector_size (cxt); |
| 537 |
|
| 538 |
lb = fdisk_get_label (cxt, NULL); |
| 539 |
if (!lb) { |
| 540 |
g_set_error_literal (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 541 |
"Failed to read partition table."); |
| 542 |
bd_part_spec_free (ret); |
| 543 |
return NULL; |
| 544 |
} |
| 545 |
|
| 546 |
if (g_strcmp0 (fdisk_label_get_name (lb), "gpt") == 0) { |
| 547 |
if (ret->type == BD_PART_TYPE_NORMAL) { |
| 548 |
/* only 'normal' partitions have GUIDs */ |
| 549 |
ret->type_guid = get_part_type_guid_and_gpt_flags (devname, fdisk_partition_get_partno (pa) + 1, |
| 550 |
&(ret->attrs), &(ret->type_name), &l_error); |
| 551 |
if (!ret->type_guid && l_error) { |
| 552 |
g_propagate_error (error, l_error); |
| 553 |
bd_part_spec_free (ret); |
| 554 |
return NULL; |
| 555 |
} |
| 556 |
} |
| 557 |
} else if (g_strcmp0 (fdisk_label_get_name (lb), "dos") == 0) { |
| 558 |
/* freespace and extended have no type/ids */ |
| 559 |
if (ret->type == BD_PART_TYPE_NORMAL || ret->type == BD_PART_TYPE_LOGICAL || ret->type == BD_PART_TYPE_EXTENDED) { |
| 560 |
ptype = fdisk_partition_get_type (pa); |
| 561 |
if (!ptype) { |
| 562 |
g_set_error_literal (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 563 |
"Failed to get partition type."); |
| 564 |
bd_part_spec_free (ret); |
| 565 |
return NULL; |
| 566 |
} |
| 567 |
ret->id = g_strdup_printf ("0x%02x", fdisk_parttype_get_code (ptype)); |
| 568 |
} |
| 569 |
if (fdisk_partition_is_bootable (pa) == 1) |
| 570 |
ret->bootable = TRUE; |
| 571 |
} |
| 572 |
|
| 573 |
return ret; |
| 574 |
} |
| 575 |
|
| 576 |
/** |
| 577 |
* bd_part_get_part_spec: |
| 578 |
* @disk: disk to remove the partition from |
| 579 |
* @part: partition to get spec for |
| 580 |
* @error: (out) (optional): place to store error (if any) |
| 581 |
* |
| 582 |
* Returns: (transfer full): spec of the @part partition from @disk or %NULL in case of error |
| 583 |
* |
| 584 |
* Tech category: %BD_PART_TECH_MODE_QUERY_PART + the tech according to the partition table type |
| 585 |
*/ |
| 586 |
BDPartSpec* bd_part_get_part_spec (const gchar *disk, const gchar *part, GError **error) { |
| 587 |
struct fdisk_context *cxt = NULL; |
| 588 |
struct fdisk_partition *pa = NULL; |
| 589 |
gint status = 0; |
| 590 |
gint part_num = 0; |
| 591 |
BDPartSpec *ret = NULL; |
| 592 |
|
| 593 |
part_num = get_part_num (part, error); |
| 594 |
if (part_num == -1) |
| 595 |
return NULL; |
| 596 |
|
| 597 |
/* first partition in fdisk is 0 */ |
| 598 |
part_num--; |
| 599 |
|
| 600 |
cxt = get_device_context (disk, TRUE, error); |
| 601 |
if (!cxt) { |
| 602 |
/* error is already populated */ |
| 603 |
return NULL; |
| 604 |
} |
| 605 |
|
| 606 |
status = fdisk_get_partition (cxt, part_num, &pa); |
| 607 |
if (status != 0) { |
| 608 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 609 |
"Failed to get partition %d on device '%s'", part_num, disk); |
| 610 |
close_context (cxt); |
| 611 |
return NULL; |
| 612 |
} |
| 613 |
|
| 614 |
ret = get_part_spec_fdisk (cxt, pa, error); |
| 615 |
|
| 616 |
fdisk_unref_partition (pa); |
| 617 |
close_context (cxt); |
| 618 |
|
| 619 |
return ret; |
| 620 |
} |
| 621 |
|
| 622 |
static BDPartSpec** get_disk_parts (const gchar *disk, gboolean parts, gboolean freespaces, gboolean metadata, GError **error) { |
| 623 |
struct fdisk_context *cxt = NULL; |
| 624 |
struct fdisk_table *table = NULL; |
| 625 |
struct fdisk_partition *pa = NULL; |
| 626 |
struct fdisk_iter *itr = NULL; |
| 627 |
BDPartSpec *spec = NULL; |
| 628 |
BDPartSpec *prev_spec = NULL; |
| 629 |
GPtrArray *array = NULL; |
| 630 |
gint status = 0; |
| 631 |
|
| 632 |
cxt = get_device_context (disk, TRUE, error); |
| 633 |
if (!cxt) { |
| 634 |
/* error is already populated */ |
| 635 |
return NULL; |
| 636 |
} |
| 637 |
|
| 638 |
table = fdisk_new_table (); |
| 639 |
if (!table) { |
| 640 |
g_set_error_literal (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 641 |
"Failed to create a new table"); |
| 642 |
close_context (cxt); |
| 643 |
return NULL; |
| 644 |
} |
| 645 |
|
| 646 |
itr = fdisk_new_iter (FDISK_ITER_FORWARD); |
| 647 |
if (!itr) { |
| 648 |
g_set_error_literal (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 649 |
"Failed to create a new iterator"); |
| 650 |
close_context (cxt); |
| 651 |
return NULL; |
| 652 |
} |
| 653 |
|
| 654 |
if (parts) { |
| 655 |
status = fdisk_get_partitions (cxt, &table); |
| 656 |
if (status != 0) { |
| 657 |
g_set_error_literal (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 658 |
"Failed to get partitions"); |
| 659 |
fdisk_free_iter (itr); |
| 660 |
fdisk_unref_table (table); |
| 661 |
close_context (cxt); |
| 662 |
return NULL; |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
if (freespaces) { |
| 667 |
status = fdisk_get_freespaces (cxt, &table); |
| 668 |
if (status != 0) { |
| 669 |
g_set_error_literal (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 670 |
"Failed to get free spaces"); |
| 671 |
fdisk_free_iter (itr); |
| 672 |
fdisk_unref_table (table); |
| 673 |
close_context (cxt); |
| 674 |
return NULL; |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
/* sort partitions by start */ |
| 679 |
status = fdisk_table_sort_partitions (table, fdisk_partition_cmp_start); |
| 680 |
if (status != 0) { |
| 681 |
g_set_error_literal (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 682 |
"Failed to sort partitions"); |
| 683 |
fdisk_free_iter (itr); |
| 684 |
fdisk_unref_table (table); |
| 685 |
close_context (cxt); |
| 686 |
return NULL; |
| 687 |
} |
| 688 |
|
| 689 |
array = g_ptr_array_new_with_free_func ((GDestroyNotify) (void *) bd_part_spec_free); |
| 690 |
|
| 691 |
while (fdisk_table_next_partition (table, itr, &pa) == 0) { |
| 692 |
spec = get_part_spec_fdisk (cxt, pa, error); |
| 693 |
if (!spec) { |
| 694 |
g_ptr_array_free (array, TRUE); |
| 695 |
fdisk_free_iter (itr); |
| 696 |
fdisk_unref_table (table); |
| 697 |
close_context (cxt); |
| 698 |
return NULL; |
| 699 |
} |
| 700 |
|
| 701 |
/* libfdisk doesn't have a special partition for metadata so we need to add |
| 702 |
a special metadata partition to the "empty" spaces between partitions |
| 703 |
and free spaces to mimic behaviour of parted |
| 704 |
metadata partitions should be present in the extended partition in front |
| 705 |
of every logical partition */ |
| 706 |
if (prev_spec && metadata) { |
| 707 |
if ((spec->start > prev_spec->start + prev_spec->size) || |
| 708 |
(prev_spec->type == BD_PART_TYPE_EXTENDED && spec->start > prev_spec->start) ) { |
| 709 |
BDPartSpec *ext_meta = g_new0 (BDPartSpec, 1); |
| 710 |
ext_meta->name = NULL; |
| 711 |
ext_meta->path = NULL; |
| 712 |
|
| 713 |
if (prev_spec->type == BD_PART_TYPE_EXTENDED) { |
| 714 |
ext_meta->start = prev_spec->start; |
| 715 |
ext_meta->size = spec->start - ext_meta->start; |
| 716 |
ext_meta->type = BD_PART_TYPE_METADATA | BD_PART_TYPE_LOGICAL; |
| 717 |
} else { |
| 718 |
ext_meta->start = prev_spec->start + prev_spec->size; |
| 719 |
ext_meta->size = spec->start - ext_meta->start; |
| 720 |
if (spec->type & BD_PART_TYPE_LOGICAL) |
| 721 |
ext_meta->type = BD_PART_TYPE_METADATA | BD_PART_TYPE_LOGICAL; |
| 722 |
else |
| 723 |
ext_meta->type = BD_PART_TYPE_METADATA; |
| 724 |
} |
| 725 |
ext_meta->type_guid = NULL; |
| 726 |
|
| 727 |
g_ptr_array_add (array, ext_meta); |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
prev_spec = spec; |
| 732 |
g_ptr_array_add (array, spec); |
| 733 |
} |
| 734 |
|
| 735 |
fdisk_free_iter (itr); |
| 736 |
fdisk_unref_table (table); |
| 737 |
close_context (cxt); |
| 738 |
|
| 739 |
g_ptr_array_add (array, NULL); |
| 740 |
return (BDPartSpec **) g_ptr_array_free (array, FALSE); |
| 741 |
} |
| 742 |
|
| 743 |
/** |
| 744 |
* bd_part_get_part_by_pos: |
| 745 |
* @disk: disk to remove the partition from |
| 746 |
* @position: position (in bytes) determining the partition |
| 747 |
* @error: (out) (optional): place to store error (if any) |
| 748 |
* |
| 749 |
* Returns: (transfer full): spec of the partition from @disk spanning over the @position or %NULL if no such |
| 750 |
* partition exists or in case of error (@error is set) |
| 751 |
* |
| 752 |
* Tech category: %BD_PART_TECH_MODE_QUERY_PART + the tech according to the partition table type |
| 753 |
*/ |
| 754 |
BDPartSpec* bd_part_get_part_by_pos (const gchar *disk, guint64 position, GError **error) { |
| 755 |
BDPartSpec **parts = NULL; |
| 756 |
BDPartSpec *ret = NULL; |
| 757 |
|
| 758 |
parts = get_disk_parts (disk, TRUE, TRUE, TRUE, error); |
| 759 |
if (!parts) |
| 760 |
return NULL; |
| 761 |
|
| 762 |
for (BDPartSpec **parts_p = parts; *parts_p; parts_p++) { |
| 763 |
if ((*parts_p)->start <= position && ((*parts_p)->start + (*parts_p)->size) > position) { |
| 764 |
if ((*parts_p)->type == BD_PART_TYPE_EXTENDED) { |
| 765 |
/* we don't want to return extended partition here -- there |
| 766 |
is either another logical or free space at this position */ |
| 767 |
continue; |
| 768 |
} |
| 769 |
|
| 770 |
ret = *parts_p; |
| 771 |
break; |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
/* free the array except the selected element */ |
| 776 |
for (BDPartSpec **parts_p = parts; *parts_p; parts_p++) |
| 777 |
if (*parts_p != ret) |
| 778 |
bd_part_spec_free (*parts_p); |
| 779 |
g_free (parts); |
| 780 |
|
| 781 |
return ret; |
| 782 |
} |
| 783 |
|
| 784 |
/** |
| 785 |
* bd_part_get_disk_spec: |
| 786 |
* @disk: disk to get information about |
| 787 |
* @error: (out) (optional): place to store error (if any) |
| 788 |
* |
| 789 |
* Returns: (transfer full): information about the given @disk or %NULL (in case of error) |
| 790 |
* |
| 791 |
* Tech category: %BD_PART_TECH_MODE_QUERY_TABLE + the tech according to the partition table type |
| 792 |
*/ |
| 793 |
BDPartDiskSpec* bd_part_get_disk_spec (const gchar *disk, GError **error) { |
| 794 |
struct fdisk_context *cxt = NULL; |
| 795 |
struct fdisk_label *lb = NULL; |
| 796 |
BDPartDiskSpec *ret = NULL; |
| 797 |
const gchar *label_name = NULL; |
| 798 |
BDPartTableType type = BD_PART_TABLE_UNDEF; |
| 799 |
gboolean found = FALSE; |
| 800 |
|
| 801 |
cxt = get_device_context (disk, TRUE, error); |
| 802 |
if (!cxt) { |
| 803 |
/* error is already populated */ |
| 804 |
return NULL; |
| 805 |
} |
| 806 |
|
| 807 |
ret = g_new0 (BDPartDiskSpec, 1); |
| 808 |
ret->path = g_strdup (fdisk_get_devname (cxt)); |
| 809 |
ret->sector_size = (guint64) fdisk_get_sector_size (cxt); |
| 810 |
ret->size = fdisk_get_nsectors (cxt) * ret->sector_size; |
| 811 |
|
| 812 |
lb = fdisk_get_label (cxt, NULL); |
| 813 |
if (lb) { |
| 814 |
label_name = fdisk_label_get_name (lb); |
| 815 |
for (type=BD_PART_TABLE_MSDOS; !found && type < BD_PART_TABLE_UNDEF; type++) { |
| 816 |
if (g_strcmp0 (label_name, table_type_str[type]) == 0) { |
| 817 |
ret->table_type = type; |
| 818 |
found = TRUE; |
| 819 |
} |
| 820 |
} |
| 821 |
if (!found) |
| 822 |
ret->table_type = BD_PART_TABLE_UNDEF; |
| 823 |
} else |
| 824 |
ret->table_type = BD_PART_TABLE_UNDEF; |
| 825 |
|
| 826 |
close_context (cxt); |
| 827 |
|
| 828 |
return ret; |
| 829 |
} |
| 830 |
|
| 831 |
/** |
| 832 |
* bd_part_get_disk_parts: |
| 833 |
* @disk: disk to get information about partitions for |
| 834 |
* @error: (out) (optional): place to store error (if any) |
| 835 |
* |
| 836 |
* Returns: (transfer full) (array zero-terminated=1): specs of the partitions from @disk or %NULL in case of error |
| 837 |
* |
| 838 |
* Tech category: %BD_PART_TECH_MODE_QUERY_TABLE + the tech according to the partition table type |
| 839 |
*/ |
| 840 |
BDPartSpec** bd_part_get_disk_parts (const gchar *disk, GError **error) { |
| 841 |
return get_disk_parts (disk, TRUE, FALSE, FALSE, error); |
| 842 |
} |
| 843 |
|
| 844 |
/** |
| 845 |
* bd_part_get_disk_free_regions: |
| 846 |
* @disk: disk to get free regions for |
| 847 |
* @error: (out) (optional): place to store error (if any) |
| 848 |
* |
| 849 |
* Returns: (transfer full) (array zero-terminated=1): specs of the free regions from @disk or %NULL in case of error |
| 850 |
* |
| 851 |
* Tech category: %BD_PART_TECH_MODE_QUERY_TABLE + the tech according to the partition table type |
| 852 |
*/ |
| 853 |
BDPartSpec** bd_part_get_disk_free_regions (const gchar *disk, GError **error) { |
| 854 |
return get_disk_parts (disk, FALSE, TRUE, FALSE, error); |
| 855 |
} |
| 856 |
|
| 857 |
/** |
| 858 |
* bd_part_get_best_free_region: |
| 859 |
* @disk: disk to get the best free region for |
| 860 |
* @type: type of the partition that is planned to be added |
| 861 |
* @size: size of the partition to be added |
| 862 |
* @error: (out) (optional): place to store error (if any) |
| 863 |
* |
| 864 |
* Returns: (transfer full): spec of the best free region on @disk for a new partition of type @type |
| 865 |
* with the size of @size or %NULL if there is none such region or if |
| 866 |
* there was an error (@error gets populated) |
| 867 |
* |
| 868 |
* Note: For the @type %BD_PART_TYPE_NORMAL, the smallest possible space that *is not* in an extended partition |
| 869 |
* is found. For the @type %BD_PART_TYPE_LOGICAL, the smallest possible space that *is* in an extended |
| 870 |
* partition is found. For %BD_PART_TYPE_EXTENDED, the biggest possible space is found as long as there |
| 871 |
* is no other extended partition (there can only be one). |
| 872 |
* |
| 873 |
* Tech category: %BD_PART_TECH_MODE_QUERY_TABLE + the tech according to the partition table type |
| 874 |
*/ |
| 875 |
BDPartSpec* bd_part_get_best_free_region (const gchar *disk, BDPartType type, guint64 size, GError **error) { |
| 876 |
BDPartSpec **free_regs = NULL; |
| 877 |
BDPartSpec **free_reg_p = NULL; |
| 878 |
BDPartSpec *ret = NULL; |
| 879 |
|
| 880 |
free_regs = bd_part_get_disk_free_regions (disk, error); |
| 881 |
if (!free_regs) |
| 882 |
/* error should be populated */ |
| 883 |
return NULL; |
| 884 |
if (!(*free_regs)) { |
| 885 |
/* no free regions */ |
| 886 |
g_free (free_regs); |
| 887 |
return NULL; |
| 888 |
} |
| 889 |
|
| 890 |
if (type == BD_PART_TYPE_NORMAL) { |
| 891 |
for (free_reg_p=free_regs; *free_reg_p; free_reg_p++) { |
| 892 |
/* check if it has enough space and is not inside an extended partition */ |
| 893 |
if ((*free_reg_p)->size > size && !((*free_reg_p)->type & BD_PART_TYPE_LOGICAL)) |
| 894 |
/* if it is the first that would fit or if it is smaller than |
| 895 |
what we found earlier, it is a better match */ |
| 896 |
if (!ret || ((*free_reg_p)->size < ret->size)) |
| 897 |
ret = *free_reg_p; |
| 898 |
} |
| 899 |
} else if (type == BD_PART_TYPE_EXTENDED) { |
| 900 |
for (free_reg_p=free_regs; *free_reg_p; free_reg_p++) { |
| 901 |
/* if there already is an extended partition, there cannot be another one */ |
| 902 |
if ((*free_reg_p)->type & BD_PART_TYPE_LOGICAL) { |
| 903 |
for (free_reg_p=free_regs; *free_reg_p; free_reg_p++) |
| 904 |
bd_part_spec_free (*free_reg_p); |
| 905 |
g_free (free_regs); |
| 906 |
return NULL; |
| 907 |
} |
| 908 |
/* check if it has enough space */ |
| 909 |
if ((*free_reg_p)->size > size) |
| 910 |
/* if it is the first that would fit or if it is bigger than |
| 911 |
what we found earlier, it is a better match */ |
| 912 |
if (!ret || ((*free_reg_p)->size > ret->size)) |
| 913 |
ret = *free_reg_p; |
| 914 |
} |
| 915 |
} else if (type == BD_PART_TYPE_LOGICAL) { |
| 916 |
for (free_reg_p=free_regs; *free_reg_p; free_reg_p++) { |
| 917 |
/* check if it has enough space and is inside an extended partition */ |
| 918 |
if ((*free_reg_p)->size > size && ((*free_reg_p)->type & BD_PART_TYPE_LOGICAL)) |
| 919 |
/* if it is the first that would fit or if it is smaller than |
| 920 |
what we found earlier, it is a better match */ |
| 921 |
if (!ret || ((*free_reg_p)->size < ret->size)) |
| 922 |
ret = *free_reg_p; |
| 923 |
} |
| 924 |
} |
| 925 |
|
| 926 |
/* free all the other specs and return the best one */ |
| 927 |
for (free_reg_p=free_regs; *free_reg_p; free_reg_p++) |
| 928 |
if (*free_reg_p != ret) |
| 929 |
bd_part_spec_free (*free_reg_p); |
| 930 |
g_free (free_regs); |
| 931 |
|
| 932 |
return ret; |
| 933 |
} |
| 934 |
|
| 935 |
/** |
| 936 |
* bd_part_create_part: |
| 937 |
* @disk: disk to create partition on |
| 938 |
* @type: type of the partition to create (if %BD_PART_TYPE_REQ_NEXT, the |
| 939 |
* partition type will be determined automatically based on the existing |
| 940 |
* partitions) |
| 941 |
* @start: where the partition should start (i.e. offset from the disk start) |
| 942 |
* @size: desired size of the partition (if 0, a max-sized partition is created) |
| 943 |
* @align: alignment to use for the partition |
| 944 |
* @error: (out) (optional): place to store error (if any) |
| 945 |
* |
| 946 |
* Returns: (transfer full): specification of the created partition or %NULL in case of error |
| 947 |
* |
| 948 |
* NOTE: The resulting partition may start at a different position than given by |
| 949 |
* @start and can have different size than @size due to alignment. |
| 950 |
* |
| 951 |
* Tech category: %BD_PART_TECH_MODE_MODIFY_TABLE + the tech according to the partition table type |
| 952 |
*/ |
| 953 |
BDPartSpec* bd_part_create_part (const gchar *disk, BDPartTypeReq type, guint64 start, guint64 size, BDPartAlign align, GError **error) { |
| 954 |
struct fdisk_context *cxt = NULL; |
| 955 |
struct fdisk_partition *npa = NULL; |
| 956 |
gint status = 0; |
| 957 |
BDPartSpec *ret = NULL; |
| 958 |
guint64 progress_id = 0; |
| 959 |
gchar *msg = NULL; |
| 960 |
guint64 sector_size = 0; |
| 961 |
guint64 grain_size = 0; |
| 962 |
guint64 end = 0; |
| 963 |
struct fdisk_parttype *ptype = NULL; |
| 964 |
struct fdisk_label *lbl = NULL; |
| 965 |
struct fdisk_table *table = NULL; |
| 966 |
struct fdisk_iter *iter = NULL; |
| 967 |
struct fdisk_partition *pa = NULL; |
| 968 |
struct fdisk_partition *epa = NULL; |
| 969 |
struct fdisk_partition *in_pa = NULL; |
| 970 |
struct fdisk_partition *n_epa = NULL; |
| 971 |
guint n_parts = 0; |
| 972 |
gboolean on_gpt = FALSE; |
| 973 |
size_t partno = 0; |
| 974 |
gchar *ppath = NULL; |
| 975 |
gboolean new_extended = FALSE; |
| 976 |
GError *l_error = NULL; |
| 977 |
|
| 978 |
msg = g_strdup_printf ("Started adding partition to '%s'", disk); |
| 979 |
progress_id = bd_utils_report_started (msg); |
| 980 |
g_free (msg); |
| 981 |
|
| 982 |
cxt = get_device_context (disk, FALSE, &l_error); |
| 983 |
if (!cxt) { |
| 984 |
/* error is already populated */ |
| 985 |
bd_utils_report_finished (progress_id, l_error->message); |
| 986 |
g_propagate_error (error, l_error); |
| 987 |
return NULL; |
| 988 |
} |
| 989 |
|
| 990 |
status = fdisk_get_partitions (cxt, &table); |
| 991 |
if (status != 0) { |
| 992 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 993 |
"Failed to get existing partitions on the device: %s", strerror_l (-status, c_locale)); |
| 994 |
close_context (cxt); |
| 995 |
bd_utils_report_finished (progress_id, l_error->message); |
| 996 |
g_propagate_error (error, l_error); |
| 997 |
return NULL; |
| 998 |
} |
| 999 |
|
| 1000 |
npa = fdisk_new_partition (); |
| 1001 |
if (!npa) { |
| 1002 |
g_set_error_literal (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1003 |
"Failed to create new partition object"); |
| 1004 |
fdisk_unref_table (table); |
| 1005 |
close_context (cxt); |
| 1006 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1007 |
g_propagate_error (error, l_error); |
| 1008 |
return NULL; |
| 1009 |
} |
| 1010 |
|
| 1011 |
sector_size = (guint64) fdisk_get_sector_size (cxt); |
| 1012 |
grain_size = (guint64) fdisk_get_grain_size (cxt); |
| 1013 |
|
| 1014 |
if (align == BD_PART_ALIGN_NONE) |
| 1015 |
grain_size = sector_size; |
| 1016 |
else if (align == BD_PART_ALIGN_MINIMAL) |
| 1017 |
grain_size = (guint64) fdisk_get_minimal_iosize (cxt); |
| 1018 |
/* else OPTIMAL or unknown -> nothing to do */ |
| 1019 |
|
| 1020 |
status = fdisk_save_user_grain (cxt, grain_size); |
| 1021 |
if (status != 0) { |
| 1022 |
g_set_error_literal (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1023 |
"Failed to setup alignment"); |
| 1024 |
fdisk_unref_table (table); |
| 1025 |
close_context (cxt); |
| 1026 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1027 |
g_propagate_error (error, l_error); |
| 1028 |
return NULL; |
| 1029 |
} |
| 1030 |
|
| 1031 |
/* this is needed so that the saved grain size from above becomes |
| 1032 |
* effective */ |
| 1033 |
status = fdisk_reset_device_properties (cxt); |
| 1034 |
if (status != 0) { |
| 1035 |
g_set_error_literal (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1036 |
"Failed to setup alignment"); |
| 1037 |
fdisk_unref_table (table); |
| 1038 |
close_context (cxt); |
| 1039 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1040 |
g_propagate_error (error, l_error); |
| 1041 |
return NULL; |
| 1042 |
} |
| 1043 |
|
| 1044 |
/* set first usable sector to 1 for none and minimal alignments |
| 1045 |
we need to set this here, because fdisk_reset_device_properties */ |
| 1046 |
if (align == BD_PART_ALIGN_NONE || align == BD_PART_ALIGN_MINIMAL) |
| 1047 |
fdisk_set_first_lba (cxt, 1); |
| 1048 |
|
| 1049 |
grain_size = (guint64) fdisk_get_grain_size (cxt); |
| 1050 |
|
| 1051 |
/* align start up to sectors, we will align it further based on grain_size |
| 1052 |
using libfdisk later */ |
| 1053 |
start = (start + sector_size - 1) / sector_size; |
| 1054 |
|
| 1055 |
/* start on sector 0 doesn't work with libfdisk alignment -- everything |
| 1056 |
else is properly aligned, but zero is aligned to zero */ |
| 1057 |
if (start == 0) |
| 1058 |
start = 1; |
| 1059 |
|
| 1060 |
start = fdisk_align_lba (cxt, (fdisk_sector_t) start, FDISK_ALIGN_UP); |
| 1061 |
|
| 1062 |
if (size == 0) |
| 1063 |
/* no size specified, set the end to default (maximum) */ |
| 1064 |
fdisk_partition_end_follow_default (npa, 1); |
| 1065 |
else { |
| 1066 |
/* align size down */ |
| 1067 |
size = (size / grain_size) * grain_size; |
| 1068 |
size = size / sector_size; |
| 1069 |
|
| 1070 |
/* use libfdisk to align the end sector */ |
| 1071 |
end = start + size; |
| 1072 |
end = fdisk_align_lba (cxt, (fdisk_sector_t) end, FDISK_ALIGN_DOWN); |
| 1073 |
size = end - start; |
| 1074 |
|
| 1075 |
if (fdisk_partition_set_size (npa, size) != 0) { |
| 1076 |
g_set_error_literal (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1077 |
"Failed to set partition size"); |
| 1078 |
fdisk_unref_table (table); |
| 1079 |
fdisk_unref_partition (npa); |
| 1080 |
close_context (cxt); |
| 1081 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1082 |
g_propagate_error (error, l_error); |
| 1083 |
return NULL; |
| 1084 |
} |
| 1085 |
} |
| 1086 |
|
| 1087 |
fdisk_partition_partno_follow_default (npa, 1); |
| 1088 |
lbl = fdisk_get_label (cxt, NULL); |
| 1089 |
on_gpt = g_strcmp0 (fdisk_label_get_name (lbl), "gpt") == 0; |
| 1090 |
|
| 1091 |
/* GPT is easy, all partitions are the same (NORMAL) */ |
| 1092 |
if (on_gpt && type == BD_PART_TYPE_REQ_NEXT) |
| 1093 |
type = BD_PART_TYPE_REQ_NORMAL; |
| 1094 |
|
| 1095 |
if (on_gpt && type != BD_PART_TYPE_REQ_NORMAL) { |
| 1096 |
g_set_error_literal (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1097 |
"Only normal partitions are supported on GPT."); |
| 1098 |
fdisk_unref_table (table); |
| 1099 |
fdisk_unref_partition (npa); |
| 1100 |
close_context (cxt); |
| 1101 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1102 |
g_propagate_error (error, l_error); |
| 1103 |
return NULL; |
| 1104 |
} |
| 1105 |
|
| 1106 |
/* on DOS we may have to decide if requested */ |
| 1107 |
if (type == BD_PART_TYPE_REQ_NEXT) { |
| 1108 |
iter = fdisk_new_iter (FDISK_ITER_FORWARD); |
| 1109 |
while (fdisk_table_next_partition (table, iter, &pa) == 0) { |
| 1110 |
if (fdisk_partition_is_freespace (pa)) |
| 1111 |
continue; |
| 1112 |
if (!epa && fdisk_partition_is_container (pa)) |
| 1113 |
epa = pa; |
| 1114 |
if (!in_pa && fdisk_partition_has_start (pa) && fdisk_partition_has_size (pa) && |
| 1115 |
fdisk_partition_get_start (pa) <= start && |
| 1116 |
(start < (fdisk_partition_get_start (pa) + fdisk_partition_get_size (pa)))) |
| 1117 |
in_pa = pa; |
| 1118 |
n_parts++; |
| 1119 |
} |
| 1120 |
if (in_pa) { |
| 1121 |
if (epa == in_pa) |
| 1122 |
/* creating a partition inside an extended partition -> LOGICAL */ |
| 1123 |
type = BD_PART_TYPE_REQ_LOGICAL; |
| 1124 |
else { |
| 1125 |
/* trying to create a partition inside an existing one, but not |
| 1126 |
an extended one -> error */ |
| 1127 |
g_set_error_literal (&l_error, BD_PART_ERROR, BD_PART_ERROR_INVAL, |
| 1128 |
"Cannot create a partition inside an existing non-extended one"); |
| 1129 |
fdisk_unref_partition (npa); |
| 1130 |
fdisk_free_iter (iter); |
| 1131 |
fdisk_unref_table (table); |
| 1132 |
close_context (cxt); |
| 1133 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1134 |
g_propagate_error (error, l_error); |
| 1135 |
return NULL; |
| 1136 |
} |
| 1137 |
} else if (epa) |
| 1138 |
/* there's an extended partition already and we are creating a new |
| 1139 |
one outside of it */ |
| 1140 |
type = BD_PART_TYPE_REQ_NORMAL; |
| 1141 |
else if (n_parts == 3) { |
| 1142 |
/* already 3 primary partitions -> create an extended partition of |
| 1143 |
the biggest possible size and a logical partition as requested in |
| 1144 |
it */ |
| 1145 |
new_extended = TRUE; |
| 1146 |
n_epa = fdisk_new_partition (); |
| 1147 |
if (!n_epa) { |
| 1148 |
g_set_error_literal (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1149 |
"Failed to create new partition object"); |
| 1150 |
fdisk_unref_partition (npa); |
| 1151 |
close_context (cxt); |
| 1152 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1153 |
g_propagate_error (error, l_error); |
| 1154 |
return NULL; |
| 1155 |
} |
| 1156 |
if (fdisk_partition_set_start (n_epa, start) != 0) { |
| 1157 |
g_set_error_literal (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1158 |
"Failed to set partition start"); |
| 1159 |
fdisk_unref_partition (n_epa); |
| 1160 |
fdisk_unref_partition (npa); |
| 1161 |
fdisk_free_iter (iter); |
| 1162 |
fdisk_unref_table (table); |
| 1163 |
close_context (cxt); |
| 1164 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1165 |
g_propagate_error (error, l_error); |
| 1166 |
return NULL; |
| 1167 |
} |
| 1168 |
|
| 1169 |
fdisk_partition_partno_follow_default (n_epa, 1); |
| 1170 |
|
| 1171 |
status = fdisk_partition_next_partno (npa, cxt, &partno); |
| 1172 |
if (status != 0) { |
| 1173 |
g_set_error_literal (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1174 |
"Failed to get new extended partition number"); |
| 1175 |
fdisk_unref_partition (npa); |
| 1176 |
close_context (cxt); |
| 1177 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1178 |
g_propagate_error (error, l_error); |
| 1179 |
return NULL; |
| 1180 |
} |
| 1181 |
|
| 1182 |
status = fdisk_partition_set_partno (npa, partno); |
| 1183 |
if (status != 0) { |
| 1184 |
g_set_error_literal (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1185 |
"Failed to set new extended partition number"); |
| 1186 |
fdisk_unref_partition (npa); |
| 1187 |
close_context (cxt); |
| 1188 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1189 |
g_propagate_error (error, l_error); |
| 1190 |
return NULL; |
| 1191 |
} |
| 1192 |
|
| 1193 |
/* set the end to default (maximum) */ |
| 1194 |
fdisk_partition_end_follow_default (n_epa, 1); |
| 1195 |
|
| 1196 |
/* "05" for extended partition */ |
| 1197 |
ptype = fdisk_label_parse_parttype (fdisk_get_label (cxt, NULL), "05"); |
| 1198 |
if (fdisk_partition_set_type (n_epa, ptype) != 0) { |
| 1199 |
g_set_error_literal (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1200 |
"Failed to set partition type"); |
| 1201 |
fdisk_unref_partition (n_epa); |
| 1202 |
fdisk_unref_partition (npa); |
| 1203 |
fdisk_free_iter (iter); |
| 1204 |
fdisk_unref_table (table); |
| 1205 |
close_context (cxt); |
| 1206 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1207 |
g_propagate_error (error, l_error); |
| 1208 |
return NULL; |
| 1209 |
} |
| 1210 |
fdisk_unref_parttype (ptype); |
| 1211 |
|
| 1212 |
status = fdisk_add_partition (cxt, n_epa, NULL); |
| 1213 |
fdisk_unref_partition (n_epa); |
| 1214 |
if (status != 0) { |
| 1215 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1216 |
"Failed to add new partition to the table: %s", strerror_l (-status, c_locale)); |
| 1217 |
fdisk_unref_partition (npa); |
| 1218 |
fdisk_free_iter (iter); |
| 1219 |
fdisk_unref_table (table); |
| 1220 |
close_context (cxt); |
| 1221 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1222 |
g_propagate_error (error, l_error); |
| 1223 |
return NULL; |
| 1224 |
} |
| 1225 |
/* shift the start 2 MiB further as that's where the first logical |
| 1226 |
partition inside an extended partition can start */ |
| 1227 |
start += (2 MiB / sector_size); |
| 1228 |
type = BD_PART_TYPE_REQ_LOGICAL; |
| 1229 |
} else |
| 1230 |
/* no extended partition and not 3 primary partitions -> just create |
| 1231 |
another primary (NORMAL) partition*/ |
| 1232 |
type = BD_PART_TYPE_REQ_NORMAL; |
| 1233 |
|
| 1234 |
fdisk_free_iter (iter); |
| 1235 |
} |
| 1236 |
|
| 1237 |
if (type == BD_PART_TYPE_REQ_EXTENDED) { |
| 1238 |
new_extended = TRUE; |
| 1239 |
/* "05" for extended partition */ |
| 1240 |
ptype = fdisk_label_parse_parttype (fdisk_get_label (cxt, NULL), "05"); |
| 1241 |
if (fdisk_partition_set_type (npa, ptype) != 0) { |
| 1242 |
g_set_error_literal (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1243 |
"Failed to set partition type"); |
| 1244 |
fdisk_unref_table (table); |
| 1245 |
fdisk_unref_partition (npa); |
| 1246 |
close_context (cxt); |
| 1247 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1248 |
g_propagate_error (error, l_error); |
| 1249 |
return NULL; |
| 1250 |
} |
| 1251 |
|
| 1252 |
fdisk_unref_parttype (ptype); |
| 1253 |
} |
| 1254 |
|
| 1255 |
if (fdisk_partition_set_start (npa, start) != 0) { |
| 1256 |
g_set_error_literal (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1257 |
"Failed to set partition start"); |
| 1258 |
fdisk_unref_table (table); |
| 1259 |
fdisk_unref_partition (npa); |
| 1260 |
close_context (cxt); |
| 1261 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1262 |
g_propagate_error (error, l_error); |
| 1263 |
return NULL; |
| 1264 |
} |
| 1265 |
|
| 1266 |
if (type == BD_PART_TYPE_REQ_LOGICAL) { |
| 1267 |
/* next_partno doesn't work for logical partitions, for these the |
| 1268 |
current maximal number of partitions supported by the label |
| 1269 |
is the next (logical) partition number */ |
| 1270 |
partno = fdisk_get_npartitions (cxt); |
| 1271 |
|
| 1272 |
} else { |
| 1273 |
status = fdisk_partition_next_partno (npa, cxt, &partno); |
| 1274 |
if (status != 0) { |
| 1275 |
g_set_error_literal (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1276 |
"Failed to get new partition number"); |
| 1277 |
fdisk_unref_table (table); |
| 1278 |
fdisk_unref_partition (npa); |
| 1279 |
close_context (cxt); |
| 1280 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1281 |
g_propagate_error (error, l_error); |
| 1282 |
return NULL; |
| 1283 |
} |
| 1284 |
} |
| 1285 |
|
| 1286 |
status = fdisk_partition_set_partno (npa, partno); |
| 1287 |
if (status != 0) { |
| 1288 |
g_set_error_literal (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1289 |
"Failed to set new partition number"); |
| 1290 |
fdisk_unref_table (table); |
| 1291 |
fdisk_unref_partition (npa); |
| 1292 |
close_context (cxt); |
| 1293 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1294 |
g_propagate_error (error, l_error); |
| 1295 |
return NULL; |
| 1296 |
} |
| 1297 |
|
| 1298 |
status = fdisk_add_partition (cxt, npa, NULL); |
| 1299 |
if (status != 0) { |
| 1300 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1301 |
"Failed to add new partition to the table: %s", strerror_l (-status, c_locale)); |
| 1302 |
fdisk_unref_table (table); |
| 1303 |
fdisk_unref_partition (npa); |
| 1304 |
close_context (cxt); |
| 1305 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1306 |
g_propagate_error (error, l_error); |
| 1307 |
return NULL; |
| 1308 |
} |
| 1309 |
|
| 1310 |
/* for new extended partition we need to force reread whole partition table with |
| 1311 |
libfdisk < 2.36.1 */ |
| 1312 |
if (!write_label (cxt, table, disk, new_extended && fdisk_version < 2361, &l_error)) { |
| 1313 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1314 |
g_propagate_error (error, l_error); |
| 1315 |
fdisk_unref_table (table); |
| 1316 |
fdisk_unref_partition (npa); |
| 1317 |
close_context (cxt); |
| 1318 |
return NULL; |
| 1319 |
} |
| 1320 |
|
| 1321 |
if (fdisk_partition_has_partno (npa)) { |
| 1322 |
if (isdigit (disk[strlen (disk) - 1])) |
| 1323 |
ppath = g_strdup_printf ("%sp%zu", disk, fdisk_partition_get_partno (npa) + 1); |
| 1324 |
else |
| 1325 |
ppath = g_strdup_printf ("%s%zu", disk, fdisk_partition_get_partno (npa) + 1); |
| 1326 |
} |
| 1327 |
|
| 1328 |
/* close the context now, we no longer need it */ |
| 1329 |
fdisk_unref_table (table); |
| 1330 |
fdisk_unref_partition (npa); |
| 1331 |
close_context (cxt); |
| 1332 |
|
| 1333 |
/* the in-memory model of the new partition is not updated, we need to |
| 1334 |
read the spec manually |
| 1335 |
if we get NULL and error here, just propagate it further */ |
| 1336 |
ret = bd_part_get_part_spec (disk, ppath, error); |
| 1337 |
g_free (ppath); |
| 1338 |
|
| 1339 |
bd_utils_report_finished (progress_id, "Completed"); |
| 1340 |
|
| 1341 |
return ret; |
| 1342 |
} |
| 1343 |
|
| 1344 |
/** |
| 1345 |
* bd_part_delete_part: |
| 1346 |
* @disk: disk to remove the partition from |
| 1347 |
* @part: partition to remove |
| 1348 |
* @error: (out) (optional): place to store error (if any) |
| 1349 |
* |
| 1350 |
* Returns: whether the @part partition was successfully deleted from @disk |
| 1351 |
* |
| 1352 |
* Tech category: %BD_PART_TECH_MODE_MODIFY_TABLE + the tech according to the partition table type |
| 1353 |
*/ |
| 1354 |
gboolean bd_part_delete_part (const gchar *disk, const gchar *part, GError **error) { |
| 1355 |
gint part_num = 0; |
| 1356 |
struct fdisk_context *cxt = NULL; |
| 1357 |
struct fdisk_table *table = NULL; |
| 1358 |
gint ret = 0; |
| 1359 |
guint64 progress_id = 0; |
| 1360 |
gchar *msg = NULL; |
| 1361 |
GError *l_error = NULL; |
| 1362 |
|
| 1363 |
msg = g_strdup_printf ("Started deleting partition '%s'", part); |
| 1364 |
progress_id = bd_utils_report_started (msg); |
| 1365 |
g_free (msg); |
| 1366 |
|
| 1367 |
part_num = get_part_num (part, &l_error); |
| 1368 |
if (part_num == -1) { |
| 1369 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1370 |
g_propagate_error (error, l_error); |
| 1371 |
return FALSE; |
| 1372 |
} |
| 1373 |
|
| 1374 |
/* /dev/sda1 is the partition number 0 in libfdisk */ |
| 1375 |
part_num--; |
| 1376 |
cxt = get_device_context (disk, FALSE, &l_error); |
| 1377 |
if (!cxt) { |
| 1378 |
/* error is already populated */ |
| 1379 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1380 |
g_propagate_error (error, l_error); |
| 1381 |
return FALSE; |
| 1382 |
} |
| 1383 |
|
| 1384 |
ret = fdisk_get_partitions (cxt, &table); |
| 1385 |
if (ret != 0) { |
| 1386 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1387 |
"Failed to get existing partitions on the device: %s", strerror_l (-ret, c_locale)); |
| 1388 |
close_context (cxt); |
| 1389 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1390 |
g_propagate_error (error, l_error); |
| 1391 |
return FALSE; |
| 1392 |
} |
| 1393 |
|
| 1394 |
ret = fdisk_delete_partition (cxt, (size_t) part_num); |
| 1395 |
if (ret != 0) { |
| 1396 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1397 |
"Failed to delete partition '%d' on device '%s': %s", part_num+1, disk, strerror_l (-ret, c_locale)); |
| 1398 |
fdisk_unref_table (table); |
| 1399 |
close_context (cxt); |
| 1400 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1401 |
g_propagate_error (error, l_error); |
| 1402 |
return FALSE; |
| 1403 |
} |
| 1404 |
|
| 1405 |
if (!write_label (cxt, table, disk, FALSE, &l_error)) { |
| 1406 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1407 |
g_propagate_error (error, l_error); |
| 1408 |
fdisk_unref_table (table); |
| 1409 |
close_context (cxt); |
| 1410 |
return FALSE; |
| 1411 |
} |
| 1412 |
|
| 1413 |
fdisk_unref_table (table); |
| 1414 |
close_context (cxt); |
| 1415 |
|
| 1416 |
bd_utils_report_finished (progress_id, "Completed"); |
| 1417 |
|
| 1418 |
return TRUE; |
| 1419 |
} |
| 1420 |
|
| 1421 |
/* get maximal size for partition when resizing |
| 1422 |
* this is a simplified copy of 'resize_get_last_possible' function from |
| 1423 |
* libfdisk which is unfortunately not public |
| 1424 |
*/ |
| 1425 |
static gboolean get_max_part_size (struct fdisk_table *tb, guint partno, guint64 *max_size, GError **error) { |
| 1426 |
struct fdisk_partition *pa = NULL; |
| 1427 |
struct fdisk_partition *cur = NULL; |
| 1428 |
struct fdisk_iter *itr = NULL; |
| 1429 |
guint64 start = 0; |
| 1430 |
gboolean found = FALSE; |
| 1431 |
|
| 1432 |
itr = fdisk_new_iter (FDISK_ITER_FORWARD); |
| 1433 |
if (!itr) { |
| 1434 |
g_set_error_literal (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1435 |
"Failed to create a new iterator"); |
| 1436 |
return FALSE; |
| 1437 |
} |
| 1438 |
|
| 1439 |
cur = fdisk_table_get_partition_by_partno (tb, partno); |
| 1440 |
if (!cur) { |
| 1441 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1442 |
"Failed to locate partition '%d' in table.", partno); |
| 1443 |
fdisk_free_iter (itr); |
| 1444 |
return FALSE; |
| 1445 |
} |
| 1446 |
|
| 1447 |
start = fdisk_partition_get_start (cur); |
| 1448 |
|
| 1449 |
while (!found && fdisk_table_next_partition (tb, itr, &pa) == 0) { |
| 1450 |
if (!fdisk_partition_has_start (pa) || !fdisk_partition_has_size (pa) || |
| 1451 |
(fdisk_partition_is_container (pa) && pa != cur)) { |
| 1452 |
/* partition has no size/start or is an extended partition */ |
| 1453 |
continue; |
| 1454 |
} |
| 1455 |
|
| 1456 |
if (fdisk_partition_is_nested (pa) && fdisk_partition_is_container (cur)) { |
| 1457 |
/* ignore logical partitions inside if we are checking an extended partition */ |
| 1458 |
continue; |
| 1459 |
} |
| 1460 |
|
| 1461 |
if (fdisk_partition_is_nested (cur) && !fdisk_partition_is_nested (pa)) { |
| 1462 |
/* current partition is nested, we are looking for a nested free space */ |
| 1463 |
continue; |
| 1464 |
} |
| 1465 |
|
| 1466 |
if (pa == cur) { |
| 1467 |
/* found our current partition */ |
| 1468 |
found = TRUE; |
| 1469 |
} |
| 1470 |
} |
| 1471 |
|
| 1472 |
if (found && fdisk_table_next_partition (tb, itr, &pa) == 0) { |
| 1473 |
/* check next partition after our current we found */ |
| 1474 |
if (fdisk_partition_is_freespace (pa)) { |
| 1475 |
/* we found a free space after current partition */ |
| 1476 |
if (LIBFDISK_MINOR_VERSION <= 32) |
| 1477 |
/* XXX: older versions of libfdisk doesn't count free space between |
| 1478 |
partitions as usable so we need to do the same here, see |
| 1479 |
https://github.com/karelzak/util-linux/commit/2f35c1ead621f42f32f7777232568cb03185b473 */ |
| 1480 |
*max_size = fdisk_partition_get_size (cur) + fdisk_partition_get_size (pa); |
| 1481 |
else |
| 1482 |
*max_size = fdisk_partition_get_size (pa) - (start - fdisk_partition_get_start (pa)); |
| 1483 |
} |
| 1484 |
} |
| 1485 |
|
| 1486 |
/* no free space found: set max_size to current size */ |
| 1487 |
if (*max_size == 0) |
| 1488 |
*max_size = fdisk_partition_get_size (cur); |
| 1489 |
|
| 1490 |
fdisk_free_iter (itr); |
| 1491 |
return TRUE; |
| 1492 |
} |
| 1493 |
|
| 1494 |
/** |
| 1495 |
* bd_part_resize_part: |
| 1496 |
* @disk: disk containing the partition |
| 1497 |
* @part: partition to resize |
| 1498 |
* @size: new partition size, 0 for maximal size |
| 1499 |
* @align: alignment to use for the partition end |
| 1500 |
* @error: (out) (optional): place to store error (if any) |
| 1501 |
* |
| 1502 |
* Returns: whether the @part partition was successfully resized on @disk to @size |
| 1503 |
* |
| 1504 |
* NOTE: The resulting partition may be slightly bigger than requested due to alignment. |
| 1505 |
* |
| 1506 |
* Tech category: %BD_PART_TECH_MODE_MODIFY_TABLE + the tech according to the partition table type |
| 1507 |
*/ |
| 1508 |
gboolean bd_part_resize_part (const gchar *disk, const gchar *part, guint64 size, BDPartAlign align, GError **error) { |
| 1509 |
gint part_num = 0; |
| 1510 |
struct fdisk_context *cxt = NULL; |
| 1511 |
struct fdisk_table *table = NULL; |
| 1512 |
struct fdisk_partition *pa = NULL; |
| 1513 |
gint ret = 0; |
| 1514 |
guint64 old_size = 0; |
| 1515 |
guint64 sector_size = 0; |
| 1516 |
guint64 grain_size = 0; |
| 1517 |
guint64 progress_id = 0; |
| 1518 |
guint64 max_size = 0; |
| 1519 |
guint64 start = 0; |
| 1520 |
guint64 end = 0; |
| 1521 |
gint version = 0; |
| 1522 |
gchar *msg = NULL; |
| 1523 |
GError *l_error = NULL; |
| 1524 |
|
| 1525 |
msg = g_strdup_printf ("Started resizing partition '%s'", part); |
| 1526 |
progress_id = bd_utils_report_started (msg); |
| 1527 |
g_free (msg); |
| 1528 |
|
| 1529 |
part_num = get_part_num (part, &l_error); |
| 1530 |
if (part_num == -1) { |
| 1531 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1532 |
g_propagate_error (error, l_error); |
| 1533 |
return FALSE; |
| 1534 |
} |
| 1535 |
|
| 1536 |
/* /dev/sda1 is the partition number 0 in libfdisk */ |
| 1537 |
part_num--; |
| 1538 |
cxt = get_device_context (disk, FALSE, &l_error); |
| 1539 |
if (!cxt) { |
| 1540 |
/* error is already populated */ |
| 1541 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1542 |
g_propagate_error (error, l_error); |
| 1543 |
return FALSE; |
| 1544 |
} |
| 1545 |
|
| 1546 |
/* get existing partitions and free spaces and sort the table */ |
| 1547 |
ret = fdisk_get_partitions (cxt, &table); |
| 1548 |
if (ret != 0) { |
| 1549 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1550 |
"Failed to get existing partitions on the device: %s", strerror_l (-ret, c_locale)); |
| 1551 |
fdisk_unref_table (table); |
| 1552 |
close_context (cxt); |
| 1553 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1554 |
g_propagate_error (error, l_error); |
| 1555 |
return FALSE; |
| 1556 |
} |
| 1557 |
|
| 1558 |
ret = fdisk_get_freespaces (cxt, &table); |
| 1559 |
if (ret != 0) { |
| 1560 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1561 |
"Failed to get free spaces on the device: %s", strerror_l (-ret, c_locale)); |
| 1562 |
fdisk_unref_table (table); |
| 1563 |
close_context (cxt); |
| 1564 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1565 |
g_propagate_error (error, l_error); |
| 1566 |
return FALSE; |
| 1567 |
} |
| 1568 |
|
| 1569 |
fdisk_table_sort_partitions (table, fdisk_partition_cmp_start); |
| 1570 |
|
| 1571 |
ret = fdisk_get_partition (cxt, part_num, &pa); |
| 1572 |
if (ret != 0) { |
| 1573 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1574 |
"Failed to get partition %d on device '%s'", part_num, disk); |
| 1575 |
fdisk_unref_table (table); |
| 1576 |
close_context (cxt); |
| 1577 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1578 |
g_propagate_error (error, l_error); |
| 1579 |
return FALSE; |
| 1580 |
} |
| 1581 |
|
| 1582 |
if (fdisk_partition_has_size (pa)) |
| 1583 |
old_size = (guint64) fdisk_partition_get_size (pa); |
| 1584 |
else { |
| 1585 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1586 |
"Failed to get size for partition %d on device '%s'", part_num, disk); |
| 1587 |
fdisk_unref_partition (pa); |
| 1588 |
fdisk_unref_table (table); |
| 1589 |
close_context (cxt); |
| 1590 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1591 |
g_propagate_error (error, l_error); |
| 1592 |
return FALSE; |
| 1593 |
} |
| 1594 |
|
| 1595 |
/* set grain_size based on user alignment preferences */ |
| 1596 |
sector_size = (guint64) fdisk_get_sector_size (cxt); |
| 1597 |
grain_size = (guint64) fdisk_get_grain_size (cxt); |
| 1598 |
|
| 1599 |
if (align == BD_PART_ALIGN_NONE) |
| 1600 |
grain_size = sector_size; |
| 1601 |
else if (align == BD_PART_ALIGN_MINIMAL) |
| 1602 |
grain_size = (guint64) fdisk_get_minimal_iosize (cxt); |
| 1603 |
/* else OPTIMAL or unknown -> nothing to do */ |
| 1604 |
|
| 1605 |
if (!get_max_part_size (table, part_num, &max_size, &l_error)) { |
| 1606 |
g_prefix_error (&l_error, "Failed to get maximal size for '%s': ", part); |
| 1607 |
fdisk_unref_table (table); |
| 1608 |
fdisk_unref_partition (pa); |
| 1609 |
close_context (cxt); |
| 1610 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1611 |
g_propagate_error (error, l_error); |
| 1612 |
return FALSE; |
| 1613 |
} |
| 1614 |
|
| 1615 |
if (size == 0) { |
| 1616 |
if (max_size == old_size) { |
| 1617 |
bd_utils_log_format (BD_UTILS_LOG_INFO, "Not resizing, partition '%s' is already at its maximum size.", part); |
| 1618 |
fdisk_unref_table (table); |
| 1619 |
fdisk_unref_partition (pa); |
| 1620 |
close_context (cxt); |
| 1621 |
bd_utils_report_finished (progress_id, "Completed"); |
| 1622 |
return TRUE; |
| 1623 |
} |
| 1624 |
|
| 1625 |
/* latest libfdisk introduces default end alignment for new partitions, we should |
| 1626 |
do the same for resizes where we calculate the size ourselves */ |
| 1627 |
version = fdisk_get_library_version (NULL); |
| 1628 |
if (version >= 2380 && align != BD_PART_ALIGN_NONE) { |
| 1629 |
start = fdisk_partition_get_start (pa); |
| 1630 |
end = start + max_size; |
| 1631 |
end = fdisk_align_lba_in_range (cxt, end, start, end); |
| 1632 |
max_size = end - start; |
| 1633 |
} |
| 1634 |
|
| 1635 |
if (fdisk_partition_set_size (pa, max_size) != 0) { |
| 1636 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1637 |
"Failed to set size for partition %d on device '%s'", part_num, disk); |
| 1638 |
fdisk_unref_table (table); |
| 1639 |
fdisk_unref_partition (pa); |
| 1640 |
close_context (cxt); |
| 1641 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1642 |
g_propagate_error (error, l_error); |
| 1643 |
return FALSE; |
| 1644 |
} |
| 1645 |
} else { |
| 1646 |
/* align size up */ |
| 1647 |
if (size % grain_size != 0) |
| 1648 |
size = ((size + grain_size) / grain_size) * grain_size; |
| 1649 |
size = size / sector_size; |
| 1650 |
|
| 1651 |
if (size == old_size) { |
| 1652 |
bd_utils_log_format (BD_UTILS_LOG_INFO, "Not resizing, new size after alignment is the same as the old size."); |
| 1653 |
fdisk_unref_table (table); |
| 1654 |
fdisk_unref_partition (pa); |
| 1655 |
close_context (cxt); |
| 1656 |
bd_utils_report_finished (progress_id, "Completed"); |
| 1657 |
return TRUE; |
| 1658 |
} |
| 1659 |
|
| 1660 |
if (size > old_size && size > max_size) { |
| 1661 |
if (size - max_size <= 4 MiB / sector_size) { |
| 1662 |
bd_utils_log_format (BD_UTILS_LOG_INFO, |
| 1663 |
"Requested size %"G_GUINT64_FORMAT" is bigger than max size for partition '%s', adjusting to %"G_GUINT64_FORMAT".", |
| 1664 |
size * sector_size, part, max_size * sector_size); |
| 1665 |
size = max_size; |
| 1666 |
} else { |
| 1667 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1668 |
"Requested size %"G_GUINT64_FORMAT" is bigger than max size (%"G_GUINT64_FORMAT") for partition '%s'", |
| 1669 |
size * sector_size, max_size * sector_size, part); |
| 1670 |
fdisk_unref_table (table); |
| 1671 |
fdisk_unref_partition (pa); |
| 1672 |
close_context (cxt); |
| 1673 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1674 |
g_propagate_error (error, l_error); |
| 1675 |
return FALSE; |
| 1676 |
} |
| 1677 |
} |
| 1678 |
|
| 1679 |
if (fdisk_partition_set_size (pa, size) != 0) { |
| 1680 |
g_set_error_literal (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1681 |
"Failed to set partition size"); |
| 1682 |
fdisk_unref_table (table); |
| 1683 |
fdisk_unref_partition (pa); |
| 1684 |
close_context (cxt); |
| 1685 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1686 |
g_propagate_error (error, l_error); |
| 1687 |
return FALSE; |
| 1688 |
} |
| 1689 |
} |
| 1690 |
|
| 1691 |
ret = fdisk_set_partition (cxt, part_num, pa); |
| 1692 |
if (ret != 0) { |
| 1693 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1694 |
"Failed to resize partition '%s': %s", part, strerror_l (-ret, c_locale)); |
| 1695 |
fdisk_unref_table (table); |
| 1696 |
fdisk_unref_partition (pa); |
| 1697 |
close_context (cxt); |
| 1698 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1699 |
g_propagate_error (error, l_error); |
| 1700 |
return FALSE; |
| 1701 |
} |
| 1702 |
|
| 1703 |
if (!write_label (cxt, table, disk, FALSE, &l_error)) { |
| 1704 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1705 |
g_propagate_error (error, l_error); |
| 1706 |
fdisk_unref_table (table); |
| 1707 |
fdisk_unref_partition (pa); |
| 1708 |
close_context (cxt); |
| 1709 |
return FALSE; |
| 1710 |
} |
| 1711 |
|
| 1712 |
fdisk_unref_table (table); |
| 1713 |
|
| 1714 |
/* fixed in mass fdisk_ref_partition() in libfdisk 2.35, |
| 1715 |
see https://github.com/karelzak/util-linux/pull/822 */ |
| 1716 |
if (fdisk_version >= 2350) |
| 1717 |
fdisk_unref_partition (pa); |
| 1718 |
close_context (cxt); |
| 1719 |
|
| 1720 |
bd_utils_report_finished (progress_id, "Completed"); |
| 1721 |
|
| 1722 |
return TRUE; |
| 1723 |
} |
| 1724 |
|
| 1725 |
static gboolean set_part_type (struct fdisk_context *cxt, gint part_num, const gchar *type_str, BDPartTableType table_type, GError **error) { |
| 1726 |
struct fdisk_label *lb = NULL; |
| 1727 |
struct fdisk_partition *pa = NULL; |
| 1728 |
struct fdisk_parttype *ptype = NULL; |
| 1729 |
const gchar *label_name = NULL; |
| 1730 |
gchar *endptr = NULL; |
| 1731 |
gint status = 0; |
| 1732 |
gint64 part_id_int = 0; |
| 1733 |
|
| 1734 |
/* check if part type/id is valid for MBR */ |
| 1735 |
if (table_type == BD_PART_TABLE_MSDOS) { |
| 1736 |
part_id_int = g_ascii_strtoll (type_str, &endptr, 0); |
| 1737 |
|
| 1738 |
if (part_id_int <= 0 || part_id_int > 0xFF || endptr == NULL || *endptr != '\0') { |
| 1739 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_INVAL, |
| 1740 |
"Invalid partition id given: '%s'.", type_str); |
| 1741 |
return FALSE; |
| 1742 |
} |
| 1743 |
|
| 1744 |
if (part_id_int == 0x05 || part_id_int == 0x0f || part_id_int == 0x85) { |
| 1745 |
g_set_error_literal (error, BD_PART_ERROR, BD_PART_ERROR_INVAL, |
| 1746 |
"Cannot change partition id to extended."); |
| 1747 |
return FALSE; |
| 1748 |
} |
| 1749 |
} |
| 1750 |
|
| 1751 |
lb = fdisk_get_label (cxt, NULL); |
| 1752 |
if (!lb) { |
| 1753 |
g_set_error_literal (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1754 |
"Failed to read partition table."); |
| 1755 |
return FALSE; |
| 1756 |
} |
| 1757 |
|
| 1758 |
label_name = fdisk_label_get_name (lb); |
| 1759 |
if (g_strcmp0 (label_name, table_type_str[table_type]) != 0) { |
| 1760 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_INVAL, |
| 1761 |
"Setting partition type is not supported on '%s' partition table", label_name); |
| 1762 |
return FALSE; |
| 1763 |
} |
| 1764 |
|
| 1765 |
status = fdisk_get_partition (cxt, part_num, &pa); |
| 1766 |
if (status != 0) { |
| 1767 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_INVAL, |
| 1768 |
"Failed to get partition %d.", part_num); |
| 1769 |
return FALSE; |
| 1770 |
} |
| 1771 |
|
| 1772 |
ptype = fdisk_label_parse_parttype (lb, type_str); |
| 1773 |
if (!ptype) { |
| 1774 |
g_set_error_literal (error, BD_PART_ERROR, BD_PART_ERROR_INVAL, |
| 1775 |
"Failed to parse partition type."); |
| 1776 |
fdisk_unref_partition (pa); |
| 1777 |
return FALSE; |
| 1778 |
} |
| 1779 |
|
| 1780 |
status = fdisk_set_partition_type (cxt, part_num, ptype); |
| 1781 |
if (status != 0) { |
| 1782 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1783 |
"Failed to set partition type for partition %d.", part_num); |
| 1784 |
fdisk_unref_parttype (ptype); |
| 1785 |
fdisk_unref_partition (pa); |
| 1786 |
return FALSE; |
| 1787 |
} |
| 1788 |
|
| 1789 |
fdisk_unref_parttype (ptype); |
| 1790 |
fdisk_unref_partition (pa); |
| 1791 |
return TRUE; |
| 1792 |
} |
| 1793 |
|
| 1794 |
/** |
| 1795 |
* bd_part_set_part_name: |
| 1796 |
* @disk: device the partition belongs to |
| 1797 |
* @part: partition the name should be set for |
| 1798 |
* @name: name to set |
| 1799 |
* @error: (out) (optional): place to store error (if any) |
| 1800 |
* |
| 1801 |
* Returns: whether the name was successfully set or not |
| 1802 |
* |
| 1803 |
* Tech category: %BD_PART_TECH_GPT-%BD_PART_TECH_MODE_MODIFY_PART |
| 1804 |
*/ |
| 1805 |
gboolean bd_part_set_part_name (const gchar *disk, const gchar *part, const gchar *name, GError **error) { |
| 1806 |
struct fdisk_context *cxt = NULL; |
| 1807 |
struct fdisk_label *lb = NULL; |
| 1808 |
struct fdisk_partition *pa = NULL; |
| 1809 |
const gchar *label_name = NULL; |
| 1810 |
gint part_num = 0; |
| 1811 |
gint status = 0; |
| 1812 |
guint64 progress_id = 0; |
| 1813 |
gchar *msg = NULL; |
| 1814 |
GError *l_error = NULL; |
| 1815 |
|
| 1816 |
msg = g_strdup_printf ("Started setting name on the partition '%s'", part); |
| 1817 |
progress_id = bd_utils_report_started (msg); |
| 1818 |
g_free (msg); |
| 1819 |
|
| 1820 |
cxt = get_device_context (disk, FALSE, &l_error); |
| 1821 |
if (!cxt) { |
| 1822 |
/* error is already populated */ |
| 1823 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1824 |
g_propagate_error (error, l_error); |
| 1825 |
return FALSE; |
| 1826 |
} |
| 1827 |
|
| 1828 |
lb = fdisk_get_label (cxt, NULL); |
| 1829 |
if (!lb) { |
| 1830 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1831 |
"Failed to read partition table on device '%s'", disk); |
| 1832 |
close_context (cxt); |
| 1833 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1834 |
g_propagate_error (error, l_error); |
| 1835 |
return FALSE; |
| 1836 |
} |
| 1837 |
|
| 1838 |
label_name = fdisk_label_get_name (lb); |
| 1839 |
if (g_strcmp0 (label_name, table_type_str[BD_PART_TABLE_GPT]) != 0) { |
| 1840 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_INVAL, |
| 1841 |
"Partition names unsupported on the device '%s' ('%s')", disk, |
| 1842 |
label_name); |
| 1843 |
close_context (cxt); |
| 1844 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1845 |
g_propagate_error (error, l_error); |
| 1846 |
return FALSE; |
| 1847 |
} |
| 1848 |
|
| 1849 |
part_num = get_part_num (part, &l_error); |
| 1850 |
if (part_num == -1) { |
| 1851 |
close_context (cxt); |
| 1852 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1853 |
g_propagate_error (error, l_error); |
| 1854 |
return FALSE; |
| 1855 |
} |
| 1856 |
|
| 1857 |
/* /dev/sda1 is the partition number 0 in libfdisk */ |
| 1858 |
part_num--; |
| 1859 |
|
| 1860 |
status = fdisk_get_partition (cxt, part_num, &pa); |
| 1861 |
if (status != 0) { |
| 1862 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1863 |
"Failed to get partition '%s' on device '%s': %s", |
| 1864 |
part, disk, strerror_l (-status, c_locale)); |
| 1865 |
close_context (cxt); |
| 1866 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1867 |
g_propagate_error (error, l_error); |
| 1868 |
return FALSE; |
| 1869 |
} |
| 1870 |
|
| 1871 |
status = fdisk_partition_set_name (pa, name); |
| 1872 |
if (status != 0) { |
| 1873 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1874 |
"Failed to set name on the partition '%s' on device '%s': %s", |
| 1875 |
part, disk, strerror_l (-status, c_locale)); |
| 1876 |
fdisk_unref_partition (pa); |
| 1877 |
close_context (cxt); |
| 1878 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1879 |
g_propagate_error (error, l_error); |
| 1880 |
return FALSE; |
| 1881 |
} |
| 1882 |
|
| 1883 |
status = fdisk_set_partition (cxt, part_num, pa); |
| 1884 |
if (status != 0) { |
| 1885 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 1886 |
"Failed to set name on the partition '%s' on device '%s': %s", |
| 1887 |
part, disk, strerror_l (-status, c_locale)); |
| 1888 |
fdisk_unref_partition (pa); |
| 1889 |
close_context (cxt); |
| 1890 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1891 |
g_propagate_error (error, l_error); |
| 1892 |
return FALSE; |
| 1893 |
} |
| 1894 |
|
| 1895 |
fdisk_unref_partition (pa); |
| 1896 |
|
| 1897 |
if (!write_label (cxt, NULL, disk, FALSE, &l_error)) { |
| 1898 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1899 |
g_propagate_error (error, l_error); |
| 1900 |
close_context (cxt); |
| 1901 |
return FALSE; |
| 1902 |
} |
| 1903 |
|
| 1904 |
close_context (cxt); |
| 1905 |
bd_utils_report_finished (progress_id, "Completed"); |
| 1906 |
return TRUE; |
| 1907 |
} |
| 1908 |
|
| 1909 |
/** |
| 1910 |
* bd_part_set_part_type: |
| 1911 |
* @disk: device the partition belongs to |
| 1912 |
* @part: partition the type should be set for |
| 1913 |
* @type_guid: GUID of the type |
| 1914 |
* @error: (out) (optional): place to store error (if any) |
| 1915 |
* |
| 1916 |
* Returns: whether the @type_guid type was successfully set for @part or not |
| 1917 |
* |
| 1918 |
* Tech category: %BD_PART_TECH_GPT-%BD_PART_TECH_MODE_MODIFY_PART |
| 1919 |
*/ |
| 1920 |
gboolean bd_part_set_part_type (const gchar *disk, const gchar *part, const gchar *type_guid, GError **error) { |
| 1921 |
guint64 progress_id = 0; |
| 1922 |
gchar *msg = NULL; |
| 1923 |
struct fdisk_context *cxt = NULL; |
| 1924 |
gint part_num = 0; |
| 1925 |
GError *l_error = NULL; |
| 1926 |
|
| 1927 |
msg = g_strdup_printf ("Started setting type on the partition '%s'", part); |
| 1928 |
progress_id = bd_utils_report_started (msg); |
| 1929 |
g_free (msg); |
| 1930 |
|
| 1931 |
part_num = get_part_num (part, &l_error); |
| 1932 |
if (part_num == -1) { |
| 1933 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1934 |
g_propagate_error (error, l_error); |
| 1935 |
return FALSE; |
| 1936 |
} |
| 1937 |
|
| 1938 |
/* /dev/sda1 is the partition number 0 in libfdisk */ |
| 1939 |
part_num--; |
| 1940 |
|
| 1941 |
cxt = get_device_context (disk, FALSE, &l_error); |
| 1942 |
if (!cxt) { |
| 1943 |
/* error is already populated */ |
| 1944 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1945 |
g_propagate_error (error, l_error); |
| 1946 |
return FALSE; |
| 1947 |
} |
| 1948 |
|
| 1949 |
if (!set_part_type (cxt, part_num, type_guid, BD_PART_TABLE_GPT, &l_error)) { |
| 1950 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1951 |
g_propagate_error (error, l_error); |
| 1952 |
close_context (cxt); |
| 1953 |
return FALSE; |
| 1954 |
} |
| 1955 |
|
| 1956 |
if (!write_label (cxt, NULL, disk, FALSE, &l_error)) { |
| 1957 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1958 |
g_propagate_error (error, l_error); |
| 1959 |
close_context (cxt); |
| 1960 |
return FALSE; |
| 1961 |
} |
| 1962 |
|
| 1963 |
close_context (cxt); |
| 1964 |
bd_utils_report_finished (progress_id, "Completed"); |
| 1965 |
return TRUE; |
| 1966 |
} |
| 1967 |
|
| 1968 |
/** |
| 1969 |
* bd_part_set_part_id: |
| 1970 |
* @disk: device the partition belongs to |
| 1971 |
* @part: partition the ID should be set for |
| 1972 |
* @part_id: partition Id |
| 1973 |
* @error: (out) (optional): place to store error (if any) |
| 1974 |
* |
| 1975 |
* Returns: whether the @part_id type was successfully set for @part or not |
| 1976 |
* |
| 1977 |
* Tech category: %BD_PART_TECH_MBR-%BD_PART_TECH_MODE_MODIFY_PART |
| 1978 |
*/ |
| 1979 |
gboolean bd_part_set_part_id (const gchar *disk, const gchar *part, const gchar *part_id, GError **error) { |
| 1980 |
guint64 progress_id = 0; |
| 1981 |
gchar *msg = NULL; |
| 1982 |
struct fdisk_context *cxt = NULL; |
| 1983 |
gint part_num = 0; |
| 1984 |
GError *l_error = NULL; |
| 1985 |
|
| 1986 |
msg = g_strdup_printf ("Started setting id on the partition '%s'", part); |
| 1987 |
progress_id = bd_utils_report_started (msg); |
| 1988 |
g_free (msg); |
| 1989 |
|
| 1990 |
part_num = get_part_num (part, &l_error); |
| 1991 |
if (part_num == -1) { |
| 1992 |
bd_utils_report_finished (progress_id, l_error->message); |
| 1993 |
g_propagate_error (error, l_error); |
| 1994 |
return FALSE; |
| 1995 |
} |
| 1996 |
|
| 1997 |
/* /dev/sda1 is the partition number 0 in libfdisk */ |
| 1998 |
part_num--; |
| 1999 |
|
| 2000 |
cxt = get_device_context (disk, FALSE, &l_error); |
| 2001 |
if (!cxt) { |
| 2002 |
/* error is already populated */ |
| 2003 |
bd_utils_report_finished (progress_id, l_error->message); |
| 2004 |
g_propagate_error (error, l_error); |
| 2005 |
return FALSE; |
| 2006 |
} |
| 2007 |
|
| 2008 |
if (!set_part_type (cxt, part_num, part_id, BD_PART_TABLE_MSDOS, &l_error)) { |
| 2009 |
bd_utils_report_finished (progress_id, l_error->message); |
| 2010 |
g_propagate_error (error, l_error); |
| 2011 |
close_context (cxt); |
| 2012 |
return FALSE; |
| 2013 |
} |
| 2014 |
|
| 2015 |
if (!write_label (cxt, NULL, disk, FALSE, &l_error)) { |
| 2016 |
bd_utils_report_finished (progress_id, l_error->message); |
| 2017 |
g_propagate_error (error, l_error); |
| 2018 |
close_context (cxt); |
| 2019 |
return FALSE; |
| 2020 |
} |
| 2021 |
|
| 2022 |
close_context (cxt); |
| 2023 |
bd_utils_report_finished (progress_id, "Completed"); |
| 2024 |
return TRUE; |
| 2025 |
} |
| 2026 |
|
| 2027 |
/** |
| 2028 |
* bd_part_set_part_uuid: |
| 2029 |
* @disk: device the partition belongs to |
| 2030 |
* @part: partition the UUID should be set for |
| 2031 |
* @uuid: partition UUID to set |
| 2032 |
* @error: (out) (optional): place to store error (if any) |
| 2033 |
* |
| 2034 |
* Returns: whether the @uuid type was successfully set for @part or not |
| 2035 |
* |
| 2036 |
* Tech category: %BD_PART_TECH_GPT-%BD_PART_TECH_MODE_MODIFY_PART |
| 2037 |
*/ |
| 2038 |
gboolean bd_part_set_part_uuid (const gchar *disk, const gchar *part, const gchar *uuid, GError **error) { |
| 2039 |
struct fdisk_context *cxt = NULL; |
| 2040 |
struct fdisk_partition *pa = NULL; |
| 2041 |
struct fdisk_label *lb = NULL; |
| 2042 |
const gchar *label_name = NULL; |
| 2043 |
gint part_num = 0; |
| 2044 |
gint status = 0; |
| 2045 |
guint64 progress_id = 0; |
| 2046 |
gchar *msg = NULL; |
| 2047 |
GError *l_error = NULL; |
| 2048 |
|
| 2049 |
msg = g_strdup_printf ("Started setting UUID on the partition '%s'", part); |
| 2050 |
progress_id = bd_utils_report_started (msg); |
| 2051 |
g_free (msg); |
| 2052 |
|
| 2053 |
cxt = get_device_context (disk, FALSE, &l_error); |
| 2054 |
if (!cxt) { |
| 2055 |
/* error is already populated */ |
| 2056 |
bd_utils_report_finished (progress_id, l_error->message); |
| 2057 |
g_propagate_error (error, l_error); |
| 2058 |
return FALSE; |
| 2059 |
} |
| 2060 |
|
| 2061 |
lb = fdisk_get_label (cxt, NULL); |
| 2062 |
if (!lb) { |
| 2063 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 2064 |
"Failed to read partition table on device '%s'", disk); |
| 2065 |
close_context (cxt); |
| 2066 |
bd_utils_report_finished (progress_id, l_error->message); |
| 2067 |
g_propagate_error (error, l_error); |
| 2068 |
return FALSE; |
| 2069 |
} |
| 2070 |
|
| 2071 |
label_name = fdisk_label_get_name (lb); |
| 2072 |
if (g_strcmp0 (label_name, table_type_str[BD_PART_TABLE_GPT]) != 0) { |
| 2073 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_INVAL, |
| 2074 |
"Partition UUIDs unsupported on the device '%s' ('%s')", disk, |
| 2075 |
label_name); |
| 2076 |
close_context (cxt); |
| 2077 |
bd_utils_report_finished (progress_id, l_error->message); |
| 2078 |
g_propagate_error (error, l_error); |
| 2079 |
return FALSE; |
| 2080 |
} |
| 2081 |
|
| 2082 |
part_num = get_part_num (part, &l_error); |
| 2083 |
if (part_num == -1) { |
| 2084 |
close_context (cxt); |
| 2085 |
bd_utils_report_finished (progress_id, l_error->message); |
| 2086 |
g_propagate_error (error, l_error); |
| 2087 |
return FALSE; |
| 2088 |
} |
| 2089 |
|
| 2090 |
/* /dev/sda1 is the partition number 0 in libfdisk */ |
| 2091 |
part_num--; |
| 2092 |
|
| 2093 |
status = fdisk_get_partition (cxt, part_num, &pa); |
| 2094 |
if (status != 0) { |
| 2095 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 2096 |
"Failed to get partition '%s' on device '%s': %s", |
| 2097 |
part, disk, strerror_l (-status, c_locale)); |
| 2098 |
close_context (cxt); |
| 2099 |
bd_utils_report_finished (progress_id, l_error->message); |
| 2100 |
g_propagate_error (error, l_error); |
| 2101 |
return FALSE; |
| 2102 |
} |
| 2103 |
|
| 2104 |
status = fdisk_partition_set_uuid (pa, uuid); |
| 2105 |
if (status != 0) { |
| 2106 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 2107 |
"Failed to set UUID on the partition '%s' on device '%s': %s", |
| 2108 |
part, disk, strerror_l (-status, c_locale)); |
| 2109 |
fdisk_unref_partition (pa); |
| 2110 |
close_context (cxt); |
| 2111 |
bd_utils_report_finished (progress_id, l_error->message); |
| 2112 |
g_propagate_error (error, l_error); |
| 2113 |
return FALSE; |
| 2114 |
} |
| 2115 |
|
| 2116 |
status = fdisk_set_partition (cxt, part_num, pa); |
| 2117 |
if (status != 0) { |
| 2118 |
g_set_error (&l_error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 2119 |
"Failed to set UUID on the partition '%s' on device '%s': %s", |
| 2120 |
part, disk, strerror_l (-status, c_locale)); |
| 2121 |
fdisk_unref_partition (pa); |
| 2122 |
close_context (cxt); |
| 2123 |
bd_utils_report_finished (progress_id, l_error->message); |
| 2124 |
g_propagate_error (error, l_error); |
| 2125 |
return FALSE; |
| 2126 |
} |
| 2127 |
|
| 2128 |
fdisk_unref_partition (pa); |
| 2129 |
|
| 2130 |
if (!write_label (cxt, NULL, disk, FALSE, &l_error)) { |
| 2131 |
bd_utils_report_finished (progress_id, l_error->message); |
| 2132 |
g_propagate_error (error, l_error); |
| 2133 |
close_context (cxt); |
| 2134 |
return FALSE; |
| 2135 |
} |
| 2136 |
|
| 2137 |
close_context (cxt); |
| 2138 |
bd_utils_report_finished (progress_id, "Completed"); |
| 2139 |
return TRUE; |
| 2140 |
} |
| 2141 |
|
| 2142 |
/** |
| 2143 |
* bd_part_set_part_bootable: |
| 2144 |
* @disk: device the partition belongs to |
| 2145 |
* @part: partition the bootable flag should be set for |
| 2146 |
* @bootable: whether to set or unset the bootable flag |
| 2147 |
* @error: (out) (optional): place to store error (if any) |
| 2148 |
* |
| 2149 |
* Returns: whether the @bootable flag was successfully set for @part or not |
| 2150 |
* |
| 2151 |
* Tech category: %BD_PART_TECH_MBR-%BD_PART_TECH_MODE_MODIFY_PART |
| 2152 |
*/ |
| 2153 |
gboolean bd_part_set_part_bootable (const gchar *disk, const gchar *part, gboolean bootable, GError **error) { |
| 2154 |
struct fdisk_context *cxt = NULL; |
| 2155 |
gint part_num = 0; |
| 2156 |
struct fdisk_partition *pa = NULL; |
| 2157 |
gint ret = 0; |
| 2158 |
|
| 2159 |
part_num = get_part_num (part, error); |
| 2160 |
if (part_num == -1) |
| 2161 |
return FALSE; |
| 2162 |
|
| 2163 |
/* /dev/sda1 is the partition number 0 in libfdisk */ |
| 2164 |
part_num--; |
| 2165 |
|
| 2166 |
cxt = get_device_context (disk, FALSE, error); |
| 2167 |
if (!cxt) |
| 2168 |
return FALSE; |
| 2169 |
|
| 2170 |
ret = fdisk_get_partition (cxt, part_num, &pa); |
| 2171 |
if (ret != 0) { |
| 2172 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 2173 |
"Failed to get partition '%d'.", part_num); |
| 2174 |
close_context (cxt); |
| 2175 |
return FALSE; |
| 2176 |
} |
| 2177 |
|
| 2178 |
ret = fdisk_partition_is_bootable (pa); |
| 2179 |
if ((ret == 1 && bootable) || (ret != 1 && !bootable)) { |
| 2180 |
/* boot flag is already set as desired, no change needed */ |
| 2181 |
fdisk_unref_partition (pa); |
| 2182 |
close_context (cxt); |
| 2183 |
return TRUE; |
| 2184 |
} |
| 2185 |
|
| 2186 |
ret = fdisk_toggle_partition_flag (cxt, part_num, DOS_FLAG_ACTIVE); |
| 2187 |
if (ret != 0) { |
| 2188 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 2189 |
"Failed to set partition bootable flag: %s", strerror_l (-ret, c_locale)); |
| 2190 |
fdisk_unref_partition (pa); |
| 2191 |
close_context (cxt); |
| 2192 |
return FALSE; |
| 2193 |
} |
| 2194 |
|
| 2195 |
if (!write_label (cxt, NULL, disk, FALSE, error)) { |
| 2196 |
fdisk_unref_partition (pa); |
| 2197 |
close_context (cxt); |
| 2198 |
return FALSE; |
| 2199 |
} |
| 2200 |
|
| 2201 |
fdisk_unref_partition (pa); |
| 2202 |
close_context (cxt); |
| 2203 |
|
| 2204 |
return TRUE; |
| 2205 |
} |
| 2206 |
|
| 2207 |
/** |
| 2208 |
* bd_part_set_part_attributes: |
| 2209 |
* @disk: device the partition belongs to |
| 2210 |
* @part: partition the attributes should be set for |
| 2211 |
* @attrs: GPT attributes to set on @part |
| 2212 |
* @error: (out) (optional): place to store error (if any) |
| 2213 |
* |
| 2214 |
* Returns: whether the @attrs GPT attributes were successfully set for @part or not |
| 2215 |
* |
| 2216 |
* Tech category: %BD_PART_TECH_GPT-%BD_PART_TECH_MODE_MODIFY_PART |
| 2217 |
*/ |
| 2218 |
gboolean bd_part_set_part_attributes (const gchar *disk, const gchar *part, guint64 attrs, GError **error) { |
| 2219 |
struct fdisk_context *cxt = NULL; |
| 2220 |
struct fdisk_partition *pa = NULL; |
| 2221 |
gint part_num = 0; |
| 2222 |
gint ret = 0; |
| 2223 |
|
| 2224 |
part_num = get_part_num (part, error); |
| 2225 |
if (part_num == -1) |
| 2226 |
return FALSE; |
| 2227 |
|
| 2228 |
/* /dev/sda1 is the partition number 0 in libfdisk */ |
| 2229 |
part_num--; |
| 2230 |
|
| 2231 |
cxt = get_device_context (disk, FALSE, error); |
| 2232 |
if (!cxt) |
| 2233 |
return FALSE; |
| 2234 |
|
| 2235 |
ret = fdisk_get_partition (cxt, part_num, &pa); |
| 2236 |
if (ret != 0) { |
| 2237 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 2238 |
"Failed to get partition '%d' on device '%s'.", part_num, disk); |
| 2239 |
close_context (cxt); |
| 2240 |
return FALSE; |
| 2241 |
} |
| 2242 |
fdisk_unref_partition (pa); |
| 2243 |
|
| 2244 |
ret = fdisk_gpt_set_partition_attrs (cxt, part_num, attrs); |
| 2245 |
if (ret < 0) { |
| 2246 |
g_set_error (error, BD_PART_ERROR, BD_PART_ERROR_FAIL, |
| 2247 |
"Failed to set GPT attributes: %s", strerror_l (-ret, c_locale)); |
| 2248 |
close_context (cxt); |
| 2249 |
return FALSE; |
| 2250 |
} |
| 2251 |
|
| 2252 |
if (!write_label (cxt, NULL, disk, FALSE, error)) { |
| 2253 |
close_context (cxt); |
| 2254 |
return FALSE; |
| 2255 |
} |
| 2256 |
|
| 2257 |
close_context (cxt); |
| 2258 |
|
| 2259 |
return TRUE; |
| 2260 |
} |
| 2261 |
|
| 2262 |
/** |
| 2263 |
* bd_part_get_part_table_type_str: |
| 2264 |
* @type: table type to get string representation for |
| 2265 |
* @error: (out) (optional): place to store error (if any) |
| 2266 |
* |
| 2267 |
* Returns: (transfer none): string representation of @table_type |
| 2268 |
* |
| 2269 |
* Tech category: the tech according to @type |
| 2270 |
*/ |
| 2271 |
const gchar* bd_part_get_part_table_type_str (BDPartTableType type, GError **error) { |
| 2272 |
if (type >= BD_PART_TABLE_UNDEF) { |
| 2273 |
g_set_error_literal (error, BD_PART_ERROR, BD_PART_ERROR_INVAL, |
| 2274 |
"Invalid partition table type given"); |
| 2275 |
return NULL; |
| 2276 |
} |
| 2277 |
|
| 2278 |
return table_type_str[type]; |
| 2279 |
} |
| 2280 |
|
| 2281 |
/* string for BD_PART_TYPE_PROTECTED is "primary", that's what parted returns... */ |
| 2282 |
static const gchar* const part_types[6] = { "primary", "logical", "extended", "free", "metadata", "primary" }; |
| 2283 |
|
| 2284 |
/** |
| 2285 |
* bd_part_get_type_str: |
| 2286 |
* @type: type to get string representation for |
| 2287 |
* @error: (out) (optional): place to store error (if any) |
| 2288 |
* |
| 2289 |
* Returns: (transfer none): string representation of @type |
| 2290 |
* |
| 2291 |
* Tech category: always available |
| 2292 |
*/ |
| 2293 |
const gchar* bd_part_get_type_str (BDPartType type, GError **error) { |
| 2294 |
if (type > BD_PART_TYPE_PROTECTED) { |
| 2295 |
g_set_error_literal (error, BD_PART_ERROR, BD_PART_ERROR_INVAL, "Invalid partition type given"); |
| 2296 |
return NULL; |
| 2297 |
} |
| 2298 |
|
| 2299 |
return part_types[log2i (type) + 1]; |
| 2300 |
} |