| /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| |
| #if HAVE_VALGRIND_MEMCHECK_H |
| #include <valgrind/memcheck.h> |
| #endif |
| |
| #include <fcntl.h> |
| #include <getopt.h> |
| #include <libfdisk.h> |
| #include <linux/fs.h> |
| #include <linux/loop.h> |
| #include <sys/file.h> |
| #include <sys/ioctl.h> |
| #include <sys/stat.h> |
| |
| #include <openssl/hmac.h> |
| #include <openssl/sha.h> |
| |
| #include "sd-id128.h" |
| |
| #include "alloc-util.h" |
| #include "blkid-util.h" |
| #include "blockdev-util.h" |
| #include "btrfs-util.h" |
| #include "conf-files.h" |
| #include "conf-parser.h" |
| #include "cryptsetup-util.h" |
| #include "def.h" |
| #include "dirent-util.h" |
| #include "efivars.h" |
| #include "errno-util.h" |
| #include "fd-util.h" |
| #include "fileio.h" |
| #include "format-table.h" |
| #include "format-util.h" |
| #include "fs-util.h" |
| #include "gpt.h" |
| #include "hexdecoct.h" |
| #include "id128-util.h" |
| #include "json.h" |
| #include "list.h" |
| #include "locale-util.h" |
| #include "loop-util.h" |
| #include "main-func.h" |
| #include "mkdir.h" |
| #include "mkfs-util.h" |
| #include "mount-util.h" |
| #include "mountpoint-util.h" |
| #include "parse-argument.h" |
| #include "parse-util.h" |
| #include "path-util.h" |
| #include "pretty-print.h" |
| #include "proc-cmdline.h" |
| #include "process-util.h" |
| #include "random-util.h" |
| #include "resize-fs.h" |
| #include "sort-util.h" |
| #include "specifier.h" |
| #include "stat-util.h" |
| #include "stdio-util.h" |
| #include "string-table.h" |
| #include "string-util.h" |
| #include "strv.h" |
| #include "terminal-util.h" |
| #include "tpm2-util.h" |
| #include "user-util.h" |
| #include "utf8.h" |
| |
| /* If not configured otherwise use a minimal partition size of 10M */ |
| #define DEFAULT_MIN_SIZE (10*1024*1024) |
| |
| /* Hard lower limit for new partition sizes */ |
| #define HARD_MIN_SIZE 4096 |
| |
| /* libfdisk takes off slightly more than 1M of the disk size when creating a GPT disk label */ |
| #define GPT_METADATA_SIZE (1044*1024) |
| |
| /* LUKS2 takes off 16M of the partition size with its metadata by default */ |
| #define LUKS2_METADATA_SIZE (16*1024*1024) |
| |
| /* Note: When growing and placing new partitions we always align to 4K sector size. It's how newer hard disks |
| * are designed, and if everything is aligned to that performance is best. And for older hard disks with 512B |
| * sector size devices were generally assumed to have an even number of sectors, hence at the worst we'll |
| * waste 3K per partition, which is probably fine. */ |
| |
| static enum { |
| EMPTY_REFUSE, /* refuse empty disks, never create a partition table */ |
| EMPTY_ALLOW, /* allow empty disks, create partition table if necessary */ |
| EMPTY_REQUIRE, /* require an empty disk, create a partition table */ |
| EMPTY_FORCE, /* make disk empty, erase everything, create a partition table always */ |
| EMPTY_CREATE, /* create disk as loopback file, create a partition table always */ |
| } arg_empty = EMPTY_REFUSE; |
| |
| static bool arg_dry_run = true; |
| static const char *arg_node = NULL; |
| static char *arg_root = NULL; |
| static char *arg_image = NULL; |
| static char *arg_definitions = NULL; |
| static bool arg_discard = true; |
| static bool arg_can_factory_reset = false; |
| static int arg_factory_reset = -1; |
| static sd_id128_t arg_seed = SD_ID128_NULL; |
| static bool arg_randomize = false; |
| static int arg_pretty = -1; |
| static uint64_t arg_size = UINT64_MAX; |
| static bool arg_size_auto = false; |
| static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; |
| static PagerFlags arg_pager_flags = 0; |
| static bool arg_legend = true; |
| static void *arg_key = NULL; |
| static size_t arg_key_size = 0; |
| static char *arg_tpm2_device = NULL; |
| static uint32_t arg_tpm2_pcr_mask = UINT32_MAX; |
| |
| STATIC_DESTRUCTOR_REGISTER(arg_root, freep); |
| STATIC_DESTRUCTOR_REGISTER(arg_image, freep); |
| STATIC_DESTRUCTOR_REGISTER(arg_definitions, freep); |
| STATIC_DESTRUCTOR_REGISTER(arg_key, erase_and_freep); |
| STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device, freep); |
| |
| typedef struct Partition Partition; |
| typedef struct FreeArea FreeArea; |
| typedef struct Context Context; |
| |
| typedef enum EncryptMode { |
| ENCRYPT_OFF, |
| ENCRYPT_KEY_FILE, |
| ENCRYPT_TPM2, |
| ENCRYPT_KEY_FILE_TPM2, |
| _ENCRYPT_MODE_MAX, |
| _ENCRYPT_MODE_INVALID = -EINVAL, |
| } EncryptMode; |
| |
| struct Partition { |
| char *definition_path; |
| |
| sd_id128_t type_uuid; |
| sd_id128_t current_uuid, new_uuid; |
| char *current_label, *new_label; |
| |
| bool dropped; |
| bool factory_reset; |
| int32_t priority; |
| |
| uint32_t weight, padding_weight; |
| |
| uint64_t current_size, new_size; |
| uint64_t size_min, size_max; |
| |
| uint64_t current_padding, new_padding; |
| uint64_t padding_min, padding_max; |
| |
| uint64_t partno; |
| uint64_t offset; |
| |
| struct fdisk_partition *current_partition; |
| struct fdisk_partition *new_partition; |
| FreeArea *padding_area; |
| FreeArea *allocated_to_area; |
| |
| char *copy_blocks_path; |
| bool copy_blocks_auto; |
| int copy_blocks_fd; |
| uint64_t copy_blocks_size; |
| |
| char *format; |
| char **copy_files; |
| char **make_directories; |
| EncryptMode encrypt; |
| |
| uint64_t gpt_flags; |
| int no_auto; |
| int read_only; |
| int growfs; |
| |
| LIST_FIELDS(Partition, partitions); |
| }; |
| |
| #define PARTITION_IS_FOREIGN(p) (!(p)->definition_path) |
| #define PARTITION_EXISTS(p) (!!(p)->current_partition) |
| |
| struct FreeArea { |
| Partition *after; |
| uint64_t size; |
| uint64_t allocated; |
| }; |
| |
| struct Context { |
| LIST_HEAD(Partition, partitions); |
| size_t n_partitions; |
| |
| FreeArea **free_areas; |
| size_t n_free_areas; |
| |
| uint64_t start, end, total; |
| |
| struct fdisk_context *fdisk_context; |
| |
| sd_id128_t seed; |
| }; |
| |
| static const char *encrypt_mode_table[_ENCRYPT_MODE_MAX] = { |
| [ENCRYPT_OFF] = "off", |
| [ENCRYPT_KEY_FILE] = "key-file", |
| [ENCRYPT_TPM2] = "tpm2", |
| [ENCRYPT_KEY_FILE_TPM2] = "key-file+tpm2", |
| }; |
| |
| #if HAVE_LIBCRYPTSETUP |
| DEFINE_PRIVATE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(encrypt_mode, EncryptMode, ENCRYPT_KEY_FILE); |
| #else |
| DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(encrypt_mode, EncryptMode, ENCRYPT_KEY_FILE); |
| #endif |
| |
| |
| static uint64_t round_down_size(uint64_t v, uint64_t p) { |
| return (v / p) * p; |
| } |
| |
| static uint64_t round_up_size(uint64_t v, uint64_t p) { |
| |
| v = DIV_ROUND_UP(v, p); |
| |
| if (v > UINT64_MAX / p) |
| return UINT64_MAX; /* overflow */ |
| |
| return v * p; |
| } |
| |
| static Partition *partition_new(void) { |
| Partition *p; |
| |
| p = new(Partition, 1); |
| if (!p) |
| return NULL; |
| |
| *p = (Partition) { |
| .weight = 1000, |
| .padding_weight = 0, |
| .current_size = UINT64_MAX, |
| .new_size = UINT64_MAX, |
| .size_min = UINT64_MAX, |
| .size_max = UINT64_MAX, |
| .current_padding = UINT64_MAX, |
| .new_padding = UINT64_MAX, |
| .padding_min = UINT64_MAX, |
| .padding_max = UINT64_MAX, |
| .partno = UINT64_MAX, |
| .offset = UINT64_MAX, |
| .copy_blocks_fd = -1, |
| .copy_blocks_size = UINT64_MAX, |
| .no_auto = -1, |
| .read_only = -1, |
| .growfs = -1, |
| }; |
| |
| return p; |
| } |
| |
| static Partition* partition_free(Partition *p) { |
| if (!p) |
| return NULL; |
| |
| free(p->current_label); |
| free(p->new_label); |
| free(p->definition_path); |
| |
| if (p->current_partition) |
| fdisk_unref_partition(p->current_partition); |
| if (p->new_partition) |
| fdisk_unref_partition(p->new_partition); |
| |
| free(p->copy_blocks_path); |
| safe_close(p->copy_blocks_fd); |
| |
| free(p->format); |
| strv_free(p->copy_files); |
| strv_free(p->make_directories); |
| |
| return mfree(p); |
| } |
| |
| static Partition* partition_unlink_and_free(Context *context, Partition *p) { |
| if (!p) |
| return NULL; |
| |
| LIST_REMOVE(partitions, context->partitions, p); |
| |
| assert(context->n_partitions > 0); |
| context->n_partitions--; |
| |
| return partition_free(p); |
| } |
| |
| DEFINE_TRIVIAL_CLEANUP_FUNC(Partition*, partition_free); |
| |
| static Context *context_new(sd_id128_t seed) { |
| Context *context; |
| |
| context = new(Context, 1); |
| if (!context) |
| return NULL; |
| |
| *context = (Context) { |
| .start = UINT64_MAX, |
| .end = UINT64_MAX, |
| .total = UINT64_MAX, |
| .seed = seed, |
| }; |
| |
| return context; |
| } |
| |
| static void context_free_free_areas(Context *context) { |
| assert(context); |
| |
| for (size_t i = 0; i < context->n_free_areas; i++) |
| free(context->free_areas[i]); |
| |
| context->free_areas = mfree(context->free_areas); |
| context->n_free_areas = 0; |
| } |
| |
| static Context *context_free(Context *context) { |
| if (!context) |
| return NULL; |
| |
| while (context->partitions) |
| partition_unlink_and_free(context, context->partitions); |
| assert(context->n_partitions == 0); |
| |
| context_free_free_areas(context); |
| |
| if (context->fdisk_context) |
| fdisk_unref_context(context->fdisk_context); |
| |
| return mfree(context); |
| } |
| |
| DEFINE_TRIVIAL_CLEANUP_FUNC(Context*, context_free); |
| |
| static int context_add_free_area( |
| Context *context, |
| uint64_t size, |
| Partition *after) { |
| |
| FreeArea *a; |
| |
| assert(context); |
| assert(!after || !after->padding_area); |
| |
| if (!GREEDY_REALLOC(context->free_areas, context->n_free_areas + 1)) |
| return -ENOMEM; |
| |
| a = new(FreeArea, 1); |
| if (!a) |
| return -ENOMEM; |
| |
| *a = (FreeArea) { |
| .size = size, |
| .after = after, |
| }; |
| |
| context->free_areas[context->n_free_areas++] = a; |
| |
| if (after) |
| after->padding_area = a; |
| |
| return 0; |
| } |
| |
| static bool context_drop_one_priority(Context *context) { |
| int32_t priority = 0; |
| Partition *p; |
| bool exists = false; |
| |
| LIST_FOREACH(partitions, p, context->partitions) { |
| if (p->dropped) |
| continue; |
| if (p->priority < priority) |
| continue; |
| if (p->priority == priority) { |
| exists = exists || PARTITION_EXISTS(p); |
| continue; |
| } |
| |
| priority = p->priority; |
| exists = PARTITION_EXISTS(p); |
| } |
| |
| /* Refuse to drop partitions with 0 or negative priorities or partitions of priorities that have at |
| * least one existing priority */ |
| if (priority <= 0 || exists) |
| return false; |
| |
| LIST_FOREACH(partitions, p, context->partitions) { |
| if (p->priority < priority) |
| continue; |
| |
| if (p->dropped) |
| continue; |
| |
| p->dropped = true; |
| log_info("Can't fit partition %s of priority %" PRIi32 ", dropping.", p->definition_path, p->priority); |
| } |
| |
| return true; |
| } |
| |
| static uint64_t partition_min_size(const Partition *p) { |
| uint64_t sz; |
| |
| /* Calculate the disk space we really need at minimum for this partition. If the partition already |
| * exists the current size is what we really need. If it doesn't exist yet refuse to allocate less |
| * than 4K. |
| * |
| * DEFAULT_MIN_SIZE is the default SizeMin= we configure if nothing else is specified. */ |
| |
| if (PARTITION_IS_FOREIGN(p)) { |
| /* Don't allow changing size of partitions not managed by us */ |
| assert(p->current_size != UINT64_MAX); |
| return p->current_size; |
| } |
| |
| sz = p->current_size != UINT64_MAX ? p->current_size : HARD_MIN_SIZE; |
| |
| if (!PARTITION_EXISTS(p)) { |
| uint64_t d = 0; |
| |
| if (p->encrypt != ENCRYPT_OFF) |
| d += round_up_size(LUKS2_METADATA_SIZE, 4096); |
| |
| if (p->copy_blocks_size != UINT64_MAX) |
| d += round_up_size(p->copy_blocks_size, 4096); |
| else if (p->format || p->encrypt != ENCRYPT_OFF) { |
| uint64_t f; |
| |
| /* If we shall synthesize a file system, take minimal fs size into account (assumed to be 4K if not known) */ |
| f = p->format ? minimal_size_by_fs_name(p->format) : UINT64_MAX; |
| d += f == UINT64_MAX ? 4096 : f; |
| } |
| |
| if (d > sz) |
| sz = d; |
| } |
| |
| return MAX(p->size_min != UINT64_MAX ? p->size_min : DEFAULT_MIN_SIZE, sz); |
| } |
| |
| static uint64_t partition_max_size(const Partition *p) { |
| /* Calculate how large the partition may become at max. This is generally the configured maximum |
| * size, except when it already exists and is larger than that. In that case it's the existing size, |
| * since we never want to shrink partitions. */ |
| |
| if (PARTITION_IS_FOREIGN(p)) { |
| /* Don't allow changing size of partitions not managed by us */ |
| assert(p->current_size != UINT64_MAX); |
| return p->current_size; |
| } |
| |
| if (p->current_size != UINT64_MAX) |
| return MAX(p->current_size, p->size_max); |
| |
| return p->size_max; |
| } |
| |
| static uint64_t partition_min_size_with_padding(const Partition *p) { |
| uint64_t sz; |
| |
| /* Calculate the disk space we need for this partition plus any free space coming after it. This |
| * takes user configured padding into account as well as any additional whitespace needed to align |
| * the next partition to 4K again. */ |
| |
| sz = partition_min_size(p); |
| |
| if (p->padding_min != UINT64_MAX) |
| sz += p->padding_min; |
| |
| if (PARTITION_EXISTS(p)) { |
| /* If the partition wasn't aligned, add extra space so that any we might add will be aligned */ |
| assert(p->offset != UINT64_MAX); |
| return round_up_size(p->offset + sz, 4096) - p->offset; |
| } |
| |
| /* If this is a new partition we'll place it aligned, hence we just need to round up the required size here */ |
| return round_up_size(sz, 4096); |
| } |
| |
| static uint64_t free_area_available(const FreeArea *a) { |
| assert(a); |
| |
| /* Determines how much of this free area is not allocated yet */ |
| |
| assert(a->size >= a->allocated); |
| return a->size - a->allocated; |
| } |
| |
| static uint64_t free_area_available_for_new_partitions(const FreeArea *a) { |
| uint64_t avail; |
| |
| /* Similar to free_area_available(), but takes into account that the required size and padding of the |
| * preceding partition is honoured. */ |
| |
| avail = free_area_available(a); |
| if (a->after) { |
| uint64_t need, space; |
| |
| need = partition_min_size_with_padding(a->after); |
| |
| assert(a->after->offset != UINT64_MAX); |
| assert(a->after->current_size != UINT64_MAX); |
| |
| space = round_up_size(a->after->offset + a->after->current_size, 4096) - a->after->offset + avail; |
| if (need >= space) |
| return 0; |
| |
| return space - need; |
| } |
| |
| return avail; |
| } |
| |
| static int free_area_compare(FreeArea *const *a, FreeArea *const*b) { |
| return CMP(free_area_available_for_new_partitions(*a), |
| free_area_available_for_new_partitions(*b)); |
| } |
| |
| static uint64_t charge_size(uint64_t total, uint64_t amount) { |
| uint64_t rounded; |
| |
| assert(amount <= total); |
| |
| /* Subtract the specified amount from total, rounding up to multiple of 4K if there's room */ |
| rounded = round_up_size(amount, 4096); |
| if (rounded >= total) |
| return 0; |
| |
| return total - rounded; |
| } |
| |
| static uint64_t charge_weight(uint64_t total, uint64_t amount) { |
| assert(amount <= total); |
| return total - amount; |
| } |
| |
| static bool context_allocate_partitions(Context *context) { |
| Partition *p; |
| |
| assert(context); |
| |
| /* A simple first-fit algorithm, assuming the array of free areas is sorted by size in decreasing |
| * order. */ |
| |
| LIST_FOREACH(partitions, p, context->partitions) { |
| bool fits = false; |
| uint64_t required; |
| FreeArea *a = NULL; |
| |
| /* Skip partitions we already dropped or that already exist */ |
| if (p->dropped || PARTITION_EXISTS(p)) |
| continue; |
| |
| /* Sort by size */ |
| typesafe_qsort(context->free_areas, context->n_free_areas, free_area_compare); |
| |
| /* How much do we need to fit? */ |
| required = partition_min_size_with_padding(p); |
| assert(required % 4096 == 0); |
| |
| for (size_t i = 0; i < context->n_free_areas; i++) { |
| a = context->free_areas[i]; |
| |
| if (free_area_available_for_new_partitions(a) >= required) { |
| fits = true; |
| break; |
| } |
| } |
| |
| if (!fits) |
| return false; /* 😢 Oh no! We can't fit this partition into any free area! */ |
| |
| /* Assign the partition to this free area */ |
| p->allocated_to_area = a; |
| |
| /* Budget the minimal partition size */ |
| a->allocated += required; |
| } |
| |
| return true; |
| } |
| |
| static int context_sum_weights(Context *context, FreeArea *a, uint64_t *ret) { |
| uint64_t weight_sum = 0; |
| Partition *p; |
| |
| assert(context); |
| assert(a); |
| assert(ret); |
| |
| /* Determine the sum of the weights of all partitions placed in or before the specified free area */ |
| |
| LIST_FOREACH(partitions, p, context->partitions) { |
| if (p->padding_area != a && p->allocated_to_area != a) |
| continue; |
| |
| if (p->weight > UINT64_MAX - weight_sum) |
| goto overflow_sum; |
| weight_sum += p->weight; |
| |
| if (p->padding_weight > UINT64_MAX - weight_sum) |
| goto overflow_sum; |
| weight_sum += p->padding_weight; |
| } |
| |
| *ret = weight_sum; |
| return 0; |
| |
| overflow_sum: |
| return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Combined weight of partition exceeds unsigned 64bit range, refusing."); |
| } |
| |
| static int scale_by_weight(uint64_t value, uint64_t weight, uint64_t weight_sum, uint64_t *ret) { |
| assert(weight_sum >= weight); |
| assert(ret); |
| |
| if (weight == 0) { |
| *ret = 0; |
| return 0; |
| } |
| |
| if (value > UINT64_MAX / weight) |
| return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Scaling by weight of partition exceeds unsigned 64bit range, refusing."); |
| |
| *ret = value * weight / weight_sum; |
| return 0; |
| } |
| |
| typedef enum GrowPartitionPhase { |
| /* The first phase: we charge partitions which need more (according to constraints) than their weight-based share. */ |
| PHASE_OVERCHARGE, |
| |
| /* The second phase: we charge partitions which need less (according to constraints) than their weight-based share. */ |
| PHASE_UNDERCHARGE, |
| |
| /* The third phase: we distribute what remains among the remaining partitions, according to the weights */ |
| PHASE_DISTRIBUTE, |
| } GrowPartitionPhase; |
| |
| static int context_grow_partitions_phase( |
| Context *context, |
| FreeArea *a, |
| GrowPartitionPhase phase, |
| uint64_t *span, |
| uint64_t *weight_sum) { |
| |
| Partition *p; |
| int r; |
| |
| assert(context); |
| assert(a); |
| |
| /* Now let's look at the intended weights and adjust them taking the minimum space assignments into |
| * account. i.e. if a partition has a small weight but a high minimum space value set it should not |
| * get any additional room from the left-overs. Similar, if two partitions have the same weight they |
| * should get the same space if possible, even if one has a smaller minimum size than the other. */ |
| LIST_FOREACH(partitions, p, context->partitions) { |
| |
| /* Look only at partitions associated with this free area, i.e. immediately |
| * preceding it, or allocated into it */ |
| if (p->allocated_to_area != a && p->padding_area != a) |
| continue; |
| |
| if (p->new_size == UINT64_MAX) { |
| bool charge = false, try_again = false; |
| uint64_t share, rsz, xsz; |
| |
| /* Calculate how much this space this partition needs if everyone would get |
| * the weight based share */ |
| r = scale_by_weight(*span, p->weight, *weight_sum, &share); |
| if (r < 0) |
| return r; |
| |
| rsz = partition_min_size(p); |
| xsz = partition_max_size(p); |
| |
| if (phase == PHASE_OVERCHARGE && rsz > share) { |
| /* This partition needs more than its calculated share. Let's assign |
| * it that, and take this partition out of all calculations and start |
| * again. */ |
| |
| p->new_size = rsz; |
| charge = try_again = true; |
| |
| } else if (phase == PHASE_UNDERCHARGE && xsz != UINT64_MAX && xsz < share) { |
| /* This partition accepts less than its calculated |
| * share. Let's assign it that, and take this partition out |
| * of all calculations and start again. */ |
| |
| p->new_size = xsz; |
| charge = try_again = true; |
| |
| } else if (phase == PHASE_DISTRIBUTE) { |
| /* This partition can accept its calculated share. Let's |
| * assign it. There's no need to restart things here since |
| * assigning this shouldn't impact the shares of the other |
| * partitions. */ |
| |
| if (PARTITION_IS_FOREIGN(p)) |
| /* Never change of foreign partitions (i.e. those we don't manage) */ |
| p->new_size = p->current_size; |
| else |
| p->new_size = MAX(round_down_size(share, 4096), rsz); |
| |
| charge = true; |
| } |
| |
| if (charge) { |
| *span = charge_size(*span, p->new_size); |
| *weight_sum = charge_weight(*weight_sum, p->weight); |
| } |
| |
| if (try_again) |
| return 0; /* try again */ |
| } |
| |
| if (p->new_padding == UINT64_MAX) { |
| bool charge = false, try_again = false; |
| uint64_t share; |
| |
| r = scale_by_weight(*span, p->padding_weight, *weight_sum, &share); |
| if (r < 0) |
| return r; |
| |
| if (phase == PHASE_OVERCHARGE && p->padding_min != UINT64_MAX && p->padding_min > share) { |
| p->new_padding = p->padding_min; |
| charge = try_again = true; |
| } else if (phase == PHASE_UNDERCHARGE && p->padding_max != UINT64_MAX && p->padding_max < share) { |
| p->new_padding = p->padding_max; |
| charge = try_again = true; |
| } else if (phase == PHASE_DISTRIBUTE) { |
| |
| p->new_padding = round_down_size(share, 4096); |
| if (p->padding_min != UINT64_MAX && p->new_padding < p->padding_min) |
| p->new_padding = p->padding_min; |
| |
| charge = true; |
| } |
| |
| if (charge) { |
| *span = charge_size(*span, p->new_padding); |
| *weight_sum = charge_weight(*weight_sum, p->padding_weight); |
| } |
| |
| if (try_again) |
| return 0; /* try again */ |
| } |
| } |
| |
| return 1; /* done */ |
| } |
| |
| static int context_grow_partitions_on_free_area(Context *context, FreeArea *a) { |
| uint64_t weight_sum = 0, span; |
| int r; |
| |
| assert(context); |
| assert(a); |
| |
| r = context_sum_weights(context, a, &weight_sum); |
| if (r < 0) |
| return r; |
| |
| /* Let's calculate the total area covered by this free area and the partition before it */ |
| span = a->size; |
| if (a->after) { |
| assert(a->after->offset != UINT64_MAX); |
| assert(a->after->current_size != UINT64_MAX); |
| |
| span += round_up_size(a->after->offset + a->after->current_size, 4096) - a->after->offset; |
| } |
| |
| GrowPartitionPhase phase = PHASE_OVERCHARGE; |
| for (;;) { |
| r = context_grow_partitions_phase(context, a, phase, &span, &weight_sum); |
| if (r < 0) |
| return r; |
| if (r == 0) /* not done yet, re-run this phase */ |
| continue; |
| |
| if (phase == PHASE_OVERCHARGE) |
| phase = PHASE_UNDERCHARGE; |
| else if (phase == PHASE_UNDERCHARGE) |
| phase = PHASE_DISTRIBUTE; |
| else if (phase == PHASE_DISTRIBUTE) |
| break; |
| } |
| |
| /* We still have space left over? Donate to preceding partition if we have one */ |
| if (span > 0 && a->after && !PARTITION_IS_FOREIGN(a->after)) { |
| uint64_t m, xsz; |
| |
| assert(a->after->new_size != UINT64_MAX); |
| m = a->after->new_size + span; |
| |
| xsz = partition_max_size(a->after); |
| if (xsz != UINT64_MAX && m > xsz) |
| m = xsz; |
| |
| span = charge_size(span, m - a->after->new_size); |
| a->after->new_size = m; |
| } |
| |
| /* What? Even still some space left (maybe because there was no preceding partition, or it had a |
| * size limit), then let's donate it to whoever wants it. */ |
| if (span > 0) { |
| Partition *p; |
| |
| LIST_FOREACH(partitions, p, context->partitions) { |
| uint64_t m, xsz; |
| |
| if (p->allocated_to_area != a) |
| continue; |
| |
| if (PARTITION_IS_FOREIGN(p)) |
| continue; |
| |
| assert(p->new_size != UINT64_MAX); |
| m = p->new_size + span; |
| |
| xsz = partition_max_size(p); |
| if (xsz != UINT64_MAX && m > xsz) |
| m = xsz; |
| |
| span = charge_size(span, m - p->new_size); |
| p->new_size = m; |
| |
| if (span == 0) |
| break; |
| } |
| } |
| |
| /* Yuck, still no one? Then make it padding */ |
| if (span > 0 && a->after) { |
| assert(a->after->new_padding != UINT64_MAX); |
| a->after->new_padding += span; |
| } |
| |
| return 0; |
| } |
| |
| static int context_grow_partitions(Context *context) { |
| Partition *p; |
| int r; |
| |
| assert(context); |
| |
| for (size_t i = 0; i < context->n_free_areas; i++) { |
| r = context_grow_partitions_on_free_area(context, context->free_areas[i]); |
| if (r < 0) |
| return r; |
| } |
| |
| /* All existing partitions that have no free space after them can't change size */ |
| LIST_FOREACH(partitions, p, context->partitions) { |
| if (p->dropped) |
| continue; |
| |
| if (!PARTITION_EXISTS(p) || p->padding_area) { |
| /* The algorithm above must have initialized this already */ |
| assert(p->new_size != UINT64_MAX); |
| continue; |
| } |
| |
| assert(p->new_size == UINT64_MAX); |
| p->new_size = p->current_size; |
| |
| assert(p->new_padding == UINT64_MAX); |
| p->new_padding = p->current_padding; |
| } |
| |
| return 0; |
| } |
| |
| static void context_place_partitions(Context *context) { |
| uint64_t partno = 0; |
| Partition *p; |
| |
| assert(context); |
| |
| /* Determine next partition number to assign */ |
| LIST_FOREACH(partitions, p, context->partitions) { |
| if (!PARTITION_EXISTS(p)) |
| continue; |
| |
| assert(p->partno != UINT64_MAX); |
| if (p->partno >= partno) |
| partno = p->partno + 1; |
| } |
| |
| for (size_t i = 0; i < context->n_free_areas; i++) { |
| FreeArea *a = context->free_areas[i]; |
| uint64_t start, left; |
| |
| if (a->after) { |
| assert(a->after->offset != UINT64_MAX); |
| assert(a->after->new_size != UINT64_MAX); |
| assert(a->after->new_padding != UINT64_MAX); |
| |
| start = a->after->offset + a->after->new_size + a->after->new_padding; |
| } else |
| start = context->start; |
| |
| start = round_up_size(start, 4096); |
| left = a->size; |
| |
| LIST_FOREACH(partitions, p, context->partitions) { |
| if (p->allocated_to_area != a) |
| continue; |
| |
| p->offset = start; |
| p->partno = partno++; |
| |
| assert(left >= p->new_size); |
| start += p->new_size; |
| left -= p->new_size; |
| |
| assert(left >= p->new_padding); |
| start += p->new_padding; |
| left -= p->new_padding; |
| } |
| } |
| } |
| |
| static int config_parse_type( |
| const char *unit, |
| const char *filename, |
| unsigned line, |
| const char *section, |
| unsigned section_line, |
| const char *lvalue, |
| int ltype, |
| const char *rvalue, |
| void *data, |
| void *userdata) { |
| |
| sd_id128_t *type_uuid = data; |
| int r; |
| |
| assert(rvalue); |
| assert(type_uuid); |
| |
| r = gpt_partition_type_uuid_from_string(rvalue, type_uuid); |
| if (r < 0) |
| return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse partition type: %s", rvalue); |
| |
| return 0; |
| } |
| |
| static int config_parse_label( |
| const char *unit, |
| const char *filename, |
| unsigned line, |
| const char *section, |
| unsigned section_line, |
| const char *lvalue, |
| int ltype, |
| const char *rvalue, |
| void *data, |
| void *userdata) { |
| |
| _cleanup_free_ char *resolved = NULL; |
| char **label = data; |
| int r; |
| |
| assert(rvalue); |
| assert(label); |
| |
| /* Nota bene: the empty label is a totally valid one. Let's hence not follow our usual rule of |
| * assigning the empty string to reset to default here, but really accept it as label to set. */ |
| |
| r = specifier_printf(rvalue, GPT_LABEL_MAX, system_and_tmp_specifier_table, arg_root, NULL, &resolved); |
| if (r < 0) { |
| log_syntax(unit, LOG_WARNING, filename, line, r, |
| "Failed to expand specifiers in Label=, ignoring: %s", rvalue); |
| return 0; |
| } |
| |
| if (!utf8_is_valid(resolved)) { |
| log_syntax(unit, LOG_WARNING, filename, line, 0, |
| "Partition label not valid UTF-8, ignoring: %s", rvalue); |
| return 0; |
| } |
| |
| r = gpt_partition_label_valid(resolved); |
| if (r < 0) { |
| log_syntax(unit, LOG_WARNING, filename, line, r, |
| "Failed to check if string is valid as GPT partition label, ignoring: \"%s\" (from \"%s\")", |
| resolved, rvalue); |
| return 0; |
| } |
| if (!r) { |
| log_syntax(unit, LOG_WARNING, filename, line, 0, |
| "Partition label too long for GPT table, ignoring: \"%s\" (from \"%s\")", |
| resolved, rvalue); |
| return 0; |
| } |
| |
| free_and_replace(*label, resolved); |
| return 0; |
| } |
| |
| static int config_parse_weight( |
| const char *unit, |
| const char *filename, |
| unsigned line, |
| const char *section, |
| unsigned section_line, |
| const char *lvalue, |
| int ltype, |
| const char *rvalue, |
| void *data, |
| void *userdata) { |
| |
| uint32_t *priority = data, v; |
| int r; |
| |
| assert(rvalue); |
| assert(priority); |
| |
| r = safe_atou32(rvalue, &v); |
| if (r < 0) { |
| log_syntax(unit, LOG_WARNING, filename, line, r, |
| "Failed to parse weight value, ignoring: %s", rvalue); |
| return 0; |
| } |
| |
| if (v > 1000U*1000U) { |
| log_syntax(unit, LOG_WARNING, filename, line, 0, |
| "Weight needs to be in range 0…10000000, ignoring: %" PRIu32, v); |
| return 0; |
| } |
| |
| *priority = v; |
| return 0; |
| } |
| |
| static int config_parse_size4096( |
| const char *unit, |
| const char *filename, |
| unsigned line, |
| const char *section, |
| unsigned section_line, |
| const char *lvalue, |
| int ltype, |
| const char *rvalue, |
| void *data, |
| void *userdata) { |
| |
| uint64_t *sz = data, parsed; |
| int r; |
| |
| assert(rvalue); |
| assert(data); |
| |
| r = parse_size(rvalue, 1024, &parsed); |
| if (r < 0) |
| return log_syntax(unit, LOG_ERR, filename, line, r, |
| "Failed to parse size value: %s", rvalue); |
| |
| if (ltype > 0) |
| *sz = round_up_size(parsed, 4096); |
| else if (ltype < 0) |
| *sz = round_down_size(parsed, 4096); |
| else |
| *sz = parsed; |
| |
| if (*sz != parsed) |
| log_syntax(unit, LOG_NOTICE, filename, line, r, "Rounded %s= size %" PRIu64 " → %" PRIu64 ", a multiple of 4096.", lvalue, parsed, *sz); |
| |
| return 0; |
| } |
| |
| static int config_parse_fstype( |
| const char *unit, |
| const char *filename, |
| unsigned line, |
| const char *section, |
| unsigned section_line, |
| const char *lvalue, |
| int ltype, |
| const char *rvalue, |
| void *data, |
| void *userdata) { |
| |
| char **fstype = data; |
| |
| assert(rvalue); |
| assert(data); |
| |
| if (!filename_is_valid(rvalue)) |
| return log_syntax(unit, LOG_ERR, filename, line, 0, |
| "File system type is not valid, refusing: %s", rvalue); |
| |
| return free_and_strdup_warn(fstype, rvalue); |
| } |
| |
| static int config_parse_copy_files( |
| const char *unit, |
| const char *filename, |
| unsigned line, |
| const char *section, |
| unsigned section_line, |
| const char *lvalue, |
| int ltype, |
| const char *rvalue, |
| void *data, |
| void *userdata) { |
| |
| _cleanup_free_ char *source = NULL, *buffer = NULL, *resolved_source = NULL, *resolved_target = NULL; |
| const char *p = rvalue, *target; |
| Partition *partition = data; |
| int r; |
| |
| assert(rvalue); |
| assert(partition); |
| |
| r = extract_first_word(&p, &source, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS); |
| if (r < 0) |
| return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract source path: %s", rvalue); |
| if (r == 0) { |
| log_syntax(unit, LOG_WARNING, filename, line, 0, "No argument specified: %s", rvalue); |
| return 0; |
| } |
| |
| r = extract_first_word(&p, &buffer, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS); |
| if (r < 0) |
| return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract target path: %s", rvalue); |
| if (r == 0) |
| target = source; /* No target, then it's the same as the source */ |
| else |
| target = buffer; |
| |
| if (!isempty(p)) |
| return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL), "Too many arguments: %s", rvalue); |
| |
| r = specifier_printf(source, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_source); |
| if (r < 0) { |
| log_syntax(unit, LOG_WARNING, filename, line, r, |
| "Failed to expand specifiers in CopyFiles= source, ignoring: %s", rvalue); |
| return 0; |
| } |
| |
| r = path_simplify_and_warn(resolved_source, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
| if (r < 0) |
| return 0; |
| |
| r = specifier_printf(target, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_target); |
| if (r < 0) { |
| log_syntax(unit, LOG_WARNING, filename, line, r, |
| "Failed to expand specifiers in CopyFiles= target, ignoring: %s", resolved_target); |
| return 0; |
| } |
| |
| r = path_simplify_and_warn(resolved_target, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
| if (r < 0) |
| return 0; |
| |
| r = strv_consume_pair(&partition->copy_files, TAKE_PTR(resolved_source), TAKE_PTR(resolved_target)); |
| if (r < 0) |
| return log_oom(); |
| |
| return 0; |
| } |
| |
| static int config_parse_copy_blocks( |
| const char *unit, |
| const char *filename, |
| unsigned line, |
| const char *section, |
| unsigned section_line, |
| const char *lvalue, |
| int ltype, |
| const char *rvalue, |
| void *data, |
| void *userdata) { |
| |
| _cleanup_free_ char *d = NULL; |
| Partition *partition = data; |
| int r; |
| |
| assert(rvalue); |
| assert(partition); |
| |
| if (isempty(rvalue)) { |
| partition->copy_blocks_path = mfree(partition->copy_blocks_path); |
| partition->copy_blocks_auto = false; |
| return 0; |
| } |
| |
| if (streq(rvalue, "auto")) { |
| partition->copy_blocks_path = mfree(partition->copy_blocks_path); |
| partition->copy_blocks_auto = true; |
| return 0; |
| } |
| |
| r = specifier_printf(rvalue, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &d); |
| if (r < 0) { |
| log_syntax(unit, LOG_WARNING, filename, line, r, |
| "Failed to expand specifiers in CopyBlocks= source path, ignoring: %s", rvalue); |
| return 0; |
| } |
| |
| r = path_simplify_and_warn(d, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
| if (r < 0) |
| return 0; |
| |
| free_and_replace(partition->copy_blocks_path, d); |
| partition->copy_blocks_auto = false; |
| return 0; |
| } |
| |
| static int config_parse_make_dirs( |
| const char *unit, |
| const char *filename, |
| unsigned line, |
| const char *section, |
| unsigned section_line, |
| const char *lvalue, |
| int ltype, |
| const char *rvalue, |
| void *data, |
| void *userdata) { |
| |
| Partition *partition = data; |
| const char *p = rvalue; |
| int r; |
| |
| assert(rvalue); |
| assert(partition); |
| |
| for (;;) { |
| _cleanup_free_ char *word = NULL, *d = NULL; |
| |
| r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE); |
| if (r == -ENOMEM) |
| return log_oom(); |
| if (r < 0) { |
| log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue); |
| return 0; |
| } |
| if (r == 0) |
| return 0; |
| |
| r = specifier_printf(word, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &d); |
| if (r < 0) { |
| log_syntax(unit, LOG_WARNING, filename, line, r, |
| "Failed to expand specifiers in MakeDirectories= parameter, ignoring: %s", word); |
| continue; |
| } |
| |
| r = path_simplify_and_warn(d, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
| if (r < 0) |
| continue; |
| |
| r = strv_consume(&partition->make_directories, TAKE_PTR(d)); |
| if (r < 0) |
| return log_oom(); |
| } |
| } |
| |
| static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_encrypt, encrypt_mode, EncryptMode, ENCRYPT_OFF, "Invalid encryption mode"); |
| |
| static int config_parse_gpt_flags( |
| const char *unit, |
| const char *filename, |
| unsigned line, |
| const char *section, |
| unsigned section_line, |
| const char *lvalue, |
| int ltype, |
| const char *rvalue, |
| void *data, |
| void *userdata) { |
| |
| uint64_t *gpt_flags = data; |
| int r; |
| |
| assert(rvalue); |
| assert(gpt_flags); |
| |
| r = safe_atou64(rvalue, gpt_flags); |
| if (r < 0) { |
| log_syntax(unit, LOG_WARNING, filename, line, r, |
| "Failed to parse Flags= value, ignoring: %s", rvalue); |
| return 0; |
| } |
| |
| return 0; |
| } |
| |
| static int partition_read_definition(Partition *p, const char *path) { |
| |
| ConfigTableItem table[] = { |
| { "Partition", "Type", config_parse_type, 0, &p->type_uuid }, |
| { "Partition", "Label", config_parse_label, 0, &p->new_label }, |
| { "Partition", "UUID", config_parse_id128, 0, &p->new_uuid }, |
| { "Partition", "Priority", config_parse_int32, 0, &p->priority }, |
| { "Partition", "Weight", config_parse_weight, 0, &p->weight }, |
| { "Partition", "PaddingWeight", config_parse_weight, 0, &p->padding_weight }, |
| { "Partition", "SizeMinBytes", config_parse_size4096, 1, &p->size_min }, |
| { "Partition", "SizeMaxBytes", config_parse_size4096, -1, &p->size_max }, |
| { "Partition", "PaddingMinBytes", config_parse_size4096, 1, &p->padding_min }, |
| { "Partition", "PaddingMaxBytes", config_parse_size4096, -1, &p->padding_max }, |
| { "Partition", "FactoryReset", config_parse_bool, 0, &p->factory_reset }, |
| { "Partition", "CopyBlocks", config_parse_copy_blocks, 0, p }, |
| { "Partition", "Format", config_parse_fstype, 0, &p->format }, |
| { "Partition", "CopyFiles", config_parse_copy_files, 0, p }, |
| { "Partition", "MakeDirectories", config_parse_make_dirs, 0, p }, |
| { "Partition", "Encrypt", config_parse_encrypt, 0, &p->encrypt }, |
| { "Partition", "Flags", config_parse_gpt_flags, 0, &p->gpt_flags }, |
| { "Partition", "ReadOnly", config_parse_tristate, 0, &p->read_only }, |
| { "Partition", "NoAuto", config_parse_tristate, 0, &p->no_auto }, |
| { "Partition", "GrowFileSystem", config_parse_tristate, 0, &p->growfs }, |
| {} |
| }; |
| int r; |
| |
| r = config_parse(NULL, path, NULL, |
| "Partition\0", |
| config_item_table_lookup, table, |
| CONFIG_PARSE_WARN, |
| p, |
| NULL); |
| if (r < 0) |
| return r; |
| |
| if (p->size_min != UINT64_MAX && p->size_max != UINT64_MAX && p->size_min > p->size_max) |
| return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| "SizeMinBytes= larger than SizeMaxBytes=, refusing."); |
| |
| if (p->padding_min != UINT64_MAX && p->padding_max != UINT64_MAX && p->padding_min > p->padding_max) |
| return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| "PaddingMinBytes= larger than PaddingMaxBytes=, refusing."); |
| |
| if (sd_id128_is_null(p->type_uuid)) |
| return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| "Type= not defined, refusing."); |
| |
| if ((p->copy_blocks_path || p->copy_blocks_auto) && |
| (p->format || !strv_isempty(p->copy_files) || !strv_isempty(p->make_directories))) |
| return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| "Format=/CopyFiles=/MakeDirectories= and CopyBlocks= cannot be combined, refusing."); |
| |
| if ((!strv_isempty(p->copy_files) || !strv_isempty(p->make_directories)) && streq_ptr(p->format, "swap")) |
| return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| "Format=swap and CopyFiles= cannot be combined, refusing."); |
| |
| if (!p->format && (!strv_isempty(p->copy_files) || !strv_isempty(p->make_directories) || (p->encrypt != ENCRYPT_OFF && !(p->copy_blocks_path || p->copy_blocks_auto)))) { |
| /* Pick "ext4" as file system if we are configured to copy files or encrypt the device */ |
| p->format = strdup("ext4"); |
| if (!p->format) |
| return log_oom(); |
| } |
| |
| /* Verity partitions are read only, let's imply the RO flag hence, unless explicitly configured otherwise. */ |
| if ((gpt_partition_type_is_root_verity(p->type_uuid) || |
| gpt_partition_type_is_usr_verity(p->type_uuid)) && |
| p->read_only < 0) |
| p->read_only = true; |
| |
| /* Default to "growfs" on, unless read-only */ |
| if (gpt_partition_type_knows_growfs(p->type_uuid) && |
| p->read_only <= 0) |
| p->growfs = true; |
| |
| return 0; |
| } |
| |
| static int context_read_definitions( |
| Context *context, |
| const char *directory, |
| const char *root) { |
| |
| _cleanup_strv_free_ char **files = NULL; |
| Partition *last = NULL; |
| char **f; |
| int r; |
| |
| assert(context); |
| |
| if (directory) |
| r = conf_files_list_strv(&files, ".conf", NULL, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, (const char**) STRV_MAKE(directory)); |
| else |
| r = conf_files_list_strv(&files, ".conf", root, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, (const char**) CONF_PATHS_STRV("repart.d")); |
| if (r < 0) |
| return log_error_errno(r, "Failed to enumerate *.conf files: %m"); |
| |
| STRV_FOREACH(f, files) { |
| _cleanup_(partition_freep) Partition *p = NULL; |
| |
| p = partition_new(); |
| if (!p) |
| return log_oom(); |
| |
| p->definition_path = strdup(*f); |
| if (!p->definition_path) |
| return log_oom(); |
| |
| r = partition_read_definition(p, *f); |
| if (r < 0) |
| return r; |
| |
| LIST_INSERT_AFTER(partitions, context->partitions, last, p); |
| last = TAKE_PTR(p); |
| context->n_partitions++; |
| } |
| |
| return 0; |
| } |
| |
| DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_context*, fdisk_unref_context, NULL); |
| DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_partition*, fdisk_unref_partition, NULL); |
| DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_parttype*, fdisk_unref_parttype, NULL); |
| DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_table*, fdisk_unref_table, NULL); |
| |
| static int determine_current_padding( |
| struct fdisk_context *c, |
| struct fdisk_table *t, |
| struct fdisk_partition *p, |
| uint64_t *ret) { |
| |
| size_t n_partitions; |
| uint64_t offset, next = UINT64_MAX; |
| |
| assert(c); |
| assert(t); |
| assert(p); |
| |
| if (!fdisk_partition_has_end(p)) |
| return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition has no end!"); |
| |
| offset = fdisk_partition_get_end(p); |
| assert(offset < UINT64_MAX / 512); |
| offset *= 512; |
| |
| n_partitions = fdisk_table_get_nents(t); |
| for (size_t i = 0; i < n_partitions; i++) { |
| struct fdisk_partition *q; |
| uint64_t start; |
| |
| q = fdisk_table_get_partition(t, i); |
| if (!q) |
| return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m"); |
| |
| if (fdisk_partition_is_used(q) <= 0) |
| continue; |
| |
| if (!fdisk_partition_has_start(q)) |
| continue; |
| |
| start = fdisk_partition_get_start(q); |
| assert(start < UINT64_MAX / 512); |
| start *= 512; |
| |
| if (start >= offset && (next == UINT64_MAX || next > start)) |
| next = start; |
| } |
| |
| if (next == UINT64_MAX) { |
| /* No later partition? In that case check the end of the usable area */ |
| next = fdisk_get_last_lba(c); |
| assert(next < UINT64_MAX); |
| next++; /* The last LBA is one sector before the end */ |
| |
| assert(next < UINT64_MAX / 512); |
| next *= 512; |
| |
| if (offset > next) |
| return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end."); |
| } |
| |
| assert(next >= offset); |
| offset = round_up_size(offset, 4096); |
| next = round_down_size(next, 4096); |
| |
| if (next >= offset) /* Check again, rounding might have fucked things up */ |
| *ret = next - offset; |
| else |
| *ret = 0; |
| |
| return 0; |
| } |
| |
| static int fdisk_ask_cb(struct fdisk_context *c, struct fdisk_ask *ask, void *data) { |
| _cleanup_free_ char *ids = NULL; |
| int r; |
| |
| if (fdisk_ask_get_type(ask) != FDISK_ASKTYPE_STRING) |
| return -EINVAL; |
| |
| ids = new(char, ID128_UUID_STRING_MAX); |
| if (!ids) |
| return -ENOMEM; |
| |
| r = fdisk_ask_string_set_result(ask, id128_to_uuid_string(*(sd_id128_t*) data, ids)); |
| if (r < 0) |
| return r; |
| |
| TAKE_PTR(ids); |
| return 0; |
| } |
| |
| static int fdisk_set_disklabel_id_by_uuid(struct fdisk_context *c, sd_id128_t id) { |
| int r; |
| |
| r = fdisk_set_ask(c, fdisk_ask_cb, &id); |
| if (r < 0) |
| return r; |
| |
| r = fdisk_set_disklabel_id(c); |
| if (r < 0) |
| return r; |
| |
| return fdisk_set_ask(c, NULL, NULL); |
| } |
| |
| static int derive_uuid(sd_id128_t base, const char *token, sd_id128_t *ret) { |
| union { |
| unsigned char md[SHA256_DIGEST_LENGTH]; |
| sd_id128_t id; |
| } result; |
| |
| assert(token); |
| assert(ret); |
| |
| /* Derive a new UUID from the specified UUID in a stable and reasonably safe way. Specifically, we |
| * calculate the HMAC-SHA256 of the specified token string, keyed by the supplied base (typically the |
| * machine ID). We use the machine ID as key (and not as cleartext!) of the HMAC operation since it's |
| * the machine ID we don't want to leak. */ |
| |
| if (!HMAC(EVP_sha256(), |
| &base, sizeof(base), |
| (const unsigned char*) token, strlen(token), |
| result.md, NULL)) |
| return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "HMAC-SHA256 calculation failed."); |
| |
| /* Take the first half, mark it as v4 UUID */ |
| assert_cc(sizeof(result.md) == sizeof(result.id) * 2); |
| *ret = id128_make_v4_uuid(result.id); |
| return 0; |
| } |
| |
| static int context_load_partition_table( |
| Context *context, |
| const char *node, |
| int *backing_fd) { |
| |
| _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL; |
| _cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL; |
| uint64_t left_boundary = UINT64_MAX, first_lba, last_lba, nsectors; |
| _cleanup_free_ char *disk_uuid_string = NULL; |
| bool from_scratch = false; |
| sd_id128_t disk_uuid; |
| size_t n_partitions; |
| int r; |
| |
| assert(context); |
| assert(node); |
| assert(backing_fd); |
| assert(!context->fdisk_context); |
| assert(!context->free_areas); |
| assert(context->start == UINT64_MAX); |
| assert(context->end == UINT64_MAX); |
| assert(context->total == UINT64_MAX); |
| |
| c = fdisk_new_context(); |
| if (!c) |
| return log_oom(); |
| |
| /* libfdisk doesn't have an API to operate on arbitrary fds, hence reopen the fd going via the |
| * /proc/self/fd/ magic path if we have an existing fd. Open the original file otherwise. */ |
| if (*backing_fd < 0) |
| r = fdisk_assign_device(c, node, arg_dry_run); |
| else { |
| char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)]; |
| xsprintf(procfs_path, "/proc/self/fd/%i", *backing_fd); |
| |
| r = fdisk_assign_device(c, procfs_path, arg_dry_run); |
| } |
| if (r == -EINVAL && arg_size_auto) { |
| struct stat st; |
| |
| /* libfdisk returns EINVAL if opening a file of size zero. Let's check for that, and accept |
| * it if automatic sizing is requested. */ |
| |
| if (*backing_fd < 0) |
| r = stat(node, &st); |
| else |
| r = fstat(*backing_fd, &st); |
| if (r < 0) |
| return log_error_errno(errno, "Failed to stat block device '%s': %m", node); |
| |
| if (S_ISREG(st.st_mode) && st.st_size == 0) |
| return /* from_scratch = */ true; |
| |
| r = -EINVAL; |
| } |
| if (r < 0) |
| return log_error_errno(r, "Failed to open device '%s': %m", node); |
| |
| if (*backing_fd < 0) { |
| /* If we have no fd referencing the device yet, make a copy of the fd now, so that we have one */ |
| *backing_fd = fcntl(fdisk_get_devfd(c), F_DUPFD_CLOEXEC, 3); |
| if (*backing_fd < 0) |
| return log_error_errno(errno, "Failed to duplicate fdisk fd: %m"); |
| } |
| |
| /* Tell udev not to interfere while we are processing the device */ |
| if (flock(fdisk_get_devfd(c), arg_dry_run ? LOCK_SH : LOCK_EX) < 0) |
| return log_error_errno(errno, "Failed to lock block device: %m"); |
| |
| switch (arg_empty) { |
| |
| case EMPTY_REFUSE: |
| /* Refuse empty disks, insist on an existing GPT partition table */ |
| if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT)) |
| return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has no GPT disk label, not repartitioning.", node); |
| |
| break; |
| |
| case EMPTY_REQUIRE: |
| /* Require an empty disk, refuse any existing partition table */ |
| r = fdisk_has_label(c); |
| if (r < 0) |
| return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", node); |
| if (r > 0) |
| return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s already has a disk label, refusing.", node); |
| |
| from_scratch = true; |
| break; |
| |
| case EMPTY_ALLOW: |
| /* Allow both an empty disk and an existing partition table, but only GPT */ |
| r = fdisk_has_label(c); |
| if (r < 0) |
| return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", node); |
| if (r > 0) { |
| if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT)) |
| return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has non-GPT disk label, not repartitioning.", node); |
| } else |
| from_scratch = true; |
| |
| break; |
| |
| case EMPTY_FORCE: |
| case EMPTY_CREATE: |
| /* Always reinitiaize the disk, don't consider what there was on the disk before */ |
| from_scratch = true; |
| break; |
| } |
| |
| if (from_scratch) { |
| r = fdisk_create_disklabel(c, "gpt"); |
| 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_by_uuid(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(c, &disk_uuid_string); |
| if (r < 0) |
| return log_error_errno(r, "Failed to get current GPT disk label UUID: %m"); |
| |
| r = sd_id128_from_string(disk_uuid_string, &disk_uuid); |
| if (r < 0) |
| return log_error_errno(r, "Failed to parse 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(c); |
| if (r < 0) |
| return log_error_errno(r, "Failed to set GPT disk label: %m"); |
| } |
| |
| r = fdisk_get_partitions(c, &t); |
| if (r < 0) |
| return log_error_errno(r, "Failed to acquire partition table: %m"); |
| |
| n_partitions = fdisk_table_get_nents(t); |
| for (size_t i = 0; i < n_partitions; i++) { |
| _cleanup_free_ char *label_copy = NULL; |
| Partition *pp, *last = NULL; |
| struct fdisk_partition *p; |
| struct fdisk_parttype *pt; |
| const char *pts, *ids, *label; |
| uint64_t sz, start; |
| bool found = false; |
| sd_id128_t ptid, id; |
| size_t partno; |
| |
| p = fdisk_table_get_partition(t, i); |
| if (!p) |
| return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m"); |
| |
| if (fdisk_partition_is_used(p) <= 0) |
| continue; |
| |
| if (fdisk_partition_has_start(p) <= 0 || |
| fdisk_partition_has_size(p) <= 0 || |
| fdisk_partition_has_partno(p) <= 0) |
| return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a position, size or number."); |
| |
| pt = fdisk_partition_get_type(p); |
| if (!pt) |
| return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition: %m"); |
| |
| pts = fdisk_parttype_get_string(pt); |
| if (!pts) |
| return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition as string: %m"); |
| |
| r = sd_id128_from_string(pts, &ptid); |
| if (r < 0) |
| return log_error_errno(r, "Failed to parse partition type UUID %s: %m", pts); |
| |
| ids = fdisk_partition_get_uuid(p); |
| if (!ids) |
| return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a UUID."); |
| |
| r = sd_id128_from_string(ids, &id); |
| if (r < 0) |
| return log_error_errno(r, "Failed to parse partition UUID %s: %m", ids); |
| |
| label = fdisk_partition_get_name(p); |
| if (!isempty(label)) { |
| label_copy = strdup(label); |
| if (!label_copy) |
| return log_oom(); |
| } |
| |
| sz = fdisk_partition_get_size(p); |
| assert_se(sz <= UINT64_MAX/512); |
| sz *= 512; |
| |
| start = fdisk_partition_get_start(p); |
| assert_se(start <= UINT64_MAX/512); |
| start *= 512; |
| |
| partno = 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; |
| fdisk_ref_partition(p); |
| |
| r = determine_current_padding(c, t, p, &pp->current_padding); |
| 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(); |
| if (!np) |
| return log_oom(); |
| |
| np->current_uuid = id; |
| np->type_uuid = ptid; |
| np->current_size = sz; |
| np->offset = start; |
| np->partno = partno; |
| np->current_label = TAKE_PTR(label_copy); |
| |
| np->current_partition = p; |
| fdisk_ref_partition(p); |
| |
| r = determine_current_padding(c, t, p, &np->current_padding); |
| 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++; |
| } |
| } |
| |
| add_initial_free_area: |
| nsectors = fdisk_get_nsectors(c); |
| assert(nsectors <= UINT64_MAX/512); |
| nsectors *= 512; |
| |
| first_lba = fdisk_get_first_lba(c); |
| assert(first_lba <= UINT64_MAX/512); |
| first_lba *= 512; |
| |
| last_lba = fdisk_get_last_lba(c); |
| assert(last_lba < UINT64_MAX); |
| last_lba++; |
| assert(last_lba <= UINT64_MAX/512); |
| last_lba *= 512; |
| |
| 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, 4096); |
| last_lba = round_down_size(last_lba, 4096); |
| |
| 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, 4096); |
| left_boundary = round_down_size(left_boundary, 4096); |
| last_lba = round_down_size(last_lba, 4096); |
| |
| 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->fdisk_context = TAKE_PTR(c); |
| |
| return from_scratch; |
| } |
| |
| static void context_unload_partition_table(Context *context) { |
| Partition *p, *next; |
| |
| assert(context); |
| |
| LIST_FOREACH_SAFE(partitions, p, next, 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) { |
| fdisk_unref_partition(p->current_partition); |
| p->current_partition = NULL; |
| } |
| |
| if (p->new_partition) { |
| 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); |
| } |
| |
| context->start = UINT64_MAX; |
| context->end = UINT64_MAX; |
| context->total = UINT64_MAX; |
| |
| if (context->fdisk_context) { |
| fdisk_unref_context(context->fdisk_context); |
| context->fdisk_context = NULL; |
| } |
| |
| context_free_free_areas(context); |
| } |
| |
| static int format_size_change(uint64_t from, uint64_t to, char **ret) { |
| char format_buffer1[FORMAT_BYTES_MAX], format_buffer2[FORMAT_BYTES_MAX], *buf; |
| |
| if (from != UINT64_MAX) |
| format_bytes(format_buffer1, sizeof(format_buffer1), from); |
| if (to != UINT64_MAX) |
| format_bytes(format_buffer2, sizeof(format_buffer2), to); |
| |
| if (from != UINT64_MAX) { |
| if (from == to || to == UINT64_MAX) |
| buf = strdup(format_buffer1); |
| else |
| buf = strjoin(format_buffer1, " ", special_glyph(SPECIAL_GLYPH_ARROW), " ", format_buffer2); |
| } else if (to != UINT64_MAX) |
| buf = strjoin(special_glyph(SPECIAL_GLYPH_ARROW), " ", format_buffer2); |
| else { |
| *ret = NULL; |
| return 0; |
| } |
| |
| if (!buf) |
| return log_oom(); |
| |
| *ret = TAKE_PTR(buf); |
| 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 context_dump_partitions(Context *context, const char *node) { |
| _cleanup_(table_unrefp) Table *t = NULL; |
| uint64_t sum_padding = 0, sum_size = 0; |
| Partition *p; |
| int r; |
| |
| if ((arg_json_format_flags & JSON_FORMAT_OFF) && context->n_partitions == 0) { |
| log_info("Empty partition table."); |
| return 0; |
| } |
| |
| t = table_new("type", "label", "uuid", "file", "node", "offset", "old size", "raw size", "size", "old padding", "raw padding", "padding", "activity"); |
| if (!t) |
| return log_oom(); |
| |
| if (!DEBUG_LOGGING) { |
| if (arg_json_format_flags & JSON_FORMAT_OFF) |
| (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) 11); |
| 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) 9, (size_t) 10, (size_t) 12); |
| } |
| |
| (void) table_set_align_percent(t, table_get_cell(t, 0, 5), 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, 10), 100); |
| (void) table_set_align_percent(t, table_get_cell(t, 0, 11), 100); |
| |
| LIST_FOREACH(partitions, p, context->partitions) { |
| _cleanup_free_ char *size_change = NULL, *padding_change = NULL, *partname = NULL; |
| char uuid_buffer[ID128_UUID_STRING_MAX]; |
| const char *label, *activity = NULL; |
| |
| if (p->dropped) |
| continue; |
| |
| 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 ? fdisk_partname(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; |
| |
| 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, sd_id128_is_null(p->new_uuid) ? p->current_uuid : p->new_uuid, |
| TABLE_STRING, p->definition_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, !p->partitions_next && 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, !p->partitions_next && sum_padding > 0 ? ansi_underline() : NULL, |
| TABLE_STRING, activity ?: "unchanged"); |
| if (r < 0) |
| return table_log_add_error(r); |
| } |
| |
| if ((arg_json_format_flags & JSON_FORMAT_OFF) && (sum_padding > 0 || sum_size > 0)) { |
| char s[FORMAT_BYTES_MAX]; |
| const char *a, *b; |
| |
| a = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", format_bytes(s, sizeof(s), sum_size)); |
| b = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", format_bytes(s, sizeof(s), sum_padding)); |
| |
| r = table_add_many( |
| t, |
| 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); |
| if (r < 0) |
| return table_log_add_error(r); |
| } |
| |
| return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend); |
| } |
| |
| static void context_bar_char_process_partition( |
| Context *context, |
| Partition *bar[], |
| size_t n, |
| Partition *p, |
| size_t *ret_start) { |
| |
| uint64_t from, to, total; |
| size_t x, y; |
| |
| assert(context); |
| assert(bar); |
| assert(n > 0); |
| assert(p); |
| |
| if (p->dropped) |
| return; |
| |
| 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; |
| |
| *ret_start = x; |
| } |
| |
| static int partition_hint(const Partition *p, const char *node, char **ret) { |
| _cleanup_free_ char *buf = NULL; |
| char ids[ID128_UUID_STRING_MAX]; |
| const char *label; |
| sd_id128_t id; |
| |
| /* Tries really hard to find a suitable description for this partition */ |
| |
| if (p->definition_path) { |
| buf = strdup(basename(p->definition_path)); |
| goto done; |
| } |
| |
| label = partition_label(p); |
| if (!isempty(label)) { |
| buf = strdup(label); |
| goto done; |
| } |
| |
| if (p->partno != UINT64_MAX) { |
| buf = fdisk_partname(node, p->partno+1); |
| goto done; |
| } |
| |
| if (!sd_id128_is_null(p->new_uuid)) |
| id = p->new_uuid; |
| else if (!sd_id128_is_null(p->current_uuid)) |
| id = p->current_uuid; |
| else |
| id = p->type_uuid; |
| |
| buf = strdup(id128_to_uuid_string(id, ids)); |
| |
| done: |
| if (!buf) |
| return -ENOMEM; |
| |
| *ret = TAKE_PTR(buf); |
| return 0; |
| } |
| |
| static int context_dump_partition_bar(Context *context, const char *node) { |
| _cleanup_free_ Partition **bar = NULL; |
| _cleanup_free_ size_t *start_array = NULL; |
| Partition *p, *last = NULL; |
| bool z = false; |
| size_t c, j = 0; |
| |
| 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(); |
| |
| start_array = new(size_t, context->n_partitions); |
| if (!start_array) |
| return log_oom(); |
| |
| LIST_FOREACH(partitions, p, context->partitions) |
| context_bar_char_process_partition(context, bar, c, p, start_array + j++); |
| |
| 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(special_glyph(SPECIAL_GLYPH_DARK_SHADE), stdout); |
| } else { |
| fputs(ansi_normal(), stdout); |
| fputs(special_glyph(SPECIAL_GLYPH_LIGHT_SHADE), stdout); |
| } |
| |
| last = bar[i]; |
| } |
| |
| fputs(ansi_normal(), stdout); |
| putc('\n', stdout); |
| |
| for (size_t i = 0; i < context->n_partitions; 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; |
| j++; |
| |
| if (i < context->n_partitions - 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]], special_glyph(SPECIAL_GLYPH_TREE_RIGHT)); |
| if (e) { |
| d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH), e); |
| if (!d) |
| return log_oom(); |
| } |
| } |
| |
| if (!d) { |
| d = strdup(special_glyph(SPECIAL_GLYPH_TREE_VERTICAL)); |
| if (!d) |
| return log_oom(); |
| } |
| |
| } else if (i == context->n_partitions - j) { |
| _cleanup_free_ char *hint = NULL; |
| |
| (void) partition_hint(p, node, &hint); |
| |
| if (streq_ptr(line[start_array[j-1]], special_glyph(SPECIAL_GLYPH_TREE_VERTICAL))) |
| d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH), " ", strna(hint)); |
| else |
| d = strjoin(special_glyph(SPECIAL_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_changed(const Context *context) { |
| Partition *p; |
| |
| 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) { |
| _cleanup_(blkid_free_probep) blkid_probe probe = NULL; |
| int r; |
| |
| assert(context); |
| assert(offset != UINT64_MAX); |
| assert(size != UINT64_MAX); |
| |
| probe = blkid_new_probe(); |
| if (!probe) |
| return log_oom(); |
| |
| errno = 0; |
| r = blkid_probe_set_device(probe, 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 (blkid_probe_enable_superblocks(probe, true) < 0 || |
| blkid_probe_set_superblocks_flags(probe, BLKID_SUBLKS_MAGIC|BLKID_SUBLKS_BADCSUM) < 0 || |
| blkid_probe_enable_partitions(probe, true) < 0 || |
| 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 = blkid_do_probe(probe); |
| if (r < 0) |
| return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe for file systems."); |
| if (r > 0) |
| break; |
| |
| errno = 0; |
| if (blkid_do_wipe(probe, false) < 0) |
| return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to wipe file system signature."); |
| } |
| |
| return 0; |
| } |
| |
| 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 = 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, 512); |
| |
| if (offset > UINT64_MAX - size) |
| return -ERANGE; |
| |
| end = offset + size; |
| if (end <= range[0]) |
| return 0; |
| |
| range[1] = round_down_size(end - range[0], 512); |
| 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); |
| return 1; |
| } |
| |
| static int context_discard_gap_after(Context *context, Partition *p) { |
| uint64_t gap, next = UINT64_MAX; |
| Partition *q; |
| int r; |
| |
| assert(context); |
| assert(!p || (p->offset != UINT64_MAX && p->new_size != UINT64_MAX)); |
| |
| if (p) |
| gap = p->offset + p->new_size; |
| else |
| gap = context->start; |
| |
| 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; |
| |
| if (next == UINT64_MAX || q->offset < next) |
| next = q->offset; |
| } |
| |
| if (next == UINT64_MAX) { |
| next = context->end; |
| if (gap > next) |
| return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end."); |
| } |
| |
| assert(next >= gap); |
| r = context_discard_range(context, gap, next - gap); |
| if (r == -EOPNOTSUPP) { |
| if (p) |
| log_info("Storage does not support discard, not discarding gap after partition %" PRIu64 ".", p->partno); |
| else |
| log_info("Storage does not support discard, not discarding gap at beginning of disk."); |
| return 0; |
| } |
| if (r == 0) /* Too short */ |
| return 0; |
| if (r < 0) { |
| if (p) |
| return log_error_errno(r, "Failed to discard gap after partition %" PRIu64 ".", p->partno); |
| else |
| return log_error_errno(r, "Failed to discard gap at beginning of disk."); |
| } |
| |
| if (p) |
| log_info("Successfully discarded gap after partition %" PRIu64 ".", p->partno); |
| else |
| log_info("Successfully discarded gap at beginning of disk."); |
| |
| return 0; |
| } |
| |
| static int context_wipe_and_discard(Context *context, bool from_scratch) { |
| Partition *p; |
| int r; |
| |
| assert(context); |
| |
| /* Wipe and discard the contents of all partitions we are about to create. We skip the discarding if |
| * we were supposed to start from scratch anyway, as in that case we just discard the whole block |
| * device in one go early on. */ |
| |
| LIST_FOREACH(partitions, p, context->partitions) { |
| |
| if (!p->allocated_to_area) |
| continue; |
| |
| r = context_wipe_partition(context, p); |
| if (r < 0) |
| return r; |
| |
| if (!from_scratch) { |
| r = context_discard_partition(context, p); |
| if (r < 0) |
| return r; |
| |
| r = context_discard_gap_after(context, p); |
| if (r < 0) |
| return r; |
| } |
| } |
| |
| if (!from_scratch) { |
| r = context_discard_gap_after(context, NULL); |
| if (r < 0) |
| return r; |
| } |
| |
| return 0; |
| } |
| |
| static int partition_encrypt( |
| Partition *p, |
| const char *node, |
| struct crypt_device **ret_cd, |
| char **ret_volume, |
| int *ret_fd) { |
| #if HAVE_LIBCRYPTSETUP |
| _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL; |
| _cleanup_(erase_and_freep) void *volume_key = NULL; |
| _cleanup_free_ char *dm_name = NULL, *vol = NULL; |
| char suuid[ID128_UUID_STRING_MAX]; |
| size_t volume_key_size = 256 / 8; |
| sd_id128_t uuid; |
| int r; |
| |
| assert(p); |
| assert(p->encrypt != ENCRYPT_OFF); |
| |
| log_debug("Encryption mode for partition %" PRIu64 ": %s", p->partno, encrypt_mode_to_string(p->encrypt)); |
| |
| r = dlopen_cryptsetup(); |
| if (r < 0) |
| return log_error_errno(r, "libcryptsetup not found, cannot encrypt: %m"); |
| |
| if (asprintf(&dm_name, "luks-repart-%08" PRIx64, random_u64()) < 0) |
| return log_oom(); |
| |
| if (ret_volume) { |
| vol = path_join("/dev/mapper/", dm_name); |
| if (!vol) |
| return log_oom(); |
| } |
| |
| r = derive_uuid(p->new_uuid, "luks-uuid", &uuid); |
| if (r < 0) |
| return r; |
| |
| log_info("Encrypting future partition %" PRIu64 "...", p->partno); |
| |
| volume_key = malloc(volume_key_size); |
| if (!volume_key) |
| return log_oom(); |
| |
| r = genuine_random_bytes(volume_key, volume_key_size, RANDOM_BLOCK); |
| if (r < 0) |
| return log_error_errno(r, "Failed to generate volume key: %m"); |
| |
| r = sym_crypt_init(&cd, node); |
| if (r < 0) |
| return log_error_errno(r, "Failed to allocate libcryptsetup context: %m"); |
| |
| cryptsetup_enable_logging(cd); |
| |
| r = sym_crypt_format(cd, |
| CRYPT_LUKS2, |
| "aes", |
| "xts-plain64", |
| id128_to_uuid_string(uuid, suuid), |
| volume_key, |
| volume_key_size, |
| &(struct crypt_params_luks2) { |
| .label = strempty(p->new_label), |
| .sector_size = 512U, |
| }); |
| if (r < 0) |
| return log_error_errno(r, "Failed to LUKS2 format future partition: %m"); |
| |
| if (IN_SET(p->encrypt, ENCRYPT_KEY_FILE, ENCRYPT_KEY_FILE_TPM2)) { |
| r = sym_crypt_keyslot_add_by_volume_key( |
| cd, |
| CRYPT_ANY_SLOT, |
| volume_key, |
| volume_key_size, |
| strempty(arg_key), |
| arg_key_size); |
| if (r < 0) |
| return log_error_errno(r, "Failed to add LUKS2 key: %m"); |
| } |
| |
| if (IN_SET(p->encrypt, ENCRYPT_TPM2, ENCRYPT_KEY_FILE_TPM2)) { |
| #if HAVE_TPM2 |
| _cleanup_(erase_and_freep) char *base64_encoded = NULL; |
| _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; |
| _cleanup_(erase_and_freep) void *secret = NULL; |
| _cleanup_free_ void *blob = NULL, *hash = NULL; |
| size_t secret_size, blob_size, hash_size; |
| int keyslot; |
| |
| r = tpm2_seal(arg_tpm2_device, arg_tpm2_pcr_mask, &secret, &secret_size, &blob, &blob_size, &hash, &hash_size); |
| if (r < 0) |
| return log_error_errno(r, "Failed to seal to TPM2: %m"); |
| |
| r = base64mem(secret, secret_size, &base64_encoded); |
| if (r < 0) |
| return log_error_errno(r, "Failed to base64 encode secret key: %m"); |
| |
| r = cryptsetup_set_minimal_pbkdf(cd); |
| if (r < 0) |
| return log_error_errno(r, "Failed to set minimal PBKDF: %m"); |
| |
| keyslot = sym_crypt_keyslot_add_by_volume_key( |
| cd, |
| CRYPT_ANY_SLOT, |
| volume_key, |
| volume_key_size, |
| base64_encoded, |
| strlen(base64_encoded)); |
| if (keyslot < 0) |
| return log_error_errno(keyslot, "Failed to add new TPM2 key to %s: %m", node); |
| |
| r = tpm2_make_luks2_json(keyslot, arg_tpm2_pcr_mask, blob, blob_size, hash, hash_size, &v); |
| if (r < 0) |
| return log_error_errno(r, "Failed to prepare TPM2 JSON token object: %m"); |
| |
| r = cryptsetup_add_token_json(cd, v); |
| if (r < 0) |
| return log_error_errno(r, "Failed to add TPM2 JSON token to LUKS2 header: %m"); |
| #else |
| return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| "Support for TPM2 enrollment not enabled."); |
| #endif |
| } |
| |
| r = sym_crypt_activate_by_volume_key( |
| cd, |
| dm_name, |
| volume_key, |
| volume_key_size, |
| arg_discard ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0); |
| if (r < 0) |
| return log_error_errno(r, "Failed to activate LUKS superblock: %m"); |
| |
| log_info("Successfully encrypted future partition %" PRIu64 ".", p->partno); |
| |
| if (ret_fd) { |
| _cleanup_close_ int dev_fd = -1; |
| |
| dev_fd = open(vol, O_RDWR|O_CLOEXEC|O_NOCTTY); |
| if (dev_fd < 0) |
| return log_error_errno(errno, "Failed to open LUKS volume '%s': %m", vol); |
| |
| *ret_fd = TAKE_FD(dev_fd); |
| } |
| |
| if (ret_cd) |
| *ret_cd = TAKE_PTR(cd); |
| if (ret_volume) |
| *ret_volume = TAKE_PTR(vol); |
| |
| return 0; |
| #else |
| return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libcryptsetup is not supported, cannot encrypt: %m"); |
| #endif |
| } |
| |
| static int deactivate_luks(struct crypt_device *cd, const char *node) { |
| #if HAVE_LIBCRYPTSETUP |
| int r; |
| |
| if (!cd) |
| return 0; |
| |
| assert(node); |
| |
| /* udev or so might access out block device in the background while we are done. Let's hence force |
| * detach the volume. We sync'ed before, hence this should be safe. */ |
| |
| r = sym_crypt_deactivate_by_name(cd, basename(node), CRYPT_DEACTIVATE_FORCE); |
| if (r < 0) |
| return log_error_errno(r, "Failed to deactivate LUKS device: %m"); |
| |
| return 1; |
| #else |
| return 0; |
| #endif |
| } |
| |
| static int context_copy_blocks(Context *context) { |
| Partition *p; |
| int whole_fd = -1, r; |
| |
| assert(context); |
| |
| /* Copy in file systems on the block level */ |
| |
| LIST_FOREACH(partitions, p, context->partitions) { |
| _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL; |
| _cleanup_(loop_device_unrefp) LoopDevice *d = NULL; |
| _cleanup_free_ char *encrypted = NULL; |
| _cleanup_close_ int encrypted_dev_fd = -1; |
| char buf[FORMAT_BYTES_MAX]; |
| int target_fd; |
| |
| if (p->copy_blocks_fd < 0) |
| continue; |
| |
| if (p->dropped) |
| continue; |
| |
| if (PARTITION_EXISTS(p)) /* Never copy over existing partitions */ |
| continue; |
| |
| assert(p->new_size != UINT64_MAX); |
| assert(p->copy_blocks_size != UINT64_MAX); |
| assert(p->new_size >= p->copy_blocks_size); |
| |
| if (whole_fd < 0) |
| assert_se((whole_fd = fdisk_get_devfd(context->fdisk_context)) >= 0); |
| |
| if (p->encrypt != ENCRYPT_OFF) { |
| r = loop_device_make(whole_fd, O_RDWR, p->offset, p->new_size, 0, &d); |
| if (r < 0) |
| return log_error_errno(r, "Failed to make loopback device of future partition %" PRIu64 ": %m", p->partno); |
| |
| r = loop_device_flock(d, LOCK_EX); |
| if (r < 0) |
| return log_error_errno(r, "Failed to lock loopback device: %m"); |
| |
| r = partition_encrypt(p, d->node, &cd, &encrypted, &encrypted_dev_fd); |
| if (r < 0) |
| return log_error_errno(r, "Failed to encrypt device: %m"); |
| |
| if (flock(encrypted_dev_fd, LOCK_EX) < 0) |
| return log_error_errno(errno, "Failed to lock LUKS device: %m"); |
| |
| target_fd = encrypted_dev_fd; |
| } else { |
| if (lseek(whole_fd, p->offset, SEEK_SET) == (off_t) -1) |
| return log_error_errno(errno, "Failed to seek to partition offset: %m"); |
| |
| target_fd = whole_fd; |
| } |
| |
| log_info("Copying in '%s' (%s) on block level into future partition %" PRIu64 ".", p->copy_blocks_path, format_bytes(buf, sizeof(buf), p->copy_blocks_size), p->partno); |
| |
| r = copy_bytes_full(p->copy_blocks_fd, target_fd, p->copy_blocks_size, 0, NULL, NULL, NULL, NULL); |
| if (r < 0) |
| return log_error_errno(r, "Failed to copy in data from '%s': %m", p->copy_blocks_path); |
| |
| if (fsync(target_fd) < 0) |
| return log_error_errno(errno, "Failed to synchronize copied data blocks: %m"); |
| |
| if (p->encrypt != ENCRYPT_OFF) { |
| encrypted_dev_fd = safe_close(encrypted_dev_fd); |
| |
| r = deactivate_luks(cd, encrypted); |
| if (r < 0) |
| return r; |
| |
| sym_crypt_free(cd); |
| cd = NULL; |
| |
| r = loop_device_sync(d); |
| if (r < 0) |
| return log_error_errno(r, "Failed to sync loopback device: %m"); |
| } |
| |
| log_info("Copying in of '%s' on block level completed.", p->copy_blocks_path); |
| } |
| |
| return 0; |
| } |
| |
| static int do_copy_files(Partition *p, const char *fs) { |
| char **source, **target; |
| int r; |
| |
| assert(p); |
| assert(fs); |
| |
| STRV_FOREACH_PAIR(source, target, p->copy_files) { |
| _cleanup_close_ int sfd = -1, pfd = -1, tfd = -1; |
| |
| sfd = chase_symlinks_and_open(*source, arg_root, CHASE_PREFIX_ROOT|CHASE_WARN, O_CLOEXEC|O_NOCTTY, NULL); |
| if (sfd < 0) |
| return log_error_errno(sfd, "Failed to open source file '%s%s': %m", strempty(arg_root), *source); |
| |
| r = fd_verify_regular(sfd); |
| if (r < 0) { |
| if (r != -EISDIR) |
| return log_error_errno(r, "Failed to check type of source file '%s': %m", *source); |
| |
| /* We are looking at a directory */ |
| tfd = chase_symlinks_and_open(*target, fs, CHASE_PREFIX_ROOT|CHASE_WARN, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL); |
| if (tfd < 0) { |
| _cleanup_free_ char *dn = NULL, *fn = NULL; |
| |
| if (tfd != -ENOENT) |
| return log_error_errno(tfd, "Failed to open target directory '%s': %m", *target); |
| |
| r = path_extract_filename(*target, &fn); |
| if (r < 0) |
| return log_error_errno(r, "Failed to extract filename from '%s': %m", *target); |
| |
| r = path_extract_directory(*target, &dn); |
| if (r < 0) |
| return log_error_errno(r, |