HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/repart/repart.c
Git blob: b40c7a91acf99c5ac898f4de16e45b142c19fa8c
Lines 4001–5000 of 11,995
if (r < 0)
return log_error_errno(r, "Failed to create GPT disk label: %m");
r = derive_uuid(context->seed, "disk-uuid", &disk_uuid);
if (r < 0)
return log_error_errno(r, "Failed to acquire disk GPT uuid: %m");
r = fdisk_set_disklabel_id_as_id128(c, disk_uuid);
if (r < 0)
return log_error_errno(r, "Failed to set GPT disk label: %m");
goto add_initial_free_area;
}
r = fdisk_get_disklabel_id_as_id128(c, &disk_uuid);
if (r < 0)
return log_error_errno(r, "Failed to get current GPT disk label UUID: %m");
if (sd_id128_is_null(disk_uuid)) {
r = derive_uuid(context->seed, "disk-uuid", &disk_uuid);
if (r < 0)
return log_error_errno(r, "Failed to acquire disk GPT uuid: %m");
r = fdisk_set_disklabel_id_as_id128(c, disk_uuid);
if (r < 0)
return log_error_errno(r, "Failed to set GPT disk label: %m");
}
r = sym_fdisk_get_partitions(c, &t);
if (r < 0)
return log_error_errno(r, "Failed to acquire partition table: %m");
n_partitions = sym_fdisk_table_get_nents(t);
for (size_t i = 0; i < n_partitions; i++) {
_cleanup_free_ char *label_copy = NULL;
Partition *last = NULL;
struct fdisk_partition *p;
const char *label;
uint64_t sz, start;
bool found = false;
sd_id128_t ptid, id;
size_t partno;
p = sym_fdisk_table_get_partition(t, i);
if (!p)
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata.");
if (sym_fdisk_partition_is_used(p) <= 0)
continue;
if (sym_fdisk_partition_has_start(p) <= 0 ||
sym_fdisk_partition_has_size(p) <= 0 ||
sym_fdisk_partition_has_partno(p) <= 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a position, size or number.");
r = fdisk_partition_get_type_as_id128(p, &ptid);
if (r < 0)
return log_error_errno(r, "Failed to query partition type UUID: %m");
r = fdisk_partition_get_uuid_as_id128(p, &id);
if (r < 0)
return log_error_errno(r, "Failed to query partition UUID: %m");
label = sym_fdisk_partition_get_name(p);
if (!isempty(label)) {
label_copy = strdup(label);
if (!label_copy)
return log_oom();
}
sz = sym_fdisk_partition_get_size(p);
assert(sz <= UINT64_MAX/secsz);
sz *= secsz;
start = sym_fdisk_partition_get_start(p);
assert(start <= UINT64_MAX/secsz);
start *= secsz;
partno = sym_fdisk_partition_get_partno(p);
if (left_boundary == UINT64_MAX || left_boundary > start)
left_boundary = start;
/* Assign this existing partition to the first partition of the right type that doesn't have
* an existing one assigned yet. */
LIST_FOREACH(partitions, pp, context->partitions) {
last = pp;
if (!sd_id128_equal(pp->type.uuid, ptid))
continue;
if (!pp->current_partition) {
pp->current_uuid = id;
pp->current_size = sz;
pp->offset = start;
pp->partno = partno;
pp->current_label = TAKE_PTR(label_copy);
pp->current_partition = p;
sym_fdisk_ref_partition(p);
r = determine_current_padding(
c,
t,
p,
secsz,
grainsz,
&pp->current_padding,
/* ret_is_last_partition= */ NULL);
if (r < 0)
return r;
if (pp->current_padding > 0) {
r = context_add_free_area(context, pp->current_padding, pp);
if (r < 0)
return r;
}
found = true;
break;
}
}
/* If we have no matching definition, create a new one. */
if (!found) {
_cleanup_(partition_freep) Partition *np = NULL;
np = partition_new(context);
if (!np)
return log_oom();
np->current_uuid = id;
np->type = gpt_partition_type_from_uuid(ptid);
np->current_size = sz;
np->offset = start;
np->partno = partno;
np->current_label = TAKE_PTR(label_copy);
/* Never touch foreign partitions: keep their UUID and label. Set here rather than in
* context_acquire_partition_uuids_and_labels(), as --size=auto reloads the partition
* table after that ran, which recreates the foreign partitions. */
np->new_uuid = id;
r = strdup_to(&np->new_label, np->current_label);
if (r < 0)
return log_oom();
np->current_partition = p;
sym_fdisk_ref_partition(p);
r = determine_current_padding(
c,
t,
p,
secsz,
grainsz,
&np->current_padding,
/* ret_is_last_partition= */ NULL);
if (r < 0)
return r;
if (np->current_padding > 0) {
r = context_add_free_area(context, np->current_padding, np);
if (r < 0)
return r;
}
LIST_INSERT_AFTER(partitions, context->partitions, last, TAKE_PTR(np));
context->n_partitions++;
}
}
LIST_FOREACH(partitions, p, context->partitions)
if (PARTITION_SUPPRESSED(p) && PARTITION_EXISTS(p))
p->supplement_for->suppressing_supplement = false;
add_initial_free_area:
nsectors = sym_fdisk_get_nsectors(c);
assert(nsectors <= UINT64_MAX/secsz);
nsectors *= secsz;
first_lba = sym_fdisk_get_first_lba(c);
assert(first_lba <= UINT64_MAX/secsz);
first_lba *= secsz;
last_lba = sym_fdisk_get_last_lba(c);
assert(last_lba < UINT64_MAX);
last_lba++;
assert(last_lba <= UINT64_MAX/secsz);
last_lba *= secsz;
assert(last_lba >= first_lba);
if (left_boundary == UINT64_MAX) {
/* No partitions at all? Then the whole disk is up for grabs. */
first_lba = round_up_size(first_lba, grainsz);
last_lba = round_down_size(last_lba, grainsz);
if (last_lba > first_lba) {
r = context_add_free_area(context, last_lba - first_lba, NULL);
if (r < 0)
return r;
}
} else {
/* Add space left of first partition */
assert(left_boundary >= first_lba);
first_lba = round_up_size(first_lba, grainsz);
left_boundary = round_down_size(left_boundary, grainsz);
last_lba = round_down_size(last_lba, grainsz);
if (left_boundary > first_lba) {
r = context_add_free_area(context, left_boundary - first_lba, NULL);
if (r < 0)
return r;
}
}
context->start = first_lba;
context->end = last_lba;
context->total = nsectors;
context->sector_size = secsz;
context->default_fs_sector_size = fs_secsz;
context->grain_size = grainsz;
context->fdisk_context = TAKE_PTR(c);
context->fdisk_context_fd = TAKE_FD(fd);
return from_scratch;
}
static void context_unload_partition_table(Context *context) {
assert(context);
LIST_FOREACH(partitions, p, context->partitions) {
/* Entirely remove partitions that have no configuration */
if (PARTITION_IS_FOREIGN(p)) {
partition_unlink_and_free(context, p);
continue;
}
/* Otherwise drop all data we read off the block device and everything we might have
* calculated based on it */
p->dropped = false;
p->current_size = UINT64_MAX;
p->new_size = UINT64_MAX;
p->current_padding = UINT64_MAX;
p->new_padding = UINT64_MAX;
p->partno = UINT64_MAX;
p->offset = UINT64_MAX;
if (p->current_partition) {
sym_fdisk_unref_partition(p->current_partition);
p->current_partition = NULL;
}
if (p->new_partition) {
sym_fdisk_unref_partition(p->new_partition);
p->new_partition = NULL;
}
p->padding_area = NULL;
p->allocated_to_area = NULL;
p->current_uuid = SD_ID128_NULL;
p->current_label = mfree(p->current_label);
/* A supplement partition is only ever un-suppressed if the existing partition table prevented
* us from suppressing it. So when unloading the partition table, we must re-suppress. */
if (p->supplement_for)
p->supplement_for->suppressing_supplement = true;
}
context->start = UINT64_MAX;
context->end = UINT64_MAX;
context->total = UINT64_MAX;
if (context->fdisk_context) {
sym_fdisk_unref_context(context->fdisk_context);
context->fdisk_context = NULL;
}
context->fdisk_context_fd = safe_close(context->fdisk_context_fd);
context_free_free_areas(context);
}
static int format_size_change(uint64_t from, uint64_t to, char **ret) {
char *t;
assert(ret);
if (from != UINT64_MAX) {
if (from == to || to == UINT64_MAX)
t = strdup(FORMAT_BYTES(from));
else
t = strjoin(FORMAT_BYTES(from), " ", glyph(GLYPH_ARROW_RIGHT), " ", FORMAT_BYTES(to));
} else if (to != UINT64_MAX)
t = strjoin(glyph(GLYPH_ARROW_RIGHT), " ", FORMAT_BYTES(to));
else {
*ret = NULL;
return 0;
}
if (!t)
return log_oom();
*ret = t;
return 1;
}
static const char *partition_label(const Partition *p) {
assert(p);
if (p->new_label)
return p->new_label;
if (p->current_label)
return p->current_label;
return gpt_partition_type_uuid_to_string(p->type.uuid);
}
static int volume_label(const Partition *p, char **ret) {
assert(p);
assert(ret);
if (p->new_volume_label) {
if (strdup_to(ret, p->new_volume_label) < 0)
return log_oom();
return 0;
}
const char *e = partition_label(p);
if (!e)
return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "Partition has no label, cannot generate volume label.");
/* Let's prefix "luks-" for the label string used for LUKS superblocks. We do this so that the
* /dev/disk/by-label/ symlink to the LUKS volume and the file system inside it do not clash */
_cleanup_free_ char *j = strjoin("luks-", e);
if (!j)
return log_oom();
/* GPT labels are limited in UTF-16 code units, hence a label with multibyte characters might be
* longer in UTF-8 than what fits into the LUKS2 superblock. */
if (strlen(j) > LUKS2_LABEL_MAX)
return log_error_errno(SYNTHETIC_ERRNO(ENAMETOOLONG),
"Volume label '%s' derived from partition label exceeds LUKS2 limit of %u bytes, please set VolumeLabel= explicitly.",
j, LUKS2_LABEL_MAX);
*ret = TAKE_PTR(j);
return 0;
}
static int context_dump_partitions(Context *context) {
_cleanup_(table_unrefp) Table *t = NULL;
uint64_t sum_padding = 0, sum_size = 0;
int r;
const size_t roothash_col = 14, dropin_files_col = 15, split_path_col = 16;
bool has_roothash = false, has_dropin_files = false, has_split_path = false;
if (context->n_partitions == 0 && !sd_json_format_enabled(arg_json_format_flags)) {
log_info("Empty partition table.");
return 0;
}
t = table_new("type", /* 0 */
"label",
"uuid",
"part",
"file",
"node",
"offset",
"old size",
"raw size",
"size",
"old padding", /* 10 */
"raw padding",
"padding",
"activity",
"roothash",
"drop-in files",
"split path");
if (!t)
return log_oom();
/* For compatibility, use the original longer name for JSON output. */
table_set_json_field_name(t, 3, "partno");
/* Starting in v257, these fields would be automatically formatted with underscores. This would have
* been a breaking change, so to avoid that let's hard-code their original names. */
table_set_json_field_name(t, 15, "drop-in_files");
if (!DEBUG_LOGGING) {
if (!sd_json_format_enabled(arg_json_format_flags))
(void) table_set_display(t, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
(size_t) 8, (size_t) 9, (size_t) 12, roothash_col, dropin_files_col,
split_path_col);
else
(void) table_set_display(t, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
(size_t) 5, (size_t) 6, (size_t) 7, (size_t) 8, (size_t) 10,
(size_t) 11, (size_t) 13, roothash_col, dropin_files_col,
split_path_col);
}
(void) table_set_align_percent(t, table_get_cell(t, 0, 3), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 6), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 7), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 8), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 9), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 11), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 12), 100);
size_t n_partitions = 0;
LIST_FOREACH(partitions, p, context->partitions) {
if (p->dropped)
continue;
n_partitions++;
}
size_t cur_n_partition = 0;
LIST_FOREACH(partitions, p, context->partitions) {
_cleanup_free_ char *size_change = NULL, *padding_change = NULL, *partname = NULL, *rh = NULL;
char uuid_buffer[SD_ID128_UUID_STRING_MAX];
const char *label, *activity = NULL;
if (p->dropped)
continue;
cur_n_partition++;
if (p->current_size == UINT64_MAX)
activity = "create";
else if (p->current_size != p->new_size)
activity = "resize";
label = partition_label(p);
partname = p->partno != UINT64_MAX ? sym_fdisk_partname(context->node, p->partno+1) : NULL;
r = format_size_change(p->current_size, p->new_size, &size_change);
if (r < 0)
return r;
r = format_size_change(p->current_padding, p->new_padding, &padding_change);
if (r < 0)
return r;
if (p->new_size != UINT64_MAX)
sum_size += p->new_size;
if (p->new_padding != UINT64_MAX)
sum_padding += p->new_padding;
if (p->verity != VERITY_OFF) {
Partition *hp = p->verity == VERITY_HASH ? p : p->siblings[VERITY_HASH];
rh = iovec_is_set(&hp->roothash) ? hexmem(hp->roothash.iov_base, hp->roothash.iov_len) : strdup("TBD");
if (!rh)
return log_oom();
}
r = table_add_many(
t,
TABLE_STRING, gpt_partition_type_uuid_to_string_harder(p->type.uuid, uuid_buffer),
TABLE_STRING, empty_to_null(label) ?: "-", TABLE_SET_COLOR, empty_to_null(label) ? NULL : ansi_grey(),
TABLE_UUID, p->new_uuid_is_set ? p->new_uuid : p->current_uuid,
TABLE_UINT64, p->partno,
TABLE_PATH_BASENAME, p->definition_path, TABLE_SET_COLOR, p->definition_path ? NULL : ansi_grey(),
TABLE_STRING, partname ?: "-", TABLE_SET_COLOR, partname ? NULL : ansi_highlight(),
TABLE_UINT64, p->offset,
TABLE_UINT64, p->current_size == UINT64_MAX ? 0 : p->current_size,
TABLE_UINT64, p->new_size,
TABLE_STRING, size_change, TABLE_SET_COLOR, cur_n_partition == n_partitions && sum_size > 0 ? ansi_underline() : NULL,
TABLE_UINT64, p->current_padding == UINT64_MAX ? 0 : p->current_padding,
TABLE_UINT64, p->new_padding,
TABLE_STRING, padding_change, TABLE_SET_COLOR, cur_n_partition == n_partitions && sum_padding > 0 ? ansi_underline() : NULL,
TABLE_STRING, activity ?: "unchanged",
TABLE_STRING, rh,
TABLE_STRV, p->drop_in_files,
TABLE_STRING, empty_to_null(p->split_path) ?: "-");
if (r < 0)
return table_log_add_error(r);
has_roothash = has_roothash || !isempty(rh);
has_dropin_files = has_dropin_files || !strv_isempty(p->drop_in_files);
has_split_path = has_split_path || !isempty(p->split_path);
}
if (!sd_json_format_enabled(arg_json_format_flags) && (sum_padding > 0 || sum_size > 0)) {
const char *a, *b;
a = strjoina(glyph(GLYPH_SIGMA), " = ", FORMAT_BYTES(sum_size));
b = strjoina(glyph(GLYPH_SIGMA), " = ", FORMAT_BYTES(sum_padding));
r = table_add_many(
t,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_STRING, a,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_STRING, b,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY);
if (r < 0)
return table_log_add_error(r);
}
if (!has_roothash) {
r = table_hide_column_from_display(t, roothash_col);
if (r < 0)
return log_error_errno(r, "Failed to set columns to display: %m");
}
if (!has_dropin_files) {
r = table_hide_column_from_display(t, dropin_files_col);
if (r < 0)
return log_error_errno(r, "Failed to set columns to display: %m");
}
if (!has_split_path) {
r = table_hide_column_from_display(t, split_path_col);
if (r < 0)
return log_error_errno(r, "Failed to set columns to display: %m");
}
return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
}
static int context_bar_char_process_partition(
Context *context,
Partition *bar[],
size_t n,
Partition *p,
size_t **start_array,
size_t *n_start_array) {
uint64_t from, to, total;
size_t x, y;
assert(context);
assert(bar);
assert(n > 0);
assert(p);
assert(start_array);
assert(n_start_array);
if (p->dropped)
return 0;
assert(p->offset != UINT64_MAX);
assert(p->new_size != UINT64_MAX);
from = p->offset;
to = from + p->new_size;
assert(context->total > 0);
total = context->total;
assert(from <= total);
x = from * n / total;
assert(to <= total);
y = to * n / total;
assert(x <= y);
assert(y <= n);
for (size_t i = x; i < y; i++)
bar[i] = p;
if (!GREEDY_REALLOC_APPEND(*start_array, *n_start_array, &x, 1))
return log_oom();
return 1;
}
static int partition_hint(const Partition *p, const char *node, char **ret) {
_cleanup_free_ char *buf = NULL;
const char *label;
sd_id128_t id;
assert(ret);
/* Tries really hard to find a suitable description for this partition */
if (p->definition_path)
return path_extract_filename(p->definition_path, ret);
label = partition_label(p);
if (!isempty(label)) {
buf = strdup(label);
goto done;
}
if (p->partno != UINT64_MAX) {
buf = sym_fdisk_partname(node, p->partno+1);
goto done;
}
if (p->new_uuid_is_set)
id = p->new_uuid;
else if (!sd_id128_is_null(p->current_uuid))
id = p->current_uuid;
else
id = p->type.uuid;
buf = strdup(SD_ID128_TO_UUID_STRING(id));
done:
if (!buf)
return -ENOMEM;
*ret = TAKE_PTR(buf);
return 0;
}
static int context_dump_partition_bar(Context *context) {
_cleanup_free_ Partition **bar = NULL;
_cleanup_free_ size_t *start_array = NULL;
size_t n_start_array = 0;
Partition *last = NULL;
bool z = false;
size_t c, j = 0;
int r;
assert_se((c = columns()) >= 2);
c -= 2; /* We do not use the leftmost and rightmost character cell */
bar = new0(Partition*, c);
if (!bar)
return log_oom();
LIST_FOREACH(partitions, p, context->partitions) {
r = context_bar_char_process_partition(context, bar, c, p, &start_array, &n_start_array);
if (r < 0)
return r;
}
putc(' ', stdout);
for (size_t i = 0; i < c; i++) {
if (bar[i]) {
if (last != bar[i])
z = !z;
fputs(z ? ansi_green() : ansi_yellow(), stdout);
fputs(glyph(GLYPH_DARK_SHADE), stdout);
} else {
fputs(ansi_normal(), stdout);
fputs(glyph(GLYPH_LIGHT_SHADE), stdout);
}
last = bar[i];
}
fputs(ansi_normal(), stdout);
putc('\n', stdout);
for (size_t i = 0; i < n_start_array; i++) {
_cleanup_free_ char **line = NULL;
line = new0(char*, c);
if (!line)
return log_oom();
j = 0;
LIST_FOREACH(partitions, p, context->partitions) {
_cleanup_free_ char *d = NULL;
if (p->dropped)
continue;
j++;
if (i < n_start_array - j) {
if (line[start_array[j-1]]) {
const char *e;
/* Upgrade final corner to the right with a branch to the right */
e = startswith(line[start_array[j-1]], glyph(GLYPH_TREE_RIGHT));
if (e) {
d = strjoin(glyph(GLYPH_TREE_BRANCH), e);
if (!d)
return log_oom();
}
}
if (!d) {
d = strdup(glyph(GLYPH_TREE_VERTICAL));
if (!d)
return log_oom();
}
} else if (i == n_start_array - j) {
_cleanup_free_ char *hint = NULL;
(void) partition_hint(p, context->node, &hint);
if (streq_ptr(line[start_array[j-1]], glyph(GLYPH_TREE_VERTICAL)))
d = strjoin(glyph(GLYPH_TREE_BRANCH), " ", strna(hint));
else
d = strjoin(glyph(GLYPH_TREE_RIGHT), " ", strna(hint));
if (!d)
return log_oom();
}
if (d)
free_and_replace(line[start_array[j-1]], d);
}
putc(' ', stdout);
j = 0;
while (j < c) {
if (line[j]) {
fputs(line[j], stdout);
j += utf8_console_width(line[j]);
} else {
putc(' ', stdout);
j++;
}
}
putc('\n', stdout);
for (j = 0; j < c; j++)
free(line[j]);
}
return 0;
}
static bool context_has_roothash(Context *context) {
LIST_FOREACH(partitions, p, context->partitions)
if (iovec_is_set(&p->roothash))
return true;
return false;
}
static int context_dump(Context *context, bool late) {
int r;
assert(context);
if (arg_pretty == 0 && !sd_json_format_enabled(arg_json_format_flags))
return 0;
/* If we're outputting JSON, only dump after doing all operations so we can include the roothashes
* in the output. */
if (!late && sd_json_format_enabled(arg_json_format_flags))
return 0;
/* If we're not outputting JSON, only dump again after doing all operations if there are any
* roothashes that we need to communicate to the user. */
if (late && !sd_json_format_enabled(arg_json_format_flags) && !context_has_roothash(context))
return 0;
r = context_dump_partitions(context);
if (r < 0)
return r;
/* Only write the partition bar once, even if we're writing the partition table twice to communicate
* roothashes. */
if (!sd_json_format_enabled(arg_json_format_flags) && !late) {
putc('\n', stdout);
r = context_dump_partition_bar(context);
if (r < 0)
return r;
putc('\n', stdout);
}
fflush(stdout);
return 0;
}
static bool context_changed(const Context *context) {
assert(context);
LIST_FOREACH(partitions, p, context->partitions) {
if (p->dropped)
continue;
if (p->allocated_to_area)
return true;
if (p->new_size != p->current_size)
return true;
}
return false;
}
static int context_wipe_range(Context *context, uint64_t offset, uint64_t size) {
#if HAVE_BLKID
_cleanup_(blkid_free_probep) blkid_probe probe = NULL;
int r;
assert(context);
assert(offset != UINT64_MAX);
assert(size != UINT64_MAX);
r = dlopen_libblkid(LOG_ERR);
if (r < 0)
return r;
probe = sym_blkid_new_probe();
if (!probe)
return log_oom();
errno = 0;
r = sym_blkid_probe_set_device(probe, sym_fdisk_get_devfd(context->fdisk_context), offset, size);
if (r < 0)
return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to allocate device probe for wiping.");
errno = 0;
if (sym_blkid_probe_enable_superblocks(probe, true) < 0 ||
sym_blkid_probe_set_superblocks_flags(probe, BLKID_SUBLKS_MAGIC|BLKID_SUBLKS_BADCSUM) < 0 ||
sym_blkid_probe_enable_partitions(probe, true) < 0 ||
sym_blkid_probe_set_partitions_flags(probe, BLKID_PARTS_MAGIC) < 0)
return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to enable superblock and partition probing.");
for (;;) {
errno = 0;
r = sym_blkid_do_probe(probe);
if (r < 0)
return log_error_errno(errno_or_else(EIO), "Failed to probe for file systems.");
if (r > 0)
break;
errno = 0;
if (sym_blkid_do_wipe(probe, false) < 0)
return log_error_errno(errno_or_else(EIO), "Failed to wipe file system signature.");
}
return 0;
#else
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
"Cannot wipe partition signatures, libblkid support is not compiled in.");
#endif
}
static int context_wipe_partition(Context *context, Partition *p) {
int r;
assert(context);
assert(p);
assert(!PARTITION_EXISTS(p)); /* Safety check: never wipe existing partitions */
assert(p->offset != UINT64_MAX);
assert(p->new_size != UINT64_MAX);
r = context_wipe_range(context, p->offset, p->new_size);
if (r < 0)
return r;
log_info("Successfully wiped file system signatures from future partition %" PRIu64 ".", p->partno);
return 0;
}
static int context_discard_range(
Context *context,
uint64_t offset,
uint64_t size) {
struct stat st;
int fd;
assert(context);
assert(offset != UINT64_MAX);
assert(size != UINT64_MAX);
if (size <= 0)
return 0;
assert_se((fd = sym_fdisk_get_devfd(context->fdisk_context)) >= 0);
if (fstat(fd, &st) < 0)
return -errno;
if (S_ISREG(st.st_mode)) {
if (fallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, offset, size) < 0) {
if (ERRNO_IS_NOT_SUPPORTED(errno))
return -EOPNOTSUPP;
return -errno;
}
return 1;
}
if (S_ISBLK(st.st_mode)) {
uint64_t range[2], end;
range[0] = round_up_size(offset, context->sector_size);
if (offset > UINT64_MAX - size)
return -ERANGE;
end = offset + size;
if (end <= range[0])
return 0;
range[1] = round_down_size(end - range[0], context->sector_size);
if (range[1] <= 0)
return 0;
if (ioctl(fd, BLKDISCARD, range) < 0) {
if (ERRNO_IS_NOT_SUPPORTED(errno))
return -EOPNOTSUPP;
return -errno;
}
return 1;
}
return -EOPNOTSUPP;
}
static int context_discard_partition(Context *context, Partition *p) {
int r;
assert(context);
assert(p);
assert(p->offset != UINT64_MAX);
assert(p->new_size != UINT64_MAX);
assert(!PARTITION_EXISTS(p)); /* Safety check: never discard existing partitions */
if (!arg_discard)
return 0;
r = context_discard_range(context, p->offset, p->new_size);
if (r == -EOPNOTSUPP) {
log_info("Storage does not support discard, not discarding data in future partition %" PRIu64 ".", p->partno);
return 0;
}
if (r == -EBUSY) {
/* Let's handle this gracefully: https://bugzilla.kernel.org/show_bug.cgi?id=211167 */
log_info("Block device is busy, not discarding partition %" PRIu64 " because it probably is mounted.", p->partno);
return 0;
}
if (r == 0) {
log_info("Partition %" PRIu64 " too short for discard, skipping.", p->partno);
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to discard data for future partition %" PRIu64 ".", p->partno);
log_info("Successfully discarded data from future partition %" PRIu64 ".", p->partno);
p->discarded = true;
return 1;
}
static int context_discard_gap_after(Context *context, Partition *p) {
uint64_t gap, next = UINT64_MAX;
int r;
assert(context);
assert(!p || (p->offset != UINT64_MAX && p->new_size != UINT64_MAX));
if (!arg_discard)
return 0;
if (p)
gap = p->offset + p->new_size;
else
/* The context start gets rounded up to grain_size, however
* existing partitions may be before that so ensure the gap
* starts at the first actually usable lba
*/
gap = sym_fdisk_get_first_lba(context->fdisk_context) * context->sector_size;
LIST_FOREACH(partitions, q, context->partitions) {
if (q->dropped)
continue;
assert(q->offset != UINT64_MAX);
assert(q->new_size != UINT64_MAX);
if (q->offset < gap)
continue;