| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <linux/dm-ioctl.h> |
| 5 |
#include <linux/magic.h> |
| 6 |
#include <sys/file.h> |
| 7 |
#include <sys/ioctl.h> |
| 8 |
#include <sys/stat.h> |
| 9 |
#include <unistd.h> |
| 10 |
|
| 11 |
#include "sd-daemon.h" |
| 12 |
#include "sd-id128.h" |
| 13 |
#include "sd-json.h" |
| 14 |
#include "sd-varlink.h" |
| 15 |
|
| 16 |
#include "alloc-util.h" |
| 17 |
#include "ask-password-api.h" |
| 18 |
#include "blkid-util.h" |
| 19 |
#include "blockdev-list.h" |
| 20 |
#include "blockdev-util.h" |
| 21 |
#include "btrfs-util.h" |
| 22 |
#include "build.h" |
| 23 |
#include "chase.h" |
| 24 |
#include "chattr-util.h" |
| 25 |
#include "conf-files.h" |
| 26 |
#include "conf-parser.h" |
| 27 |
#include "constants.h" |
| 28 |
#include "copy.h" |
| 29 |
#include "crypto-util.h" |
| 30 |
#include "cryptsetup-util.h" |
| 31 |
#include "device-util.h" |
| 32 |
#include "devnum-util.h" |
| 33 |
#include "dirent-util.h" |
| 34 |
#include "dissect-image.h" |
| 35 |
#include "efivars.h" |
| 36 |
#include "errno-util.h" |
| 37 |
#include "extract-word.h" |
| 38 |
#include "factory-reset.h" |
| 39 |
#include "fd-util.h" |
| 40 |
#include "fdisk-util.h" |
| 41 |
#include "fileio.h" |
| 42 |
#include "format-table.h" |
| 43 |
#include "format-util.h" |
| 44 |
#include "fs-util.h" |
| 45 |
#include "glyph-util.h" |
| 46 |
#include "gpt.h" |
| 47 |
#include "hexdecoct.h" |
| 48 |
#include "hmac.h" |
| 49 |
#include "id128-util.h" |
| 50 |
#include "image-policy.h" |
| 51 |
#include "initrd-util.h" |
| 52 |
#include "install-file.h" |
| 53 |
#include "io-util.h" |
| 54 |
#include "iso9660.h" |
| 55 |
#include "json-util.h" |
| 56 |
#include "libmount-util.h" |
| 57 |
#include "list.h" |
| 58 |
#include "loop-util.h" |
| 59 |
#include "main-func.h" |
| 60 |
#include "mkdir.h" |
| 61 |
#include "mkfs-util.h" |
| 62 |
#include "mount-util.h" |
| 63 |
#include "mountpoint-util.h" |
| 64 |
#include "nulstr-util.h" |
| 65 |
#include "options.h" |
| 66 |
#include "parse-argument.h" |
| 67 |
#include "parse-helpers.h" |
| 68 |
#include "parse-util.h" |
| 69 |
#include "pretty-print.h" |
| 70 |
#include "process-util.h" |
| 71 |
#include "random-util.h" |
| 72 |
#include "ratelimit.h" |
| 73 |
#include "reread-partition-table.h" |
| 74 |
#include "resize-fs.h" |
| 75 |
#include "rm-rf.h" |
| 76 |
#include "set.h" |
| 77 |
#include "sort-util.h" |
| 78 |
#include "specifier.h" |
| 79 |
#include "string-table.h" |
| 80 |
#include "string-util.h" |
| 81 |
#include "strv.h" |
| 82 |
#include "sync-util.h" |
| 83 |
#include "terminal-util.h" |
| 84 |
#include "time-util.h" |
| 85 |
#include "tmpfile-util.h" |
| 86 |
#include "tpm2-pcr.h" |
| 87 |
#include "tpm2-util.h" |
| 88 |
#include "utf8.h" |
| 89 |
#include "varlink-io.systemd.Repart.h" |
| 90 |
#include "varlink-util.h" |
| 91 |
#include "xattr-util.h" |
| 92 |
|
| 93 |
/* If not configured otherwise use a minimal partition size of 10M */ |
| 94 |
#define DEFAULT_MIN_SIZE (10ULL*1024ULL*1024ULL) |
| 95 |
|
| 96 |
/* Hard lower limit for new partition sizes */ |
| 97 |
#define HARD_MIN_SIZE 4096ULL |
| 98 |
|
| 99 |
/* We know up front we're never going to put more than this in a verity sig partition. */ |
| 100 |
#define VERITY_SIG_SIZE (HARD_MIN_SIZE*4ULL) |
| 101 |
|
| 102 |
/* libfdisk takes off slightly more than 1M of the disk size when creating a GPT disk label */ |
| 103 |
#define GPT_METADATA_SIZE (1044ULL*1024ULL) |
| 104 |
|
| 105 |
/* LUKS2 takes off 16M of the partition size with its metadata by default */ |
| 106 |
#define LUKS2_METADATA_SIZE (16ULL*1024ULL*1024ULL) |
| 107 |
|
| 108 |
/* To do LUKS2 offline encryption, we need to keep some extra free space at the end of the partition. */ |
| 109 |
#define LUKS2_METADATA_KEEP_FREE (LUKS2_METADATA_SIZE*2ULL) |
| 110 |
|
| 111 |
/* LUKS2 default volume key size (no integrity). */ |
| 112 |
#define VOLUME_KEY_SIZE (512ULL/8ULL) |
| 113 |
|
| 114 |
/* Use 4K as the default filesystem sector size because as long as the partitions are aligned to 4K, the |
| 115 |
* filesystems will then also be compatible with sector sizes 512, 1024 and 2048. */ |
| 116 |
#define DEFAULT_FILESYSTEM_SECTOR_SIZE 4096ULL |
| 117 |
|
| 118 |
/* Minimum sizes for the ESP depending on sector size. What the minimum is, is severely underdocumented, but |
| 119 |
* it appears for 4K sector size it must be 260M, and otherwise 100M. This is what Microsoft says here: |
| 120 |
* |
| 121 |
* https://learn.microsoft.com/en-us/windows-hardware/manufacture/desktop/configure-uefigpt-based-hard-drive-partitions?view=windows-11 |
| 122 |
* https://learn.microsoft.com/en-us/windows-hardware/manufacture/desktop/oem-deployment-of-windows-desktop-editions-sample-scripts?view=windows-11&preserve-view=true#-createpartitions-uefitxt |
| 123 |
*/ |
| 124 |
#define ESP_MIN_SIZE (100 * U64_MB) |
| 125 |
#define ESP_MIN_SIZE_4K (260 * U64_MB) |
| 126 |
|
| 127 |
#define APIVFS_TMP_DIRS_NULSTR "proc\0sys\0dev\0tmp\0run\0var/tmp\0" |
| 128 |
|
| 129 |
#define AUTOMATIC_FSTAB_HEADER_START "# Start section ↓ of automatically generated fstab by systemd-repart" |
| 130 |
#define AUTOMATIC_FSTAB_HEADER_END "# End section ↑ of automatically generated fstab by systemd-repart" |
| 131 |
|
| 132 |
/* Note: When growing and placing new partitions we always align to 4K sector size. It's how newer hard disks |
| 133 |
* are designed, and if everything is aligned to that performance is best. And for older hard disks with 512B |
| 134 |
* sector size devices were generally assumed to have an even number of sectors, hence at the worst we'll |
| 135 |
* waste 3K per partition, which is probably fine. */ |
| 136 |
|
| 137 |
typedef enum EmptyMode { |
| 138 |
EMPTY_UNSET, /* no choice has been made yet */ |
| 139 |
EMPTY_REFUSE, /* refuse empty disks, never create a partition table */ |
| 140 |
EMPTY_ALLOW, /* allow empty disks, create partition table if necessary */ |
| 141 |
EMPTY_REQUIRE, /* require an empty disk, create a partition table */ |
| 142 |
EMPTY_FORCE, /* make disk empty, erase everything, create a partition table always */ |
| 143 |
EMPTY_CREATE, /* create disk as loopback file, create a partition table always */ |
| 144 |
_EMPTY_MODE_MAX, |
| 145 |
_EMPTY_MODE_INVALID = -EINVAL, |
| 146 |
} EmptyMode; |
| 147 |
|
| 148 |
typedef enum FilterPartitionType { |
| 149 |
FILTER_PARTITIONS_NONE, |
| 150 |
FILTER_PARTITIONS_EXCLUDE, |
| 151 |
FILTER_PARTITIONS_INCLUDE, |
| 152 |
_FILTER_PARTITIONS_MAX, |
| 153 |
_FILTER_PARTITIONS_INVALID = -EINVAL, |
| 154 |
} FilterPartitionsType; |
| 155 |
|
| 156 |
typedef enum AppendMode { |
| 157 |
APPEND_NO, |
| 158 |
APPEND_AUTO, |
| 159 |
APPEND_REPLACE, |
| 160 |
_APPEND_MODE_MAX, |
| 161 |
_APPEND_MODE_INVALID = -EINVAL, |
| 162 |
} AppendMode; |
| 163 |
|
| 164 |
static EmptyMode arg_empty = EMPTY_UNSET; |
| 165 |
static bool arg_dry_run = true; |
| 166 |
static char *arg_node = NULL; |
| 167 |
static bool arg_node_none = false; |
| 168 |
static char *arg_root = NULL; |
| 169 |
static char *arg_image = NULL; |
| 170 |
static char **arg_definitions = NULL; |
| 171 |
static bool arg_discard = true; |
| 172 |
static bool arg_can_factory_reset = false; |
| 173 |
static int arg_factory_reset = -1; |
| 174 |
static sd_id128_t arg_seed = SD_ID128_NULL; |
| 175 |
static bool arg_randomize = false; |
| 176 |
static int arg_pretty = -1; |
| 177 |
static uint64_t arg_size = UINT64_MAX; |
| 178 |
static bool arg_size_auto = false; |
| 179 |
static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; |
| 180 |
static PagerFlags arg_pager_flags = 0; |
| 181 |
static bool arg_legend = true; |
| 182 |
static struct iovec arg_key = {}; |
| 183 |
static char *arg_private_key = NULL; |
| 184 |
static KeySourceType arg_private_key_source_type = OPENSSL_KEY_SOURCE_FILE; |
| 185 |
static char *arg_private_key_source = NULL; |
| 186 |
static char *arg_certificate = NULL; |
| 187 |
static CertificateSourceType arg_certificate_source_type = OPENSSL_CERTIFICATE_SOURCE_FILE; |
| 188 |
static char *arg_certificate_source = NULL; |
| 189 |
static char *arg_tpm2_device = NULL; |
| 190 |
static uint32_t arg_tpm2_seal_key_handle = 0; |
| 191 |
static char *arg_tpm2_device_key = NULL; |
| 192 |
static Tpm2PCRValue *arg_tpm2_hash_pcr_values = NULL; |
| 193 |
static size_t arg_tpm2_n_hash_pcr_values = 0; |
| 194 |
static char *arg_tpm2_public_key = NULL; |
| 195 |
static uint32_t arg_tpm2_public_key_pcr_mask = 0; |
| 196 |
static char *arg_tpm2_pcrlock = NULL; |
| 197 |
static bool arg_split = false; |
| 198 |
static GptPartitionType *arg_filter_partitions = NULL; |
| 199 |
static size_t arg_n_filter_partitions = 0; |
| 200 |
static FilterPartitionsType arg_filter_partitions_type = FILTER_PARTITIONS_NONE; |
| 201 |
static GptPartitionType *arg_defer_partitions = NULL; |
| 202 |
static size_t arg_n_defer_partitions = 0; |
| 203 |
static bool arg_defer_partitions_empty = false; |
| 204 |
static bool arg_defer_partitions_factory_reset = false; |
| 205 |
static uint64_t arg_sector_size = 0; |
| 206 |
static uint64_t arg_grain_size = 0; |
| 207 |
static ImagePolicy *arg_image_policy = NULL; |
| 208 |
static Architecture arg_architecture = _ARCHITECTURE_INVALID; |
| 209 |
static int arg_offline = -1; |
| 210 |
static char **arg_copy_from = NULL; |
| 211 |
static char *arg_copy_source = NULL; |
| 212 |
static char *arg_make_ddi = NULL; |
| 213 |
static AppendMode arg_append_fstab = APPEND_NO; |
| 214 |
static char *arg_generate_fstab = NULL; |
| 215 |
static char *arg_generate_crypttab = NULL; |
| 216 |
static Set *arg_verity_settings = NULL; |
| 217 |
static bool arg_relax_copy_block_security = false; |
| 218 |
static bool arg_varlink = false; |
| 219 |
static bool arg_eltorito = false; |
| 220 |
static char *arg_eltorito_system = NULL; |
| 221 |
static char *arg_eltorito_volume = NULL; |
| 222 |
static char *arg_eltorito_publisher = NULL; |
| 223 |
|
| 224 |
STATIC_DESTRUCTOR_REGISTER(arg_node, freep); |
| 225 |
STATIC_DESTRUCTOR_REGISTER(arg_root, freep); |
| 226 |
STATIC_DESTRUCTOR_REGISTER(arg_image, freep); |
| 227 |
STATIC_DESTRUCTOR_REGISTER(arg_definitions, strv_freep); |
| 228 |
STATIC_DESTRUCTOR_REGISTER(arg_key, iovec_done_erase); |
| 229 |
STATIC_DESTRUCTOR_REGISTER(arg_private_key, freep); |
| 230 |
STATIC_DESTRUCTOR_REGISTER(arg_private_key_source, freep); |
| 231 |
STATIC_DESTRUCTOR_REGISTER(arg_certificate, freep); |
| 232 |
STATIC_DESTRUCTOR_REGISTER(arg_certificate_source, freep); |
| 233 |
STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device, freep); |
| 234 |
STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device_key, freep); |
| 235 |
STATIC_DESTRUCTOR_REGISTER(arg_tpm2_hash_pcr_values, freep); |
| 236 |
STATIC_DESTRUCTOR_REGISTER(arg_tpm2_public_key, freep); |
| 237 |
STATIC_DESTRUCTOR_REGISTER(arg_tpm2_pcrlock, freep); |
| 238 |
STATIC_DESTRUCTOR_REGISTER(arg_filter_partitions, freep); |
| 239 |
STATIC_DESTRUCTOR_REGISTER(arg_defer_partitions, freep); |
| 240 |
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep); |
| 241 |
STATIC_DESTRUCTOR_REGISTER(arg_copy_from, strv_freep); |
| 242 |
STATIC_DESTRUCTOR_REGISTER(arg_copy_source, freep); |
| 243 |
STATIC_DESTRUCTOR_REGISTER(arg_make_ddi, freep); |
| 244 |
STATIC_DESTRUCTOR_REGISTER(arg_generate_fstab, freep); |
| 245 |
STATIC_DESTRUCTOR_REGISTER(arg_generate_crypttab, freep); |
| 246 |
STATIC_DESTRUCTOR_REGISTER(arg_verity_settings, set_freep); |
| 247 |
STATIC_DESTRUCTOR_REGISTER(arg_eltorito_system, freep); |
| 248 |
STATIC_DESTRUCTOR_REGISTER(arg_eltorito_volume, freep); |
| 249 |
STATIC_DESTRUCTOR_REGISTER(arg_eltorito_publisher, freep); |
| 250 |
|
| 251 |
typedef enum ProgressPhase { |
| 252 |
PROGRESS_LOADING_DEFINITIONS, |
| 253 |
PROGRESS_LOADING_TABLE, |
| 254 |
PROGRESS_OPENING_COPY_BLOCK_SOURCES, |
| 255 |
PROGRESS_OPENING_BLOCK_DEVICE_REPLACE_SOURCES, |
| 256 |
PROGRESS_ACQUIRING_PARTITION_LABELS, |
| 257 |
PROGRESS_MINIMIZING, |
| 258 |
PROGRESS_PLACING, |
| 259 |
PROGRESS_WIPING_DISK, |
| 260 |
PROGRESS_WIPING_PARTITION, |
| 261 |
PROGRESS_COPYING_PARTITION, |
| 262 |
PROGRESS_FORMATTING_PARTITION, |
| 263 |
PROGRESS_ADJUSTING_PARTITION, |
| 264 |
PROGRESS_WRITING_TABLE, |
| 265 |
PROGRESS_REREADING_TABLE, |
| 266 |
PROGRESS_REPLACING_DEVICE, |
| 267 |
_PROGRESS_PHASE_MAX, |
| 268 |
_PROGRESS_PHASE_INVALID = -EINVAL, |
| 269 |
} ProgressPhase; |
| 270 |
|
| 271 |
typedef struct FreeArea FreeArea; |
| 272 |
|
| 273 |
typedef enum EncryptMode { |
| 274 |
ENCRYPT_OFF, |
| 275 |
ENCRYPT_KEY_FILE, |
| 276 |
ENCRYPT_TPM2, |
| 277 |
ENCRYPT_KEY_FILE_TPM2, |
| 278 |
_ENCRYPT_MODE_MAX, |
| 279 |
_ENCRYPT_MODE_INVALID = -EINVAL, |
| 280 |
} EncryptMode; |
| 281 |
|
| 282 |
typedef enum IntegrityMode { |
| 283 |
INTEGRITY_OFF, |
| 284 |
INTEGRITY_INLINE, |
| 285 |
_INTEGRITY_MODE_MAX, |
| 286 |
_INTEGRITY_MODE_INVALID = -EINVAL, |
| 287 |
} IntegrityMode; |
| 288 |
|
| 289 |
typedef enum IntegrityAlg { |
| 290 |
INTEGRITY_ALG_HMAC_SHA1, |
| 291 |
INTEGRITY_ALG_HMAC_SHA256, |
| 292 |
INTEGRITY_ALG_HMAC_SHA512, |
| 293 |
_INTEGRITY_ALG_MAX, |
| 294 |
_INTEGRITY_ALG_INVALID = -EINVAL, |
| 295 |
} IntegrityAlg; |
| 296 |
|
| 297 |
typedef enum EncryptKDF { |
| 298 |
ENCRYPT_KDF_ARGON2ID, |
| 299 |
ENCRYPT_KDF_PBKDF2, |
| 300 |
ENCRYPT_KDF_MINIMAL, |
| 301 |
_ENCRYPT_KDF_MAX, |
| 302 |
_ENCRYPT_KDF_INVALID = -EINVAL, |
| 303 |
} EncryptKDF; |
| 304 |
|
| 305 |
typedef enum VerityMode { |
| 306 |
VERITY_OFF, |
| 307 |
VERITY_DATA, |
| 308 |
VERITY_HASH, |
| 309 |
VERITY_SIG, |
| 310 |
_VERITY_MODE_MAX, |
| 311 |
_VERITY_MODE_INVALID = -EINVAL, |
| 312 |
} VerityMode; |
| 313 |
|
| 314 |
typedef enum MinimizeMode { |
| 315 |
MINIMIZE_OFF, |
| 316 |
MINIMIZE_BEST, |
| 317 |
MINIMIZE_GUESS, |
| 318 |
_MINIMIZE_MODE_MAX, |
| 319 |
_MINIMIZE_MODE_INVALID = -EINVAL, |
| 320 |
} MinimizeMode; |
| 321 |
|
| 322 |
typedef struct PartitionMountPoint { |
| 323 |
char *where; |
| 324 |
char *options; |
| 325 |
} PartitionMountPoint; |
| 326 |
|
| 327 |
static void partition_mountpoint_free_many(PartitionMountPoint *f, size_t n) { |
| 328 |
assert(f || n == 0); |
| 329 |
|
| 330 |
FOREACH_ARRAY(i, f, n) { |
| 331 |
free(i->where); |
| 332 |
free(i->options); |
| 333 |
} |
| 334 |
|
| 335 |
free(f); |
| 336 |
} |
| 337 |
|
| 338 |
typedef struct PartitionEncryptedVolume { |
| 339 |
char *name; |
| 340 |
char *keyfile; |
| 341 |
char *options; |
| 342 |
bool fixate_volume_key; |
| 343 |
} PartitionEncryptedVolume; |
| 344 |
|
| 345 |
static PartitionEncryptedVolume* partition_encrypted_volume_free(PartitionEncryptedVolume *c) { |
| 346 |
if (!c) |
| 347 |
return NULL; |
| 348 |
|
| 349 |
free(c->name); |
| 350 |
free(c->keyfile); |
| 351 |
free(c->options); |
| 352 |
|
| 353 |
return mfree(c); |
| 354 |
} |
| 355 |
|
| 356 |
typedef struct CopyFiles { |
| 357 |
char *source; |
| 358 |
char *target; |
| 359 |
CopyFlags flags; |
| 360 |
} CopyFiles; |
| 361 |
|
| 362 |
static void copy_files_free_many(CopyFiles *f, size_t n) { |
| 363 |
assert(f || n == 0); |
| 364 |
|
| 365 |
FOREACH_ARRAY(i, f, n) { |
| 366 |
free(i->source); |
| 367 |
free(i->target); |
| 368 |
} |
| 369 |
|
| 370 |
free(f); |
| 371 |
} |
| 372 |
|
| 373 |
static BtrfsSubvolFlags subvolume_flags_from_string_one(const char *s) { |
| 374 |
/* This is a bitmask (i.e. not dense), hence we don't use the "string-table.h" stuff here. */ |
| 375 |
|
| 376 |
assert(s); |
| 377 |
|
| 378 |
if (streq(s, "ro")) |
| 379 |
return BTRFS_SUBVOL_RO; |
| 380 |
|
| 381 |
if (streq(s, "nodatacow")) |
| 382 |
return BTRFS_SUBVOL_NODATACOW; |
| 383 |
|
| 384 |
return _BTRFS_SUBVOL_FLAGS_INVALID; |
| 385 |
} |
| 386 |
|
| 387 |
static BtrfsSubvolFlags subvolume_flags_from_string(const char *s) { |
| 388 |
BtrfsSubvolFlags flags = 0; |
| 389 |
int r; |
| 390 |
|
| 391 |
assert(s); |
| 392 |
|
| 393 |
for (;;) { |
| 394 |
_cleanup_free_ char *f = NULL; |
| 395 |
BtrfsSubvolFlags ff; |
| 396 |
|
| 397 |
r = extract_first_word(&s, &f, ",", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 398 |
if (r < 0) |
| 399 |
return r; |
| 400 |
if (r == 0) |
| 401 |
break; |
| 402 |
|
| 403 |
ff = subvolume_flags_from_string_one(f); |
| 404 |
if (ff < 0) |
| 405 |
return -EBADRQC; /* recognizable error */ |
| 406 |
|
| 407 |
flags |= ff; |
| 408 |
} |
| 409 |
|
| 410 |
return flags; |
| 411 |
} |
| 412 |
|
| 413 |
typedef struct Subvolume { |
| 414 |
char *path; |
| 415 |
BtrfsSubvolFlags flags; |
| 416 |
} Subvolume; |
| 417 |
|
| 418 |
static Subvolume* subvolume_free(Subvolume *s) { |
| 419 |
if (!s) |
| 420 |
return NULL; |
| 421 |
|
| 422 |
free(s->path); |
| 423 |
return mfree(s); |
| 424 |
} |
| 425 |
|
| 426 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(subvolume_hash_ops, char, path_hash_func, path_compare, Subvolume, subvolume_free); |
| 427 |
|
| 428 |
typedef struct Context Context; |
| 429 |
|
| 430 |
typedef struct { |
| 431 |
uint64_t devid; |
| 432 |
int mountpoint_fd; |
| 433 |
int source_fd; |
| 434 |
char *source_path; |
| 435 |
uint64_t source_size; |
| 436 |
bool done; |
| 437 |
} BtrfsReplacement; |
| 438 |
|
| 439 |
static BtrfsReplacement* btrfs_replacement_free(BtrfsReplacement *b) { |
| 440 |
if (!b) |
| 441 |
return NULL; |
| 442 |
|
| 443 |
safe_close(b->mountpoint_fd); |
| 444 |
safe_close(b->source_fd); |
| 445 |
free(b->source_path); |
| 446 |
return mfree(b); |
| 447 |
} |
| 448 |
|
| 449 |
DEFINE_TRIVIAL_CLEANUP_FUNC(BtrfsReplacement*, btrfs_replacement_free); |
| 450 |
|
| 451 |
typedef struct Partition { |
| 452 |
Context *context; |
| 453 |
|
| 454 |
char *definition_path; |
| 455 |
char **drop_in_files; |
| 456 |
|
| 457 |
GptPartitionType type; |
| 458 |
sd_id128_t current_uuid, new_uuid; |
| 459 |
bool new_uuid_is_set; |
| 460 |
char *current_label, *new_label; /* Used for the GPT partition label + fs superblock label */ |
| 461 |
char *new_volume_label; /* used for LUKS superblock */ |
| 462 |
sd_id128_t fs_uuid, luks_uuid, verity_uuid; |
| 463 |
uint8_t verity_salt[SHA256_DIGEST_SIZE]; |
| 464 |
|
| 465 |
bool dropped; |
| 466 |
bool factory_reset; |
| 467 |
bool discarded; |
| 468 |
int32_t priority; |
| 469 |
|
| 470 |
uint32_t weight, padding_weight; |
| 471 |
|
| 472 |
uint64_t current_size, new_size; |
| 473 |
uint64_t size_min, size_max; |
| 474 |
|
| 475 |
uint64_t current_padding, new_padding; |
| 476 |
uint64_t padding_min, padding_max; |
| 477 |
|
| 478 |
uint64_t partno; |
| 479 |
uint64_t offset; |
| 480 |
|
| 481 |
struct fdisk_partition *current_partition; |
| 482 |
struct fdisk_partition *new_partition; |
| 483 |
FreeArea *padding_area; |
| 484 |
FreeArea *allocated_to_area; |
| 485 |
|
| 486 |
char *copy_blocks_path; |
| 487 |
bool copy_blocks_path_is_our_file; |
| 488 |
bool copy_blocks_auto; |
| 489 |
const char *copy_blocks_root; |
| 490 |
int copy_blocks_fd; |
| 491 |
uint64_t copy_blocks_offset; |
| 492 |
uint64_t copy_blocks_size; |
| 493 |
uint64_t copy_blocks_done; |
| 494 |
|
| 495 |
char *format; |
| 496 |
char *block_device_replace; |
| 497 |
BtrfsReplacement *btrfs_replaced; |
| 498 |
char *volume_name; |
| 499 |
char **exclude_files_source; |
| 500 |
char **exclude_files_target; |
| 501 |
char **make_directories; |
| 502 |
char **make_symlinks; |
| 503 |
OrderedHashmap *subvolumes; |
| 504 |
char *default_subvolume; |
| 505 |
EncryptMode encrypt; |
| 506 |
struct iovec key; |
| 507 |
Tpm2PCRValue *tpm2_hash_pcr_values; |
| 508 |
size_t tpm2_n_hash_pcr_values; |
| 509 |
IntegrityMode integrity; |
| 510 |
IntegrityAlg integrity_alg; |
| 511 |
EncryptKDF encrypt_kdf; |
| 512 |
VerityMode verity; |
| 513 |
char *verity_match_key; |
| 514 |
MinimizeMode minimize; |
| 515 |
uint64_t verity_data_block_size; |
| 516 |
uint64_t verity_hash_block_size; |
| 517 |
char *compression; |
| 518 |
char *compression_level; |
| 519 |
uint64_t fs_sector_size; |
| 520 |
int discard; |
| 521 |
|
| 522 |
int add_validatefs; |
| 523 |
CopyFiles *copy_files; |
| 524 |
size_t n_copy_files; |
| 525 |
|
| 526 |
uint64_t gpt_flags; |
| 527 |
int no_auto; |
| 528 |
int read_only; |
| 529 |
int growfs; |
| 530 |
|
| 531 |
struct iovec roothash; |
| 532 |
|
| 533 |
char *split_name_format; |
| 534 |
char *split_path; |
| 535 |
|
| 536 |
PartitionMountPoint *mountpoints; |
| 537 |
size_t n_mountpoints; |
| 538 |
|
| 539 |
PartitionEncryptedVolume *encrypted_volume; |
| 540 |
|
| 541 |
unsigned last_percent; |
| 542 |
RateLimit progress_ratelimit; |
| 543 |
|
| 544 |
char *supplement_for_name; |
| 545 |
struct Partition *supplement_for, *supplement_target_for; |
| 546 |
struct Partition *suppressing; |
| 547 |
|
| 548 |
struct Partition *siblings[_VERITY_MODE_MAX]; |
| 549 |
|
| 550 |
LIST_FIELDS(struct Partition, partitions); |
| 551 |
} Partition; |
| 552 |
|
| 553 |
#define PARTITION_IS_FOREIGN(p) (!(p)->definition_path) |
| 554 |
#define PARTITION_EXISTS(p) (!!(p)->current_partition) |
| 555 |
#define PARTITION_SUPPRESSED(p) ((p)->supplement_for && (p)->supplement_for->suppressing == (p)) |
| 556 |
|
| 557 |
struct FreeArea { |
| 558 |
Partition *after; |
| 559 |
uint64_t size; |
| 560 |
uint64_t allocated; |
| 561 |
}; |
| 562 |
|
| 563 |
struct Context { |
| 564 |
char **definitions; |
| 565 |
|
| 566 |
LIST_HEAD(Partition, partitions); |
| 567 |
size_t n_partitions; |
| 568 |
|
| 569 |
FreeArea **free_areas; |
| 570 |
size_t n_free_areas; |
| 571 |
|
| 572 |
uint64_t start, end, total; |
| 573 |
|
| 574 |
struct fdisk_context *fdisk_context; |
| 575 |
int fdisk_context_fd; |
| 576 |
uint64_t sector_size, grain_size, default_fs_sector_size; |
| 577 |
|
| 578 |
sd_id128_t seed; |
| 579 |
|
| 580 |
char *node; |
| 581 |
bool node_is_our_file; |
| 582 |
int backing_fd; |
| 583 |
|
| 584 |
EmptyMode empty; |
| 585 |
bool dry_run; |
| 586 |
|
| 587 |
bool from_scratch; |
| 588 |
|
| 589 |
#if HAVE_OPENSSL |
| 590 |
X509 *certificate; |
| 591 |
OpenSSLAskPasswordUI *ui; |
| 592 |
EVP_PKEY *private_key; |
| 593 |
#endif |
| 594 |
|
| 595 |
bool defer_partitions_empty; |
| 596 |
bool defer_partitions_factory_reset; |
| 597 |
|
| 598 |
sd_varlink *link; /* If 'more' is used on the Varlink call, we'll send progress info over this link */ |
| 599 |
|
| 600 |
bool needs_rescan; |
| 601 |
}; |
| 602 |
|
| 603 |
static const char *empty_mode_table[_EMPTY_MODE_MAX] = { |
| 604 |
[EMPTY_UNSET] = "unset", |
| 605 |
[EMPTY_REFUSE] = "refuse", |
| 606 |
[EMPTY_ALLOW] = "allow", |
| 607 |
[EMPTY_REQUIRE] = "require", |
| 608 |
[EMPTY_FORCE] = "force", |
| 609 |
[EMPTY_CREATE] = "create", |
| 610 |
}; |
| 611 |
|
| 612 |
static const char *append_mode_table[_APPEND_MODE_MAX] = { |
| 613 |
[APPEND_NO] = "no", |
| 614 |
[APPEND_AUTO] = "auto", |
| 615 |
[APPEND_REPLACE] = "replace", |
| 616 |
}; |
| 617 |
|
| 618 |
static const char *encrypt_mode_table[_ENCRYPT_MODE_MAX] = { |
| 619 |
[ENCRYPT_OFF] = "off", |
| 620 |
[ENCRYPT_KEY_FILE] = "key-file", |
| 621 |
[ENCRYPT_TPM2] = "tpm2", |
| 622 |
[ENCRYPT_KEY_FILE_TPM2] = "key-file+tpm2", |
| 623 |
}; |
| 624 |
|
| 625 |
/* Going forward, the plan is to add two more modes: |
| 626 |
* [INTEGRITY_DATA] = "data" (interleave data and integrity tags on the same device), |
| 627 |
* [INTEGRITY_META] = "meta" (use a separate device for storing integrity tags). |
| 628 |
* Also, INTEGRITY_INLINE will be using hardware sector integrity fields when used |
| 629 |
* without encryption. */ |
| 630 |
static const char *integrity_mode_table[_INTEGRITY_MODE_MAX] = { |
| 631 |
[INTEGRITY_OFF] = "off", /* no integrity protection */ |
| 632 |
[INTEGRITY_INLINE] = "inline", /* luks2 storage when encrypted */ |
| 633 |
}; |
| 634 |
|
| 635 |
static const char *integrity_alg_table[_INTEGRITY_ALG_MAX] = { |
| 636 |
[INTEGRITY_ALG_HMAC_SHA1] = "hmac-sha1", |
| 637 |
[INTEGRITY_ALG_HMAC_SHA256] = "hmac-sha256", |
| 638 |
[INTEGRITY_ALG_HMAC_SHA512] = "hmac-sha512", |
| 639 |
}; |
| 640 |
|
| 641 |
static const char *encrypt_kdf_table[_ENCRYPT_KDF_MAX] = { |
| 642 |
[ENCRYPT_KDF_ARGON2ID] = "argon2id", |
| 643 |
[ENCRYPT_KDF_PBKDF2] = "pbkdf2", |
| 644 |
[ENCRYPT_KDF_MINIMAL] = "minimal", |
| 645 |
}; |
| 646 |
|
| 647 |
static const char *verity_mode_table[_VERITY_MODE_MAX] = { |
| 648 |
[VERITY_OFF] = "off", |
| 649 |
[VERITY_DATA] = "data", |
| 650 |
[VERITY_HASH] = "hash", |
| 651 |
[VERITY_SIG] = "signature", |
| 652 |
}; |
| 653 |
|
| 654 |
static const char *minimize_mode_table[_MINIMIZE_MODE_MAX] = { |
| 655 |
[MINIMIZE_OFF] = "off", |
| 656 |
[MINIMIZE_BEST] = "best", |
| 657 |
[MINIMIZE_GUESS] = "guess", |
| 658 |
}; |
| 659 |
|
| 660 |
static const char *progress_phase_table[_PROGRESS_PHASE_MAX] = { |
| 661 |
[PROGRESS_LOADING_DEFINITIONS] = "loading-definitions", |
| 662 |
[PROGRESS_LOADING_TABLE] = "loading-table", |
| 663 |
[PROGRESS_OPENING_COPY_BLOCK_SOURCES] = "opening-copy-block-sources", |
| 664 |
[PROGRESS_OPENING_BLOCK_DEVICE_REPLACE_SOURCES] = "opening-block-device-replace-sources", |
| 665 |
[PROGRESS_ACQUIRING_PARTITION_LABELS] = "acquiring-partition-labels", |
| 666 |
[PROGRESS_MINIMIZING] = "minimizing", |
| 667 |
[PROGRESS_PLACING] = "placing", |
| 668 |
[PROGRESS_WIPING_DISK] = "wiping-disk", |
| 669 |
[PROGRESS_WIPING_PARTITION] = "wiping-partition", |
| 670 |
[PROGRESS_COPYING_PARTITION] = "copying-partition", |
| 671 |
[PROGRESS_FORMATTING_PARTITION] = "formatting-partition", |
| 672 |
[PROGRESS_ADJUSTING_PARTITION] = "adjusting-partition", |
| 673 |
[PROGRESS_WRITING_TABLE] = "writing-table", |
| 674 |
[PROGRESS_REREADING_TABLE] = "rereading-table", |
| 675 |
[PROGRESS_REPLACING_DEVICE] = "replacing-device", |
| 676 |
}; |
| 677 |
|
| 678 |
static uint64_t determine_grain_size(uint64_t sector_size) { |
| 679 |
return MAX(arg_grain_size > 0 ? arg_grain_size : 4096U, sector_size); |
| 680 |
} |
| 681 |
|
| 682 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(empty_mode, EmptyMode); |
| 683 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(append_mode, AppendMode); |
| 684 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(encrypt_mode, EncryptMode, ENCRYPT_KEY_FILE); |
| 685 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(integrity_mode, IntegrityMode, INTEGRITY_INLINE); |
| 686 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(integrity_alg, IntegrityAlg); |
| 687 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(encrypt_kdf, EncryptKDF); |
| 688 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(verity_mode, VerityMode); |
| 689 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(minimize_mode, MinimizeMode, MINIMIZE_BEST); |
| 690 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(progress_phase, ProgressPhase); |
| 691 |
|
| 692 |
static uint64_t round_down_size(uint64_t v, uint64_t p) { |
| 693 |
return (v / p) * p; |
| 694 |
} |
| 695 |
|
| 696 |
static uint64_t round_up_size(uint64_t v, uint64_t p) { |
| 697 |
|
| 698 |
v = DIV_ROUND_UP(v, p); |
| 699 |
|
| 700 |
if (v > UINT64_MAX / p) |
| 701 |
return UINT64_MAX; /* overflow */ |
| 702 |
|
| 703 |
return v * p; |
| 704 |
} |
| 705 |
|
| 706 |
/* calculates the size of a dm-verity hash partition's contents */ |
| 707 |
static int calculate_verity_hash_size( |
| 708 |
uint64_t data_bytes, |
| 709 |
uint64_t hash_block_size, |
| 710 |
uint64_t data_block_size, |
| 711 |
uint64_t *ret_bytes) { |
| 712 |
|
| 713 |
assert(ret_bytes); |
| 714 |
|
| 715 |
/* The calculation here is based on the documented on-disk format of the dm-verity |
| 716 |
* https://docs.kernel.org/admin-guide/device-mapper/verity.html#hash-tree |
| 717 |
* |
| 718 |
* Upstream implementation: |
| 719 |
* https://gitlab.com/cryptsetup/cryptsetup/-/blob/v2.7.5/lib/verity/verity_hash.c */ |
| 720 |
|
| 721 |
uint64_t data_blocks = DIV_ROUND_UP(data_bytes, data_block_size); |
| 722 |
if (data_blocks > UINT64_MAX / data_block_size) |
| 723 |
return -EOVERFLOW; |
| 724 |
|
| 725 |
/* hashes that fit in one hash block (node in the merkle tree) */ |
| 726 |
uint64_t hashes_per_hash_block = hash_block_size / SHA256_DIGEST_SIZE; |
| 727 |
|
| 728 |
/* initialize with 2 for the root of the merkle tree + the superblock */ |
| 729 |
uint64_t hash_blocks = 2; |
| 730 |
|
| 731 |
/* iterate through the levels of the merkle tree bottom up */ |
| 732 |
uint64_t remaining_blocks = data_blocks; |
| 733 |
|
| 734 |
while (remaining_blocks > hashes_per_hash_block) { |
| 735 |
uint64_t hash_blocks_for_level; |
| 736 |
/* number of hash blocks required to reference the underlying blocks */ |
| 737 |
hash_blocks_for_level = DIV_ROUND_UP(remaining_blocks, hashes_per_hash_block); |
| 738 |
|
| 739 |
if (hash_blocks > UINT64_MAX - hash_blocks_for_level) |
| 740 |
return -EOVERFLOW; |
| 741 |
|
| 742 |
/* add current layer to the total number of hash blocks */ |
| 743 |
hash_blocks += hash_blocks_for_level; |
| 744 |
/* hashes on this level serve as the blocks on which the next level is built */ |
| 745 |
remaining_blocks = hash_blocks_for_level; |
| 746 |
} |
| 747 |
|
| 748 |
if (hash_blocks > UINT64_MAX / hash_block_size) |
| 749 |
return -EOVERFLOW; |
| 750 |
|
| 751 |
*ret_bytes = hash_blocks * hash_block_size; |
| 752 |
|
| 753 |
return 0; |
| 754 |
} |
| 755 |
|
| 756 |
static Partition *partition_new(Context *c) { |
| 757 |
Partition *p; |
| 758 |
|
| 759 |
p = new(Partition, 1); |
| 760 |
if (!p) |
| 761 |
return NULL; |
| 762 |
|
| 763 |
*p = (Partition) { |
| 764 |
.context = c, |
| 765 |
.weight = 1000, |
| 766 |
.padding_weight = 0, |
| 767 |
.current_size = UINT64_MAX, |
| 768 |
.new_size = UINT64_MAX, |
| 769 |
.size_min = UINT64_MAX, |
| 770 |
.size_max = UINT64_MAX, |
| 771 |
.current_padding = UINT64_MAX, |
| 772 |
.new_padding = UINT64_MAX, |
| 773 |
.padding_min = UINT64_MAX, |
| 774 |
.padding_max = UINT64_MAX, |
| 775 |
.partno = UINT64_MAX, |
| 776 |
.offset = UINT64_MAX, |
| 777 |
.copy_blocks_fd = -EBADF, |
| 778 |
.copy_blocks_offset = UINT64_MAX, |
| 779 |
.copy_blocks_size = UINT64_MAX, |
| 780 |
.no_auto = -1, |
| 781 |
.read_only = -1, |
| 782 |
.growfs = -1, |
| 783 |
.verity_data_block_size = UINT64_MAX, |
| 784 |
.verity_hash_block_size = UINT64_MAX, |
| 785 |
.add_validatefs = -1, |
| 786 |
.last_percent = UINT_MAX, |
| 787 |
.progress_ratelimit = { 100 * USEC_PER_MSEC, 1 }, |
| 788 |
.fs_sector_size = UINT64_MAX, |
| 789 |
.discard = -1, |
| 790 |
.encrypt_kdf = _ENCRYPT_KDF_INVALID, |
| 791 |
}; |
| 792 |
|
| 793 |
return p; |
| 794 |
} |
| 795 |
|
| 796 |
static void partition_unlink_supplement(Partition *p) { |
| 797 |
assert(p); |
| 798 |
|
| 799 |
assert(!p->supplement_for || !p->supplement_target_for); /* Can't be both */ |
| 800 |
|
| 801 |
if (p->supplement_target_for) { |
| 802 |
assert(p->supplement_target_for->supplement_for == p); |
| 803 |
|
| 804 |
p->supplement_target_for->supplement_for = NULL; |
| 805 |
} |
| 806 |
|
| 807 |
if (p->supplement_for) { |
| 808 |
assert(p->supplement_for->supplement_target_for == p); |
| 809 |
assert(!p->supplement_for->suppressing || p->supplement_for->suppressing == p); |
| 810 |
|
| 811 |
p->supplement_for->supplement_target_for = p->supplement_for->suppressing = NULL; |
| 812 |
} |
| 813 |
|
| 814 |
p->supplement_for_name = mfree(p->supplement_for_name); |
| 815 |
p->supplement_target_for = p->supplement_for = p->suppressing = NULL; |
| 816 |
} |
| 817 |
|
| 818 |
static Partition* partition_free(Partition *p) { |
| 819 |
if (!p) |
| 820 |
return NULL; |
| 821 |
|
| 822 |
free(p->current_label); |
| 823 |
free(p->new_label); |
| 824 |
free(p->new_volume_label); |
| 825 |
free(p->definition_path); |
| 826 |
strv_free(p->drop_in_files); |
| 827 |
|
| 828 |
if (p->current_partition) |
| 829 |
sym_fdisk_unref_partition(p->current_partition); |
| 830 |
if (p->new_partition) |
| 831 |
sym_fdisk_unref_partition(p->new_partition); |
| 832 |
|
| 833 |
if (p->copy_blocks_path_is_our_file) |
| 834 |
unlink_and_free(p->copy_blocks_path); |
| 835 |
else |
| 836 |
free(p->copy_blocks_path); |
| 837 |
safe_close(p->copy_blocks_fd); |
| 838 |
|
| 839 |
free(p->format); |
| 840 |
free(p->block_device_replace); |
| 841 |
btrfs_replacement_free(p->btrfs_replaced); |
| 842 |
free(p->volume_name); |
| 843 |
strv_free(p->exclude_files_source); |
| 844 |
strv_free(p->exclude_files_target); |
| 845 |
strv_free(p->make_directories); |
| 846 |
strv_free(p->make_symlinks); |
| 847 |
ordered_hashmap_free(p->subvolumes); |
| 848 |
free(p->default_subvolume); |
| 849 |
free(p->tpm2_hash_pcr_values); |
| 850 |
free(p->verity_match_key); |
| 851 |
free(p->compression); |
| 852 |
free(p->compression_level); |
| 853 |
|
| 854 |
iovec_done_erase(&p->key); |
| 855 |
|
| 856 |
copy_files_free_many(p->copy_files, p->n_copy_files); |
| 857 |
|
| 858 |
iovec_done(&p->roothash); |
| 859 |
|
| 860 |
free(p->split_name_format); |
| 861 |
unlink_and_free(p->split_path); |
| 862 |
partition_mountpoint_free_many(p->mountpoints, p->n_mountpoints); |
| 863 |
p->mountpoints = NULL; |
| 864 |
p->n_mountpoints = 0; |
| 865 |
|
| 866 |
partition_encrypted_volume_free(p->encrypted_volume); |
| 867 |
|
| 868 |
partition_unlink_supplement(p); |
| 869 |
|
| 870 |
return mfree(p); |
| 871 |
} |
| 872 |
|
| 873 |
static void partition_foreignize(Partition *p) { |
| 874 |
assert(p); |
| 875 |
assert(PARTITION_EXISTS(p)); |
| 876 |
|
| 877 |
/* Reset several parameters set through definition file to make the partition foreign. */ |
| 878 |
|
| 879 |
p->definition_path = mfree(p->definition_path); |
| 880 |
p->drop_in_files = strv_free(p->drop_in_files); |
| 881 |
|
| 882 |
p->copy_blocks_path = mfree(p->copy_blocks_path); |
| 883 |
p->copy_blocks_fd = safe_close(p->copy_blocks_fd); |
| 884 |
p->copy_blocks_root = NULL; |
| 885 |
|
| 886 |
p->format = mfree(p->format); |
| 887 |
p->block_device_replace = mfree(p->block_device_replace); |
| 888 |
p->btrfs_replaced = btrfs_replacement_free(p->btrfs_replaced); |
| 889 |
p->volume_name = mfree(p->volume_name); |
| 890 |
p->exclude_files_source = strv_free(p->exclude_files_source); |
| 891 |
p->exclude_files_target = strv_free(p->exclude_files_target); |
| 892 |
p->make_directories = strv_free(p->make_directories); |
| 893 |
p->make_symlinks = strv_free(p->make_symlinks); |
| 894 |
p->subvolumes = ordered_hashmap_free(p->subvolumes); |
| 895 |
p->default_subvolume = mfree(p->default_subvolume); |
| 896 |
p->tpm2_hash_pcr_values = mfree(p->tpm2_hash_pcr_values); |
| 897 |
p->verity_match_key = mfree(p->verity_match_key); |
| 898 |
p->compression = mfree(p->compression); |
| 899 |
p->compression_level = mfree(p->compression_level); |
| 900 |
|
| 901 |
iovec_done_erase(&p->key); |
| 902 |
|
| 903 |
copy_files_free_many(p->copy_files, p->n_copy_files); |
| 904 |
p->copy_files = NULL; |
| 905 |
p->n_copy_files = 0; |
| 906 |
|
| 907 |
p->priority = 0; |
| 908 |
p->weight = 1000; |
| 909 |
p->padding_weight = 0; |
| 910 |
p->size_min = UINT64_MAX; |
| 911 |
p->size_max = UINT64_MAX; |
| 912 |
p->padding_min = UINT64_MAX; |
| 913 |
p->padding_max = UINT64_MAX; |
| 914 |
p->no_auto = -1; |
| 915 |
p->read_only = -1; |
| 916 |
p->growfs = -1; |
| 917 |
p->verity = VERITY_OFF; |
| 918 |
p->add_validatefs = false; |
| 919 |
p->fs_sector_size = UINT64_MAX; |
| 920 |
p->discard = -1; |
| 921 |
|
| 922 |
partition_mountpoint_free_many(p->mountpoints, p->n_mountpoints); |
| 923 |
p->mountpoints = NULL; |
| 924 |
p->n_mountpoints = 0; |
| 925 |
|
| 926 |
p->encrypted_volume = partition_encrypted_volume_free(p->encrypted_volume); |
| 927 |
|
| 928 |
partition_unlink_supplement(p); |
| 929 |
} |
| 930 |
|
| 931 |
static bool partition_type_exclude(const GptPartitionType *type) { |
| 932 |
if (arg_filter_partitions_type == FILTER_PARTITIONS_NONE) |
| 933 |
return false; |
| 934 |
|
| 935 |
for (size_t i = 0; i < arg_n_filter_partitions; i++) |
| 936 |
if (sd_id128_equal(type->uuid, arg_filter_partitions[i].uuid)) |
| 937 |
return arg_filter_partitions_type == FILTER_PARTITIONS_EXCLUDE; |
| 938 |
|
| 939 |
return arg_filter_partitions_type == FILTER_PARTITIONS_INCLUDE; |
| 940 |
} |
| 941 |
|
| 942 |
static bool partition_type_defer(const GptPartitionType *type) { |
| 943 |
for (size_t i = 0; i < arg_n_defer_partitions; i++) |
| 944 |
if (sd_id128_equal(type->uuid, arg_defer_partitions[i].uuid)) |
| 945 |
return true; |
| 946 |
|
| 947 |
return false; |
| 948 |
} |
| 949 |
|
| 950 |
static Partition* partition_unlink_and_free(Context *context, Partition *p) { |
| 951 |
if (!p) |
| 952 |
return NULL; |
| 953 |
|
| 954 |
LIST_REMOVE(partitions, context->partitions, p); |
| 955 |
|
| 956 |
assert(context->n_partitions > 0); |
| 957 |
context->n_partitions--; |
| 958 |
|
| 959 |
return partition_free(p); |
| 960 |
} |
| 961 |
|
| 962 |
DEFINE_TRIVIAL_CLEANUP_FUNC(Partition*, partition_free); |
| 963 |
|
| 964 |
static Context* context_new( |
| 965 |
char **definitions, |
| 966 |
EmptyMode empty, |
| 967 |
bool dry_run, |
| 968 |
sd_id128_t seed) { |
| 969 |
|
| 970 |
_cleanup_strv_free_ char **d = NULL; |
| 971 |
if (!strv_isempty(definitions)) { |
| 972 |
d = strv_copy(definitions); |
| 973 |
if (!d) |
| 974 |
return NULL; |
| 975 |
} |
| 976 |
|
| 977 |
Context *context = new(Context, 1); |
| 978 |
if (!context) |
| 979 |
return NULL; |
| 980 |
|
| 981 |
*context = (Context) { |
| 982 |
.definitions = TAKE_PTR(d), |
| 983 |
.start = UINT64_MAX, |
| 984 |
.end = UINT64_MAX, |
| 985 |
.total = UINT64_MAX, |
| 986 |
.seed = seed, |
| 987 |
.empty = empty, |
| 988 |
.dry_run = dry_run, |
| 989 |
.backing_fd = -EBADF, |
| 990 |
.fdisk_context_fd = -EBADF, |
| 991 |
}; |
| 992 |
|
| 993 |
return context; |
| 994 |
} |
| 995 |
|
| 996 |
static void context_free_free_areas(Context *context) { |
| 997 |
assert(context); |
| 998 |
|
| 999 |
for (size_t i = 0; i < context->n_free_areas; i++) |
| 1000 |
free(context->free_areas[i]); |
| 1001 |
|
| 1002 |
context->free_areas = mfree(context->free_areas); |
| 1003 |
context->n_free_areas = 0; |
| 1004 |
} |
| 1005 |
|
| 1006 |
static Context* context_free(Context *context) { |
| 1007 |
if (!context) |
| 1008 |
return NULL; |
| 1009 |
|
| 1010 |
strv_free(context->definitions); |
| 1011 |
|
| 1012 |
while (context->partitions) |
| 1013 |
partition_unlink_and_free(context, context->partitions); |
| 1014 |
assert(context->n_partitions == 0); |
| 1015 |
|
| 1016 |
context_free_free_areas(context); |
| 1017 |
|
| 1018 |
if (context->fdisk_context) |
| 1019 |
sym_fdisk_unref_context(context->fdisk_context); |
| 1020 |
safe_close(context->fdisk_context_fd); |
| 1021 |
|
| 1022 |
safe_close(context->backing_fd); |
| 1023 |
if (context->node_is_our_file) |
| 1024 |
unlink_and_free(context->node); |
| 1025 |
else |
| 1026 |
free(context->node); |
| 1027 |
|
| 1028 |
#if HAVE_OPENSSL |
| 1029 |
if (context->certificate) |
| 1030 |
sym_X509_free(context->certificate); |
| 1031 |
openssl_ask_password_ui_free(context->ui); |
| 1032 |
if (context->private_key) |
| 1033 |
sym_EVP_PKEY_free(context->private_key); |
| 1034 |
#endif |
| 1035 |
|
| 1036 |
context->link = sd_varlink_unref(context->link); |
| 1037 |
|
| 1038 |
return mfree(context); |
| 1039 |
} |
| 1040 |
|
| 1041 |
DEFINE_TRIVIAL_CLEANUP_FUNC(Context*, context_free); |
| 1042 |
|
| 1043 |
static void context_disarm_auto_removal(Context *context) { |
| 1044 |
assert(context); |
| 1045 |
|
| 1046 |
/* Make sure automatic removal of half-written artifacts is disarmed */ |
| 1047 |
context->node = mfree(context->node); |
| 1048 |
|
| 1049 |
LIST_FOREACH(partitions, p, context->partitions) |
| 1050 |
p->split_path = mfree(p->split_path); |
| 1051 |
} |
| 1052 |
|
| 1053 |
static int context_add_free_area( |
| 1054 |
Context *context, |
| 1055 |
uint64_t size, |
| 1056 |
Partition *after) { |
| 1057 |
|
| 1058 |
FreeArea *a; |
| 1059 |
|
| 1060 |
assert(context); |
| 1061 |
assert(!after || !after->padding_area); |
| 1062 |
|
| 1063 |
if (!GREEDY_REALLOC(context->free_areas, context->n_free_areas + 1)) |
| 1064 |
return -ENOMEM; |
| 1065 |
|
| 1066 |
a = new(FreeArea, 1); |
| 1067 |
if (!a) |
| 1068 |
return -ENOMEM; |
| 1069 |
|
| 1070 |
*a = (FreeArea) { |
| 1071 |
.size = size, |
| 1072 |
.after = after, |
| 1073 |
}; |
| 1074 |
|
| 1075 |
context->free_areas[context->n_free_areas++] = a; |
| 1076 |
|
| 1077 |
if (after) |
| 1078 |
after->padding_area = a; |
| 1079 |
|
| 1080 |
return 0; |
| 1081 |
} |
| 1082 |
|
| 1083 |
static void partition_drop_or_foreignize(Partition *p) { |
| 1084 |
if (!p || p->dropped || PARTITION_IS_FOREIGN(p)) |
| 1085 |
return; |
| 1086 |
|
| 1087 |
if (PARTITION_EXISTS(p)) { |
| 1088 |
log_info("Can't grow existing partition %s of priority %" PRIi32 ", ignoring.", |
| 1089 |
strna(p->current_label ?: p->new_label), p->priority); |
| 1090 |
|
| 1091 |
/* Handle the partition as foreign. Do not set dropped flag. */ |
| 1092 |
partition_foreignize(p); |
| 1093 |
} else { |
| 1094 |
log_info("Can't fit partition %s of priority %" PRIi32 ", dropping.", |
| 1095 |
p->definition_path, p->priority); |
| 1096 |
|
| 1097 |
p->dropped = true; |
| 1098 |
p->allocated_to_area = NULL; |
| 1099 |
|
| 1100 |
/* If a supplement partition is dropped, we don't want to merge in its settings. */ |
| 1101 |
if (PARTITION_SUPPRESSED(p)) |
| 1102 |
p->supplement_for->suppressing = NULL; |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|
| 1106 |
static bool context_drop_or_foreignize_one_priority(Context *context) { |
| 1107 |
int32_t priority = 0; |
| 1108 |
|
| 1109 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 1110 |
if (p->dropped) |
| 1111 |
continue; |
| 1112 |
|
| 1113 |
priority = MAX(priority, p->priority); |
| 1114 |
} |
| 1115 |
|
| 1116 |
/* Refuse to drop partitions with 0 or negative priorities or partitions of priorities that have at |
| 1117 |
* least one existing priority */ |
| 1118 |
if (priority <= 0) |
| 1119 |
return false; |
| 1120 |
|
| 1121 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 1122 |
if (p->priority < priority) |
| 1123 |
continue; |
| 1124 |
|
| 1125 |
partition_drop_or_foreignize(p); |
| 1126 |
|
| 1127 |
/* We ensure that all verity sibling partitions have the same priority, so it's safe |
| 1128 |
* to drop all siblings here as well. */ |
| 1129 |
|
| 1130 |
for (VerityMode mode = VERITY_OFF + 1; mode < _VERITY_MODE_MAX; mode++) |
| 1131 |
partition_drop_or_foreignize(p->siblings[mode]); |
| 1132 |
} |
| 1133 |
|
| 1134 |
return true; |
| 1135 |
} |
| 1136 |
|
| 1137 |
static uint64_t partition_fs_sector_size(const Context *c, const Partition *p) { |
| 1138 |
assert(c); |
| 1139 |
assert(p); |
| 1140 |
|
| 1141 |
uint64_t ss; |
| 1142 |
|
| 1143 |
if (p->fs_sector_size != UINT64_MAX) |
| 1144 |
/* Prefer explicitly configured value */ |
| 1145 |
ss = p->fs_sector_size; |
| 1146 |
else |
| 1147 |
/* Otherwise follow the default sector size */ |
| 1148 |
ss = c->default_fs_sector_size; |
| 1149 |
|
| 1150 |
/* never allow the fs sector size to be picked smaller than the physical sector size */ |
| 1151 |
return MAX(ss, c->sector_size); |
| 1152 |
} |
| 1153 |
|
| 1154 |
static uint64_t partition_fstype_min_size(const Context *c, const Partition *p) { |
| 1155 |
assert(c); |
| 1156 |
assert(p); |
| 1157 |
|
| 1158 |
/* If a file system type is configured, then take it into consideration for the minimum partition |
| 1159 |
* size */ |
| 1160 |
|
| 1161 |
if (IN_SET(p->type.designator, PARTITION_ESP, PARTITION_XBOOTLDR) && streq_ptr(p->format, "vfat")) { |
| 1162 |
uint64_t ss = partition_fs_sector_size(c, p); |
| 1163 |
return ss >= 4096 ? ESP_MIN_SIZE_4K : ESP_MIN_SIZE; |
| 1164 |
} |
| 1165 |
|
| 1166 |
return minimal_size_by_fs_name(p->format); |
| 1167 |
} |
| 1168 |
|
| 1169 |
static uint64_t partition_min_size(const Context *context, const Partition *p) { |
| 1170 |
uint64_t sz; |
| 1171 |
|
| 1172 |
assert(context); |
| 1173 |
assert(p); |
| 1174 |
|
| 1175 |
/* Calculate the disk space we really need at minimum for this partition. If the partition already |
| 1176 |
* exists the current size is what we really need. If it doesn't exist yet refuse to allocate less |
| 1177 |
* than 4K. |
| 1178 |
* |
| 1179 |
* DEFAULT_MIN_SIZE is the default SizeMinBytes= we configure if nothing else is specified. */ |
| 1180 |
|
| 1181 |
if (PARTITION_IS_FOREIGN(p)) { |
| 1182 |
/* Don't allow changing size of partitions not managed by us */ |
| 1183 |
assert(p->current_size != UINT64_MAX); |
| 1184 |
return p->current_size; |
| 1185 |
} |
| 1186 |
|
| 1187 |
if (partition_designator_is_verity_sig(p->type.designator)) |
| 1188 |
return VERITY_SIG_SIZE; |
| 1189 |
|
| 1190 |
sz = p->current_size != UINT64_MAX ? p->current_size : HARD_MIN_SIZE; |
| 1191 |
|
| 1192 |
if (!PARTITION_EXISTS(p)) { |
| 1193 |
uint64_t d = 0; |
| 1194 |
|
| 1195 |
if (p->encrypt != ENCRYPT_OFF) |
| 1196 |
assert_se(INC_SAFE(&d, round_up_size(LUKS2_METADATA_KEEP_FREE, context->grain_size))); |
| 1197 |
|
| 1198 |
if (p->copy_blocks_size != UINT64_MAX) |
| 1199 |
assert_se(INC_SAFE(&d, round_up_size(p->copy_blocks_size, context->grain_size))); |
| 1200 |
else if (p->btrfs_replaced) |
| 1201 |
assert_se(INC_SAFE(&d, round_up_size(p->btrfs_replaced->source_size, context->grain_size))); |
| 1202 |
else if (p->format || p->encrypt != ENCRYPT_OFF) { |
| 1203 |
uint64_t f; |
| 1204 |
|
| 1205 |
/* If we shall synthesize a file system, take minimal fs size into account (assumed to be 4K if not known) */ |
| 1206 |
f = partition_fstype_min_size(context, p); |
| 1207 |
assert_se(INC_SAFE(&d, f == UINT64_MAX ? context->grain_size : round_up_size(f, context->grain_size))); |
| 1208 |
} |
| 1209 |
|
| 1210 |
if (d > sz) |
| 1211 |
sz = d; |
| 1212 |
} |
| 1213 |
|
| 1214 |
uint64_t min_size = p->size_min; |
| 1215 |
if (p->suppressing && (min_size == UINT64_MAX || p->suppressing->size_min > min_size)) |
| 1216 |
min_size = p->suppressing->size_min; |
| 1217 |
|
| 1218 |
/* Default to 10M min size, except if the file system is read-only, in which case let's not enforce a |
| 1219 |
* minimum size, because even if we wanted to we couldn't take possession of the extra space |
| 1220 |
* allocated. */ |
| 1221 |
if (min_size == UINT64_MAX) |
| 1222 |
min_size = (p->format && fstype_is_ro(p->format)) || p->verity != VERITY_OFF ? 1 : DEFAULT_MIN_SIZE; |
| 1223 |
|
| 1224 |
return MAX(round_up_size(min_size, context->grain_size), sz); |
| 1225 |
} |
| 1226 |
|
| 1227 |
static uint64_t partition_max_size(const Context *context, const Partition *p) { |
| 1228 |
uint64_t sm, override_max; |
| 1229 |
|
| 1230 |
/* Calculate how large the partition may become at max. This is generally the configured maximum |
| 1231 |
* size, except when it already exists and is larger than that. In that case it's the existing size, |
| 1232 |
* since we never want to shrink partitions. */ |
| 1233 |
|
| 1234 |
assert(context); |
| 1235 |
assert(p); |
| 1236 |
|
| 1237 |
if (PARTITION_IS_FOREIGN(p)) { |
| 1238 |
/* Don't allow changing size of partitions not managed by us */ |
| 1239 |
assert(p->current_size != UINT64_MAX); |
| 1240 |
return p->current_size; |
| 1241 |
} |
| 1242 |
|
| 1243 |
if (partition_designator_is_verity_sig(p->type.designator)) |
| 1244 |
return VERITY_SIG_SIZE; |
| 1245 |
|
| 1246 |
override_max = p->suppressing ? MIN(p->size_max, p->suppressing->size_max) : p->size_max; |
| 1247 |
if (override_max == UINT64_MAX) |
| 1248 |
return UINT64_MAX; |
| 1249 |
|
| 1250 |
sm = round_down_size(override_max, context->grain_size); |
| 1251 |
|
| 1252 |
if (p->current_size != UINT64_MAX) |
| 1253 |
sm = MAX(p->current_size, sm); |
| 1254 |
|
| 1255 |
return MAX(partition_min_size(context, p), sm); |
| 1256 |
} |
| 1257 |
|
| 1258 |
static uint64_t partition_min_padding(const Partition *p) { |
| 1259 |
uint64_t override_min; |
| 1260 |
|
| 1261 |
assert(p); |
| 1262 |
|
| 1263 |
override_min = p->suppressing ? MAX(p->padding_min, p->suppressing->padding_min) : p->padding_min; |
| 1264 |
return override_min != UINT64_MAX ? override_min : 0; |
| 1265 |
} |
| 1266 |
|
| 1267 |
static uint64_t partition_max_padding(const Partition *p) { |
| 1268 |
assert(p); |
| 1269 |
return p->suppressing ? MIN(p->padding_max, p->suppressing->padding_max) : p->padding_max; |
| 1270 |
} |
| 1271 |
|
| 1272 |
static uint64_t partition_min_size_with_padding(const Context *context, const Partition *p) { |
| 1273 |
uint64_t sz; |
| 1274 |
|
| 1275 |
/* Calculate the disk space we need for this partition plus any free space coming after it. This |
| 1276 |
* takes user configured padding into account as well as any additional whitespace needed to align |
| 1277 |
* the next partition to 4K again. */ |
| 1278 |
|
| 1279 |
assert(context); |
| 1280 |
assert(p); |
| 1281 |
|
| 1282 |
sz = partition_min_size(context, p) + partition_min_padding(p); |
| 1283 |
|
| 1284 |
if (PARTITION_EXISTS(p)) { |
| 1285 |
/* If the partition wasn't aligned, add extra space so that any we might add will be aligned */ |
| 1286 |
assert(p->offset != UINT64_MAX); |
| 1287 |
return round_up_size(p->offset + sz, context->grain_size) - p->offset; |
| 1288 |
} |
| 1289 |
|
| 1290 |
/* If this is a new partition we'll place it aligned, hence we just need to round up the required size here */ |
| 1291 |
return round_up_size(sz, context->grain_size); |
| 1292 |
} |
| 1293 |
|
| 1294 |
static uint64_t free_area_available(const FreeArea *a) { |
| 1295 |
assert(a); |
| 1296 |
|
| 1297 |
/* Determines how much of this free area is not allocated yet */ |
| 1298 |
|
| 1299 |
assert(a->size >= a->allocated); |
| 1300 |
return a->size - a->allocated; |
| 1301 |
} |
| 1302 |
|
| 1303 |
static uint64_t free_area_current_end(const Context *context, const FreeArea *a) { |
| 1304 |
assert(context); |
| 1305 |
assert(a); |
| 1306 |
|
| 1307 |
if (!a->after) |
| 1308 |
return free_area_available(a); |
| 1309 |
|
| 1310 |
assert(a->after->offset != UINT64_MAX); |
| 1311 |
assert(a->after->current_size != UINT64_MAX); |
| 1312 |
/* Silence static analyzers */ |
| 1313 |
assert(a->after->current_size <= UINT64_MAX - a->after->offset); |
| 1314 |
|
| 1315 |
/* Calculate where the free area ends, based on the offset of the partition preceding it. */ |
| 1316 |
return round_up_size(a->after->offset + a->after->current_size, context->grain_size) + free_area_available(a); |
| 1317 |
} |
| 1318 |
|
| 1319 |
static uint64_t free_area_min_end(const Context *context, const FreeArea *a) { |
| 1320 |
assert(context); |
| 1321 |
assert(a); |
| 1322 |
|
| 1323 |
if (!a->after) |
| 1324 |
return 0; |
| 1325 |
|
| 1326 |
assert(a->after->offset != UINT64_MAX); |
| 1327 |
assert(a->after->current_size != UINT64_MAX); |
| 1328 |
|
| 1329 |
/* Calculate where the partition would end when we give it as much as it needs. */ |
| 1330 |
return round_up_size(a->after->offset + partition_min_size_with_padding(context, a->after), context->grain_size); |
| 1331 |
} |
| 1332 |
|
| 1333 |
static uint64_t free_area_available_for_new_partitions(const Context *context, const FreeArea *a) { |
| 1334 |
assert(context); |
| 1335 |
assert(a); |
| 1336 |
|
| 1337 |
/* Similar to free_area_available(), but takes into account that the required size and padding of the |
| 1338 |
* preceding partition is honoured. */ |
| 1339 |
|
| 1340 |
return LESS_BY(free_area_current_end(context, a), free_area_min_end(context, a)); |
| 1341 |
} |
| 1342 |
|
| 1343 |
static int free_area_compare(FreeArea *const *a, FreeArea *const*b, Context *context) { |
| 1344 |
assert(a); |
| 1345 |
assert(b); |
| 1346 |
assert(context); |
| 1347 |
|
| 1348 |
return CMP(free_area_available_for_new_partitions(context, *a), |
| 1349 |
free_area_available_for_new_partitions(context, *b)); |
| 1350 |
} |
| 1351 |
|
| 1352 |
static uint64_t charge_size(const Context *context, uint64_t total, uint64_t amount) { |
| 1353 |
assert(context); |
| 1354 |
/* Subtract the specified amount from total, rounding up to multiple of 4K if there's room */ |
| 1355 |
assert(amount <= total); |
| 1356 |
return LESS_BY(total, round_up_size(amount, context->grain_size)); |
| 1357 |
} |
| 1358 |
|
| 1359 |
static uint64_t charge_weight(uint64_t total, uint64_t amount) { |
| 1360 |
assert(amount <= total); |
| 1361 |
return total - amount; |
| 1362 |
} |
| 1363 |
|
| 1364 |
static bool context_allocate_partitions(Context *context, uint64_t *ret_largest_free_area) { |
| 1365 |
assert(context); |
| 1366 |
|
| 1367 |
/* This may be called multiple times. Reset previous assignments. */ |
| 1368 |
for (size_t i = 0; i < context->n_free_areas; i++) |
| 1369 |
context->free_areas[i]->allocated = 0; |
| 1370 |
|
| 1371 |
/* Sort free areas by size, putting smallest first */ |
| 1372 |
typesafe_qsort_r(context->free_areas, context->n_free_areas, free_area_compare, context); |
| 1373 |
|
| 1374 |
/* In any case return size of the largest free area (i.e. not the size of all free areas |
| 1375 |
* combined!) */ |
| 1376 |
if (ret_largest_free_area) |
| 1377 |
*ret_largest_free_area = |
| 1378 |
context->n_free_areas == 0 ? 0 : |
| 1379 |
free_area_available_for_new_partitions(context, context->free_areas[context->n_free_areas-1]); |
| 1380 |
|
| 1381 |
/* Check that each existing partition can fit its area. */ |
| 1382 |
for (size_t i = 0; i < context->n_free_areas; i++) |
| 1383 |
if (free_area_current_end(context, context->free_areas[i]) < |
| 1384 |
free_area_min_end(context, context->free_areas[i])) |
| 1385 |
return false; |
| 1386 |
|
| 1387 |
/* A simple first-fit algorithm. We return true if we can fit the partitions in, otherwise false. */ |
| 1388 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 1389 |
bool fits = false; |
| 1390 |
uint64_t required; |
| 1391 |
FreeArea *a = NULL; |
| 1392 |
|
| 1393 |
if (p->dropped || PARTITION_IS_FOREIGN(p) || PARTITION_SUPPRESSED(p)) |
| 1394 |
continue; |
| 1395 |
|
| 1396 |
/* How much do we need to fit? */ |
| 1397 |
required = partition_min_size_with_padding(context, p); |
| 1398 |
|
| 1399 |
/* For existing partitions, we should verify that they'll actually fit */ |
| 1400 |
if (PARTITION_EXISTS(p)) { |
| 1401 |
if (p->current_size + p->current_padding < required) |
| 1402 |
return false; /* 😢 We won't be able to grow to the required min size! */ |
| 1403 |
|
| 1404 |
continue; |
| 1405 |
} |
| 1406 |
|
| 1407 |
/* For new partitions, see if there's a free area big enough */ |
| 1408 |
for (size_t i = 0; i < context->n_free_areas; i++) { |
| 1409 |
a = context->free_areas[i]; |
| 1410 |
|
| 1411 |
if (free_area_available_for_new_partitions(context, a) >= required) { |
| 1412 |
fits = true; |
| 1413 |
break; |
| 1414 |
} |
| 1415 |
} |
| 1416 |
|
| 1417 |
if (!fits) |
| 1418 |
return false; /* 😢 Oh no! We can't fit this partition into any free area! */ |
| 1419 |
|
| 1420 |
/* Assign the partition to this free area */ |
| 1421 |
p->allocated_to_area = a; |
| 1422 |
|
| 1423 |
/* Budget the minimal partition size */ |
| 1424 |
a->allocated += required; |
| 1425 |
} |
| 1426 |
|
| 1427 |
return true; |
| 1428 |
} |
| 1429 |
|
| 1430 |
static bool context_unmerge_and_allocate_partitions(Context *context) { |
| 1431 |
assert(context); |
| 1432 |
|
| 1433 |
/* This should only be called after plain context_allocate_partitions fails. This algorithm will |
| 1434 |
* try, in the order that minimizes the number of created supplement partitions, all combinations of |
| 1435 |
* un-suppressing supplement partitions until it finds one that works. */ |
| 1436 |
|
| 1437 |
/* First, let's try to un-suppress just one supplement partition and see if that gets us anywhere */ |
| 1438 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 1439 |
Partition *unsuppressed; |
| 1440 |
|
| 1441 |
if (!p->suppressing) |
| 1442 |
continue; |
| 1443 |
|
| 1444 |
unsuppressed = TAKE_PTR(p->suppressing); |
| 1445 |
|
| 1446 |
if (context_allocate_partitions(context, NULL)) |
| 1447 |
return true; |
| 1448 |
|
| 1449 |
p->suppressing = unsuppressed; |
| 1450 |
} |
| 1451 |
|
| 1452 |
/* Looks like not. So we have to un-suppress at least two partitions. We can do this recursively */ |
| 1453 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 1454 |
Partition *unsuppressed; |
| 1455 |
|
| 1456 |
if (!p->suppressing) |
| 1457 |
continue; |
| 1458 |
|
| 1459 |
unsuppressed = TAKE_PTR(p->suppressing); |
| 1460 |
|
| 1461 |
if (context_unmerge_and_allocate_partitions(context)) |
| 1462 |
return true; |
| 1463 |
|
| 1464 |
p->suppressing = unsuppressed; |
| 1465 |
} |
| 1466 |
|
| 1467 |
/* No combination of un-suppressed supplements made it possible to fit the partitions */ |
| 1468 |
return false; |
| 1469 |
} |
| 1470 |
|
| 1471 |
static uint32_t partition_weight(const Partition *p) { |
| 1472 |
assert(p); |
| 1473 |
return p->suppressing ? p->suppressing->weight : p->weight; |
| 1474 |
} |
| 1475 |
|
| 1476 |
static uint32_t partition_padding_weight(const Partition *p) { |
| 1477 |
assert(p); |
| 1478 |
return p->suppressing ? p->suppressing->padding_weight : p->padding_weight; |
| 1479 |
} |
| 1480 |
|
| 1481 |
static int context_sum_weights(const Context *context, const FreeArea *a, uint64_t *ret) { |
| 1482 |
uint64_t weight_sum = 0; |
| 1483 |
|
| 1484 |
assert(context); |
| 1485 |
assert(a); |
| 1486 |
assert(ret); |
| 1487 |
|
| 1488 |
/* Determine the sum of the weights of all partitions placed in or before the specified free area */ |
| 1489 |
|
| 1490 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 1491 |
if (p->padding_area != a && p->allocated_to_area != a) |
| 1492 |
continue; |
| 1493 |
|
| 1494 |
if (!INC_SAFE(&weight_sum, partition_weight(p))) |
| 1495 |
goto overflow_sum; |
| 1496 |
|
| 1497 |
if (!INC_SAFE(&weight_sum, partition_padding_weight(p))) |
| 1498 |
goto overflow_sum; |
| 1499 |
} |
| 1500 |
|
| 1501 |
*ret = weight_sum; |
| 1502 |
return 0; |
| 1503 |
|
| 1504 |
overflow_sum: |
| 1505 |
return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Combined weight of partition exceeds unsigned 64-bit range, refusing."); |
| 1506 |
} |
| 1507 |
|
| 1508 |
static uint64_t scale_by_weight(uint64_t value, uint64_t weight, uint64_t weight_sum) { |
| 1509 |
assert(weight_sum >= weight); |
| 1510 |
|
| 1511 |
for (;;) { |
| 1512 |
if (weight == 0) |
| 1513 |
return 0; |
| 1514 |
if (weight == weight_sum) |
| 1515 |
return value; |
| 1516 |
if (value <= UINT64_MAX / weight) |
| 1517 |
return value * weight / weight_sum; |
| 1518 |
|
| 1519 |
/* Rescale weight and weight_sum to make not the calculation overflow. To satisfy the |
| 1520 |
* following conditions, 'weight_sum' is rounded up but 'weight' is rounded down: |
| 1521 |
* - the sum of scale_by_weight() for all weights must not be larger than the input value, |
| 1522 |
* - scale_by_weight() must not be larger than the ideal value (i.e. calculated with uint128_t). */ |
| 1523 |
weight_sum = DIV_ROUND_UP(weight_sum, 2); |
| 1524 |
weight /= 2; |
| 1525 |
} |
| 1526 |
} |
| 1527 |
|
| 1528 |
typedef enum GrowPartitionPhase { |
| 1529 |
/* The zeroth phase: do not touch foreign partitions (i.e. those we don't manage). */ |
| 1530 |
PHASE_FOREIGN, |
| 1531 |
|
| 1532 |
/* The first phase: we charge partitions which need more (according to constraints) than their weight-based share. */ |
| 1533 |
PHASE_OVERCHARGE, |
| 1534 |
|
| 1535 |
/* The second phase: we charge partitions which need less (according to constraints) than their weight-based share. */ |
| 1536 |
PHASE_UNDERCHARGE, |
| 1537 |
|
| 1538 |
/* The third phase: we distribute what remains among the remaining partitions, according to the weights */ |
| 1539 |
PHASE_DISTRIBUTE, |
| 1540 |
|
| 1541 |
_GROW_PARTITION_PHASE_MAX, |
| 1542 |
} GrowPartitionPhase; |
| 1543 |
|
| 1544 |
static bool context_grow_partitions_phase( |
| 1545 |
const Context *context, |
| 1546 |
const FreeArea *a, |
| 1547 |
GrowPartitionPhase phase, |
| 1548 |
uint64_t *span, |
| 1549 |
uint64_t *weight_sum) { |
| 1550 |
|
| 1551 |
bool try_again = false; |
| 1552 |
|
| 1553 |
assert(context); |
| 1554 |
assert(a); |
| 1555 |
assert(span); |
| 1556 |
assert(weight_sum); |
| 1557 |
|
| 1558 |
/* Now let's look at the intended weights and adjust them taking the minimum space assignments into |
| 1559 |
* account. i.e. if a partition has a small weight but a high minimum space value set it should not |
| 1560 |
* get any additional room from the left-overs. Similar, if two partitions have the same weight they |
| 1561 |
* should get the same space if possible, even if one has a smaller minimum size than the other. */ |
| 1562 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 1563 |
/* Look only at partitions associated with this free area, i.e. immediately |
| 1564 |
* preceding it, or allocated into it */ |
| 1565 |
if (p->allocated_to_area != a && p->padding_area != a) |
| 1566 |
continue; |
| 1567 |
|
| 1568 |
if (p->new_size == UINT64_MAX) { |
| 1569 |
uint64_t share, rsz, xsz; |
| 1570 |
uint32_t weight; |
| 1571 |
bool charge = false; |
| 1572 |
|
| 1573 |
weight = partition_weight(p); |
| 1574 |
|
| 1575 |
/* Calculate how much this space this partition needs if everyone would get |
| 1576 |
* the weight based share */ |
| 1577 |
share = scale_by_weight(*span, weight, *weight_sum); |
| 1578 |
|
| 1579 |
rsz = partition_min_size(context, p); |
| 1580 |
xsz = partition_max_size(context, p); |
| 1581 |
|
| 1582 |
if (phase == PHASE_FOREIGN && PARTITION_IS_FOREIGN(p)) { |
| 1583 |
/* Never change of foreign partitions (i.e. those we don't manage) */ |
| 1584 |
|
| 1585 |
p->new_size = p->current_size; |
| 1586 |
charge = true; |
| 1587 |
|
| 1588 |
} else if (phase == PHASE_OVERCHARGE && rsz > share) { |
| 1589 |
/* This partition needs more than its calculated share. Let's assign |
| 1590 |
* it that, and take this partition out of all calculations and start |
| 1591 |
* again. */ |
| 1592 |
|
| 1593 |
p->new_size = rsz; |
| 1594 |
charge = try_again = true; |
| 1595 |
|
| 1596 |
} else if (phase == PHASE_UNDERCHARGE && xsz < share) { |
| 1597 |
/* This partition accepts less than its calculated |
| 1598 |
* share. Let's assign it that, and take this partition out |
| 1599 |
* of all calculations and start again. */ |
| 1600 |
|
| 1601 |
p->new_size = xsz; |
| 1602 |
charge = try_again = true; |
| 1603 |
|
| 1604 |
} else if (phase == PHASE_DISTRIBUTE) { |
| 1605 |
/* This partition can accept its calculated share. Let's |
| 1606 |
* assign it. There's no need to restart things here since |
| 1607 |
* assigning this shouldn't impact the shares of the other |
| 1608 |
* partitions. */ |
| 1609 |
|
| 1610 |
assert(share >= rsz); |
| 1611 |
p->new_size = CLAMP(round_down_size(share, context->grain_size), rsz, xsz); |
| 1612 |
charge = true; |
| 1613 |
} |
| 1614 |
|
| 1615 |
if (charge) { |
| 1616 |
*span = charge_size(context, *span, p->new_size); |
| 1617 |
*weight_sum = charge_weight(*weight_sum, weight); |
| 1618 |
} |
| 1619 |
} |
| 1620 |
|
| 1621 |
if (p->new_padding == UINT64_MAX) { |
| 1622 |
uint64_t share, rsz, xsz; |
| 1623 |
uint32_t padding_weight; |
| 1624 |
bool charge = false; |
| 1625 |
|
| 1626 |
padding_weight = partition_padding_weight(p); |
| 1627 |
|
| 1628 |
share = scale_by_weight(*span, padding_weight, *weight_sum); |
| 1629 |
|
| 1630 |
rsz = partition_min_padding(p); |
| 1631 |
xsz = partition_max_padding(p); |
| 1632 |
|
| 1633 |
if (phase == PHASE_OVERCHARGE && rsz > share) { |
| 1634 |
p->new_padding = rsz; |
| 1635 |
charge = try_again = true; |
| 1636 |
} else if (phase == PHASE_UNDERCHARGE && xsz < share) { |
| 1637 |
p->new_padding = xsz; |
| 1638 |
charge = try_again = true; |
| 1639 |
} else if (phase == PHASE_DISTRIBUTE) { |
| 1640 |
assert(share >= rsz); |
| 1641 |
p->new_padding = CLAMP(round_down_size(share, context->grain_size), rsz, xsz); |
| 1642 |
charge = true; |
| 1643 |
} |
| 1644 |
|
| 1645 |
if (charge) { |
| 1646 |
*span = charge_size(context, *span, p->new_padding); |
| 1647 |
*weight_sum = charge_weight(*weight_sum, padding_weight); |
| 1648 |
} |
| 1649 |
} |
| 1650 |
} |
| 1651 |
|
| 1652 |
return !try_again; |
| 1653 |
} |
| 1654 |
|
| 1655 |
static void context_grow_partition_one(Context *context, FreeArea *a, Partition *p, uint64_t *span) { |
| 1656 |
uint64_t m; |
| 1657 |
|
| 1658 |
assert(context); |
| 1659 |
assert(a); |
| 1660 |
assert(p); |
| 1661 |
assert(span); |
| 1662 |
|
| 1663 |
if (*span == 0) |
| 1664 |
return; |
| 1665 |
|
| 1666 |
if (p->allocated_to_area != a) |
| 1667 |
return; |
| 1668 |
|
| 1669 |
if (PARTITION_IS_FOREIGN(p)) |
| 1670 |
return; |
| 1671 |
|
| 1672 |
assert(p->new_size != UINT64_MAX); |
| 1673 |
|
| 1674 |
/* Calculate new size and align. */ |
| 1675 |
m = round_down_size(p->new_size + *span, context->grain_size); |
| 1676 |
/* But ensure this doesn't shrink the size. */ |
| 1677 |
m = MAX(m, p->new_size); |
| 1678 |
/* And ensure this doesn't exceed the maximum size. */ |
| 1679 |
m = MIN(m, partition_max_size(context, p)); |
| 1680 |
|
| 1681 |
assert(m >= p->new_size); |
| 1682 |
|
| 1683 |
*span = charge_size(context, *span, m - p->new_size); |
| 1684 |
p->new_size = m; |
| 1685 |
} |
| 1686 |
|
| 1687 |
static int context_grow_partitions_on_free_area(Context *context, FreeArea *a) { |
| 1688 |
uint64_t weight_sum = 0, span; |
| 1689 |
int r; |
| 1690 |
|
| 1691 |
assert(context); |
| 1692 |
assert(a); |
| 1693 |
|
| 1694 |
r = context_sum_weights(context, a, &weight_sum); |
| 1695 |
if (r < 0) |
| 1696 |
return r; |
| 1697 |
|
| 1698 |
/* Let's calculate the total area covered by this free area and the partition before it */ |
| 1699 |
span = a->size; |
| 1700 |
if (a->after) { |
| 1701 |
assert(a->after->offset != UINT64_MAX); |
| 1702 |
assert(a->after->current_size != UINT64_MAX); |
| 1703 |
|
| 1704 |
span += round_up_size(a->after->offset + a->after->current_size, context->grain_size) - a->after->offset; |
| 1705 |
} |
| 1706 |
|
| 1707 |
for (GrowPartitionPhase phase = 0; phase < _GROW_PARTITION_PHASE_MAX;) |
| 1708 |
if (context_grow_partitions_phase(context, a, phase, &span, &weight_sum)) |
| 1709 |
phase++; /* go to the next phase */ |
| 1710 |
|
| 1711 |
/* We still have space left over? Donate to preceding partition if we have one */ |
| 1712 |
if (span > 0 && a->after) |
| 1713 |
context_grow_partition_one(context, a, a->after, &span); |
| 1714 |
|
| 1715 |
/* What? Even still some space left (maybe because there was no preceding partition, or it had a |
| 1716 |
* size limit), then let's donate it to whoever wants it. */ |
| 1717 |
if (span > 0) |
| 1718 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 1719 |
context_grow_partition_one(context, a, p, &span); |
| 1720 |
if (span == 0) |
| 1721 |
break; |
| 1722 |
} |
| 1723 |
|
| 1724 |
/* Yuck, still no one? Then make it padding */ |
| 1725 |
if (span > 0 && a->after) { |
| 1726 |
assert(a->after->new_padding != UINT64_MAX); |
| 1727 |
a->after->new_padding += span; |
| 1728 |
} |
| 1729 |
|
| 1730 |
return 0; |
| 1731 |
} |
| 1732 |
|
| 1733 |
static int context_grow_partitions(Context *context) { |
| 1734 |
int r; |
| 1735 |
|
| 1736 |
assert(context); |
| 1737 |
|
| 1738 |
for (size_t i = 0; i < context->n_free_areas; i++) { |
| 1739 |
r = context_grow_partitions_on_free_area(context, context->free_areas[i]); |
| 1740 |
if (r < 0) |
| 1741 |
return r; |
| 1742 |
} |
| 1743 |
|
| 1744 |
/* All existing partitions that have no free space after them can't change size */ |
| 1745 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 1746 |
if (p->dropped) |
| 1747 |
continue; |
| 1748 |
|
| 1749 |
if (!PARTITION_EXISTS(p) || p->padding_area) { |
| 1750 |
/* The algorithm above must have initialized this already */ |
| 1751 |
assert(p->new_size != UINT64_MAX); |
| 1752 |
continue; |
| 1753 |
} |
| 1754 |
|
| 1755 |
assert(p->new_size == UINT64_MAX); |
| 1756 |
p->new_size = p->current_size; |
| 1757 |
|
| 1758 |
assert(p->new_padding == UINT64_MAX); |
| 1759 |
p->new_padding = p->current_padding; |
| 1760 |
} |
| 1761 |
|
| 1762 |
return 0; |
| 1763 |
} |
| 1764 |
|
| 1765 |
static uint64_t find_first_unused_partno(Context *context) { |
| 1766 |
uint64_t partno = 0; |
| 1767 |
|
| 1768 |
assert(context); |
| 1769 |
|
| 1770 |
for (partno = 0;; partno++) { |
| 1771 |
bool found = false; |
| 1772 |
LIST_FOREACH(partitions, p, context->partitions) |
| 1773 |
if (p->partno != UINT64_MAX && p->partno == partno) |
| 1774 |
found = true; |
| 1775 |
if (!found) |
| 1776 |
break; |
| 1777 |
} |
| 1778 |
|
| 1779 |
return partno; |
| 1780 |
} |
| 1781 |
|
| 1782 |
static void context_place_partitions(Context *context) { |
| 1783 |
|
| 1784 |
assert(context); |
| 1785 |
|
| 1786 |
for (size_t i = 0; i < context->n_free_areas; i++) { |
| 1787 |
FreeArea *a = context->free_areas[i]; |
| 1788 |
uint64_t left; |
| 1789 |
uint64_t start; |
| 1790 |
|
| 1791 |
if (a->after) { |
| 1792 |
assert(a->after->offset != UINT64_MAX); |
| 1793 |
assert(a->after->new_size != UINT64_MAX); |
| 1794 |
assert(a->after->new_padding != UINT64_MAX); |
| 1795 |
|
| 1796 |
start = a->after->offset + a->after->new_size + a->after->new_padding; |
| 1797 |
} else |
| 1798 |
start = context->start; |
| 1799 |
|
| 1800 |
start = round_up_size(start, context->grain_size); |
| 1801 |
left = a->size; |
| 1802 |
|
| 1803 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 1804 |
uint64_t gap; |
| 1805 |
|
| 1806 |
if (p->allocated_to_area != a) |
| 1807 |
continue; |
| 1808 |
|
| 1809 |
p->offset = start; |
| 1810 |
p->partno = find_first_unused_partno(context); |
| 1811 |
|
| 1812 |
assert(left >= p->new_size); |
| 1813 |
start += p->new_size; |
| 1814 |
left -= p->new_size; |
| 1815 |
|
| 1816 |
assert(left >= p->new_padding); |
| 1817 |
start += p->new_padding; |
| 1818 |
left -= p->new_padding; |
| 1819 |
|
| 1820 |
/* Re-align start to the grain after each partition, so that the next |
| 1821 |
* partition placed into this free area also starts on a grain boundary. |
| 1822 |
* This matters when the grain is larger than the default (e.g. 1 MiB via |
| 1823 |
* --grain-size=) and a small partition like verity-sig (16 KiB) precedes |
| 1824 |
* a larger one: without this, the successor would start at an unaligned |
| 1825 |
* offset. */ |
| 1826 |
gap = round_up_size(start, context->grain_size) - start; |
| 1827 |
if (gap > left) { |
| 1828 |
log_warning("Not enough space left in free area to re-align partition start to grain size, " |
| 1829 |
"next partition may start at an unaligned offset."); |
| 1830 |
gap = 0; |
| 1831 |
} |
| 1832 |
start += gap; |
| 1833 |
left -= gap; |
| 1834 |
} |
| 1835 |
} |
| 1836 |
} |
| 1837 |
|
| 1838 |
static int config_parse_type( |
| 1839 |
const char *unit, |
| 1840 |
const char *filename, |
| 1841 |
unsigned line, |
| 1842 |
const char *section, |
| 1843 |
unsigned section_line, |
| 1844 |
const char *lvalue, |
| 1845 |
int ltype, |
| 1846 |
const char *rvalue, |
| 1847 |
void *data, |
| 1848 |
void *userdata) { |
| 1849 |
|
| 1850 |
GptPartitionType *type = ASSERT_PTR(data); |
| 1851 |
int r; |
| 1852 |
|
| 1853 |
assert(rvalue); |
| 1854 |
|
| 1855 |
r = gpt_partition_type_from_string(rvalue, type); |
| 1856 |
if (r < 0) |
| 1857 |
return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse partition type: %s", rvalue); |
| 1858 |
|
| 1859 |
if (arg_architecture >= 0) |
| 1860 |
*type = gpt_partition_type_override_architecture(*type, arg_architecture); |
| 1861 |
|
| 1862 |
return 0; |
| 1863 |
} |
| 1864 |
|
| 1865 |
static int config_parse_label( |
| 1866 |
const char *unit, |
| 1867 |
const char *filename, |
| 1868 |
unsigned line, |
| 1869 |
const char *section, |
| 1870 |
unsigned section_line, |
| 1871 |
const char *lvalue, |
| 1872 |
int ltype, |
| 1873 |
const char *rvalue, |
| 1874 |
void *data, |
| 1875 |
void *userdata) { |
| 1876 |
|
| 1877 |
_cleanup_free_ char *resolved = NULL; |
| 1878 |
char **label = ASSERT_PTR(data); |
| 1879 |
int r; |
| 1880 |
|
| 1881 |
assert(rvalue); |
| 1882 |
|
| 1883 |
/* Nota bene: the empty label is a totally valid one. Let's hence not follow our usual rule of |
| 1884 |
* assigning the empty string to reset to default here, but really accept it as label to set. */ |
| 1885 |
|
| 1886 |
r = specifier_printf(rvalue, GPT_LABEL_MAX, system_and_tmp_specifier_table, arg_root, NULL, &resolved); |
| 1887 |
if (r < 0) { |
| 1888 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1889 |
"Failed to expand specifiers in Label=, ignoring: %s", rvalue); |
| 1890 |
return 0; |
| 1891 |
} |
| 1892 |
|
| 1893 |
if (!utf8_is_valid(resolved)) { |
| 1894 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1895 |
"Partition label not valid UTF-8, ignoring: %s", rvalue); |
| 1896 |
return 0; |
| 1897 |
} |
| 1898 |
|
| 1899 |
r = gpt_partition_label_valid(resolved); |
| 1900 |
if (r < 0) { |
| 1901 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1902 |
"Failed to check if string is valid as GPT partition label, ignoring: \"%s\" (from \"%s\")", |
| 1903 |
resolved, rvalue); |
| 1904 |
return 0; |
| 1905 |
} |
| 1906 |
if (!r) { |
| 1907 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1908 |
"Partition label too long for GPT table, ignoring: \"%s\" (from \"%s\")", |
| 1909 |
resolved, rvalue); |
| 1910 |
return 0; |
| 1911 |
} |
| 1912 |
|
| 1913 |
free_and_replace(*label, resolved); |
| 1914 |
return 0; |
| 1915 |
} |
| 1916 |
|
| 1917 |
static int config_parse_weight( |
| 1918 |
const char *unit, |
| 1919 |
const char *filename, |
| 1920 |
unsigned line, |
| 1921 |
const char *section, |
| 1922 |
unsigned section_line, |
| 1923 |
const char *lvalue, |
| 1924 |
int ltype, |
| 1925 |
const char *rvalue, |
| 1926 |
void *data, |
| 1927 |
void *userdata) { |
| 1928 |
|
| 1929 |
uint32_t *w = ASSERT_PTR(data), v; |
| 1930 |
int r; |
| 1931 |
|
| 1932 |
assert(rvalue); |
| 1933 |
|
| 1934 |
r = safe_atou32(rvalue, &v); |
| 1935 |
if (r < 0) { |
| 1936 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1937 |
"Failed to parse weight value, ignoring: %s", rvalue); |
| 1938 |
return 0; |
| 1939 |
} |
| 1940 |
|
| 1941 |
if (v > 1000U*1000U) { |
| 1942 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1943 |
"Weight needs to be in range 0…10000000, ignoring: %" PRIu32, v); |
| 1944 |
return 0; |
| 1945 |
} |
| 1946 |
|
| 1947 |
*w = v; |
| 1948 |
return 0; |
| 1949 |
} |
| 1950 |
|
| 1951 |
static int config_parse_size4096( |
| 1952 |
const char *unit, |
| 1953 |
const char *filename, |
| 1954 |
unsigned line, |
| 1955 |
const char *section, |
| 1956 |
unsigned section_line, |
| 1957 |
const char *lvalue, |
| 1958 |
int ltype, |
| 1959 |
const char *rvalue, |
| 1960 |
void *data, |
| 1961 |
void *userdata) { |
| 1962 |
|
| 1963 |
uint64_t *sz = data, parsed; |
| 1964 |
int r; |
| 1965 |
|
| 1966 |
assert(rvalue); |
| 1967 |
assert(data); |
| 1968 |
|
| 1969 |
r = parse_size(rvalue, 1024, &parsed); |
| 1970 |
if (r < 0) |
| 1971 |
return log_syntax(unit, LOG_ERR, filename, line, r, |
| 1972 |
"Failed to parse size value: %s", rvalue); |
| 1973 |
|
| 1974 |
if (ltype > 0) |
| 1975 |
*sz = round_up_size(parsed, 4096); |
| 1976 |
else if (ltype < 0) |
| 1977 |
*sz = round_down_size(parsed, 4096); |
| 1978 |
else |
| 1979 |
*sz = parsed; |
| 1980 |
|
| 1981 |
if (*sz != parsed) |
| 1982 |
log_syntax(unit, LOG_NOTICE, filename, line, r, "Rounded %s= size %" PRIu64 " %s %" PRIu64 ", a multiple of 4096.", |
| 1983 |
lvalue, parsed, glyph(GLYPH_ARROW_RIGHT), *sz); |
| 1984 |
|
| 1985 |
return 0; |
| 1986 |
} |
| 1987 |
|
| 1988 |
static int config_parse_block_size( |
| 1989 |
const char *unit, |
| 1990 |
const char *filename, |
| 1991 |
unsigned line, |
| 1992 |
const char *section, |
| 1993 |
unsigned section_line, |
| 1994 |
const char *lvalue, |
| 1995 |
int ltype, |
| 1996 |
const char *rvalue, |
| 1997 |
void *data, |
| 1998 |
void *userdata) { |
| 1999 |
|
| 2000 |
uint64_t *blksz = ASSERT_PTR(data), parsed; |
| 2001 |
int r; |
| 2002 |
|
| 2003 |
assert(rvalue); |
| 2004 |
|
| 2005 |
r = parse_size(rvalue, 1024, &parsed); |
| 2006 |
if (r < 0) |
| 2007 |
return log_syntax(unit, LOG_ERR, filename, line, r, |
| 2008 |
"Failed to parse size value: %s", rvalue); |
| 2009 |
|
| 2010 |
if (parsed < 512 || parsed > 4096) |
| 2011 |
return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL), |
| 2012 |
"Value not between 512 and 4096: %s", rvalue); |
| 2013 |
|
| 2014 |
if (!ISPOWEROF2(parsed)) |
| 2015 |
return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL), |
| 2016 |
"Value not a power of 2: %s", rvalue); |
| 2017 |
|
| 2018 |
*blksz = parsed; |
| 2019 |
return 0; |
| 2020 |
} |
| 2021 |
|
| 2022 |
static int config_parse_fs_sector_size( |
| 2023 |
const char *unit, |
| 2024 |
const char *filename, |
| 2025 |
unsigned line, |
| 2026 |
const char *section, |
| 2027 |
unsigned section_line, |
| 2028 |
const char *lvalue, |
| 2029 |
int ltype, |
| 2030 |
const char *rvalue, |
| 2031 |
void *data, |
| 2032 |
void *userdata) { |
| 2033 |
|
| 2034 |
uint64_t *fssecsz = ASSERT_PTR(data), parsed; |
| 2035 |
int r; |
| 2036 |
|
| 2037 |
assert(rvalue); |
| 2038 |
|
| 2039 |
if (isempty(rvalue)) { |
| 2040 |
*fssecsz = UINT64_MAX; |
| 2041 |
return 0; |
| 2042 |
} |
| 2043 |
|
| 2044 |
r = parse_size(rvalue, 1024, &parsed); |
| 2045 |
if (r < 0) |
| 2046 |
return log_syntax(unit, LOG_ERR, filename, line, r, |
| 2047 |
"Failed to parse size value: %s", rvalue); |
| 2048 |
|
| 2049 |
if (!ISPOWEROF2(parsed)) |
| 2050 |
return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL), |
| 2051 |
"Value not a power of 2: %s", rvalue); |
| 2052 |
|
| 2053 |
/* NB: we make no upper restriction here, since the maximum logical sector sizes file systems support |
| 2054 |
* vary greatly, and can be much larger than 4K. (That's also the reason we don't use |
| 2055 |
* parse_sector_size() here.) */ |
| 2056 |
|
| 2057 |
*fssecsz = parsed; |
| 2058 |
return 0; |
| 2059 |
} |
| 2060 |
|
| 2061 |
static int config_parse_fstype( |
| 2062 |
const char *unit, |
| 2063 |
const char *filename, |
| 2064 |
unsigned line, |
| 2065 |
const char *section, |
| 2066 |
unsigned section_line, |
| 2067 |
const char *lvalue, |
| 2068 |
int ltype, |
| 2069 |
const char *rvalue, |
| 2070 |
void *data, |
| 2071 |
void *userdata) { |
| 2072 |
|
| 2073 |
char **fstype = ASSERT_PTR(data); |
| 2074 |
const char *e; |
| 2075 |
|
| 2076 |
assert(rvalue); |
| 2077 |
|
| 2078 |
/* Let's provide an easy way to override the chosen fstype for file system partitions */ |
| 2079 |
e = secure_getenv("SYSTEMD_REPART_OVERRIDE_FSTYPE"); |
| 2080 |
if (e && !streq(rvalue, e)) { |
| 2081 |
log_syntax(unit, LOG_NOTICE, filename, line, 0, |
| 2082 |
"Overriding defined file system type '%s' with '%s'.", rvalue, e); |
| 2083 |
rvalue = e; |
| 2084 |
} |
| 2085 |
|
| 2086 |
if (!filename_is_valid(rvalue)) |
| 2087 |
return log_syntax(unit, LOG_ERR, filename, line, 0, |
| 2088 |
"File system type is not valid, refusing: %s", rvalue); |
| 2089 |
|
| 2090 |
return free_and_strdup_warn(fstype, rvalue); |
| 2091 |
} |
| 2092 |
|
| 2093 |
static int config_parse_copy_files( |
| 2094 |
const char *unit, |
| 2095 |
const char *filename, |
| 2096 |
unsigned line, |
| 2097 |
const char *section, |
| 2098 |
unsigned section_line, |
| 2099 |
const char *lvalue, |
| 2100 |
int ltype, |
| 2101 |
const char *rvalue, |
| 2102 |
void *data, |
| 2103 |
void *userdata) { |
| 2104 |
|
| 2105 |
_cleanup_free_ char *source = NULL, *buffer = NULL, *resolved_source = NULL, *resolved_target = NULL, *options = NULL; |
| 2106 |
Partition *partition = ASSERT_PTR(data); |
| 2107 |
const char *p = rvalue, *target; |
| 2108 |
int r; |
| 2109 |
|
| 2110 |
assert(rvalue); |
| 2111 |
|
| 2112 |
r = extract_first_word(&p, &source, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS); |
| 2113 |
if (r < 0) |
| 2114 |
return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract source path: %s", rvalue); |
| 2115 |
if (r == 0) { |
| 2116 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "No argument specified: %s", rvalue); |
| 2117 |
return 0; |
| 2118 |
} |
| 2119 |
|
| 2120 |
r = extract_first_word(&p, &buffer, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS); |
| 2121 |
if (r < 0) |
| 2122 |
return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract target path: %s", rvalue); |
| 2123 |
if (r == 0) |
| 2124 |
target = source; /* No target, then it's the same as the source */ |
| 2125 |
else |
| 2126 |
target = buffer; |
| 2127 |
|
| 2128 |
r = extract_first_word(&p, &options, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS); |
| 2129 |
if (r < 0) |
| 2130 |
return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract options: %s", rvalue); |
| 2131 |
|
| 2132 |
if (!isempty(p)) |
| 2133 |
return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL), "Too many arguments: %s", rvalue); |
| 2134 |
|
| 2135 |
CopyFlags flags = COPY_REFLINK|COPY_HOLES|COPY_MERGE|COPY_REPLACE|COPY_SIGINT|COPY_HARDLINKS|COPY_ALL_XATTRS|COPY_GRACEFUL_WARN|COPY_TRUNCATE|COPY_RESTORE_DIRECTORY_TIMESTAMPS; |
| 2136 |
for (const char *opts = options;;) { |
| 2137 |
_cleanup_free_ char *word = NULL; |
| 2138 |
const char *val; |
| 2139 |
|
| 2140 |
r = extract_first_word(&opts, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS | EXTRACT_UNESCAPE_SEPARATORS); |
| 2141 |
if (r < 0) |
| 2142 |
return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CopyFile options: %s", options); |
| 2143 |
if (r == 0) |
| 2144 |
break; |
| 2145 |
|
| 2146 |
if (isempty(word)) |
| 2147 |
continue; |
| 2148 |
|
| 2149 |
if ((val = startswith(word, "fsverity="))) { |
| 2150 |
if (streq(val, "copy")) |
| 2151 |
flags |= COPY_PRESERVE_FS_VERITY; |
| 2152 |
else if (streq(val, "off")) |
| 2153 |
flags &= ~COPY_PRESERVE_FS_VERITY; |
| 2154 |
else |
| 2155 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "fsverity= expects either 'off' or 'copy'."); |
| 2156 |
} else |
| 2157 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "Encountered unknown option '%s', ignoring.", word); |
| 2158 |
} |
| 2159 |
|
| 2160 |
r = specifier_printf(source, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_source); |
| 2161 |
if (r < 0) { |
| 2162 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 2163 |
"Failed to expand specifiers in CopyFiles= source, ignoring: %s", rvalue); |
| 2164 |
return 0; |
| 2165 |
} |
| 2166 |
|
| 2167 |
r = path_simplify_and_warn(resolved_source, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
| 2168 |
if (r < 0) |
| 2169 |
return 0; |
| 2170 |
|
| 2171 |
r = specifier_printf(target, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_target); |
| 2172 |
if (r < 0) { |
| 2173 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 2174 |
"Failed to expand specifiers in CopyFiles= target, ignoring: %s", resolved_target); |
| 2175 |
return 0; |
| 2176 |
} |
| 2177 |
|
| 2178 |
r = path_simplify_and_warn(resolved_target, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
| 2179 |
if (r < 0) |
| 2180 |
return 0; |
| 2181 |
|
| 2182 |
if (!GREEDY_REALLOC(partition->copy_files, partition->n_copy_files + 1)) |
| 2183 |
return log_oom(); |
| 2184 |
|
| 2185 |
partition->copy_files[partition->n_copy_files++] = (CopyFiles) { |
| 2186 |
.source = TAKE_PTR(resolved_source), |
| 2187 |
.target = TAKE_PTR(resolved_target), |
| 2188 |
.flags = flags, |
| 2189 |
}; |
| 2190 |
|
| 2191 |
return 0; |
| 2192 |
} |
| 2193 |
|
| 2194 |
static int config_parse_exclude_files( |
| 2195 |
const char *unit, |
| 2196 |
const char *filename, |
| 2197 |
unsigned line, |
| 2198 |
const char *section, |
| 2199 |
unsigned section_line, |
| 2200 |
const char *lvalue, |
| 2201 |
int ltype, |
| 2202 |
const char *rvalue, |
| 2203 |
void *data, |
| 2204 |
void *userdata) { |
| 2205 |
|
| 2206 |
char ***exclude_files = ASSERT_PTR(data); |
| 2207 |
const char *p = ASSERT_PTR(rvalue); |
| 2208 |
int r; |
| 2209 |
|
| 2210 |
if (isempty(rvalue)) { |
| 2211 |
*exclude_files = strv_free(*exclude_files); |
| 2212 |
return 0; |
| 2213 |
} |
| 2214 |
|
| 2215 |
for (;;) { |
| 2216 |
_cleanup_free_ char *word = NULL, *resolved = NULL; |
| 2217 |
|
| 2218 |
r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE); |
| 2219 |
if (r == -ENOMEM) |
| 2220 |
return log_oom(); |
| 2221 |
if (r < 0) { |
| 2222 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", p); |
| 2223 |
return 0; |
| 2224 |
} |
| 2225 |
if (r == 0) |
| 2226 |
return 0; |
| 2227 |
|
| 2228 |
r = specifier_printf(word, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved); |
| 2229 |
if (r < 0) { |
| 2230 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 2231 |
"Failed to expand specifiers in %s path, ignoring: %s", lvalue, word); |
| 2232 |
return 0; |
| 2233 |
} |
| 2234 |
|
| 2235 |
r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE|PATH_KEEP_TRAILING_SLASH, unit, filename, line, lvalue); |
| 2236 |
if (r < 0) |
| 2237 |
return 0; |
| 2238 |
|
| 2239 |
if (strv_consume(exclude_files, TAKE_PTR(resolved)) < 0) |
| 2240 |
return log_oom(); |
| 2241 |
} |
| 2242 |
|
| 2243 |
return 0; |
| 2244 |
} |
| 2245 |
|
| 2246 |
static int config_parse_copy_blocks( |
| 2247 |
const char *unit, |
| 2248 |
const char *filename, |
| 2249 |
unsigned line, |
| 2250 |
const char *section, |
| 2251 |
unsigned section_line, |
| 2252 |
const char *lvalue, |
| 2253 |
int ltype, |
| 2254 |
const char *rvalue, |
| 2255 |
void *data, |
| 2256 |
void *userdata) { |
| 2257 |
|
| 2258 |
_cleanup_free_ char *d = NULL; |
| 2259 |
Partition *partition = ASSERT_PTR(data); |
| 2260 |
int r; |
| 2261 |
|
| 2262 |
assert(rvalue); |
| 2263 |
|
| 2264 |
if (isempty(rvalue)) { |
| 2265 |
partition->copy_blocks_path = mfree(partition->copy_blocks_path); |
| 2266 |
partition->copy_blocks_auto = false; |
| 2267 |
return 0; |
| 2268 |
} |
| 2269 |
|
| 2270 |
if (streq(rvalue, "auto")) { |
| 2271 |
partition->copy_blocks_path = mfree(partition->copy_blocks_path); |
| 2272 |
partition->copy_blocks_auto = true; |
| 2273 |
partition->copy_blocks_root = arg_root; |
| 2274 |
return 0; |
| 2275 |
} |
| 2276 |
|
| 2277 |
r = specifier_printf(rvalue, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &d); |
| 2278 |
if (r < 0) { |
| 2279 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 2280 |
"Failed to expand specifiers in CopyBlocks= source path, ignoring: %s", rvalue); |
| 2281 |
return 0; |
| 2282 |
} |
| 2283 |
|
| 2284 |
r = path_simplify_and_warn(d, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
| 2285 |
if (r < 0) |
| 2286 |
return 0; |
| 2287 |
|
| 2288 |
free_and_replace(partition->copy_blocks_path, d); |
| 2289 |
partition->copy_blocks_auto = false; |
| 2290 |
partition->copy_blocks_root = arg_root; |
| 2291 |
return 0; |
| 2292 |
} |
| 2293 |
|
| 2294 |
static int config_parse_make_dirs( |
| 2295 |
const char *unit, |
| 2296 |
const char *filename, |
| 2297 |
unsigned line, |
| 2298 |
const char *section, |
| 2299 |
unsigned section_line, |
| 2300 |
const char *lvalue, |
| 2301 |
int ltype, |
| 2302 |
const char *rvalue, |
| 2303 |
void *data, |
| 2304 |
void *userdata) { |
| 2305 |
|
| 2306 |
char ***sv = ASSERT_PTR(data); |
| 2307 |
const char *p = ASSERT_PTR(rvalue); |
| 2308 |
int r; |
| 2309 |
|
| 2310 |
for (;;) { |
| 2311 |
_cleanup_free_ char *word = NULL, *d = NULL; |
| 2312 |
|
| 2313 |
r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE); |
| 2314 |
if (r == -ENOMEM) |
| 2315 |
return log_oom(); |
| 2316 |
if (r < 0) { |
| 2317 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue); |
| 2318 |
return 0; |
| 2319 |
} |
| 2320 |
if (r == 0) |
| 2321 |
return 0; |
| 2322 |
|
| 2323 |
r = specifier_printf(word, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &d); |
| 2324 |
if (r < 0) { |
| 2325 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 2326 |
"Failed to expand specifiers in MakeDirectories= parameter, ignoring: %s", word); |
| 2327 |
continue; |
| 2328 |
} |
| 2329 |
|
| 2330 |
r = path_simplify_and_warn(d, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
| 2331 |
if (r < 0) |
| 2332 |
continue; |
| 2333 |
|
| 2334 |
if (strv_contains(*sv, d)) |
| 2335 |
continue; |
| 2336 |
|
| 2337 |
r = strv_consume(sv, TAKE_PTR(d)); |
| 2338 |
if (r < 0) |
| 2339 |
return log_oom(); |
| 2340 |
} |
| 2341 |
} |
| 2342 |
|
| 2343 |
static int config_parse_make_symlinks( |
| 2344 |
const char *unit, |
| 2345 |
const char *filename, |
| 2346 |
unsigned line, |
| 2347 |
const char *section, |
| 2348 |
unsigned section_line, |
| 2349 |
const char *lvalue, |
| 2350 |
int ltype, |
| 2351 |
const char *rvalue, |
| 2352 |
void *data, |
| 2353 |
void *userdata) { |
| 2354 |
|
| 2355 |
char ***sv = ASSERT_PTR(data); |
| 2356 |
const char *p = ASSERT_PTR(rvalue); |
| 2357 |
int r; |
| 2358 |
|
| 2359 |
for (;;) { |
| 2360 |
_cleanup_free_ char *word = NULL, *path = NULL, *target = NULL, *d = NULL; |
| 2361 |
|
| 2362 |
r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE); |
| 2363 |
if (r == -ENOMEM) |
| 2364 |
return log_oom(); |
| 2365 |
if (r < 0) { |
| 2366 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue); |
| 2367 |
return 0; |
| 2368 |
} |
| 2369 |
if (r == 0) |
| 2370 |
return 0; |
| 2371 |
|
| 2372 |
const char *q = word; |
| 2373 |
r = extract_many_words(&q, ":", EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS, &path, &target); |
| 2374 |
if (r < 0) { |
| 2375 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", q); |
| 2376 |
continue; |
| 2377 |
} |
| 2378 |
if (r != 2) { |
| 2379 |
log_syntax(unit, LOG_WARNING, filename, line, SYNTHETIC_ERRNO(EINVAL), |
| 2380 |
"Missing source or target in %s, ignoring", rvalue); |
| 2381 |
continue; |
| 2382 |
} |
| 2383 |
|
| 2384 |
r = specifier_printf(path, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, /* userdata= */ NULL, &d); |
| 2385 |
if (r < 0) { |
| 2386 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 2387 |
"Failed to expand specifiers in Subvolumes= parameter, ignoring: %s", path); |
| 2388 |
continue; |
| 2389 |
} |
| 2390 |
|
| 2391 |
r = path_simplify_and_warn(d, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
| 2392 |
if (r < 0) |
| 2393 |
continue; |
| 2394 |
|
| 2395 |
r = strv_consume_pair(sv, TAKE_PTR(d), TAKE_PTR(target)); |
| 2396 |
if (r < 0) |
| 2397 |
return log_error_errno(r, "Failed to add symlink to list: %m"); |
| 2398 |
} |
| 2399 |
} |
| 2400 |
|
| 2401 |
static int config_parse_subvolumes( |
| 2402 |
const char *unit, |
| 2403 |
const char *filename, |
| 2404 |
unsigned line, |
| 2405 |
const char *section, |
| 2406 |
unsigned section_line, |
| 2407 |
const char *lvalue, |
| 2408 |
int ltype, |
| 2409 |
const char *rvalue, |
| 2410 |
void *data, |
| 2411 |
void *userdata) { |
| 2412 |
|
| 2413 |
OrderedHashmap **subvolumes = ASSERT_PTR(data); |
| 2414 |
const char *p = ASSERT_PTR(rvalue); |
| 2415 |
int r; |
| 2416 |
|
| 2417 |
for (;;) { |
| 2418 |
_cleanup_free_ char *word = NULL, *path = NULL, *f = NULL, *d = NULL; |
| 2419 |
Subvolume *s = NULL; |
| 2420 |
|
| 2421 |
r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE); |
| 2422 |
if (r == -ENOMEM) |
| 2423 |
return log_oom(); |
| 2424 |
if (r < 0) { |
| 2425 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue); |
| 2426 |
return 0; |
| 2427 |
} |
| 2428 |
if (r == 0) |
| 2429 |
return 0; |
| 2430 |
|
| 2431 |
const char *q = word; |
| 2432 |
r = extract_many_words(&q, ":", EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS, &path, &f); |
| 2433 |
if (r < 0) { |
| 2434 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", q); |
| 2435 |
continue; |
| 2436 |
} |
| 2437 |
|
| 2438 |
r = specifier_printf(path, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, /* userdata= */ NULL, &d); |
| 2439 |
if (r < 0) { |
| 2440 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 2441 |
"Failed to expand specifiers in Subvolumes= parameter, ignoring: %s", path); |
| 2442 |
continue; |
| 2443 |
} |
| 2444 |
|
| 2445 |
r = path_simplify_and_warn(d, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
| 2446 |
if (r < 0) |
| 2447 |
continue; |
| 2448 |
|
| 2449 |
s = ordered_hashmap_get(*subvolumes, d); |
| 2450 |
if (!s) { |
| 2451 |
s = new(Subvolume, 1); |
| 2452 |
if (!s) |
| 2453 |
return log_oom(); |
| 2454 |
|
| 2455 |
*s = (Subvolume) { |
| 2456 |
.path = TAKE_PTR(d), |
| 2457 |
}; |
| 2458 |
|
| 2459 |
r = ordered_hashmap_ensure_put(subvolumes, &subvolume_hash_ops, s->path, s); |
| 2460 |
if (r < 0) { |
| 2461 |
subvolume_free(s); |
| 2462 |
return r; |
| 2463 |
} |
| 2464 |
} |
| 2465 |
|
| 2466 |
if (f) { |
| 2467 |
BtrfsSubvolFlags flags = subvolume_flags_from_string(f); |
| 2468 |
if (flags == -EBADRQC) { |
| 2469 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Unknown subvolume flag in subvolume, ignoring: %s", f); |
| 2470 |
continue; |
| 2471 |
} |
| 2472 |
if (flags < 0) { |
| 2473 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse subvolume flags, ignoring: %s", f); |
| 2474 |
continue; |
| 2475 |
} |
| 2476 |
|
| 2477 |
s->flags = flags; |
| 2478 |
} |
| 2479 |
} |
| 2480 |
} |
| 2481 |
|
| 2482 |
static int config_parse_default_subvolume( |
| 2483 |
const char *unit, |
| 2484 |
const char *filename, |
| 2485 |
unsigned line, |
| 2486 |
const char *section, |
| 2487 |
unsigned section_line, |
| 2488 |
const char *lvalue, |
| 2489 |
int ltype, |
| 2490 |
const char *rvalue, |
| 2491 |
void *data, |
| 2492 |
void *userdata) { |
| 2493 |
|
| 2494 |
char **subvol = ASSERT_PTR(data); |
| 2495 |
_cleanup_free_ char *p = NULL; |
| 2496 |
int r; |
| 2497 |
|
| 2498 |
if (isempty(rvalue)) { |
| 2499 |
*subvol = mfree(*subvol); |
| 2500 |
return 0; |
| 2501 |
} |
| 2502 |
|
| 2503 |
r = specifier_printf(rvalue, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &p); |
| 2504 |
if (r < 0) { |
| 2505 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 2506 |
"Failed to expand specifiers in DefaultSubvolume= parameter, ignoring: %s", rvalue); |
| 2507 |
return 0; |
| 2508 |
} |
| 2509 |
|
| 2510 |
r = path_simplify_and_warn(p, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
| 2511 |
if (r < 0) |
| 2512 |
return 0; |
| 2513 |
|
| 2514 |
return free_and_replace(*subvol, p); |
| 2515 |
} |
| 2516 |
|
| 2517 |
static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_encrypt, encrypt_mode, EncryptMode, ENCRYPT_OFF); |
| 2518 |
|
| 2519 |
static int config_parse_gpt_flags( |
| 2520 |
const char *unit, |
| 2521 |
const char *filename, |
| 2522 |
unsigned line, |
| 2523 |
const char *section, |
| 2524 |
unsigned section_line, |
| 2525 |
const char *lvalue, |
| 2526 |
int ltype, |
| 2527 |
const char *rvalue, |
| 2528 |
void *data, |
| 2529 |
void *userdata) { |
| 2530 |
|
| 2531 |
uint64_t *gpt_flags = ASSERT_PTR(data); |
| 2532 |
int r; |
| 2533 |
|
| 2534 |
assert(rvalue); |
| 2535 |
|
| 2536 |
r = safe_atou64(rvalue, gpt_flags); |
| 2537 |
if (r < 0) { |
| 2538 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 2539 |
"Failed to parse Flags= value, ignoring: %s", rvalue); |
| 2540 |
return 0; |
| 2541 |
} |
| 2542 |
|
| 2543 |
return 0; |
| 2544 |
} |
| 2545 |
|
| 2546 |
static int config_parse_uuid( |
| 2547 |
const char *unit, |
| 2548 |
const char *filename, |
| 2549 |
unsigned line, |
| 2550 |
const char *section, |
| 2551 |
unsigned section_line, |
| 2552 |
const char *lvalue, |
| 2553 |
int ltype, |
| 2554 |
const char *rvalue, |
| 2555 |
void *data, |
| 2556 |
void *userdata) { |
| 2557 |
|
| 2558 |
Partition *partition = ASSERT_PTR(data); |
| 2559 |
int r; |
| 2560 |
|
| 2561 |
if (isempty(rvalue)) { |
| 2562 |
partition->new_uuid = SD_ID128_NULL; |
| 2563 |
partition->new_uuid_is_set = false; |
| 2564 |
return 0; |
| 2565 |
} |
| 2566 |
|
| 2567 |
if (streq(rvalue, "null")) { |
| 2568 |
partition->new_uuid = SD_ID128_NULL; |
| 2569 |
partition->new_uuid_is_set = true; |
| 2570 |
return 0; |
| 2571 |
} |
| 2572 |
|
| 2573 |
r = sd_id128_from_string(rvalue, &partition->new_uuid); |
| 2574 |
if (r < 0) { |
| 2575 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse 128-bit ID/UUID, ignoring: %s", rvalue); |
| 2576 |
return 0; |
| 2577 |
} |
| 2578 |
|
| 2579 |
partition->new_uuid_is_set = true; |
| 2580 |
|
| 2581 |
return 0; |
| 2582 |
} |
| 2583 |
|
| 2584 |
static int config_parse_mountpoint( |
| 2585 |
const char *unit, |
| 2586 |
const char *filename, |
| 2587 |
unsigned line, |
| 2588 |
const char *section, |
| 2589 |
unsigned section_line, |
| 2590 |
const char *lvalue, |
| 2591 |
int ltype, |
| 2592 |
const char *rvalue, |
| 2593 |
void *data, |
| 2594 |
void *userdata) { |
| 2595 |
|
| 2596 |
_cleanup_free_ char *where = NULL, *options = NULL; |
| 2597 |
Partition *p = ASSERT_PTR(data); |
| 2598 |
int r; |
| 2599 |
|
| 2600 |
if (isempty(rvalue)) { |
| 2601 |
partition_mountpoint_free_many(p->mountpoints, p->n_mountpoints); |
| 2602 |
return 0; |
| 2603 |
} |
| 2604 |
|
| 2605 |
const char *q = rvalue; |
| 2606 |
r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS|EXTRACT_UNQUOTE, |
| 2607 |
&where, &options); |
| 2608 |
if (r == -ENOMEM) |
| 2609 |
return log_oom(); |
| 2610 |
if (r < 0) { |
| 2611 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 2612 |
"Invalid syntax in %s=, ignoring: %s", lvalue, rvalue); |
| 2613 |
return 0; |
| 2614 |
} |
| 2615 |
if (r < 1) { |
| 2616 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 2617 |
"Too few arguments in %s=, ignoring: %s", lvalue, rvalue); |
| 2618 |
return 0; |
| 2619 |
} |
| 2620 |
if (!isempty(q)) { |
| 2621 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 2622 |
"Too many arguments in %s=, ignoring: %s", lvalue, rvalue); |
| 2623 |
return 0; |
| 2624 |
} |
| 2625 |
|
| 2626 |
r = path_simplify_and_warn(where, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue); |
| 2627 |
if (r < 0) |
| 2628 |
return 0; |
| 2629 |
|
| 2630 |
if (!GREEDY_REALLOC(p->mountpoints, p->n_mountpoints + 1)) |
| 2631 |
return log_oom(); |
| 2632 |
|
| 2633 |
p->mountpoints[p->n_mountpoints++] = (PartitionMountPoint) { |
| 2634 |
.where = TAKE_PTR(where), |
| 2635 |
.options = TAKE_PTR(options), |
| 2636 |
}; |
| 2637 |
|
| 2638 |
return 0; |
| 2639 |
} |
| 2640 |
|
| 2641 |
static int config_parse_encrypted_volume( |
| 2642 |
const char *unit, |
| 2643 |
const char *filename, |
| 2644 |
unsigned line, |
| 2645 |
const char *section, |
| 2646 |
unsigned section_line, |
| 2647 |
const char *lvalue, |
| 2648 |
int ltype, |
| 2649 |
const char *rvalue, |
| 2650 |
void *data, |
| 2651 |
void *userdata) { |
| 2652 |
|
| 2653 |
_cleanup_free_ char *volume = NULL, *keyfile = NULL, *options = NULL, *extra = NULL; |
| 2654 |
bool fixate_volume_key = false; |
| 2655 |
Partition *p = ASSERT_PTR(data); |
| 2656 |
int r; |
| 2657 |
|
| 2658 |
if (isempty(rvalue)) { |
| 2659 |
p->encrypted_volume = mfree(p->encrypted_volume); |
| 2660 |
return 0; |
| 2661 |
} |
| 2662 |
|
| 2663 |
const char *q = rvalue; |
| 2664 |
r = extract_many_words(&q, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS|EXTRACT_UNQUOTE, |
| 2665 |
&volume, &keyfile, &options, &extra); |
| 2666 |
if (r == -ENOMEM) |
| 2667 |
return log_oom(); |
| 2668 |
if (r < 0) { |
| 2669 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 2670 |
"Invalid syntax in %s=, ignoring: %s", lvalue, rvalue); |
| 2671 |
return 0; |
| 2672 |
} |
| 2673 |
if (r < 1) { |
| 2674 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 2675 |
"Too few arguments in %s=, ignoring: %s", lvalue, rvalue); |
| 2676 |
return 0; |
| 2677 |
} |
| 2678 |
if (!isempty(q)) { |
| 2679 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 2680 |
"Too many arguments in %s=, ignoring: %s", lvalue, rvalue); |
| 2681 |
return 0; |
| 2682 |
} |
| 2683 |
|
| 2684 |
if (!filename_is_valid(volume)) { |
| 2685 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 2686 |
"Volume name %s is not valid, ignoring", volume); |
| 2687 |
return 0; |
| 2688 |
} |
| 2689 |
|
| 2690 |
partition_encrypted_volume_free(p->encrypted_volume); |
| 2691 |
|
| 2692 |
p->encrypted_volume = new(PartitionEncryptedVolume, 1); |
| 2693 |
if (!p->encrypted_volume) |
| 2694 |
return log_oom(); |
| 2695 |
|
| 2696 |
for (const char *e = extra;;) { |
| 2697 |
_cleanup_free_ char *word = NULL; |
| 2698 |
|
| 2699 |
r = extract_first_word(&e, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS | EXTRACT_UNESCAPE_SEPARATORS); |
| 2700 |
if (r < 0) { |
| 2701 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 2702 |
"Failed to parse extra options '%s', ignoring", word); |
| 2703 |
break; |
| 2704 |
} |
| 2705 |
if (r == 0) |
| 2706 |
break; |
| 2707 |
|
| 2708 |
if (streq(word, "fixate-volume-key")) |
| 2709 |
fixate_volume_key = true; |
| 2710 |
else |
| 2711 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown extra option '%s', ignoring", word); |
| 2712 |
} |
| 2713 |
|
| 2714 |
*p->encrypted_volume = (PartitionEncryptedVolume) { |
| 2715 |
.name = TAKE_PTR(volume), |
| 2716 |
.keyfile = TAKE_PTR(keyfile), |
| 2717 |
.options = TAKE_PTR(options), |
| 2718 |
.fixate_volume_key = fixate_volume_key, |
| 2719 |
}; |
| 2720 |
|
| 2721 |
return 0; |
| 2722 |
} |
| 2723 |
|
| 2724 |
static int config_parse_tpm2_pcrs( |
| 2725 |
const char *unit, |
| 2726 |
const char *filename, |
| 2727 |
unsigned line, |
| 2728 |
const char *section, |
| 2729 |
unsigned section_line, |
| 2730 |
const char *lvalue, |
| 2731 |
int ltype, |
| 2732 |
const char *rvalue, |
| 2733 |
void *data, |
| 2734 |
void *userdata) { |
| 2735 |
|
| 2736 |
Partition *partition = ASSERT_PTR(data); |
| 2737 |
|
| 2738 |
assert(rvalue); |
| 2739 |
|
| 2740 |
if (isempty(rvalue)) { |
| 2741 |
/* Clear existing PCR values if empty */ |
| 2742 |
partition->tpm2_hash_pcr_values = mfree(partition->tpm2_hash_pcr_values); |
| 2743 |
partition->tpm2_n_hash_pcr_values = 0; |
| 2744 |
return 0; |
| 2745 |
} |
| 2746 |
|
| 2747 |
return tpm2_parse_pcr_argument_append(rvalue, &partition->tpm2_hash_pcr_values, |
| 2748 |
&partition->tpm2_n_hash_pcr_values); |
| 2749 |
} |
| 2750 |
|
| 2751 |
static int parse_key_file(const char *filename, struct iovec *key) { |
| 2752 |
_cleanup_(erase_and_freep) char *k = NULL; |
| 2753 |
size_t n = 0; |
| 2754 |
int r; |
| 2755 |
|
| 2756 |
assert(key); |
| 2757 |
|
| 2758 |
r = read_full_file_full( |
| 2759 |
AT_FDCWD, filename, |
| 2760 |
/* offset= */ UINT64_MAX, |
| 2761 |
/* size= */ SIZE_MAX, |
| 2762 |
READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET, |
| 2763 |
/* bind_name= */ NULL, |
| 2764 |
&k, &n); |
| 2765 |
if (r < 0) |
| 2766 |
return log_error_errno(r, "Failed to read key file '%s': %m", filename); |
| 2767 |
|
| 2768 |
iovec_done_erase(key); |
| 2769 |
*key = IOVEC_MAKE(TAKE_PTR(k), n); |
| 2770 |
|
| 2771 |
return 0; |
| 2772 |
} |
| 2773 |
|
| 2774 |
static int config_parse_key_file( |
| 2775 |
const char *unit, |
| 2776 |
const char *filename, |
| 2777 |
unsigned line, |
| 2778 |
const char *section, |
| 2779 |
unsigned section_line, |
| 2780 |
const char *lvalue, |
| 2781 |
int ltype, |
| 2782 |
const char *rvalue, |
| 2783 |
void *data, |
| 2784 |
void *userdata) { |
| 2785 |
|
| 2786 |
Partition *partition = ASSERT_PTR(userdata); |
| 2787 |
|
| 2788 |
assert(rvalue); |
| 2789 |
|
| 2790 |
if (isempty(rvalue)) { |
| 2791 |
iovec_done_erase(&partition->key); |
| 2792 |
return 0; |
| 2793 |
} |
| 2794 |
|
| 2795 |
return parse_key_file(rvalue, &partition->key); |
| 2796 |
} |
| 2797 |
|
| 2798 |
static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_integrity, integrity_mode, IntegrityMode, INTEGRITY_OFF); |
| 2799 |
static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_integrity_alg, integrity_alg, IntegrityAlg, INTEGRITY_ALG_HMAC_SHA256); |
| 2800 |
static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_encrypt_kdf, encrypt_kdf, EncryptKDF, _ENCRYPT_KDF_INVALID); |
| 2801 |
|
| 2802 |
static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_verity, verity_mode, VerityMode, VERITY_OFF); |
| 2803 |
static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_minimize, minimize_mode, MinimizeMode, MINIMIZE_OFF); |
| 2804 |
|
| 2805 |
static int partition_finalize_fstype(Partition *p, const char *path) { |
| 2806 |
_cleanup_free_ char *e = NULL, *upper = NULL; |
| 2807 |
|
| 2808 |
assert(p); |
| 2809 |
assert(path); |
| 2810 |
|
| 2811 |
if (!gpt_partition_type_has_filesystem(p->type)) |
| 2812 |
return 0; |
| 2813 |
|
| 2814 |
upper = strdup(partition_designator_to_string(p->type.designator)); |
| 2815 |
if (!upper) |
| 2816 |
return log_oom(); |
| 2817 |
|
| 2818 |
e = strjoin("SYSTEMD_REPART_OVERRIDE_FSTYPE_", string_replace_char(ascii_strupper(upper), '-', '_')); |
| 2819 |
if (!e) |
| 2820 |
return log_oom(); |
| 2821 |
|
| 2822 |
const char *v = secure_getenv(e); |
| 2823 |
if (!v || streq_ptr(p->format, v)) |
| 2824 |
return 0; |
| 2825 |
|
| 2826 |
log_syntax(NULL, LOG_NOTICE, path, 1, 0, |
| 2827 |
"Overriding defined file system type '%s' for '%s' partition with '%s'.", |
| 2828 |
p->format, partition_designator_to_string(p->type.designator), v); |
| 2829 |
|
| 2830 |
return free_and_strdup_warn(&p->format, v); |
| 2831 |
} |
| 2832 |
|
| 2833 |
static bool partition_add_validatefs(const Partition *p) { |
| 2834 |
assert(p); |
| 2835 |
|
| 2836 |
if (p->add_validatefs >= 0) |
| 2837 |
return p->add_validatefs; |
| 2838 |
|
| 2839 |
return p->format && !STR_IN_SET(p->format, "swap", "vfat"); |
| 2840 |
} |
| 2841 |
|
| 2842 |
static bool partition_needs_populate(const Partition *p) { |
| 2843 |
assert(p); |
| 2844 |
assert(!p->supplement_for || !p->suppressing); /* Avoid infinite recursion */ |
| 2845 |
|
| 2846 |
return p->n_copy_files > 0 || |
| 2847 |
!strv_isempty(p->make_directories) || |
| 2848 |
!strv_isempty(p->make_symlinks) || |
| 2849 |
partition_add_validatefs(p) || |
| 2850 |
(p->suppressing && partition_needs_populate(p->suppressing)); |
| 2851 |
} |
| 2852 |
|
| 2853 |
static MakeFileSystemFlags partition_mkfs_flags(const Partition *p) { |
| 2854 |
MakeFileSystemFlags flags = 0; |
| 2855 |
|
| 2856 |
if (arg_discard && !p->discarded) |
| 2857 |
flags |= MKFS_DISCARD; |
| 2858 |
|
| 2859 |
if (streq(p->format, "erofs") && !DEBUG_LOGGING && !isatty_safe(STDERR_FILENO)) |
| 2860 |
flags |= MKFS_QUIET; |
| 2861 |
|
| 2862 |
FOREACH_ARRAY(cf, p->copy_files, p->n_copy_files) |
| 2863 |
if (cf->flags & COPY_PRESERVE_FS_VERITY) { |
| 2864 |
flags |= MKFS_FS_VERITY; |
| 2865 |
break; |
| 2866 |
} |
| 2867 |
|
| 2868 |
return flags; |
| 2869 |
} |
| 2870 |
|
| 2871 |
static int context_notify( |
| 2872 |
Context *c, |
| 2873 |
ProgressPhase phase, |
| 2874 |
const char *object, |
| 2875 |
unsigned percent) { |
| 2876 |
|
| 2877 |
int r; |
| 2878 |
|
| 2879 |
assert(c); |
| 2880 |
assert(phase >= 0); |
| 2881 |
assert(phase < _PROGRESS_PHASE_MAX); |
| 2882 |
|
| 2883 |
/* Send progress information, via sd_notify() and via varlink (if client asked for it by setting "more" flag) */ |
| 2884 |
|
| 2885 |
_cleanup_free_ char *n = NULL; |
| 2886 |
if (asprintf(&n, |
| 2887 |
"STATUS=Phase %1$s\n" |
| 2888 |
"X_SYSTEMD_PHASE=%1$s", |
| 2889 |
progress_phase_to_string(phase)) < 0) |
| 2890 |
return log_oom_debug(); |
| 2891 |
|
| 2892 |
if (percent != UINT_MAX) |
| 2893 |
if (strextendf(&n, "\nX_SYSTEMD_PHASE_PROGRESS=%u", percent) < 0) |
| 2894 |
return log_oom_debug(); |
| 2895 |
|
| 2896 |
r = sd_notify(/* unset_environment= */ false, n); |
| 2897 |
if (r < 0) |
| 2898 |
log_debug_errno(r, "Failed to send sd_notify() progress notification, ignoring: %m"); |
| 2899 |
|
| 2900 |
if (c->link) { |
| 2901 |
r = sd_varlink_notifybo( |
| 2902 |
c->link, |
| 2903 |
JSON_BUILD_PAIR_ENUM("phase", progress_phase_to_string(phase)), |
| 2904 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("object", object), |
| 2905 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("progress", percent, UINT_MAX)); |
| 2906 |
if (r < 0) |
| 2907 |
log_debug_errno(r, "Failed to send varlink notify progress notification, ignoring: %m"); |
| 2908 |
|
| 2909 |
r = sd_varlink_flush(c->link); |
| 2910 |
if (r < 0) |
| 2911 |
log_debug_errno(r, "Failed to flush varlink notify progress notification, ignoring: %m"); |
| 2912 |
} |
| 2913 |
|
| 2914 |
return 0; |
| 2915 |
} |
| 2916 |
|
| 2917 |
static int partition_read_definition( |
| 2918 |
Context *c, |
| 2919 |
Partition *p, |
| 2920 |
const char *path, |
| 2921 |
const char *const *conf_file_dirs) { |
| 2922 |
|
| 2923 |
ConfigTableItem table[] = { |
| 2924 |
{ "Partition", "Type", config_parse_type, 0, &p->type }, |
| 2925 |
{ "Partition", "Label", config_parse_label, 0, &p->new_label }, |
| 2926 |
{ "Partition", "VolumeLabel", config_parse_label, 0, &p->new_volume_label }, |
| 2927 |
{ "Partition", "UUID", config_parse_uuid, 0, p }, |
| 2928 |
{ "Partition", "Priority", config_parse_int32, 0, &p->priority }, |
| 2929 |
{ "Partition", "Weight", config_parse_weight, 0, &p->weight }, |
| 2930 |
{ "Partition", "PaddingWeight", config_parse_weight, 0, &p->padding_weight }, |
| 2931 |
{ "Partition", "SizeMinBytes", config_parse_size4096, -1, &p->size_min }, |
| 2932 |
{ "Partition", "SizeMaxBytes", config_parse_size4096, 1, &p->size_max }, |
| 2933 |
{ "Partition", "PaddingMinBytes", config_parse_size4096, -1, &p->padding_min }, |
| 2934 |
{ "Partition", "PaddingMaxBytes", config_parse_size4096, 1, &p->padding_max }, |
| 2935 |
{ "Partition", "FactoryReset", config_parse_bool, 0, &p->factory_reset }, |
| 2936 |
{ "Partition", "CopyBlocks", config_parse_copy_blocks, 0, p }, |
| 2937 |
{ "Partition", "Format", config_parse_fstype, 0, &p->format }, |
| 2938 |
{ "Partition", "CopyFiles", config_parse_copy_files, 0, p }, |
| 2939 |
{ "Partition", "ExcludeFiles", config_parse_exclude_files, 0, &p->exclude_files_source }, |
| 2940 |
{ "Partition", "ExcludeFilesTarget", config_parse_exclude_files, 0, &p->exclude_files_target }, |
| 2941 |
{ "Partition", "MakeDirectories", config_parse_make_dirs, 0, &p->make_directories }, |
| 2942 |
{ "Partition", "MakeSymlinks", config_parse_make_symlinks, 0, &p->make_symlinks }, |
| 2943 |
{ "Partition", "Encrypt", config_parse_encrypt, 0, &p->encrypt }, |
| 2944 |
{ "Partition", "Verity", config_parse_verity, 0, &p->verity }, |
| 2945 |
{ "Partition", "VerityMatchKey", config_parse_string, 0, &p->verity_match_key }, |
| 2946 |
{ "Partition", "Flags", config_parse_gpt_flags, 0, &p->gpt_flags }, |
| 2947 |
{ "Partition", "ReadOnly", config_parse_tristate, 0, &p->read_only }, |
| 2948 |
{ "Partition", "NoAuto", config_parse_tristate, 0, &p->no_auto }, |
| 2949 |
{ "Partition", "GrowFileSystem", config_parse_tristate, 0, &p->growfs }, |
| 2950 |
{ "Partition", "SplitName", config_parse_string, 0, &p->split_name_format }, |
| 2951 |
{ "Partition", "Minimize", config_parse_minimize, 0, &p->minimize }, |
| 2952 |
{ "Partition", "Subvolumes", config_parse_subvolumes, 0, &p->subvolumes }, |
| 2953 |
{ "Partition", "DefaultSubvolume", config_parse_default_subvolume, 0, &p->default_subvolume }, |
| 2954 |
{ "Partition", "VerityDataBlockSizeBytes", config_parse_block_size, 0, &p->verity_data_block_size }, |
| 2955 |
{ "Partition", "VerityHashBlockSizeBytes", config_parse_block_size, 0, &p->verity_hash_block_size }, |
| 2956 |
{ "Partition", "MountPoint", config_parse_mountpoint, 0, p }, |
| 2957 |
{ "Partition", "EncryptedVolume", config_parse_encrypted_volume, 0, p }, |
| 2958 |
{ "Partition", "TPM2PCRs", config_parse_tpm2_pcrs, 0, p }, |
| 2959 |
{ "Partition", "KeyFile", config_parse_key_file, 0, p }, |
| 2960 |
{ "Partition", "Integrity", config_parse_integrity, 0, &p->integrity }, |
| 2961 |
{ "Partition", "IntegrityAlgorithm", config_parse_integrity_alg, 0, &p->integrity_alg }, |
| 2962 |
{ "Partition", "EncryptKDF", config_parse_encrypt_kdf, 0, &p->encrypt_kdf }, |
| 2963 |
{ "Partition", "Compression", config_parse_string, CONFIG_PARSE_STRING_SAFE_AND_ASCII, &p->compression }, |
| 2964 |
{ "Partition", "CompressionLevel", config_parse_string, CONFIG_PARSE_STRING_SAFE_AND_ASCII, &p->compression_level }, |
| 2965 |
{ "Partition", "SupplementFor", config_parse_string, 0, &p->supplement_for_name }, |
| 2966 |
{ "Partition", "AddValidateFS", config_parse_tristate, 0, &p->add_validatefs }, |
| 2967 |
{ "Partition", "FileSystemSectorSize", config_parse_fs_sector_size, 0, &p->fs_sector_size }, |
| 2968 |
{ "Partition", "Discard", config_parse_tristate, 0, &p->discard }, |
| 2969 |
{ "Partition", "BlockDeviceReplace", config_parse_path, 0, &p->block_device_replace }, |
| 2970 |
{ "Partition", "VolumeName", config_parse_string, CONFIG_PARSE_STRING_SAFE, &p->volume_name }, |
| 2971 |
{} |
| 2972 |
}; |
| 2973 |
_cleanup_free_ char *filename = NULL; |
| 2974 |
const char* dropin_dirname; |
| 2975 |
int r; |
| 2976 |
|
| 2977 |
assert(c); |
| 2978 |
assert(p); |
| 2979 |
assert(path); |
| 2980 |
|
| 2981 |
r = path_extract_filename(path, &filename); |
| 2982 |
if (r < 0) |
| 2983 |
return log_error_errno(r, "Failed to extract filename from path '%s': %m", path); |
| 2984 |
|
| 2985 |
dropin_dirname = strjoina(filename, ".d"); |
| 2986 |
|
| 2987 |
r = config_parse_many_full( |
| 2988 |
STRV_MAKE_CONST(path), |
| 2989 |
conf_file_dirs, |
| 2990 |
dropin_dirname, |
| 2991 |
c->definitions ? NULL : arg_root, |
| 2992 |
/* root_fd= */ -EBADF, |
| 2993 |
"Partition\0", |
| 2994 |
config_item_table_lookup, table, |
| 2995 |
CONFIG_PARSE_WARN, |
| 2996 |
p, |
| 2997 |
/* ret_stats_by_path= */ NULL, |
| 2998 |
&p->drop_in_files); |
| 2999 |
if (r < 0) |
| 3000 |
return r; |
| 3001 |
|
| 3002 |
if (partition_type_exclude(&p->type)) |
| 3003 |
return 0; |
| 3004 |
|
| 3005 |
if (p->size_min != UINT64_MAX && p->size_max != UINT64_MAX && p->size_min > p->size_max) |
| 3006 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3007 |
"SizeMinBytes= larger than SizeMaxBytes=, refusing."); |
| 3008 |
|
| 3009 |
if (p->padding_min != UINT64_MAX && p->padding_max != UINT64_MAX && p->padding_min > p->padding_max) |
| 3010 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3011 |
"PaddingMinBytes= larger than PaddingMaxBytes=, refusing."); |
| 3012 |
|
| 3013 |
if (sd_id128_is_null(p->type.uuid)) |
| 3014 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3015 |
"Type= not defined, refusing."); |
| 3016 |
|
| 3017 |
if ((p->copy_blocks_path || p->copy_blocks_auto) && (p->format || partition_needs_populate(p))) |
| 3018 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3019 |
"Format=/CopyFiles=/MakeDirectories=/MakeSymlinks= and CopyBlocks= cannot be combined, refusing."); |
| 3020 |
|
| 3021 |
if (p->block_device_replace && (p->format || partition_needs_populate(p))) |
| 3022 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3023 |
"Format=/CopyFiles=/MakeDirectories=/MakeSymlinks= and BlockDeviceReplace= cannot be combined, refusing."); |
| 3024 |
|
| 3025 |
if ((p->copy_blocks_path || p->copy_blocks_auto) && p->block_device_replace) |
| 3026 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3027 |
"CopyBlocks= and BlockDeviceReplace= cannot be combined, refusing."); |
| 3028 |
|
| 3029 |
if (p->block_device_replace && arg_offline == 1) |
| 3030 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3031 |
"BlockDeviceReplace= is incompatible with --offline=yes, refusing."); |
| 3032 |
|
| 3033 |
if (p->volume_name && !filename_is_valid(p->volume_name)) |
| 3034 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3035 |
"VolumeName= has an invalid filename value, refusing."); |
| 3036 |
|
| 3037 |
if (p->volume_name && strlen(p->volume_name) > (DM_NAME_LEN - 1)) |
| 3038 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3039 |
"VolumeName= is too long, refusing."); |
| 3040 |
|
| 3041 |
if (p->volume_name && p->encrypt == ENCRYPT_OFF) |
| 3042 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3043 |
"VolumeName= requires Encrypt= to be enabled, refusing."); |
| 3044 |
|
| 3045 |
if (p->volume_name && !p->block_device_replace) |
| 3046 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3047 |
"VolumeName= requires BlockDeviceReplace= to be set, refusing."); |
| 3048 |
|
| 3049 |
if (partition_needs_populate(p) && streq_ptr(p->format, "swap")) |
| 3050 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3051 |
"Format=swap and CopyFiles=/MakeDirectories=/MakeSymlinks= cannot be combined, refusing."); |
| 3052 |
|
| 3053 |
if (!p->format) { |
| 3054 |
const char *format = NULL; |
| 3055 |
|
| 3056 |
if (p->type.designator == PARTITION_SWAP) |
| 3057 |
format = "swap"; |
| 3058 |
else if (partition_needs_populate(p) || (p->encrypt != ENCRYPT_OFF && !(p->copy_blocks_path || p->copy_blocks_auto || p->block_device_replace))) |
| 3059 |
/* Pick "vfat" as file system for esp and xbootldr partitions, otherwise default to "ext4". */ |
| 3060 |
format = IN_SET(p->type.designator, PARTITION_ESP, PARTITION_XBOOTLDR) ? "vfat" : "ext4"; |
| 3061 |
|
| 3062 |
if (format) { |
| 3063 |
p->format = strdup(format); |
| 3064 |
if (!p->format) |
| 3065 |
return log_oom(); |
| 3066 |
} |
| 3067 |
} |
| 3068 |
|
| 3069 |
if (streq_ptr(p->format, "empty")) { |
| 3070 |
p->format = mfree(p->format); |
| 3071 |
|
| 3072 |
if (p->no_auto < 0 && gpt_partition_type_knows_no_auto(p->type)) |
| 3073 |
p->no_auto = true; |
| 3074 |
|
| 3075 |
if (!p->new_label) { |
| 3076 |
p->new_label = strdup("_empty"); |
| 3077 |
if (!p->new_label) |
| 3078 |
return log_oom(); |
| 3079 |
} |
| 3080 |
} |
| 3081 |
|
| 3082 |
if (p->minimize != MINIMIZE_OFF && !p->format && p->verity != VERITY_HASH) |
| 3083 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3084 |
"Minimize= can only be enabled if Format= or Verity=hash are set."); |
| 3085 |
|
| 3086 |
if (p->minimize == MINIMIZE_BEST && |
| 3087 |
p->format && |
| 3088 |
!fstype_is_ro(p->format) && |
| 3089 |
!streq(p->format, "btrfs") && |
| 3090 |
p->verity != VERITY_HASH) |
| 3091 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3092 |
"Minimize=best can only be used with read-only filesystems, btrfs, or Verity=hash."); |
| 3093 |
|
| 3094 |
if (partition_needs_populate(p) && !mkfs_supports_root_option(p->format) && geteuid() != 0) |
| 3095 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EPERM), |
| 3096 |
"Need to be root to populate %s filesystems with CopyFiles=/MakeDirectories=/MakeSymlinks=.", |
| 3097 |
p->format); |
| 3098 |
|
| 3099 |
if (p->format && fstype_is_ro(p->format) && !partition_needs_populate(p)) |
| 3100 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3101 |
"Cannot format %s filesystem without source files, refusing.", p->format); |
| 3102 |
|
| 3103 |
if (p->verity != VERITY_OFF || p->encrypt != ENCRYPT_OFF) { |
| 3104 |
r = DLOPEN_CRYPTSETUP(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED); |
| 3105 |
if (r < 0) |
| 3106 |
return log_syntax(NULL, LOG_ERR, path, 1, r, |
| 3107 |
"libcryptsetup not found, Verity=/Encrypt= are not supported: %m"); |
| 3108 |
} |
| 3109 |
|
| 3110 |
if (p->verity != VERITY_OFF && !p->verity_match_key) |
| 3111 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3112 |
"VerityMatchKey= must be set if Verity=%s.", verity_mode_to_string(p->verity)); |
| 3113 |
|
| 3114 |
if (p->verity == VERITY_OFF && p->verity_match_key) |
| 3115 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3116 |
"VerityMatchKey= can only be set if Verity= is not \"%s\".", |
| 3117 |
verity_mode_to_string(p->verity)); |
| 3118 |
|
| 3119 |
if (IN_SET(p->verity, VERITY_HASH, VERITY_SIG)) { |
| 3120 |
if (p->format || partition_needs_populate(p)) |
| 3121 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3122 |
"CopyFiles=/Format=/MakeDirectories=/MakeSymlinks= cannot be used with Verity=%s.", |
| 3123 |
verity_mode_to_string(p->verity)); |
| 3124 |
|
| 3125 |
/* Later we check that the same CopyBlocks= type (auto vs path) is used for the entire verity set. |
| 3126 |
* So we assume that CopyBlocks=auto is going to be correct and just path based blocks might result in |
| 3127 |
* a broken setup */ |
| 3128 |
if (p->copy_blocks_path) |
| 3129 |
log_syntax(NULL, LOG_DEBUG, path, 1, 0, |
| 3130 |
"CopyBlocks= with Verity=%s bypasses dm-verity hash/signature computation; repart cannot verify the resulting setup is correct.", |
| 3131 |
verity_mode_to_string(p->verity)); |
| 3132 |
} |
| 3133 |
|
| 3134 |
if (p->verity != VERITY_OFF && p->encrypt != ENCRYPT_OFF) |
| 3135 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3136 |
"Encrypting verity hash/data partitions is not supported."); |
| 3137 |
|
| 3138 |
if (p->verity == VERITY_SIG && (p->size_min != UINT64_MAX || p->size_max != UINT64_MAX)) |
| 3139 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3140 |
"SizeMinBytes=/SizeMaxBytes= cannot be used with Verity=%s.", |
| 3141 |
verity_mode_to_string(p->verity)); |
| 3142 |
|
| 3143 |
if (p->integrity == INTEGRITY_INLINE && p->encrypt == ENCRYPT_OFF) |
| 3144 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3145 |
"Integrity=inline requires Encrypt=."); |
| 3146 |
|
| 3147 |
if (p->default_subvolume && !ordered_hashmap_contains(p->subvolumes, p->default_subvolume)) |
| 3148 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3149 |
"DefaultSubvolume= must be one of the paths in Subvolumes=."); |
| 3150 |
|
| 3151 |
if (p->supplement_for_name) { |
| 3152 |
if (!filename_part_is_valid(p->supplement_for_name)) |
| 3153 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3154 |
"SupplementFor= is an invalid filename: %s", |
| 3155 |
p->supplement_for_name); |
| 3156 |
|
| 3157 |
if (p->copy_blocks_path || p->copy_blocks_auto || p->encrypt != ENCRYPT_OFF || |
| 3158 |
p->verity != VERITY_OFF) |
| 3159 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3160 |
"SupplementFor= cannot be combined with CopyBlocks=/Encrypt=/Verity="); |
| 3161 |
} |
| 3162 |
|
| 3163 |
if (p->encrypt == ENCRYPT_OFF && p->discard > 0) |
| 3164 |
log_syntax(NULL, LOG_WARNING, path, 1, 0, |
| 3165 |
"Discard=yes has no effect with Encrypt=off."); |
| 3166 |
|
| 3167 |
if (p->encrypt == ENCRYPT_OFF && p->encrypt_kdf >= 0) |
| 3168 |
log_syntax(NULL, LOG_WARNING, path, 1, 0, |
| 3169 |
"EncryptKDF= has no effect with Encrypt=off."); |
| 3170 |
|
| 3171 |
if (p->encrypt != ENCRYPT_OFF && p->integrity == INTEGRITY_INLINE && p->discard > 0) |
| 3172 |
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3173 |
"Integrity=inline is incompatible with Discard=yes."); |
| 3174 |
|
| 3175 |
/* Verity partitions are read only, let's imply the RO flag hence, unless explicitly configured otherwise. */ |
| 3176 |
if ((partition_designator_is_verity_hash(p->type.designator) || |
| 3177 |
partition_designator_is_verity_sig(p->type.designator) || |
| 3178 |
IN_SET(p->verity, VERITY_DATA, VERITY_SIG)) && p->read_only < 0) |
| 3179 |
p->read_only = true; |
| 3180 |
|
| 3181 |
/* Default to "growfs" on, unless read-only */ |
| 3182 |
if (gpt_partition_type_knows_growfs(p->type) && |
| 3183 |
p->read_only <= 0) |
| 3184 |
p->growfs = true; |
| 3185 |
|
| 3186 |
if (!p->split_name_format) { |
| 3187 |
char *s = strdup("%t"); |
| 3188 |
if (!s) |
| 3189 |
return log_oom(); |
| 3190 |
|
| 3191 |
p->split_name_format = s; |
| 3192 |
} else if (streq(p->split_name_format, "-")) |
| 3193 |
p->split_name_format = mfree(p->split_name_format); |
| 3194 |
|
| 3195 |
r = partition_finalize_fstype(p, path); |
| 3196 |
if (r < 0) |
| 3197 |
return r; |
| 3198 |
|
| 3199 |
return 1; |
| 3200 |
} |
| 3201 |
|
| 3202 |
static int find_verity_sibling(Context *context, Partition *p, VerityMode mode, Partition **ret) { |
| 3203 |
Partition *s = NULL; |
| 3204 |
|
| 3205 |
assert(p); |
| 3206 |
assert(p->verity != VERITY_OFF); |
| 3207 |
assert(p->verity_match_key); |
| 3208 |
assert(mode != VERITY_OFF); |
| 3209 |
assert(p->verity != mode); |
| 3210 |
assert(ret); |
| 3211 |
|
| 3212 |
/* Try to find the matching sibling partition of the given type for a verity partition. For a data |
| 3213 |
* partition, this is the corresponding hash partition with the same verity name (and vice versa for |
| 3214 |
* the hash partition). */ |
| 3215 |
|
| 3216 |
LIST_FOREACH(partitions, q, context->partitions) { |
| 3217 |
if (p == q) |
| 3218 |
continue; |
| 3219 |
|
| 3220 |
if (q->verity != mode) |
| 3221 |
continue; |
| 3222 |
|
| 3223 |
assert(q->verity_match_key); |
| 3224 |
|
| 3225 |
if (!streq(p->verity_match_key, q->verity_match_key)) |
| 3226 |
continue; |
| 3227 |
|
| 3228 |
if (s) |
| 3229 |
return -ENOTUNIQ; |
| 3230 |
|
| 3231 |
s = q; |
| 3232 |
} |
| 3233 |
|
| 3234 |
if (!s) |
| 3235 |
return -ENXIO; |
| 3236 |
|
| 3237 |
*ret = s; |
| 3238 |
|
| 3239 |
return 0; |
| 3240 |
} |
| 3241 |
|
| 3242 |
static int context_open_and_lock_backing_fd(const char *node, int operation, int mode, int *backing_fd) { |
| 3243 |
_cleanup_close_ int fd = -EBADF; |
| 3244 |
|
| 3245 |
assert(node); |
| 3246 |
assert(backing_fd); |
| 3247 |
assert(IN_SET(mode, O_RDONLY, O_RDWR)); |
| 3248 |
|
| 3249 |
if (*backing_fd >= 0) |
| 3250 |
return 0; |
| 3251 |
|
| 3252 |
fd = open(node, mode|O_CLOEXEC); |
| 3253 |
if (fd < 0) |
| 3254 |
return log_error_errno(errno, "Failed to open device '%s': %m", node); |
| 3255 |
|
| 3256 |
/* Tell udev not to interfere while we are processing the device */ |
| 3257 |
if (flock(fd, operation) < 0) |
| 3258 |
return log_error_errno(errno, "Failed to lock device '%s': %m", node); |
| 3259 |
|
| 3260 |
log_debug("Device %s opened and locked.", node); |
| 3261 |
*backing_fd = TAKE_FD(fd); |
| 3262 |
return 1; |
| 3263 |
} |
| 3264 |
|
| 3265 |
static int determine_current_padding( |
| 3266 |
struct fdisk_context *c, |
| 3267 |
struct fdisk_table *t, |
| 3268 |
struct fdisk_partition *p, |
| 3269 |
uint64_t secsz, |
| 3270 |
uint64_t grainsz, |
| 3271 |
uint64_t *ret) { |
| 3272 |
|
| 3273 |
size_t n_partitions; |
| 3274 |
uint64_t offset, next = UINT64_MAX; |
| 3275 |
|
| 3276 |
assert(c); |
| 3277 |
assert(t); |
| 3278 |
assert(p); |
| 3279 |
assert(ret); |
| 3280 |
|
| 3281 |
if (!sym_fdisk_partition_has_end(p)) |
| 3282 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition has no end."); |
| 3283 |
|
| 3284 |
offset = sym_fdisk_partition_get_end(p); |
| 3285 |
assert(offset < UINT64_MAX); |
| 3286 |
offset++; /* The end is one sector before the next partition or padding. */ |
| 3287 |
assert(offset < UINT64_MAX / secsz); |
| 3288 |
offset *= secsz; |
| 3289 |
|
| 3290 |
n_partitions = sym_fdisk_table_get_nents(t); |
| 3291 |
for (size_t i = 0; i < n_partitions; i++) { |
| 3292 |
struct fdisk_partition *q; |
| 3293 |
uint64_t start; |
| 3294 |
|
| 3295 |
q = sym_fdisk_table_get_partition(t, i); |
| 3296 |
if (!q) |
| 3297 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata."); |
| 3298 |
|
| 3299 |
if (sym_fdisk_partition_is_used(q) <= 0) |
| 3300 |
continue; |
| 3301 |
|
| 3302 |
if (!sym_fdisk_partition_has_start(q)) |
| 3303 |
continue; |
| 3304 |
|
| 3305 |
start = sym_fdisk_partition_get_start(q); |
| 3306 |
assert(start < UINT64_MAX / secsz); |
| 3307 |
start *= secsz; |
| 3308 |
|
| 3309 |
if (start >= offset && (next == UINT64_MAX || next > start)) |
| 3310 |
next = start; |
| 3311 |
} |
| 3312 |
|
| 3313 |
if (next == UINT64_MAX) { |
| 3314 |
/* No later partition? In that case check the end of the usable area */ |
| 3315 |
next = sym_fdisk_get_last_lba(c); |
| 3316 |
assert(next < UINT64_MAX); |
| 3317 |
next++; /* The last LBA is one sector before the end */ |
| 3318 |
|
| 3319 |
assert(next < UINT64_MAX / secsz); |
| 3320 |
next *= secsz; |
| 3321 |
|
| 3322 |
if (offset > next) |
| 3323 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end."); |
| 3324 |
} |
| 3325 |
|
| 3326 |
assert(next >= offset); |
| 3327 |
offset = round_up_size(offset, grainsz); |
| 3328 |
next = round_down_size(next, grainsz); |
| 3329 |
|
| 3330 |
*ret = LESS_BY(next, offset); /* Saturated subtraction, rounding might have fucked things up */ |
| 3331 |
return 0; |
| 3332 |
} |
| 3333 |
|
| 3334 |
static int context_copy_from_one(Context *context, const char *src) { |
| 3335 |
_cleanup_close_ int fd = -EBADF; |
| 3336 |
_cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL; |
| 3337 |
_cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL; |
| 3338 |
Partition *last = NULL; |
| 3339 |
unsigned long secsz, grainsz; |
| 3340 |
size_t n_partitions; |
| 3341 |
int r; |
| 3342 |
|
| 3343 |
assert(src); |
| 3344 |
|
| 3345 |
r = context_open_and_lock_backing_fd(src, LOCK_SH, O_RDONLY, &fd); |
| 3346 |
if (r < 0) |
| 3347 |
return r; |
| 3348 |
|
| 3349 |
r = fd_verify_regular_or_block(fd); |
| 3350 |
if (r < 0) |
| 3351 |
return log_error_errno(r, "'%s' is not a file nor a block device: %m", src); |
| 3352 |
|
| 3353 |
r = fdisk_new_context_at(fd, /* path= */ NULL, /* read_only= */ true, /* sector_size= */ UINT32_MAX, &c); |
| 3354 |
if (r < 0) |
| 3355 |
return log_error_errno(r, "Failed to create fdisk context: %m"); |
| 3356 |
|
| 3357 |
secsz = sym_fdisk_get_sector_size(c); |
| 3358 |
grainsz = sym_fdisk_get_grain_size(c); |
| 3359 |
|
| 3360 |
/* Insist on a power of two, and that it's a multiple of 512, i.e. the traditional sector size. */ |
| 3361 |
if (secsz < 512 || !ISPOWEROF2(secsz)) |
| 3362 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Sector size %lu is not a power of two larger than 512? Refusing.", secsz); |
| 3363 |
|
| 3364 |
if (!sym_fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT)) |
| 3365 |
return log_error_errno(SYNTHETIC_ERRNO(EHWPOISON), "Cannot copy from disk %s with no GPT disk label.", src); |
| 3366 |
|
| 3367 |
r = sym_fdisk_get_partitions(c, &t); |
| 3368 |
if (r < 0) |
| 3369 |
return log_error_errno(r, "Failed to acquire partition table: %m"); |
| 3370 |
|
| 3371 |
n_partitions = sym_fdisk_table_get_nents(t); |
| 3372 |
for (size_t i = 0; i < n_partitions; i++) { |
| 3373 |
_cleanup_(partition_freep) Partition *np = NULL; |
| 3374 |
_cleanup_free_ char *label_copy = NULL; |
| 3375 |
struct fdisk_partition *p; |
| 3376 |
const char *label; |
| 3377 |
uint64_t sz, start, padding; |
| 3378 |
sd_id128_t ptid, id; |
| 3379 |
GptPartitionType type; |
| 3380 |
|
| 3381 |
p = sym_fdisk_table_get_partition(t, i); |
| 3382 |
if (!p) |
| 3383 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata."); |
| 3384 |
|
| 3385 |
if (sym_fdisk_partition_is_used(p) <= 0) |
| 3386 |
continue; |
| 3387 |
|
| 3388 |
if (sym_fdisk_partition_has_start(p) <= 0 || |
| 3389 |
sym_fdisk_partition_has_size(p) <= 0 || |
| 3390 |
sym_fdisk_partition_has_partno(p) <= 0) |
| 3391 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a position, size or number."); |
| 3392 |
|
| 3393 |
r = fdisk_partition_get_type_as_id128(p, &ptid); |
| 3394 |
if (r < 0) |
| 3395 |
return log_error_errno(r, "Failed to query partition type UUID: %m"); |
| 3396 |
|
| 3397 |
type = gpt_partition_type_from_uuid(ptid); |
| 3398 |
|
| 3399 |
r = fdisk_partition_get_uuid_as_id128(p, &id); |
| 3400 |
if (r < 0) |
| 3401 |
return log_error_errno(r, "Failed to query partition UUID: %m"); |
| 3402 |
|
| 3403 |
label = sym_fdisk_partition_get_name(p); |
| 3404 |
if (!isempty(label)) { |
| 3405 |
label_copy = strdup(label); |
| 3406 |
if (!label_copy) |
| 3407 |
return log_oom(); |
| 3408 |
} |
| 3409 |
|
| 3410 |
sz = sym_fdisk_partition_get_size(p); |
| 3411 |
assert(sz <= UINT64_MAX/secsz); |
| 3412 |
sz *= secsz; |
| 3413 |
|
| 3414 |
start = sym_fdisk_partition_get_start(p); |
| 3415 |
assert(start <= UINT64_MAX/secsz); |
| 3416 |
start *= secsz; |
| 3417 |
|
| 3418 |
if (partition_type_exclude(&type)) |
| 3419 |
continue; |
| 3420 |
|
| 3421 |
np = partition_new(context); |
| 3422 |
if (!np) |
| 3423 |
return log_oom(); |
| 3424 |
|
| 3425 |
np->type = type; |
| 3426 |
np->new_uuid = id; |
| 3427 |
np->new_uuid_is_set = true; |
| 3428 |
np->size_min = np->size_max = sz; |
| 3429 |
np->new_label = TAKE_PTR(label_copy); |
| 3430 |
|
| 3431 |
np->definition_path = strdup(src); |
| 3432 |
if (!np->definition_path) |
| 3433 |
return log_oom(); |
| 3434 |
|
| 3435 |
np->split_name_format = strdup("%t"); |
| 3436 |
if (!np->split_name_format) |
| 3437 |
return log_oom(); |
| 3438 |
|
| 3439 |
r = determine_current_padding(c, t, p, secsz, grainsz, &padding); |
| 3440 |
if (r < 0) |
| 3441 |
return r; |
| 3442 |
|
| 3443 |
np->padding_min = np->padding_max = padding; |
| 3444 |
|
| 3445 |
np->copy_blocks_path = strdup(src); |
| 3446 |
if (!np->copy_blocks_path) |
| 3447 |
return log_oom(); |
| 3448 |
|
| 3449 |
np->copy_blocks_fd = r = RET_NERRNO(fcntl(fd, F_DUPFD_CLOEXEC, 3)); |
| 3450 |
if (r < 0) |
| 3451 |
return log_error_errno(r, "Failed to duplicate file descriptor of %s: %m", src); |
| 3452 |
|
| 3453 |
np->copy_blocks_offset = start; |
| 3454 |
np->copy_blocks_size = sz; |
| 3455 |
|
| 3456 |
r = fdisk_partition_get_attrs_as_uint64(p, &np->gpt_flags); |
| 3457 |
if (r < 0) |
| 3458 |
return log_error_errno(r, "Failed to get partition flags: %m"); |
| 3459 |
|
| 3460 |
LIST_INSERT_AFTER(partitions, context->partitions, last, np); |
| 3461 |
last = TAKE_PTR(np); |
| 3462 |
context->n_partitions++; |
| 3463 |
} |
| 3464 |
|
| 3465 |
return 0; |
| 3466 |
} |
| 3467 |
|
| 3468 |
static int context_copy_from(Context *context) { |
| 3469 |
int r; |
| 3470 |
|
| 3471 |
assert(context); |
| 3472 |
|
| 3473 |
STRV_FOREACH(src, arg_copy_from) { |
| 3474 |
r = context_copy_from_one(context, *src); |
| 3475 |
if (r < 0) |
| 3476 |
return r; |
| 3477 |
} |
| 3478 |
|
| 3479 |
return 0; |
| 3480 |
} |
| 3481 |
|
| 3482 |
static bool check_cross_def_ranges_valid(uint64_t a_min, uint64_t a_max, uint64_t b_min, uint64_t b_max) { |
| 3483 |
if (a_min == UINT64_MAX && b_min == UINT64_MAX) |
| 3484 |
return true; |
| 3485 |
|
| 3486 |
if (a_max == UINT64_MAX && b_max == UINT64_MAX) |
| 3487 |
return true; |
| 3488 |
|
| 3489 |
return MAX(a_min != UINT64_MAX ? a_min : 0, b_min != UINT64_MAX ? b_min : 0) <= MIN(a_max, b_max); |
| 3490 |
} |
| 3491 |
|
| 3492 |
static int supplement_find_target(const Context *context, const Partition *supplement, Partition **ret) { |
| 3493 |
int r; |
| 3494 |
|
| 3495 |
assert(context); |
| 3496 |
assert(supplement); |
| 3497 |
assert(ret); |
| 3498 |
|
| 3499 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 3500 |
_cleanup_free_ char *filename = NULL; |
| 3501 |
|
| 3502 |
if (p == supplement) |
| 3503 |
continue; |
| 3504 |
|
| 3505 |
r = path_extract_filename(p->definition_path, &filename); |
| 3506 |
if (r < 0) |
| 3507 |
return log_error_errno(r, |
| 3508 |
"Failed to extract filename from path '%s': %m", |
| 3509 |
p->definition_path); |
| 3510 |
|
| 3511 |
*ASSERT_PTR(endswith(filename, ".conf")) = 0; /* Remove the file extension */ |
| 3512 |
|
| 3513 |
if (!streq(supplement->supplement_for_name, filename)) |
| 3514 |
continue; |
| 3515 |
|
| 3516 |
if (p->supplement_for_name) |
| 3517 |
return log_syntax(NULL, LOG_ERR, supplement->definition_path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3518 |
"SupplementFor= target is itself configured as a supplement."); |
| 3519 |
|
| 3520 |
if (p->suppressing) |
| 3521 |
return log_syntax(NULL, LOG_ERR, supplement->definition_path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3522 |
"SupplementFor= target already has a supplement defined: %s", |
| 3523 |
p->suppressing->definition_path); |
| 3524 |
|
| 3525 |
*ret = p; |
| 3526 |
return 0; |
| 3527 |
} |
| 3528 |
|
| 3529 |
return log_syntax(NULL, LOG_ERR, supplement->definition_path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3530 |
"Couldn't find target partition for SupplementFor=%s", |
| 3531 |
supplement->supplement_for_name); |
| 3532 |
} |
| 3533 |
|
| 3534 |
static int context_read_definitions(Context *context) { |
| 3535 |
_cleanup_strv_free_ char **files = NULL; |
| 3536 |
Partition *last = LIST_FIND_TAIL(partitions, context->partitions); |
| 3537 |
const char *const *dirs; |
| 3538 |
int r; |
| 3539 |
|
| 3540 |
assert(context); |
| 3541 |
|
| 3542 |
(void) context_notify(context, PROGRESS_LOADING_DEFINITIONS, /* object= */ NULL, UINT_MAX); |
| 3543 |
|
| 3544 |
dirs = (const char* const*) (context->definitions ?: CONF_PATHS_STRV("repart.d")); |
| 3545 |
|
| 3546 |
r = conf_files_list_strv( |
| 3547 |
&files, |
| 3548 |
".conf", |
| 3549 |
context->definitions ? NULL : arg_root, |
| 3550 |
CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED|CONF_FILES_WARN|CONF_FILES_DONT_PREFIX_ROOT, |
| 3551 |
dirs); |
| 3552 |
if (r < 0) |
| 3553 |
return log_error_errno(r, "Failed to enumerate *.conf files: %m"); |
| 3554 |
|
| 3555 |
STRV_FOREACH(f, files) { |
| 3556 |
_cleanup_(partition_freep) Partition *p = NULL; |
| 3557 |
|
| 3558 |
p = partition_new(context); |
| 3559 |
if (!p) |
| 3560 |
return log_oom(); |
| 3561 |
|
| 3562 |
p->definition_path = strdup(*f); |
| 3563 |
if (!p->definition_path) |
| 3564 |
return log_oom(); |
| 3565 |
|
| 3566 |
r = partition_read_definition(context, p, *f, dirs); |
| 3567 |
if (r < 0) |
| 3568 |
return r; |
| 3569 |
if (r == 0) |
| 3570 |
continue; |
| 3571 |
|
| 3572 |
LIST_INSERT_AFTER(partitions, context->partitions, last, p); |
| 3573 |
last = TAKE_PTR(p); |
| 3574 |
context->n_partitions++; |
| 3575 |
} |
| 3576 |
|
| 3577 |
/* Check that each configured verity hash/data partition has a matching verity data/hash partition. */ |
| 3578 |
|
| 3579 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 3580 |
if (p->verity == VERITY_OFF) |
| 3581 |
continue; |
| 3582 |
|
| 3583 |
for (VerityMode mode = VERITY_OFF + 1; mode < _VERITY_MODE_MAX; mode++) { |
| 3584 |
Partition *q = NULL; |
| 3585 |
|
| 3586 |
if (p->verity == mode) |
| 3587 |
continue; |
| 3588 |
|
| 3589 |
if (p->siblings[mode]) |
| 3590 |
continue; |
| 3591 |
|
| 3592 |
r = find_verity_sibling(context, p, mode, &q); |
| 3593 |
if (r == -ENXIO) { |
| 3594 |
if (mode != VERITY_SIG) |
| 3595 |
return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3596 |
"Missing verity %s partition for verity %s partition with VerityMatchKey=%s.", |
| 3597 |
verity_mode_to_string(mode), verity_mode_to_string(p->verity), p->verity_match_key); |
| 3598 |
} else if (r == -ENOTUNIQ) |
| 3599 |
return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3600 |
"Multiple verity %s partitions found for verity %s partition with VerityMatchKey=%s.", |
| 3601 |
verity_mode_to_string(mode), verity_mode_to_string(p->verity), p->verity_match_key); |
| 3602 |
else if (r < 0) |
| 3603 |
return log_syntax(NULL, LOG_ERR, p->definition_path, 1, r, |
| 3604 |
"Failed to find verity %s partition for verity %s partition with VerityMatchKey=%s.", |
| 3605 |
verity_mode_to_string(mode), verity_mode_to_string(p->verity), p->verity_match_key); |
| 3606 |
|
| 3607 |
if (q) { |
| 3608 |
if (q->priority != p->priority) |
| 3609 |
return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3610 |
"Priority mismatch (%i != %i) for verity sibling partitions with VerityMatchKey=%s.", |
| 3611 |
p->priority, q->priority, p->verity_match_key); |
| 3612 |
|
| 3613 |
p->siblings[mode] = q; |
| 3614 |
} |
| 3615 |
} |
| 3616 |
} |
| 3617 |
|
| 3618 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 3619 |
if (!IN_SET(p->verity, VERITY_HASH, VERITY_SIG)) |
| 3620 |
continue; |
| 3621 |
|
| 3622 |
/* We check all verity siblings up until our current type and ensure that if we are using CopyBlocks= |
| 3623 |
* the previous ones are using the same type of CopyBlocks=. */ |
| 3624 |
for (VerityMode mode = VERITY_DATA; mode < p->verity; mode++) { |
| 3625 |
Partition *q = ASSERT_PTR(p->siblings[mode]); |
| 3626 |
|
| 3627 |
if (p->copy_blocks_auto && !q->copy_blocks_auto) |
| 3628 |
return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3629 |
"CopyBlocks=auto set with Verity=%s but Verity=%s partition does not set CopyBlocks=auto.", |
| 3630 |
verity_mode_to_string(p->verity), verity_mode_to_string(mode)); |
| 3631 |
|
| 3632 |
if (p->copy_blocks_path && !q->copy_blocks_path) |
| 3633 |
return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3634 |
"CopyBlocks= set with Verity=%s but Verity=%s partition does not set CopyBlocks=.", |
| 3635 |
verity_mode_to_string(p->verity), verity_mode_to_string(mode)); |
| 3636 |
} |
| 3637 |
} |
| 3638 |
|
| 3639 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 3640 |
Partition *dp; |
| 3641 |
|
| 3642 |
if (p->verity != VERITY_HASH) |
| 3643 |
continue; |
| 3644 |
|
| 3645 |
if (p->minimize == MINIMIZE_OFF) |
| 3646 |
continue; |
| 3647 |
|
| 3648 |
assert_se(dp = p->siblings[VERITY_DATA]); |
| 3649 |
|
| 3650 |
if (dp->minimize == MINIMIZE_OFF && !(dp->copy_blocks_path || dp->copy_blocks_auto)) |
| 3651 |
return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3652 |
"Minimize= set for verity hash partition but data partition does not set CopyBlocks= or Minimize=."); |
| 3653 |
} |
| 3654 |
|
| 3655 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 3656 |
Partition *tgt = NULL; |
| 3657 |
|
| 3658 |
if (!p->supplement_for_name) |
| 3659 |
continue; |
| 3660 |
|
| 3661 |
r = supplement_find_target(context, p, &tgt); |
| 3662 |
if (r < 0) |
| 3663 |
return r; |
| 3664 |
|
| 3665 |
if (tgt->copy_blocks_path || tgt->copy_blocks_auto || tgt->encrypt != ENCRYPT_OFF || |
| 3666 |
tgt->verity != VERITY_OFF) |
| 3667 |
return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3668 |
"SupplementFor= target uses CopyBlocks=/Encrypt=/Verity="); |
| 3669 |
|
| 3670 |
if (!check_cross_def_ranges_valid(p->size_min, p->size_max, tgt->size_min, tgt->size_max)) |
| 3671 |
return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3672 |
"SizeMinBytes= larger than SizeMaxBytes= when merged with SupplementFor= target."); |
| 3673 |
|
| 3674 |
if (!check_cross_def_ranges_valid(p->padding_min, p->padding_max, tgt->padding_min, tgt->padding_max)) |
| 3675 |
return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL), |
| 3676 |
"PaddingMinBytes= larger than PaddingMaxBytes= when merged with SupplementFor= target."); |
| 3677 |
|
| 3678 |
p->supplement_for = tgt; |
| 3679 |
tgt->suppressing = tgt->supplement_target_for = p; |
| 3680 |
} |
| 3681 |
|
| 3682 |
return 0; |
| 3683 |
} |
| 3684 |
|
| 3685 |
static int fdisk_ask_cb(struct fdisk_context *c, struct fdisk_ask *ask, void *data) { |
| 3686 |
_cleanup_free_ char *ids = NULL; |
| 3687 |
int r; |
| 3688 |
|
| 3689 |
if (sym_fdisk_ask_get_type(ask) != FDISK_ASKTYPE_STRING) |
| 3690 |
return -EINVAL; |
| 3691 |
|
| 3692 |
ids = new(char, SD_ID128_UUID_STRING_MAX); |
| 3693 |
if (!ids) |
| 3694 |
return -ENOMEM; |
| 3695 |
|
| 3696 |
r = sym_fdisk_ask_string_set_result(ask, sd_id128_to_uuid_string(*(sd_id128_t*) data, ids)); |
| 3697 |
if (r < 0) |
| 3698 |
return r; |
| 3699 |
|
| 3700 |
TAKE_PTR(ids); |
| 3701 |
return 0; |
| 3702 |
} |
| 3703 |
|
| 3704 |
static int fdisk_set_disklabel_id_by_uuid(struct fdisk_context *c, sd_id128_t id) { |
| 3705 |
int r; |
| 3706 |
|
| 3707 |
r = sym_fdisk_set_ask(c, fdisk_ask_cb, &id); |
| 3708 |
if (r < 0) |
| 3709 |
return r; |
| 3710 |
|
| 3711 |
r = sym_fdisk_set_disklabel_id(c); |
| 3712 |
if (r < 0) |
| 3713 |
return r; |
| 3714 |
|
| 3715 |
return sym_fdisk_set_ask(c, NULL, NULL); |
| 3716 |
} |
| 3717 |
|
| 3718 |
static int derive_uuid(sd_id128_t base, const char *token, sd_id128_t *ret) { |
| 3719 |
union { |
| 3720 |
uint8_t md[SHA256_DIGEST_SIZE]; |
| 3721 |
sd_id128_t id; |
| 3722 |
} result; |
| 3723 |
|
| 3724 |
assert(token); |
| 3725 |
assert(ret); |
| 3726 |
|
| 3727 |
/* Derive a new UUID from the specified UUID in a stable and reasonably safe way. Specifically, we |
| 3728 |
* calculate the HMAC-SHA256 of the specified token string, keyed by the supplied base (typically the |
| 3729 |
* machine ID). We use the machine ID as key (and not as cleartext!) of the HMAC operation since it's |
| 3730 |
* the machine ID we don't want to leak. */ |
| 3731 |
|
| 3732 |
hmac_sha256(base.bytes, sizeof(base.bytes), token, strlen(token), result.md); |
| 3733 |
|
| 3734 |
/* Take the first half, mark it as v4 UUID */ |
| 3735 |
assert_cc(sizeof(result.md) == sizeof(result.id) * 2); |
| 3736 |
*ret = id128_make_v4_uuid(result.id); |
| 3737 |
return 0; |
| 3738 |
} |
| 3739 |
|
| 3740 |
static void derive_salt(sd_id128_t base, const char *token, uint8_t ret[static SHA256_DIGEST_SIZE]) { |
| 3741 |
assert(token); |
| 3742 |
|
| 3743 |
hmac_sha256(base.bytes, sizeof(base.bytes), token, strlen(token), ret); |
| 3744 |
} |
| 3745 |
|
| 3746 |
static int context_load_fallback_metrics(Context *context) { |
| 3747 |
assert(context); |
| 3748 |
|
| 3749 |
context->sector_size = arg_sector_size > 0 ? arg_sector_size : 512; |
| 3750 |
context->grain_size = determine_grain_size(context->sector_size); |
| 3751 |
context->default_fs_sector_size = arg_sector_size > 0 ? arg_sector_size : DEFAULT_FILESYSTEM_SECTOR_SIZE; |
| 3752 |
return 1; /* Starting from scratch */ |
| 3753 |
} |
| 3754 |
|
| 3755 |
static int context_open_mode(Context *context) { |
| 3756 |
return ASSERT_PTR(context)->dry_run ? O_RDONLY : O_RDWR; |
| 3757 |
} |
| 3758 |
|
| 3759 |
static int context_load_partition_table(Context *context) { |
| 3760 |
_cleanup_close_ int fd = -EBADF; |
| 3761 |
_cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL; |
| 3762 |
_cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL; |
| 3763 |
uint64_t left_boundary = UINT64_MAX, first_lba, last_lba, nsectors; |
| 3764 |
_cleanup_free_ char *disk_uuid_string = NULL; |
| 3765 |
bool from_scratch = false; |
| 3766 |
sd_id128_t disk_uuid; |
| 3767 |
size_t n_partitions; |
| 3768 |
uint64_t grainsz, fs_secsz = DEFAULT_FILESYSTEM_SECTOR_SIZE; |
| 3769 |
int r; |
| 3770 |
|
| 3771 |
assert(context); |
| 3772 |
assert(context->node); |
| 3773 |
assert(!context->fdisk_context); |
| 3774 |
assert(context->fdisk_context_fd < 0); |
| 3775 |
assert(!context->free_areas); |
| 3776 |
assert(context->start == UINT64_MAX); |
| 3777 |
assert(context->end == UINT64_MAX); |
| 3778 |
assert(context->total == UINT64_MAX); |
| 3779 |
|
| 3780 |
context_notify(context, PROGRESS_LOADING_TABLE, /* object= */ NULL, UINT_MAX); |
| 3781 |
|
| 3782 |
c = sym_fdisk_new_context(); |
| 3783 |
if (!c) |
| 3784 |
return log_oom(); |
| 3785 |
|
| 3786 |
if (arg_sector_size > 0) { |
| 3787 |
fs_secsz = arg_sector_size; |
| 3788 |
r = sym_fdisk_save_user_sector_size(c, /* phy= */ 0, arg_sector_size); |
| 3789 |
} else { |
| 3790 |
uint32_t ssz; |
| 3791 |
struct stat st; |
| 3792 |
|
| 3793 |
r = context_open_and_lock_backing_fd( |
| 3794 |
context->node, |
| 3795 |
context->dry_run ? LOCK_SH : LOCK_EX, |
| 3796 |
context_open_mode(context), |
| 3797 |
&context->backing_fd); |
| 3798 |
if (r < 0) |
| 3799 |
return r; |
| 3800 |
|
| 3801 |
if (fstat(context->backing_fd, &st) < 0) |
| 3802 |
return log_error_errno(errno, "Failed to stat %s: %m", context->node); |
| 3803 |
|
| 3804 |
if (IN_SET(context->empty, EMPTY_REQUIRE, EMPTY_FORCE, EMPTY_CREATE) && S_ISREG(st.st_mode)) |
| 3805 |
/* Don't probe sector size from partition table if we are supposed to start from an empty disk */ |
| 3806 |
ssz = 512; |
| 3807 |
else { |
| 3808 |
/* Auto-detect sector size if not specified. */ |
| 3809 |
r = probe_sector_size_prefer_ioctl(context->backing_fd, &ssz); |
| 3810 |
if (r < 0) |
| 3811 |
return log_error_errno(r, "Failed to probe sector size of '%s': %m", context->node); |
| 3812 |
|
| 3813 |
/* If we found the sector size and we're operating on a block device, use it as the file |
| 3814 |
* system sector size as well, as we know its the sector size of the actual block device and |
| 3815 |
* not just the offset at which we found the GPT header. */ |
| 3816 |
if (r > 0 && S_ISBLK(st.st_mode)) { |
| 3817 |
log_debug("Probed sector size of %s is %" PRIu32 " bytes.", context->node, ssz); |
| 3818 |
fs_secsz = ssz; |
| 3819 |
} |
| 3820 |
} |
| 3821 |
|
| 3822 |
r = sym_fdisk_save_user_sector_size(c, /* phy= */ 0, ssz); |
| 3823 |
} |
| 3824 |
if (r < 0) |
| 3825 |
return log_error_errno(r, "Failed to set sector size: %m"); |
| 3826 |
|
| 3827 |
if (context->backing_fd < 0) { |
| 3828 |
fd = open(context->node, context_open_mode(context)|O_CLOEXEC); |
| 3829 |
if (fd < 0) |
| 3830 |
return log_error_errno(errno, "Failed to open backing node '%s': %m", context->node); |
| 3831 |
} |
| 3832 |
r = sym_fdisk_assign_device_by_fd( |
| 3833 |
c, |
| 3834 |
context->backing_fd >= 0 ? context->backing_fd : fd, |
| 3835 |
context->node, |
| 3836 |
context->dry_run); |
| 3837 |
if (r == -EINVAL && arg_size_auto) { |
| 3838 |
struct stat st; |
| 3839 |
|
| 3840 |
/* libfdisk returns EINVAL if opening a file of size zero. Let's check for that, and accept |
| 3841 |
* it if automatic sizing is requested. */ |
| 3842 |
|
| 3843 |
if (context->backing_fd < 0) |
| 3844 |
r = fstat(fd, &st); |
| 3845 |
else |
| 3846 |
r = fstat(context->backing_fd, &st); |
| 3847 |
if (r < 0) |
| 3848 |
return log_error_errno(errno, "Failed to stat block device '%s': %m", context->node); |
| 3849 |
|
| 3850 |
if (S_ISREG(st.st_mode) && st.st_size == 0) { |
| 3851 |
/* Use the fallback values if we have no better idea */ |
| 3852 |
context->sector_size = sym_fdisk_get_sector_size(c); |
| 3853 |
context->default_fs_sector_size = fs_secsz; |
| 3854 |
context->grain_size = determine_grain_size(context->sector_size); |
| 3855 |
return /* from_scratch= */ true; |
| 3856 |
} |
| 3857 |
|
| 3858 |
r = -EINVAL; |
| 3859 |
} |
| 3860 |
if (r < 0) |
| 3861 |
return log_error_errno(r, "Failed to open device '%s': %m", context->node); |
| 3862 |
|
| 3863 |
if (context->backing_fd < 0) { |
| 3864 |
/* If we have no fd referencing the device yet, make a copy of the fd now, so that we have one */ |
| 3865 |
r = context_open_and_lock_backing_fd(FORMAT_PROC_FD_PATH(sym_fdisk_get_devfd(c)), |
| 3866 |
context->dry_run ? LOCK_SH : LOCK_EX, |
| 3867 |
context_open_mode(context), |
| 3868 |
&context->backing_fd); |
| 3869 |
if (r < 0) |
| 3870 |
return r; |
| 3871 |
} |
| 3872 |
|
| 3873 |
/* The offsets/sizes libfdisk returns to us will be in multiple of the sector size of the |
| 3874 |
* device. This is typically 512, and sometimes 4096. Let's query libfdisk once for it, and then use |
| 3875 |
* it for all our needs. Note that the values we use ourselves always are in bytes though, thus mean |
| 3876 |
* the same thing universally. Also note that regardless what kind of sector size is in use we'll |
| 3877 |
* place partitions at multiples of 4K. */ |
| 3878 |
unsigned long secsz = sym_fdisk_get_sector_size(c); |
| 3879 |
|
| 3880 |
/* Insist on a power of two, and that it's a multiple of 512, i.e. the traditional sector size. */ |
| 3881 |
if (secsz < 512 || !ISPOWEROF2(secsz)) |
| 3882 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Sector size %lu is not a power of two larger than 512? Refusing.", secsz); |
| 3883 |
|
| 3884 |
/* Determine the grain size: by default at least 4K and a multiple of the sector size, but may be |
| 3885 |
* overridden via --grain-size=. */ |
| 3886 |
grainsz = determine_grain_size(secsz); |
| 3887 |
|
| 3888 |
log_debug("Sector size of device is %lu bytes. Using default filesystem sector size of %" PRIu64 " and grain size of %" PRIu64 ".", secsz, fs_secsz, grainsz); |
| 3889 |
|
| 3890 |
switch (context->empty) { |
| 3891 |
|
| 3892 |
case EMPTY_REFUSE: |
| 3893 |
/* Refuse empty disks, insist on an existing GPT partition table */ |
| 3894 |
if (!sym_fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT)) |
| 3895 |
return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has no GPT disk label, not repartitioning.", context->node); |
| 3896 |
|
| 3897 |
break; |
| 3898 |
|
| 3899 |
case EMPTY_REQUIRE: |
| 3900 |
/* Require an empty disk, refuse any existing partition table */ |
| 3901 |
r = sym_fdisk_has_label(c); |
| 3902 |
if (r < 0) |
| 3903 |
return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", context->node); |
| 3904 |
if (r > 0) |
| 3905 |
return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s already has a disk label, refusing.", context->node); |
| 3906 |
|
| 3907 |
from_scratch = true; |
| 3908 |
break; |
| 3909 |
|
| 3910 |
case EMPTY_ALLOW: |
| 3911 |
/* Allow both an empty disk and an existing partition table, but only GPT */ |
| 3912 |
r = sym_fdisk_has_label(c); |
| 3913 |
if (r < 0) |
| 3914 |
return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", context->node); |
| 3915 |
if (r > 0) { |
| 3916 |
if (!sym_fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT)) |
| 3917 |
return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has non-GPT disk label, not repartitioning.", context->node); |
| 3918 |
} else |
| 3919 |
from_scratch = true; |
| 3920 |
|
| 3921 |
break; |
| 3922 |
|
| 3923 |
case EMPTY_FORCE: |
| 3924 |
case EMPTY_CREATE: |
| 3925 |
/* Always reinitiaize the disk, don't consider what there was on the disk before */ |
| 3926 |
from_scratch = true; |
| 3927 |
break; |
| 3928 |
|
| 3929 |
default: |
| 3930 |
assert_not_reached(); |
| 3931 |
} |
| 3932 |
|
| 3933 |
if (from_scratch) { |
| 3934 |
r = sym_fdisk_create_disklabel(c, "gpt"); |
| 3935 |
if (r < 0) |
| 3936 |
return log_error_errno(r, "Failed to create GPT disk label: %m"); |
| 3937 |
|
| 3938 |
r = derive_uuid(context->seed, "disk-uuid", &disk_uuid); |
| 3939 |
if (r < 0) |
| 3940 |
return log_error_errno(r, "Failed to acquire disk GPT uuid: %m"); |
| 3941 |
|
| 3942 |
r = fdisk_set_disklabel_id_by_uuid(c, disk_uuid); |
| 3943 |
if (r < 0) |
| 3944 |
return log_error_errno(r, "Failed to set GPT disk label: %m"); |
| 3945 |
|
| 3946 |
goto add_initial_free_area; |
| 3947 |
} |
| 3948 |
|
| 3949 |
r = sym_fdisk_get_disklabel_id(c, &disk_uuid_string); |
| 3950 |
if (r < 0) |
| 3951 |
return log_error_errno(r, "Failed to get current GPT disk label UUID: %m"); |
| 3952 |
|
| 3953 |
r = id128_from_string_nonzero(disk_uuid_string, &disk_uuid); |
| 3954 |
if (r == -ENXIO) { |
| 3955 |
r = derive_uuid(context->seed, "disk-uuid", &disk_uuid); |
| 3956 |
if (r < 0) |
| 3957 |
return log_error_errno(r, "Failed to acquire disk GPT uuid: %m"); |
| 3958 |
|
| 3959 |
r = sym_fdisk_set_disklabel_id(c); |
| 3960 |
if (r < 0) |
| 3961 |
return log_error_errno(r, "Failed to set GPT disk label: %m"); |
| 3962 |
} else if (r < 0) |
| 3963 |
return log_error_errno(r, "Failed to parse current GPT disk label UUID: %m"); |
| 3964 |
|
| 3965 |
r = sym_fdisk_get_partitions(c, &t); |
| 3966 |
if (r < 0) |
| 3967 |
return log_error_errno(r, "Failed to acquire partition table: %m"); |
| 3968 |
|
| 3969 |
n_partitions = sym_fdisk_table_get_nents(t); |
| 3970 |
for (size_t i = 0; i < n_partitions; i++) { |
| 3971 |
_cleanup_free_ char *label_copy = NULL; |
| 3972 |
Partition *last = NULL; |
| 3973 |
struct fdisk_partition *p; |
| 3974 |
const char *label; |
| 3975 |
uint64_t sz, start; |
| 3976 |
bool found = false; |
| 3977 |
sd_id128_t ptid, id; |
| 3978 |
size_t partno; |
| 3979 |
|
| 3980 |
p = sym_fdisk_table_get_partition(t, i); |
| 3981 |
if (!p) |
| 3982 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata."); |
| 3983 |
|
| 3984 |
if (sym_fdisk_partition_is_used(p) <= 0) |
| 3985 |
continue; |
| 3986 |
|
| 3987 |
if (sym_fdisk_partition_has_start(p) <= 0 || |
| 3988 |
sym_fdisk_partition_has_size(p) <= 0 || |
| 3989 |
sym_fdisk_partition_has_partno(p) <= 0) |
| 3990 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a position, size or number."); |
| 3991 |
|
| 3992 |
r = fdisk_partition_get_type_as_id128(p, &ptid); |
| 3993 |
if (r < 0) |
| 3994 |
return log_error_errno(r, "Failed to query partition type UUID: %m"); |
| 3995 |
|
| 3996 |
r = fdisk_partition_get_uuid_as_id128(p, &id); |
| 3997 |
if (r < 0) |
| 3998 |
return log_error_errno(r, "Failed to query partition UUID: %m"); |
| 3999 |
|
| 4000 |
label = sym_fdisk_partition_get_name(p); |
| 4001 |
if (!isempty(label)) { |
| 4002 |
label_copy = strdup(label); |
| 4003 |
if (!label_copy) |
| 4004 |
return log_oom(); |
| 4005 |
} |
| 4006 |
|
| 4007 |
sz = sym_fdisk_partition_get_size(p); |
| 4008 |
assert(sz <= UINT64_MAX/secsz); |
| 4009 |
sz *= secsz; |
| 4010 |
|
| 4011 |
start = sym_fdisk_partition_get_start(p); |
| 4012 |
assert(start <= UINT64_MAX/secsz); |
| 4013 |
start *= secsz; |
| 4014 |
|
| 4015 |
partno = sym_fdisk_partition_get_partno(p); |
| 4016 |
|
| 4017 |
if (left_boundary == UINT64_MAX || left_boundary > start) |
| 4018 |
left_boundary = start; |
| 4019 |
|
| 4020 |
/* Assign this existing partition to the first partition of the right type that doesn't have |
| 4021 |
* an existing one assigned yet. */ |
| 4022 |
LIST_FOREACH(partitions, pp, context->partitions) { |
| 4023 |
last = pp; |
| 4024 |
|
| 4025 |
if (!sd_id128_equal(pp->type.uuid, ptid)) |
| 4026 |
continue; |
| 4027 |
|
| 4028 |
if (!pp->current_partition) { |
| 4029 |
pp->current_uuid = id; |
| 4030 |
pp->current_size = sz; |
| 4031 |
pp->offset = start; |
| 4032 |
pp->partno = partno; |
| 4033 |
pp->current_label = TAKE_PTR(label_copy); |
| 4034 |
|
| 4035 |
pp->current_partition = p; |
| 4036 |
sym_fdisk_ref_partition(p); |
| 4037 |
|
| 4038 |
r = determine_current_padding(c, t, p, secsz, grainsz, &pp->current_padding); |
| 4039 |
if (r < 0) |
| 4040 |
return r; |
| 4041 |
|
| 4042 |
if (pp->current_padding > 0) { |
| 4043 |
r = context_add_free_area(context, pp->current_padding, pp); |
| 4044 |
if (r < 0) |
| 4045 |
return r; |
| 4046 |
} |
| 4047 |
|
| 4048 |
found = true; |
| 4049 |
break; |
| 4050 |
} |
| 4051 |
} |
| 4052 |
|
| 4053 |
/* If we have no matching definition, create a new one. */ |
| 4054 |
if (!found) { |
| 4055 |
_cleanup_(partition_freep) Partition *np = NULL; |
| 4056 |
|
| 4057 |
np = partition_new(context); |
| 4058 |
if (!np) |
| 4059 |
return log_oom(); |
| 4060 |
|
| 4061 |
np->current_uuid = id; |
| 4062 |
np->type = gpt_partition_type_from_uuid(ptid); |
| 4063 |
np->current_size = sz; |
| 4064 |
np->offset = start; |
| 4065 |
np->partno = partno; |
| 4066 |
np->current_label = TAKE_PTR(label_copy); |
| 4067 |
|
| 4068 |
np->current_partition = p; |
| 4069 |
sym_fdisk_ref_partition(p); |
| 4070 |
|
| 4071 |
r = determine_current_padding(c, t, p, secsz, grainsz, &np->current_padding); |
| 4072 |
if (r < 0) |
| 4073 |
return r; |
| 4074 |
|
| 4075 |
if (np->current_padding > 0) { |
| 4076 |
r = context_add_free_area(context, np->current_padding, np); |
| 4077 |
if (r < 0) |
| 4078 |
return r; |
| 4079 |
} |
| 4080 |
|
| 4081 |
LIST_INSERT_AFTER(partitions, context->partitions, last, TAKE_PTR(np)); |
| 4082 |
context->n_partitions++; |
| 4083 |
} |
| 4084 |
} |
| 4085 |
|
| 4086 |
LIST_FOREACH(partitions, p, context->partitions) |
| 4087 |
if (PARTITION_SUPPRESSED(p) && PARTITION_EXISTS(p)) |
| 4088 |
p->supplement_for->suppressing = NULL; |
| 4089 |
|
| 4090 |
add_initial_free_area: |
| 4091 |
nsectors = sym_fdisk_get_nsectors(c); |
| 4092 |
assert(nsectors <= UINT64_MAX/secsz); |
| 4093 |
nsectors *= secsz; |
| 4094 |
|
| 4095 |
first_lba = sym_fdisk_get_first_lba(c); |
| 4096 |
assert(first_lba <= UINT64_MAX/secsz); |
| 4097 |
first_lba *= secsz; |
| 4098 |
|
| 4099 |
last_lba = sym_fdisk_get_last_lba(c); |
| 4100 |
assert(last_lba < UINT64_MAX); |
| 4101 |
last_lba++; |
| 4102 |
assert(last_lba <= UINT64_MAX/secsz); |
| 4103 |
last_lba *= secsz; |
| 4104 |
|
| 4105 |
assert(last_lba >= first_lba); |
| 4106 |
|
| 4107 |
if (left_boundary == UINT64_MAX) { |
| 4108 |
/* No partitions at all? Then the whole disk is up for grabs. */ |
| 4109 |
|
| 4110 |
first_lba = round_up_size(first_lba, grainsz); |
| 4111 |
last_lba = round_down_size(last_lba, grainsz); |
| 4112 |
|
| 4113 |
if (last_lba > first_lba) { |
| 4114 |
r = context_add_free_area(context, last_lba - first_lba, NULL); |
| 4115 |
if (r < 0) |
| 4116 |
return r; |
| 4117 |
} |
| 4118 |
} else { |
| 4119 |
/* Add space left of first partition */ |
| 4120 |
assert(left_boundary >= first_lba); |
| 4121 |
|
| 4122 |
first_lba = round_up_size(first_lba, grainsz); |
| 4123 |
left_boundary = round_down_size(left_boundary, grainsz); |
| 4124 |
last_lba = round_down_size(last_lba, grainsz); |
| 4125 |
|
| 4126 |
if (left_boundary > first_lba) { |
| 4127 |
r = context_add_free_area(context, left_boundary - first_lba, NULL); |
| 4128 |
if (r < 0) |
| 4129 |
return r; |
| 4130 |
} |
| 4131 |
} |
| 4132 |
|
| 4133 |
context->start = first_lba; |
| 4134 |
context->end = last_lba; |
| 4135 |
context->total = nsectors; |
| 4136 |
context->sector_size = secsz; |
| 4137 |
context->default_fs_sector_size = fs_secsz; |
| 4138 |
context->grain_size = grainsz; |
| 4139 |
context->fdisk_context = TAKE_PTR(c); |
| 4140 |
context->fdisk_context_fd = TAKE_FD(fd); |
| 4141 |
|
| 4142 |
return from_scratch; |
| 4143 |
} |
| 4144 |
|
| 4145 |
static void context_unload_partition_table(Context *context) { |
| 4146 |
assert(context); |
| 4147 |
|
| 4148 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 4149 |
|
| 4150 |
/* Entirely remove partitions that have no configuration */ |
| 4151 |
if (PARTITION_IS_FOREIGN(p)) { |
| 4152 |
partition_unlink_and_free(context, p); |
| 4153 |
continue; |
| 4154 |
} |
| 4155 |
|
| 4156 |
/* Otherwise drop all data we read off the block device and everything we might have |
| 4157 |
* calculated based on it */ |
| 4158 |
|
| 4159 |
p->dropped = false; |
| 4160 |
p->current_size = UINT64_MAX; |
| 4161 |
p->new_size = UINT64_MAX; |
| 4162 |
p->current_padding = UINT64_MAX; |
| 4163 |
p->new_padding = UINT64_MAX; |
| 4164 |
p->partno = UINT64_MAX; |
| 4165 |
p->offset = UINT64_MAX; |
| 4166 |
|
| 4167 |
if (p->current_partition) { |
| 4168 |
sym_fdisk_unref_partition(p->current_partition); |
| 4169 |
p->current_partition = NULL; |
| 4170 |
} |
| 4171 |
|
| 4172 |
if (p->new_partition) { |
| 4173 |
sym_fdisk_unref_partition(p->new_partition); |
| 4174 |
p->new_partition = NULL; |
| 4175 |
} |
| 4176 |
|
| 4177 |
p->padding_area = NULL; |
| 4178 |
p->allocated_to_area = NULL; |
| 4179 |
|
| 4180 |
p->current_uuid = SD_ID128_NULL; |
| 4181 |
p->current_label = mfree(p->current_label); |
| 4182 |
|
| 4183 |
/* A supplement partition is only ever un-suppressed if the existing partition table prevented |
| 4184 |
* us from suppressing it. So when unloading the partition table, we must re-suppress. */ |
| 4185 |
if (p->supplement_for) |
| 4186 |
p->supplement_for->suppressing = p; |
| 4187 |
} |
| 4188 |
|
| 4189 |
context->start = UINT64_MAX; |
| 4190 |
context->end = UINT64_MAX; |
| 4191 |
context->total = UINT64_MAX; |
| 4192 |
|
| 4193 |
if (context->fdisk_context) { |
| 4194 |
sym_fdisk_unref_context(context->fdisk_context); |
| 4195 |
context->fdisk_context = NULL; |
| 4196 |
} |
| 4197 |
context->fdisk_context_fd = safe_close(context->fdisk_context_fd); |
| 4198 |
|
| 4199 |
context_free_free_areas(context); |
| 4200 |
} |
| 4201 |
|
| 4202 |
static int format_size_change(uint64_t from, uint64_t to, char **ret) { |
| 4203 |
char *t; |
| 4204 |
|
| 4205 |
assert(ret); |
| 4206 |
|
| 4207 |
if (from != UINT64_MAX) { |
| 4208 |
if (from == to || to == UINT64_MAX) |
| 4209 |
t = strdup(FORMAT_BYTES(from)); |
| 4210 |
else |
| 4211 |
t = strjoin(FORMAT_BYTES(from), " ", glyph(GLYPH_ARROW_RIGHT), " ", FORMAT_BYTES(to)); |
| 4212 |
} else if (to != UINT64_MAX) |
| 4213 |
t = strjoin(glyph(GLYPH_ARROW_RIGHT), " ", FORMAT_BYTES(to)); |
| 4214 |
else { |
| 4215 |
*ret = NULL; |
| 4216 |
return 0; |
| 4217 |
} |
| 4218 |
|
| 4219 |
if (!t) |
| 4220 |
return log_oom(); |
| 4221 |
|
| 4222 |
*ret = t; |
| 4223 |
return 1; |
| 4224 |
} |
| 4225 |
|
| 4226 |
static const char *partition_label(const Partition *p) { |
| 4227 |
assert(p); |
| 4228 |
|
| 4229 |
if (p->new_label) |
| 4230 |
return p->new_label; |
| 4231 |
|
| 4232 |
if (p->current_label) |
| 4233 |
return p->current_label; |
| 4234 |
|
| 4235 |
return gpt_partition_type_uuid_to_string(p->type.uuid); |
| 4236 |
} |
| 4237 |
|
| 4238 |
static int volume_label(const Partition *p, char **ret) { |
| 4239 |
assert(p); |
| 4240 |
assert(ret); |
| 4241 |
|
| 4242 |
if (p->new_volume_label) |
| 4243 |
return strdup_to(ret, p->new_volume_label); |
| 4244 |
|
| 4245 |
const char *e = partition_label(p); |
| 4246 |
if (!e) |
| 4247 |
return -ENODATA; |
| 4248 |
|
| 4249 |
/* Let's prefix "luks-" for the label string used for LUKS superblocks. We do this so that the |
| 4250 |
* /dev/disk/by-label/ symlink to the LUKS volume and the file system inside it do not clash */ |
| 4251 |
char *j = strjoin("luks-", e); |
| 4252 |
if (!j) |
| 4253 |
return -ENOMEM; |
| 4254 |
|
| 4255 |
*ret = j; |
| 4256 |
return 0; |
| 4257 |
} |
| 4258 |
|
| 4259 |
static int context_dump_partitions(Context *context) { |
| 4260 |
_cleanup_(table_unrefp) Table *t = NULL; |
| 4261 |
uint64_t sum_padding = 0, sum_size = 0; |
| 4262 |
int r; |
| 4263 |
const size_t roothash_col = 14, dropin_files_col = 15, split_path_col = 16; |
| 4264 |
bool has_roothash = false, has_dropin_files = false, has_split_path = false; |
| 4265 |
|
| 4266 |
if (context->n_partitions == 0 && !sd_json_format_enabled(arg_json_format_flags)) { |
| 4267 |
log_info("Empty partition table."); |
| 4268 |
return 0; |
| 4269 |
} |
| 4270 |
|
| 4271 |
t = table_new("type", /* 0 */ |
| 4272 |
"label", |
| 4273 |
"uuid", |
| 4274 |
"part", |
| 4275 |
"file", |
| 4276 |
"node", |
| 4277 |
"offset", |
| 4278 |
"old size", |
| 4279 |
"raw size", |
| 4280 |
"size", |
| 4281 |
"old padding", /* 10 */ |
| 4282 |
"raw padding", |
| 4283 |
"padding", |
| 4284 |
"activity", |
| 4285 |
"roothash", |
| 4286 |
"drop-in files", |
| 4287 |
"split path"); |
| 4288 |
if (!t) |
| 4289 |
return log_oom(); |
| 4290 |
|
| 4291 |
/* For compatibility, use the original longer name for JSON output. */ |
| 4292 |
table_set_json_field_name(t, 3, "partno"); |
| 4293 |
|
| 4294 |
/* Starting in v257, these fields would be automatically formatted with underscores. This would have |
| 4295 |
* been a breaking change, so to avoid that let's hard-code their original names. */ |
| 4296 |
table_set_json_field_name(t, 15, "drop-in_files"); |
| 4297 |
|
| 4298 |
if (!DEBUG_LOGGING) { |
| 4299 |
if (!sd_json_format_enabled(arg_json_format_flags)) |
| 4300 |
(void) table_set_display(t, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4, |
| 4301 |
(size_t) 8, (size_t) 9, (size_t) 12, roothash_col, dropin_files_col, |
| 4302 |
split_path_col); |
| 4303 |
else |
| 4304 |
(void) table_set_display(t, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4, |
| 4305 |
(size_t) 5, (size_t) 6, (size_t) 7, (size_t) 8, (size_t) 10, |
| 4306 |
(size_t) 11, (size_t) 13, roothash_col, dropin_files_col, |
| 4307 |
split_path_col); |
| 4308 |
} |
| 4309 |
|
| 4310 |
(void) table_set_align_percent(t, table_get_cell(t, 0, 3), 100); |
| 4311 |
(void) table_set_align_percent(t, table_get_cell(t, 0, 6), 100); |
| 4312 |
(void) table_set_align_percent(t, table_get_cell(t, 0, 7), 100); |
| 4313 |
(void) table_set_align_percent(t, table_get_cell(t, 0, 8), 100); |
| 4314 |
(void) table_set_align_percent(t, table_get_cell(t, 0, 9), 100); |
| 4315 |
(void) table_set_align_percent(t, table_get_cell(t, 0, 11), 100); |
| 4316 |
(void) table_set_align_percent(t, table_get_cell(t, 0, 12), 100); |
| 4317 |
|
| 4318 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 4319 |
_cleanup_free_ char *size_change = NULL, *padding_change = NULL, *partname = NULL, *rh = NULL; |
| 4320 |
char uuid_buffer[SD_ID128_UUID_STRING_MAX]; |
| 4321 |
const char *label, *activity = NULL; |
| 4322 |
|
| 4323 |
if (p->dropped) |
| 4324 |
continue; |
| 4325 |
|
| 4326 |
if (p->current_size == UINT64_MAX) |
| 4327 |
activity = "create"; |
| 4328 |
else if (p->current_size != p->new_size) |
| 4329 |
activity = "resize"; |
| 4330 |
|
| 4331 |
label = partition_label(p); |
| 4332 |
partname = p->partno != UINT64_MAX ? sym_fdisk_partname(context->node, p->partno+1) : NULL; |
| 4333 |
|
| 4334 |
r = format_size_change(p->current_size, p->new_size, &size_change); |
| 4335 |
if (r < 0) |
| 4336 |
return r; |
| 4337 |
|
| 4338 |
r = format_size_change(p->current_padding, p->new_padding, &padding_change); |
| 4339 |
if (r < 0) |
| 4340 |
return r; |
| 4341 |
|
| 4342 |
if (p->new_size != UINT64_MAX) |
| 4343 |
sum_size += p->new_size; |
| 4344 |
if (p->new_padding != UINT64_MAX) |
| 4345 |
sum_padding += p->new_padding; |
| 4346 |
|
| 4347 |
if (p->verity != VERITY_OFF) { |
| 4348 |
Partition *hp = p->verity == VERITY_HASH ? p : p->siblings[VERITY_HASH]; |
| 4349 |
|
| 4350 |
rh = iovec_is_set(&hp->roothash) ? hexmem(hp->roothash.iov_base, hp->roothash.iov_len) : strdup("TBD"); |
| 4351 |
if (!rh) |
| 4352 |
return log_oom(); |
| 4353 |
} |
| 4354 |
|
| 4355 |
r = table_add_many( |
| 4356 |
t, |
| 4357 |
TABLE_STRING, gpt_partition_type_uuid_to_string_harder(p->type.uuid, uuid_buffer), |
| 4358 |
TABLE_STRING, empty_to_null(label) ?: "-", TABLE_SET_COLOR, empty_to_null(label) ? NULL : ansi_grey(), |
| 4359 |
TABLE_UUID, p->new_uuid_is_set ? p->new_uuid : p->current_uuid, |
| 4360 |
TABLE_UINT64, p->partno, |
| 4361 |
TABLE_PATH_BASENAME, p->definition_path, TABLE_SET_COLOR, p->definition_path ? NULL : ansi_grey(), |
| 4362 |
TABLE_STRING, partname ?: "-", TABLE_SET_COLOR, partname ? NULL : ansi_highlight(), |
| 4363 |
TABLE_UINT64, p->offset, |
| 4364 |
TABLE_UINT64, p->current_size == UINT64_MAX ? 0 : p->current_size, |
| 4365 |
TABLE_UINT64, p->new_size, |
| 4366 |
TABLE_STRING, size_change, TABLE_SET_COLOR, !p->partitions_next && sum_size > 0 ? ansi_underline() : NULL, |
| 4367 |
TABLE_UINT64, p->current_padding == UINT64_MAX ? 0 : p->current_padding, |
| 4368 |
TABLE_UINT64, p->new_padding, |
| 4369 |
TABLE_STRING, padding_change, TABLE_SET_COLOR, !p->partitions_next && sum_padding > 0 ? ansi_underline() : NULL, |
| 4370 |
TABLE_STRING, activity ?: "unchanged", |
| 4371 |
TABLE_STRING, rh, |
| 4372 |
TABLE_STRV, p->drop_in_files, |
| 4373 |
TABLE_STRING, empty_to_null(p->split_path) ?: "-"); |
| 4374 |
if (r < 0) |
| 4375 |
return table_log_add_error(r); |
| 4376 |
|
| 4377 |
has_roothash = has_roothash || !isempty(rh); |
| 4378 |
has_dropin_files = has_dropin_files || !strv_isempty(p->drop_in_files); |
| 4379 |
has_split_path = has_split_path || !isempty(p->split_path); |
| 4380 |
} |
| 4381 |
|
| 4382 |
if (!sd_json_format_enabled(arg_json_format_flags) && (sum_padding > 0 || sum_size > 0)) { |
| 4383 |
const char *a, *b; |
| 4384 |
|
| 4385 |
a = strjoina(glyph(GLYPH_SIGMA), " = ", FORMAT_BYTES(sum_size)); |
| 4386 |
b = strjoina(glyph(GLYPH_SIGMA), " = ", FORMAT_BYTES(sum_padding)); |
| 4387 |
|
| 4388 |
r = table_add_many( |
| 4389 |
t, |
| 4390 |
TABLE_EMPTY, |
| 4391 |
TABLE_EMPTY, |
| 4392 |
TABLE_EMPTY, |
| 4393 |
TABLE_EMPTY, |
| 4394 |
TABLE_EMPTY, |
| 4395 |
TABLE_EMPTY, |
| 4396 |
TABLE_EMPTY, |
| 4397 |
TABLE_EMPTY, |
| 4398 |
TABLE_EMPTY, |
| 4399 |
TABLE_STRING, a, |
| 4400 |
TABLE_EMPTY, |
| 4401 |
TABLE_EMPTY, |
| 4402 |
TABLE_STRING, b, |
| 4403 |
TABLE_EMPTY, |
| 4404 |
TABLE_EMPTY, |
| 4405 |
TABLE_EMPTY, |
| 4406 |
TABLE_EMPTY); |
| 4407 |
if (r < 0) |
| 4408 |
return table_log_add_error(r); |
| 4409 |
} |
| 4410 |
|
| 4411 |
if (!has_roothash) { |
| 4412 |
r = table_hide_column_from_display(t, roothash_col); |
| 4413 |
if (r < 0) |
| 4414 |
return log_error_errno(r, "Failed to set columns to display: %m"); |
| 4415 |
} |
| 4416 |
|
| 4417 |
if (!has_dropin_files) { |
| 4418 |
r = table_hide_column_from_display(t, dropin_files_col); |
| 4419 |
if (r < 0) |
| 4420 |
return log_error_errno(r, "Failed to set columns to display: %m"); |
| 4421 |
} |
| 4422 |
|
| 4423 |
if (!has_split_path) { |
| 4424 |
r = table_hide_column_from_display(t, split_path_col); |
| 4425 |
if (r < 0) |
| 4426 |
return log_error_errno(r, "Failed to set columns to display: %m"); |
| 4427 |
} |
| 4428 |
|
| 4429 |
return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend); |
| 4430 |
} |
| 4431 |
|
| 4432 |
static int context_bar_char_process_partition( |
| 4433 |
Context *context, |
| 4434 |
Partition *bar[], |
| 4435 |
size_t n, |
| 4436 |
Partition *p, |
| 4437 |
size_t **start_array, |
| 4438 |
size_t *n_start_array) { |
| 4439 |
|
| 4440 |
uint64_t from, to, total; |
| 4441 |
size_t x, y; |
| 4442 |
|
| 4443 |
assert(context); |
| 4444 |
assert(bar); |
| 4445 |
assert(n > 0); |
| 4446 |
assert(p); |
| 4447 |
assert(start_array); |
| 4448 |
assert(n_start_array); |
| 4449 |
|
| 4450 |
if (p->dropped) |
| 4451 |
return 0; |
| 4452 |
|
| 4453 |
assert(p->offset != UINT64_MAX); |
| 4454 |
assert(p->new_size != UINT64_MAX); |
| 4455 |
|
| 4456 |
from = p->offset; |
| 4457 |
to = from + p->new_size; |
| 4458 |
|
| 4459 |
assert(context->total > 0); |
| 4460 |
total = context->total; |
| 4461 |
|
| 4462 |
assert(from <= total); |
| 4463 |
x = from * n / total; |
| 4464 |
|
| 4465 |
assert(to <= total); |
| 4466 |
y = to * n / total; |
| 4467 |
|
| 4468 |
assert(x <= y); |
| 4469 |
assert(y <= n); |
| 4470 |
|
| 4471 |
for (size_t i = x; i < y; i++) |
| 4472 |
bar[i] = p; |
| 4473 |
|
| 4474 |
if (!GREEDY_REALLOC_APPEND(*start_array, *n_start_array, &x, 1)) |
| 4475 |
return log_oom(); |
| 4476 |
|
| 4477 |
return 1; |
| 4478 |
} |
| 4479 |
|
| 4480 |
static int partition_hint(const Partition *p, const char *node, char **ret) { |
| 4481 |
_cleanup_free_ char *buf = NULL; |
| 4482 |
const char *label; |
| 4483 |
sd_id128_t id; |
| 4484 |
|
| 4485 |
assert(ret); |
| 4486 |
|
| 4487 |
/* Tries really hard to find a suitable description for this partition */ |
| 4488 |
|
| 4489 |
if (p->definition_path) |
| 4490 |
return path_extract_filename(p->definition_path, ret); |
| 4491 |
|
| 4492 |
label = partition_label(p); |
| 4493 |
if (!isempty(label)) { |
| 4494 |
buf = strdup(label); |
| 4495 |
goto done; |
| 4496 |
} |
| 4497 |
|
| 4498 |
if (p->partno != UINT64_MAX) { |
| 4499 |
buf = sym_fdisk_partname(node, p->partno+1); |
| 4500 |
goto done; |
| 4501 |
} |
| 4502 |
|
| 4503 |
if (p->new_uuid_is_set) |
| 4504 |
id = p->new_uuid; |
| 4505 |
else if (!sd_id128_is_null(p->current_uuid)) |
| 4506 |
id = p->current_uuid; |
| 4507 |
else |
| 4508 |
id = p->type.uuid; |
| 4509 |
|
| 4510 |
buf = strdup(SD_ID128_TO_UUID_STRING(id)); |
| 4511 |
|
| 4512 |
done: |
| 4513 |
if (!buf) |
| 4514 |
return -ENOMEM; |
| 4515 |
|
| 4516 |
*ret = TAKE_PTR(buf); |
| 4517 |
return 0; |
| 4518 |
} |
| 4519 |
|
| 4520 |
static int context_dump_partition_bar(Context *context) { |
| 4521 |
_cleanup_free_ Partition **bar = NULL; |
| 4522 |
_cleanup_free_ size_t *start_array = NULL; |
| 4523 |
size_t n_start_array = 0; |
| 4524 |
Partition *last = NULL; |
| 4525 |
bool z = false; |
| 4526 |
size_t c, j = 0; |
| 4527 |
int r; |
| 4528 |
|
| 4529 |
assert_se((c = columns()) >= 2); |
| 4530 |
c -= 2; /* We do not use the leftmost and rightmost character cell */ |
| 4531 |
|
| 4532 |
bar = new0(Partition*, c); |
| 4533 |
if (!bar) |
| 4534 |
return log_oom(); |
| 4535 |
|
| 4536 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 4537 |
r = context_bar_char_process_partition(context, bar, c, p, &start_array, &n_start_array); |
| 4538 |
if (r < 0) |
| 4539 |
return r; |
| 4540 |
} |
| 4541 |
|
| 4542 |
putc(' ', stdout); |
| 4543 |
|
| 4544 |
for (size_t i = 0; i < c; i++) { |
| 4545 |
if (bar[i]) { |
| 4546 |
if (last != bar[i]) |
| 4547 |
z = !z; |
| 4548 |
|
| 4549 |
fputs(z ? ansi_green() : ansi_yellow(), stdout); |
| 4550 |
fputs(glyph(GLYPH_DARK_SHADE), stdout); |
| 4551 |
} else { |
| 4552 |
fputs(ansi_normal(), stdout); |
| 4553 |
fputs(glyph(GLYPH_LIGHT_SHADE), stdout); |
| 4554 |
} |
| 4555 |
|
| 4556 |
last = bar[i]; |
| 4557 |
} |
| 4558 |
|
| 4559 |
fputs(ansi_normal(), stdout); |
| 4560 |
putc('\n', stdout); |
| 4561 |
|
| 4562 |
for (size_t i = 0; i < n_start_array; i++) { |
| 4563 |
_cleanup_free_ char **line = NULL; |
| 4564 |
|
| 4565 |
line = new0(char*, c); |
| 4566 |
if (!line) |
| 4567 |
return log_oom(); |
| 4568 |
|
| 4569 |
j = 0; |
| 4570 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 4571 |
_cleanup_free_ char *d = NULL; |
| 4572 |
|
| 4573 |
if (p->dropped) |
| 4574 |
continue; |
| 4575 |
|
| 4576 |
j++; |
| 4577 |
|
| 4578 |
if (i < n_start_array - j) { |
| 4579 |
|
| 4580 |
if (line[start_array[j-1]]) { |
| 4581 |
const char *e; |
| 4582 |
|
| 4583 |
/* Upgrade final corner to the right with a branch to the right */ |
| 4584 |
e = startswith(line[start_array[j-1]], glyph(GLYPH_TREE_RIGHT)); |
| 4585 |
if (e) { |
| 4586 |
d = strjoin(glyph(GLYPH_TREE_BRANCH), e); |
| 4587 |
if (!d) |
| 4588 |
return log_oom(); |
| 4589 |
} |
| 4590 |
} |
| 4591 |
|
| 4592 |
if (!d) { |
| 4593 |
d = strdup(glyph(GLYPH_TREE_VERTICAL)); |
| 4594 |
if (!d) |
| 4595 |
return log_oom(); |
| 4596 |
} |
| 4597 |
|
| 4598 |
} else if (i == n_start_array - j) { |
| 4599 |
_cleanup_free_ char *hint = NULL; |
| 4600 |
|
| 4601 |
(void) partition_hint(p, context->node, &hint); |
| 4602 |
|
| 4603 |
if (streq_ptr(line[start_array[j-1]], glyph(GLYPH_TREE_VERTICAL))) |
| 4604 |
d = strjoin(glyph(GLYPH_TREE_BRANCH), " ", strna(hint)); |
| 4605 |
else |
| 4606 |
d = strjoin(glyph(GLYPH_TREE_RIGHT), " ", strna(hint)); |
| 4607 |
|
| 4608 |
if (!d) |
| 4609 |
return log_oom(); |
| 4610 |
} |
| 4611 |
|
| 4612 |
if (d) |
| 4613 |
free_and_replace(line[start_array[j-1]], d); |
| 4614 |
} |
| 4615 |
|
| 4616 |
putc(' ', stdout); |
| 4617 |
|
| 4618 |
j = 0; |
| 4619 |
while (j < c) { |
| 4620 |
if (line[j]) { |
| 4621 |
fputs(line[j], stdout); |
| 4622 |
j += utf8_console_width(line[j]); |
| 4623 |
} else { |
| 4624 |
putc(' ', stdout); |
| 4625 |
j++; |
| 4626 |
} |
| 4627 |
} |
| 4628 |
|
| 4629 |
putc('\n', stdout); |
| 4630 |
|
| 4631 |
for (j = 0; j < c; j++) |
| 4632 |
free(line[j]); |
| 4633 |
} |
| 4634 |
|
| 4635 |
return 0; |
| 4636 |
} |
| 4637 |
|
| 4638 |
static bool context_has_roothash(Context *context) { |
| 4639 |
LIST_FOREACH(partitions, p, context->partitions) |
| 4640 |
if (iovec_is_set(&p->roothash)) |
| 4641 |
return true; |
| 4642 |
|
| 4643 |
return false; |
| 4644 |
} |
| 4645 |
|
| 4646 |
static int context_dump(Context *context, bool late) { |
| 4647 |
int r; |
| 4648 |
|
| 4649 |
assert(context); |
| 4650 |
|
| 4651 |
if (arg_pretty == 0 && !sd_json_format_enabled(arg_json_format_flags)) |
| 4652 |
return 0; |
| 4653 |
|
| 4654 |
/* If we're outputting JSON, only dump after doing all operations so we can include the roothashes |
| 4655 |
* in the output. */ |
| 4656 |
if (!late && sd_json_format_enabled(arg_json_format_flags)) |
| 4657 |
return 0; |
| 4658 |
|
| 4659 |
/* If we're not outputting JSON, only dump again after doing all operations if there are any |
| 4660 |
* roothashes that we need to communicate to the user. */ |
| 4661 |
if (late && !sd_json_format_enabled(arg_json_format_flags) && !context_has_roothash(context)) |
| 4662 |
return 0; |
| 4663 |
|
| 4664 |
r = context_dump_partitions(context); |
| 4665 |
if (r < 0) |
| 4666 |
return r; |
| 4667 |
|
| 4668 |
/* Only write the partition bar once, even if we're writing the partition table twice to communicate |
| 4669 |
* roothashes. */ |
| 4670 |
if (!sd_json_format_enabled(arg_json_format_flags) && !late) { |
| 4671 |
putc('\n', stdout); |
| 4672 |
|
| 4673 |
r = context_dump_partition_bar(context); |
| 4674 |
if (r < 0) |
| 4675 |
return r; |
| 4676 |
|
| 4677 |
putc('\n', stdout); |
| 4678 |
} |
| 4679 |
|
| 4680 |
fflush(stdout); |
| 4681 |
|
| 4682 |
return 0; |
| 4683 |
} |
| 4684 |
|
| 4685 |
static bool context_changed(const Context *context) { |
| 4686 |
assert(context); |
| 4687 |
|
| 4688 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 4689 |
if (p->dropped) |
| 4690 |
continue; |
| 4691 |
|
| 4692 |
if (p->allocated_to_area) |
| 4693 |
return true; |
| 4694 |
|
| 4695 |
if (p->new_size != p->current_size) |
| 4696 |
return true; |
| 4697 |
} |
| 4698 |
|
| 4699 |
return false; |
| 4700 |
} |
| 4701 |
|
| 4702 |
static int context_wipe_range(Context *context, uint64_t offset, uint64_t size) { |
| 4703 |
#if HAVE_BLKID |
| 4704 |
_cleanup_(blkid_free_probep) blkid_probe probe = NULL; |
| 4705 |
int r; |
| 4706 |
|
| 4707 |
assert(context); |
| 4708 |
assert(offset != UINT64_MAX); |
| 4709 |
assert(size != UINT64_MAX); |
| 4710 |
|
| 4711 |
r = DLOPEN_LIBBLKID(LOG_ERR, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 4712 |
if (r < 0) |
| 4713 |
return r; |
| 4714 |
|
| 4715 |
probe = sym_blkid_new_probe(); |
| 4716 |
if (!probe) |
| 4717 |
return log_oom(); |
| 4718 |
|
| 4719 |
errno = 0; |
| 4720 |
r = sym_blkid_probe_set_device(probe, sym_fdisk_get_devfd(context->fdisk_context), offset, size); |
| 4721 |
if (r < 0) |
| 4722 |
return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to allocate device probe for wiping."); |
| 4723 |
|
| 4724 |
errno = 0; |
| 4725 |
if (sym_blkid_probe_enable_superblocks(probe, true) < 0 || |
| 4726 |
sym_blkid_probe_set_superblocks_flags(probe, BLKID_SUBLKS_MAGIC|BLKID_SUBLKS_BADCSUM) < 0 || |
| 4727 |
sym_blkid_probe_enable_partitions(probe, true) < 0 || |
| 4728 |
sym_blkid_probe_set_partitions_flags(probe, BLKID_PARTS_MAGIC) < 0) |
| 4729 |
return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to enable superblock and partition probing."); |
| 4730 |
|
| 4731 |
for (;;) { |
| 4732 |
errno = 0; |
| 4733 |
r = sym_blkid_do_probe(probe); |
| 4734 |
if (r < 0) |
| 4735 |
return log_error_errno(errno_or_else(EIO), "Failed to probe for file systems."); |
| 4736 |
if (r > 0) |
| 4737 |
break; |
| 4738 |
|
| 4739 |
errno = 0; |
| 4740 |
if (sym_blkid_do_wipe(probe, false) < 0) |
| 4741 |
return log_error_errno(errno_or_else(EIO), "Failed to wipe file system signature."); |
| 4742 |
} |
| 4743 |
|
| 4744 |
return 0; |
| 4745 |
#else |
| 4746 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 4747 |
"Cannot wipe partition signatures, libblkid support is not compiled in."); |
| 4748 |
#endif |
| 4749 |
} |
| 4750 |
|
| 4751 |
static int context_wipe_partition(Context *context, Partition *p) { |
| 4752 |
int r; |
| 4753 |
|
| 4754 |
assert(context); |
| 4755 |
assert(p); |
| 4756 |
assert(!PARTITION_EXISTS(p)); /* Safety check: never wipe existing partitions */ |
| 4757 |
|
| 4758 |
assert(p->offset != UINT64_MAX); |
| 4759 |
assert(p->new_size != UINT64_MAX); |
| 4760 |
|
| 4761 |
r = context_wipe_range(context, p->offset, p->new_size); |
| 4762 |
if (r < 0) |
| 4763 |
return r; |
| 4764 |
|
| 4765 |
log_info("Successfully wiped file system signatures from future partition %" PRIu64 ".", p->partno); |
| 4766 |
return 0; |
| 4767 |
} |
| 4768 |
|
| 4769 |
static int context_discard_range( |
| 4770 |
Context *context, |
| 4771 |
uint64_t offset, |
| 4772 |
uint64_t size) { |
| 4773 |
|
| 4774 |
struct stat st; |
| 4775 |
int fd; |
| 4776 |
|
| 4777 |
assert(context); |
| 4778 |
assert(offset != UINT64_MAX); |
| 4779 |
assert(size != UINT64_MAX); |
| 4780 |
|
| 4781 |
if (size <= 0) |
| 4782 |
return 0; |
| 4783 |
|
| 4784 |
assert_se((fd = sym_fdisk_get_devfd(context->fdisk_context)) >= 0); |
| 4785 |
|
| 4786 |
if (fstat(fd, &st) < 0) |
| 4787 |
return -errno; |
| 4788 |
|
| 4789 |
if (S_ISREG(st.st_mode)) { |
| 4790 |
if (fallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, offset, size) < 0) { |
| 4791 |
if (ERRNO_IS_NOT_SUPPORTED(errno)) |
| 4792 |
return -EOPNOTSUPP; |
| 4793 |
|
| 4794 |
return -errno; |
| 4795 |
} |
| 4796 |
|
| 4797 |
return 1; |
| 4798 |
} |
| 4799 |
|
| 4800 |
if (S_ISBLK(st.st_mode)) { |
| 4801 |
uint64_t range[2], end; |
| 4802 |
|
| 4803 |
range[0] = round_up_size(offset, context->sector_size); |
| 4804 |
|
| 4805 |
if (offset > UINT64_MAX - size) |
| 4806 |
return -ERANGE; |
| 4807 |
|
| 4808 |
end = offset + size; |
| 4809 |
if (end <= range[0]) |
| 4810 |
return 0; |
| 4811 |
|
| 4812 |
range[1] = round_down_size(end - range[0], context->sector_size); |
| 4813 |
if (range[1] <= 0) |
| 4814 |
return 0; |
| 4815 |
|
| 4816 |
if (ioctl(fd, BLKDISCARD, range) < 0) { |
| 4817 |
if (ERRNO_IS_NOT_SUPPORTED(errno)) |
| 4818 |
return -EOPNOTSUPP; |
| 4819 |
|
| 4820 |
return -errno; |
| 4821 |
} |
| 4822 |
|
| 4823 |
return 1; |
| 4824 |
} |
| 4825 |
|
| 4826 |
return -EOPNOTSUPP; |
| 4827 |
} |
| 4828 |
|
| 4829 |
static int context_discard_partition(Context *context, Partition *p) { |
| 4830 |
int r; |
| 4831 |
|
| 4832 |
assert(context); |
| 4833 |
assert(p); |
| 4834 |
|
| 4835 |
assert(p->offset != UINT64_MAX); |
| 4836 |
assert(p->new_size != UINT64_MAX); |
| 4837 |
assert(!PARTITION_EXISTS(p)); /* Safety check: never discard existing partitions */ |
| 4838 |
|
| 4839 |
if (!arg_discard) |
| 4840 |
return 0; |
| 4841 |
|
| 4842 |
r = context_discard_range(context, p->offset, p->new_size); |
| 4843 |
if (r == -EOPNOTSUPP) { |
| 4844 |
log_info("Storage does not support discard, not discarding data in future partition %" PRIu64 ".", p->partno); |
| 4845 |
return 0; |
| 4846 |
} |
| 4847 |
if (r == -EBUSY) { |
| 4848 |
/* Let's handle this gracefully: https://bugzilla.kernel.org/show_bug.cgi?id=211167 */ |
| 4849 |
log_info("Block device is busy, not discarding partition %" PRIu64 " because it probably is mounted.", p->partno); |
| 4850 |
return 0; |
| 4851 |
} |
| 4852 |
if (r == 0) { |
| 4853 |
log_info("Partition %" PRIu64 " too short for discard, skipping.", p->partno); |
| 4854 |
return 0; |
| 4855 |
} |
| 4856 |
if (r < 0) |
| 4857 |
return log_error_errno(r, "Failed to discard data for future partition %" PRIu64 ".", p->partno); |
| 4858 |
|
| 4859 |
log_info("Successfully discarded data from future partition %" PRIu64 ".", p->partno); |
| 4860 |
p->discarded = true; |
| 4861 |
return 1; |
| 4862 |
} |
| 4863 |
|
| 4864 |
static int context_discard_gap_after(Context *context, Partition *p) { |
| 4865 |
uint64_t gap, next = UINT64_MAX; |
| 4866 |
int r; |
| 4867 |
|
| 4868 |
assert(context); |
| 4869 |
assert(!p || (p->offset != UINT64_MAX && p->new_size != UINT64_MAX)); |
| 4870 |
|
| 4871 |
if (!arg_discard) |
| 4872 |
return 0; |
| 4873 |
|
| 4874 |
if (p) |
| 4875 |
gap = p->offset + p->new_size; |
| 4876 |
else |
| 4877 |
/* The context start gets rounded up to grain_size, however |
| 4878 |
* existing partitions may be before that so ensure the gap |
| 4879 |
* starts at the first actually usable lba |
| 4880 |
*/ |
| 4881 |
gap = sym_fdisk_get_first_lba(context->fdisk_context) * context->sector_size; |
| 4882 |
|
| 4883 |
LIST_FOREACH(partitions, q, context->partitions) { |
| 4884 |
if (q->dropped) |
| 4885 |
continue; |
| 4886 |
|
| 4887 |
assert(q->offset != UINT64_MAX); |
| 4888 |
assert(q->new_size != UINT64_MAX); |
| 4889 |
|
| 4890 |
if (q->offset < gap) |
| 4891 |
continue; |
| 4892 |
|
| 4893 |
if (next == UINT64_MAX || q->offset < next) |
| 4894 |
next = q->offset; |
| 4895 |
} |
| 4896 |
|
| 4897 |
if (next == UINT64_MAX) { |
| 4898 |
next = (sym_fdisk_get_last_lba(context->fdisk_context) + 1) * context->sector_size; |
| 4899 |
if (gap > next) |
| 4900 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end."); |
| 4901 |
} |
| 4902 |
|
| 4903 |
assert(next >= gap); |
| 4904 |
r = context_discard_range(context, gap, next - gap); |
| 4905 |
if (r == -EOPNOTSUPP) { |
| 4906 |
if (p) |
| 4907 |
log_info("Storage does not support discard, not discarding gap after partition %" PRIu64 ".", p->partno); |
| 4908 |
else |
| 4909 |
log_info("Storage does not support discard, not discarding gap at beginning of disk."); |
| 4910 |
return 0; |
| 4911 |
} |
| 4912 |
if (r == 0) /* Too short */ |
| 4913 |
return 0; |
| 4914 |
if (r < 0) { |
| 4915 |
if (p) |
| 4916 |
return log_error_errno(r, "Failed to discard gap after partition %" PRIu64 ".", p->partno); |
| 4917 |
else |
| 4918 |
return log_error_errno(r, "Failed to discard gap at beginning of disk."); |
| 4919 |
} |
| 4920 |
|
| 4921 |
if (p) |
| 4922 |
log_info("Successfully discarded gap after partition %" PRIu64 ".", p->partno); |
| 4923 |
else |
| 4924 |
log_info("Successfully discarded gap at beginning of disk."); |
| 4925 |
|
| 4926 |
return 0; |
| 4927 |
} |
| 4928 |
|
| 4929 |
static bool partition_defer(const Context *c, const Partition *p) { |
| 4930 |
assert(c); |
| 4931 |
assert(p); |
| 4932 |
|
| 4933 |
if (partition_type_defer(&p->type)) |
| 4934 |
return true; |
| 4935 |
|
| 4936 |
if (c->defer_partitions_empty && streq_ptr(p->new_label, "_empty")) |
| 4937 |
return true; |
| 4938 |
|
| 4939 |
if (c->defer_partitions_factory_reset && p->factory_reset) |
| 4940 |
return true; |
| 4941 |
|
| 4942 |
return false; |
| 4943 |
} |
| 4944 |
|
| 4945 |
static int context_wipe_and_discard(Context *context) { |
| 4946 |
int r; |
| 4947 |
|
| 4948 |
assert(context); |
| 4949 |
|
| 4950 |
if (context->empty == EMPTY_CREATE) /* If we just created the image, no need to wipe */ |
| 4951 |
return 0; |
| 4952 |
|
| 4953 |
/* Wipe and discard the contents of all partitions we are about to create. We skip the discarding if |
| 4954 |
* we were supposed to start from scratch anyway, as in that case we just discard the whole block |
| 4955 |
* device in one go early on. */ |
| 4956 |
|
| 4957 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 4958 |
|
| 4959 |
if (!p->allocated_to_area) |
| 4960 |
continue; |
| 4961 |
|
| 4962 |
if (partition_defer(context, p)) |
| 4963 |
continue; |
| 4964 |
|
| 4965 |
(void) context_notify(context, PROGRESS_WIPING_PARTITION, p->definition_path, UINT_MAX); |
| 4966 |
|
| 4967 |
r = context_wipe_partition(context, p); |
| 4968 |
if (r < 0) |
| 4969 |
return r; |
| 4970 |
|
| 4971 |
if (!context->from_scratch) { |
| 4972 |
r = context_discard_partition(context, p); |
| 4973 |
if (r < 0) |
| 4974 |
return r; |
| 4975 |
|
| 4976 |
r = context_discard_gap_after(context, p); |
| 4977 |
if (r < 0) |
| 4978 |
return r; |
| 4979 |
} |
| 4980 |
} |
| 4981 |
|
| 4982 |
if (!context->from_scratch) { |
| 4983 |
r = context_discard_gap_after(context, NULL); |
| 4984 |
if (r < 0) |
| 4985 |
return r; |
| 4986 |
} |
| 4987 |
|
| 4988 |
return 0; |
| 4989 |
} |
| 4990 |
|
| 4991 |
typedef struct DecryptedPartitionTarget { |
| 4992 |
int fd; |
| 4993 |
char *dm_name; |
| 4994 |
char *volume; |
| 4995 |
bool keep; |
| 4996 |
struct crypt_device *device; |
| 4997 |
} DecryptedPartitionTarget; |
| 4998 |
|
| 4999 |
static DecryptedPartitionTarget* decrypted_partition_target_free(DecryptedPartitionTarget *t) { |
| 5000 |
#if HAVE_LIBCRYPTSETUP |
| 5001 |
int r; |
| 5002 |
|
| 5003 |
if (!t) |
| 5004 |
return NULL; |
| 5005 |
|
| 5006 |
safe_close(t->fd); |
| 5007 |
|
| 5008 |
if (!t->keep) { |
| 5009 |
/* udev or so might access our block device in the background while we are done. Let's hence |
| 5010 |
* force detach the volume. We sync'ed before, hence this should be safe. */ |
| 5011 |
r = sym_crypt_deactivate_by_name(t->device, t->dm_name, CRYPT_DEACTIVATE_FORCE); |
| 5012 |
if (r < 0) |
| 5013 |
log_warning_errno(r, "Failed to deactivate LUKS device, ignoring: %m"); |
| 5014 |
} else |
| 5015 |
log_debug("Keeping encrypted device '%s' open.", t->dm_name); |
| 5016 |
|
| 5017 |
sym_crypt_free(t->device); |
| 5018 |
free(t->dm_name); |
| 5019 |
free(t->volume); |
| 5020 |
free(t); |
| 5021 |
#endif |
| 5022 |
return NULL; |
| 5023 |
} |
| 5024 |
|
| 5025 |
/* BlockPartition represents partitions that have been created with BLKPG */ |
| 5026 |
typedef struct { |
| 5027 |
int fd; |
| 5028 |
int whole_fd; /* not owned */ |
| 5029 |
int nr; |
| 5030 |
uint64_t offset; |
| 5031 |
uint64_t size; |
| 5032 |
char *node; |
| 5033 |
} BlockPartition; |
| 5034 |
|
| 5035 |
static BlockPartition* block_partition_free(BlockPartition *p) { |
| 5036 |
if (!p) |
| 5037 |
return NULL; |
| 5038 |
|
| 5039 |
safe_close(p->fd); |
| 5040 |
free(p->node); |
| 5041 |
return mfree(p); |
| 5042 |
} |
| 5043 |
|
| 5044 |
DEFINE_TRIVIAL_CLEANUP_FUNC(BlockPartition*, block_partition_free); |
| 5045 |
|
| 5046 |
typedef struct { |
| 5047 |
LoopDevice *loop; |
| 5048 |
BlockPartition *block_partition; |
| 5049 |
int fd; |
| 5050 |
char *path; |
| 5051 |
int whole_fd; |
| 5052 |
DecryptedPartitionTarget *decrypted; |
| 5053 |
} PartitionTarget; |
| 5054 |
|
| 5055 |
static int partition_target_fd(PartitionTarget *t) { |
| 5056 |
assert(t); |
| 5057 |
assert(t->loop || t->fd >= 0 || t->whole_fd >= 0 || t->block_partition); |
| 5058 |
|
| 5059 |
if (t->decrypted) |
| 5060 |
return t->decrypted->fd; |
| 5061 |
|
| 5062 |
if (t->loop) |
| 5063 |
return t->loop->fd; |
| 5064 |
|
| 5065 |
if (t->block_partition) |
| 5066 |
return t->block_partition->fd; |
| 5067 |
|
| 5068 |
if (t->fd >= 0) |
| 5069 |
return t->fd; |
| 5070 |
|
| 5071 |
return t->whole_fd; |
| 5072 |
} |
| 5073 |
|
| 5074 |
static const char* partition_target_path(PartitionTarget *t) { |
| 5075 |
assert(t); |
| 5076 |
assert(t->loop || t->path || t->block_partition); |
| 5077 |
|
| 5078 |
if (t->decrypted) |
| 5079 |
return t->decrypted->volume; |
| 5080 |
|
| 5081 |
if (t->loop) |
| 5082 |
return t->loop->node; |
| 5083 |
|
| 5084 |
if (t->block_partition) |
| 5085 |
return t->block_partition->node; |
| 5086 |
|
| 5087 |
return t->path; |
| 5088 |
} |
| 5089 |
|
| 5090 |
static PartitionTarget* partition_target_free(PartitionTarget *t) { |
| 5091 |
if (!t) |
| 5092 |
return NULL; |
| 5093 |
|
| 5094 |
decrypted_partition_target_free(t->decrypted); |
| 5095 |
loop_device_unref(t->loop); |
| 5096 |
safe_close(t->fd); |
| 5097 |
unlink_and_free(t->path); |
| 5098 |
block_partition_free(t->block_partition); |
| 5099 |
|
| 5100 |
return mfree(t); |
| 5101 |
} |
| 5102 |
|
| 5103 |
DEFINE_TRIVIAL_CLEANUP_FUNC(PartitionTarget*, partition_target_free); |
| 5104 |
|
| 5105 |
static int prepare_temporary_file(Context *context, PartitionTarget *t, uint64_t size) { |
| 5106 |
_cleanup_(unlink_and_freep) char *temp = NULL; |
| 5107 |
_cleanup_close_ int fd = -EBADF; |
| 5108 |
const char *vt; |
| 5109 |
unsigned attrs = 0; |
| 5110 |
int r; |
| 5111 |
|
| 5112 |
assert(context); |
| 5113 |
assert(t); |
| 5114 |
|
| 5115 |
r = var_tmp_dir(&vt); |
| 5116 |
if (r < 0) |
| 5117 |
return log_error_errno(r, "Could not determine temporary directory: %m"); |
| 5118 |
|
| 5119 |
temp = path_join(vt, "repart-XXXXXX"); |
| 5120 |
if (!temp) |
| 5121 |
return log_oom(); |
| 5122 |
|
| 5123 |
fd = mkostemp_safe(temp); |
| 5124 |
if (fd < 0) |
| 5125 |
return log_error_errno(fd, "Failed to create temporary file: %m"); |
| 5126 |
|
| 5127 |
if (context->fdisk_context) { |
| 5128 |
r = read_attr_fd(sym_fdisk_get_devfd(context->fdisk_context), &attrs); |
| 5129 |
if (r < 0 && !ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 5130 |
log_warning_errno(r, "Failed to read file attributes of %s, ignoring: %m", context->node); |
| 5131 |
|
| 5132 |
if (FLAGS_SET(attrs, FS_NOCOW_FL)) { |
| 5133 |
r = chattr_fd(fd, FS_NOCOW_FL, FS_NOCOW_FL); |
| 5134 |
if (r < 0 && !ERRNO_IS_IOCTL_NOT_SUPPORTED(r)) |
| 5135 |
return log_error_errno(r, "Failed to disable copy-on-write on %s: %m", temp); |
| 5136 |
} |
| 5137 |
} |
| 5138 |
|
| 5139 |
if (ftruncate(fd, size) < 0) |
| 5140 |
return log_error_errno(errno, "Failed to truncate temporary file to %s: %m", |
| 5141 |
FORMAT_BYTES(size)); |
| 5142 |
|
| 5143 |
t->fd = TAKE_FD(fd); |
| 5144 |
t->path = TAKE_PTR(temp); |
| 5145 |
|
| 5146 |
return 0; |
| 5147 |
} |
| 5148 |
|
| 5149 |
static bool loop_device_error_is_fatal(const Partition *p, int r) { |
| 5150 |
assert(p); |
| 5151 |
return arg_offline == 0 || (r != -ENOENT && !ERRNO_IS_PRIVILEGE(r)); |
| 5152 |
} |
| 5153 |
|
| 5154 |
static int partition_target_prepare( |
| 5155 |
Context *context, |
| 5156 |
Partition *p, |
| 5157 |
uint64_t size, |
| 5158 |
bool need_path, |
| 5159 |
PartitionTarget **ret) { |
| 5160 |
|
| 5161 |
_cleanup_(partition_target_freep) PartitionTarget *t = NULL; |
| 5162 |
_cleanup_(loop_device_unrefp) LoopDevice *d = NULL; |
| 5163 |
int whole_fd, r; |
| 5164 |
|
| 5165 |
assert(context); |
| 5166 |
assert(p); |
| 5167 |
assert(ret); |
| 5168 |
|
| 5169 |
assert_se((whole_fd = sym_fdisk_get_devfd(context->fdisk_context)) >= 0); |
| 5170 |
|
| 5171 |
t = new(PartitionTarget, 1); |
| 5172 |
if (!t) |
| 5173 |
return log_oom(); |
| 5174 |
*t = (PartitionTarget) { |
| 5175 |
.fd = -EBADF, |
| 5176 |
.whole_fd = -EBADF, |
| 5177 |
}; |
| 5178 |
|
| 5179 |
if (!need_path) { |
| 5180 |
if (lseek(whole_fd, p->offset, SEEK_SET) < 0) |
| 5181 |
return log_error_errno(errno, "Failed to seek to partition offset: %m"); |
| 5182 |
|
| 5183 |
t->whole_fd = whole_fd; |
| 5184 |
*ret = TAKE_PTR(t); |
| 5185 |
return 0; |
| 5186 |
} |
| 5187 |
|
| 5188 |
if (arg_offline <= 0) { |
| 5189 |
r = blockdev_partscan_enabled_fd(whole_fd); |
| 5190 |
if (r > 0) { |
| 5191 |
_cleanup_close_ int dev_fd = -EBADF; |
| 5192 |
_cleanup_free_ char *part_node = NULL; |
| 5193 |
_cleanup_(block_partition_freep) BlockPartition *b = NULL; |
| 5194 |
int nr; |
| 5195 |
|
| 5196 |
/* blkpg takes int for partition numbers */ |
| 5197 |
if (p->partno >= INT_MAX) |
| 5198 |
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Partition number %" PRIu64 " is too large for blkpg.", p->partno); |
| 5199 |
nr = p->partno + 1; |
| 5200 |
|
| 5201 |
part_node = sym_fdisk_partname(context->node, p->partno + 1); |
| 5202 |
if (!part_node) |
| 5203 |
return log_oom(); |
| 5204 |
|
| 5205 |
/* There is no corresponding call to block_device_remove_partition because we want to |
| 5206 |
* keep them alive if we succeed, and the rescan will remove them if possible if |
| 5207 |
* there is an error before writing the partition table. |
| 5208 |
*/ |
| 5209 |
log_debug("Adding partition '%s' (nr=%i, offset=%" PRIu64 " [align=%" PRIu64 "], size=%" PRIu64 " [align=%" PRIu64 "])", |
| 5210 |
part_node, nr, |
| 5211 |
p->offset, NATURAL_ALIGNMENT(p->offset), |
| 5212 |
size, NATURAL_ALIGNMENT(size)); |
| 5213 |
r = block_device_add_partition(whole_fd, nr, p->offset, size); |
| 5214 |
if (r < 0) |
| 5215 |
return log_error_errno(r, "Failed to create new partition '%s': %m", part_node); |
| 5216 |
|
| 5217 |
context->needs_rescan = true; |
| 5218 |
|
| 5219 |
dev_fd = open(part_node, O_RDWR|O_CLOEXEC|O_NOCTTY); |
| 5220 |
if (dev_fd < 0) { |
| 5221 |
r = -errno; |
| 5222 |
int q = block_device_remove_partition(whole_fd, nr); |
| 5223 |
if (q < 0) |
| 5224 |
log_warning_errno(q, "Error while removing block device partition '%s', ignoring: %m", part_node); |
| 5225 |
return log_error_errno(r, "Failed to open new partition '%s': %m", part_node); |
| 5226 |
} |
| 5227 |
|
| 5228 |
/* No need to flock for udev, the whole disk fd is already locked. */ |
| 5229 |
|
| 5230 |
b = new(BlockPartition, 1); |
| 5231 |
if (!b) { |
| 5232 |
r = block_device_remove_partition(whole_fd, nr); |
| 5233 |
if (r < 0) |
| 5234 |
log_warning_errno(r, "Error while removing block device partition '%s', ignoring: %m", part_node); |
| 5235 |
|
| 5236 |
return log_oom(); |
| 5237 |
} |
| 5238 |
|
| 5239 |
*b = (BlockPartition) { |
| 5240 |
.fd = TAKE_FD(dev_fd), |
| 5241 |
.whole_fd = whole_fd, |
| 5242 |
.nr = nr, |
| 5243 |
.offset = p->offset, |
| 5244 |
.size = size, |
| 5245 |
.node = TAKE_PTR(part_node), |
| 5246 |
}; |
| 5247 |
|
| 5248 |
t->block_partition = TAKE_PTR(b); |
| 5249 |
*ret = TAKE_PTR(t); |
| 5250 |
return 0; |
| 5251 |
} else { |
| 5252 |
if (!IN_SET(r, 0, -ENOTBLK)) |
| 5253 |
log_warning_errno(r, "Could not detect whether the device can be partitioned, assuming it cannot be: %m"); |
| 5254 |
/* Loopback block devices are not only useful to turn regular files into block devices, but |
| 5255 |
* also to cut out sections of block devices into new block devices. */ |
| 5256 |
r = loop_device_make(whole_fd, O_RDWR, p->offset, size, context->sector_size, /* loop_flags= */ 0, LOCK_EX, &d); |
| 5257 |
if (r < 0 && loop_device_error_is_fatal(p, r)) |
| 5258 |
return log_error_errno(r, "Failed to make loopback device of future partition %" PRIu64 ": %m", p->partno); |
| 5259 |
if (r >= 0) { |
| 5260 |
t->loop = TAKE_PTR(d); |
| 5261 |
*ret = TAKE_PTR(t); |
| 5262 |
return 0; |
| 5263 |
} |
| 5264 |
|
| 5265 |
log_debug_errno(r, "No access to loop devices, falling back to a regular file"); |
| 5266 |
} |
| 5267 |
} |
| 5268 |
|
| 5269 |
/* If we can't allocate a loop device, let's write to a regular file that we copy into the final |
| 5270 |
* image so we can run in containers and without needing root privileges. On filesystems with |
| 5271 |
* reflinking support, we can take advantage of this and just reflink the result into the image. |
| 5272 |
*/ |
| 5273 |
|
| 5274 |
r = prepare_temporary_file(context, t, size); |
| 5275 |
if (r < 0) |
| 5276 |
return r; |
| 5277 |
|
| 5278 |
*ret = TAKE_PTR(t); |
| 5279 |
|
| 5280 |
return 0; |
| 5281 |
} |
| 5282 |
|
| 5283 |
static int partition_target_grow(PartitionTarget *t, uint64_t size) { |
| 5284 |
int r; |
| 5285 |
|
| 5286 |
assert(t); |
| 5287 |
assert(!t->decrypted); |
| 5288 |
|
| 5289 |
if (t->loop) { |
| 5290 |
r = loop_device_refresh_size(t->loop, UINT64_MAX, size); |
| 5291 |
if (r < 0) |
| 5292 |
return log_error_errno(r, "Failed to refresh loopback device size: %m"); |
| 5293 |
} else if (t->block_partition) { |
| 5294 |
r = block_device_resize_partition(t->block_partition->whole_fd, t->block_partition->nr, t->block_partition->offset, size); |
| 5295 |
if (r < 0) |
| 5296 |
return log_error_errno(r, "Failed to resize partition %d: %m", t->block_partition->nr); |
| 5297 |
t->block_partition->size = size; |
| 5298 |
} else if (t->fd >= 0) { |
| 5299 |
if (ftruncate(t->fd, size) < 0) |
| 5300 |
return log_error_errno(errno, "Failed to grow '%s' to %s by truncation: %m", |
| 5301 |
t->path, FORMAT_BYTES(size)); |
| 5302 |
} |
| 5303 |
|
| 5304 |
return 0; |
| 5305 |
} |
| 5306 |
|
| 5307 |
static int partition_target_sync(Context *context, Partition *p, PartitionTarget *t) { |
| 5308 |
int whole_fd, r; |
| 5309 |
|
| 5310 |
assert(context); |
| 5311 |
assert(p); |
| 5312 |
assert(t); |
| 5313 |
|
| 5314 |
assert_se((whole_fd = sym_fdisk_get_devfd(context->fdisk_context)) >= 0); |
| 5315 |
|
| 5316 |
log_info("Syncing future partition %"PRIu64" contents to disk.", p->partno); |
| 5317 |
|
| 5318 |
if (t->decrypted && fsync(t->decrypted->fd) < 0) |
| 5319 |
return log_error_errno(errno, "Failed to sync changes to '%s': %m", t->decrypted->volume); |
| 5320 |
|
| 5321 |
if (t->loop) { |
| 5322 |
r = loop_device_sync(t->loop); |
| 5323 |
if (r < 0) |
| 5324 |
return log_error_errno(r, "Failed to sync loopback device: %m"); |
| 5325 |
} else if (t->block_partition) { |
| 5326 |
if (fsync(t->block_partition->fd) < 0) |
| 5327 |
return log_error_errno(errno, "Failed to sync blkpg partition: %m"); |
| 5328 |
} else if (t->fd >= 0) { |
| 5329 |
struct stat st; |
| 5330 |
|
| 5331 |
if (lseek(whole_fd, p->offset, SEEK_SET) < 0) |
| 5332 |
return log_error_errno(errno, "Failed to seek to partition offset: %m"); |
| 5333 |
|
| 5334 |
if (fstat(t->fd, &st) < 0) |
| 5335 |
return log_error_errno(errno, "Failed to stat temporary file: %m"); |
| 5336 |
|
| 5337 |
if (st.st_size > (off_t) p->new_size) |
| 5338 |
return log_error_errno(SYNTHETIC_ERRNO(ENOSPC), |
| 5339 |
"Partition %" PRIu64 "'s contents (%s) don't fit in the partition (%s).", |
| 5340 |
p->partno, FORMAT_BYTES(st.st_size), FORMAT_BYTES(p->new_size)); |
| 5341 |
|
| 5342 |
r = copy_bytes(t->fd, whole_fd, UINT64_MAX, COPY_REFLINK|COPY_HOLES|COPY_FSYNC|COPY_SEEK0_SOURCE); |
| 5343 |
if (r < 0) |
| 5344 |
return log_error_errno(r, "Failed to copy bytes to partition: %m"); |
| 5345 |
} else { |
| 5346 |
if (fsync(t->whole_fd) < 0) |
| 5347 |
return log_error_errno(errno, "Failed to sync changes: %m"); |
| 5348 |
} |
| 5349 |
|
| 5350 |
return 0; |
| 5351 |
} |
| 5352 |
|
| 5353 |
/* libcryptsetup uses its own names for integrity algorithms, e.g. 'hmac(sha1)' but systemd |
| 5354 |
* prefers more standardized 'hmac-sha1', do the conversion here. Default to hmac(sha256). */ |
| 5355 |
static const char* dmcrypt_integrity_alg_name(Partition *p) { |
| 5356 |
if (p->integrity != INTEGRITY_INLINE) |
| 5357 |
return NULL; |
| 5358 |
|
| 5359 |
switch (p->integrity_alg) { |
| 5360 |
case INTEGRITY_ALG_HMAC_SHA1: |
| 5361 |
return "hmac(sha1)"; |
| 5362 |
case INTEGRITY_ALG_HMAC_SHA512: |
| 5363 |
return "hmac(sha512)"; |
| 5364 |
case INTEGRITY_ALG_HMAC_SHA256: |
| 5365 |
default: |
| 5366 |
return "hmac(sha256)"; |
| 5367 |
} |
| 5368 |
} |
| 5369 |
|
| 5370 |
/* Integrity puts specific limitations on the key size depending on the algorithm */ |
| 5371 |
static size_t dmcrypt_proper_key_size(Partition *p) { |
| 5372 |
if (p->integrity != INTEGRITY_INLINE) |
| 5373 |
return VOLUME_KEY_SIZE; |
| 5374 |
|
| 5375 |
switch (p->integrity_alg) { |
| 5376 |
case INTEGRITY_ALG_HMAC_SHA1: |
| 5377 |
return 672/8; |
| 5378 |
case INTEGRITY_ALG_HMAC_SHA512: |
| 5379 |
return 1024/8; |
| 5380 |
case INTEGRITY_ALG_HMAC_SHA256: |
| 5381 |
default: |
| 5382 |
return 768/8; |
| 5383 |
} |
| 5384 |
} |
| 5385 |
|
| 5386 |
static int partition_encrypt(Context *context, Partition *p, PartitionTarget *target, bool offline, bool temporary) { |
| 5387 |
#if HAVE_LIBCRYPTSETUP |
| 5388 |
#if HAVE_TPM2 |
| 5389 |
_cleanup_(erase_and_freep) char *base64_encoded = NULL; |
| 5390 |
#endif |
| 5391 |
_cleanup_fclose_ FILE *h = NULL; |
| 5392 |
_cleanup_free_ char *hp = NULL, *vol = NULL, *dm_name = NULL; |
| 5393 |
const char *passphrase = NULL; |
| 5394 |
const size_t volume_key_size = dmcrypt_proper_key_size(p); |
| 5395 |
size_t passphrase_size = 0; |
| 5396 |
const char *vt; |
| 5397 |
int r; |
| 5398 |
|
| 5399 |
assert(context); |
| 5400 |
assert(p); |
| 5401 |
assert(p->encrypt != ENCRYPT_OFF); |
| 5402 |
|
| 5403 |
r = DLOPEN_CRYPTSETUP(LOG_ERR, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED); |
| 5404 |
if (r < 0) |
| 5405 |
return r; |
| 5406 |
|
| 5407 |
log_info("Encrypting future partition %" PRIu64 "...", p->partno); |
| 5408 |
|
| 5409 |
_cleanup_free_ char *vl = NULL; |
| 5410 |
r = volume_label(p, &vl); |
| 5411 |
if (r < 0) |
| 5412 |
return log_error_errno(r, "Failed to generate volume label: %m"); |
| 5413 |
|
| 5414 |
const char *node = partition_target_path(target); |
| 5415 |
struct crypt_params_luks2 luks_params = { |
| 5416 |
.label = vl, |
| 5417 |
.sector_size = partition_fs_sector_size(context, p), |
| 5418 |
.data_device = offline ? node : NULL, |
| 5419 |
.integrity = dmcrypt_integrity_alg_name(p), |
| 5420 |
}; |
| 5421 |
struct crypt_params_reencrypt reencrypt_params = { |
| 5422 |
.mode = CRYPT_REENCRYPT_ENCRYPT, |
| 5423 |
.direction = CRYPT_REENCRYPT_BACKWARD, |
| 5424 |
.resilience = "datashift", |
| 5425 |
.data_shift = LUKS2_METADATA_SIZE / 512, |
| 5426 |
.luks2 = &luks_params, |
| 5427 |
.flags = CRYPT_REENCRYPT_INITIALIZE_ONLY|CRYPT_REENCRYPT_MOVE_FIRST_SEGMENT, |
| 5428 |
}; |
| 5429 |
|
| 5430 |
if (offline) { |
| 5431 |
/* libcryptsetup does not currently support reencryption of devices with integrity profiles.*/ |
| 5432 |
if (p->integrity == INTEGRITY_INLINE) |
| 5433 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Integrity=inline cannot be enabled in offline mode."); |
| 5434 |
|
| 5435 |
r = var_tmp_dir(&vt); |
| 5436 |
if (r < 0) |
| 5437 |
return log_error_errno(r, "Failed to determine temporary files directory: %m"); |
| 5438 |
|
| 5439 |
r = fopen_temporary_child(vt, &h, &hp); |
| 5440 |
if (r < 0) |
| 5441 |
return log_error_errno(r, "Failed to create temporary LUKS header file: %m"); |
| 5442 |
|
| 5443 |
/* Weird cryptsetup requirement which requires the header file to be the size of at least one |
| 5444 |
* sector. */ |
| 5445 |
if (ftruncate(fileno(h), luks_params.sector_size) < 0) |
| 5446 |
return log_error_errno(errno, "Failed to grow temporary LUKS header file: %m"); |
| 5447 |
} else { |
| 5448 |
if (!temporary && p->volume_name) |
| 5449 |
dm_name = strdup(p->volume_name); |
| 5450 |
else if (!temporary && filename_is_valid(vl)) |
| 5451 |
dm_name = strdup(vl); |
| 5452 |
else |
| 5453 |
dm_name = asprintf_safe("luks-repart-%08" PRIx64, random_u64()); |
| 5454 |
if (!dm_name) |
| 5455 |
return log_oom(); |
| 5456 |
|
| 5457 |
vol = path_join("/dev/mapper/", dm_name); |
| 5458 |
if (!vol) |
| 5459 |
return log_oom(); |
| 5460 |
} |
| 5461 |
|
| 5462 |
_cleanup_(crypt_freep) struct crypt_device *cd = NULL; |
| 5463 |
r = sym_crypt_init(&cd, offline ? hp : node); |
| 5464 |
if (r < 0) |
| 5465 |
return log_error_errno(r, "Failed to allocate libcryptsetup context for %s: %m", hp); |
| 5466 |
|
| 5467 |
cryptsetup_enable_logging(cd); |
| 5468 |
|
| 5469 |
if (offline) { |
| 5470 |
/* Disable kernel keyring usage by libcryptsetup as a workaround for |
| 5471 |
* https://gitlab.com/cryptsetup/cryptsetup/-/merge_requests/273. This makes sure that we can |
| 5472 |
* do offline encryption even when repart is running in a container. */ |
| 5473 |
r = sym_crypt_volume_key_keyring(cd, false); |
| 5474 |
if (r < 0) |
| 5475 |
return log_error_errno(r, "Failed to disable kernel keyring: %m"); |
| 5476 |
|
| 5477 |
r = sym_crypt_metadata_locking(cd, false); |
| 5478 |
if (r < 0) |
| 5479 |
return log_error_errno(r, "Failed to disable metadata locking: %m"); |
| 5480 |
|
| 5481 |
r = sym_crypt_set_data_offset(cd, LUKS2_METADATA_SIZE / 512); |
| 5482 |
if (r < 0) |
| 5483 |
return log_error_errno(r, "Failed to set data offset: %m"); |
| 5484 |
} |
| 5485 |
|
| 5486 |
r = sym_crypt_format( |
| 5487 |
cd, |
| 5488 |
CRYPT_LUKS2, |
| 5489 |
"aes", |
| 5490 |
"xts-plain64", |
| 5491 |
SD_ID128_TO_UUID_STRING(p->luks_uuid), |
| 5492 |
NULL, |
| 5493 |
/* volume_key_size= */ volume_key_size, |
| 5494 |
&luks_params); |
| 5495 |
if (r < 0) |
| 5496 |
return log_error_errno(r, "Failed to LUKS2 format future partition: %m"); |
| 5497 |
|
| 5498 |
/* If an explicit KDF is configured, apply it before adding any keyslots. */ |
| 5499 |
if (p->encrypt_kdf >= 0) { |
| 5500 |
if (p->encrypt_kdf == ENCRYPT_KDF_MINIMAL) { |
| 5501 |
/* Minimal PBKDF2 with sha512, 1000 iterations, no benchmarking — appropriate |
| 5502 |
* for high-entropy keys where the KDF only satisfies the LUKS2 format requirement |
| 5503 |
* (e.g. kdump with crashkernel=512MB). */ |
| 5504 |
r = cryptsetup_set_minimal_pbkdf(cd); |
| 5505 |
} else { |
| 5506 |
/* For argon2id or pbkdf2, set the type and let libcryptsetup benchmark |
| 5507 |
* and determine the parameters. */ |
| 5508 |
static const struct crypt_pbkdf_type argon2id_pbkdf = { |
| 5509 |
.type = "argon2id", |
| 5510 |
}; |
| 5511 |
static const struct crypt_pbkdf_type pbkdf2_pbkdf = { |
| 5512 |
.type = "pbkdf2", |
| 5513 |
}; |
| 5514 |
|
| 5515 |
r = sym_crypt_set_pbkdf_type(cd, p->encrypt_kdf == ENCRYPT_KDF_ARGON2ID ? |
| 5516 |
&argon2id_pbkdf : &pbkdf2_pbkdf); |
| 5517 |
} |
| 5518 |
if (r < 0) |
| 5519 |
return log_error_errno(r, "Failed to set KDF type: %m"); |
| 5520 |
} |
| 5521 |
|
| 5522 |
bool allow_discards = p->integrity != INTEGRITY_INLINE && (arg_discard ? p->discard != 0 : p->discard > 0); |
| 5523 |
if (allow_discards) { |
| 5524 |
uint32_t flags; |
| 5525 |
|
| 5526 |
r = sym_crypt_persistent_flags_get(cd, CRYPT_FLAGS_ACTIVATION, &flags); |
| 5527 |
if (r < 0) |
| 5528 |
return log_error_errno(r, "Failed to get persistent activation flags for %s: %m", node); |
| 5529 |
|
| 5530 |
if (!FLAGS_SET(flags, CRYPT_ACTIVATE_ALLOW_DISCARDS)) { |
| 5531 |
flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS; |
| 5532 |
r = sym_crypt_persistent_flags_set(cd, CRYPT_FLAGS_ACTIVATION, flags); |
| 5533 |
if (r < 0) |
| 5534 |
return log_error_errno(r, "Failed to set persistent activation flags for %s: %m", node); |
| 5535 |
} |
| 5536 |
} |
| 5537 |
|
| 5538 |
if (p->encrypted_volume && p->encrypted_volume->fixate_volume_key) { |
| 5539 |
_cleanup_free_ char *key_id = NULL, *hash_option = NULL; |
| 5540 |
|
| 5541 |
r = sym_crypt_get_volume_key_size(cd); |
| 5542 |
if (r < 0) |
| 5543 |
return log_error_errno(r, "Failed to determine volume key size: %m"); |
| 5544 |
if (r == 0) |
| 5545 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume key has zero size and 'fixate-volume-key' is used"); |
| 5546 |
|
| 5547 |
_cleanup_(iovec_done) struct iovec vk = { |
| 5548 |
.iov_base = malloc(r), |
| 5549 |
.iov_len = r, |
| 5550 |
}; |
| 5551 |
|
| 5552 |
if (!vk.iov_base) |
| 5553 |
return log_oom(); |
| 5554 |
|
| 5555 |
r = sym_crypt_volume_key_get(cd, CRYPT_ANY_SLOT, (char *) vk.iov_base, &vk.iov_len, NULL, 0); |
| 5556 |
if (r < 0) |
| 5557 |
return log_error_errno(r, "Failed to get volume key: %m"); |
| 5558 |
|
| 5559 |
r = cryptsetup_get_volume_key_id( |
| 5560 |
cd, |
| 5561 |
/* volume_name= */ p->encrypted_volume->name, |
| 5562 |
/* volume_key= */ vk.iov_base, |
| 5563 |
/* volume_key_size= */ vk.iov_len, |
| 5564 |
/* ret= */ &key_id); |
| 5565 |
if (r < 0) |
| 5566 |
return log_error_errno(r, "Failed to get volume key hash: %m"); |
| 5567 |
|
| 5568 |
hash_option = strjoin("fixate-volume-key=", key_id); |
| 5569 |
if (!hash_option) |
| 5570 |
return log_oom(); |
| 5571 |
|
| 5572 |
if (!strextend_with_separator(&p->encrypted_volume->options, ",", hash_option)) |
| 5573 |
return log_oom(); |
| 5574 |
} |
| 5575 |
|
| 5576 |
if (IN_SET(p->encrypt, ENCRYPT_KEY_FILE, ENCRYPT_KEY_FILE_TPM2)) { |
| 5577 |
/* Use partition-specific key if available, otherwise fall back to global key */ |
| 5578 |
struct iovec *iovec_key = arg_key.iov_base ? &arg_key : &p->key; |
| 5579 |
|
| 5580 |
r = sym_crypt_keyslot_add_by_volume_key( |
| 5581 |
cd, |
| 5582 |
CRYPT_ANY_SLOT, |
| 5583 |
NULL, |
| 5584 |
/* volume_key_size= */ volume_key_size, |
| 5585 |
strempty(iovec_key->iov_base), |
| 5586 |
iovec_key->iov_len); |
| 5587 |
if (r < 0) |
| 5588 |
return log_error_errno(r, "Failed to add LUKS2 key: %m"); |
| 5589 |
|
| 5590 |
passphrase = strempty(iovec_key->iov_base); |
| 5591 |
passphrase_size = iovec_key->iov_len; |
| 5592 |
} |
| 5593 |
|
| 5594 |
if (IN_SET(p->encrypt, ENCRYPT_TPM2, ENCRYPT_KEY_FILE_TPM2)) { |
| 5595 |
#if HAVE_TPM2 |
| 5596 |
_cleanup_(iovec_done) struct iovec pubkey = {}, srk = {}; |
| 5597 |
_cleanup_(iovec_done_erase) struct iovec secret = {}; |
| 5598 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 5599 |
ssize_t base64_encoded_size; |
| 5600 |
int keyslot; |
| 5601 |
TPM2Flags flags = 0; |
| 5602 |
Tpm2PCRValue *pcr_values = arg_tpm2_n_hash_pcr_values > 0 ? arg_tpm2_hash_pcr_values : p->tpm2_hash_pcr_values; |
| 5603 |
size_t n_pcr_values = arg_tpm2_n_hash_pcr_values > 0 ? arg_tpm2_n_hash_pcr_values : p->tpm2_n_hash_pcr_values; |
| 5604 |
|
| 5605 |
if (n_pcr_values == 0 && |
| 5606 |
arg_tpm2_public_key_pcr_mask == 0 && |
| 5607 |
!arg_tpm2_pcrlock) |
| 5608 |
log_notice("Notice: encrypting future partition %" PRIu64 ", locking against TPM2 with an empty policy, i.e. without any state or access restrictions.\n" |
| 5609 |
"Use --tpm2-public-key=, --tpm2-pcrlock=, or --tpm2-pcrs= to enable one or more restrictions.", p->partno); |
| 5610 |
|
| 5611 |
if (arg_tpm2_public_key_pcr_mask != 0) { |
| 5612 |
r = tpm2_load_pcr_public_key(arg_tpm2_public_key, &pubkey.iov_base, &pubkey.iov_len); |
| 5613 |
if (r < 0) { |
| 5614 |
if (arg_tpm2_public_key || r != -ENOENT) |
| 5615 |
return log_error_errno(r, "Failed to read TPM PCR public key: %m"); |
| 5616 |
|
| 5617 |
log_debug_errno(r, "Failed to read TPM2 PCR public key, proceeding without: %m"); |
| 5618 |
arg_tpm2_public_key_pcr_mask = 0; |
| 5619 |
} |
| 5620 |
} |
| 5621 |
|
| 5622 |
TPM2B_PUBLIC public; |
| 5623 |
if (iovec_is_set(&pubkey)) { |
| 5624 |
r = tpm2_tpm2b_public_from_pem(pubkey.iov_base, pubkey.iov_len, &public); |
| 5625 |
if (r < 0) |
| 5626 |
return log_error_errno(r, "Could not convert public key to TPM2B_PUBLIC: %m"); |
| 5627 |
} |
| 5628 |
|
| 5629 |
_cleanup_(tpm2_pcrlock_policy_done) Tpm2PCRLockPolicy pcrlock_policy = {}; |
| 5630 |
if (arg_tpm2_pcrlock) { |
| 5631 |
r = tpm2_pcrlock_policy_load(arg_tpm2_pcrlock, &pcrlock_policy); |
| 5632 |
if (r < 0) |
| 5633 |
return r; |
| 5634 |
|
| 5635 |
flags |= TPM2_FLAGS_USE_PCRLOCK; |
| 5636 |
} |
| 5637 |
|
| 5638 |
_cleanup_(tpm2_context_unrefp) Tpm2Context *tpm2_context = NULL; |
| 5639 |
TPM2B_PUBLIC device_key_public = {}; |
| 5640 |
if (arg_tpm2_device_key) { |
| 5641 |
r = tpm2_load_public_key_file(arg_tpm2_device_key, &device_key_public); |
| 5642 |
if (r < 0) |
| 5643 |
return r; |
| 5644 |
|
| 5645 |
if (!tpm2_pcr_values_has_all_values(pcr_values, n_pcr_values)) |
| 5646 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 5647 |
"Must provide all PCR values when using TPM2 device key."); |
| 5648 |
} else { |
| 5649 |
r = tpm2_context_new_or_warn(arg_tpm2_device, &tpm2_context); |
| 5650 |
if (r < 0) |
| 5651 |
return r; |
| 5652 |
|
| 5653 |
if (!tpm2_pcr_values_has_all_values(pcr_values, n_pcr_values)) { |
| 5654 |
r = tpm2_pcr_read_missing_values(tpm2_context, pcr_values, n_pcr_values); |
| 5655 |
if (r < 0) |
| 5656 |
return log_error_errno(r, "Could not read pcr values: %m"); |
| 5657 |
} |
| 5658 |
} |
| 5659 |
|
| 5660 |
uint16_t hash_pcr_bank = 0; |
| 5661 |
uint32_t hash_pcr_mask = 0; |
| 5662 |
if (n_pcr_values > 0) { |
| 5663 |
size_t hash_count; |
| 5664 |
r = tpm2_pcr_values_hash_count(pcr_values, n_pcr_values, &hash_count); |
| 5665 |
if (r < 0) |
| 5666 |
return log_error_errno(r, "Could not get hash count: %m"); |
| 5667 |
|
| 5668 |
if (hash_count > 1) |
| 5669 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Multiple PCR banks selected."); |
| 5670 |
|
| 5671 |
hash_pcr_bank = pcr_values[0].hash; |
| 5672 |
r = tpm2_pcr_values_to_mask(pcr_values, n_pcr_values, hash_pcr_bank, &hash_pcr_mask); |
| 5673 |
if (r < 0) |
| 5674 |
return log_error_errno(r, "Could not get hash mask: %m"); |
| 5675 |
} |
| 5676 |
|
| 5677 |
TPM2B_DIGEST policy_hash[2] = { |
| 5678 |
TPM2B_DIGEST_MAKE(NULL, TPM2_SHA256_DIGEST_SIZE), |
| 5679 |
TPM2B_DIGEST_MAKE(NULL, TPM2_SHA256_DIGEST_SIZE), |
| 5680 |
}; |
| 5681 |
size_t n_policy_hash = 1; |
| 5682 |
|
| 5683 |
/* If both PCR public key unlock and pcrlock unlock is selected, then shard the encryption key. */ |
| 5684 |
r = tpm2_calculate_sealing_policy( |
| 5685 |
pcr_values, |
| 5686 |
n_pcr_values, |
| 5687 |
iovec_is_set(&pubkey) ? &public : NULL, |
| 5688 |
/* use_pin= */ false, |
| 5689 |
arg_tpm2_pcrlock && !iovec_is_set(&pubkey) ? &pcrlock_policy : NULL, |
| 5690 |
policy_hash + 0); |
| 5691 |
if (r < 0) |
| 5692 |
return log_error_errno(r, "Could not calculate sealing policy digest for shard 0: %m"); |
| 5693 |
|
| 5694 |
if (arg_tpm2_pcrlock && iovec_is_set(&pubkey)) { |
| 5695 |
r = tpm2_calculate_sealing_policy( |
| 5696 |
pcr_values, |
| 5697 |
n_pcr_values, |
| 5698 |
/* public= */ NULL, /* Turn this one off for the 2nd shard */ |
| 5699 |
/* use_pin= */ false, |
| 5700 |
&pcrlock_policy, /* But turn this one on */ |
| 5701 |
policy_hash + 1); |
| 5702 |
if (r < 0) |
| 5703 |
return log_error_errno(r, "Could not calculate sealing policy digest for shard 1: %m"); |
| 5704 |
|
| 5705 |
n_policy_hash++; |
| 5706 |
} |
| 5707 |
|
| 5708 |
struct iovec *blobs = NULL; |
| 5709 |
size_t n_blobs = 0; |
| 5710 |
CLEANUP_ARRAY(blobs, n_blobs, iovec_array_free); |
| 5711 |
|
| 5712 |
if (arg_tpm2_device_key) { |
| 5713 |
if (n_policy_hash > 1) |
| 5714 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 5715 |
"Combined signed PCR policies and pcrlock policies cannot be calculated offline, currently."); |
| 5716 |
|
| 5717 |
blobs = new0(struct iovec, 1); |
| 5718 |
if (!blobs) |
| 5719 |
return log_oom(); |
| 5720 |
|
| 5721 |
n_blobs = 1; |
| 5722 |
|
| 5723 |
r = tpm2_calculate_seal( |
| 5724 |
arg_tpm2_seal_key_handle, |
| 5725 |
&device_key_public, |
| 5726 |
/* attributes= */ NULL, |
| 5727 |
/* secret= */ NULL, |
| 5728 |
policy_hash + 0, |
| 5729 |
/* pin= */ NULL, |
| 5730 |
&secret, |
| 5731 |
blobs + 0, |
| 5732 |
&srk); |
| 5733 |
} else |
| 5734 |
r = tpm2_seal(tpm2_context, |
| 5735 |
arg_tpm2_seal_key_handle, |
| 5736 |
policy_hash, |
| 5737 |
n_policy_hash, |
| 5738 |
/* pin= */ NULL, |
| 5739 |
&secret, |
| 5740 |
&blobs, |
| 5741 |
&n_blobs, |
| 5742 |
/* ret_primary_alg= */ NULL, |
| 5743 |
&srk); |
| 5744 |
if (r < 0) |
| 5745 |
return log_error_errno(r, "Failed to seal to TPM2: %m"); |
| 5746 |
|
| 5747 |
base64_encoded_size = base64mem(secret.iov_base, secret.iov_len, &base64_encoded); |
| 5748 |
if (base64_encoded_size < 0) |
| 5749 |
return log_error_errno(base64_encoded_size, "Failed to base64 encode secret key: %m"); |
| 5750 |
|
| 5751 |
r = cryptsetup_set_minimal_pbkdf(cd); |
| 5752 |
if (r < 0) |
| 5753 |
return log_error_errno(r, "Failed to set minimal PBKDF: %m"); |
| 5754 |
|
| 5755 |
keyslot = sym_crypt_keyslot_add_by_volume_key( |
| 5756 |
cd, |
| 5757 |
CRYPT_ANY_SLOT, |
| 5758 |
/* volume_key= */ NULL, |
| 5759 |
/* volume_key_size= */ volume_key_size, |
| 5760 |
base64_encoded, |
| 5761 |
base64_encoded_size); |
| 5762 |
if (keyslot < 0) |
| 5763 |
return log_error_errno(keyslot, "Failed to add new TPM2 key: %m"); |
| 5764 |
|
| 5765 |
struct iovec policy_hash_as_iovec[2] = { |
| 5766 |
IOVEC_MAKE(policy_hash[0].buffer, policy_hash[0].size), |
| 5767 |
IOVEC_MAKE(policy_hash[1].buffer, policy_hash[1].size), |
| 5768 |
}; |
| 5769 |
|
| 5770 |
r = tpm2_make_luks2_json( |
| 5771 |
keyslot, |
| 5772 |
hash_pcr_mask, |
| 5773 |
hash_pcr_bank, |
| 5774 |
&pubkey, |
| 5775 |
arg_tpm2_public_key_pcr_mask, |
| 5776 |
/* primary_alg= */ 0, |
| 5777 |
blobs, |
| 5778 |
n_blobs, |
| 5779 |
policy_hash_as_iovec, |
| 5780 |
n_policy_hash, |
| 5781 |
/* salt= */ NULL, /* no salt because tpm2_seal has no pin */ |
| 5782 |
&srk, |
| 5783 |
&pcrlock_policy.nv_handle, |
| 5784 |
flags, |
| 5785 |
&v); |
| 5786 |
if (r < 0) |
| 5787 |
return log_error_errno(r, "Failed to prepare TPM2 JSON token object: %m"); |
| 5788 |
|
| 5789 |
r = cryptsetup_add_token_json(cd, v); |
| 5790 |
if (r < 0) |
| 5791 |
return log_error_errno(r, "Failed to add TPM2 JSON token to LUKS2 header: %m"); |
| 5792 |
|
| 5793 |
passphrase = base64_encoded; |
| 5794 |
passphrase_size = strlen(base64_encoded); |
| 5795 |
#else |
| 5796 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 5797 |
"Support for TPM2 enrollment not enabled."); |
| 5798 |
#endif |
| 5799 |
} |
| 5800 |
|
| 5801 |
if (offline) { |
| 5802 |
r = sym_crypt_reencrypt_init_by_passphrase( |
| 5803 |
cd, |
| 5804 |
NULL, |
| 5805 |
passphrase, |
| 5806 |
passphrase_size, |
| 5807 |
CRYPT_ANY_SLOT, |
| 5808 |
0, |
| 5809 |
sym_crypt_get_cipher(cd), |
| 5810 |
sym_crypt_get_cipher_mode(cd), |
| 5811 |
&reencrypt_params); |
| 5812 |
if (r < 0) |
| 5813 |
return log_error_errno(r, "Failed to prepare for reencryption: %m"); |
| 5814 |
|
| 5815 |
/* crypt_reencrypt_init_by_passphrase() doesn't actually put the LUKS header at the front, we |
| 5816 |
* have to do that ourselves. */ |
| 5817 |
|
| 5818 |
sym_crypt_free(cd); |
| 5819 |
cd = NULL; |
| 5820 |
|
| 5821 |
r = sym_crypt_init(&cd, node); |
| 5822 |
if (r < 0) |
| 5823 |
return log_error_errno(r, "Failed to allocate libcryptsetup context for %s: %m", node); |
| 5824 |
|
| 5825 |
r = sym_crypt_header_restore(cd, CRYPT_LUKS2, hp); |
| 5826 |
if (r < 0) |
| 5827 |
return log_error_errno(r, "Failed to place new LUKS header at head of %s: %m", node); |
| 5828 |
|
| 5829 |
reencrypt_params.flags &= ~CRYPT_REENCRYPT_INITIALIZE_ONLY; |
| 5830 |
|
| 5831 |
r = sym_crypt_reencrypt_init_by_passphrase( |
| 5832 |
cd, |
| 5833 |
NULL, |
| 5834 |
passphrase, |
| 5835 |
passphrase_size, |
| 5836 |
CRYPT_ANY_SLOT, |
| 5837 |
0, |
| 5838 |
NULL, |
| 5839 |
NULL, |
| 5840 |
&reencrypt_params); |
| 5841 |
if (r < 0) |
| 5842 |
return log_error_errno(r, "Failed to load reencryption context: %m"); |
| 5843 |
|
| 5844 |
r = sym_crypt_reencrypt_run(cd, NULL, NULL); |
| 5845 |
if (r < 0) |
| 5846 |
return log_error_errno(r, "Failed to encrypt %s: %m", node); |
| 5847 |
} else { |
| 5848 |
_cleanup_free_ DecryptedPartitionTarget *t = NULL; |
| 5849 |
_cleanup_close_ int dev_fd = -1; |
| 5850 |
|
| 5851 |
r = sym_crypt_activate_by_volume_key( |
| 5852 |
cd, |
| 5853 |
dm_name, |
| 5854 |
NULL, |
| 5855 |
/* volume_key_size= */ volume_key_size, |
| 5856 |
(allow_discards ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0) | CRYPT_ACTIVATE_PRIVATE); |
| 5857 |
if (r < 0) |
| 5858 |
return log_error_errno(r, "Failed to activate LUKS superblock: %m"); |
| 5859 |
|
| 5860 |
/* crypt_wipe() the whole device to avoid integrity errors upon mkfs */ |
| 5861 |
if (p->integrity == INTEGRITY_INLINE) { |
| 5862 |
r = sym_crypt_wipe( |
| 5863 |
cd, |
| 5864 |
vol, |
| 5865 |
CRYPT_WIPE_ZERO, |
| 5866 |
/* offset= */ 0, |
| 5867 |
/* length= */ 0, |
| 5868 |
/* wipe_block_size= */ 1 * U64_MB, |
| 5869 |
/* flags= */ 0, |
| 5870 |
/* progress= */ NULL, |
| 5871 |
/* usrptr= */ NULL); |
| 5872 |
if (r < 0) |
| 5873 |
return log_error_errno(r, "Failed to wipe LUKS device: %m"); |
| 5874 |
|
| 5875 |
log_info("%s integrity protection for future partition %" PRIu64 " initialized.", |
| 5876 |
integrity_alg_to_string(p->integrity_alg), p->partno); |
| 5877 |
} |
| 5878 |
|
| 5879 |
dev_fd = open(vol, O_RDWR|O_CLOEXEC|O_NOCTTY); |
| 5880 |
if (dev_fd < 0) |
| 5881 |
return log_error_errno(errno, "Failed to open LUKS volume '%s': %m", vol); |
| 5882 |
|
| 5883 |
if (flock(dev_fd, LOCK_EX) < 0) |
| 5884 |
return log_error_errno(errno, "Failed to lock '%s': %m", vol); |
| 5885 |
|
| 5886 |
t = new(DecryptedPartitionTarget, 1); |
| 5887 |
if (!t) |
| 5888 |
return log_oom(); |
| 5889 |
|
| 5890 |
*t = (DecryptedPartitionTarget) { |
| 5891 |
.fd = TAKE_FD(dev_fd), |
| 5892 |
.dm_name = TAKE_PTR(dm_name), |
| 5893 |
.volume = TAKE_PTR(vol), |
| 5894 |
.device = TAKE_PTR(cd), |
| 5895 |
}; |
| 5896 |
|
| 5897 |
target->decrypted = TAKE_PTR(t); |
| 5898 |
} |
| 5899 |
|
| 5900 |
log_info("Successfully encrypted future partition %" PRIu64 ".", p->partno); |
| 5901 |
|
| 5902 |
return 0; |
| 5903 |
#else |
| 5904 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 5905 |
"libcryptsetup is not supported, cannot encrypt."); |
| 5906 |
#endif |
| 5907 |
} |
| 5908 |
|
| 5909 |
static int partition_format_verity_hash( |
| 5910 |
Context *context, |
| 5911 |
Partition *p, |
| 5912 |
const char *node, |
| 5913 |
const char *data_node) { |
| 5914 |
|
| 5915 |
#if HAVE_LIBCRYPTSETUP |
| 5916 |
Partition *dp; |
| 5917 |
_cleanup_(partition_target_freep) PartitionTarget *t = NULL; |
| 5918 |
_cleanup_(crypt_freep) struct crypt_device *cd = NULL; |
| 5919 |
_cleanup_free_ char *hint = NULL; |
| 5920 |
int r; |
| 5921 |
|
| 5922 |
assert(context); |
| 5923 |
assert(p); |
| 5924 |
assert(p->verity == VERITY_HASH); |
| 5925 |
assert(data_node); |
| 5926 |
|
| 5927 |
if (p->dropped) |
| 5928 |
return 0; |
| 5929 |
|
| 5930 |
if (PARTITION_EXISTS(p)) /* Never format existing partitions */ |
| 5931 |
return 0; |
| 5932 |
|
| 5933 |
/* Either we are minimizing the partition or we were instructed to copy an existing hash block directly. */ |
| 5934 |
if (p->copy_blocks_fd >= 0) |
| 5935 |
return 0; |
| 5936 |
|
| 5937 |
assert_se(dp = p->siblings[VERITY_DATA]); |
| 5938 |
assert(!dp->dropped); |
| 5939 |
|
| 5940 |
(void) partition_hint(p, node, &hint); |
| 5941 |
|
| 5942 |
r = DLOPEN_CRYPTSETUP(LOG_ERR, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED); |
| 5943 |
if (r < 0) |
| 5944 |
return r; |
| 5945 |
|
| 5946 |
if (!node) { |
| 5947 |
r = partition_target_prepare(context, p, p->new_size, /* need_path= */ true, &t); |
| 5948 |
if (r < 0) |
| 5949 |
return r; |
| 5950 |
|
| 5951 |
node = partition_target_path(t); |
| 5952 |
} |
| 5953 |
|
| 5954 |
r = sym_crypt_init(&cd, node); |
| 5955 |
if (r < 0) |
| 5956 |
return log_error_errno(r, "Failed to allocate libcryptsetup context for %s: %m", node); |
| 5957 |
|
| 5958 |
cryptsetup_enable_logging(cd); |
| 5959 |
|
| 5960 |
r = sym_crypt_format( |
| 5961 |
cd, CRYPT_VERITY, NULL, NULL, SD_ID128_TO_UUID_STRING(p->verity_uuid), NULL, 0, |
| 5962 |
&(struct crypt_params_verity){ |
| 5963 |
.data_device = data_node, |
| 5964 |
.flags = CRYPT_VERITY_CREATE_HASH, |
| 5965 |
.hash_name = "sha256", |
| 5966 |
.hash_type = 1, |
| 5967 |
.data_block_size = p->verity_data_block_size, |
| 5968 |
.hash_block_size = p->verity_hash_block_size, |
| 5969 |
.salt_size = sizeof(p->verity_salt), |
| 5970 |
.salt = (const char*)p->verity_salt, |
| 5971 |
}); |
| 5972 |
if (r < 0) { |
| 5973 |
/* libcryptsetup reports non-descriptive EIO errors for every I/O failure. Luckily, it |
| 5974 |
* doesn't clobber errno so let's check for ENOSPC so we can report a better error if the |
| 5975 |
* partition is too small. */ |
| 5976 |
if (r == -EIO && errno == ENOSPC) |
| 5977 |
return log_error_errno(errno, |
| 5978 |
"Verity hash data does not fit in partition %s with size %s", |
| 5979 |
strna(hint), FORMAT_BYTES(p->new_size)); |
| 5980 |
|
| 5981 |
return log_error_errno(r, "Failed to setup verity hash data of partition %s: %m", strna(hint)); |
| 5982 |
} |
| 5983 |
|
| 5984 |
if (t) { |
| 5985 |
r = partition_target_sync(context, p, t); |
| 5986 |
if (r < 0) |
| 5987 |
return r; |
| 5988 |
} |
| 5989 |
|
| 5990 |
r = sym_crypt_get_volume_key_size(cd); |
| 5991 |
if (r < 0) |
| 5992 |
return log_error_errno(r, "Failed to determine verity root hash size of partition %s: %m", strna(hint)); |
| 5993 |
|
| 5994 |
_cleanup_(iovec_done) struct iovec rh = { |
| 5995 |
.iov_base = malloc(r), |
| 5996 |
.iov_len = r, |
| 5997 |
}; |
| 5998 |
if (!rh.iov_base) |
| 5999 |
return log_oom(); |
| 6000 |
|
| 6001 |
r = sym_crypt_volume_key_get(cd, CRYPT_ANY_SLOT, (char *) rh.iov_base, &rh.iov_len, NULL, 0); |
| 6002 |
if (r < 0) |
| 6003 |
return log_error_errno(r, "Failed to get verity root hash of partition %s: %m", strna(hint)); |
| 6004 |
|
| 6005 |
assert(rh.iov_len >= sizeof(sd_id128_t) * 2); |
| 6006 |
|
| 6007 |
if (!dp->new_uuid_is_set) { |
| 6008 |
memcpy_safe(dp->new_uuid.bytes, rh.iov_base, sizeof(sd_id128_t)); |
| 6009 |
dp->new_uuid_is_set = true; |
| 6010 |
} |
| 6011 |
|
| 6012 |
if (!p->new_uuid_is_set) { |
| 6013 |
memcpy_safe(p->new_uuid.bytes, (uint8_t*) rh.iov_base + (rh.iov_len - sizeof(sd_id128_t)), sizeof(sd_id128_t)); |
| 6014 |
p->new_uuid_is_set = true; |
| 6015 |
} |
| 6016 |
|
| 6017 |
p->roothash = TAKE_STRUCT(rh); |
| 6018 |
|
| 6019 |
return 0; |
| 6020 |
#else |
| 6021 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libcryptsetup is not supported, cannot setup verity hashes."); |
| 6022 |
#endif |
| 6023 |
} |
| 6024 |
|
| 6025 |
static int sign_verity_roothash( |
| 6026 |
Context *context, |
| 6027 |
const struct iovec *roothash, |
| 6028 |
struct iovec *ret_signature) { |
| 6029 |
|
| 6030 |
#if HAVE_OPENSSL |
| 6031 |
_cleanup_(BIO_freep) BIO *rb = NULL; |
| 6032 |
_cleanup_(PKCS7_freep) PKCS7 *p7 = NULL; |
| 6033 |
_cleanup_free_ char *hex = NULL; |
| 6034 |
_cleanup_free_ uint8_t *sig = NULL; |
| 6035 |
int sigsz, r; |
| 6036 |
|
| 6037 |
assert(context); |
| 6038 |
assert(context->certificate); |
| 6039 |
assert(context->private_key); |
| 6040 |
assert(roothash); |
| 6041 |
assert(iovec_is_set(roothash)); |
| 6042 |
assert(ret_signature); |
| 6043 |
|
| 6044 |
r = DLOPEN_LIBCRYPTO(LOG_ERR, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED); |
| 6045 |
if (r < 0) |
| 6046 |
return r; |
| 6047 |
|
| 6048 |
hex = hexmem(roothash->iov_base, roothash->iov_len); |
| 6049 |
if (!hex) |
| 6050 |
return log_oom(); |
| 6051 |
|
| 6052 |
rb = sym_BIO_new_mem_buf(hex, -1); |
| 6053 |
if (!rb) |
| 6054 |
return log_oom(); |
| 6055 |
|
| 6056 |
p7 = sym_PKCS7_sign(context->certificate, context->private_key, NULL, rb, PKCS7_DETACHED|PKCS7_NOATTR|PKCS7_BINARY); |
| 6057 |
if (!p7) |
| 6058 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to calculate PKCS7 signature: %s", |
| 6059 |
sym_ERR_error_string(sym_ERR_get_error(), NULL)); |
| 6060 |
|
| 6061 |
sigsz = sym_i2d_PKCS7(p7, &sig); |
| 6062 |
if (sigsz < 0) |
| 6063 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to convert PKCS7 signature to DER: %s", |
| 6064 |
sym_ERR_error_string(sym_ERR_get_error(), NULL)); |
| 6065 |
|
| 6066 |
*ret_signature = IOVEC_MAKE(TAKE_PTR(sig), sigsz); |
| 6067 |
|
| 6068 |
return 0; |
| 6069 |
#else |
| 6070 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "OpenSSL is not supported, cannot setup verity signature."); |
| 6071 |
#endif |
| 6072 |
} |
| 6073 |
|
| 6074 |
static int iovec_roothash_from_uuid_pair( |
| 6075 |
sd_id128_t data_uuid, |
| 6076 |
sd_id128_t hash_uuid, |
| 6077 |
struct iovec *ret_roothash) { |
| 6078 |
|
| 6079 |
uint8_t roothash_bytes[sizeof(sd_id128_t) * 2]; |
| 6080 |
|
| 6081 |
assert(ret_roothash); |
| 6082 |
|
| 6083 |
if (sd_id128_is_null(data_uuid) || sd_id128_is_null(hash_uuid)) |
| 6084 |
return -EINVAL; |
| 6085 |
|
| 6086 |
/* As per the https://uapi-group.org/specifications/specs/discoverable_partitions_specification/ the |
| 6087 |
* UUIDs of the data and verity partitions are respectively the first and second halves of the |
| 6088 |
* dm-verity roothash, so we can use them to match the signature to the right partition. */ |
| 6089 |
|
| 6090 |
memcpy(roothash_bytes, data_uuid.bytes, sizeof(sd_id128_t)); |
| 6091 |
memcpy(roothash_bytes + sizeof(sd_id128_t), hash_uuid.bytes, sizeof(sd_id128_t)); |
| 6092 |
|
| 6093 |
if (!iovec_memdup(&IOVEC_MAKE(roothash_bytes, sizeof(roothash_bytes)), ret_roothash)) |
| 6094 |
return -ENOMEM; |
| 6095 |
|
| 6096 |
return 0; |
| 6097 |
} |
| 6098 |
|
| 6099 |
static const VeritySettings *lookup_verity_settings_by_uuid_pair(sd_id128_t data_uuid, sd_id128_t hash_uuid) { |
| 6100 |
_cleanup_(iovec_done) struct iovec roothash = {}; |
| 6101 |
int r; |
| 6102 |
|
| 6103 |
r = iovec_roothash_from_uuid_pair(data_uuid, hash_uuid, &roothash); |
| 6104 |
if (r < 0) |
| 6105 |
return NULL; |
| 6106 |
|
| 6107 |
VeritySettings key = { |
| 6108 |
.root_hash = roothash, |
| 6109 |
}; |
| 6110 |
|
| 6111 |
return set_get(arg_verity_settings, &key); |
| 6112 |
} |
| 6113 |
|
| 6114 |
static int partition_format_verity_sig(Context *context, Partition *p) { |
| 6115 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 6116 |
_cleanup_free_ char *text = NULL, *hint = NULL; |
| 6117 |
const VeritySettings *verity_settings; |
| 6118 |
Partition *hp, *rp; |
| 6119 |
uint8_t fp[X509_FINGERPRINT_SIZE]; |
| 6120 |
int whole_fd, r; |
| 6121 |
bool has_fp = false; |
| 6122 |
|
| 6123 |
assert(p->verity == VERITY_SIG); |
| 6124 |
|
| 6125 |
if (p->dropped) |
| 6126 |
return 0; |
| 6127 |
|
| 6128 |
if (PARTITION_EXISTS(p)) |
| 6129 |
return 0; |
| 6130 |
|
| 6131 |
/* We were instructed to copy an existing signature block directly */ |
| 6132 |
if (p->copy_blocks_fd >= 0) |
| 6133 |
return 0; |
| 6134 |
|
| 6135 |
assert_se(hp = p->siblings[VERITY_HASH]); |
| 6136 |
assert(!hp->dropped); |
| 6137 |
assert_se(rp = p->siblings[VERITY_DATA]); |
| 6138 |
assert(!rp->dropped); |
| 6139 |
|
| 6140 |
/* Currently only set while formatting the hash partition. But if this is skipped via CopyBlocks= |
| 6141 |
* we just derive the roothash from the UUIDs from the data + hash partition. */ |
| 6142 |
if (!iovec_is_set(&hp->roothash)) { |
| 6143 |
r = iovec_roothash_from_uuid_pair(rp->new_uuid, hp->new_uuid, &hp->roothash); |
| 6144 |
if (r < 0) |
| 6145 |
return log_error_errno(r, "Unable to derive roothash: %m"); |
| 6146 |
} |
| 6147 |
|
| 6148 |
verity_settings = lookup_verity_settings_by_uuid_pair(rp->current_uuid, hp->current_uuid); |
| 6149 |
|
| 6150 |
if (!verity_settings) { |
| 6151 |
#if HAVE_OPENSSL |
| 6152 |
if (!context->private_key) |
| 6153 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 6154 |
"Verity signature partition signing requested but no private key provided (--private-key=)."); |
| 6155 |
|
| 6156 |
if (!context->certificate) |
| 6157 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 6158 |
"Verity signature partition signing requested but no PEM certificate provided (--certificate=)."); |
| 6159 |
#else |
| 6160 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 6161 |
"Verity signature partition signing requested but OpenSSL support is disabled."); |
| 6162 |
#endif |
| 6163 |
} |
| 6164 |
|
| 6165 |
(void) partition_hint(p, context->node, &hint); |
| 6166 |
|
| 6167 |
assert_se((whole_fd = sym_fdisk_get_devfd(context->fdisk_context)) >= 0); |
| 6168 |
|
| 6169 |
_cleanup_(iovec_done) struct iovec sig_free = {}; |
| 6170 |
const struct iovec *roothash, *sig; |
| 6171 |
if (verity_settings) { |
| 6172 |
sig = &verity_settings->root_hash_sig; |
| 6173 |
roothash = &verity_settings->root_hash; |
| 6174 |
} else { |
| 6175 |
r = sign_verity_roothash(context, &hp->roothash, &sig_free); |
| 6176 |
if (r < 0) |
| 6177 |
return r; |
| 6178 |
|
| 6179 |
sig = &sig_free; |
| 6180 |
roothash = &hp->roothash; |
| 6181 |
} |
| 6182 |
|
| 6183 |
#if HAVE_OPENSSL |
| 6184 |
r = x509_fingerprint(context->certificate, fp); |
| 6185 |
if (r < 0) |
| 6186 |
return log_error_errno(r, "Unable to calculate X509 certificate fingerprint: %m"); |
| 6187 |
has_fp = true; |
| 6188 |
#endif |
| 6189 |
|
| 6190 |
r = sd_json_buildo( |
| 6191 |
&v, |
| 6192 |
SD_JSON_BUILD_PAIR("rootHash", SD_JSON_BUILD_HEX(roothash->iov_base, roothash->iov_len)), |
| 6193 |
SD_JSON_BUILD_PAIR_CONDITION(has_fp, "certificateFingerprint", SD_JSON_BUILD_HEX(fp, sizeof(fp))), |
| 6194 |
SD_JSON_BUILD_PAIR("signature", JSON_BUILD_IOVEC_BASE64(sig))); |
| 6195 |
if (r < 0) |
| 6196 |
return log_error_errno(r, "Failed to build verity signature JSON object: %m"); |
| 6197 |
|
| 6198 |
r = sd_json_variant_format(v, 0, &text); |
| 6199 |
if (r < 0) |
| 6200 |
return log_error_errno(r, "Failed to format verity signature JSON object: %m"); |
| 6201 |
|
| 6202 |
if (strlen(text)+1 > p->new_size) |
| 6203 |
return log_error_errno(SYNTHETIC_ERRNO(E2BIG), "Verity signature too long for partition."); |
| 6204 |
|
| 6205 |
r = strgrowpad0(&text, p->new_size); |
| 6206 |
if (r < 0) |
| 6207 |
return log_error_errno(r, "Failed to pad string to %s", FORMAT_BYTES(p->new_size)); |
| 6208 |
|
| 6209 |
if (lseek(whole_fd, p->offset, SEEK_SET) < 0) |
| 6210 |
return log_error_errno(errno, "Failed to seek to partition %s offset: %m", strna(hint)); |
| 6211 |
|
| 6212 |
r = loop_write(whole_fd, text, p->new_size); |
| 6213 |
if (r < 0) |
| 6214 |
return log_error_errno(r, "Failed to write verity signature to partition %s: %m", strna(hint)); |
| 6215 |
|
| 6216 |
if (fsync(whole_fd) < 0) |
| 6217 |
return log_error_errno(errno, "Failed to synchronize partition %s: %m", strna(hint)); |
| 6218 |
|
| 6219 |
return 0; |
| 6220 |
} |
| 6221 |
|
| 6222 |
static int progress_bytes(uint64_t n_bytes, uint64_t bps, void *userdata) { |
| 6223 |
Partition *p = ASSERT_PTR(userdata); |
| 6224 |
unsigned percent; |
| 6225 |
|
| 6226 |
p->copy_blocks_done += n_bytes; |
| 6227 |
|
| 6228 |
/* Catch division by zero. */ |
| 6229 |
if (p->copy_blocks_done >= p->copy_blocks_size) |
| 6230 |
percent = 100; |
| 6231 |
else |
| 6232 |
percent = (unsigned) (100.0 * (double) p->copy_blocks_done / (double) p->copy_blocks_size); |
| 6233 |
|
| 6234 |
if (percent == p->last_percent) |
| 6235 |
return 0; |
| 6236 |
|
| 6237 |
if (!ratelimit_below(&p->progress_ratelimit)) |
| 6238 |
return 0; |
| 6239 |
|
| 6240 |
if (bps != UINT64_MAX) |
| 6241 |
(void) draw_progress_barf( |
| 6242 |
percent, |
| 6243 |
"%s %s %s %s/%s %s/s", |
| 6244 |
strna(p->copy_blocks_path), |
| 6245 |
glyph(GLYPH_ARROW_RIGHT), |
| 6246 |
strna(p->definition_path), |
| 6247 |
FORMAT_BYTES_WITH_POINT(p->copy_blocks_done), |
| 6248 |
FORMAT_BYTES_WITH_POINT(p->copy_blocks_size), |
| 6249 |
FORMAT_BYTES_WITH_POINT(bps)); |
| 6250 |
else |
| 6251 |
(void) draw_progress_barf( |
| 6252 |
percent, |
| 6253 |
"%s %s %s %s/%s", |
| 6254 |
strna(p->copy_blocks_path), |
| 6255 |
glyph(GLYPH_ARROW_RIGHT), |
| 6256 |
strna(p->definition_path), |
| 6257 |
FORMAT_BYTES_WITH_POINT(p->copy_blocks_done), |
| 6258 |
FORMAT_BYTES_WITH_POINT(p->copy_blocks_size)); |
| 6259 |
|
| 6260 |
p->last_percent = percent; |
| 6261 |
|
| 6262 |
(void) context_notify(p->context, PROGRESS_COPYING_PARTITION, p->definition_path, percent); |
| 6263 |
|
| 6264 |
return 0; |
| 6265 |
} |
| 6266 |
|
| 6267 |
static int context_copy_blocks(Context *context) { |
| 6268 |
int r; |
| 6269 |
|
| 6270 |
assert(context); |
| 6271 |
|
| 6272 |
/* Copy in file systems on the block level */ |
| 6273 |
|
| 6274 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 6275 |
_cleanup_(partition_target_freep) PartitionTarget *t = NULL; |
| 6276 |
|
| 6277 |
if (p->dropped) |
| 6278 |
continue; |
| 6279 |
|
| 6280 |
if (PARTITION_EXISTS(p)) /* Never copy over existing partitions */ |
| 6281 |
continue; |
| 6282 |
|
| 6283 |
if (partition_defer(context, p)) |
| 6284 |
continue; |
| 6285 |
|
| 6286 |
/* For offline signing case */ |
| 6287 |
if (!set_isempty(arg_verity_settings) && partition_designator_is_verity_sig(p->type.designator)) |
| 6288 |
return partition_format_verity_sig(context, p); |
| 6289 |
|
| 6290 |
if (p->copy_blocks_fd < 0) |
| 6291 |
continue; |
| 6292 |
|
| 6293 |
(void) context_notify(context, PROGRESS_COPYING_PARTITION, p->definition_path, UINT_MAX); |
| 6294 |
|
| 6295 |
assert(p->new_size != UINT64_MAX); |
| 6296 |
|
| 6297 |
size_t extra = p->encrypt != ENCRYPT_OFF ? LUKS2_METADATA_KEEP_FREE : 0; |
| 6298 |
|
| 6299 |
if (p->copy_blocks_size == UINT64_MAX) |
| 6300 |
p->copy_blocks_size = LESS_BY(p->new_size, extra); |
| 6301 |
|
| 6302 |
assert(p->new_size >= p->copy_blocks_size + extra); |
| 6303 |
|
| 6304 |
usec_t start_timestamp = now(CLOCK_MONOTONIC); |
| 6305 |
|
| 6306 |
r = partition_target_prepare(context, p, p->new_size, |
| 6307 |
/* need_path= */ p->encrypt != ENCRYPT_OFF || p->siblings[VERITY_HASH], |
| 6308 |
&t); |
| 6309 |
if (r < 0) |
| 6310 |
return r; |
| 6311 |
|
| 6312 |
if (p->encrypt != ENCRYPT_OFF && (t->loop || t->block_partition)) { |
| 6313 |
r = partition_encrypt(context, p, t, /* offline= */ false, /* temporary= */ true); |
| 6314 |
if (r < 0) |
| 6315 |
return r; |
| 6316 |
} |
| 6317 |
|
| 6318 |
if (p->copy_blocks_offset == UINT64_MAX) |
| 6319 |
log_info("Copying in '%s' (%s) on block level into future partition %" PRIu64 ".", |
| 6320 |
p->copy_blocks_path, FORMAT_BYTES(p->copy_blocks_size), p->partno); |
| 6321 |
else { |
| 6322 |
log_info("Copying in '%s' @ %" PRIu64 " (%s) on block level into future partition %" PRIu64 ".", |
| 6323 |
p->copy_blocks_path, p->copy_blocks_offset, FORMAT_BYTES(p->copy_blocks_size), p->partno); |
| 6324 |
|
| 6325 |
if (lseek(p->copy_blocks_fd, p->copy_blocks_offset, SEEK_SET) < 0) |
| 6326 |
return log_error_errno(errno, "Failed to seek to copy blocks offset in %s: %m", p->copy_blocks_path); |
| 6327 |
} |
| 6328 |
|
| 6329 |
r = copy_bytes_full(p->copy_blocks_fd, partition_target_fd(t), p->copy_blocks_size, COPY_REFLINK, /* ret_remains= */ NULL, /* ret_remains_size= */ NULL, progress_bytes, p); |
| 6330 |
clear_progress_bar(/* prefix= */ NULL); |
| 6331 |
if (r < 0) |
| 6332 |
return log_error_errno(r, "Failed to copy in data from '%s': %m", p->copy_blocks_path); |
| 6333 |
|
| 6334 |
log_info("Copying in of '%s' on block level completed.", p->copy_blocks_path); |
| 6335 |
|
| 6336 |
if (p->encrypt != ENCRYPT_OFF && !t->loop && !t->block_partition) { |
| 6337 |
r = partition_encrypt(context, p, t, /* offline= */ true, /* temporary= */ true); |
| 6338 |
if (r < 0) |
| 6339 |
return r; |
| 6340 |
} |
| 6341 |
|
| 6342 |
r = partition_target_sync(context, p, t); |
| 6343 |
if (r < 0) |
| 6344 |
return r; |
| 6345 |
|
| 6346 |
usec_t time_spent = usec_sub_unsigned(now(CLOCK_MONOTONIC), start_timestamp); |
| 6347 |
if (time_spent > 250 * USEC_PER_MSEC) /* Show throughput, but not if we spent too little time on it, since it's just noise then */ |
| 6348 |
log_info("Block level copying and synchronization of partition %" PRIu64 " complete in %s (%s/s).", |
| 6349 |
p->partno, FORMAT_TIMESPAN(time_spent, 0), FORMAT_BYTES((uint64_t) ((double) p->copy_blocks_size / time_spent * USEC_PER_SEC))); |
| 6350 |
else |
| 6351 |
log_info("Block level copying and synchronization of partition %" PRIu64 " complete in %s.", |
| 6352 |
p->partno, FORMAT_TIMESPAN(time_spent, 0)); |
| 6353 |
|
| 6354 |
if (p->siblings[VERITY_HASH] && !partition_defer(context, p->siblings[VERITY_HASH])) { |
| 6355 |
r = partition_format_verity_hash(context, p->siblings[VERITY_HASH], |
| 6356 |
/* node= */ NULL, partition_target_path(t)); |
| 6357 |
if (r < 0) |
| 6358 |
return r; |
| 6359 |
} |
| 6360 |
|
| 6361 |
if (p->siblings[VERITY_SIG] && !partition_defer(context, p->siblings[VERITY_SIG])) { |
| 6362 |
r = partition_format_verity_sig(context, p->siblings[VERITY_SIG]); |
| 6363 |
if (r < 0) |
| 6364 |
return r; |
| 6365 |
} |
| 6366 |
} |
| 6367 |
|
| 6368 |
return 0; |
| 6369 |
} |
| 6370 |
|
| 6371 |
static int add_exclude_path(const char *path, Hashmap **denylist, DenyType type) { |
| 6372 |
_cleanup_free_ struct stat *st = NULL; |
| 6373 |
int r; |
| 6374 |
|
| 6375 |
assert(path); |
| 6376 |
assert(denylist); |
| 6377 |
|
| 6378 |
st = new(struct stat, 1); |
| 6379 |
if (!st) |
| 6380 |
return log_oom(); |
| 6381 |
|
| 6382 |
r = chase_and_stat(path, arg_copy_source, CHASE_PREFIX_ROOT, NULL, st); |
| 6383 |
if (r == -ENOENT) |
| 6384 |
return 0; |
| 6385 |
if (r < 0) |
| 6386 |
return log_error_errno(r, "Failed to stat source file '%s/%s': %m", strempty(arg_copy_source), path); |
| 6387 |
|
| 6388 |
r = hashmap_ensure_put(denylist, &inode_hash_ops, st, INT_TO_PTR(type)); |
| 6389 |
if (r == -EEXIST) |
| 6390 |
return 0; |
| 6391 |
if (r < 0) |
| 6392 |
return log_oom(); |
| 6393 |
if (r > 0) |
| 6394 |
TAKE_PTR(st); |
| 6395 |
|
| 6396 |
return 0; |
| 6397 |
} |
| 6398 |
|
| 6399 |
static int shallow_join_strv(char ***ret, char **a, char **b) { |
| 6400 |
_cleanup_free_ char **joined = NULL; |
| 6401 |
char **iter; |
| 6402 |
|
| 6403 |
assert(ret); |
| 6404 |
|
| 6405 |
joined = new(char*, strv_length(a) + strv_length(b) + 1); |
| 6406 |
if (!joined) |
| 6407 |
return log_oom(); |
| 6408 |
|
| 6409 |
iter = joined; |
| 6410 |
|
| 6411 |
STRV_FOREACH(i, a) |
| 6412 |
*(iter++) = *i; |
| 6413 |
|
| 6414 |
STRV_FOREACH(i, b) |
| 6415 |
if (!strv_contains(joined, *i)) |
| 6416 |
*(iter++) = *i; |
| 6417 |
|
| 6418 |
*iter = NULL; |
| 6419 |
|
| 6420 |
*ret = TAKE_PTR(joined); |
| 6421 |
return 0; |
| 6422 |
} |
| 6423 |
|
| 6424 |
static int make_copy_files_denylist( |
| 6425 |
Context *context, |
| 6426 |
const Partition *p, |
| 6427 |
const char *source, |
| 6428 |
const char *target, |
| 6429 |
Hashmap **ret) { |
| 6430 |
|
| 6431 |
_cleanup_hashmap_free_ Hashmap *denylist = NULL; |
| 6432 |
_cleanup_free_ char **override_exclude_src = NULL, **override_exclude_tgt = NULL; |
| 6433 |
int r; |
| 6434 |
|
| 6435 |
assert(context); |
| 6436 |
assert(p); |
| 6437 |
assert(source); |
| 6438 |
assert(target); |
| 6439 |
assert(ret); |
| 6440 |
|
| 6441 |
/* Always exclude the top level APIVFS and temporary directories since the contents of these |
| 6442 |
* directories are almost certainly not intended to end up in an image. */ |
| 6443 |
|
| 6444 |
NULSTR_FOREACH(s, APIVFS_TMP_DIRS_NULSTR) { |
| 6445 |
r = add_exclude_path(s, &denylist, DENY_CONTENTS); |
| 6446 |
if (r < 0) |
| 6447 |
return r; |
| 6448 |
} |
| 6449 |
|
| 6450 |
/* Add the user configured excludes. */ |
| 6451 |
|
| 6452 |
if (p->suppressing) { |
| 6453 |
r = shallow_join_strv(&override_exclude_src, |
| 6454 |
p->exclude_files_source, |
| 6455 |
p->suppressing->exclude_files_source); |
| 6456 |
if (r < 0) |
| 6457 |
return r; |
| 6458 |
r = shallow_join_strv(&override_exclude_tgt, |
| 6459 |
p->exclude_files_target, |
| 6460 |
p->suppressing->exclude_files_target); |
| 6461 |
if (r < 0) |
| 6462 |
return r; |
| 6463 |
} |
| 6464 |
|
| 6465 |
STRV_FOREACH(e, override_exclude_src ?: p->exclude_files_source) { |
| 6466 |
if (path_startswith(source, *e)) |
| 6467 |
return 1; |
| 6468 |
|
| 6469 |
r = add_exclude_path(*e, &denylist, endswith(*e, "/") ? DENY_CONTENTS : DENY_INODE); |
| 6470 |
if (r < 0) |
| 6471 |
return r; |
| 6472 |
} |
| 6473 |
|
| 6474 |
STRV_FOREACH(e, override_exclude_tgt ?: p->exclude_files_target) { |
| 6475 |
_cleanup_free_ char *path = NULL; |
| 6476 |
|
| 6477 |
if (path_startswith(target, *e)) |
| 6478 |
return 1; |
| 6479 |
|
| 6480 |
const char *s = path_startswith(*e, target); |
| 6481 |
if (!s) |
| 6482 |
continue; |
| 6483 |
|
| 6484 |
path = path_join(source, s); |
| 6485 |
if (!path) |
| 6486 |
return log_oom(); |
| 6487 |
|
| 6488 |
r = add_exclude_path(path, &denylist, endswith(*e, "/") ? DENY_CONTENTS : DENY_INODE); |
| 6489 |
if (r < 0) |
| 6490 |
return r; |
| 6491 |
} |
| 6492 |
|
| 6493 |
/* If we're populating a root partition, we don't want any files to end up under the APIVFS mount |
| 6494 |
* points. While we already exclude <source>/proc, users could still do something such as |
| 6495 |
* "CopyFiles=/abc:/". Now, if /abc has a proc subdirectory with files in it, those will end up in |
| 6496 |
* the top level proc directory in the root partition, which we want to avoid. To deal with these |
| 6497 |
* cases, whenever we're populating a root partition and the target of CopyFiles= is the root |
| 6498 |
* directory of the root partition, we exclude all directories under the source that are named after |
| 6499 |
* APIVFS directories or named after mount points of other partitions that are also going to be part |
| 6500 |
* of the image. */ |
| 6501 |
|
| 6502 |
if (p->type.designator == PARTITION_ROOT && empty_or_root(target)) { |
| 6503 |
LIST_FOREACH(partitions, q, context->partitions) { |
| 6504 |
if (q->type.designator == PARTITION_ROOT) |
| 6505 |
continue; |
| 6506 |
|
| 6507 |
const char *sources = gpt_partition_type_mountpoint_nulstr(q->type); |
| 6508 |
if (!sources) |
| 6509 |
continue; |
| 6510 |
|
| 6511 |
NULSTR_FOREACH(s, sources) { |
| 6512 |
_cleanup_free_ char *path = NULL; |
| 6513 |
|
| 6514 |
/* Exclude only the children of partition mount points so that the nested |
| 6515 |
* partition mount point itself still ends up in the upper partition. */ |
| 6516 |
|
| 6517 |
path = path_join(source, s); |
| 6518 |
if (!path) |
| 6519 |
return -ENOMEM; |
| 6520 |
|
| 6521 |
r = add_exclude_path(path, &denylist, DENY_CONTENTS); |
| 6522 |
if (r < 0) |
| 6523 |
return r; |
| 6524 |
} |
| 6525 |
} |
| 6526 |
|
| 6527 |
NULSTR_FOREACH(s, APIVFS_TMP_DIRS_NULSTR) { |
| 6528 |
_cleanup_free_ char *path = NULL; |
| 6529 |
|
| 6530 |
path = path_join(source, s); |
| 6531 |
if (!path) |
| 6532 |
return -ENOMEM; |
| 6533 |
|
| 6534 |
r = add_exclude_path(path, &denylist, DENY_CONTENTS); |
| 6535 |
if (r < 0) |
| 6536 |
return r; |
| 6537 |
} |
| 6538 |
} |
| 6539 |
|
| 6540 |
*ret = TAKE_PTR(denylist); |
| 6541 |
return 0; |
| 6542 |
} |
| 6543 |
|
| 6544 |
static int add_subvolume_path(const char *path, BtrfsSubvolFlags flags, Hashmap **subvolumes) { |
| 6545 |
_cleanup_free_ struct stat *st = NULL; |
| 6546 |
int r; |
| 6547 |
|
| 6548 |
assert(path); |
| 6549 |
assert(subvolumes); |
| 6550 |
|
| 6551 |
st = new(struct stat, 1); |
| 6552 |
if (!st) |
| 6553 |
return log_oom(); |
| 6554 |
|
| 6555 |
r = chase_and_stat(path, arg_copy_source, CHASE_PREFIX_ROOT, NULL, st); |
| 6556 |
if (r == -ENOENT) |
| 6557 |
return 0; |
| 6558 |
if (r < 0) |
| 6559 |
return log_error_errno(r, "Failed to stat source file '%s/%s': %m", strempty(arg_copy_source), path); |
| 6560 |
|
| 6561 |
r = hashmap_ensure_put(subvolumes, &inode_hash_ops, st, INT_TO_PTR(flags)); |
| 6562 |
if (r < 0) |
| 6563 |
return log_oom(); |
| 6564 |
|
| 6565 |
TAKE_PTR(st); |
| 6566 |
|
| 6567 |
return 0; |
| 6568 |
} |
| 6569 |
|
| 6570 |
static int make_subvolumes_hashmap(const Partition *p, Hashmap **ret) { |
| 6571 |
_cleanup_hashmap_free_ Hashmap *hashmap = NULL; |
| 6572 |
Subvolume *subvolume; |
| 6573 |
int r; |
| 6574 |
|
| 6575 |
assert(p); |
| 6576 |
assert(ret); |
| 6577 |
|
| 6578 |
ORDERED_HASHMAP_FOREACH(subvolume, p->subvolumes) { |
| 6579 |
_cleanup_free_ char *path = NULL; |
| 6580 |
|
| 6581 |
path = strdup(subvolume->path); |
| 6582 |
if (!path) |
| 6583 |
return log_oom(); |
| 6584 |
|
| 6585 |
r = hashmap_ensure_put(&hashmap, &path_hash_ops_free, path, INT_TO_PTR(subvolume->flags)); |
| 6586 |
if (r < 0) |
| 6587 |
return log_oom(); |
| 6588 |
|
| 6589 |
TAKE_PTR(path); |
| 6590 |
} |
| 6591 |
|
| 6592 |
if (p->suppressing) { |
| 6593 |
Hashmap *suppressing; |
| 6594 |
|
| 6595 |
r = make_subvolumes_hashmap(p->suppressing, &suppressing); |
| 6596 |
if (r < 0) |
| 6597 |
return r; |
| 6598 |
|
| 6599 |
r = hashmap_merge(hashmap, suppressing); |
| 6600 |
if (r < 0) |
| 6601 |
return log_oom(); |
| 6602 |
} |
| 6603 |
|
| 6604 |
*ret = TAKE_PTR(hashmap); |
| 6605 |
return 0; |
| 6606 |
} |
| 6607 |
|
| 6608 |
static int make_subvolumes_by_source_inode_hashmap( |
| 6609 |
const Partition *p, |
| 6610 |
const char *source, |
| 6611 |
const char *target, |
| 6612 |
Hashmap **ret) { |
| 6613 |
|
| 6614 |
_cleanup_hashmap_free_ Hashmap *hashmap = NULL; |
| 6615 |
Subvolume *subvolume; |
| 6616 |
int r; |
| 6617 |
|
| 6618 |
assert(p); |
| 6619 |
assert(target); |
| 6620 |
assert(ret); |
| 6621 |
|
| 6622 |
ORDERED_HASHMAP_FOREACH(subvolume, p->subvolumes) { |
| 6623 |
_cleanup_free_ char *path = NULL; |
| 6624 |
|
| 6625 |
const char *s = path_startswith(subvolume->path, target); |
| 6626 |
if (!s) |
| 6627 |
continue; |
| 6628 |
|
| 6629 |
path = path_join(source, s); |
| 6630 |
if (!path) |
| 6631 |
return log_oom(); |
| 6632 |
|
| 6633 |
r = add_subvolume_path(path, subvolume->flags, &hashmap); |
| 6634 |
if (r < 0) |
| 6635 |
return r; |
| 6636 |
} |
| 6637 |
|
| 6638 |
if (p->suppressing) { |
| 6639 |
Hashmap *suppressing; |
| 6640 |
|
| 6641 |
r = make_subvolumes_by_source_inode_hashmap(p->suppressing, source, target, &suppressing); |
| 6642 |
if (r < 0) |
| 6643 |
return r; |
| 6644 |
|
| 6645 |
r = hashmap_merge(hashmap, suppressing); |
| 6646 |
if (r < 0) |
| 6647 |
return log_oom(); |
| 6648 |
} |
| 6649 |
|
| 6650 |
*ret = TAKE_PTR(hashmap); |
| 6651 |
return 0; |
| 6652 |
} |
| 6653 |
|
| 6654 |
static int file_is_denylisted(const char *source, Hashmap *denylist) { |
| 6655 |
_cleanup_close_ int pfd = -EBADF; |
| 6656 |
struct stat st, rst; |
| 6657 |
int r; |
| 6658 |
|
| 6659 |
r = chase_and_stat(source, arg_copy_source, CHASE_PREFIX_ROOT, /* ret_path= */ NULL, &st); |
| 6660 |
if (r < 0) |
| 6661 |
return log_error_errno(r, "Failed to stat source file '%s/%s': %m", strempty(arg_copy_source), source); |
| 6662 |
|
| 6663 |
if (PTR_TO_INT(hashmap_get(denylist, &st)) == DENY_INODE) |
| 6664 |
return 1; |
| 6665 |
|
| 6666 |
if (stat(empty_to_root(arg_copy_source), &rst) < 0) |
| 6667 |
return log_error_errno(errno, "Failed to stat '%s': %m", empty_to_root(arg_copy_source)); |
| 6668 |
|
| 6669 |
pfd = chase_and_open_parent(source, arg_copy_source, CHASE_PREFIX_ROOT, /* ret_filename= */ NULL); |
| 6670 |
if (pfd < 0) |
| 6671 |
return log_error_errno(pfd, "Failed to chase '%s/%s': %m", strempty(arg_copy_source), source); |
| 6672 |
|
| 6673 |
for (;;) { |
| 6674 |
if (fstat(pfd, &st) < 0) |
| 6675 |
return log_error_errno(errno, "Failed to stat parent: %m"); |
| 6676 |
|
| 6677 |
if (PTR_TO_INT(hashmap_get(denylist, &st)) != DENY_DONT) |
| 6678 |
return 1; |
| 6679 |
|
| 6680 |
if (stat_inode_same(&st, &rst)) |
| 6681 |
break; |
| 6682 |
|
| 6683 |
_cleanup_close_ int new_pfd = openat(pfd, "..", O_DIRECTORY|O_RDONLY); |
| 6684 |
if (new_pfd < 0) |
| 6685 |
return log_error_errno(errno, "Failed to open parent directory: %m"); |
| 6686 |
|
| 6687 |
close_and_replace(pfd, new_pfd); |
| 6688 |
} |
| 6689 |
|
| 6690 |
return 0; |
| 6691 |
} |
| 6692 |
|
| 6693 |
static int do_copy_files(Context *context, Partition *p, const char *root) { |
| 6694 |
_cleanup_hashmap_free_ Hashmap *subvolumes = NULL; |
| 6695 |
int r; |
| 6696 |
|
| 6697 |
assert(p); |
| 6698 |
assert(root); |
| 6699 |
|
| 6700 |
r = make_subvolumes_hashmap(p, &subvolumes); |
| 6701 |
if (r < 0) |
| 6702 |
return r; |
| 6703 |
|
| 6704 |
_cleanup_free_ CopyFiles *copy_files = newdup(CopyFiles, p->copy_files, p->n_copy_files); |
| 6705 |
if (!copy_files) |
| 6706 |
return log_oom(); |
| 6707 |
|
| 6708 |
size_t n_copy_files = p->n_copy_files; |
| 6709 |
if (p->suppressing) { |
| 6710 |
if (!GREEDY_REALLOC_APPEND(copy_files, n_copy_files, |
| 6711 |
p->suppressing->copy_files, p->suppressing->n_copy_files)) |
| 6712 |
return log_oom(); |
| 6713 |
} |
| 6714 |
|
| 6715 |
bool copy_ownership = fstype_can_ownership(p->format); |
| 6716 |
|
| 6717 |
/* copy_tree_at() automatically copies the permissions of source directories to target directories if |
| 6718 |
* it created them. However, the root directory is created by us, so we have to manually take care |
| 6719 |
* that it is initialized. We use the first source directory targeting "/" as the metadata source for |
| 6720 |
* the root directory. */ |
| 6721 |
FOREACH_ARRAY(line, copy_files, n_copy_files) { |
| 6722 |
_cleanup_close_ int rfd = -EBADF, sfd = -EBADF; |
| 6723 |
|
| 6724 |
if (!path_equal(line->target, "/")) |
| 6725 |
continue; |
| 6726 |
|
| 6727 |
rfd = open(root, O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW); |
| 6728 |
if (rfd < 0) |
| 6729 |
return -errno; |
| 6730 |
|
| 6731 |
sfd = chase_and_open(line->source, arg_copy_source, CHASE_PREFIX_ROOT, O_PATH|O_DIRECTORY|O_CLOEXEC|O_NOCTTY, NULL); |
| 6732 |
if (sfd == -ENOTDIR) |
| 6733 |
continue; |
| 6734 |
if (sfd < 0) |
| 6735 |
return log_error_errno(sfd, "Failed to open source file '%s%s': %m", strempty(arg_copy_source), line->source); |
| 6736 |
|
| 6737 |
(void) copy_xattr(sfd, NULL, rfd, NULL, COPY_ALL_XATTRS); |
| 6738 |
if (copy_ownership) |
| 6739 |
(void) copy_access(sfd, rfd); |
| 6740 |
(void) copy_times(sfd, rfd, 0); |
| 6741 |
|
| 6742 |
break; |
| 6743 |
} |
| 6744 |
|
| 6745 |
uid_t uid = copy_ownership ? UID_INVALID : getuid(); |
| 6746 |
gid_t gid = copy_ownership ? GID_INVALID : getgid(); |
| 6747 |
|
| 6748 |
FOREACH_ARRAY(line, copy_files, n_copy_files) { |
| 6749 |
_cleanup_hashmap_free_ Hashmap *denylist = NULL; |
| 6750 |
_cleanup_hashmap_free_ Hashmap *subvolumes_by_source_inode = NULL; |
| 6751 |
_cleanup_close_ int sfd = -EBADF, pfd = -EBADF, tfd = -EBADF; |
| 6752 |
usec_t ts = parse_source_date_epoch(); |
| 6753 |
|
| 6754 |
r = make_copy_files_denylist(context, p, line->source, line->target, &denylist); |
| 6755 |
if (r < 0) |
| 6756 |
return r; |
| 6757 |
if (r > 0) |
| 6758 |
continue; |
| 6759 |
|
| 6760 |
r = make_subvolumes_by_source_inode_hashmap(p, line->source, line->target, &subvolumes_by_source_inode); |
| 6761 |
if (r < 0) |
| 6762 |
return r; |
| 6763 |
|
| 6764 |
sfd = chase_and_open(line->source, arg_copy_source, CHASE_PREFIX_ROOT, O_CLOEXEC|O_NOCTTY, NULL); |
| 6765 |
if (sfd == -ENOENT) { |
| 6766 |
log_notice_errno(sfd, "Failed to open source file '%s%s', skipping: %m", strempty(arg_copy_source), line->source); |
| 6767 |
continue; |
| 6768 |
} |
| 6769 |
if (sfd < 0) |
| 6770 |
return log_error_errno(sfd, "Failed to open source file '%s%s': %m", strempty(arg_copy_source), line->source); |
| 6771 |
|
| 6772 |
r = fd_verify_regular(sfd); |
| 6773 |
if (r < 0) { |
| 6774 |
if (r != -EISDIR) |
| 6775 |
return log_error_errno(r, "Failed to check type of source file '%s': %m", line->source); |
| 6776 |
|
| 6777 |
/* We are looking at a directory */ |
| 6778 |
tfd = chase_and_open(line->target, root, CHASE_PREFIX_ROOT, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL); |
| 6779 |
if (tfd < 0) { |
| 6780 |
_cleanup_free_ char *dn = NULL, *fn = NULL; |
| 6781 |
|
| 6782 |
if (tfd != -ENOENT) |
| 6783 |
return log_error_errno(tfd, "Failed to open target directory '%s': %m", line->target); |
| 6784 |
|
| 6785 |
r = path_extract_filename(line->target, &fn); |
| 6786 |
if (r < 0) |
| 6787 |
return log_error_errno(r, "Failed to extract filename from '%s': %m", line->target); |
| 6788 |
|
| 6789 |
r = path_extract_directory(line->target, &dn); |
| 6790 |
if (r < 0) |
| 6791 |
return log_error_errno(r, "Failed to extract directory from '%s': %m", line->target); |
| 6792 |
|
| 6793 |
r = mkdir_p_root_full(root, dn, UID_INVALID, GID_INVALID, 0755, ts, subvolumes); |
| 6794 |
if (r < 0) |
| 6795 |
return log_error_errno(r, "Failed to create parent directory '%s': %m", dn); |
| 6796 |
|
| 6797 |
pfd = chase_and_open(dn, root, CHASE_PREFIX_ROOT, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL); |
| 6798 |
if (pfd < 0) |
| 6799 |
return log_error_errno(pfd, "Failed to open parent directory of target: %m"); |
| 6800 |
|
| 6801 |
r = copy_tree_at( |
| 6802 |
sfd, ".", |
| 6803 |
pfd, fn, |
| 6804 |
uid, gid, |
| 6805 |
line->flags, |
| 6806 |
denylist, subvolumes_by_source_inode); |
| 6807 |
} else |
| 6808 |
r = copy_tree_at( |
| 6809 |
sfd, ".", |
| 6810 |
tfd, ".", |
| 6811 |
uid, gid, |
| 6812 |
line->flags, |
| 6813 |
denylist, subvolumes_by_source_inode); |
| 6814 |
if (r < 0) |
| 6815 |
return log_error_errno(r, "Failed to copy '%s%s' to '%s%s': %m", |
| 6816 |
strempty(arg_copy_source), line->source, strempty(root), line->target); |
| 6817 |
} else { |
| 6818 |
_cleanup_free_ char *dn = NULL, *fn = NULL; |
| 6819 |
|
| 6820 |
/* We are looking at a regular file */ |
| 6821 |
|
| 6822 |
r = file_is_denylisted(line->source, denylist); |
| 6823 |
if (r < 0) |
| 6824 |
return r; |
| 6825 |
if (r > 0) { |
| 6826 |
log_debug("%s is in the denylist, ignoring", line->source); |
| 6827 |
continue; |
| 6828 |
} |
| 6829 |
|
| 6830 |
r = path_extract_filename(line->target, &fn); |
| 6831 |
if (r == -EADDRNOTAVAIL || r == O_DIRECTORY) |
| 6832 |
return log_error_errno(SYNTHETIC_ERRNO(EISDIR), |
| 6833 |
"Target path '%s' refers to a directory, but source path '%s' refers to regular file, can't copy.", line->target, line->source); |
| 6834 |
if (r < 0) |
| 6835 |
return log_error_errno(r, "Failed to extract filename from '%s': %m", line->target); |
| 6836 |
|
| 6837 |
r = path_extract_directory(line->target, &dn); |
| 6838 |
if (r < 0) |
| 6839 |
return log_error_errno(r, "Failed to extract directory from '%s': %m", line->target); |
| 6840 |
|
| 6841 |
r = mkdir_p_root_full(root, dn, UID_INVALID, GID_INVALID, 0755, ts, subvolumes); |
| 6842 |
if (r < 0) |
| 6843 |
return log_error_errno(r, "Failed to create parent directory: %m"); |
| 6844 |
|
| 6845 |
pfd = chase_and_open(dn, root, CHASE_PREFIX_ROOT, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL); |
| 6846 |
if (pfd < 0) |
| 6847 |
return log_error_errno(pfd, "Failed to open parent directory of target: %m"); |
| 6848 |
|
| 6849 |
tfd = openat(pfd, fn, O_CREAT|O_EXCL|O_WRONLY|O_CLOEXEC, 0700); |
| 6850 |
if (tfd < 0) |
| 6851 |
return log_error_errno(errno, "Failed to create target file '%s': %m", line->target); |
| 6852 |
|
| 6853 |
r = copy_bytes(sfd, tfd, UINT64_MAX, COPY_REFLINK|COPY_HOLES|COPY_SIGINT|COPY_TRUNCATE); |
| 6854 |
if (r < 0) |
| 6855 |
return log_error_errno(r, "Failed to copy '%s' to '%s%s': %m", line->source, strempty(arg_copy_source), line->target); |
| 6856 |
|
| 6857 |
(void) copy_xattr(sfd, NULL, tfd, NULL, COPY_ALL_XATTRS); |
| 6858 |
if (copy_ownership) |
| 6859 |
(void) copy_access(sfd, tfd); |
| 6860 |
(void) copy_times(sfd, tfd, 0); |
| 6861 |
|
| 6862 |
if (ts != USEC_INFINITY) { |
| 6863 |
struct timespec tspec; |
| 6864 |
timespec_store(&tspec, ts); |
| 6865 |
|
| 6866 |
if (futimens(pfd, (const struct timespec[2]) { TIMESPEC_OMIT, tspec }) < 0) |
| 6867 |
return -errno; |
| 6868 |
} |
| 6869 |
} |
| 6870 |
} |
| 6871 |
|
| 6872 |
return 0; |
| 6873 |
} |
| 6874 |
|
| 6875 |
static int do_make_directories(Partition *p, const char *root) { |
| 6876 |
_cleanup_hashmap_free_ Hashmap *subvolumes = NULL; |
| 6877 |
_cleanup_free_ char **override_dirs = NULL; |
| 6878 |
int r; |
| 6879 |
|
| 6880 |
assert(p); |
| 6881 |
assert(root); |
| 6882 |
|
| 6883 |
r = make_subvolumes_hashmap(p, &subvolumes); |
| 6884 |
if (r < 0) |
| 6885 |
return r; |
| 6886 |
|
| 6887 |
if (p->suppressing) { |
| 6888 |
r = shallow_join_strv(&override_dirs, p->make_directories, p->suppressing->make_directories); |
| 6889 |
if (r < 0) |
| 6890 |
return r; |
| 6891 |
} |
| 6892 |
|
| 6893 |
STRV_FOREACH(d, override_dirs ?: p->make_directories) { |
| 6894 |
r = mkdir_p_root_full(root, *d, UID_INVALID, GID_INVALID, 0755, parse_source_date_epoch(), subvolumes); |
| 6895 |
if (r < 0) |
| 6896 |
return log_error_errno(r, "Failed to create directory '%s' in file system: %m", *d); |
| 6897 |
} |
| 6898 |
|
| 6899 |
return 0; |
| 6900 |
} |
| 6901 |
|
| 6902 |
static int do_make_symlinks(Partition *p, const char *root) { |
| 6903 |
int r; |
| 6904 |
|
| 6905 |
assert(p); |
| 6906 |
assert(root); |
| 6907 |
|
| 6908 |
STRV_FOREACH_PAIR(path, target, p->make_symlinks) { |
| 6909 |
_cleanup_close_ int parent_fd = -EBADF; |
| 6910 |
_cleanup_free_ char *f = NULL; |
| 6911 |
|
| 6912 |
r = chase(*path, root, CHASE_PREFIX_ROOT|CHASE_PARENT|CHASE_EXTRACT_FILENAME, &f, &parent_fd); |
| 6913 |
if (r < 0) |
| 6914 |
return log_error_errno(r, "Failed to resolve %s in %s", *path, root); |
| 6915 |
|
| 6916 |
if (symlinkat(*target, parent_fd, f) < 0) |
| 6917 |
return log_error_errno(errno, "Failed to create symlink at %s to %s: %m", *path, *target); |
| 6918 |
} |
| 6919 |
|
| 6920 |
return 0; |
| 6921 |
} |
| 6922 |
|
| 6923 |
static int make_subvolumes_read_only(Partition *p, const char *root) { |
| 6924 |
_cleanup_free_ char *path = NULL; |
| 6925 |
Subvolume *subvolume; |
| 6926 |
int r; |
| 6927 |
|
| 6928 |
ORDERED_HASHMAP_FOREACH(subvolume, p->subvolumes) { |
| 6929 |
if (!FLAGS_SET(subvolume->flags, BTRFS_SUBVOL_RO)) |
| 6930 |
continue; |
| 6931 |
|
| 6932 |
path = path_join(root, subvolume->path); |
| 6933 |
if (!path) |
| 6934 |
return log_oom(); |
| 6935 |
|
| 6936 |
r = btrfs_subvol_set_read_only(path, true); |
| 6937 |
if (r < 0) |
| 6938 |
return log_error_errno(r, "Failed to make subvolume '%s' read-only: %m", subvolume->path); |
| 6939 |
} |
| 6940 |
|
| 6941 |
if (p->suppressing) { |
| 6942 |
r = make_subvolumes_read_only(p->suppressing, root); |
| 6943 |
if (r < 0) |
| 6944 |
return r; |
| 6945 |
} |
| 6946 |
|
| 6947 |
return 0; |
| 6948 |
} |
| 6949 |
|
| 6950 |
static int set_default_subvolume(Partition *p, const char *root) { |
| 6951 |
_cleanup_free_ char *path = NULL; |
| 6952 |
int r; |
| 6953 |
|
| 6954 |
assert(p); |
| 6955 |
assert(root); |
| 6956 |
|
| 6957 |
if (!p->default_subvolume) |
| 6958 |
return 0; |
| 6959 |
|
| 6960 |
path = path_join(root, p->default_subvolume); |
| 6961 |
if (!path) |
| 6962 |
return log_oom(); |
| 6963 |
|
| 6964 |
r = btrfs_subvol_make_default(path); |
| 6965 |
if (r < 0) |
| 6966 |
return log_error_errno(r, "Failed to make '%s' the default subvolume: %m", p->default_subvolume); |
| 6967 |
|
| 6968 |
return 0; |
| 6969 |
} |
| 6970 |
|
| 6971 |
static int partition_acquire_sibling_labels(const Partition *p, char ***ret) { |
| 6972 |
assert(p); |
| 6973 |
assert(ret); |
| 6974 |
|
| 6975 |
_cleanup_strv_free_ char **l = NULL; |
| 6976 |
if (p->new_label) { |
| 6977 |
l = strv_new(p->new_label); |
| 6978 |
if (!l) |
| 6979 |
return log_oom(); |
| 6980 |
} |
| 6981 |
|
| 6982 |
FOREACH_ELEMENT(sibling, p->siblings) { |
| 6983 |
Partition *s = *sibling; |
| 6984 |
|
| 6985 |
if (!s || s == p || !s->new_label || strv_contains(l, s->new_label)) |
| 6986 |
continue; |
| 6987 |
|
| 6988 |
if (strv_extend(&l, s->new_label) < 0) |
| 6989 |
return log_oom(); |
| 6990 |
} |
| 6991 |
|
| 6992 |
strv_sort(l); /* bring into a systematic order to make things reproducible */ |
| 6993 |
|
| 6994 |
*ret = TAKE_PTR(l); |
| 6995 |
return 0; |
| 6996 |
} |
| 6997 |
|
| 6998 |
static int partition_acquire_sibling_uuids(const Partition *p, char ***ret) { |
| 6999 |
assert(p); |
| 7000 |
assert(ret); |
| 7001 |
|
| 7002 |
_cleanup_strv_free_ char **l = NULL; |
| 7003 |
l = strv_new(SD_ID128_TO_UUID_STRING(p->type.uuid)); |
| 7004 |
if (!l) |
| 7005 |
return log_oom(); |
| 7006 |
|
| 7007 |
FOREACH_ELEMENT(sibling, p->siblings) { |
| 7008 |
Partition *s = *sibling; |
| 7009 |
|
| 7010 |
if (!s || s == p) |
| 7011 |
continue; |
| 7012 |
|
| 7013 |
const char *u = SD_ID128_TO_UUID_STRING(s->type.uuid); |
| 7014 |
if (strv_contains(l, u)) |
| 7015 |
continue; |
| 7016 |
|
| 7017 |
if (strv_extend(&l, u) < 0) |
| 7018 |
return log_oom(); |
| 7019 |
} |
| 7020 |
|
| 7021 |
strv_sort(l); /* bring into a systematic order to make things reproducible */ |
| 7022 |
|
| 7023 |
*ret = TAKE_PTR(l); |
| 7024 |
return 0; |
| 7025 |
} |
| 7026 |
|
| 7027 |
|
| 7028 |
static int do_make_validatefs_xattrs(const Partition *p, const char *root) { |
| 7029 |
int r; |
| 7030 |
|
| 7031 |
assert(p); |
| 7032 |
assert(root); |
| 7033 |
|
| 7034 |
if (!partition_add_validatefs(p)) |
| 7035 |
return 0; |
| 7036 |
|
| 7037 |
_cleanup_close_ int fd = open(root, O_DIRECTORY|O_CLOEXEC); |
| 7038 |
if (fd < 0) |
| 7039 |
return log_error_errno(errno, "Failed to open root inode '%s': %m", root); |
| 7040 |
|
| 7041 |
_cleanup_strv_free_ char **l = NULL; |
| 7042 |
r = partition_acquire_sibling_labels(p, &l); |
| 7043 |
if (r < 0) |
| 7044 |
return r; |
| 7045 |
if (!strv_isempty(l)) { |
| 7046 |
r = xsetxattr_strv(fd, /* path= */ NULL, AT_EMPTY_PATH, "user.validatefs.gpt_label", l); |
| 7047 |
if (r < 0) |
| 7048 |
return log_error_errno(r, "Failed to set 'user.validatefs.gpt_label' extended attribute: %m"); |
| 7049 |
} |
| 7050 |
l = strv_free(l); |
| 7051 |
|
| 7052 |
r = partition_acquire_sibling_uuids(p, &l); |
| 7053 |
if (r < 0) |
| 7054 |
return r; |
| 7055 |
r = xsetxattr_strv(fd, /* path= */ NULL, AT_EMPTY_PATH, "user.validatefs.gpt_type_uuid", l); |
| 7056 |
if (r < 0) |
| 7057 |
return log_error_errno(r, "Failed to set 'user.validatefs.gpt_type_uuid' extended attribute: %m"); |
| 7058 |
l = strv_free(l); |
| 7059 |
|
| 7060 |
/* Prefer the data from MountPoint= if specified, otherwise use data we derive from the partition type */ |
| 7061 |
if (p->n_mountpoints > 0) { |
| 7062 |
FOREACH_ARRAY(m, p->mountpoints, p->n_mountpoints) |
| 7063 |
if (strv_extend(&l, m->where) < 0) |
| 7064 |
return log_oom(); |
| 7065 |
} else { |
| 7066 |
const char *m = gpt_partition_type_mountpoint_nulstr(p->type); |
| 7067 |
if (m) { |
| 7068 |
l = strv_split_nulstr(m); |
| 7069 |
if (!l) |
| 7070 |
return log_oom(); |
| 7071 |
} |
| 7072 |
} |
| 7073 |
|
| 7074 |
if (!strv_isempty(l)) { |
| 7075 |
r = xsetxattr_strv(fd, /* path= */ NULL, AT_EMPTY_PATH, "user.validatefs.mount_point", l); |
| 7076 |
if (r < 0) |
| 7077 |
return log_error_errno(r, "Failed to set 'user.validatefs.mount_point' extended attribute: %m"); |
| 7078 |
} |
| 7079 |
|
| 7080 |
return 0; |
| 7081 |
} |
| 7082 |
|
| 7083 |
static int partition_populate_directory(Context *context, Partition *p, char **ret) { |
| 7084 |
_cleanup_(rm_rf_physical_and_freep) char *root = NULL; |
| 7085 |
const char *vt; |
| 7086 |
int r; |
| 7087 |
|
| 7088 |
assert(ret); |
| 7089 |
|
| 7090 |
log_info("Preparing to populate %s filesystem.", p->format); |
| 7091 |
|
| 7092 |
r = var_tmp_dir(&vt); |
| 7093 |
if (r < 0) |
| 7094 |
return log_error_errno(r, "Could not determine temporary directory: %m"); |
| 7095 |
|
| 7096 |
r = tempfn_random_child(vt, "repart", &root); |
| 7097 |
if (r < 0) |
| 7098 |
return log_error_errno(r, "Failed to generate temporary directory: %m"); |
| 7099 |
|
| 7100 |
r = mkdir(root, 0755); |
| 7101 |
if (r < 0) |
| 7102 |
return log_error_errno(errno, "Failed to create temporary directory: %m"); |
| 7103 |
|
| 7104 |
r = do_copy_files(context, p, root); |
| 7105 |
if (r < 0) |
| 7106 |
return r; |
| 7107 |
|
| 7108 |
r = do_make_directories(p, root); |
| 7109 |
if (r < 0) |
| 7110 |
return r; |
| 7111 |
|
| 7112 |
r = do_make_symlinks(p, root); |
| 7113 |
if (r < 0) |
| 7114 |
return r; |
| 7115 |
|
| 7116 |
r = do_make_validatefs_xattrs(p, root); |
| 7117 |
if (r < 0) |
| 7118 |
return r; |
| 7119 |
|
| 7120 |
log_info("Ready to populate %s filesystem.", p->format); |
| 7121 |
|
| 7122 |
*ret = TAKE_PTR(root); |
| 7123 |
return 0; |
| 7124 |
} |
| 7125 |
|
| 7126 |
static int partition_populate_filesystem(Context *context, Partition *p, const char *node) { |
| 7127 |
int r; |
| 7128 |
|
| 7129 |
assert(p); |
| 7130 |
assert(node); |
| 7131 |
|
| 7132 |
log_info("Populating %s filesystem.", p->format); |
| 7133 |
|
| 7134 |
/* We copy in a child process, since we have to mount the fs for that, and we don't want that fs to |
| 7135 |
* appear in the host namespace. Hence we fork a child that has its own file system namespace and |
| 7136 |
* detached mount propagation. */ |
| 7137 |
|
| 7138 |
(void) DLOPEN_LIBMOUNT(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 7139 |
|
| 7140 |
r = pidref_safe_fork( |
| 7141 |
"(sd-copy)", |
| 7142 |
FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, |
| 7143 |
/* ret= */ NULL); |
| 7144 |
if (r < 0) |
| 7145 |
return r; |
| 7146 |
if (r == 0) { |
| 7147 |
static const char fs[] = "/run/systemd/mount-root"; |
| 7148 |
/* This is a child process with its own mount namespace and propagation to host turned off */ |
| 7149 |
|
| 7150 |
r = mkdir_p(fs, 0700); |
| 7151 |
if (r < 0) { |
| 7152 |
log_error_errno(r, "Failed to create mount point: %m"); |
| 7153 |
_exit(EXIT_FAILURE); |
| 7154 |
} |
| 7155 |
|
| 7156 |
if (mount_nofollow_verbose(LOG_ERR, node, fs, p->format, MS_NOATIME|MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL) < 0) |
| 7157 |
_exit(EXIT_FAILURE); |
| 7158 |
|
| 7159 |
if (do_copy_files(context, p, fs) < 0) |
| 7160 |
_exit(EXIT_FAILURE); |
| 7161 |
|
| 7162 |
if (do_make_directories(p, fs) < 0) |
| 7163 |
_exit(EXIT_FAILURE); |
| 7164 |
|
| 7165 |
if (do_make_symlinks(p, fs) < 0) |
| 7166 |
_exit(EXIT_FAILURE); |
| 7167 |
|
| 7168 |
if (do_make_validatefs_xattrs(p, fs) < 0) |
| 7169 |
_exit(EXIT_FAILURE); |
| 7170 |
|
| 7171 |
if (make_subvolumes_read_only(p, fs) < 0) |
| 7172 |
_exit(EXIT_FAILURE); |
| 7173 |
|
| 7174 |
if (set_default_subvolume(p, fs) < 0) |
| 7175 |
_exit(EXIT_FAILURE); |
| 7176 |
|
| 7177 |
r = syncfs_path(AT_FDCWD, fs); |
| 7178 |
if (r < 0) { |
| 7179 |
log_error_errno(r, "Failed to synchronize written files: %m"); |
| 7180 |
_exit(EXIT_FAILURE); |
| 7181 |
} |
| 7182 |
|
| 7183 |
_exit(EXIT_SUCCESS); |
| 7184 |
} |
| 7185 |
|
| 7186 |
log_info("Successfully populated %s filesystem.", p->format); |
| 7187 |
return 0; |
| 7188 |
} |
| 7189 |
|
| 7190 |
static int append_btrfs_subvols(char ***l, OrderedHashmap *subvolumes, const char *default_subvolume) { |
| 7191 |
Subvolume *subvolume; |
| 7192 |
int r; |
| 7193 |
|
| 7194 |
assert(l); |
| 7195 |
|
| 7196 |
ORDERED_HASHMAP_FOREACH(subvolume, subvolumes) { |
| 7197 |
_cleanup_free_ char *s = NULL; |
| 7198 |
|
| 7199 |
if (streq_ptr(subvolume->path, default_subvolume) && !strextend(&s, "default")) |
| 7200 |
return log_oom(); |
| 7201 |
|
| 7202 |
if (FLAGS_SET(subvolume->flags, BTRFS_SUBVOL_RO) && !strextend_with_separator(&s, "-", "ro")) |
| 7203 |
return log_oom(); |
| 7204 |
|
| 7205 |
if (!strextend_with_separator(&s, ":", subvolume->path)) |
| 7206 |
return log_oom(); |
| 7207 |
|
| 7208 |
r = strv_extend_many(l, "--subvol", s); |
| 7209 |
if (r < 0) |
| 7210 |
return log_oom(); |
| 7211 |
} |
| 7212 |
|
| 7213 |
return 0; |
| 7214 |
} |
| 7215 |
|
| 7216 |
static int append_btrfs_inode_flags(char ***l, OrderedHashmap *subvolumes) { |
| 7217 |
Subvolume *subvolume; |
| 7218 |
int r; |
| 7219 |
|
| 7220 |
assert(l); |
| 7221 |
|
| 7222 |
ORDERED_HASHMAP_FOREACH(subvolume, subvolumes) { |
| 7223 |
if (!FLAGS_SET(subvolume->flags, BTRFS_SUBVOL_NODATACOW)) |
| 7224 |
continue; |
| 7225 |
|
| 7226 |
_cleanup_free_ char *s = strjoin("nodatacow:", subvolume->path); |
| 7227 |
if (!s) |
| 7228 |
return log_oom(); |
| 7229 |
|
| 7230 |
r = strv_extend_many(l, "--inode-flags", s); |
| 7231 |
if (r < 0) |
| 7232 |
return log_oom(); |
| 7233 |
} |
| 7234 |
|
| 7235 |
return 0; |
| 7236 |
} |
| 7237 |
|
| 7238 |
static int finalize_extra_mkfs_options(const Partition *p, const char *root, char ***ret) { |
| 7239 |
_cleanup_strv_free_ char **sv = NULL; |
| 7240 |
int r; |
| 7241 |
|
| 7242 |
assert(p); |
| 7243 |
assert(ret); |
| 7244 |
|
| 7245 |
r = mkfs_options_from_env("REPART", p->format, &sv); |
| 7246 |
if (r < 0) |
| 7247 |
return log_error_errno(r, |
| 7248 |
"Failed to determine mkfs command line options for '%s': %m", |
| 7249 |
p->format); |
| 7250 |
|
| 7251 |
if (partition_needs_populate(p) && root && streq(p->format, "btrfs")) { |
| 7252 |
r = append_btrfs_subvols(&sv, p->subvolumes, p->default_subvolume); |
| 7253 |
if (r < 0) |
| 7254 |
return r; |
| 7255 |
|
| 7256 |
r = append_btrfs_inode_flags(&sv, p->subvolumes); |
| 7257 |
if (r < 0) |
| 7258 |
return r; |
| 7259 |
|
| 7260 |
if (p->suppressing) { |
| 7261 |
r = append_btrfs_subvols(&sv, p->suppressing->subvolumes, NULL); |
| 7262 |
if (r < 0) |
| 7263 |
return r; |
| 7264 |
|
| 7265 |
r = append_btrfs_inode_flags(&sv, p->suppressing->subvolumes); |
| 7266 |
if (r < 0) |
| 7267 |
return r; |
| 7268 |
} |
| 7269 |
|
| 7270 |
if (p->minimize != MINIMIZE_OFF && strv_extend(&sv, "--shrink") < 0) |
| 7271 |
return log_oom(); |
| 7272 |
} |
| 7273 |
|
| 7274 |
*ret = TAKE_PTR(sv); |
| 7275 |
return 0; |
| 7276 |
} |
| 7277 |
|
| 7278 |
static int context_block_device_replace(Context *context) { |
| 7279 |
int r; |
| 7280 |
|
| 7281 |
assert(context); |
| 7282 |
|
| 7283 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 7284 |
_cleanup_(partition_target_freep) PartitionTarget *t = NULL; |
| 7285 |
|
| 7286 |
if (p->dropped) |
| 7287 |
continue; |
| 7288 |
|
| 7289 |
if (PARTITION_EXISTS(p)) |
| 7290 |
continue; |
| 7291 |
|
| 7292 |
if (!p->btrfs_replaced) |
| 7293 |
continue; |
| 7294 |
|
| 7295 |
if (partition_defer(context, p)) |
| 7296 |
continue; |
| 7297 |
|
| 7298 |
assert(!p->btrfs_replaced->done); |
| 7299 |
|
| 7300 |
(void) context_notify(context, PROGRESS_REPLACING_DEVICE, p->definition_path, UINT_MAX); |
| 7301 |
|
| 7302 |
assert(p->offset != UINT64_MAX); |
| 7303 |
assert(p->new_size != UINT64_MAX); |
| 7304 |
|
| 7305 |
r = partition_target_prepare(context, p, |
| 7306 |
p->new_size, |
| 7307 |
/* need_path= */ true, |
| 7308 |
&t); |
| 7309 |
if (r < 0) |
| 7310 |
return r; |
| 7311 |
|
| 7312 |
if (p->encrypt != ENCRYPT_OFF) { |
| 7313 |
r = partition_encrypt(context, p, t, /* offline= */ false, /* temporary= */ false); |
| 7314 |
if (r < 0) |
| 7315 |
return log_error_errno(r, "Failed to encrypt device: %m"); |
| 7316 |
} |
| 7317 |
|
| 7318 |
log_info("Replacing partition %" PRIu64 ".", p->partno); |
| 7319 |
|
| 7320 |
/* btrfs_replace calls a synchronous ioctl and will return when replace is finished */ |
| 7321 |
r = btrfs_replace(p->btrfs_replaced->mountpoint_fd, p->btrfs_replaced->devid, partition_target_path(t)); |
| 7322 |
if (r < 0) |
| 7323 |
return log_error_errno(r, "Failed to replace btrfs device on partition %" PRIu64 ": %m", p->partno); |
| 7324 |
|
| 7325 |
p->btrfs_replaced->done = true; |
| 7326 |
|
| 7327 |
if (t->decrypted) |
| 7328 |
t->decrypted->keep = true; |
| 7329 |
|
| 7330 |
log_info("Successfully replaced partition %" PRIu64 ".", p->partno); |
| 7331 |
} |
| 7332 |
|
| 7333 |
return 0; |
| 7334 |
} |
| 7335 |
|
| 7336 |
static void context_btrfs_replace_resize(Context *context) { |
| 7337 |
int r; |
| 7338 |
|
| 7339 |
assert(context); |
| 7340 |
|
| 7341 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 7342 |
if (!p->btrfs_replaced) |
| 7343 |
continue; |
| 7344 |
|
| 7345 |
if (!p->btrfs_replaced->done) |
| 7346 |
continue; |
| 7347 |
|
| 7348 |
r = btrfs_resize_max(p->btrfs_replaced->mountpoint_fd, p->btrfs_replaced->devid); |
| 7349 |
if (r < 0) |
| 7350 |
log_warning_errno(r, "Could not resize btrfs filesystem moved to partition %" PRIu64 ", proceeding without resizing: %m", p->partno); |
| 7351 |
else |
| 7352 |
log_info("Successfully resized partition %" PRIu64 ".", p->partno); |
| 7353 |
} |
| 7354 |
} |
| 7355 |
|
| 7356 |
static void context_btrfs_replace_back(Context *context) { |
| 7357 |
int r; |
| 7358 |
|
| 7359 |
assert(context); |
| 7360 |
|
| 7361 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 7362 |
if (!p->btrfs_replaced) |
| 7363 |
continue; |
| 7364 |
|
| 7365 |
if (!p->btrfs_replaced->done) |
| 7366 |
continue; |
| 7367 |
|
| 7368 |
r = btrfs_replace(p->btrfs_replaced->mountpoint_fd, p->btrfs_replaced->devid, p->btrfs_replaced->source_path); |
| 7369 |
if (r < 0) |
| 7370 |
log_warning_errno(r, "Could not move back btrfs filesystem from partition %" PRIu64 ", leaving it on new device: %m", p->partno); |
| 7371 |
} |
| 7372 |
} |
| 7373 |
|
| 7374 |
static int check_mkfs(const Context *context) { |
| 7375 |
int r; |
| 7376 |
|
| 7377 |
assert(context); |
| 7378 |
|
| 7379 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 7380 |
if (p->dropped) |
| 7381 |
continue; |
| 7382 |
|
| 7383 |
if (PARTITION_EXISTS(p)) /* Never format existing partitions */ |
| 7384 |
continue; |
| 7385 |
|
| 7386 |
if (!p->format) |
| 7387 |
continue; |
| 7388 |
|
| 7389 |
if (partition_defer(context, p)) |
| 7390 |
continue; |
| 7391 |
|
| 7392 |
/* For offline signing case */ |
| 7393 |
if (!set_isempty(arg_verity_settings) && partition_designator_is_verity_sig(p->type.designator)) |
| 7394 |
continue; |
| 7395 |
|
| 7396 |
/* Minimized partitions will use the copy blocks logic so skip those here. */ |
| 7397 |
if (p->copy_blocks_fd >= 0) |
| 7398 |
continue; |
| 7399 |
|
| 7400 |
/* We don't yet quite know if have_root= will be true, so just pass -1 which |
| 7401 |
* means "not sure". */ |
| 7402 |
r = mkfs_find_or_warn(p->format, /* have_root= */ -1, /* ret= */ NULL); |
| 7403 |
if (r < 0) |
| 7404 |
return r; |
| 7405 |
} |
| 7406 |
|
| 7407 |
return 0; |
| 7408 |
} |
| 7409 |
|
| 7410 |
static int context_mkfs(Context *context) { |
| 7411 |
int r; |
| 7412 |
|
| 7413 |
assert(context); |
| 7414 |
|
| 7415 |
/* Make a file system */ |
| 7416 |
|
| 7417 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 7418 |
_cleanup_(rm_rf_physical_and_freep) char *root = NULL; |
| 7419 |
_cleanup_(partition_target_freep) PartitionTarget *t = NULL; |
| 7420 |
_cleanup_strv_free_ char **extra_mkfs_options = NULL; |
| 7421 |
|
| 7422 |
if (p->dropped) |
| 7423 |
continue; |
| 7424 |
|
| 7425 |
if (PARTITION_EXISTS(p)) /* Never format existing partitions */ |
| 7426 |
continue; |
| 7427 |
|
| 7428 |
if (!p->format) |
| 7429 |
continue; |
| 7430 |
|
| 7431 |
if (partition_defer(context, p)) |
| 7432 |
continue; |
| 7433 |
|
| 7434 |
/* For offline signing case */ |
| 7435 |
if (!set_isempty(arg_verity_settings) && partition_designator_is_verity_sig(p->type.designator)) |
| 7436 |
return partition_format_verity_sig(context, p); |
| 7437 |
|
| 7438 |
/* Minimized partitions will use the copy blocks logic so skip those here. */ |
| 7439 |
if (p->copy_blocks_fd >= 0) |
| 7440 |
continue; |
| 7441 |
|
| 7442 |
(void) context_notify(context, PROGRESS_FORMATTING_PARTITION, p->definition_path, UINT_MAX); |
| 7443 |
|
| 7444 |
assert(p->offset != UINT64_MAX); |
| 7445 |
assert(p->new_size != UINT64_MAX); |
| 7446 |
assert(p->new_size >= (p->encrypt != ENCRYPT_OFF ? LUKS2_METADATA_KEEP_FREE : 0)); |
| 7447 |
|
| 7448 |
/* If we're doing encryption, keep free space at the end which is required |
| 7449 |
* for cryptsetup's offline encryption. */ |
| 7450 |
r = partition_target_prepare(context, p, |
| 7451 |
p->new_size - (p->encrypt != ENCRYPT_OFF ? LUKS2_METADATA_KEEP_FREE : 0), |
| 7452 |
/* need_path= */ true, |
| 7453 |
&t); |
| 7454 |
if (r < 0) |
| 7455 |
return r; |
| 7456 |
|
| 7457 |
if (p->encrypt != ENCRYPT_OFF && (t->loop || t->block_partition)) { |
| 7458 |
r = partition_target_grow(t, p->new_size); |
| 7459 |
if (r < 0) |
| 7460 |
return r; |
| 7461 |
|
| 7462 |
r = partition_encrypt(context, p, t, /* offline= */ false, /* temporary= */ true); |
| 7463 |
if (r < 0) |
| 7464 |
return log_error_errno(r, "Failed to encrypt device: %m"); |
| 7465 |
} |
| 7466 |
|
| 7467 |
log_info("Formatting future partition %" PRIu64 ".", p->partno); |
| 7468 |
|
| 7469 |
/* If we're not writing to a loop device or if we're populating a read-only filesystem, we |
| 7470 |
* have to populate using the filesystem's mkfs's --root= (or equivalent) option. To do that, |
| 7471 |
* we need to set up the final directory tree beforehand. */ |
| 7472 |
|
| 7473 |
if (partition_needs_populate(p) && |
| 7474 |
((!t->loop && !t->block_partition) || fstype_is_ro(p->format) || (streq_ptr(p->format, "btrfs") && p->compression))) { |
| 7475 |
if (!mkfs_supports_root_option(p->format)) |
| 7476 |
return log_error_errno(SYNTHETIC_ERRNO(ENODEV), |
| 7477 |
"Loop device or block partition access is required to populate %s filesystems.", |
| 7478 |
p->format); |
| 7479 |
|
| 7480 |
r = partition_populate_directory(context, p, &root); |
| 7481 |
if (r < 0) |
| 7482 |
return r; |
| 7483 |
} |
| 7484 |
|
| 7485 |
r = finalize_extra_mkfs_options(p, root, &extra_mkfs_options); |
| 7486 |
if (r < 0) |
| 7487 |
return r; |
| 7488 |
|
| 7489 |
r = make_filesystem( |
| 7490 |
partition_target_path(t), |
| 7491 |
p->format, |
| 7492 |
strempty(p->new_label), |
| 7493 |
root, |
| 7494 |
p->fs_uuid, |
| 7495 |
partition_mkfs_flags(p), |
| 7496 |
partition_fs_sector_size(context, p), |
| 7497 |
p->compression, |
| 7498 |
p->compression_level, |
| 7499 |
extra_mkfs_options); |
| 7500 |
if (r < 0) |
| 7501 |
return r; |
| 7502 |
|
| 7503 |
/* The mkfs binary we invoked might have removed our temporary file when we're not operating |
| 7504 |
* on a loop device, so open the file again to make sure our file descriptor points to actual |
| 7505 |
* new file. */ |
| 7506 |
|
| 7507 |
if (t->fd >= 0 && t->path && !t->loop && !t->block_partition) { |
| 7508 |
safe_close(t->fd); |
| 7509 |
t->fd = open(t->path, O_RDWR|O_CLOEXEC); |
| 7510 |
if (t->fd < 0) |
| 7511 |
return log_error_errno(errno, "Failed to reopen temporary file: %m"); |
| 7512 |
} |
| 7513 |
|
| 7514 |
log_info("Successfully formatted future partition %" PRIu64 ".", p->partno); |
| 7515 |
|
| 7516 |
/* If we're writing to a loop device or BLKPG partition, we can now mount the empty filesystem and populate it. */ |
| 7517 |
if (partition_needs_populate(p) && !root) { |
| 7518 |
assert(t->loop || t->block_partition); |
| 7519 |
|
| 7520 |
r = partition_populate_filesystem(context, p, partition_target_path(t)); |
| 7521 |
if (r < 0) |
| 7522 |
return r; |
| 7523 |
} |
| 7524 |
|
| 7525 |
if (p->encrypt != ENCRYPT_OFF && !t->loop && !t->block_partition) { |
| 7526 |
r = partition_target_grow(t, p->new_size); |
| 7527 |
if (r < 0) |
| 7528 |
return r; |
| 7529 |
|
| 7530 |
r = partition_encrypt(context, p, t, /* offline= */ true, /* temporary= */ true); |
| 7531 |
if (r < 0) |
| 7532 |
return log_error_errno(r, "Failed to encrypt device: %m"); |
| 7533 |
} |
| 7534 |
|
| 7535 |
/* Note that we always sync explicitly here, since mkfs.fat doesn't do that on its own, and |
| 7536 |
* if we don't sync before detaching a block device the in-flight sectors possibly won't hit |
| 7537 |
* the disk. */ |
| 7538 |
|
| 7539 |
r = partition_target_sync(context, p, t); |
| 7540 |
if (r < 0) |
| 7541 |
return r; |
| 7542 |
|
| 7543 |
if (p->siblings[VERITY_HASH] && !partition_defer(context, p->siblings[VERITY_HASH])) { |
| 7544 |
r = partition_format_verity_hash(context, p->siblings[VERITY_HASH], |
| 7545 |
/* node= */ NULL, partition_target_path(t)); |
| 7546 |
if (r < 0) |
| 7547 |
return r; |
| 7548 |
} |
| 7549 |
|
| 7550 |
if (p->siblings[VERITY_SIG] && !partition_defer(context, p->siblings[VERITY_SIG])) { |
| 7551 |
r = partition_format_verity_sig(context, p->siblings[VERITY_SIG]); |
| 7552 |
if (r < 0) |
| 7553 |
return r; |
| 7554 |
} |
| 7555 |
} |
| 7556 |
|
| 7557 |
return 0; |
| 7558 |
} |
| 7559 |
|
| 7560 |
static int partition_acquire_uuid(Context *context, Partition *p, sd_id128_t *ret) { |
| 7561 |
struct { |
| 7562 |
sd_id128_t type_uuid; |
| 7563 |
uint64_t counter; |
| 7564 |
} _packed_ plaintext = {}; |
| 7565 |
union { |
| 7566 |
uint8_t md[SHA256_DIGEST_SIZE]; |
| 7567 |
sd_id128_t id; |
| 7568 |
} result; |
| 7569 |
|
| 7570 |
uint64_t k = 0; |
| 7571 |
int r; |
| 7572 |
|
| 7573 |
assert(context); |
| 7574 |
assert(p); |
| 7575 |
assert(ret); |
| 7576 |
|
| 7577 |
/* Calculate a good UUID for the indicated partition. We want a certain degree of reproducibility, |
| 7578 |
* hence we won't generate the UUIDs randomly. Instead we use a cryptographic hash (precisely: |
| 7579 |
* HMAC-SHA256) to derive them from a single seed. The seed is generally the machine ID of the |
| 7580 |
* installation we are processing, but if random behaviour is desired can be random, too. We use the |
| 7581 |
* seed value as key for the HMAC (since the machine ID is something we generally don't want to leak) |
| 7582 |
* and the partition type as plaintext. The partition type is suffixed with a counter (only for the |
| 7583 |
* second and later partition of the same type) if we have more than one partition of the same |
| 7584 |
* time. Or in other words: |
| 7585 |
* |
| 7586 |
* With: |
| 7587 |
* SEED := /etc/machine-id |
| 7588 |
* |
| 7589 |
* If first partition instance of type TYPE_UUID: |
| 7590 |
* PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID) |
| 7591 |
* |
| 7592 |
* For all later partition instances of type TYPE_UUID with INSTANCE being the LE64 encoded instance number: |
| 7593 |
* PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID || INSTANCE) |
| 7594 |
*/ |
| 7595 |
|
| 7596 |
LIST_FOREACH(partitions, q, context->partitions) { |
| 7597 |
if (p == q) |
| 7598 |
break; |
| 7599 |
|
| 7600 |
if (!sd_id128_equal(p->type.uuid, q->type.uuid)) |
| 7601 |
continue; |
| 7602 |
|
| 7603 |
k++; |
| 7604 |
} |
| 7605 |
|
| 7606 |
plaintext.type_uuid = p->type.uuid; |
| 7607 |
plaintext.counter = htole64(k); |
| 7608 |
|
| 7609 |
hmac_sha256(context->seed.bytes, sizeof(context->seed.bytes), |
| 7610 |
&plaintext, |
| 7611 |
k == 0 ? sizeof(sd_id128_t) : sizeof(plaintext), |
| 7612 |
result.md); |
| 7613 |
|
| 7614 |
/* Take the first half, mark it as v4 UUID */ |
| 7615 |
assert_cc(sizeof(result.md) == sizeof(result.id) * 2); |
| 7616 |
result.id = id128_make_v4_uuid(result.id); |
| 7617 |
|
| 7618 |
/* Ensure this partition UUID is actually unique, and there's no remaining partition from an earlier run? */ |
| 7619 |
LIST_FOREACH(partitions, q, context->partitions) { |
| 7620 |
if (p == q) |
| 7621 |
continue; |
| 7622 |
|
| 7623 |
if (sd_id128_in_set(result.id, q->current_uuid, q->new_uuid)) { |
| 7624 |
log_warning("Partition UUID calculated from seed for partition %" PRIu64 " already used, reverting to randomized UUID.", p->partno); |
| 7625 |
|
| 7626 |
r = sd_id128_randomize(&result.id); |
| 7627 |
if (r < 0) |
| 7628 |
return log_error_errno(r, "Failed to generate randomized UUID: %m"); |
| 7629 |
|
| 7630 |
break; |
| 7631 |
} |
| 7632 |
} |
| 7633 |
|
| 7634 |
*ret = result.id; |
| 7635 |
return 0; |
| 7636 |
} |
| 7637 |
|
| 7638 |
static int partition_acquire_label(Context *context, Partition *p, char **ret) { |
| 7639 |
_cleanup_free_ char *label = NULL; |
| 7640 |
const char *prefix; |
| 7641 |
unsigned k = 1; |
| 7642 |
|
| 7643 |
assert(context); |
| 7644 |
assert(p); |
| 7645 |
assert(ret); |
| 7646 |
|
| 7647 |
prefix = gpt_partition_type_uuid_to_string(p->type.uuid); |
| 7648 |
if (!prefix) |
| 7649 |
prefix = "linux"; |
| 7650 |
|
| 7651 |
for (;;) { |
| 7652 |
const char *ll = label ?: prefix; |
| 7653 |
bool retry = false; |
| 7654 |
|
| 7655 |
LIST_FOREACH(partitions, q, context->partitions) { |
| 7656 |
if (p == q) |
| 7657 |
break; |
| 7658 |
|
| 7659 |
if (streq_ptr(ll, q->current_label) || |
| 7660 |
streq_ptr(ll, q->new_label)) { |
| 7661 |
retry = true; |
| 7662 |
break; |
| 7663 |
} |
| 7664 |
} |
| 7665 |
|
| 7666 |
if (!retry) |
| 7667 |
break; |
| 7668 |
|
| 7669 |
label = mfree(label); |
| 7670 |
if (asprintf(&label, "%s-%u", prefix, ++k) < 0) |
| 7671 |
return log_oom(); |
| 7672 |
} |
| 7673 |
|
| 7674 |
if (!label) { |
| 7675 |
label = strdup(prefix); |
| 7676 |
if (!label) |
| 7677 |
return log_oom(); |
| 7678 |
} |
| 7679 |
|
| 7680 |
*ret = TAKE_PTR(label); |
| 7681 |
return 0; |
| 7682 |
} |
| 7683 |
|
| 7684 |
static int context_acquire_partition_uuids_and_labels(Context *context) { |
| 7685 |
int r; |
| 7686 |
|
| 7687 |
assert(context); |
| 7688 |
|
| 7689 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 7690 |
sd_id128_t uuid; |
| 7691 |
|
| 7692 |
/* Never touch foreign partitions */ |
| 7693 |
if (PARTITION_IS_FOREIGN(p)) { |
| 7694 |
p->new_uuid = p->current_uuid; |
| 7695 |
|
| 7696 |
if (p->current_label) { |
| 7697 |
r = free_and_strdup_warn(&p->new_label, strempty(p->current_label)); |
| 7698 |
if (r < 0) |
| 7699 |
return r; |
| 7700 |
} |
| 7701 |
|
| 7702 |
continue; |
| 7703 |
} |
| 7704 |
|
| 7705 |
(void) context_notify(context, PROGRESS_ACQUIRING_PARTITION_LABELS, p->definition_path, UINT_MAX); |
| 7706 |
|
| 7707 |
if (!sd_id128_is_null(p->current_uuid)) |
| 7708 |
p->new_uuid = uuid = p->current_uuid; /* Never change initialized UUIDs */ |
| 7709 |
else if (p->new_uuid_is_set) |
| 7710 |
uuid = p->new_uuid; |
| 7711 |
else { |
| 7712 |
/* Not explicitly set by user! */ |
| 7713 |
r = partition_acquire_uuid(context, p, &uuid); |
| 7714 |
if (r < 0) |
| 7715 |
return r; |
| 7716 |
|
| 7717 |
/* The final verity hash/data UUIDs can only be determined after formatting the |
| 7718 |
* verity hash partition. However, we still want to use the generated partition UUID |
| 7719 |
* to derive other UUIDs to keep things unique and reproducible, so we always |
| 7720 |
* generate a UUID if none is set, but we only use it as the actual partition UUID if |
| 7721 |
* verity is not configured. */ |
| 7722 |
if (!IN_SET(p->verity, VERITY_DATA, VERITY_HASH)) { |
| 7723 |
p->new_uuid = uuid; |
| 7724 |
p->new_uuid_is_set = true; |
| 7725 |
} |
| 7726 |
} |
| 7727 |
|
| 7728 |
/* Calculate the UUID for the file system as HMAC-SHA256 of the string "file-system-uuid", |
| 7729 |
* keyed off the partition UUID. */ |
| 7730 |
r = derive_uuid(uuid, "file-system-uuid", &p->fs_uuid); |
| 7731 |
if (r < 0) |
| 7732 |
return r; |
| 7733 |
|
| 7734 |
if (p->encrypt != ENCRYPT_OFF) { |
| 7735 |
r = derive_uuid(uuid, "luks-uuid", &p->luks_uuid); |
| 7736 |
if (r < 0) |
| 7737 |
return r; |
| 7738 |
} |
| 7739 |
|
| 7740 |
/* Derive the verity salt and verity superblock UUID from the seed to keep them reproducible */ |
| 7741 |
if (p->verity == VERITY_HASH) { |
| 7742 |
derive_salt(context->seed, "verity-salt", p->verity_salt); |
| 7743 |
|
| 7744 |
r = derive_uuid(context->seed, "verity-uuid", &p->verity_uuid); |
| 7745 |
if (r < 0) |
| 7746 |
return log_error_errno(r, "Failed to acquire verity uuid: %m"); |
| 7747 |
} |
| 7748 |
|
| 7749 |
if (!isempty(p->current_label)) { |
| 7750 |
/* never change initialized labels */ |
| 7751 |
r = free_and_strdup_warn(&p->new_label, p->current_label); |
| 7752 |
if (r < 0) |
| 7753 |
return r; |
| 7754 |
} else if (!p->new_label) { |
| 7755 |
/* Not explicitly set by user! */ |
| 7756 |
|
| 7757 |
r = partition_acquire_label(context, p, &p->new_label); |
| 7758 |
if (r < 0) |
| 7759 |
return r; |
| 7760 |
} |
| 7761 |
} |
| 7762 |
|
| 7763 |
return 0; |
| 7764 |
} |
| 7765 |
|
| 7766 |
static int set_gpt_flags(struct fdisk_partition *q, uint64_t flags) { |
| 7767 |
_cleanup_free_ char *a = NULL; |
| 7768 |
int r; |
| 7769 |
|
| 7770 |
BIT_FOREACH(i, flags) { |
| 7771 |
r = strextendf_with_separator(&a, ",", "%i", i); |
| 7772 |
if (r < 0) |
| 7773 |
return r; |
| 7774 |
} |
| 7775 |
|
| 7776 |
return sym_fdisk_partition_set_attrs(q, strempty(a)); |
| 7777 |
} |
| 7778 |
|
| 7779 |
static uint64_t partition_merge_flags(Partition *p) { |
| 7780 |
uint64_t f; |
| 7781 |
|
| 7782 |
assert(p); |
| 7783 |
|
| 7784 |
f = p->gpt_flags; |
| 7785 |
|
| 7786 |
if (p->no_auto >= 0) { |
| 7787 |
if (gpt_partition_type_knows_no_auto(p->type)) |
| 7788 |
SET_FLAG(f, SD_GPT_FLAG_NO_AUTO, p->no_auto); |
| 7789 |
else { |
| 7790 |
char buffer[SD_ID128_UUID_STRING_MAX]; |
| 7791 |
log_warning("Configured NoAuto=%s for partition type '%s' that doesn't support it, ignoring.", |
| 7792 |
yes_no(p->no_auto), |
| 7793 |
gpt_partition_type_uuid_to_string_harder(p->type.uuid, buffer)); |
| 7794 |
} |
| 7795 |
} |
| 7796 |
|
| 7797 |
if (p->read_only >= 0) { |
| 7798 |
if (gpt_partition_type_knows_read_only(p->type)) |
| 7799 |
SET_FLAG(f, SD_GPT_FLAG_READ_ONLY, p->read_only); |
| 7800 |
else { |
| 7801 |
char buffer[SD_ID128_UUID_STRING_MAX]; |
| 7802 |
log_warning("Configured ReadOnly=%s for partition type '%s' that doesn't support it, ignoring.", |
| 7803 |
yes_no(p->read_only), |
| 7804 |
gpt_partition_type_uuid_to_string_harder(p->type.uuid, buffer)); |
| 7805 |
} |
| 7806 |
} |
| 7807 |
|
| 7808 |
if (p->growfs >= 0) { |
| 7809 |
if (gpt_partition_type_knows_growfs(p->type)) |
| 7810 |
SET_FLAG(f, SD_GPT_FLAG_GROWFS, p->growfs); |
| 7811 |
else { |
| 7812 |
char buffer[SD_ID128_UUID_STRING_MAX]; |
| 7813 |
log_warning("Configured GrowFileSystem=%s for partition type '%s' that doesn't support it, ignoring.", |
| 7814 |
yes_no(p->growfs), |
| 7815 |
gpt_partition_type_uuid_to_string_harder(p->type.uuid, buffer)); |
| 7816 |
} |
| 7817 |
} |
| 7818 |
|
| 7819 |
return f; |
| 7820 |
} |
| 7821 |
|
| 7822 |
static int context_mangle_partitions(Context *context) { |
| 7823 |
int r; |
| 7824 |
|
| 7825 |
assert(context); |
| 7826 |
|
| 7827 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 7828 |
if (p->dropped) |
| 7829 |
continue; |
| 7830 |
|
| 7831 |
if (partition_defer(context, p)) |
| 7832 |
continue; |
| 7833 |
|
| 7834 |
(void) context_notify(context, PROGRESS_ADJUSTING_PARTITION, p->definition_path, UINT_MAX); |
| 7835 |
|
| 7836 |
assert(p->new_size != UINT64_MAX); |
| 7837 |
assert(p->offset != UINT64_MAX); |
| 7838 |
assert(p->partno != UINT64_MAX); |
| 7839 |
|
| 7840 |
if (PARTITION_EXISTS(p)) { |
| 7841 |
bool changed = false; |
| 7842 |
|
| 7843 |
assert(p->current_partition); |
| 7844 |
|
| 7845 |
if (p->new_size != p->current_size) { |
| 7846 |
assert(p->new_size >= p->current_size); |
| 7847 |
assert(p->new_size % context->sector_size == 0); |
| 7848 |
|
| 7849 |
r = sym_fdisk_partition_size_explicit(p->current_partition, true); |
| 7850 |
if (r < 0) |
| 7851 |
return log_error_errno(r, "Failed to enable explicit sizing: %m"); |
| 7852 |
|
| 7853 |
r = sym_fdisk_partition_set_size(p->current_partition, p->new_size / context->sector_size); |
| 7854 |
if (r < 0) |
| 7855 |
return log_error_errno(r, "Failed to grow partition: %m"); |
| 7856 |
|
| 7857 |
log_info("Growing existing partition %" PRIu64 ".", p->partno); |
| 7858 |
changed = true; |
| 7859 |
} |
| 7860 |
|
| 7861 |
if (!sd_id128_equal(p->new_uuid, p->current_uuid)) { |
| 7862 |
r = sym_fdisk_partition_set_uuid(p->current_partition, SD_ID128_TO_UUID_STRING(p->new_uuid)); |
| 7863 |
if (r < 0) |
| 7864 |
return log_error_errno(r, "Failed to set partition UUID: %m"); |
| 7865 |
|
| 7866 |
log_info("Initializing UUID of existing partition %" PRIu64 ".", p->partno); |
| 7867 |
changed = true; |
| 7868 |
} |
| 7869 |
|
| 7870 |
if (!streq_ptr(p->new_label, p->current_label)) { |
| 7871 |
r = sym_fdisk_partition_set_name(p->current_partition, strempty(p->new_label)); |
| 7872 |
if (r < 0) |
| 7873 |
return log_error_errno(r, "Failed to set partition label: %m"); |
| 7874 |
|
| 7875 |
log_info("Setting partition label of existing partition %" PRIu64 ".", p->partno); |
| 7876 |
changed = true; |
| 7877 |
} |
| 7878 |
|
| 7879 |
if (changed) { |
| 7880 |
assert(!PARTITION_IS_FOREIGN(p)); /* never touch foreign partitions */ |
| 7881 |
|
| 7882 |
r = sym_fdisk_set_partition(context->fdisk_context, p->partno, p->current_partition); |
| 7883 |
if (r < 0) |
| 7884 |
return log_error_errno(r, "Failed to update partition: %m"); |
| 7885 |
} |
| 7886 |
} else { |
| 7887 |
_cleanup_(fdisk_unref_partitionp) struct fdisk_partition *q = NULL; |
| 7888 |
_cleanup_(fdisk_unref_parttypep) struct fdisk_parttype *t = NULL; |
| 7889 |
|
| 7890 |
assert(!p->new_partition); |
| 7891 |
assert(p->offset % context->sector_size == 0); |
| 7892 |
assert(p->new_size % context->sector_size == 0); |
| 7893 |
assert(p->new_label); |
| 7894 |
|
| 7895 |
t = sym_fdisk_new_parttype(); |
| 7896 |
if (!t) |
| 7897 |
return log_oom(); |
| 7898 |
|
| 7899 |
r = sym_fdisk_parttype_set_typestr(t, SD_ID128_TO_UUID_STRING(p->type.uuid)); |
| 7900 |
if (r < 0) |
| 7901 |
return log_error_errno(r, "Failed to initialize partition type: %m"); |
| 7902 |
|
| 7903 |
q = sym_fdisk_new_partition(); |
| 7904 |
if (!q) |
| 7905 |
return log_oom(); |
| 7906 |
|
| 7907 |
r = sym_fdisk_partition_set_type(q, t); |
| 7908 |
if (r < 0) |
| 7909 |
return log_error_errno(r, "Failed to set partition type: %m"); |
| 7910 |
|
| 7911 |
r = sym_fdisk_partition_size_explicit(q, true); |
| 7912 |
if (r < 0) |
| 7913 |
return log_error_errno(r, "Failed to enable explicit sizing: %m"); |
| 7914 |
|
| 7915 |
r = sym_fdisk_partition_set_start(q, p->offset / context->sector_size); |
| 7916 |
if (r < 0) |
| 7917 |
return log_error_errno(r, "Failed to position partition: %m"); |
| 7918 |
|
| 7919 |
r = sym_fdisk_partition_set_size(q, p->new_size / context->sector_size); |
| 7920 |
if (r < 0) |
| 7921 |
return log_error_errno(r, "Failed to grow partition: %m"); |
| 7922 |
|
| 7923 |
r = sym_fdisk_partition_set_partno(q, p->partno); |
| 7924 |
if (r < 0) |
| 7925 |
return log_error_errno(r, "Failed to set partition number: %m"); |
| 7926 |
|
| 7927 |
r = sym_fdisk_partition_set_uuid(q, SD_ID128_TO_UUID_STRING(p->new_uuid)); |
| 7928 |
if (r < 0) |
| 7929 |
return log_error_errno(r, "Failed to set partition UUID: %m"); |
| 7930 |
|
| 7931 |
r = sym_fdisk_partition_set_name(q, strempty(p->new_label)); |
| 7932 |
if (r < 0) |
| 7933 |
return log_error_errno(r, "Failed to set partition label: %m"); |
| 7934 |
|
| 7935 |
/* Merge the no auto + read only + growfs setting with the literal flags, and set them for the partition */ |
| 7936 |
r = set_gpt_flags(q, partition_merge_flags(p)); |
| 7937 |
if (r < 0) |
| 7938 |
return log_error_errno(r, "Failed to set GPT partition flags: %m"); |
| 7939 |
|
| 7940 |
log_info("Adding new partition %" PRIu64 " to partition table.", p->partno); |
| 7941 |
|
| 7942 |
r = sym_fdisk_add_partition(context->fdisk_context, q, NULL); |
| 7943 |
if (r < 0) |
| 7944 |
return log_error_errno(r, "Failed to add partition: %m"); |
| 7945 |
|
| 7946 |
assert(!p->new_partition); |
| 7947 |
p->new_partition = TAKE_PTR(q); |
| 7948 |
} |
| 7949 |
} |
| 7950 |
|
| 7951 |
return 0; |
| 7952 |
} |
| 7953 |
|
| 7954 |
static int split_name_printf(Partition *p, char **ret) { |
| 7955 |
assert(p); |
| 7956 |
|
| 7957 |
const Specifier table[] = { |
| 7958 |
{ 't', specifier_string, GPT_PARTITION_TYPE_UUID_TO_STRING_HARDER(p->type.uuid) }, |
| 7959 |
{ 'T', specifier_id128, &p->type.uuid }, |
| 7960 |
{ 'U', specifier_id128, &p->new_uuid }, |
| 7961 |
{ 'n', specifier_uint64, &p->partno }, |
| 7962 |
|
| 7963 |
COMMON_SYSTEM_SPECIFIERS, |
| 7964 |
{} |
| 7965 |
}; |
| 7966 |
|
| 7967 |
return specifier_printf(p->split_name_format, NAME_MAX, table, arg_root, p, ret); |
| 7968 |
} |
| 7969 |
|
| 7970 |
static int split_node(const char *node, char **ret_base, char **ret_ext) { |
| 7971 |
_cleanup_free_ char *base = NULL, *ext = NULL; |
| 7972 |
char *e; |
| 7973 |
int r; |
| 7974 |
|
| 7975 |
assert(node); |
| 7976 |
assert(ret_base); |
| 7977 |
assert(ret_ext); |
| 7978 |
|
| 7979 |
r = path_extract_filename(node, &base); |
| 7980 |
if (r == O_DIRECTORY || r == -EADDRNOTAVAIL) |
| 7981 |
return log_error_errno(r, "Device node %s cannot be a directory", node); |
| 7982 |
if (r < 0) |
| 7983 |
return log_error_errno(r, "Failed to extract filename from %s: %m", node); |
| 7984 |
|
| 7985 |
e = endswith(base, ".raw"); |
| 7986 |
if (e) { |
| 7987 |
ext = strdup(e); |
| 7988 |
if (!ext) |
| 7989 |
return log_oom(); |
| 7990 |
|
| 7991 |
*e = 0; |
| 7992 |
} |
| 7993 |
|
| 7994 |
*ret_base = TAKE_PTR(base); |
| 7995 |
*ret_ext = TAKE_PTR(ext); |
| 7996 |
|
| 7997 |
return 0; |
| 7998 |
} |
| 7999 |
|
| 8000 |
static int split_name_resolve(Context *context) { |
| 8001 |
_cleanup_free_ char *parent = NULL, *base = NULL, *ext = NULL; |
| 8002 |
int r; |
| 8003 |
|
| 8004 |
assert(context); |
| 8005 |
|
| 8006 |
r = path_extract_directory(context->node, &parent); |
| 8007 |
if (r < 0 && r != -EDESTADDRREQ) |
| 8008 |
return log_error_errno(r, "Failed to extract directory from %s: %m", context->node); |
| 8009 |
|
| 8010 |
r = split_node(context->node, &base, &ext); |
| 8011 |
if (r < 0) |
| 8012 |
return r; |
| 8013 |
|
| 8014 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 8015 |
_cleanup_free_ char *resolved = NULL; |
| 8016 |
|
| 8017 |
if (p->dropped) |
| 8018 |
continue; |
| 8019 |
|
| 8020 |
if (!p->split_name_format) |
| 8021 |
continue; |
| 8022 |
|
| 8023 |
r = split_name_printf(p, &resolved); |
| 8024 |
if (r < 0) |
| 8025 |
return log_error_errno(r, "Failed to resolve specifiers in %s: %m", p->split_name_format); |
| 8026 |
|
| 8027 |
if (parent) |
| 8028 |
p->split_path = strjoin(parent, "/", base, ".", resolved, ext); |
| 8029 |
else |
| 8030 |
p->split_path = strjoin(base, ".", resolved, ext); |
| 8031 |
if (!p->split_path) |
| 8032 |
return log_oom(); |
| 8033 |
} |
| 8034 |
|
| 8035 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 8036 |
if (!p->split_path) |
| 8037 |
continue; |
| 8038 |
|
| 8039 |
LIST_FOREACH(partitions, q, context->partitions) { |
| 8040 |
if (p == q) |
| 8041 |
continue; |
| 8042 |
|
| 8043 |
if (!q->split_path) |
| 8044 |
continue; |
| 8045 |
|
| 8046 |
if (!streq(p->split_path, q->split_path)) |
| 8047 |
continue; |
| 8048 |
|
| 8049 |
return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ), |
| 8050 |
"%s and %s have the same resolved split name \"%s\", refusing.", |
| 8051 |
p->definition_path, q->definition_path, p->split_path); |
| 8052 |
} |
| 8053 |
} |
| 8054 |
|
| 8055 |
return 0; |
| 8056 |
} |
| 8057 |
|
| 8058 |
static int context_split(Context *context) { |
| 8059 |
unsigned attrs = 0; |
| 8060 |
int fd = -EBADF, r; |
| 8061 |
|
| 8062 |
if (!arg_split) |
| 8063 |
return 0; |
| 8064 |
|
| 8065 |
assert(context); |
| 8066 |
|
| 8067 |
/* We can't do resolution earlier because the partition UUIDs for verity partitions are only filled |
| 8068 |
* in after they've been generated. */ |
| 8069 |
|
| 8070 |
r = split_name_resolve(context); |
| 8071 |
if (r < 0) |
| 8072 |
return r; |
| 8073 |
|
| 8074 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 8075 |
_cleanup_close_ int fdt = -EBADF; |
| 8076 |
|
| 8077 |
if (p->dropped) |
| 8078 |
continue; |
| 8079 |
|
| 8080 |
if (!p->split_path) |
| 8081 |
continue; |
| 8082 |
|
| 8083 |
if (partition_defer(context, p)) |
| 8084 |
continue; |
| 8085 |
|
| 8086 |
if (fd < 0) { |
| 8087 |
assert_se((fd = sym_fdisk_get_devfd(context->fdisk_context)) >= 0); |
| 8088 |
|
| 8089 |
r = read_attr_fd(fd, &attrs); |
| 8090 |
if (r < 0 && !ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 8091 |
log_warning_errno(r, "Failed to read file attributes of %s, ignoring: %m", context->node); |
| 8092 |
} |
| 8093 |
|
| 8094 |
fdt = xopenat_full( |
| 8095 |
AT_FDCWD, |
| 8096 |
p->split_path, |
| 8097 |
O_WRONLY|O_NOCTTY|O_CLOEXEC|O_NOFOLLOW|O_CREAT|O_EXCL, |
| 8098 |
attrs & FS_NOCOW_FL ? XO_NOCOW : 0, |
| 8099 |
0666); |
| 8100 |
if (fdt < 0) |
| 8101 |
return log_error_errno(fdt, "Failed to open split partition file %s: %m", p->split_path); |
| 8102 |
|
| 8103 |
if (lseek(fd, p->offset, SEEK_SET) < 0) |
| 8104 |
return log_error_errno(errno, "Failed to seek to partition offset: %m"); |
| 8105 |
|
| 8106 |
/* Verity signature partitions contain a JSON object NUL-padded out to the partition |
| 8107 |
* size. The on-disk partition must keep the padding, but the split-out file is a |
| 8108 |
* standalone artifact, so trim the trailing NUL bytes there to avoid tripping jq. */ |
| 8109 |
if (partition_designator_is_verity_sig(p->type.designator)) { |
| 8110 |
_cleanup_free_ char *buf = malloc(p->new_size); |
| 8111 |
if (!buf) |
| 8112 |
return log_oom(); |
| 8113 |
|
| 8114 |
r = loop_read_exact(fd, buf, p->new_size, /* do_poll= */ false); |
| 8115 |
if (r < 0) |
| 8116 |
return log_error_errno(r, "Failed to read verity signature partition: %m"); |
| 8117 |
|
| 8118 |
size_t len = strnlen(buf, p->new_size); |
| 8119 |
if (len == 0) |
| 8120 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Verity signature partition is empty"); |
| 8121 |
|
| 8122 |
r = loop_write(fdt, buf, len); |
| 8123 |
if (r < 0) |
| 8124 |
return log_error_errno(r, "Failed to write to split partition %s: %m", p->split_path); |
| 8125 |
} else { |
| 8126 |
r = copy_bytes(fd, fdt, p->new_size, COPY_REFLINK|COPY_HOLES|COPY_TRUNCATE); |
| 8127 |
if (r < 0) |
| 8128 |
return log_error_errno(r, "Failed to copy to split partition %s: %m", p->split_path); |
| 8129 |
} |
| 8130 |
} |
| 8131 |
|
| 8132 |
return 0; |
| 8133 |
} |
| 8134 |
|
| 8135 |
static int write_primary_descriptor( |
| 8136 |
int fd, |
| 8137 |
uint32_t root_sector, |
| 8138 |
usec_t usec, |
| 8139 |
bool utc, |
| 8140 |
const char *system_id, |
| 8141 |
const char *volume_id, |
| 8142 |
const char *publisher_id) { |
| 8143 |
int r; |
| 8144 |
|
| 8145 |
assert(fd >= 0); |
| 8146 |
|
| 8147 |
struct iso9660_primary_volume_descriptor desc = { |
| 8148 |
.header = { |
| 8149 |
.type = 1, |
| 8150 |
.version = 1, |
| 8151 |
}, |
| 8152 |
.volume_space_size_little = htole32(ISO9660_START + ISO9660_SIZE), |
| 8153 |
.volume_space_size_big = htobe32(ISO9660_START + ISO9660_SIZE), |
| 8154 |
.volume_set_size_little = htole16(1), |
| 8155 |
.volume_set_size_big = htobe16(1), |
| 8156 |
.volume_sequence_number_little = htole16(1), |
| 8157 |
.volume_sequence_number_big = htobe16(1), |
| 8158 |
.logical_block_size_little = htole16(ISO9660_BLOCK_SIZE), |
| 8159 |
.logical_block_size_big = htobe16(ISO9660_BLOCK_SIZE), |
| 8160 |
.file_structure_version = 1, |
| 8161 |
.root_directory_entry = { |
| 8162 |
.len = sizeof(struct iso9660_directory_entry), |
| 8163 |
.extent_loc_little = htole32(root_sector), |
| 8164 |
.extent_loc_big = htobe32(root_sector), |
| 8165 |
.data_len_little = htole32(2*sizeof(struct iso9660_directory_entry)), /* 2 entries with ident size 1: . and .. */ |
| 8166 |
.data_len_big = htobe32(2*sizeof(struct iso9660_directory_entry)), /* 2 entries with ident size 1: . and .. */ |
| 8167 |
.flags = 2, /* directory */ |
| 8168 |
.volume_seq_num_little = htole16(1), |
| 8169 |
.volume_seq_num_big = htobe16(1), |
| 8170 |
.ident_len = 1, |
| 8171 |
.ident[0] = 0, /* special value for root */ |
| 8172 |
} |
| 8173 |
}; |
| 8174 |
|
| 8175 |
iso9660_set_const_string(desc.header.identifier, sizeof(desc.header.identifier), "CD001", /* allow_a_chars= */ true); |
| 8176 |
|
| 8177 |
r = iso9660_dir_datetime_from_usec(usec, utc, &desc.root_directory_entry.time); |
| 8178 |
if (r < 0) |
| 8179 |
return r; |
| 8180 |
|
| 8181 |
r = iso9660_set_string(desc.system_identifier, sizeof(desc.system_identifier), system_id, /* allow_a_chars= */ true); |
| 8182 |
if (r < 0) |
| 8183 |
return r; |
| 8184 |
|
| 8185 |
/* In theory the volume identifier should be d-chars, but in practice, a-chars are allowed */ |
| 8186 |
r = iso9660_set_string(desc.volume_identifier, sizeof(desc.volume_identifier), volume_id, /* allow_a_chars= */ true); |
| 8187 |
if (r < 0) |
| 8188 |
return r; |
| 8189 |
|
| 8190 |
iso9660_set_const_string(desc.volume_set_identifier, sizeof(desc.volume_set_identifier), NULL, /* allow_a_chars= */ false); |
| 8191 |
|
| 8192 |
r = iso9660_set_string(desc.publisher_identifier, sizeof(desc.publisher_identifier), publisher_id, /* allow_a_chars= */ true); |
| 8193 |
if (r < 0) |
| 8194 |
return r; |
| 8195 |
|
| 8196 |
iso9660_set_const_string(desc.data_preparer_identifier, sizeof(desc.data_preparer_identifier), NULL, /* allow_a_chars= */ true); |
| 8197 |
iso9660_set_const_string(desc.application_identifier, sizeof(desc.application_identifier), "SYSTEMD-REPART", /* allow_a_chars= */ true); |
| 8198 |
iso9660_set_const_string(desc.copyright_file_identifier, sizeof(desc.copyright_file_identifier), NULL, /* allow_a_chars= */ false); |
| 8199 |
iso9660_set_const_string(desc.abstract_file_identifier, sizeof(desc.abstract_file_identifier), NULL, /* allow_a_chars= */ false); |
| 8200 |
iso9660_set_const_string(desc.bibliographic_file_identifier, sizeof(desc.bibliographic_file_identifier), NULL, /* allow_a_chars= */ false); |
| 8201 |
|
| 8202 |
r = iso9660_datetime_from_usec(usec, utc, &desc.volume_creation_date); |
| 8203 |
if (r < 0) |
| 8204 |
return r; |
| 8205 |
|
| 8206 |
r = iso9660_datetime_from_usec(usec, utc, &desc.volume_modification_date); |
| 8207 |
if (r < 0) |
| 8208 |
return r; |
| 8209 |
|
| 8210 |
iso9660_datetime_zero(&desc.volume_expiration_date); |
| 8211 |
iso9660_datetime_zero(&desc.volume_effective_date); |
| 8212 |
|
| 8213 |
ssize_t s = pwrite(fd, &desc, sizeof(desc), ISO9660_PRIMARY_DESCRIPTOR*ISO9660_BLOCK_SIZE); |
| 8214 |
if (s < 0) |
| 8215 |
return log_error_errno(errno, "Failed to write ISO9660 primary descriptor: %m"); |
| 8216 |
if (s != sizeof(desc)) |
| 8217 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to fully write ISO9660 primary descriptor"); |
| 8218 |
|
| 8219 |
return 0; |
| 8220 |
} |
| 8221 |
|
| 8222 |
static int write_eltorito_descriptor(int fd, uint32_t catalog_sector) { |
| 8223 |
assert(fd >= 0); |
| 8224 |
|
| 8225 |
struct iso9660_eltorito_descriptor desc = { |
| 8226 |
.header = { |
| 8227 |
.type = 0, |
| 8228 |
.version = 1, |
| 8229 |
}, |
| 8230 |
.boot_catalog_sector = htole32(catalog_sector), |
| 8231 |
}; |
| 8232 |
|
| 8233 |
iso9660_set_const_string(desc.header.identifier, sizeof(desc.header.identifier), "CD001", /* allow_a_chars= */ true); |
| 8234 |
|
| 8235 |
strncpy(desc.boot_system_identifier, "EL TORITO SPECIFICATION", sizeof(desc.boot_system_identifier)); |
| 8236 |
|
| 8237 |
ssize_t s = pwrite(fd, &desc, sizeof(desc), ISO9660_ELTORITO_DESCRIPTOR*ISO9660_BLOCK_SIZE); |
| 8238 |
if (s < 0) |
| 8239 |
return log_error_errno(errno, "Failed to write ISO9660 El-Torito descriptor: %m"); |
| 8240 |
if (s != sizeof(desc)) |
| 8241 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to fully write ISO9660 El-Torito descriptor"); |
| 8242 |
|
| 8243 |
return 0; |
| 8244 |
} |
| 8245 |
|
| 8246 |
static int write_terminal_descriptor(int fd) { |
| 8247 |
assert(fd >= 0); |
| 8248 |
|
| 8249 |
struct iso9660_terminal_descriptor desc = { |
| 8250 |
.header = { |
| 8251 |
.type = 255, |
| 8252 |
.version = 1, |
| 8253 |
}, |
| 8254 |
}; |
| 8255 |
|
| 8256 |
iso9660_set_const_string(desc.header.identifier, sizeof(desc.header.identifier), "CD001", /* allow_a_chars= */ true); |
| 8257 |
|
| 8258 |
ssize_t s = pwrite(fd, &desc, sizeof(desc), ISO9660_TERMINAL_DESCRIPTOR*ISO9660_BLOCK_SIZE); |
| 8259 |
if (s < 0) |
| 8260 |
return log_error_errno(errno, "Failed to write ISO9660 terminal descriptor: %m"); |
| 8261 |
if (s != sizeof(desc)) |
| 8262 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to fully write ISO9660 terminal descriptor"); |
| 8263 |
|
| 8264 |
return 0; |
| 8265 |
} |
| 8266 |
|
| 8267 |
static uint16_t calculate_validation_entry_checksum(const void *p, size_t size) { |
| 8268 |
assert(p || size == 0); |
| 8269 |
assert(size % 2 == 0); |
| 8270 |
|
| 8271 |
uint16_t checksum = 0; |
| 8272 |
|
| 8273 |
for (size_t i = 0; i < (size/2); i++) |
| 8274 |
checksum -= le16toh(((const le16_t*)p)[i]); |
| 8275 |
|
| 8276 |
return checksum; |
| 8277 |
} |
| 8278 |
|
| 8279 |
static int write_boot_catalog(int fd, uint32_t load_block) { |
| 8280 |
assert(fd >= 0); |
| 8281 |
|
| 8282 |
struct el_torito_validation_entry ve = { |
| 8283 |
.header_indicator = 1, |
| 8284 |
.platform = 0xef, /* EFI */ |
| 8285 |
.key_bytes = {0x55, 0xaa}, |
| 8286 |
}; |
| 8287 |
|
| 8288 |
ve.checksum = htole16(calculate_validation_entry_checksum(&ve, sizeof(ve))); |
| 8289 |
|
| 8290 |
struct el_torito_initial_entry ie = { |
| 8291 |
.boot_indicator = 0x88, /* bootable */ |
| 8292 |
.boot_media_type = 0, /* no emul */ |
| 8293 |
/* From UEFI specification: |
| 8294 |
* > If the value of Sector Count is set to 0 or 1, EFI will assume the system partition |
| 8295 |
* > consumes the space from the beginning of the “no emulation” image to the end of the |
| 8296 |
* > CD-ROM. |
| 8297 |
*/ |
| 8298 |
.sector_count = htole16(0), |
| 8299 |
.load_rba = htole32(load_block), |
| 8300 |
|
| 8301 |
}; |
| 8302 |
|
| 8303 |
struct el_torito_section_header sh = { |
| 8304 |
.header_indicator = 0x91, /* final header */ |
| 8305 |
.nentries = htole16(0), /* no more entries */ |
| 8306 |
}; |
| 8307 |
|
| 8308 |
uint8_t sector[ISO9660_BLOCK_SIZE] = {}; |
| 8309 |
uint8_t *p = sector; |
| 8310 |
p = mempcpy(p, &ve, sizeof(ve)); |
| 8311 |
p = mempcpy(p, &ie, sizeof(ie)); |
| 8312 |
p = mempcpy(p, &sh, sizeof(sh)); |
| 8313 |
assert((size_t) (p - sector) <= sizeof(sector)); |
| 8314 |
|
| 8315 |
ssize_t s = pwrite(fd, §or, sizeof(sector), ISO9660_BOOT_CATALOG*ISO9660_BLOCK_SIZE); |
| 8316 |
if (s < 0) |
| 8317 |
return log_error_errno(errno, "Failed to write El-Torito boot catalog: %m"); |
| 8318 |
if (s != sizeof(sector)) |
| 8319 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to fully write El-Torito boot catalog"); |
| 8320 |
|
| 8321 |
return 0; |
| 8322 |
} |
| 8323 |
|
| 8324 |
static int write_directories( |
| 8325 |
int fd, |
| 8326 |
usec_t usec, |
| 8327 |
bool utc, |
| 8328 |
uint32_t root_sector) { |
| 8329 |
|
| 8330 |
int r; |
| 8331 |
|
| 8332 |
assert(fd >= 0); |
| 8333 |
|
| 8334 |
uint32_t dir_size = 2*sizeof(struct iso9660_directory_entry); /* 2 entries with ident size 1: . and .. */ |
| 8335 |
|
| 8336 |
struct iso9660_directory_entry self = { |
| 8337 |
.len = sizeof(struct iso9660_directory_entry), |
| 8338 |
.extent_loc_little = htole32(root_sector), |
| 8339 |
.extent_loc_big = htobe32(root_sector), |
| 8340 |
.data_len_little = htole32(dir_size), |
| 8341 |
.data_len_big = htobe32(dir_size), |
| 8342 |
.flags = 2, /* directory */ |
| 8343 |
.volume_seq_num_little = htole16(1), |
| 8344 |
.volume_seq_num_big = htobe16(1), |
| 8345 |
.ident_len = 1, |
| 8346 |
.ident[0] = 0, /* special value for self */ |
| 8347 |
}; |
| 8348 |
|
| 8349 |
r = iso9660_dir_datetime_from_usec(usec, utc, &self.time); |
| 8350 |
if (r < 0) |
| 8351 |
return r; |
| 8352 |
|
| 8353 |
struct iso9660_directory_entry parent = { |
| 8354 |
.len = sizeof(struct iso9660_directory_entry), |
| 8355 |
.extent_loc_little = htole32(root_sector), |
| 8356 |
.extent_loc_big = htobe32(root_sector), |
| 8357 |
.data_len_little = htole32(dir_size), |
| 8358 |
.data_len_big = htobe32(dir_size), |
| 8359 |
.flags = 2, /* directory */ |
| 8360 |
.volume_seq_num_little = htole16(1), |
| 8361 |
.volume_seq_num_big = htobe16(1), |
| 8362 |
.ident_len = 1, |
| 8363 |
.ident[0] = 1, /* special value for parent */ |
| 8364 |
}; |
| 8365 |
|
| 8366 |
// TODO: we should probably add some text file explaining there is no content through ISO9660 |
| 8367 |
|
| 8368 |
r = iso9660_dir_datetime_from_usec(usec, utc, &parent.time); |
| 8369 |
if (r < 0) |
| 8370 |
return r; |
| 8371 |
|
| 8372 |
uint8_t sector[ISO9660_BLOCK_SIZE] = {}; |
| 8373 |
uint8_t *p = sector; |
| 8374 |
p = mempcpy(p, &self, sizeof(self)); |
| 8375 |
p = mempcpy(p, &parent, sizeof(parent)); |
| 8376 |
assert((size_t) (p - sector) <= sizeof(sector)); |
| 8377 |
|
| 8378 |
ssize_t s = pwrite(fd, §or, sizeof(sector), ISO9660_ROOT_DIRECTORY*ISO9660_BLOCK_SIZE); |
| 8379 |
if (s < 0) |
| 8380 |
return log_error_errno(errno, "Failed to write ISO9660 root directory: %m"); |
| 8381 |
if (s != sizeof(sector)) |
| 8382 |
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to fully write ISO9660 root directory"); |
| 8383 |
|
| 8384 |
return 0; |
| 8385 |
} |
| 8386 |
|
| 8387 |
static int write_eltorito( |
| 8388 |
int fd, |
| 8389 |
usec_t usec, |
| 8390 |
bool utc, |
| 8391 |
uint32_t load_block, /* in iso9660 blocks */ |
| 8392 |
const char *system_id, |
| 8393 |
const char *volume_id, |
| 8394 |
const char *publisher_id) { |
| 8395 |
|
| 8396 |
int r; |
| 8397 |
|
| 8398 |
assert(fd >= 0); |
| 8399 |
|
| 8400 |
r = write_primary_descriptor(fd, ISO9660_ROOT_DIRECTORY, usec, utc, system_id, volume_id, publisher_id); |
| 8401 |
if (r < 0) |
| 8402 |
return r; |
| 8403 |
|
| 8404 |
r = write_eltorito_descriptor(fd, ISO9660_BOOT_CATALOG); |
| 8405 |
if (r < 0) |
| 8406 |
return r; |
| 8407 |
|
| 8408 |
r = write_terminal_descriptor(fd); |
| 8409 |
if (r < 0) |
| 8410 |
return r; |
| 8411 |
|
| 8412 |
r = write_boot_catalog(fd, load_block); |
| 8413 |
if (r < 0) |
| 8414 |
return r; |
| 8415 |
|
| 8416 |
r = write_directories(fd, usec, utc, ISO9660_ROOT_DIRECTORY); |
| 8417 |
if (r < 0) |
| 8418 |
return r; |
| 8419 |
|
| 8420 |
return 0; |
| 8421 |
} |
| 8422 |
|
| 8423 |
static int context_verify_eltorito_overlap(Context *context) { |
| 8424 |
/* Check if the partition table collides with the ISO9660 El Torito area. */ |
| 8425 |
assert(context); |
| 8426 |
|
| 8427 |
if (!arg_eltorito) |
| 8428 |
return 0; |
| 8429 |
|
| 8430 |
/* Check how many GPT partition entries can be stored. */ |
| 8431 |
size_t nents = sym_fdisk_get_npartitions(context->fdisk_context); |
| 8432 |
/* The GPT contains |
| 8433 |
* - 1 unused block (protective MBR) |
| 8434 |
* - GPT header |
| 8435 |
* - N entries of 128 bytes each. |
| 8436 |
*/ |
| 8437 |
size_t first_free_offset = 2*context->sector_size + round_up_size(nents*128, context->sector_size); |
| 8438 |
|
| 8439 |
if (first_free_offset > ISO9660_START*ISO9660_BLOCK_SIZE) |
| 8440 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "The partition table is overlapping with the El Torito boot catalog."); |
| 8441 |
|
| 8442 |
/* The first lba is the first block where a partition could exist. Even if there is no partition |
| 8443 |
* there, we should still not overlap with it since a partition could be added later. |
| 8444 |
* It is unexpected for tools to change the first lba in the GPT header. So this should be safe. |
| 8445 |
*/ |
| 8446 |
if (sym_fdisk_get_first_lba(context->fdisk_context) * context->sector_size < (ISO9660_START+ISO9660_SIZE)*ISO9660_BLOCK_SIZE) |
| 8447 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "El Torito is overlapping with the first partition block."); |
| 8448 |
|
| 8449 |
return 0; |
| 8450 |
} |
| 8451 |
|
| 8452 |
static int context_find_esp_offset(Context *context, uint64_t *ret) { |
| 8453 |
assert(ret); |
| 8454 |
|
| 8455 |
uint64_t esp_offset = UINT64_MAX; |
| 8456 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 8457 |
if (p->dropped || PARTITION_IS_FOREIGN(p)) |
| 8458 |
continue; |
| 8459 |
if (p->type.designator == PARTITION_ESP) { |
| 8460 |
esp_offset = p->offset; |
| 8461 |
break; |
| 8462 |
} |
| 8463 |
} |
| 8464 |
|
| 8465 |
if (esp_offset == UINT64_MAX) |
| 8466 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "El Torito boot catalog requires an ESP."); |
| 8467 |
if (esp_offset / ISO9660_BLOCK_SIZE > UINT32_MAX) |
| 8468 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "ESP offset is farther than El Torito boot catalog can support."); |
| 8469 |
if (esp_offset % ISO9660_BLOCK_SIZE != 0) |
| 8470 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "ESP offset not aligned on 2K blocks."); |
| 8471 |
|
| 8472 |
*ret = esp_offset; |
| 8473 |
return 0; |
| 8474 |
} |
| 8475 |
|
| 8476 |
static int context_partscan(Context *context) { |
| 8477 |
int capable, r; |
| 8478 |
|
| 8479 |
assert(context); |
| 8480 |
|
| 8481 |
context->needs_rescan = false; |
| 8482 |
|
| 8483 |
capable = blockdev_partscan_enabled_fd(sym_fdisk_get_devfd(context->fdisk_context)); |
| 8484 |
if (capable == -ENOTBLK) |
| 8485 |
log_debug("Not telling kernel to reread partition table, since we are not operating on a block device."); |
| 8486 |
else if (capable < 0) |
| 8487 |
return log_error_errno(capable, "Failed to check if block device supports partition scanning: %m"); |
| 8488 |
else if (capable > 0) { |
| 8489 |
log_info("Informing kernel about changed partitions..."); |
| 8490 |
(void) context_notify(context, PROGRESS_REREADING_TABLE, /* object= */ NULL, UINT_MAX); |
| 8491 |
|
| 8492 |
r = reread_partition_table_fd(sym_fdisk_get_devfd(context->fdisk_context), /* flags= */ 0); |
| 8493 |
if (r < 0) |
| 8494 |
return log_error_errno(r, "Failed to reread partition table: %m"); |
| 8495 |
} else |
| 8496 |
log_notice("Not telling kernel to reread partition table, because selected image does not support kernel partition block devices."); |
| 8497 |
|
| 8498 |
return 0; |
| 8499 |
} |
| 8500 |
|
| 8501 |
static int context_write_partition_table(Context *context) { |
| 8502 |
_cleanup_(fdisk_unref_tablep) struct fdisk_table *original_table = NULL; |
| 8503 |
int r; |
| 8504 |
|
| 8505 |
assert(context); |
| 8506 |
|
| 8507 |
if (!context->from_scratch && !context_changed(context)) { |
| 8508 |
log_info("No changes."); |
| 8509 |
return 0; |
| 8510 |
} |
| 8511 |
|
| 8512 |
if (context->dry_run) { |
| 8513 |
log_notice("Refusing to repartition, please re-run with --dry-run=no."); |
| 8514 |
return 0; |
| 8515 |
} |
| 8516 |
|
| 8517 |
log_info("Applying changes to %s.", context->node); |
| 8518 |
|
| 8519 |
if (context->from_scratch && context->empty != EMPTY_CREATE) { |
| 8520 |
|
| 8521 |
(void) context_notify(context, PROGRESS_WIPING_DISK, /* object= */ NULL, UINT_MAX); |
| 8522 |
|
| 8523 |
/* Erase everything if we operate from scratch, except if the image was just created anyway, and thus is definitely empty. */ |
| 8524 |
r = context_wipe_range(context, 0, context->total); |
| 8525 |
if (r < 0) |
| 8526 |
return r; |
| 8527 |
|
| 8528 |
log_info("Wiped block device."); |
| 8529 |
|
| 8530 |
if (arg_discard) { |
| 8531 |
r = context_discard_range(context, 0, context->total); |
| 8532 |
if (r == -EOPNOTSUPP) |
| 8533 |
log_info("Storage does not support discard, not discarding entire block device data."); |
| 8534 |
else if (r < 0) |
| 8535 |
return log_error_errno(r, "Failed to discard entire block device: %m"); |
| 8536 |
else if (r > 0) |
| 8537 |
log_info("Discarded entire block device."); |
| 8538 |
} |
| 8539 |
} |
| 8540 |
|
| 8541 |
r = sym_fdisk_get_partitions(context->fdisk_context, &original_table); |
| 8542 |
if (r < 0) |
| 8543 |
return log_error_errno(r, "Failed to acquire partition table: %m"); |
| 8544 |
|
| 8545 |
/* Wipe fs signatures and discard sectors where the new partitions are going to be placed and in the |
| 8546 |
* gaps between partitions, just to be sure. */ |
| 8547 |
r = context_wipe_and_discard(context); |
| 8548 |
if (r < 0) |
| 8549 |
goto error; |
| 8550 |
|
| 8551 |
r = context_copy_blocks(context); |
| 8552 |
if (r < 0) |
| 8553 |
goto error; |
| 8554 |
|
| 8555 |
r = context_mkfs(context); |
| 8556 |
if (r < 0) |
| 8557 |
goto error; |
| 8558 |
|
| 8559 |
/* We are now moving destructively btrfs filesystems into the disk before we have written the |
| 8560 |
* partitions. This is OK because the main use case is that the btrfs filesystems moved are initially |
| 8561 |
* volatile (in ram disk for example) with little data to save. But we do not want to finish the gpt |
| 8562 |
* table in case we lose power and reboot and try to boot that incomplete disk. |
| 8563 |
*/ |
| 8564 |
r = context_block_device_replace(context); |
| 8565 |
if (r < 0) |
| 8566 |
goto error; |
| 8567 |
|
| 8568 |
r = context_mangle_partitions(context); |
| 8569 |
if (r < 0) |
| 8570 |
goto error; |
| 8571 |
|
| 8572 |
log_info("Writing new partition table."); |
| 8573 |
|
| 8574 |
(void) context_notify(context, PROGRESS_WRITING_TABLE, /* object= */ NULL, UINT_MAX); |
| 8575 |
|
| 8576 |
r = sym_fdisk_write_disklabel(context->fdisk_context); |
| 8577 |
if (r < 0) { |
| 8578 |
r = log_error_errno(r, "Failed to write partition table: %m"); |
| 8579 |
goto error; |
| 8580 |
} |
| 8581 |
|
| 8582 |
context_btrfs_replace_resize(context); |
| 8583 |
|
| 8584 |
r = context_partscan(context); |
| 8585 |
if (r < 0) |
| 8586 |
return r; |
| 8587 |
|
| 8588 |
log_info("Partition table written."); |
| 8589 |
|
| 8590 |
return 0; |
| 8591 |
|
| 8592 |
error: |
| 8593 |
context_btrfs_replace_back(context); |
| 8594 |
|
| 8595 |
if (context->needs_rescan) |
| 8596 |
(void) context_partscan(context); |
| 8597 |
|
| 8598 |
return r; |
| 8599 |
} |
| 8600 |
|
| 8601 |
static int context_write_eltorito(Context *context) { |
| 8602 |
int r; |
| 8603 |
|
| 8604 |
assert(context); |
| 8605 |
|
| 8606 |
if (!arg_eltorito) |
| 8607 |
return 0; |
| 8608 |
|
| 8609 |
if (context->dry_run) |
| 8610 |
return 0; |
| 8611 |
|
| 8612 |
bool utc = true; |
| 8613 |
usec_t usec = parse_source_date_epoch(); |
| 8614 |
if (usec == USEC_INFINITY) { |
| 8615 |
usec = now(CLOCK_REALTIME); |
| 8616 |
utc = false; |
| 8617 |
} |
| 8618 |
|
| 8619 |
uint64_t esp_offset; |
| 8620 |
r = context_find_esp_offset(context, &esp_offset); |
| 8621 |
if (r < 0) |
| 8622 |
return r; |
| 8623 |
|
| 8624 |
log_info("Writing El Torito boot catalog."); |
| 8625 |
|
| 8626 |
r = write_eltorito( |
| 8627 |
sym_fdisk_get_devfd(context->fdisk_context), |
| 8628 |
usec, |
| 8629 |
utc, |
| 8630 |
esp_offset / ISO9660_BLOCK_SIZE, |
| 8631 |
arg_eltorito_system, |
| 8632 |
arg_eltorito_volume, |
| 8633 |
arg_eltorito_publisher); |
| 8634 |
if (r < 0) |
| 8635 |
return log_error_errno(r, "Failed to write El Torito boot catalog: %m"); |
| 8636 |
|
| 8637 |
return 0; |
| 8638 |
} |
| 8639 |
|
| 8640 |
static int context_read_seed(Context *context, const char *root) { |
| 8641 |
int r; |
| 8642 |
|
| 8643 |
assert(context); |
| 8644 |
|
| 8645 |
if (!sd_id128_is_null(context->seed)) |
| 8646 |
return 0; |
| 8647 |
|
| 8648 |
if (!arg_randomize) { |
| 8649 |
r = id128_get_machine(root, &context->seed); |
| 8650 |
if (r >= 0) |
| 8651 |
return 0; |
| 8652 |
|
| 8653 |
if (!ERRNO_IS_MACHINE_ID_UNSET(r)) |
| 8654 |
return log_error_errno(r, "Failed to parse machine ID of image: %m"); |
| 8655 |
|
| 8656 |
log_info("No machine ID set, using randomized partition UUIDs."); |
| 8657 |
} |
| 8658 |
|
| 8659 |
r = sd_id128_randomize(&context->seed); |
| 8660 |
if (r < 0) |
| 8661 |
return log_error_errno(r, "Failed to generate randomized seed: %m"); |
| 8662 |
|
| 8663 |
return 0; |
| 8664 |
} |
| 8665 |
|
| 8666 |
static int context_factory_reset(Context *context) { |
| 8667 |
size_t n = 0; |
| 8668 |
int r; |
| 8669 |
|
| 8670 |
assert(context); |
| 8671 |
|
| 8672 |
if (arg_factory_reset <= 0) |
| 8673 |
return 0; |
| 8674 |
|
| 8675 |
if (context->from_scratch) /* Nothing to reset if we start from scratch */ |
| 8676 |
return 0; |
| 8677 |
|
| 8678 |
if (context->dry_run) { |
| 8679 |
log_notice("Refusing to factory reset, please re-run with --dry-run=no."); |
| 8680 |
return 0; |
| 8681 |
} |
| 8682 |
|
| 8683 |
log_info("Applying factory reset."); |
| 8684 |
|
| 8685 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 8686 |
|
| 8687 |
if (!p->factory_reset || !PARTITION_EXISTS(p)) |
| 8688 |
continue; |
| 8689 |
|
| 8690 |
assert(p->partno != UINT64_MAX); |
| 8691 |
|
| 8692 |
log_info("Removing partition %" PRIu64 " for factory reset.", p->partno); |
| 8693 |
|
| 8694 |
r = sym_fdisk_delete_partition(context->fdisk_context, p->partno); |
| 8695 |
if (r < 0) |
| 8696 |
return log_error_errno(r, "Failed to remove partition %" PRIu64 ": %m", p->partno); |
| 8697 |
|
| 8698 |
n++; |
| 8699 |
} |
| 8700 |
|
| 8701 |
if (n == 0) { |
| 8702 |
log_info("Factory reset requested, but no partitions to delete found."); |
| 8703 |
return 0; |
| 8704 |
} |
| 8705 |
|
| 8706 |
r = sym_fdisk_write_disklabel(context->fdisk_context); |
| 8707 |
if (r < 0) |
| 8708 |
return log_error_errno(r, "Failed to write disk label: %m"); |
| 8709 |
|
| 8710 |
/* We do not want to stop if partscan has errors */ |
| 8711 |
(void) context_partscan(context); |
| 8712 |
|
| 8713 |
log_info("Successfully deleted %zu partitions.", n); |
| 8714 |
return 1; |
| 8715 |
} |
| 8716 |
|
| 8717 |
static int context_can_factory_reset(Context *context) { |
| 8718 |
assert(context); |
| 8719 |
|
| 8720 |
LIST_FOREACH(partitions, p, context->partitions) |
| 8721 |
if (p->factory_reset && PARTITION_EXISTS(p)) |
| 8722 |
return true; |
| 8723 |
|
| 8724 |
return false; |
| 8725 |
} |
| 8726 |
|
| 8727 |
static int resolve_copy_blocks_auto_candidate( |
| 8728 |
dev_t partition_devno, |
| 8729 |
GptPartitionType partition_type, |
| 8730 |
dev_t restrict_devno, |
| 8731 |
sd_id128_t *ret_uuid) { |
| 8732 |
|
| 8733 |
#if HAVE_BLKID |
| 8734 |
_cleanup_(blkid_free_probep) blkid_probe b = NULL; |
| 8735 |
_cleanup_close_ int fd = -EBADF; |
| 8736 |
_cleanup_free_ char *p = NULL; |
| 8737 |
const char *pttype; |
| 8738 |
sd_id128_t pt_parsed, u; |
| 8739 |
blkid_partition pp; |
| 8740 |
dev_t whole_devno; |
| 8741 |
blkid_partlist pl; |
| 8742 |
int r; |
| 8743 |
|
| 8744 |
/* Checks if the specified partition has the specified GPT type UUID, and is located on the specified |
| 8745 |
* 'restrict_devno' device. The type check is particularly relevant if we have Verity volume which is |
| 8746 |
* backed by two separate partitions: the data and the hash partitions, and we need to find the right |
| 8747 |
* one of the two. */ |
| 8748 |
|
| 8749 |
r = block_get_whole_disk(partition_devno, &whole_devno); |
| 8750 |
if (r < 0) |
| 8751 |
return log_error_errno( |
| 8752 |
r, |
| 8753 |
"Unable to determine containing block device of partition %u:%u: %m", |
| 8754 |
major(partition_devno), minor(partition_devno)); |
| 8755 |
|
| 8756 |
if (restrict_devno != (dev_t) -1 && |
| 8757 |
restrict_devno != whole_devno) |
| 8758 |
return log_error_errno( |
| 8759 |
SYNTHETIC_ERRNO(EPERM), |
| 8760 |
"Partition %u:%u is located outside of block device %u:%u, refusing.", |
| 8761 |
major(partition_devno), minor(partition_devno), |
| 8762 |
major(restrict_devno), minor(restrict_devno)); |
| 8763 |
|
| 8764 |
fd = r = device_open_from_devnum(S_IFBLK, whole_devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &p); |
| 8765 |
if (r < 0) |
| 8766 |
return log_error_errno(r, "Failed to open block device " DEVNUM_FORMAT_STR ": %m", |
| 8767 |
DEVNUM_FORMAT_VAL(whole_devno)); |
| 8768 |
|
| 8769 |
r = DLOPEN_LIBBLKID(LOG_ERR, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 8770 |
if (r < 0) |
| 8771 |
return r; |
| 8772 |
|
| 8773 |
b = sym_blkid_new_probe(); |
| 8774 |
if (!b) |
| 8775 |
return log_oom(); |
| 8776 |
|
| 8777 |
errno = 0; |
| 8778 |
r = sym_blkid_probe_set_device(b, fd, 0, 0); |
| 8779 |
if (r != 0) |
| 8780 |
return log_error_errno(errno_or_else(ENOMEM), "Failed to open block device '%s': %m", p); |
| 8781 |
|
| 8782 |
(void) sym_blkid_probe_enable_partitions(b, 1); |
| 8783 |
(void) sym_blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS); |
| 8784 |
|
| 8785 |
errno = 0; |
| 8786 |
r = sym_blkid_do_safeprobe(b); |
| 8787 |
if (r == _BLKID_SAFEPROBE_ERROR) |
| 8788 |
return log_error_errno(errno_or_else(EIO), "Unable to probe for partition table of '%s': %m", p); |
| 8789 |
if (IN_SET(r, _BLKID_SAFEPROBE_AMBIGUOUS, _BLKID_SAFEPROBE_NOT_FOUND)) { |
| 8790 |
log_debug("Didn't find partition table on block device '%s'.", p); |
| 8791 |
return false; |
| 8792 |
} |
| 8793 |
|
| 8794 |
assert(r == _BLKID_SAFEPROBE_FOUND); |
| 8795 |
|
| 8796 |
(void) sym_blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL); |
| 8797 |
if (!streq_ptr(pttype, "gpt")) { |
| 8798 |
log_debug("Didn't find a GPT partition table on '%s'.", p); |
| 8799 |
return false; |
| 8800 |
} |
| 8801 |
|
| 8802 |
errno = 0; |
| 8803 |
pl = sym_blkid_probe_get_partitions(b); |
| 8804 |
if (!pl) |
| 8805 |
return log_error_errno(errno_or_else(EIO), "Unable read partition table of '%s': %m", p); |
| 8806 |
|
| 8807 |
pp = sym_blkid_partlist_devno_to_partition(pl, partition_devno); |
| 8808 |
if (!pp) { |
| 8809 |
log_debug("Partition %u:%u has no matching partition table entry on '%s'.", |
| 8810 |
major(partition_devno), minor(partition_devno), p); |
| 8811 |
return false; |
| 8812 |
} |
| 8813 |
|
| 8814 |
r = blkid_partition_get_type_id128(pp, &pt_parsed); |
| 8815 |
if (r < 0) { |
| 8816 |
log_debug_errno(r, "Failed to read partition type UUID of partition %u:%u: %m", |
| 8817 |
major(partition_devno), minor(partition_devno)); |
| 8818 |
return false; |
| 8819 |
} |
| 8820 |
|
| 8821 |
if (!sd_id128_equal(pt_parsed, partition_type.uuid)) { |
| 8822 |
log_debug("Partition %u:%u has non-matching partition type " SD_ID128_FORMAT_STR " (needed: " SD_ID128_FORMAT_STR "), ignoring.", |
| 8823 |
major(partition_devno), minor(partition_devno), |
| 8824 |
SD_ID128_FORMAT_VAL(pt_parsed), SD_ID128_FORMAT_VAL(partition_type.uuid)); |
| 8825 |
return false; |
| 8826 |
} |
| 8827 |
|
| 8828 |
r = blkid_partition_get_uuid_id128(pp, &u); |
| 8829 |
if (r == -ENXIO) { |
| 8830 |
log_debug_errno(r, "Partition " DEVNUM_FORMAT_STR " has no UUID.", DEVNUM_FORMAT_VAL(partition_devno)); |
| 8831 |
return false; |
| 8832 |
} |
| 8833 |
if (r < 0) { |
| 8834 |
log_debug_errno(r, "Failed to read partition UUID of " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(partition_devno)); |
| 8835 |
return false; |
| 8836 |
} |
| 8837 |
|
| 8838 |
log_debug("Automatically found partition " DEVNUM_FORMAT_STR " of right type " SD_ID128_FORMAT_STR ".", |
| 8839 |
DEVNUM_FORMAT_VAL(partition_devno), |
| 8840 |
SD_ID128_FORMAT_VAL(pt_parsed)); |
| 8841 |
|
| 8842 |
if (ret_uuid) |
| 8843 |
*ret_uuid = u; |
| 8844 |
|
| 8845 |
return true; |
| 8846 |
#else |
| 8847 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 8848 |
"Cannot check partition type UUID and device location, libblkid support is not compiled in."); |
| 8849 |
#endif |
| 8850 |
} |
| 8851 |
|
| 8852 |
static int resolve_copy_blocks_auto_candidate_harder( |
| 8853 |
dev_t start_devno, |
| 8854 |
GptPartitionType partition_type, |
| 8855 |
dev_t restrict_devno, |
| 8856 |
dev_t *ret_found_devno, |
| 8857 |
sd_id128_t *ret_uuid) { |
| 8858 |
|
| 8859 |
_cleanup_(sd_device_unrefp) sd_device *d = NULL, *nd = NULL; |
| 8860 |
int r; |
| 8861 |
|
| 8862 |
/* A wrapper around resolve_copy_blocks_auto_candidate(), but looks for verity/verity-sig associated |
| 8863 |
* partitions, too. i.e. if the input is a data or verity partition, will try to find the |
| 8864 |
* verity/verity-sig partition for it, based on udev metadata. */ |
| 8865 |
|
| 8866 |
const char *property; |
| 8867 |
if (partition_designator_is_verity_hash(partition_type.designator)) |
| 8868 |
property = "ID_DISSECT_PART_VERITY_DEVICE"; |
| 8869 |
else if (partition_designator_is_verity_sig(partition_type.designator)) |
| 8870 |
property = "ID_DISSECT_PART_VERITY_SIG_DEVICE"; |
| 8871 |
else |
| 8872 |
goto not_found; |
| 8873 |
|
| 8874 |
r = sd_device_new_from_devnum(&d, 'b', start_devno); |
| 8875 |
if (r < 0) |
| 8876 |
return log_error_errno(r, "Failed to allocate device object for " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(start_devno)); |
| 8877 |
|
| 8878 |
const char *node; |
| 8879 |
r = sd_device_get_property_value(d, property, &node); |
| 8880 |
if (r == -ENOENT) { |
| 8881 |
log_debug_errno(r, "Property %s not set on " DEVNUM_FORMAT_STR ", skipping.", property, DEVNUM_FORMAT_VAL(start_devno)); |
| 8882 |
goto not_found; |
| 8883 |
} |
| 8884 |
if (r < 0) |
| 8885 |
return log_error_errno(r, "Failed to read property %s from device " DEVNUM_FORMAT_STR ": %m", property, DEVNUM_FORMAT_VAL(start_devno)); |
| 8886 |
|
| 8887 |
r = sd_device_new_from_devname(&nd, node); |
| 8888 |
if (ERRNO_IS_NEG_DEVICE_ABSENT(r)) { |
| 8889 |
log_debug_errno(r, "Device %s referenced in %s property not found, skipping: %m", node, property); |
| 8890 |
goto not_found; |
| 8891 |
} |
| 8892 |
if (r < 0) |
| 8893 |
return log_error_errno(r, "Failed to allocate device object for '%s': %m", node); |
| 8894 |
|
| 8895 |
r = device_in_subsystem(nd, "block"); |
| 8896 |
if (r < 0) |
| 8897 |
return log_error_errno(r, "Failed to determine if '%s' is a block device: %m", node); |
| 8898 |
if (r == 0) { |
| 8899 |
log_debug("Device referenced by %s property of %s does not refer to block device, refusing.", property, node); |
| 8900 |
goto not_found; |
| 8901 |
} |
| 8902 |
|
| 8903 |
dev_t found_devno = 0; |
| 8904 |
r = sd_device_get_devnum(nd, &found_devno); |
| 8905 |
if (r < 0) |
| 8906 |
return log_error_errno(r, "Failed to get device number for '%s': %m", node); |
| 8907 |
|
| 8908 |
r = resolve_copy_blocks_auto_candidate(found_devno, partition_type, restrict_devno, ret_uuid); |
| 8909 |
if (r < 0) |
| 8910 |
return r; |
| 8911 |
if (r == 0) |
| 8912 |
goto not_found; |
| 8913 |
|
| 8914 |
if (ret_found_devno) |
| 8915 |
*ret_found_devno = found_devno; |
| 8916 |
|
| 8917 |
return 1; |
| 8918 |
|
| 8919 |
not_found: |
| 8920 |
if (ret_found_devno) |
| 8921 |
*ret_found_devno = 0; |
| 8922 |
if (ret_uuid) |
| 8923 |
*ret_uuid = SD_ID128_NULL; |
| 8924 |
|
| 8925 |
return 0; |
| 8926 |
} |
| 8927 |
|
| 8928 |
static int find_backing_devno( |
| 8929 |
const char *path, |
| 8930 |
const char *root, |
| 8931 |
dev_t *ret) { |
| 8932 |
|
| 8933 |
_cleanup_free_ char *resolved = NULL; |
| 8934 |
int r; |
| 8935 |
|
| 8936 |
assert(path); |
| 8937 |
|
| 8938 |
r = chase(path, root, CHASE_PREFIX_ROOT, &resolved, NULL); |
| 8939 |
if (r < 0) |
| 8940 |
return r; |
| 8941 |
|
| 8942 |
r = path_is_mount_point(resolved); |
| 8943 |
if (r < 0) |
| 8944 |
return r; |
| 8945 |
if (r == 0) /* Not a mount point, then it's not a partition of its own, let's not automatically use it. */ |
| 8946 |
return -ENOENT; |
| 8947 |
|
| 8948 |
r = get_block_device(resolved, ret); |
| 8949 |
if (r < 0) |
| 8950 |
return r; |
| 8951 |
if (r == 0) /* Not backed by physical file system, we can't use this */ |
| 8952 |
return -ENOENT; |
| 8953 |
|
| 8954 |
return 0; |
| 8955 |
} |
| 8956 |
|
| 8957 |
static int resolve_copy_blocks_auto( |
| 8958 |
GptPartitionType type, |
| 8959 |
const char *root, |
| 8960 |
dev_t restrict_devno, |
| 8961 |
dev_t *ret_devno, |
| 8962 |
sd_id128_t *ret_uuid) { |
| 8963 |
|
| 8964 |
const char *try1 = NULL, *try2 = NULL; |
| 8965 |
char p[SYS_BLOCK_PATH_MAX("/slaves")]; |
| 8966 |
_cleanup_closedir_ DIR *d = NULL; |
| 8967 |
sd_id128_t found_uuid = SD_ID128_NULL; |
| 8968 |
dev_t devno, found = 0; |
| 8969 |
int r; |
| 8970 |
|
| 8971 |
/* Enforce some security restrictions: CopyBlocks=auto should not be an avenue to get outside of the |
| 8972 |
* --root=/--image= confinement. Specifically, refuse CopyBlocks= in combination with --root= at all, |
| 8973 |
* and restrict block device references in the --image= case to loopback block device we set up. |
| 8974 |
* |
| 8975 |
* restrict_devno contain the dev_t of the loop back device we operate on in case of --image=, and |
| 8976 |
* thus declares which device (and its partition subdevices) we shall limit access to. If |
| 8977 |
* restrict_devno is zero no device probing access shall be allowed at all (used for --root=) and if |
| 8978 |
* it is (dev_t) -1 then free access shall be allowed (if neither switch is used). */ |
| 8979 |
|
| 8980 |
if (restrict_devno == 0) |
| 8981 |
return log_error_errno(SYNTHETIC_ERRNO(EPERM), |
| 8982 |
"Automatic discovery of backing block devices not permitted in --root= mode, refusing."); |
| 8983 |
|
| 8984 |
/* Handles CopyBlocks=auto, and finds the right source partition to copy from. We look for matching |
| 8985 |
* partitions in the host, using the appropriate directory as key and ensuring that the partition |
| 8986 |
* type matches. */ |
| 8987 |
|
| 8988 |
switch (type.designator) { |
| 8989 |
|
| 8990 |
case PARTITION_ROOT: |
| 8991 |
case PARTITION_ROOT_VERITY: |
| 8992 |
case PARTITION_ROOT_VERITY_SIG: |
| 8993 |
try1 = "/"; |
| 8994 |
break; |
| 8995 |
|
| 8996 |
case PARTITION_USR: |
| 8997 |
case PARTITION_USR_VERITY: |
| 8998 |
case PARTITION_USR_VERITY_SIG: |
| 8999 |
try1 = "/usr/"; |
| 9000 |
break; |
| 9001 |
|
| 9002 |
case PARTITION_ESP: |
| 9003 |
try1 = "/efi/"; |
| 9004 |
try2 = "/boot/"; |
| 9005 |
break; |
| 9006 |
|
| 9007 |
case PARTITION_XBOOTLDR: |
| 9008 |
try1 = "/boot/"; |
| 9009 |
break; |
| 9010 |
|
| 9011 |
default: |
| 9012 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 9013 |
"Partition type %s not supported from automatic source block device discovery.", |
| 9014 |
strna(partition_designator_to_string(type.designator))); |
| 9015 |
} |
| 9016 |
|
| 9017 |
r = find_backing_devno(try1, root, &devno); |
| 9018 |
if (r == -ENOENT && try2) |
| 9019 |
r = find_backing_devno(try2, root, &devno); |
| 9020 |
if (r < 0) |
| 9021 |
return log_error_errno(r, "Failed to resolve automatic CopyBlocks= path for partition type %s, sorry: %m", |
| 9022 |
partition_designator_to_string(type.designator)); |
| 9023 |
|
| 9024 |
xsprintf_sys_block_path(p, "/slaves", devno); |
| 9025 |
d = opendir(p); |
| 9026 |
if (d) { |
| 9027 |
struct dirent *de; |
| 9028 |
|
| 9029 |
for (;;) { |
| 9030 |
_cleanup_free_ char *q = NULL, *t = NULL; |
| 9031 |
sd_id128_t u; |
| 9032 |
dev_t sl; |
| 9033 |
|
| 9034 |
errno = 0; |
| 9035 |
de = readdir_no_dot(d); |
| 9036 |
if (!de) { |
| 9037 |
if (errno != 0) |
| 9038 |
return log_error_errno(errno, "Failed to read directory '%s': %m", p); |
| 9039 |
|
| 9040 |
break; |
| 9041 |
} |
| 9042 |
|
| 9043 |
if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN)) |
| 9044 |
continue; |
| 9045 |
|
| 9046 |
q = path_join(p, de->d_name, "/dev"); |
| 9047 |
if (!q) |
| 9048 |
return log_oom(); |
| 9049 |
|
| 9050 |
r = read_one_line_file(q, &t); |
| 9051 |
if (r < 0) |
| 9052 |
return log_error_errno(r, "Failed to read %s: %m", q); |
| 9053 |
|
| 9054 |
r = parse_devnum(t, &sl); |
| 9055 |
if (r < 0) { |
| 9056 |
log_debug_errno(r, "Failed to parse %s, ignoring: %m", q); |
| 9057 |
continue; |
| 9058 |
} |
| 9059 |
if (major(sl) == 0) { |
| 9060 |
log_debug("Device backing %s is special, ignoring.", q); |
| 9061 |
continue; |
| 9062 |
} |
| 9063 |
|
| 9064 |
r = resolve_copy_blocks_auto_candidate(sl, type, restrict_devno, &u); |
| 9065 |
if (r < 0) |
| 9066 |
return r; |
| 9067 |
if (r > 0) { |
| 9068 |
/* We found a matching one! */ |
| 9069 |
if (found != 0 && found != sl) |
| 9070 |
return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ), |
| 9071 |
"Multiple matching partitions found for partition type %s, refusing.", |
| 9072 |
partition_designator_to_string(type.designator)); |
| 9073 |
|
| 9074 |
found = sl; |
| 9075 |
found_uuid = u; |
| 9076 |
} |
| 9077 |
|
| 9078 |
dev_t harder_devno = 0; |
| 9079 |
r = resolve_copy_blocks_auto_candidate_harder(sl, type, restrict_devno, &harder_devno, &u); |
| 9080 |
if (r < 0) |
| 9081 |
return r; |
| 9082 |
if (r > 0) { |
| 9083 |
/* We found a matching one! */ |
| 9084 |
if (found != 0 && found != harder_devno) |
| 9085 |
return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ), |
| 9086 |
"Multiple matching partitions found, refusing."); |
| 9087 |
|
| 9088 |
found = harder_devno; |
| 9089 |
found_uuid = u; |
| 9090 |
} |
| 9091 |
} |
| 9092 |
} else if (errno != ENOENT) |
| 9093 |
return log_error_errno(errno, "Failed to open %s: %m", p); |
| 9094 |
else { |
| 9095 |
r = resolve_copy_blocks_auto_candidate(devno, type, restrict_devno, &found_uuid); |
| 9096 |
if (r < 0) |
| 9097 |
return r; |
| 9098 |
if (r > 0) |
| 9099 |
found = devno; |
| 9100 |
} |
| 9101 |
|
| 9102 |
if (found == 0) |
| 9103 |
return log_error_errno(SYNTHETIC_ERRNO(ENXIO), |
| 9104 |
"Unable to automatically discover suitable partition to copy blocks from for partition type %s.", |
| 9105 |
partition_designator_to_string(type.designator)); |
| 9106 |
|
| 9107 |
if (ret_devno) |
| 9108 |
*ret_devno = found; |
| 9109 |
|
| 9110 |
if (ret_uuid) |
| 9111 |
*ret_uuid = found_uuid; |
| 9112 |
|
| 9113 |
return 0; |
| 9114 |
} |
| 9115 |
|
| 9116 |
static int context_open_copy_block_paths( |
| 9117 |
Context *context, |
| 9118 |
dev_t restrict_devno) { |
| 9119 |
|
| 9120 |
int r; |
| 9121 |
|
| 9122 |
assert(context); |
| 9123 |
|
| 9124 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 9125 |
_cleanup_close_ int source_fd = -EBADF; |
| 9126 |
_cleanup_free_ char *opened = NULL; |
| 9127 |
sd_id128_t uuid = SD_ID128_NULL; |
| 9128 |
uint64_t size; |
| 9129 |
struct stat st; |
| 9130 |
|
| 9131 |
if (p->copy_blocks_fd >= 0) |
| 9132 |
continue; |
| 9133 |
|
| 9134 |
assert(p->copy_blocks_size == UINT64_MAX); |
| 9135 |
|
| 9136 |
if (PARTITION_EXISTS(p)) /* Never copy over partitions that already exist! */ |
| 9137 |
continue; |
| 9138 |
|
| 9139 |
if (p->copy_blocks_path) { |
| 9140 |
|
| 9141 |
source_fd = chase_and_open(p->copy_blocks_path, p->copy_blocks_root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &opened); |
| 9142 |
if (source_fd < 0) |
| 9143 |
return log_error_errno(source_fd, "Failed to open '%s': %m", p->copy_blocks_path); |
| 9144 |
|
| 9145 |
if (fstat(source_fd, &st) < 0) |
| 9146 |
return log_error_errno(errno, "Failed to stat block copy file '%s': %m", opened); |
| 9147 |
|
| 9148 |
if (!S_ISREG(st.st_mode) && restrict_devno != (dev_t) -1) |
| 9149 |
return log_error_errno(SYNTHETIC_ERRNO(EPERM), |
| 9150 |
"Copying from block device node is not permitted in --image=/--root= mode, refusing."); |
| 9151 |
|
| 9152 |
} else if (p->copy_blocks_auto) { |
| 9153 |
dev_t devno = 0; /* Fake initialization to appease gcc. */ |
| 9154 |
|
| 9155 |
r = resolve_copy_blocks_auto(p->type, p->copy_blocks_root, restrict_devno, &devno, &uuid); |
| 9156 |
if (r < 0) |
| 9157 |
return r; |
| 9158 |
assert(devno != 0); |
| 9159 |
|
| 9160 |
source_fd = r = device_open_from_devnum(S_IFBLK, devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &opened); |
| 9161 |
if (r < 0) |
| 9162 |
return log_error_errno(r, "Failed to open automatically determined source block copy device " DEVNUM_FORMAT_STR ": %m", |
| 9163 |
DEVNUM_FORMAT_VAL(devno)); |
| 9164 |
|
| 9165 |
if (fstat(source_fd, &st) < 0) |
| 9166 |
return log_error_errno(errno, "Failed to stat block copy file '%s': %m", opened); |
| 9167 |
} else |
| 9168 |
continue; |
| 9169 |
|
| 9170 |
(void) context_notify(context, PROGRESS_OPENING_COPY_BLOCK_SOURCES, p->definition_path, UINT_MAX); |
| 9171 |
|
| 9172 |
if (S_ISDIR(st.st_mode)) { |
| 9173 |
_cleanup_free_ char *bdev = NULL; |
| 9174 |
dev_t devt; |
| 9175 |
|
| 9176 |
/* If the file is a directory, automatically find the backing block device */ |
| 9177 |
|
| 9178 |
if (major(st.st_dev) != 0) |
| 9179 |
devt = st.st_dev; |
| 9180 |
else { |
| 9181 |
/* Special support for btrfs */ |
| 9182 |
r = btrfs_get_block_device_fd(source_fd, &devt); |
| 9183 |
if (r == -EUCLEAN) |
| 9184 |
return btrfs_log_dev_root(LOG_ERR, r, opened); |
| 9185 |
if (r < 0) |
| 9186 |
return log_error_errno(r, "Unable to determine backing block device of '%s': %m", opened); |
| 9187 |
} |
| 9188 |
|
| 9189 |
safe_close(source_fd); |
| 9190 |
|
| 9191 |
source_fd = r = device_open_from_devnum(S_IFBLK, devt, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &bdev); |
| 9192 |
if (r < 0) |
| 9193 |
return log_error_errno(r, "Failed to open block device backing '%s': %m", opened); |
| 9194 |
|
| 9195 |
if (fstat(source_fd, &st) < 0) |
| 9196 |
return log_error_errno(errno, "Failed to stat block device '%s': %m", bdev); |
| 9197 |
} |
| 9198 |
|
| 9199 |
if (S_ISREG(st.st_mode)) |
| 9200 |
size = st.st_size; |
| 9201 |
else if (S_ISBLK(st.st_mode)) { |
| 9202 |
r = blockdev_get_device_size(source_fd, &size); |
| 9203 |
if (r < 0) |
| 9204 |
return log_error_errno(r, "Failed to determine size of block device to copy from: %m"); |
| 9205 |
} else if (S_ISCHR(st.st_mode)) |
| 9206 |
size = UINT64_MAX; |
| 9207 |
else |
| 9208 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified path to copy blocks from '%s' is not a regular file, block device or directory, refusing.", opened); |
| 9209 |
|
| 9210 |
if (size != UINT64_MAX) { |
| 9211 |
if (size <= 0) |
| 9212 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File to copy bytes from '%s' has zero size, refusing.", opened); |
| 9213 |
if (size % 512 != 0) |
| 9214 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File to copy bytes from '%s' has size that is not multiple of 512, refusing.", opened); |
| 9215 |
} |
| 9216 |
|
| 9217 |
p->copy_blocks_fd = TAKE_FD(source_fd); |
| 9218 |
p->copy_blocks_size = size; |
| 9219 |
|
| 9220 |
free_and_replace(p->copy_blocks_path, opened); |
| 9221 |
|
| 9222 |
/* When copying from an existing partition copy that partitions UUID if none is configured explicitly */ |
| 9223 |
if (!p->new_uuid_is_set && !sd_id128_is_null(uuid)) { |
| 9224 |
p->new_uuid = uuid; |
| 9225 |
p->new_uuid_is_set = true; |
| 9226 |
} |
| 9227 |
} |
| 9228 |
|
| 9229 |
return 0; |
| 9230 |
} |
| 9231 |
|
| 9232 |
static int context_open_btrfs_filesystems(Context *context) { |
| 9233 |
int r; |
| 9234 |
|
| 9235 |
assert(context); |
| 9236 |
|
| 9237 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 9238 |
_cleanup_(btrfs_replacement_freep) BtrfsReplacement *replacement = NULL; |
| 9239 |
|
| 9240 |
if (p->dropped) |
| 9241 |
continue; |
| 9242 |
|
| 9243 |
if (PARTITION_EXISTS(p)) |
| 9244 |
continue; |
| 9245 |
|
| 9246 |
if (!p->block_device_replace) |
| 9247 |
continue; |
| 9248 |
|
| 9249 |
if (partition_defer(context, p)) |
| 9250 |
continue; |
| 9251 |
|
| 9252 |
(void) context_notify(context, PROGRESS_OPENING_BLOCK_DEVICE_REPLACE_SOURCES, p->definition_path, UINT_MAX); |
| 9253 |
|
| 9254 |
replacement = new(BtrfsReplacement, 1); |
| 9255 |
if (!replacement) |
| 9256 |
return log_oom(); |
| 9257 |
|
| 9258 |
*replacement = (BtrfsReplacement) { |
| 9259 |
.mountpoint_fd = -EBADF, |
| 9260 |
.source_fd = -EBADF, |
| 9261 |
}; |
| 9262 |
|
| 9263 |
replacement->mountpoint_fd = xopenat(AT_FDCWD, p->block_device_replace, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); |
| 9264 |
if (replacement->mountpoint_fd < 0) |
| 9265 |
return log_error_errno(replacement->mountpoint_fd, "Failed to open mountpoint %s for btrfs filesystem: %m", p->block_device_replace); |
| 9266 |
|
| 9267 |
r = fd_is_fs_type(replacement->mountpoint_fd, BTRFS_SUPER_MAGIC); |
| 9268 |
if (r < 0) |
| 9269 |
return log_error_errno(r, "Failed to check filesystem for mountpoint %s: %m", p->block_device_replace); |
| 9270 |
if (r == 0) |
| 9271 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Mountpoint %s is not a btrfs filesystem", p->block_device_replace); |
| 9272 |
|
| 9273 |
r = btrfs_get_block_device_at_full(replacement->mountpoint_fd, "", &replacement->devid, &replacement->source_path, /* ret= */ NULL); |
| 9274 |
if (r < 0) |
| 9275 |
return log_error_errno(r, "Failed to find device id for btrfs filesystem: %m"); |
| 9276 |
if (r == 0) |
| 9277 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Btrfs filesystem has multiple devices."); |
| 9278 |
|
| 9279 |
/* We need to keep the source device open otherwise, it might be collected. */ |
| 9280 |
replacement->source_fd = open(replacement->source_path, O_RDONLY|O_CLOEXEC); |
| 9281 |
if (replacement->source_fd < 0) |
| 9282 |
return log_error_errno(errno, "Failed to open source device %s: %m", replacement->source_path); |
| 9283 |
|
| 9284 |
r = fd_verify_block(replacement->source_fd); |
| 9285 |
if (r < 0) |
| 9286 |
return log_error_errno(r, "Device %s is not a block device: %m", replacement->source_path); |
| 9287 |
|
| 9288 |
r = blockdev_get_device_size(replacement->source_fd, &replacement->source_size); |
| 9289 |
if (r < 0) |
| 9290 |
return log_error_errno(r, "Failed to get device size %s: %m", replacement->source_path); |
| 9291 |
|
| 9292 |
p->btrfs_replaced = TAKE_PTR(replacement); |
| 9293 |
} |
| 9294 |
|
| 9295 |
return 0; |
| 9296 |
} |
| 9297 |
|
| 9298 |
static int fd_apparent_size(int fd, uint64_t *ret) { |
| 9299 |
off_t initial = 0; |
| 9300 |
uint64_t size = 0; |
| 9301 |
|
| 9302 |
assert(fd >= 0); |
| 9303 |
assert(ret); |
| 9304 |
|
| 9305 |
initial = lseek(fd, 0, SEEK_CUR); |
| 9306 |
if (initial < 0) |
| 9307 |
return log_error_errno(errno, "Failed to get file offset: %m"); |
| 9308 |
|
| 9309 |
for (off_t off = 0;;) { |
| 9310 |
off_t r; |
| 9311 |
|
| 9312 |
r = lseek(fd, off, SEEK_DATA); |
| 9313 |
if (r < 0 && errno == ENXIO) |
| 9314 |
/* If errno == ENXIO, that means we've reached the final hole of the file and |
| 9315 |
* that hole isn't followed by more data. */ |
| 9316 |
break; |
| 9317 |
if (r < 0) |
| 9318 |
return log_error_errno(errno, "Failed to seek data in file from offset %"PRIi64": %m", off); |
| 9319 |
|
| 9320 |
off = r; /* Set the offset to the start of the data segment. */ |
| 9321 |
|
| 9322 |
/* After copying a potential hole, find the end of the data segment by looking for |
| 9323 |
* the next hole. If we get ENXIO, we're at EOF. */ |
| 9324 |
r = lseek(fd, off, SEEK_HOLE); |
| 9325 |
if (r < 0) { |
| 9326 |
if (errno == ENXIO) |
| 9327 |
break; |
| 9328 |
return log_error_errno(errno, "Failed to seek hole in file from offset %"PRIi64": %m", off); |
| 9329 |
} |
| 9330 |
|
| 9331 |
size += r - off; |
| 9332 |
off = r; |
| 9333 |
} |
| 9334 |
|
| 9335 |
if (lseek(fd, initial, SEEK_SET) < 0) |
| 9336 |
return log_error_errno(errno, "Failed to reset file offset: %m"); |
| 9337 |
|
| 9338 |
*ret = size; |
| 9339 |
|
| 9340 |
return 0; |
| 9341 |
} |
| 9342 |
|
| 9343 |
static bool need_fstab_one(const Partition *p) { |
| 9344 |
assert(p); |
| 9345 |
|
| 9346 |
if (p->dropped) |
| 9347 |
return false; |
| 9348 |
|
| 9349 |
if (!p->format) |
| 9350 |
return false; |
| 9351 |
|
| 9352 |
if (p->n_mountpoints == 0) |
| 9353 |
return false; |
| 9354 |
|
| 9355 |
return true; |
| 9356 |
} |
| 9357 |
|
| 9358 |
static bool need_fstab(const Context *context) { |
| 9359 |
assert(context); |
| 9360 |
|
| 9361 |
LIST_FOREACH(partitions, p, context->partitions) |
| 9362 |
if (need_fstab_one(p)) |
| 9363 |
return true; |
| 9364 |
|
| 9365 |
return false; |
| 9366 |
} |
| 9367 |
|
| 9368 |
static int make_by_uuid_symlink_path(const Partition *p, char **ret) { |
| 9369 |
_cleanup_free_ char *what = NULL; |
| 9370 |
|
| 9371 |
assert(p); |
| 9372 |
assert(ret); |
| 9373 |
|
| 9374 |
if (streq_ptr(p->format, "vfat")) { |
| 9375 |
if (asprintf(&what, "UUID=%04X-%04X", |
| 9376 |
((uint32_t) p->fs_uuid.bytes[0] << 8) | |
| 9377 |
((uint32_t) p->fs_uuid.bytes[1] << 0), |
| 9378 |
((uint32_t) p->fs_uuid.bytes[2] << 8) | |
| 9379 |
((uint32_t) p->fs_uuid.bytes[3])) < 0) /* Take first 32 bytes of UUID */ |
| 9380 |
return log_oom(); |
| 9381 |
} else { |
| 9382 |
what = strjoin("UUID=", SD_ID128_TO_UUID_STRING(p->fs_uuid)); |
| 9383 |
if (!what) |
| 9384 |
return log_oom(); |
| 9385 |
} |
| 9386 |
|
| 9387 |
*ret = TAKE_PTR(what); |
| 9388 |
return 0; |
| 9389 |
} |
| 9390 |
|
| 9391 |
static int context_fstab(Context *context) { |
| 9392 |
_cleanup_(unlink_and_freep) char *t = NULL; |
| 9393 |
_cleanup_fclose_ FILE *f = NULL; |
| 9394 |
_cleanup_free_ char *path = NULL, *c = NULL; |
| 9395 |
int r; |
| 9396 |
|
| 9397 |
assert(context); |
| 9398 |
|
| 9399 |
if (!arg_generate_fstab) |
| 9400 |
return false; |
| 9401 |
|
| 9402 |
if (!need_fstab(context)) { |
| 9403 |
log_notice("MountPoint= is not specified for any eligible partitions, not generating %s", |
| 9404 |
arg_generate_fstab); |
| 9405 |
return 0; |
| 9406 |
} |
| 9407 |
|
| 9408 |
path = path_join(arg_copy_source, arg_generate_fstab); |
| 9409 |
if (!path) |
| 9410 |
return log_oom(); |
| 9411 |
|
| 9412 |
r = fopen_tmpfile_linkable(path, O_WRONLY|O_CLOEXEC, &t, &f); |
| 9413 |
if (r < 0) |
| 9414 |
return log_error_errno(r, "Failed to open temporary file for %s: %m", path); |
| 9415 |
|
| 9416 |
fputs(AUTOMATIC_FSTAB_HEADER_START "\n", f); |
| 9417 |
|
| 9418 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 9419 |
_cleanup_free_ char *what = NULL; |
| 9420 |
|
| 9421 |
if (!need_fstab_one(p)) |
| 9422 |
continue; |
| 9423 |
|
| 9424 |
r = make_by_uuid_symlink_path(p, &what); |
| 9425 |
if (r < 0) |
| 9426 |
return r; |
| 9427 |
|
| 9428 |
FOREACH_ARRAY(mountpoint, p->mountpoints, p->n_mountpoints) { |
| 9429 |
_cleanup_free_ char *options = NULL; |
| 9430 |
|
| 9431 |
r = partition_pick_mount_options( |
| 9432 |
p->type.designator, |
| 9433 |
p->format, |
| 9434 |
/* rw= */ true, |
| 9435 |
/* discard= */ !IN_SET(p->type.designator, PARTITION_ESP, PARTITION_XBOOTLDR), |
| 9436 |
&options, |
| 9437 |
NULL); |
| 9438 |
if (r < 0) |
| 9439 |
return r; |
| 9440 |
|
| 9441 |
if (!strextend_with_separator(&options, ",", mountpoint->options)) |
| 9442 |
return log_oom(); |
| 9443 |
|
| 9444 |
fprintf(f, "%s %s %s %s 0 %i\n", |
| 9445 |
what, |
| 9446 |
mountpoint->where, |
| 9447 |
p->format, |
| 9448 |
options, |
| 9449 |
p->type.designator == PARTITION_ROOT ? 1 : 2); |
| 9450 |
} |
| 9451 |
} |
| 9452 |
|
| 9453 |
fputs(AUTOMATIC_FSTAB_HEADER_END "\n", f); |
| 9454 |
|
| 9455 |
switch (arg_append_fstab) { |
| 9456 |
case APPEND_AUTO: { |
| 9457 |
r = read_full_file(path, &c, NULL); |
| 9458 |
if (r == -ENOENT) { |
| 9459 |
log_debug("File fstab not found in %s", path); |
| 9460 |
break; |
| 9461 |
} |
| 9462 |
if (r < 0) |
| 9463 |
return log_error_errno(r, "Failed to open %s: %m", path); |
| 9464 |
|
| 9465 |
const char *acs, *ace; |
| 9466 |
acs = find_line(c, AUTOMATIC_FSTAB_HEADER_START); |
| 9467 |
if (acs) { |
| 9468 |
fwrite(c, 1, acs - c, f); |
| 9469 |
ace = find_line_after(acs, AUTOMATIC_FSTAB_HEADER_END); |
| 9470 |
if (ace) |
| 9471 |
fputs(ace, f); |
| 9472 |
} else |
| 9473 |
fputs(c, f); |
| 9474 |
break; |
| 9475 |
} |
| 9476 |
case APPEND_NO: |
| 9477 |
case APPEND_REPLACE: |
| 9478 |
break; |
| 9479 |
default: |
| 9480 |
assert_not_reached(); |
| 9481 |
} |
| 9482 |
|
| 9483 |
r = fchmod_umask(fileno(f), 0666); |
| 9484 |
if (r < 0) |
| 9485 |
return log_error_errno(r, "Failed to adjust access mode of generated fstab file: %m"); |
| 9486 |
|
| 9487 |
r = flink_tmpfile(f, t, path, IN_SET(arg_append_fstab, APPEND_AUTO, APPEND_REPLACE) ? LINK_TMPFILE_REPLACE : 0); |
| 9488 |
if (r < 0) |
| 9489 |
return log_error_errno(r, "Failed to link temporary file to %s: %m", path); |
| 9490 |
|
| 9491 |
t = mfree(t); |
| 9492 |
|
| 9493 |
log_info("%s written.", path); |
| 9494 |
|
| 9495 |
return 0; |
| 9496 |
} |
| 9497 |
|
| 9498 |
static bool need_crypttab_one(const Partition *p) { |
| 9499 |
assert(p); |
| 9500 |
|
| 9501 |
if (p->dropped) |
| 9502 |
return false; |
| 9503 |
|
| 9504 |
if (p->encrypt == ENCRYPT_OFF) |
| 9505 |
return false; |
| 9506 |
|
| 9507 |
if (!p->encrypted_volume) |
| 9508 |
return false; |
| 9509 |
|
| 9510 |
return true; |
| 9511 |
} |
| 9512 |
|
| 9513 |
static bool need_crypttab(Context *context) { |
| 9514 |
assert(context); |
| 9515 |
|
| 9516 |
LIST_FOREACH(partitions, p, context->partitions) |
| 9517 |
if (need_crypttab_one(p)) |
| 9518 |
return true; |
| 9519 |
|
| 9520 |
return false; |
| 9521 |
} |
| 9522 |
|
| 9523 |
static int context_crypttab(Context *context, bool late) { |
| 9524 |
_cleanup_(unlink_and_freep) char *t = NULL; |
| 9525 |
_cleanup_fclose_ FILE *f = NULL; |
| 9526 |
_cleanup_free_ char *path = NULL; |
| 9527 |
int r; |
| 9528 |
|
| 9529 |
assert(context); |
| 9530 |
|
| 9531 |
if (!arg_generate_crypttab) |
| 9532 |
return false; |
| 9533 |
|
| 9534 |
if (!need_crypttab(context)) { |
| 9535 |
log_notice("EncryptedVolume= is not specified for any eligible partitions, not generating %s", |
| 9536 |
arg_generate_crypttab); |
| 9537 |
return 0; |
| 9538 |
} |
| 9539 |
|
| 9540 |
path = path_join(arg_copy_source, arg_generate_crypttab); |
| 9541 |
if (!path) |
| 9542 |
return log_oom(); |
| 9543 |
|
| 9544 |
r = fopen_tmpfile_linkable(path, O_WRONLY|O_CLOEXEC, &t, &f); |
| 9545 |
if (r < 0) |
| 9546 |
return log_error_errno(r, "Failed to open temporary file for %s: %m", path); |
| 9547 |
|
| 9548 |
fprintf(f, "# Automatically generated by systemd-repart\n\n"); |
| 9549 |
|
| 9550 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 9551 |
_cleanup_free_ char *volume = NULL; |
| 9552 |
|
| 9553 |
if (!need_crypttab_one(p)) |
| 9554 |
continue; |
| 9555 |
|
| 9556 |
if (!p->encrypted_volume->name && asprintf(&volume, "luks-%s", SD_ID128_TO_UUID_STRING(p->luks_uuid)) < 0) |
| 9557 |
return log_oom(); |
| 9558 |
|
| 9559 |
fprintf(f, "%s UUID=%s %s %s\n", |
| 9560 |
p->encrypted_volume->name ?: volume, |
| 9561 |
SD_ID128_TO_UUID_STRING(p->luks_uuid), |
| 9562 |
isempty(p->encrypted_volume->keyfile) ? "-" : p->encrypted_volume->keyfile, |
| 9563 |
strempty(p->encrypted_volume->options)); |
| 9564 |
} |
| 9565 |
|
| 9566 |
r = flink_tmpfile(f, t, path, late ? LINK_TMPFILE_REPLACE : 0); |
| 9567 |
if (r < 0) |
| 9568 |
return log_error_errno(r, "Failed to link temporary file to %s: %m", path); |
| 9569 |
|
| 9570 |
t = mfree(t); |
| 9571 |
|
| 9572 |
log_info("%s written.", path); |
| 9573 |
|
| 9574 |
return 0; |
| 9575 |
} |
| 9576 |
|
| 9577 |
/* update block sizes for verity siblings, calculate hash partition size if requested */ |
| 9578 |
static int context_update_verity_size(Context *context) { |
| 9579 |
int r; |
| 9580 |
|
| 9581 |
assert(context); |
| 9582 |
|
| 9583 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 9584 |
Partition *dp; |
| 9585 |
|
| 9586 |
if (p->verity != VERITY_HASH) |
| 9587 |
continue; |
| 9588 |
|
| 9589 |
if (p->dropped) |
| 9590 |
continue; |
| 9591 |
|
| 9592 |
if (PARTITION_EXISTS(p)) /* Never format existing partitions */ |
| 9593 |
continue; |
| 9594 |
|
| 9595 |
assert_se(dp = p->siblings[VERITY_DATA]); |
| 9596 |
|
| 9597 |
if (p->verity_data_block_size == UINT64_MAX) |
| 9598 |
p->verity_data_block_size = partition_fs_sector_size(context, p); |
| 9599 |
|
| 9600 |
if (p->verity_hash_block_size == UINT64_MAX) |
| 9601 |
p->verity_hash_block_size = partition_fs_sector_size(context, p); |
| 9602 |
|
| 9603 |
uint64_t sz; |
| 9604 |
if (dp->size_max != UINT64_MAX) { |
| 9605 |
r = calculate_verity_hash_size( |
| 9606 |
dp->size_max, |
| 9607 |
p->verity_hash_block_size, |
| 9608 |
p->verity_data_block_size, |
| 9609 |
&sz); |
| 9610 |
if (r < 0) |
| 9611 |
return log_error_errno(r, "Failed to calculate size of dm-verity hash partition: %m"); |
| 9612 |
|
| 9613 |
if (sz > p->size_min || sz > p->size_max) |
| 9614 |
log_warning("The dm-verity hash partition %s may be too small for a data partition " |
| 9615 |
"with SizeMaxBytes=%s. The hash partition would require %s for a data " |
| 9616 |
"partition of specified max size. Consider increasing the size of the " |
| 9617 |
"hash partition, or decreasing SizeMaxBytes= of the data partition.", |
| 9618 |
p->definition_path, FORMAT_BYTES(dp->size_max), FORMAT_BYTES(sz)); |
| 9619 |
else if (p->size_min == UINT64_MAX) { |
| 9620 |
log_debug("Derived size %s of verity hash partition %s from verity data partition %s.", |
| 9621 |
FORMAT_BYTES(sz), p->definition_path, dp->definition_path); |
| 9622 |
|
| 9623 |
p->size_min = sz; |
| 9624 |
} |
| 9625 |
} |
| 9626 |
} |
| 9627 |
|
| 9628 |
return 0; |
| 9629 |
} |
| 9630 |
|
| 9631 |
static int context_minimize(Context *context) { |
| 9632 |
const char *vt = NULL; |
| 9633 |
unsigned attrs = 0; |
| 9634 |
int r; |
| 9635 |
|
| 9636 |
assert(context); |
| 9637 |
|
| 9638 |
if (context->backing_fd >= 0) { |
| 9639 |
r = read_attr_fd(context->backing_fd, &attrs); |
| 9640 |
if (r < 0 && !ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 9641 |
log_warning_errno(r, "Failed to read file attributes of %s, ignoring: %m", context->node); |
| 9642 |
} |
| 9643 |
|
| 9644 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 9645 |
_cleanup_(rm_rf_physical_and_freep) char *root = NULL; |
| 9646 |
_cleanup_(unlink_and_freep) char *temp = NULL; |
| 9647 |
_cleanup_(loop_device_unrefp) LoopDevice *d = NULL; |
| 9648 |
_cleanup_strv_free_ char **extra_mkfs_options = NULL; |
| 9649 |
_cleanup_close_ int fd = -EBADF; |
| 9650 |
_cleanup_free_ char *hint = NULL; |
| 9651 |
sd_id128_t fs_uuid; |
| 9652 |
struct stat st; |
| 9653 |
uint64_t fsz; |
| 9654 |
|
| 9655 |
if (p->dropped) |
| 9656 |
continue; |
| 9657 |
|
| 9658 |
if (PARTITION_EXISTS(p)) /* Never format existing partitions */ |
| 9659 |
continue; |
| 9660 |
|
| 9661 |
if (!p->format) |
| 9662 |
continue; |
| 9663 |
|
| 9664 |
bool is_btrfs = streq(p->format, "btrfs"); |
| 9665 |
|
| 9666 |
if (p->copy_blocks_fd >= 0) |
| 9667 |
continue; |
| 9668 |
|
| 9669 |
if (p->minimize == MINIMIZE_OFF) |
| 9670 |
continue; |
| 9671 |
|
| 9672 |
if (!partition_needs_populate(p)) |
| 9673 |
continue; |
| 9674 |
|
| 9675 |
(void) context_notify(context, PROGRESS_MINIMIZING, p->definition_path, UINT_MAX); |
| 9676 |
|
| 9677 |
assert(!p->copy_blocks_path); |
| 9678 |
|
| 9679 |
(void) partition_hint(p, context->node, &hint); |
| 9680 |
|
| 9681 |
log_info("Pre-populating %s filesystem of partition %s to calculate minimal partition size", |
| 9682 |
p->format, strna(hint)); |
| 9683 |
|
| 9684 |
if (!vt) { |
| 9685 |
r = var_tmp_dir(&vt); |
| 9686 |
if (r < 0) |
| 9687 |
return log_error_errno(r, "Could not determine temporary directory: %m"); |
| 9688 |
} |
| 9689 |
|
| 9690 |
r = tempfn_random_child(vt, "repart", &temp); |
| 9691 |
if (r < 0) |
| 9692 |
return log_error_errno(r, "Failed to generate temporary file path: %m"); |
| 9693 |
|
| 9694 |
fd = xopenat_full( |
| 9695 |
AT_FDCWD, |
| 9696 |
temp, |
| 9697 |
O_CREAT|O_EXCL|O_CLOEXEC|O_RDWR|O_NOCTTY, |
| 9698 |
attrs & FS_NOCOW_FL ? XO_NOCOW : 0, |
| 9699 |
0600); |
| 9700 |
if (fd < 0) |
| 9701 |
return log_error_errno(fd, "Failed to open temporary file %s: %m", temp); |
| 9702 |
|
| 9703 |
if (fstype_is_ro(p->format) || is_btrfs) |
| 9704 |
/* Read-only filesystems and btrfs (with mkfs.btrfs --shrink) produce a minimal |
| 9705 |
* filesystem in one pass, so we can use the real UUID directly. */ |
| 9706 |
fs_uuid = p->fs_uuid; |
| 9707 |
else { |
| 9708 |
/* This may seem huge but it will be created sparse so it doesn't take up any space |
| 9709 |
* on disk until written to. */ |
| 9710 |
if (ftruncate(fd, 1024ULL * 1024ULL * 1024ULL * 1024ULL) < 0) |
| 9711 |
return log_error_errno(errno, "Failed to truncate temporary file to %s: %m", |
| 9712 |
FORMAT_BYTES(1024ULL * 1024ULL * 1024ULL * 1024ULL)); |
| 9713 |
|
| 9714 |
if (arg_offline <= 0) { |
| 9715 |
r = loop_device_make(fd, O_RDWR, 0, UINT64_MAX, context->sector_size, 0, LOCK_EX, &d); |
| 9716 |
if (r < 0 && loop_device_error_is_fatal(p, r)) |
| 9717 |
return log_error_errno(r, "Failed to make loopback device of %s: %m", temp); |
| 9718 |
} |
| 9719 |
|
| 9720 |
/* We're going to populate this filesystem twice so use a random UUID the first time |
| 9721 |
* to avoid UUID conflicts. */ |
| 9722 |
r = sd_id128_randomize(&fs_uuid); |
| 9723 |
if (r < 0) |
| 9724 |
return r; |
| 9725 |
} |
| 9726 |
|
| 9727 |
if (!d || fstype_is_ro(p->format)) { |
| 9728 |
if (!mkfs_supports_root_option(p->format)) |
| 9729 |
return log_error_errno(SYNTHETIC_ERRNO(ENODEV), |
| 9730 |
"Loop device access is required to populate %s filesystems.", |
| 9731 |
p->format); |
| 9732 |
|
| 9733 |
r = partition_populate_directory(context, p, &root); |
| 9734 |
if (r < 0) |
| 9735 |
return r; |
| 9736 |
} |
| 9737 |
|
| 9738 |
r = finalize_extra_mkfs_options(p, root, &extra_mkfs_options); |
| 9739 |
if (r < 0) |
| 9740 |
return r; |
| 9741 |
|
| 9742 |
r = make_filesystem( |
| 9743 |
d ? d->node : temp, |
| 9744 |
p->format, |
| 9745 |
strempty(p->new_label), |
| 9746 |
root, |
| 9747 |
fs_uuid, |
| 9748 |
partition_mkfs_flags(p), |
| 9749 |
partition_fs_sector_size(context, p), |
| 9750 |
p->compression, |
| 9751 |
p->compression_level, |
| 9752 |
extra_mkfs_options); |
| 9753 |
if (r < 0) |
| 9754 |
return r; |
| 9755 |
|
| 9756 |
/* Read-only filesystems are minimal from the first try because they create and size the |
| 9757 |
* loopback file for us. Similarly, mkfs.btrfs --shrink populates the filesystem from the |
| 9758 |
* root directory and then shrinks the backing file to the minimal size. */ |
| 9759 |
if (fstype_is_ro(p->format) || is_btrfs) { |
| 9760 |
fd = safe_close(fd); |
| 9761 |
|
| 9762 |
fd = open(temp, O_RDONLY|O_CLOEXEC|O_NONBLOCK); |
| 9763 |
if (fd < 0) |
| 9764 |
return log_error_errno(errno, "Failed to open temporary file %s: %m", temp); |
| 9765 |
|
| 9766 |
if (fstat(fd, &st) < 0) |
| 9767 |
return log_error_errno(errno, "Failed to stat temporary file: %m"); |
| 9768 |
|
| 9769 |
if ((uint64_t) st.st_size > partition_max_size(context, p)) |
| 9770 |
return log_error_errno(SYNTHETIC_ERRNO(E2BIG), |
| 9771 |
"Minimal partition size of %s filesystem of partition %s exceeds configured maximum size (%s > %s)", |
| 9772 |
p->format, strna(hint), FORMAT_BYTES(st.st_size), FORMAT_BYTES(partition_max_size(context, p))); |
| 9773 |
|
| 9774 |
log_info("Minimal partition size of %s filesystem of partition %s is %s", |
| 9775 |
p->format, strna(hint), FORMAT_BYTES(st.st_size)); |
| 9776 |
|
| 9777 |
p->copy_blocks_path = TAKE_PTR(temp); |
| 9778 |
p->copy_blocks_path_is_our_file = true; |
| 9779 |
p->copy_blocks_fd = TAKE_FD(fd); |
| 9780 |
p->copy_blocks_size = st.st_size; |
| 9781 |
continue; |
| 9782 |
} |
| 9783 |
|
| 9784 |
if (!root) { |
| 9785 |
assert(d); |
| 9786 |
|
| 9787 |
r = partition_populate_filesystem(context, p, d->node); |
| 9788 |
if (r < 0) |
| 9789 |
return r; |
| 9790 |
} |
| 9791 |
|
| 9792 |
/* Other filesystems need to be provided with a pre-sized loopback file and will adapt to |
| 9793 |
* fully occupy it. Because we gave the filesystem a 1T sparse file, we need to shrink the |
| 9794 |
* filesystem down to a reasonable size again to fit it in the disk image. Let's populate the |
| 9795 |
* filesystem again, but this time, instead of providing the filesystem with a 1T sparse |
| 9796 |
* loopback file, let's size the loopback file based on the actual data used by the |
| 9797 |
* filesystem in the sparse file after the first attempt. This should be a good guess of the |
| 9798 |
* minimal amount of space needed in the filesystem to fit all the required data. |
| 9799 |
*/ |
| 9800 |
r = fd_apparent_size(fd, &fsz); |
| 9801 |
if (r < 0) |
| 9802 |
return r; |
| 9803 |
|
| 9804 |
/* Massage the size a bit because just going by actual data used in the sparse file isn't |
| 9805 |
* fool-proof. */ |
| 9806 |
uint64_t heuristic = streq(p->format, "xfs") ? fsz : fsz / 2; |
| 9807 |
fsz = round_up_size(fsz + heuristic, context->grain_size); |
| 9808 |
fsz = MAX(partition_min_size(context, p), fsz); |
| 9809 |
|
| 9810 |
if (fsz > partition_max_size(context, p)) |
| 9811 |
return log_error_errno(SYNTHETIC_ERRNO(E2BIG), |
| 9812 |
"Minimal partition size of %s filesystem of partition %s exceeds configured maximum size (%s > %s)", |
| 9813 |
p->format, strna(hint), FORMAT_BYTES(fsz), FORMAT_BYTES(partition_max_size(context, p))); |
| 9814 |
|
| 9815 |
log_info("Minimal partition size of %s filesystem of partition %s is %s", |
| 9816 |
p->format, strna(hint), FORMAT_BYTES(fsz)); |
| 9817 |
|
| 9818 |
d = loop_device_unref(d); |
| 9819 |
|
| 9820 |
/* Erase the previous filesystem first. */ |
| 9821 |
if (ftruncate(fd, 0) < 0) |
| 9822 |
return log_error_errno(errno, "Failed to erase temporary file: %m"); |
| 9823 |
|
| 9824 |
if (ftruncate(fd, fsz) < 0) |
| 9825 |
return log_error_errno(errno, "Failed to truncate temporary file to %s: %m", FORMAT_BYTES(fsz)); |
| 9826 |
|
| 9827 |
if (arg_offline <= 0) { |
| 9828 |
r = loop_device_make(fd, O_RDWR, 0, UINT64_MAX, context->sector_size, 0, LOCK_EX, &d); |
| 9829 |
if (r < 0 && loop_device_error_is_fatal(p, r)) |
| 9830 |
return log_error_errno(r, "Failed to make loopback device of %s: %m", temp); |
| 9831 |
} |
| 9832 |
|
| 9833 |
r = make_filesystem( |
| 9834 |
d ? d->node : temp, |
| 9835 |
p->format, |
| 9836 |
strempty(p->new_label), |
| 9837 |
root, |
| 9838 |
p->fs_uuid, |
| 9839 |
partition_mkfs_flags(p), |
| 9840 |
partition_fs_sector_size(context, p), |
| 9841 |
p->compression, |
| 9842 |
p->compression_level, |
| 9843 |
extra_mkfs_options); |
| 9844 |
if (r < 0) |
| 9845 |
return r; |
| 9846 |
|
| 9847 |
if (!root) { |
| 9848 |
assert(d); |
| 9849 |
|
| 9850 |
r = partition_populate_filesystem(context, p, d->node); |
| 9851 |
if (r < 0) |
| 9852 |
return r; |
| 9853 |
} |
| 9854 |
|
| 9855 |
if (fstat(fd, &st) < 0) |
| 9856 |
return log_error_errno(errno, "Failed to stat temporary file: %m"); |
| 9857 |
|
| 9858 |
p->copy_blocks_path = TAKE_PTR(temp); |
| 9859 |
p->copy_blocks_path_is_our_file = true; |
| 9860 |
p->copy_blocks_fd = TAKE_FD(fd); |
| 9861 |
p->copy_blocks_size = st.st_size; |
| 9862 |
} |
| 9863 |
|
| 9864 |
/* Now that we've done the data partitions, do the verity hash partitions. We do these in a separate |
| 9865 |
* step because they might depend on data generated in the previous step. */ |
| 9866 |
|
| 9867 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 9868 |
_cleanup_(unlink_and_freep) char *temp = NULL; |
| 9869 |
_cleanup_free_ char *hint = NULL; |
| 9870 |
_cleanup_close_ int fd = -EBADF; |
| 9871 |
struct stat st; |
| 9872 |
Partition *dp; |
| 9873 |
|
| 9874 |
if (p->dropped) |
| 9875 |
continue; |
| 9876 |
|
| 9877 |
if (PARTITION_EXISTS(p)) /* Never format existing partitions */ |
| 9878 |
continue; |
| 9879 |
|
| 9880 |
if (p->copy_blocks_fd >= 0) |
| 9881 |
continue; |
| 9882 |
|
| 9883 |
if (p->minimize == MINIMIZE_OFF) |
| 9884 |
continue; |
| 9885 |
|
| 9886 |
if (p->verity != VERITY_HASH) |
| 9887 |
continue; |
| 9888 |
|
| 9889 |
assert_se(dp = p->siblings[VERITY_DATA]); |
| 9890 |
assert(!dp->dropped); |
| 9891 |
assert(dp->copy_blocks_path); |
| 9892 |
|
| 9893 |
(void) partition_hint(p, context->node, &hint); |
| 9894 |
|
| 9895 |
log_info("Pre-populating verity hash data of partition %s to calculate minimal partition size", |
| 9896 |
strna(hint)); |
| 9897 |
|
| 9898 |
if (!vt) { |
| 9899 |
r = var_tmp_dir(&vt); |
| 9900 |
if (r < 0) |
| 9901 |
return log_error_errno(r, "Could not determine temporary directory: %m"); |
| 9902 |
} |
| 9903 |
|
| 9904 |
r = tempfn_random_child(vt, "repart", &temp); |
| 9905 |
if (r < 0) |
| 9906 |
return log_error_errno(r, "Failed to generate temporary file path: %m"); |
| 9907 |
|
| 9908 |
fd = xopenat_full( |
| 9909 |
AT_FDCWD, |
| 9910 |
temp, |
| 9911 |
O_RDONLY|O_CLOEXEC|O_CREAT|O_NONBLOCK, |
| 9912 |
attrs & FS_NOCOW_FL ? XO_NOCOW : 0, |
| 9913 |
0600); |
| 9914 |
if (fd < 0) |
| 9915 |
return log_error_errno(fd, "Failed to open temporary file %s: %m", temp); |
| 9916 |
|
| 9917 |
r = partition_format_verity_hash(context, p, temp, dp->copy_blocks_path); |
| 9918 |
if (r < 0) |
| 9919 |
return r; |
| 9920 |
|
| 9921 |
if (fstat(fd, &st) < 0) |
| 9922 |
return log_error_errno(errno, "Failed to stat temporary file: %m"); |
| 9923 |
|
| 9924 |
log_info("Minimal partition size of verity hash partition %s is %s", |
| 9925 |
strna(hint), FORMAT_BYTES(st.st_size)); |
| 9926 |
|
| 9927 |
p->copy_blocks_path = TAKE_PTR(temp); |
| 9928 |
p->copy_blocks_path_is_our_file = true; |
| 9929 |
p->copy_blocks_fd = TAKE_FD(fd); |
| 9930 |
p->copy_blocks_size = st.st_size; |
| 9931 |
} |
| 9932 |
|
| 9933 |
return 0; |
| 9934 |
} |
| 9935 |
|
| 9936 |
static int context_load_keys(Context *context) { |
| 9937 |
#if HAVE_OPENSSL |
| 9938 |
int r; |
| 9939 |
|
| 9940 |
assert(context); |
| 9941 |
|
| 9942 |
if (arg_certificate) { |
| 9943 |
if (arg_certificate_source_type == OPENSSL_CERTIFICATE_SOURCE_FILE) { |
| 9944 |
r = parse_path_argument(arg_certificate, /* suppress_root= */ false, &arg_certificate); |
| 9945 |
if (r < 0) |
| 9946 |
return r; |
| 9947 |
} |
| 9948 |
|
| 9949 |
r = openssl_load_x509_certificate( |
| 9950 |
arg_certificate_source_type, |
| 9951 |
arg_certificate_source, |
| 9952 |
arg_certificate, |
| 9953 |
&context->certificate); |
| 9954 |
if (r < 0) |
| 9955 |
return log_error_errno(r, "Failed to load X.509 certificate from %s: %m", arg_certificate); |
| 9956 |
} |
| 9957 |
|
| 9958 |
if (arg_private_key) { |
| 9959 |
if (arg_private_key_source_type == OPENSSL_KEY_SOURCE_FILE) { |
| 9960 |
r = parse_path_argument(arg_private_key, /* suppress_root= */ false, &arg_private_key); |
| 9961 |
if (r < 0) |
| 9962 |
return r; |
| 9963 |
} |
| 9964 |
|
| 9965 |
r = openssl_load_private_key( |
| 9966 |
arg_private_key_source_type, |
| 9967 |
arg_private_key_source, |
| 9968 |
arg_private_key, |
| 9969 |
&(AskPasswordRequest) { |
| 9970 |
.tty_fd = -EBADF, |
| 9971 |
.id = "repart-private-key-pin", |
| 9972 |
.keyring = arg_private_key, |
| 9973 |
.credential = "repart.private-key-pin", |
| 9974 |
.until = USEC_INFINITY, |
| 9975 |
.hup_fd = -EBADF, |
| 9976 |
}, |
| 9977 |
&context->private_key, |
| 9978 |
&context->ui); |
| 9979 |
if (r < 0) |
| 9980 |
return log_error_errno(r, "Failed to load private key from %s: %m", arg_private_key); |
| 9981 |
} |
| 9982 |
|
| 9983 |
#endif |
| 9984 |
return 0; |
| 9985 |
} |
| 9986 |
|
| 9987 |
static int parse_partition_types(const char *p, GptPartitionType **partitions, size_t *n_partitions) { |
| 9988 |
int r; |
| 9989 |
|
| 9990 |
assert(partitions); |
| 9991 |
assert(n_partitions); |
| 9992 |
|
| 9993 |
for (;;) { |
| 9994 |
_cleanup_free_ char *name = NULL; |
| 9995 |
GptPartitionType type; |
| 9996 |
|
| 9997 |
r = extract_first_word(&p, &name, ",", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS); |
| 9998 |
if (r == 0) |
| 9999 |
break; |
| 10000 |
if (r < 0) |
| 10001 |
return log_error_errno(r, "Failed to extract partition type identifier or GUID: %s", p); |
| 10002 |
|
| 10003 |
r = gpt_partition_type_from_string(name, &type); |
| 10004 |
if (r < 0) |
| 10005 |
return log_error_errno(r, "'%s' is not a valid partition type identifier or GUID", name); |
| 10006 |
|
| 10007 |
if (!GREEDY_REALLOC(*partitions, *n_partitions + 1)) |
| 10008 |
return log_oom(); |
| 10009 |
|
| 10010 |
(*partitions)[(*n_partitions)++] = type; |
| 10011 |
} |
| 10012 |
|
| 10013 |
return 0; |
| 10014 |
} |
| 10015 |
|
| 10016 |
static int parse_join_signature(const char *p, Set **verity_settings_map) { |
| 10017 |
_cleanup_(verity_settings_freep) VeritySettings *verity_settings = NULL; |
| 10018 |
_cleanup_free_ char *root_hash = NULL; |
| 10019 |
const char *signature; |
| 10020 |
_cleanup_(iovec_done) struct iovec content = {}; |
| 10021 |
int r; |
| 10022 |
|
| 10023 |
assert(p); |
| 10024 |
assert(verity_settings_map); |
| 10025 |
|
| 10026 |
r = extract_first_word(&p, &root_hash, ":", 0); |
| 10027 |
if (r < 0) |
| 10028 |
return log_error_errno(r, "Failed to parse signature parameter '%s': %m", p); |
| 10029 |
if (!p) |
| 10030 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Expected hash:sig"); |
| 10031 |
if ((signature = startswith(p, "base64:"))) { |
| 10032 |
r = unbase64mem(signature, &content.iov_base, &content.iov_len); |
| 10033 |
if (r < 0) |
| 10034 |
return log_error_errno(r, "Failed to parse root hash signature '%s': %m", signature); |
| 10035 |
} else { |
| 10036 |
r = read_full_file(p, (char**) &content.iov_base, &content.iov_len); |
| 10037 |
if (r < 0) |
| 10038 |
return log_error_errno(r, "Failed to read root hash signature file '%s': %m", p); |
| 10039 |
} |
| 10040 |
if (!iovec_is_set(&content)) |
| 10041 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Empty verity signature specified."); |
| 10042 |
if (content.iov_len > VERITY_SIG_SIZE) |
| 10043 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 10044 |
"Verity signatures larger than %llu are not allowed.", |
| 10045 |
VERITY_SIG_SIZE); |
| 10046 |
|
| 10047 |
verity_settings = new(VeritySettings, 1); |
| 10048 |
if (!verity_settings) |
| 10049 |
return log_oom(); |
| 10050 |
|
| 10051 |
*verity_settings = (VeritySettings) { |
| 10052 |
.root_hash_sig = TAKE_STRUCT(content), |
| 10053 |
}; |
| 10054 |
|
| 10055 |
r = unhexmem(root_hash, &verity_settings->root_hash.iov_base, &verity_settings->root_hash.iov_len); |
| 10056 |
if (r < 0) |
| 10057 |
return log_error_errno(r, "Failed to parse root hash '%s': %m", root_hash); |
| 10058 |
if (verity_settings->root_hash.iov_len < sizeof(sd_id128_t)) |
| 10059 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 10060 |
"Root hash must be at least 128-bit long: %s", |
| 10061 |
root_hash); |
| 10062 |
|
| 10063 |
r = set_ensure_put(verity_settings_map, &verity_settings_hash_ops, verity_settings); |
| 10064 |
if (r < 0) |
| 10065 |
return log_error_errno(r, "Failed to add entry to hashmap: %m"); |
| 10066 |
|
| 10067 |
TAKE_PTR(verity_settings); |
| 10068 |
|
| 10069 |
return 0; |
| 10070 |
} |
| 10071 |
|
| 10072 |
static int help(void) { |
| 10073 |
_cleanup_free_ char *link = NULL; |
| 10074 |
int r; |
| 10075 |
|
| 10076 |
r = terminal_urlify_man("systemd-repart", "8", &link); |
| 10077 |
if (r < 0) |
| 10078 |
return log_oom(); |
| 10079 |
|
| 10080 |
static const char *const option_groups[] = { |
| 10081 |
"Options", |
| 10082 |
"Operation", |
| 10083 |
"Output", |
| 10084 |
"Factory Reset", |
| 10085 |
"Configuration & Image Control", |
| 10086 |
"Verity", |
| 10087 |
"Encryption", |
| 10088 |
"Partition Control", |
| 10089 |
"Copying", |
| 10090 |
"DDI Profile", |
| 10091 |
"Auxiliary Resource Generation", |
| 10092 |
"El Torito boot catalog", |
| 10093 |
}; |
| 10094 |
|
| 10095 |
Table *option_tables[ELEMENTSOF(option_groups)] = {}; |
| 10096 |
CLEANUP_ELEMENTS(option_tables, table_unref_array_clear); |
| 10097 |
|
| 10098 |
for (size_t i = 0; i < ELEMENTSOF(option_groups); i++) { |
| 10099 |
r = option_parser_get_help_table_group(option_groups[i], &option_tables[i]); |
| 10100 |
if (r < 0) |
| 10101 |
return r; |
| 10102 |
} |
| 10103 |
|
| 10104 |
(void) table_sync_column_widths(0, |
| 10105 |
option_tables[0], option_tables[1], option_tables[2], |
| 10106 |
option_tables[3], option_tables[4], option_tables[5], |
| 10107 |
option_tables[6], option_tables[7], option_tables[8], |
| 10108 |
option_tables[9], option_tables[10], option_tables[11]); |
| 10109 |
|
| 10110 |
printf("%s [OPTIONS...] [DEVICE]\n" |
| 10111 |
"\n%sGrow and add partitions to a partition table, and generate disk images (DDIs).%s\n", |
| 10112 |
program_invocation_short_name, |
| 10113 |
ansi_highlight(), |
| 10114 |
ansi_normal()); |
| 10115 |
|
| 10116 |
for (size_t i = 0; i < ELEMENTSOF(option_groups); i++) { |
| 10117 |
printf("\n%s%s:%s\n", ansi_underline(), option_groups[i], ansi_normal()); |
| 10118 |
|
| 10119 |
r = table_print_or_warn(option_tables[i]); |
| 10120 |
if (r < 0) |
| 10121 |
return r; |
| 10122 |
} |
| 10123 |
|
| 10124 |
printf("\nSee the %s for details.\n", link); |
| 10125 |
return 0; |
| 10126 |
} |
| 10127 |
|
| 10128 |
static int parse_argv(int argc, char *argv[]) { |
| 10129 |
assert(argc >= 0); |
| 10130 |
assert(argv); |
| 10131 |
|
| 10132 |
OptionParser opts = { argc, argv }; |
| 10133 |
bool auto_public_key_pcr_mask = true, auto_pcrlock = true; |
| 10134 |
int r; |
| 10135 |
|
| 10136 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 10137 |
switch (c) { |
| 10138 |
|
| 10139 |
OPTION_GROUP("Options"): {} |
| 10140 |
|
| 10141 |
OPTION_COMMON_HELP: |
| 10142 |
return help(); |
| 10143 |
|
| 10144 |
OPTION_COMMON_VERSION: |
| 10145 |
return version(); |
| 10146 |
|
| 10147 |
OPTION_COMMON_NO_PAGER: |
| 10148 |
arg_pager_flags |= PAGER_DISABLE; |
| 10149 |
break; |
| 10150 |
|
| 10151 |
OPTION_COMMON_NO_LEGEND: |
| 10152 |
arg_legend = false; |
| 10153 |
break; |
| 10154 |
|
| 10155 |
OPTION_GROUP("Operation"): {} |
| 10156 |
|
| 10157 |
OPTION_LONG("dry-run", "BOOL", |
| 10158 |
"Whether to run dry-run operation"): |
| 10159 |
r = parse_boolean_argument("--dry-run=", opts.arg, &arg_dry_run); |
| 10160 |
if (r < 0) |
| 10161 |
return r; |
| 10162 |
break; |
| 10163 |
|
| 10164 |
OPTION_LONG("empty", "MODE", |
| 10165 |
"How to handle empty disks lacking partition tables (refuse, allow, require, force, create)"): |
| 10166 |
if (isempty(opts.arg)) { |
| 10167 |
arg_empty = EMPTY_UNSET; |
| 10168 |
break; |
| 10169 |
} |
| 10170 |
|
| 10171 |
arg_empty = empty_mode_from_string(opts.arg); |
| 10172 |
if (arg_empty < 0) |
| 10173 |
return log_error_errno(arg_empty, "Failed to parse --empty= parameter: %s", opts.arg); |
| 10174 |
|
| 10175 |
break; |
| 10176 |
|
| 10177 |
OPTION_LONG("offline", "BOOL", |
| 10178 |
"Whether to build the image offline"): |
| 10179 |
r = parse_tristate_argument_with_auto("--offline=", opts.arg, &arg_offline); |
| 10180 |
if (r < 0) |
| 10181 |
return r; |
| 10182 |
break; |
| 10183 |
|
| 10184 |
OPTION_LONG("discard", "BOOL", |
| 10185 |
"Whether to discard backing blocks for new partitions"): |
| 10186 |
r = parse_boolean_argument("--discard=", opts.arg, &arg_discard); |
| 10187 |
if (r < 0) |
| 10188 |
return r; |
| 10189 |
break; |
| 10190 |
|
| 10191 |
OPTION_LONG("sector-size", "SIZE", |
| 10192 |
"Set the logical sector size for the image"): |
| 10193 |
r = parse_sector_size(opts.arg, &arg_sector_size); |
| 10194 |
if (r < 0) |
| 10195 |
return r; |
| 10196 |
|
| 10197 |
break; |
| 10198 |
|
| 10199 |
OPTION_LONG("grain-size", "BYTES", |
| 10200 |
"Set the grain size for partition alignment"): |
| 10201 |
r = parse_size(opts.arg, 1024, &arg_grain_size); |
| 10202 |
if (r < 0) |
| 10203 |
return log_error_errno(r, "Failed to parse --grain-size= parameter: %s", opts.arg); |
| 10204 |
if (arg_grain_size < 512 || !ISPOWEROF2(arg_grain_size)) |
| 10205 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Grain size must be a power of 2 >= 512."); |
| 10206 |
|
| 10207 |
break; |
| 10208 |
|
| 10209 |
OPTION_LONG("architecture", "ARCH", |
| 10210 |
"Set the generic architecture for the image"): |
| 10211 |
r = architecture_from_string(opts.arg); |
| 10212 |
if (r < 0) |
| 10213 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid architecture '%s'.", opts.arg); |
| 10214 |
|
| 10215 |
arg_architecture = r; |
| 10216 |
break; |
| 10217 |
|
| 10218 |
OPTION_LONG("size", "BYTES", |
| 10219 |
"Grow loopback file to specified size"): { |
| 10220 |
uint64_t parsed, rounded; |
| 10221 |
|
| 10222 |
if (streq(opts.arg, "auto")) { |
| 10223 |
arg_size = UINT64_MAX; |
| 10224 |
arg_size_auto = true; |
| 10225 |
break; |
| 10226 |
} |
| 10227 |
|
| 10228 |
r = parse_size(opts.arg, 1024, &parsed); |
| 10229 |
if (r < 0) |
| 10230 |
return log_error_errno(r, "Failed to parse --size= parameter: %s", opts.arg); |
| 10231 |
|
| 10232 |
rounded = round_up_size(parsed, 4096); |
| 10233 |
if (rounded == 0) |
| 10234 |
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Specified image size too small, refusing."); |
| 10235 |
if (rounded == UINT64_MAX) |
| 10236 |
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Specified image size too large, refusing."); |
| 10237 |
|
| 10238 |
if (rounded != parsed) |
| 10239 |
log_warning("Specified size is not a multiple of 4096, rounding up automatically. (%" PRIu64 " %s %" PRIu64 ")", |
| 10240 |
parsed, glyph(GLYPH_ARROW_RIGHT), rounded); |
| 10241 |
|
| 10242 |
arg_size = rounded; |
| 10243 |
arg_size_auto = false; |
| 10244 |
break; |
| 10245 |
} |
| 10246 |
|
| 10247 |
OPTION_LONG("seed", "UUID", |
| 10248 |
"128-bit seed UUID to derive all UUIDs from"): |
| 10249 |
if (isempty(opts.arg)) { |
| 10250 |
arg_seed = SD_ID128_NULL; |
| 10251 |
arg_randomize = false; |
| 10252 |
} else if (streq(opts.arg, "random")) |
| 10253 |
arg_randomize = true; |
| 10254 |
else { |
| 10255 |
r = sd_id128_from_string(opts.arg, &arg_seed); |
| 10256 |
if (r < 0) |
| 10257 |
return log_error_errno(r, "Failed to parse seed: %s", opts.arg); |
| 10258 |
|
| 10259 |
arg_randomize = false; |
| 10260 |
} |
| 10261 |
|
| 10262 |
break; |
| 10263 |
|
| 10264 |
OPTION_LONG("split", "BOOL", |
| 10265 |
"Whether to generate split artifacts"): |
| 10266 |
r = parse_boolean_argument("--split=", opts.arg, NULL); |
| 10267 |
if (r < 0) |
| 10268 |
return r; |
| 10269 |
|
| 10270 |
arg_split = r; |
| 10271 |
break; |
| 10272 |
|
| 10273 |
OPTION_GROUP("Output"): {} |
| 10274 |
|
| 10275 |
OPTION_LONG("pretty", "BOOL", |
| 10276 |
"Whether to show pretty summary before doing changes"): |
| 10277 |
r = parse_boolean_argument("--pretty=", opts.arg, NULL); |
| 10278 |
if (r < 0) |
| 10279 |
return r; |
| 10280 |
arg_pretty = r; |
| 10281 |
break; |
| 10282 |
|
| 10283 |
OPTION_COMMON_JSON: |
| 10284 |
r = parse_json_argument(opts.arg, &arg_json_format_flags); |
| 10285 |
if (r <= 0) |
| 10286 |
return r; |
| 10287 |
|
| 10288 |
break; |
| 10289 |
|
| 10290 |
OPTION_GROUP("Factory Reset"): {} |
| 10291 |
|
| 10292 |
OPTION_LONG("factory-reset", "BOOL", |
| 10293 |
"Whether to remove data partitions before recreating them"): |
| 10294 |
r = parse_boolean_argument("--factory-reset=", opts.arg, NULL); |
| 10295 |
if (r < 0) |
| 10296 |
return r; |
| 10297 |
arg_factory_reset = r; |
| 10298 |
break; |
| 10299 |
|
| 10300 |
OPTION_LONG("can-factory-reset", NULL, |
| 10301 |
"Test whether factory reset is defined"): |
| 10302 |
arg_can_factory_reset = true; |
| 10303 |
break; |
| 10304 |
|
| 10305 |
OPTION_GROUP("Configuration & Image Control"): {} |
| 10306 |
|
| 10307 |
OPTION_LONG("root", "PATH", |
| 10308 |
"Operate relative to root path"): |
| 10309 |
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_root); |
| 10310 |
if (r < 0) |
| 10311 |
return r; |
| 10312 |
break; |
| 10313 |
|
| 10314 |
OPTION_LONG("image", "PATH", |
| 10315 |
"Operate relative to image file"): |
| 10316 |
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_image); |
| 10317 |
if (r < 0) |
| 10318 |
return r; |
| 10319 |
|
| 10320 |
arg_relax_copy_block_security = false; |
| 10321 |
|
| 10322 |
break; |
| 10323 |
|
| 10324 |
OPTION_LONG("image-policy", "POLICY", |
| 10325 |
"Specify disk image dissection policy"): |
| 10326 |
r = parse_image_policy_argument(opts.arg, &arg_image_policy); |
| 10327 |
if (r < 0) |
| 10328 |
return r; |
| 10329 |
break; |
| 10330 |
|
| 10331 |
OPTION_LONG("definitions", "DIR", |
| 10332 |
"Find partition definitions in specified directory"): { |
| 10333 |
_cleanup_free_ char *path = NULL; |
| 10334 |
r = parse_path_argument(opts.arg, false, &path); |
| 10335 |
if (r < 0) |
| 10336 |
return r; |
| 10337 |
if (strv_consume(&arg_definitions, TAKE_PTR(path)) < 0) |
| 10338 |
return log_oom(); |
| 10339 |
break; |
| 10340 |
} |
| 10341 |
|
| 10342 |
OPTION_LONG("list-devices", NULL, |
| 10343 |
"List candidate block devices to operate on"): |
| 10344 |
r = blockdev_list( |
| 10345 |
BLOCKDEV_LIST_SHOW_SYMLINKS| |
| 10346 |
BLOCKDEV_LIST_REQUIRE_PARTITION_SCANNING| |
| 10347 |
BLOCKDEV_LIST_IGNORE_ZRAM| |
| 10348 |
BLOCKDEV_LIST_IGNORE_READ_ONLY, |
| 10349 |
/* ret_devices= */ NULL, |
| 10350 |
/* ret_n_devices= */ NULL); |
| 10351 |
if (r < 0) |
| 10352 |
return r; |
| 10353 |
|
| 10354 |
return 0; |
| 10355 |
|
| 10356 |
OPTION_GROUP("Verity"): {} |
| 10357 |
|
| 10358 |
OPTION_COMMON_PRIVATE_KEY("Private key to use when generating verity roothash signatures"): |
| 10359 |
r = free_and_strdup_warn(&arg_private_key, opts.arg); |
| 10360 |
if (r < 0) |
| 10361 |
return r; |
| 10362 |
break; |
| 10363 |
|
| 10364 |
OPTION_COMMON_PRIVATE_KEY_SOURCE: |
| 10365 |
r = parse_openssl_key_source_argument( |
| 10366 |
opts.arg, |
| 10367 |
&arg_private_key_source, |
| 10368 |
&arg_private_key_source_type); |
| 10369 |
if (r < 0) |
| 10370 |
return r; |
| 10371 |
break; |
| 10372 |
|
| 10373 |
OPTION_COMMON_CERTIFICATE("PEM certificate to use when generating verity roothash signatures"): |
| 10374 |
r = free_and_strdup_warn(&arg_certificate, opts.arg); |
| 10375 |
if (r < 0) |
| 10376 |
return r; |
| 10377 |
break; |
| 10378 |
|
| 10379 |
OPTION_COMMON_CERTIFICATE_SOURCE: |
| 10380 |
r = parse_openssl_certificate_source_argument( |
| 10381 |
opts.arg, |
| 10382 |
&arg_certificate_source, |
| 10383 |
&arg_certificate_source_type); |
| 10384 |
if (r < 0) |
| 10385 |
return r; |
| 10386 |
break; |
| 10387 |
|
| 10388 |
OPTION_LONG("join-signature", "HASH:SIG", |
| 10389 |
"Specify root hash and pkcs7 signature of root hash for verity as a tuple of " |
| 10390 |
"hex-encoded hash and a DER-encoded PKCS7, either as a path to a file or as an " |
| 10391 |
"ASCII base64-encoded string prefixed by 'base64:'"): |
| 10392 |
r = parse_join_signature(opts.arg, &arg_verity_settings); |
| 10393 |
if (r < 0) |
| 10394 |
return r; |
| 10395 |
break; |
| 10396 |
|
| 10397 |
OPTION_GROUP("Encryption"): {} |
| 10398 |
|
| 10399 |
OPTION_LONG("key-file", "PATH", |
| 10400 |
"Key to use when encrypting partitions"): |
| 10401 |
r = parse_key_file(opts.arg, &arg_key); |
| 10402 |
if (r < 0) |
| 10403 |
return r; |
| 10404 |
break; |
| 10405 |
|
| 10406 |
OPTION_LONG("tpm2-device", "PATH", |
| 10407 |
"Path to TPM2 device node to use"): { |
| 10408 |
_cleanup_free_ char *device = NULL; |
| 10409 |
|
| 10410 |
if (streq(opts.arg, "list")) |
| 10411 |
return tpm2_list_devices(/* legend= */ true, /* quiet= */ false); |
| 10412 |
|
| 10413 |
if (!streq(opts.arg, "auto")) { |
| 10414 |
device = strdup(opts.arg); |
| 10415 |
if (!device) |
| 10416 |
return log_oom(); |
| 10417 |
} |
| 10418 |
|
| 10419 |
free(arg_tpm2_device); |
| 10420 |
arg_tpm2_device = TAKE_PTR(device); |
| 10421 |
break; |
| 10422 |
} |
| 10423 |
|
| 10424 |
OPTION_LONG("tpm2-device-key", "PATH", |
| 10425 |
"Enroll a TPM2 device using its public key"): |
| 10426 |
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_tpm2_device_key); |
| 10427 |
if (r < 0) |
| 10428 |
return r; |
| 10429 |
|
| 10430 |
break; |
| 10431 |
|
| 10432 |
OPTION_LONG("tpm2-seal-key-handle", "HANDLE", |
| 10433 |
"Specify handle of key to use for sealing"): |
| 10434 |
r = safe_atou32_full(opts.arg, 16, &arg_tpm2_seal_key_handle); |
| 10435 |
if (r < 0) |
| 10436 |
return log_error_errno(r, "Could not parse TPM2 seal key handle index '%s': %m", opts.arg); |
| 10437 |
|
| 10438 |
break; |
| 10439 |
|
| 10440 |
OPTION_LONG("tpm2-pcrs", "PCR1+PCR2+…", |
| 10441 |
"TPM2 PCR indexes to use for TPM2 enrollment"): |
| 10442 |
r = tpm2_parse_pcr_argument_append(opts.arg, &arg_tpm2_hash_pcr_values, &arg_tpm2_n_hash_pcr_values); |
| 10443 |
if (r < 0) |
| 10444 |
return r; |
| 10445 |
|
| 10446 |
break; |
| 10447 |
|
| 10448 |
OPTION_LONG("tpm2-public-key", "PATH", |
| 10449 |
"Enroll signed TPM2 PCR policy against PEM public key"): |
| 10450 |
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_tpm2_public_key); |
| 10451 |
if (r < 0) |
| 10452 |
return r; |
| 10453 |
|
| 10454 |
break; |
| 10455 |
|
| 10456 |
OPTION_LONG("tpm2-public-key-pcrs", "PCR1+PCR2+…", |
| 10457 |
"Enroll signed TPM2 PCR policy for specified TPM2 PCRs"): |
| 10458 |
auto_public_key_pcr_mask = false; |
| 10459 |
r = tpm2_parse_pcr_argument_to_mask(opts.arg, &arg_tpm2_public_key_pcr_mask); |
| 10460 |
if (r < 0) |
| 10461 |
return r; |
| 10462 |
|
| 10463 |
break; |
| 10464 |
|
| 10465 |
OPTION_LONG("tpm2-pcrlock", "PATH", |
| 10466 |
"Specify pcrlock policy to lock against"): |
| 10467 |
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_tpm2_pcrlock); |
| 10468 |
if (r < 0) |
| 10469 |
return r; |
| 10470 |
|
| 10471 |
auto_pcrlock = false; |
| 10472 |
break; |
| 10473 |
|
| 10474 |
OPTION_GROUP("Partition Control"): {} |
| 10475 |
|
| 10476 |
OPTION_LONG("include-partitions", "PART1,PART2…", |
| 10477 |
"Ignore partitions not of the specified types"): |
| 10478 |
if (arg_filter_partitions_type == FILTER_PARTITIONS_EXCLUDE) |
| 10479 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 10480 |
"Combination of --include-partitions= and --exclude-partitions= is invalid."); |
| 10481 |
|
| 10482 |
r = parse_partition_types(opts.arg, &arg_filter_partitions, &arg_n_filter_partitions); |
| 10483 |
if (r < 0) |
| 10484 |
return r; |
| 10485 |
|
| 10486 |
arg_filter_partitions_type = FILTER_PARTITIONS_INCLUDE; |
| 10487 |
|
| 10488 |
break; |
| 10489 |
|
| 10490 |
OPTION_LONG("exclude-partitions", "PART1,PART2…", |
| 10491 |
"Ignore partitions of the specified types"): |
| 10492 |
if (arg_filter_partitions_type == FILTER_PARTITIONS_INCLUDE) |
| 10493 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 10494 |
"Combination of --include-partitions= and --exclude-partitions= is invalid."); |
| 10495 |
|
| 10496 |
r = parse_partition_types(opts.arg, &arg_filter_partitions, &arg_n_filter_partitions); |
| 10497 |
if (r < 0) |
| 10498 |
return r; |
| 10499 |
|
| 10500 |
arg_filter_partitions_type = FILTER_PARTITIONS_EXCLUDE; |
| 10501 |
|
| 10502 |
break; |
| 10503 |
|
| 10504 |
OPTION_LONG("defer-partitions", "PART1,PART2…", |
| 10505 |
"Take partitions of the specified types into account but don't populate them yet"): |
| 10506 |
r = parse_partition_types(opts.arg, &arg_defer_partitions, &arg_n_defer_partitions); |
| 10507 |
if (r < 0) |
| 10508 |
return r; |
| 10509 |
|
| 10510 |
break; |
| 10511 |
|
| 10512 |
OPTION_LONG("defer-partitions-empty", "BOOL", |
| 10513 |
"Defer all partitions marked for formatting as empty"): |
| 10514 |
r = parse_boolean_argument("--defer-partitions-empty=", opts.arg, &arg_defer_partitions_empty); |
| 10515 |
if (r < 0) |
| 10516 |
return r; |
| 10517 |
|
| 10518 |
break; |
| 10519 |
|
| 10520 |
OPTION_LONG("defer-partitions-factory-reset", "BOOL", |
| 10521 |
"Defer all partitions marked for factory reset"): |
| 10522 |
r = parse_boolean_argument("--defer-partitions-factory-reset=", opts.arg, &arg_defer_partitions_factory_reset); |
| 10523 |
if (r < 0) |
| 10524 |
return r; |
| 10525 |
|
| 10526 |
break; |
| 10527 |
|
| 10528 |
OPTION_GROUP("Copying"): {} |
| 10529 |
|
| 10530 |
OPTION('s', "copy-source", "PATH", |
| 10531 |
"Specify the primary source tree to copy files from"): |
| 10532 |
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_copy_source); |
| 10533 |
if (r < 0) |
| 10534 |
return r; |
| 10535 |
break; |
| 10536 |
|
| 10537 |
OPTION_LONG("copy-from", "IMAGE", |
| 10538 |
"Copy partitions from the given image"): { |
| 10539 |
_cleanup_free_ char *p = NULL; |
| 10540 |
|
| 10541 |
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &p); |
| 10542 |
if (r < 0) |
| 10543 |
return r; |
| 10544 |
|
| 10545 |
if (strv_consume(&arg_copy_from, TAKE_PTR(p)) < 0) |
| 10546 |
return log_oom(); |
| 10547 |
|
| 10548 |
break; |
| 10549 |
} |
| 10550 |
|
| 10551 |
OPTION_GROUP("DDI Profile"): {} |
| 10552 |
|
| 10553 |
OPTION_LONG("make-ddi", "TYPE", |
| 10554 |
"Create a DDI of the given type"): |
| 10555 |
if (!filename_is_valid(opts.arg)) |
| 10556 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid DDI type: %s", opts.arg); |
| 10557 |
|
| 10558 |
r = free_and_strdup_warn(&arg_make_ddi, opts.arg); |
| 10559 |
if (r < 0) |
| 10560 |
return r; |
| 10561 |
break; |
| 10562 |
|
| 10563 |
OPTION_SHORT('S', NULL, "Same as --make-ddi=sysext, make a system extension"): |
| 10564 |
r = free_and_strdup_warn(&arg_make_ddi, "sysext"); |
| 10565 |
if (r < 0) |
| 10566 |
return r; |
| 10567 |
break; |
| 10568 |
|
| 10569 |
OPTION_SHORT('C', NULL, "Same as --make-ddi=confext, make a configuration extension"): |
| 10570 |
r = free_and_strdup_warn(&arg_make_ddi, "confext"); |
| 10571 |
if (r < 0) |
| 10572 |
return r; |
| 10573 |
break; |
| 10574 |
|
| 10575 |
OPTION_SHORT('P', NULL, "Same as --make-ddi=portable, make a portable service"): |
| 10576 |
r = free_and_strdup_warn(&arg_make_ddi, "portable"); |
| 10577 |
if (r < 0) |
| 10578 |
return r; |
| 10579 |
break; |
| 10580 |
|
| 10581 |
OPTION_GROUP("Auxiliary Resource Generation"): {} |
| 10582 |
|
| 10583 |
OPTION_LONG("append-fstab", "MODE", |
| 10584 |
"How to join the content of a pre-existing fstab with the generated one " |
| 10585 |
"(no, auto, replace)"): |
| 10586 |
if (isempty(opts.arg)) { |
| 10587 |
arg_append_fstab = APPEND_AUTO; |
| 10588 |
break; |
| 10589 |
} |
| 10590 |
|
| 10591 |
arg_append_fstab = append_mode_from_string(opts.arg); |
| 10592 |
if (arg_append_fstab < 0) |
| 10593 |
return log_error_errno(arg_append_fstab, "Failed to parse --append-fstab= parameter: %s", opts.arg); |
| 10594 |
break; |
| 10595 |
|
| 10596 |
OPTION_LONG("generate-fstab", "PATH", |
| 10597 |
"Write fstab configuration to the given path"): |
| 10598 |
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_generate_fstab); |
| 10599 |
if (r < 0) |
| 10600 |
return r; |
| 10601 |
break; |
| 10602 |
|
| 10603 |
OPTION_LONG("generate-crypttab", "PATH", |
| 10604 |
"Write crypttab configuration to the given path"): |
| 10605 |
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_generate_crypttab); |
| 10606 |
if (r < 0) |
| 10607 |
return r; |
| 10608 |
break; |
| 10609 |
|
| 10610 |
OPTION_GROUP("El Torito boot catalog"): {} |
| 10611 |
|
| 10612 |
OPTION_LONG("el-torito", "BOOL", |
| 10613 |
"Whether to add a boot catalog to boot the ESP"): |
| 10614 |
r = parse_boolean_argument("--el-torito=", opts.arg, &arg_eltorito); |
| 10615 |
if (r < 0) |
| 10616 |
return r; |
| 10617 |
|
| 10618 |
break; |
| 10619 |
|
| 10620 |
OPTION_LONG("el-torito-system", "STRING", |
| 10621 |
"Set the system identifier in the ISO9660 descriptor"): |
| 10622 |
if (!iso9660_system_name_valid(opts.arg)) |
| 10623 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid value '%s' for --el-torito-system=.", opts.arg); |
| 10624 |
|
| 10625 |
r = free_and_strdup_warn(&arg_eltorito_system, opts.arg); |
| 10626 |
if (r < 0) |
| 10627 |
return r; |
| 10628 |
|
| 10629 |
break; |
| 10630 |
|
| 10631 |
OPTION_LONG("el-torito-volume", "STRING", |
| 10632 |
"Set the volume identifier in the ISO9660 descriptor"): |
| 10633 |
if (!iso9660_volume_name_valid(opts.arg)) |
| 10634 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid value '%s' for --el-torito-volume=.", opts.arg); |
| 10635 |
|
| 10636 |
r = free_and_strdup_warn(&arg_eltorito_volume, opts.arg); |
| 10637 |
if (r < 0) |
| 10638 |
return r; |
| 10639 |
|
| 10640 |
break; |
| 10641 |
|
| 10642 |
OPTION_LONG("el-torito-publisher", "STRING", |
| 10643 |
"Set the publisher identifier in the ISO9660 descriptor"): |
| 10644 |
if (!iso9660_publisher_name_valid(opts.arg)) |
| 10645 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid value '%s' for --el-torito-publisher=.", opts.arg); |
| 10646 |
|
| 10647 |
r = free_and_strdup_warn(&arg_eltorito_publisher, opts.arg); |
| 10648 |
if (r < 0) |
| 10649 |
return r; |
| 10650 |
|
| 10651 |
break; |
| 10652 |
} |
| 10653 |
|
| 10654 |
if (option_parser_get_n_args(&opts) > 1) |
| 10655 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 10656 |
"Expected at most one argument, the path to the block device or image file."); |
| 10657 |
|
| 10658 |
if (arg_make_ddi) { |
| 10659 |
if (arg_definitions) |
| 10660 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Combination of --make-ddi= and --definitions= is not supported."); |
| 10661 |
if (!IN_SET(arg_empty, EMPTY_UNSET, EMPTY_CREATE)) |
| 10662 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Combination of --make-ddi= and --empty=%s is not supported.", empty_mode_to_string(arg_empty)); |
| 10663 |
|
| 10664 |
/* Imply automatic sizing in DDI mode */ |
| 10665 |
if (arg_size == UINT64_MAX) |
| 10666 |
arg_size_auto = true; |
| 10667 |
|
| 10668 |
if (!arg_copy_source) |
| 10669 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No --copy-source= specified, refusing."); |
| 10670 |
|
| 10671 |
r = dir_is_empty(arg_copy_source, /* ignore_hidden_or_backup= */ false); |
| 10672 |
if (r < 0) |
| 10673 |
return log_error_errno(r, "Failed to determine if '%s' is empty: %m", arg_copy_source); |
| 10674 |
if (r > 0) |
| 10675 |
return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Source directory '%s' is empty, refusing to create empty image.", arg_copy_source); |
| 10676 |
|
| 10677 |
if (sd_id128_is_null(arg_seed) && !arg_randomize) { |
| 10678 |
/* We don't want that /etc/machine-id leaks into any image built this way, hence |
| 10679 |
* let's randomize the seed if not specified explicitly */ |
| 10680 |
log_notice("No seed value specified, randomizing generated UUIDs, resulting image will not be reproducible."); |
| 10681 |
arg_randomize = true; |
| 10682 |
} |
| 10683 |
|
| 10684 |
arg_empty = EMPTY_CREATE; |
| 10685 |
} |
| 10686 |
|
| 10687 |
if (arg_empty == EMPTY_UNSET) /* default to refuse mode, if not otherwise specified */ |
| 10688 |
arg_empty = EMPTY_REFUSE; |
| 10689 |
|
| 10690 |
if (!set_isempty(arg_verity_settings) && !arg_certificate) |
| 10691 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Verity signature specified without --certificate=."); |
| 10692 |
|
| 10693 |
if (arg_factory_reset > 0 && IN_SET(arg_empty, EMPTY_FORCE, EMPTY_REQUIRE, EMPTY_CREATE)) |
| 10694 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 10695 |
"Combination of --factory-reset=yes and --empty=force/--empty=require/--empty=create is invalid."); |
| 10696 |
|
| 10697 |
if (arg_can_factory_reset) |
| 10698 |
arg_dry_run = true; /* When --can-factory-reset is specified we don't make changes, hence |
| 10699 |
* non-dry-run mode makes no sense. Thus, imply dry run mode so that we |
| 10700 |
* open things strictly read-only. */ |
| 10701 |
else if (arg_empty == EMPTY_CREATE) |
| 10702 |
arg_dry_run = false; /* Imply --dry-run=no if we create the loopback file anew. After all we |
| 10703 |
* cannot really break anyone's partition tables that way. */ |
| 10704 |
|
| 10705 |
/* Disable pager once we are not just reviewing, but doing things. */ |
| 10706 |
if (!arg_dry_run) |
| 10707 |
arg_pager_flags |= PAGER_DISABLE; |
| 10708 |
|
| 10709 |
if (arg_empty == EMPTY_CREATE && arg_size == UINT64_MAX && !arg_size_auto) |
| 10710 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 10711 |
"If --empty=create is specified, --size= must be specified, too."); |
| 10712 |
|
| 10713 |
if (arg_image && arg_root) |
| 10714 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported."); |
| 10715 |
else if (!arg_image && !arg_root && in_initrd()) { |
| 10716 |
|
| 10717 |
/* By default operate on /sysusr/ or /sysroot/ when invoked in the initrd. We prefer the |
| 10718 |
* former, if it is mounted, so that we have deterministic behaviour on systems where /usr/ |
| 10719 |
* is vendor-supplied but the root fs formatted on first boot. */ |
| 10720 |
r = path_is_mount_point("/sysusr/usr"); |
| 10721 |
if (r <= 0) { |
| 10722 |
if (r < 0 && r != -ENOENT) |
| 10723 |
log_debug_errno(r, "Unable to determine whether /sysusr/usr is a mount point, assuming it is not: %m"); |
| 10724 |
|
| 10725 |
arg_root = strdup("/sysroot"); |
| 10726 |
} else |
| 10727 |
arg_root = strdup("/sysusr"); |
| 10728 |
if (!arg_root) |
| 10729 |
return log_oom(); |
| 10730 |
|
| 10731 |
arg_relax_copy_block_security = true; |
| 10732 |
} |
| 10733 |
|
| 10734 |
char **args = option_parser_get_args(&opts); |
| 10735 |
if (!strv_isempty(args)) { |
| 10736 |
if (empty_or_dash(args[0])) |
| 10737 |
arg_node_none = true; |
| 10738 |
else { |
| 10739 |
arg_node = strdup(args[0]); |
| 10740 |
if (!arg_node) |
| 10741 |
return log_oom(); |
| 10742 |
arg_node_none = false; |
| 10743 |
} |
| 10744 |
} |
| 10745 |
|
| 10746 |
if (IN_SET(arg_empty, EMPTY_FORCE, EMPTY_REQUIRE, EMPTY_CREATE) && !arg_node && !arg_image) |
| 10747 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 10748 |
"A path to a device node or image file must be specified when --make-ddi=, --empty=force, --empty=require or --empty=create are used."); |
| 10749 |
|
| 10750 |
if (arg_split && !arg_node) |
| 10751 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 10752 |
"A path to an image file must be specified when --split is used."); |
| 10753 |
|
| 10754 |
if (auto_pcrlock) { |
| 10755 |
assert(!arg_tpm2_pcrlock); |
| 10756 |
|
| 10757 |
r = tpm2_pcrlock_search_file(NULL, NULL, &arg_tpm2_pcrlock); |
| 10758 |
if (r < 0) { |
| 10759 |
if (r != -ENOENT) |
| 10760 |
log_warning_errno(r, "Search for pcrlock.json failed, assuming it does not exist: %m"); |
| 10761 |
} else |
| 10762 |
log_debug("Automatically using pcrlock policy '%s'.", arg_tpm2_pcrlock); |
| 10763 |
} |
| 10764 |
|
| 10765 |
if (auto_public_key_pcr_mask) { |
| 10766 |
assert(arg_tpm2_public_key_pcr_mask == 0); |
| 10767 |
arg_tpm2_public_key_pcr_mask = INDEX_TO_MASK(uint32_t, TPM2_PCR_KERNEL_BOOT); |
| 10768 |
} |
| 10769 |
|
| 10770 |
if (arg_pretty < 0 && isatty_safe(STDOUT_FILENO)) |
| 10771 |
arg_pretty = true; |
| 10772 |
|
| 10773 |
if (arg_architecture >= 0) { |
| 10774 |
FOREACH_ARRAY(p, arg_filter_partitions, arg_n_filter_partitions) |
| 10775 |
*p = gpt_partition_type_override_architecture(*p, arg_architecture); |
| 10776 |
|
| 10777 |
FOREACH_ARRAY(p, arg_defer_partitions, arg_n_defer_partitions) |
| 10778 |
*p = gpt_partition_type_override_architecture(*p, arg_architecture); |
| 10779 |
} |
| 10780 |
|
| 10781 |
if (arg_append_fstab && !arg_generate_fstab) |
| 10782 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No --generate-fstab= specified for --append-fstab=%s.", append_mode_to_string(arg_append_fstab)); |
| 10783 |
|
| 10784 |
r = sd_varlink_invocation(SD_VARLINK_ALLOW_ACCEPT); |
| 10785 |
if (r < 0) |
| 10786 |
return log_error_errno(r, "Failed to check if invoked in Varlink mode: %m"); |
| 10787 |
if (r > 0) { |
| 10788 |
arg_varlink = true; |
| 10789 |
arg_pager_flags |= PAGER_DISABLE; |
| 10790 |
} |
| 10791 |
|
| 10792 |
return 1; |
| 10793 |
} |
| 10794 |
|
| 10795 |
static int parse_proc_cmdline_factory_reset(void) { |
| 10796 |
if (arg_factory_reset >= 0) /* Never override what is specified on the process command line */ |
| 10797 |
return 0; |
| 10798 |
|
| 10799 |
if (!in_initrd()) /* Never honour kernel command line factory reset request outside of the initrd */ |
| 10800 |
return 0; |
| 10801 |
|
| 10802 |
FactoryResetMode f = factory_reset_mode(); |
| 10803 |
if (f < 0) |
| 10804 |
return log_error_errno(f, "Failed to determine factory reset status: %m"); |
| 10805 |
if (f != FACTORY_RESET_UNSPECIFIED) { |
| 10806 |
arg_factory_reset = f == FACTORY_RESET_ON; |
| 10807 |
|
| 10808 |
if (arg_factory_reset) |
| 10809 |
log_notice("Honouring factory reset requested via kernel command line or EFI variable."); |
| 10810 |
} |
| 10811 |
|
| 10812 |
return 0; |
| 10813 |
} |
| 10814 |
|
| 10815 |
static int parse_efi_variable_factory_reset(void) { |
| 10816 |
_cleanup_free_ char *value = NULL; |
| 10817 |
int r; |
| 10818 |
|
| 10819 |
/* NB: This is legacy, people should move to the newer FactoryResetRequest variable! */ |
| 10820 |
|
| 10821 |
// FIXME: Remove this in v260 |
| 10822 |
|
| 10823 |
if (arg_factory_reset >= 0) /* Never override what is specified on the process command line */ |
| 10824 |
return 0; |
| 10825 |
|
| 10826 |
if (!in_initrd()) /* Never honour EFI variable factory reset request outside of the initrd */ |
| 10827 |
return 0; |
| 10828 |
|
| 10829 |
r = efi_get_variable_string(EFI_SYSTEMD_VARIABLE_STR("FactoryReset"), &value); |
| 10830 |
if (r == -ENOENT || ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 10831 |
return 0; |
| 10832 |
if (r < 0) |
| 10833 |
return log_error_errno(r, "Failed to read EFI variable FactoryReset: %m"); |
| 10834 |
|
| 10835 |
log_warning("Warning, EFI variable FactoryReset is in use, please migrate to use FactoryResetRequest instead, support will be removed in v260!"); |
| 10836 |
|
| 10837 |
r = parse_boolean(value); |
| 10838 |
if (r < 0) |
| 10839 |
return log_error_errno(r, "Failed to parse EFI variable FactoryReset: %m"); |
| 10840 |
|
| 10841 |
arg_factory_reset = r; |
| 10842 |
if (r) |
| 10843 |
log_notice("Factory reset requested via EFI variable FactoryReset."); |
| 10844 |
|
| 10845 |
return 0; |
| 10846 |
} |
| 10847 |
|
| 10848 |
static int remove_efi_variable_factory_reset(void) { |
| 10849 |
int r; |
| 10850 |
|
| 10851 |
// FIXME: Remove this in v260, see above |
| 10852 |
|
| 10853 |
r = efi_set_variable(EFI_SYSTEMD_VARIABLE_STR("FactoryReset"), NULL, 0); |
| 10854 |
if (r == -ENOENT || ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 10855 |
return 0; |
| 10856 |
if (r < 0) |
| 10857 |
return log_error_errno(r, "Failed to remove EFI variable FactoryReset: %m"); |
| 10858 |
|
| 10859 |
log_info("Successfully unset EFI variable FactoryReset."); |
| 10860 |
return 0; |
| 10861 |
} |
| 10862 |
|
| 10863 |
static int acquire_root_devno( |
| 10864 |
const char *p, |
| 10865 |
const char *root, |
| 10866 |
int mode, |
| 10867 |
char **ret, |
| 10868 |
int *ret_fd) { |
| 10869 |
|
| 10870 |
_cleanup_free_ char *found_path = NULL, *node = NULL; |
| 10871 |
dev_t devno, fd_devno = MODE_INVALID; |
| 10872 |
_cleanup_close_ int fd = -EBADF; |
| 10873 |
struct stat st; |
| 10874 |
int r; |
| 10875 |
|
| 10876 |
assert(p); |
| 10877 |
assert(ret); |
| 10878 |
assert(ret_fd); |
| 10879 |
|
| 10880 |
fd = chase_and_open(p, root, CHASE_PREFIX_ROOT, (mode & ~O_ACCMODE_STRICT) | O_RDONLY, &found_path); |
| 10881 |
if (fd < 0) |
| 10882 |
return fd; |
| 10883 |
|
| 10884 |
if (fstat(fd, &st) < 0) |
| 10885 |
return -errno; |
| 10886 |
|
| 10887 |
if ((S_ISREG(st.st_mode) || S_ISBLK(st.st_mode)) && (mode & O_ACCMODE_STRICT) != O_RDONLY) { |
| 10888 |
_cleanup_close_ int new_fd = fd_reopen(fd, mode); |
| 10889 |
if (new_fd < 0) |
| 10890 |
return new_fd; |
| 10891 |
|
| 10892 |
close_and_replace(fd, new_fd); |
| 10893 |
} |
| 10894 |
|
| 10895 |
if (S_ISREG(st.st_mode)) { |
| 10896 |
*ret = TAKE_PTR(found_path); |
| 10897 |
*ret_fd = TAKE_FD(fd); |
| 10898 |
return 0; |
| 10899 |
} |
| 10900 |
|
| 10901 |
if (S_ISBLK(st.st_mode)) { |
| 10902 |
/* Refuse referencing explicit block devices if a root dir is specified, after all we should |
| 10903 |
* not be able to leave the image the root path constrains us to. */ |
| 10904 |
if (root) |
| 10905 |
return -EPERM; |
| 10906 |
|
| 10907 |
fd_devno = devno = st.st_rdev; |
| 10908 |
} else if (S_ISDIR(st.st_mode)) { |
| 10909 |
|
| 10910 |
devno = st.st_dev; |
| 10911 |
if (major(devno) == 0) { |
| 10912 |
r = btrfs_get_block_device_fd(fd, &devno); |
| 10913 |
if (r == -ENOTTY) /* not btrfs */ |
| 10914 |
return -ENODEV; |
| 10915 |
if (r < 0) |
| 10916 |
return r; |
| 10917 |
} |
| 10918 |
} else |
| 10919 |
return -ENOTBLK; |
| 10920 |
|
| 10921 |
/* From dm-crypt to backing partition */ |
| 10922 |
r = block_get_originating(devno, &devno, /* recursive= */ false); |
| 10923 |
if (r == -ENOENT) |
| 10924 |
log_debug_errno(r, "Device '%s' has no dm-crypt/dm-verity device, no need to look for underlying block device.", p); |
| 10925 |
else if (r < 0) |
| 10926 |
log_debug_errno(r, "Failed to find underlying block device for '%s', ignoring: %m", p); |
| 10927 |
|
| 10928 |
/* From partition to whole disk containing it */ |
| 10929 |
r = block_get_whole_disk(devno, &devno); |
| 10930 |
if (r < 0) |
| 10931 |
log_debug_errno(r, "Failed to find whole disk block device for '%s', ignoring: %m", p); |
| 10932 |
|
| 10933 |
r = devname_from_devnum(S_IFBLK, devno, &node); |
| 10934 |
if (r < 0) |
| 10935 |
return log_debug_errno(r, "Failed to determine canonical path for '%s': %m", p); |
| 10936 |
|
| 10937 |
/* Only if we still look at the same block device we can reuse the fd. Otherwise return an |
| 10938 |
* invalidated fd. */ |
| 10939 |
if (fd_devno != MODE_INVALID && fd_devno == devno) { |
| 10940 |
/* Tell udev not to interfere while we are processing the device */ |
| 10941 |
if (flock(fd, arg_dry_run ? LOCK_SH : LOCK_EX) < 0) |
| 10942 |
return log_error_errno(errno, "Failed to lock device '%s': %m", node); |
| 10943 |
|
| 10944 |
*ret_fd = TAKE_FD(fd); |
| 10945 |
} else |
| 10946 |
*ret_fd = -EBADF; |
| 10947 |
|
| 10948 |
*ret = TAKE_PTR(node); |
| 10949 |
return 0; |
| 10950 |
} |
| 10951 |
|
| 10952 |
static int find_root(Context *context) { |
| 10953 |
_cleanup_free_ char *device = NULL; |
| 10954 |
int r, open_flags = O_CLOEXEC|context_open_mode(context); |
| 10955 |
|
| 10956 |
assert(context); |
| 10957 |
|
| 10958 |
if (arg_node_none) |
| 10959 |
return 0; |
| 10960 |
|
| 10961 |
if (arg_node) { |
| 10962 |
if (context->empty == EMPTY_CREATE) { |
| 10963 |
_cleanup_close_ int fd = -EBADF; |
| 10964 |
_cleanup_free_ char *s = NULL; |
| 10965 |
|
| 10966 |
s = strdup(arg_node); |
| 10967 |
if (!s) |
| 10968 |
return log_oom(); |
| 10969 |
|
| 10970 |
fd = xopenat_full(AT_FDCWD, arg_node, open_flags|O_CREAT|O_EXCL|O_NOFOLLOW, XO_NOCOW, 0666); |
| 10971 |
if (fd < 0) |
| 10972 |
return log_error_errno(fd, "Failed to create '%s': %m", arg_node); |
| 10973 |
|
| 10974 |
context->node = TAKE_PTR(s); |
| 10975 |
context->node_is_our_file = true; |
| 10976 |
context->backing_fd = TAKE_FD(fd); |
| 10977 |
return 0; |
| 10978 |
} |
| 10979 |
|
| 10980 |
/* Note that we don't specify a root argument here: if the user explicitly configured a node |
| 10981 |
* we'll take it relative to the host, not the image */ |
| 10982 |
r = acquire_root_devno(arg_node, NULL, open_flags, &context->node, &context->backing_fd); |
| 10983 |
if (r == -EUCLEAN) |
| 10984 |
return btrfs_log_dev_root(LOG_ERR, r, arg_node); |
| 10985 |
if (r < 0) |
| 10986 |
return log_error_errno(r, "Failed to open file or determine backing device of %s: %m", arg_node); |
| 10987 |
|
| 10988 |
return 0; |
| 10989 |
} |
| 10990 |
|
| 10991 |
assert(IN_SET(context->empty, EMPTY_REFUSE, EMPTY_ALLOW)); |
| 10992 |
|
| 10993 |
/* If the root mount has been replaced by some form of volatile file system (overlayfs), the |
| 10994 |
* original root block device node is symlinked in /run/systemd/volatile-root. Let's read that |
| 10995 |
* here. */ |
| 10996 |
r = readlink_malloc("/run/systemd/volatile-root", &device); |
| 10997 |
if (r == -ENOENT) { /* volatile-root not found */ |
| 10998 |
/* Let's search for the root device. We look for two cases here: first in /, and then in /usr. The |
| 10999 |
* latter we check for cases where / is a tmpfs and only /usr is an actual persistent block device |
| 11000 |
* (think: volatile setups) */ |
| 11001 |
|
| 11002 |
FOREACH_STRING(p, "/", "/usr") { |
| 11003 |
|
| 11004 |
r = acquire_root_devno(p, arg_root, open_flags|O_DIRECTORY, &context->node, |
| 11005 |
&context->backing_fd); |
| 11006 |
if (r < 0) { |
| 11007 |
if (r == -EUCLEAN) |
| 11008 |
return btrfs_log_dev_root(LOG_ERR, r, p); |
| 11009 |
if (r != -ENODEV) |
| 11010 |
return log_error_errno(r, "Failed to determine backing device of %s%s: %m", strempty(arg_root), p); |
| 11011 |
} else |
| 11012 |
return 0; |
| 11013 |
} |
| 11014 |
} else if (r < 0) |
| 11015 |
return log_error_errno(r, "Failed to read symlink /run/systemd/volatile-root: %m"); |
| 11016 |
else { |
| 11017 |
r = acquire_root_devno(device, /* root= */ NULL, open_flags, &context->node, &context->backing_fd); |
| 11018 |
if (r == -EUCLEAN) |
| 11019 |
return btrfs_log_dev_root(LOG_ERR, r, device); |
| 11020 |
if (r < 0) |
| 11021 |
return log_error_errno(r, "Failed to open file or determine backing device of %s: %m", device); |
| 11022 |
|
| 11023 |
return 0; |
| 11024 |
} |
| 11025 |
|
| 11026 |
return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "Failed to discover root block device."); |
| 11027 |
} |
| 11028 |
|
| 11029 |
static int resize_pt(int fd, uint64_t sector_size) { |
| 11030 |
_cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL; |
| 11031 |
int r; |
| 11032 |
|
| 11033 |
/* After resizing the backing file we need to resize the partition table itself too, so that it takes |
| 11034 |
* possession of the enlarged backing file. For this it suffices to open the device with libfdisk and |
| 11035 |
* immediately write it again, with no changes. */ |
| 11036 |
|
| 11037 |
r = fdisk_new_context_at(fd, /* path= */ NULL, /* read_only= */ false, sector_size, &c); |
| 11038 |
if (r < 0) |
| 11039 |
return log_error_errno(r, "Failed to open device '%s': %m", FORMAT_PROC_FD_PATH(fd)); |
| 11040 |
|
| 11041 |
r = sym_fdisk_has_label(c); |
| 11042 |
if (r < 0) |
| 11043 |
return log_error_errno(r, "Failed to determine whether disk '%s' has a disk label: %m", FORMAT_PROC_FD_PATH(fd)); |
| 11044 |
if (r == 0) { |
| 11045 |
log_debug("Not resizing partition table, as there currently is none."); |
| 11046 |
return 0; |
| 11047 |
} |
| 11048 |
|
| 11049 |
r = sym_fdisk_write_disklabel(c); |
| 11050 |
if (r < 0) |
| 11051 |
return log_error_errno(r, "Failed to write resized partition table: %m"); |
| 11052 |
|
| 11053 |
log_info("Resized partition table."); |
| 11054 |
return 1; |
| 11055 |
} |
| 11056 |
|
| 11057 |
static int resize_backing_fd( |
| 11058 |
const char *node, /* The primary way we access the disk image to operate on */ |
| 11059 |
int *fd, /* An O_RDONLY fd referring to that inode */ |
| 11060 |
const char *backing_file, /* If the above refers to a loopback device, the backing regular file for that, which we can grow */ |
| 11061 |
LoopDevice *loop_device, |
| 11062 |
uint64_t sector_size) { |
| 11063 |
|
| 11064 |
_cleanup_close_ int writable_fd = -EBADF; |
| 11065 |
uint64_t current_size; |
| 11066 |
struct stat st; |
| 11067 |
int r; |
| 11068 |
|
| 11069 |
assert(node); |
| 11070 |
assert(fd); |
| 11071 |
|
| 11072 |
if (arg_size == UINT64_MAX) /* Nothing to do */ |
| 11073 |
return 0; |
| 11074 |
|
| 11075 |
if (*fd < 0) { |
| 11076 |
/* Open the file if we haven't opened it yet. Note that we open it read-only here, just to |
| 11077 |
* keep a reference to the file we can pass around. */ |
| 11078 |
*fd = open(node, O_RDONLY|O_CLOEXEC); |
| 11079 |
if (*fd < 0) |
| 11080 |
return log_error_errno(errno, "Failed to open '%s' in order to adjust size: %m", node); |
| 11081 |
} |
| 11082 |
|
| 11083 |
if (fstat(*fd, &st) < 0) |
| 11084 |
return log_error_errno(errno, "Failed to stat '%s': %m", node); |
| 11085 |
|
| 11086 |
if (S_ISBLK(st.st_mode)) { |
| 11087 |
if (!backing_file) |
| 11088 |
return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Cannot resize block device '%s'.", node); |
| 11089 |
|
| 11090 |
assert(loop_device); |
| 11091 |
|
| 11092 |
r = blockdev_get_device_size(*fd, ¤t_size); |
| 11093 |
if (r < 0) |
| 11094 |
return log_error_errno(r, "Failed to determine size of block device %s: %m", node); |
| 11095 |
} else { |
| 11096 |
r = stat_verify_regular(&st); |
| 11097 |
if (r < 0) |
| 11098 |
return log_error_errno(r, "Specified path '%s' is not a regular file or loopback block device, cannot resize: %m", node); |
| 11099 |
|
| 11100 |
assert(!backing_file); |
| 11101 |
assert(!loop_device); |
| 11102 |
current_size = st.st_size; |
| 11103 |
} |
| 11104 |
|
| 11105 |
if (current_size >= arg_size) { |
| 11106 |
log_info("File '%s' already is of requested size or larger, not growing. (%s >= %s)", |
| 11107 |
node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size)); |
| 11108 |
return 0; |
| 11109 |
} |
| 11110 |
|
| 11111 |
if (S_ISBLK(st.st_mode)) { |
| 11112 |
assert(backing_file); |
| 11113 |
|
| 11114 |
/* This is a loopback device. We can't really grow those directly, but we can grow the |
| 11115 |
* backing file, hence let's do that. */ |
| 11116 |
|
| 11117 |
writable_fd = open(backing_file, O_WRONLY|O_CLOEXEC|O_NONBLOCK); |
| 11118 |
if (writable_fd < 0) |
| 11119 |
return log_error_errno(errno, "Failed to open backing file '%s': %m", backing_file); |
| 11120 |
|
| 11121 |
if (fstat(writable_fd, &st) < 0) |
| 11122 |
return log_error_errno(errno, "Failed to stat() backing file '%s': %m", backing_file); |
| 11123 |
|
| 11124 |
r = stat_verify_regular(&st); |
| 11125 |
if (r < 0) |
| 11126 |
return log_error_errno(r, "Backing file '%s' of block device is not a regular file: %m", backing_file); |
| 11127 |
|
| 11128 |
if ((uint64_t) st.st_size != current_size) |
| 11129 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 11130 |
"Size of backing file '%s' of loopback block device '%s' don't match, refusing.", |
| 11131 |
node, backing_file); |
| 11132 |
} else { |
| 11133 |
assert(S_ISREG(st.st_mode)); |
| 11134 |
assert(!backing_file); |
| 11135 |
|
| 11136 |
/* The file descriptor is read-only. In order to grow the file we need to have a writable fd. We |
| 11137 |
* reopen the file for that temporarily. We keep the writable fd only open for this operation though, |
| 11138 |
* as fdisk can't accept it anyway. */ |
| 11139 |
|
| 11140 |
writable_fd = fd_reopen(*fd, O_WRONLY|O_CLOEXEC); |
| 11141 |
if (writable_fd < 0) |
| 11142 |
return log_error_errno(writable_fd, "Failed to reopen backing file '%s' writable: %m", node); |
| 11143 |
} |
| 11144 |
|
| 11145 |
if (!arg_discard) { |
| 11146 |
if (fallocate(writable_fd, 0, 0, arg_size) < 0) { |
| 11147 |
if (!ERRNO_IS_NOT_SUPPORTED(errno)) |
| 11148 |
return log_error_errno(errno, "Failed to grow '%s' from %s to %s by allocation: %m", |
| 11149 |
node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size)); |
| 11150 |
|
| 11151 |
/* Fallback to truncation, if fallocate() is not supported. */ |
| 11152 |
log_debug("Backing file system does not support fallocate(), falling back to ftruncate()."); |
| 11153 |
} else { |
| 11154 |
if (current_size == 0) /* Likely regular file just created by us */ |
| 11155 |
log_info("Allocated %s for '%s'.", FORMAT_BYTES(arg_size), node); |
| 11156 |
else |
| 11157 |
log_info("File '%s' grown from %s to %s by allocation.", |
| 11158 |
node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size)); |
| 11159 |
|
| 11160 |
goto done; |
| 11161 |
} |
| 11162 |
} |
| 11163 |
|
| 11164 |
if (ftruncate(writable_fd, arg_size) < 0) |
| 11165 |
return log_error_errno(errno, "Failed to grow '%s' from %s to %s by truncation: %m", |
| 11166 |
node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size)); |
| 11167 |
|
| 11168 |
if (current_size == 0) /* Likely regular file just created by us */ |
| 11169 |
log_info("Sized '%s' to %s.", node, FORMAT_BYTES(arg_size)); |
| 11170 |
else |
| 11171 |
log_info("File '%s' grown from %s to %s by truncation.", |
| 11172 |
node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size)); |
| 11173 |
|
| 11174 |
done: |
| 11175 |
r = resize_pt(writable_fd, sector_size); |
| 11176 |
if (r < 0) |
| 11177 |
return r; |
| 11178 |
|
| 11179 |
if (loop_device) { |
| 11180 |
r = loop_device_refresh_size(loop_device, UINT64_MAX, arg_size); |
| 11181 |
if (r < 0) |
| 11182 |
return log_error_errno(r, "Failed to update loop device size: %m"); |
| 11183 |
} |
| 11184 |
|
| 11185 |
return 1; |
| 11186 |
} |
| 11187 |
|
| 11188 |
static int determine_auto_size( |
| 11189 |
Context *c, |
| 11190 |
int level, |
| 11191 |
uint64_t *ret_current_size, |
| 11192 |
uint64_t *ret_foreign_size, |
| 11193 |
uint64_t *ret_minimal_size) { |
| 11194 |
|
| 11195 |
uint64_t current_size, foreign_size, minimal_size; |
| 11196 |
|
| 11197 |
assert(c); |
| 11198 |
|
| 11199 |
minimal_size = round_up_size(GPT_METADATA_SIZE, 4096); |
| 11200 |
|
| 11201 |
if (c->from_scratch) |
| 11202 |
current_size = 0; |
| 11203 |
else |
| 11204 |
current_size = round_up_size(GPT_METADATA_SIZE, 4096); |
| 11205 |
|
| 11206 |
foreign_size = 0; |
| 11207 |
|
| 11208 |
LIST_FOREACH(partitions, p, c->partitions) { |
| 11209 |
uint64_t m; |
| 11210 |
|
| 11211 |
if (PARTITION_IS_FOREIGN(p)) |
| 11212 |
foreign_size += p->current_size; |
| 11213 |
|
| 11214 |
if (PARTITION_EXISTS(p)) |
| 11215 |
current_size += p->current_size; |
| 11216 |
|
| 11217 |
if (p->dropped || PARTITION_SUPPRESSED(p)) |
| 11218 |
continue; |
| 11219 |
|
| 11220 |
m = partition_min_size_with_padding(c, p); |
| 11221 |
if (m > UINT64_MAX - minimal_size) |
| 11222 |
return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Image would grow too large, refusing."); |
| 11223 |
|
| 11224 |
minimal_size += m; |
| 11225 |
} |
| 11226 |
|
| 11227 |
if (current_size != 0) |
| 11228 |
/* Image already allocated? Then show its size. */ |
| 11229 |
log_full(level, |
| 11230 |
"Automatically determined minimal disk image size as %s, current block device/image size is %s.", |
| 11231 |
FORMAT_BYTES(minimal_size), FORMAT_BYTES(current_size)); |
| 11232 |
else |
| 11233 |
/* If the image is being created right now, then it has no previous size, suppress any comment about it hence. */ |
| 11234 |
log_full(level, |
| 11235 |
"Automatically determined minimal disk image size as %s.", |
| 11236 |
FORMAT_BYTES(minimal_size)); |
| 11237 |
|
| 11238 |
if (ret_current_size) |
| 11239 |
*ret_current_size = current_size; |
| 11240 |
if (ret_foreign_size) |
| 11241 |
*ret_foreign_size = foreign_size; |
| 11242 |
if (ret_minimal_size) |
| 11243 |
*ret_minimal_size = minimal_size; |
| 11244 |
|
| 11245 |
return 0; |
| 11246 |
} |
| 11247 |
|
| 11248 |
static int context_ponder(Context *context) { |
| 11249 |
int r; |
| 11250 |
|
| 11251 |
assert(context); |
| 11252 |
|
| 11253 |
(void) context_notify(context, PROGRESS_PLACING, /* object= */ NULL, UINT_MAX); |
| 11254 |
|
| 11255 |
/* First try to fit new partitions in, dropping by priority until it fits */ |
| 11256 |
for (;;) { |
| 11257 |
uint64_t largest_free_area; |
| 11258 |
|
| 11259 |
if (context_allocate_partitions(context, &largest_free_area)) |
| 11260 |
break; /* Success! */ |
| 11261 |
|
| 11262 |
if (context_unmerge_and_allocate_partitions(context)) |
| 11263 |
break; /* We had to un-suppress a supplement or few, but still success! */ |
| 11264 |
|
| 11265 |
if (context_drop_or_foreignize_one_priority(context)) |
| 11266 |
continue; /* Still no luck. Let's drop a priority and try again. */ |
| 11267 |
|
| 11268 |
/* No more priorities left to drop. This configuration just doesn't fit on this disk... */ |
| 11269 |
return log_error_errno(SYNTHETIC_ERRNO(ENOSPC), |
| 11270 |
"Can't fit requested partitions into available free space (%s), refusing.", |
| 11271 |
FORMAT_BYTES(largest_free_area)); |
| 11272 |
} |
| 11273 |
|
| 11274 |
LIST_FOREACH(partitions, p, context->partitions) { |
| 11275 |
if (!p->supplement_for) |
| 11276 |
continue; |
| 11277 |
|
| 11278 |
if (PARTITION_SUPPRESSED(p)) { |
| 11279 |
assert(!p->allocated_to_area); |
| 11280 |
p->dropped = true; |
| 11281 |
|
| 11282 |
log_debug("Partition %s can be merged into %s, suppressing supplement.", |
| 11283 |
p->definition_path, p->supplement_for->definition_path); |
| 11284 |
} else if (PARTITION_EXISTS(p)) |
| 11285 |
log_info("Partition %s already exists on disk, using supplement verbatim.", |
| 11286 |
p->definition_path); |
| 11287 |
else |
| 11288 |
log_info("Couldn't allocate partitions with %s merged into %s, using supplement verbatim.", |
| 11289 |
p->definition_path, p->supplement_for->definition_path); |
| 11290 |
} |
| 11291 |
|
| 11292 |
/* Now assign free space according to the weight logic */ |
| 11293 |
r = context_grow_partitions(context); |
| 11294 |
if (r < 0) |
| 11295 |
return r; |
| 11296 |
|
| 11297 |
/* Now calculate where each new partition gets placed */ |
| 11298 |
context_place_partitions(context); |
| 11299 |
|
| 11300 |
return 0; |
| 11301 |
} |
| 11302 |
|
| 11303 |
static int vl_method_list_candidate_devices( |
| 11304 |
sd_varlink *link, |
| 11305 |
sd_json_variant *parameters, |
| 11306 |
sd_varlink_method_flags_t flags, |
| 11307 |
void *userdata) { |
| 11308 |
|
| 11309 |
struct { |
| 11310 |
bool ignore_root; |
| 11311 |
bool ignore_empty; |
| 11312 |
} p = {}; |
| 11313 |
|
| 11314 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 11315 |
{ "ignoreRoot", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, voffsetof(p, ignore_root), 0 }, |
| 11316 |
{ "ignoreEmpty", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, voffsetof(p, ignore_empty), 0 }, |
| 11317 |
{} |
| 11318 |
}; |
| 11319 |
|
| 11320 |
int r; |
| 11321 |
|
| 11322 |
assert(link); |
| 11323 |
assert(FLAGS_SET(flags, SD_VARLINK_METHOD_MORE)); |
| 11324 |
|
| 11325 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 11326 |
if (r != 0) |
| 11327 |
return r; |
| 11328 |
|
| 11329 |
BlockDevice *l = NULL; |
| 11330 |
size_t n = 0; |
| 11331 |
CLEANUP_ARRAY(l, n, block_device_array_free); |
| 11332 |
|
| 11333 |
r = blockdev_list( |
| 11334 |
BLOCKDEV_LIST_SHOW_SYMLINKS| |
| 11335 |
BLOCKDEV_LIST_REQUIRE_PARTITION_SCANNING| |
| 11336 |
BLOCKDEV_LIST_IGNORE_ZRAM| |
| 11337 |
BLOCKDEV_LIST_METADATA| |
| 11338 |
BLOCKDEV_LIST_IGNORE_READ_ONLY| |
| 11339 |
(p.ignore_empty ? BLOCKDEV_LIST_IGNORE_EMPTY : 0)| |
| 11340 |
(p.ignore_root ? BLOCKDEV_LIST_IGNORE_ROOT : 0), |
| 11341 |
&l, |
| 11342 |
&n); |
| 11343 |
if (r < 0) |
| 11344 |
return r; |
| 11345 |
|
| 11346 |
r = sd_varlink_set_sentinel(link, "io.systemd.Repart.NoCandidateDevices"); |
| 11347 |
if (r < 0) |
| 11348 |
return r; |
| 11349 |
|
| 11350 |
FOREACH_ARRAY(d, l, n) { |
| 11351 |
r = sd_varlink_replybo(link, |
| 11352 |
SD_JSON_BUILD_PAIR_STRING("node", d->node), |
| 11353 |
JSON_BUILD_PAIR_STRV_NON_EMPTY("symlinks", d->symlinks), |
| 11354 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("diskseq", d->diskseq, UINT64_MAX), |
| 11355 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("sizeBytes", d->size, UINT64_MAX), |
| 11356 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("model", d->model), |
| 11357 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("vendor", d->vendor), |
| 11358 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("subsystem", d->subsystem)); |
| 11359 |
if (r < 0) |
| 11360 |
return r; |
| 11361 |
} |
| 11362 |
|
| 11363 |
return 0; |
| 11364 |
} |
| 11365 |
|
| 11366 |
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_empty_mode, EmptyMode, empty_mode_from_string); |
| 11367 |
|
| 11368 |
typedef struct RunParameters { |
| 11369 |
char *node; |
| 11370 |
EmptyMode empty; |
| 11371 |
bool dry_run; |
| 11372 |
sd_id128_t seed; |
| 11373 |
char **definitions; |
| 11374 |
bool defer_partitions_empty; |
| 11375 |
bool defer_partitions_factory_reset; |
| 11376 |
} RunParameters; |
| 11377 |
|
| 11378 |
static void run_parameters_done(RunParameters *p) { |
| 11379 |
assert(p); |
| 11380 |
|
| 11381 |
p->node = mfree(p->node); |
| 11382 |
p->definitions = strv_free(p->definitions); |
| 11383 |
} |
| 11384 |
|
| 11385 |
static int vl_method_run( |
| 11386 |
sd_varlink *link, |
| 11387 |
sd_json_variant *parameters, |
| 11388 |
sd_varlink_method_flags_t flags, |
| 11389 |
void *userdata) { |
| 11390 |
|
| 11391 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 11392 |
{ "node", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(RunParameters, node), SD_JSON_NULLABLE }, |
| 11393 |
{ "empty", SD_JSON_VARIANT_STRING, json_dispatch_empty_mode, offsetof(RunParameters, empty), SD_JSON_MANDATORY }, |
| 11394 |
{ "seed", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(RunParameters, seed), SD_JSON_NULLABLE }, |
| 11395 |
{ "dryRun", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(RunParameters, dry_run), SD_JSON_MANDATORY }, |
| 11396 |
{ "definitions", SD_JSON_VARIANT_ARRAY, json_dispatch_strv_path, offsetof(RunParameters, definitions), SD_JSON_STRICT }, |
| 11397 |
{ "deferPartitionsEmpty", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(RunParameters, defer_partitions_empty), SD_JSON_NULLABLE }, |
| 11398 |
{ "deferPartitionsFactoryReset", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(RunParameters, defer_partitions_factory_reset), SD_JSON_NULLABLE }, |
| 11399 |
{} |
| 11400 |
}; |
| 11401 |
|
| 11402 |
int r; |
| 11403 |
|
| 11404 |
assert(link); |
| 11405 |
|
| 11406 |
_cleanup_(run_parameters_done) RunParameters p = { |
| 11407 |
.empty = _EMPTY_MODE_INVALID, |
| 11408 |
.dry_run = true, |
| 11409 |
}; |
| 11410 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 11411 |
if (r != 0) |
| 11412 |
return r; |
| 11413 |
|
| 11414 |
/* If no device node is specified, this is a dry run. Refuse if the caller claims otherwise. */ |
| 11415 |
if (!p.node && !p.dry_run) |
| 11416 |
return sd_varlink_error_invalid_parameter_name(link, "dryRun"); |
| 11417 |
|
| 11418 |
_cleanup_(context_freep) Context* context = NULL; |
| 11419 |
context = context_new( |
| 11420 |
p.definitions, |
| 11421 |
p.empty, |
| 11422 |
p.dry_run, |
| 11423 |
p.seed); |
| 11424 |
if (!context) |
| 11425 |
return log_oom(); |
| 11426 |
|
| 11427 |
context->defer_partitions_empty = p.defer_partitions_empty; |
| 11428 |
context->defer_partitions_factory_reset = p.defer_partitions_factory_reset; |
| 11429 |
|
| 11430 |
if (FLAGS_SET(flags, SD_VARLINK_METHOD_MORE)) |
| 11431 |
context->link = sd_varlink_ref(link); |
| 11432 |
|
| 11433 |
r = context_read_seed(context, arg_root); |
| 11434 |
if (r < 0) |
| 11435 |
return r; |
| 11436 |
|
| 11437 |
r = context_read_definitions(context); |
| 11438 |
if (r < 0) |
| 11439 |
return r; |
| 11440 |
|
| 11441 |
if (p.node) { |
| 11442 |
r = acquire_root_devno(p.node, NULL, O_CLOEXEC|context_open_mode(context), &context->node, &context->backing_fd); |
| 11443 |
if (r < 0) |
| 11444 |
return log_error_errno(r, "Failed to open file or determine backing device of %s: %m", p.node); |
| 11445 |
|
| 11446 |
r = context_load_partition_table(context); |
| 11447 |
if (r == -EHWPOISON) |
| 11448 |
return sd_varlink_error(link, "io.systemd.Repart.ConflictingDiskLabelPresent", NULL); |
| 11449 |
} else |
| 11450 |
r = context_load_fallback_metrics(context); |
| 11451 |
if (r < 0) |
| 11452 |
return r; |
| 11453 |
context->from_scratch = r > 0; /* Starting from scratch */ |
| 11454 |
|
| 11455 |
r = context_open_copy_block_paths(context, (dev_t) -1); |
| 11456 |
if (r < 0) |
| 11457 |
return r; |
| 11458 |
|
| 11459 |
r = context_open_btrfs_filesystems(context); |
| 11460 |
if (r < 0) |
| 11461 |
return r; |
| 11462 |
|
| 11463 |
r = context_acquire_partition_uuids_and_labels(context); |
| 11464 |
if (r < 0) |
| 11465 |
return r; |
| 11466 |
|
| 11467 |
r = context_update_verity_size(context); |
| 11468 |
if (r < 0) |
| 11469 |
return r; |
| 11470 |
|
| 11471 |
r = context_minimize(context); |
| 11472 |
if (r < 0) |
| 11473 |
return r; |
| 11474 |
|
| 11475 |
/* If we have no node, just sum up how much space we need */ |
| 11476 |
if (!context->node) { |
| 11477 |
/* Check if space issue is caused by the whole disk being too small */ |
| 11478 |
uint64_t minimal_size; |
| 11479 |
r = determine_auto_size(context, LOG_DEBUG, /* ret_current_size= */ NULL, /* ret_foreign_size= */ NULL, &minimal_size); |
| 11480 |
if (r < 0) |
| 11481 |
return r; |
| 11482 |
|
| 11483 |
return sd_varlink_replybo( |
| 11484 |
link, |
| 11485 |
SD_JSON_BUILD_PAIR_UNSIGNED("minimalSizeBytes", minimal_size)); |
| 11486 |
} |
| 11487 |
|
| 11488 |
r = context_verify_eltorito_overlap(context); |
| 11489 |
if (r < 0) |
| 11490 |
return r; |
| 11491 |
|
| 11492 |
r = context_ponder(context); |
| 11493 |
if (r == -ENOSPC) { |
| 11494 |
uint64_t current_size, foreign_size, minimal_size; |
| 11495 |
|
| 11496 |
r = determine_auto_size(context, LOG_DEBUG, ¤t_size, &foreign_size, &minimal_size); |
| 11497 |
if (r < 0) |
| 11498 |
return r; |
| 11499 |
|
| 11500 |
uint64_t needed_size = LESS_BY(minimal_size, foreign_size); |
| 11501 |
|
| 11502 |
/* Check if space issue is caused by the whole disk being too small */ |
| 11503 |
if (needed_size > context->total) |
| 11504 |
return sd_varlink_errorbo( |
| 11505 |
link, |
| 11506 |
"io.systemd.Repart.DiskTooSmall", |
| 11507 |
SD_JSON_BUILD_PAIR_UNSIGNED("currentSizeBytes", current_size), |
| 11508 |
SD_JSON_BUILD_PAIR_UNSIGNED("minimalSizeBytes", minimal_size)); |
| 11509 |
|
| 11510 |
/* Or if the disk would fit, but theres's not enough unallocated space */ |
| 11511 |
uint64_t need_free = LESS_BY(minimal_size, current_size); |
| 11512 |
return sd_varlink_errorbo( |
| 11513 |
link, |
| 11514 |
"io.systemd.Repart.InsufficientFreeSpace", |
| 11515 |
SD_JSON_BUILD_PAIR_UNSIGNED("currentSizeBytes", current_size), |
| 11516 |
JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("needFreeBytes", need_free), |
| 11517 |
SD_JSON_BUILD_PAIR_UNSIGNED("minimalSizeBytes", minimal_size)); |
| 11518 |
} |
| 11519 |
if (r < 0) |
| 11520 |
return r; |
| 11521 |
|
| 11522 |
if (p.dry_run) { |
| 11523 |
uint64_t current_size, minimal_size; |
| 11524 |
|
| 11525 |
/* If we are doing a dry-run, report the minimal size. */ |
| 11526 |
r = determine_auto_size(context, LOG_DEBUG, ¤t_size, /* ret_foreign_size= */ NULL, &minimal_size); |
| 11527 |
if (r < 0) |
| 11528 |
return r; |
| 11529 |
|
| 11530 |
return sd_varlink_replybo( |
| 11531 |
link, |
| 11532 |
SD_JSON_BUILD_PAIR_UNSIGNED("minimalSizeBytes", minimal_size), |
| 11533 |
SD_JSON_BUILD_PAIR_UNSIGNED("currentSizeBytes", current_size)); |
| 11534 |
} |
| 11535 |
|
| 11536 |
r = context_write_partition_table(context); |
| 11537 |
if (r < 0) |
| 11538 |
return r; |
| 11539 |
|
| 11540 |
r = context_write_eltorito(context); |
| 11541 |
if (r < 0) |
| 11542 |
return r; |
| 11543 |
|
| 11544 |
context_disarm_auto_removal(context); |
| 11545 |
|
| 11546 |
return sd_varlink_reply(link, NULL); |
| 11547 |
} |
| 11548 |
|
| 11549 |
static int vl_server(void) { |
| 11550 |
_cleanup_(sd_varlink_server_unrefp) sd_varlink_server *varlink_server = NULL; |
| 11551 |
int r; |
| 11552 |
|
| 11553 |
/* Invocation as Varlink service */ |
| 11554 |
|
| 11555 |
r = varlink_server_new(&varlink_server, |
| 11556 |
SD_VARLINK_SERVER_ROOT_ONLY | SD_VARLINK_SERVER_MYSELF_ONLY, |
| 11557 |
/* userdata= */ NULL); |
| 11558 |
if (r < 0) |
| 11559 |
return log_error_errno(r, "Failed to allocate Varlink server: %m"); |
| 11560 |
|
| 11561 |
r = sd_varlink_server_add_interface(varlink_server, &vl_interface_io_systemd_Repart); |
| 11562 |
if (r < 0) |
| 11563 |
return log_error_errno(r, "Failed to add Varlink interface: %m"); |
| 11564 |
|
| 11565 |
r = sd_varlink_server_bind_method_many( |
| 11566 |
varlink_server, |
| 11567 |
"io.systemd.Repart.ListCandidateDevices", vl_method_list_candidate_devices, |
| 11568 |
"io.systemd.Repart.Run", vl_method_run); |
| 11569 |
if (r < 0) |
| 11570 |
return log_error_errno(r, "Failed to bind Varlink methods: %m"); |
| 11571 |
|
| 11572 |
r = sd_varlink_server_loop_auto(varlink_server); |
| 11573 |
if (r < 0) |
| 11574 |
return log_error_errno(r, "Failed to run Varlink event loop: %m"); |
| 11575 |
|
| 11576 |
return 0; |
| 11577 |
} |
| 11578 |
|
| 11579 |
static int run(int argc, char *argv[]) { |
| 11580 |
_cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; |
| 11581 |
_cleanup_(umount_and_freep) char *mounted_dir = NULL; |
| 11582 |
_cleanup_(context_freep) Context* context = NULL; |
| 11583 |
bool node_is_our_loop = false; |
| 11584 |
int r; |
| 11585 |
|
| 11586 |
log_setup(); |
| 11587 |
|
| 11588 |
r = parse_argv(argc, argv); |
| 11589 |
if (r <= 0) |
| 11590 |
return r; |
| 11591 |
|
| 11592 |
r = DLOPEN_FDISK(LOG_ERR, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 11593 |
if (r < 0) |
| 11594 |
return r; |
| 11595 |
|
| 11596 |
if (arg_varlink) |
| 11597 |
return vl_server(); |
| 11598 |
|
| 11599 |
r = parse_proc_cmdline_factory_reset(); |
| 11600 |
if (r < 0) |
| 11601 |
return r; |
| 11602 |
|
| 11603 |
r = parse_efi_variable_factory_reset(); |
| 11604 |
if (r < 0) |
| 11605 |
return r; |
| 11606 |
|
| 11607 |
if (arg_image) { |
| 11608 |
assert(!arg_root); |
| 11609 |
|
| 11610 |
/* Mount this strictly read-only: we shall modify the partition table, not the file |
| 11611 |
* systems */ |
| 11612 |
r = mount_image_privately_interactively( |
| 11613 |
arg_image, |
| 11614 |
arg_image_policy, |
| 11615 |
DISSECT_IMAGE_MOUNT_READ_ONLY | |
| 11616 |
(arg_node ? DISSECT_IMAGE_DEVICE_READ_ONLY : 0) | /* If a different node to make changes to is specified let's open the device in read-only mode) */ |
| 11617 |
DISSECT_IMAGE_GPT_ONLY | |
| 11618 |
DISSECT_IMAGE_RELAX_VAR_CHECK | |
| 11619 |
DISSECT_IMAGE_USR_NO_ROOT | |
| 11620 |
DISSECT_IMAGE_REQUIRE_ROOT | |
| 11621 |
DISSECT_IMAGE_ALLOW_USERSPACE_VERITY, |
| 11622 |
&mounted_dir, |
| 11623 |
/* ret_dir_fd= */ NULL, |
| 11624 |
&loop_device); |
| 11625 |
if (r < 0) |
| 11626 |
return r; |
| 11627 |
|
| 11628 |
arg_root = strdup(mounted_dir); |
| 11629 |
if (!arg_root) |
| 11630 |
return log_oom(); |
| 11631 |
|
| 11632 |
if (!arg_node && !arg_node_none) { |
| 11633 |
arg_node = strdup(loop_device->node); |
| 11634 |
if (!arg_node) |
| 11635 |
return log_oom(); |
| 11636 |
|
| 11637 |
/* Remember that the device we are about to manipulate is actually the one we |
| 11638 |
* allocated here, and thus to increase its backing file we know what to do */ |
| 11639 |
node_is_our_loop = true; |
| 11640 |
} |
| 11641 |
} |
| 11642 |
|
| 11643 |
if (!arg_copy_source && arg_root) { |
| 11644 |
/* If no explicit copy source is specified, then use --root=/--image= */ |
| 11645 |
arg_copy_source = strdup(arg_root); |
| 11646 |
if (!arg_copy_source) |
| 11647 |
return log_oom(); |
| 11648 |
} |
| 11649 |
|
| 11650 |
context = context_new( |
| 11651 |
arg_definitions, |
| 11652 |
arg_empty, |
| 11653 |
arg_dry_run, |
| 11654 |
arg_seed); |
| 11655 |
if (!context) |
| 11656 |
return log_oom(); |
| 11657 |
|
| 11658 |
r = context_load_keys(context); |
| 11659 |
if (r < 0) |
| 11660 |
return r; |
| 11661 |
|
| 11662 |
context->defer_partitions_empty = arg_defer_partitions_empty; |
| 11663 |
context->defer_partitions_factory_reset = arg_defer_partitions_factory_reset; |
| 11664 |
|
| 11665 |
r = context_read_seed(context, arg_root); |
| 11666 |
if (r < 0) |
| 11667 |
return r; |
| 11668 |
|
| 11669 |
r = context_copy_from(context); |
| 11670 |
if (r < 0) |
| 11671 |
return r; |
| 11672 |
|
| 11673 |
if (arg_make_ddi) { |
| 11674 |
_cleanup_free_ char *d = NULL, *dp = NULL; |
| 11675 |
assert(!context->definitions); |
| 11676 |
|
| 11677 |
d = strjoin(arg_make_ddi, ".repart.d/"); |
| 11678 |
if (!d) |
| 11679 |
return log_oom(); |
| 11680 |
|
| 11681 |
r = search_and_access(d, F_OK, NULL, CONF_PATHS_STRV("systemd/repart/definitions"), &dp); |
| 11682 |
if (r < 0) |
| 11683 |
return log_error_errno(r, "DDI type '%s' is not defined: %m", arg_make_ddi); |
| 11684 |
|
| 11685 |
if (strv_consume(&context->definitions, TAKE_PTR(dp)) < 0) |
| 11686 |
return log_oom(); |
| 11687 |
} else |
| 11688 |
strv_uniq(context->definitions); |
| 11689 |
|
| 11690 |
r = context_read_definitions(context); |
| 11691 |
if (r < 0) |
| 11692 |
return r; |
| 11693 |
|
| 11694 |
r = find_root(context); |
| 11695 |
if (r == -ENODEV) |
| 11696 |
return 76; /* Special return value which means "Root block device not found, so not doing |
| 11697 |
* anything". This isn't really an error when called at boot. */ |
| 11698 |
if (r < 0) |
| 11699 |
return r; |
| 11700 |
|
| 11701 |
if (context->node) { |
| 11702 |
if (arg_size != UINT64_MAX) { |
| 11703 |
r = resize_backing_fd( |
| 11704 |
context->node, |
| 11705 |
&context->backing_fd, |
| 11706 |
node_is_our_loop ? arg_image : NULL, |
| 11707 |
node_is_our_loop ? loop_device : NULL, |
| 11708 |
context->sector_size); |
| 11709 |
if (r < 0) |
| 11710 |
return r; |
| 11711 |
} |
| 11712 |
|
| 11713 |
r = context_load_partition_table(context); |
| 11714 |
if (r == -EHWPOISON) |
| 11715 |
return 77; /* Special return value which means "Not GPT, so not doing anything". This isn't |
| 11716 |
* really an error when called at boot. */ |
| 11717 |
} else |
| 11718 |
r = context_load_fallback_metrics(context); |
| 11719 |
if (r < 0) |
| 11720 |
return r; |
| 11721 |
context->from_scratch = r > 0; /* Starting from scratch */ |
| 11722 |
|
| 11723 |
r = check_mkfs(context); |
| 11724 |
if (r < 0) |
| 11725 |
return r; |
| 11726 |
|
| 11727 |
if (arg_can_factory_reset) { |
| 11728 |
r = context_can_factory_reset(context); |
| 11729 |
if (r < 0) |
| 11730 |
return r; |
| 11731 |
if (r == 0) |
| 11732 |
return EXIT_FAILURE; |
| 11733 |
|
| 11734 |
return 0; |
| 11735 |
} |
| 11736 |
|
| 11737 |
r = context_factory_reset(context); |
| 11738 |
if (r < 0) |
| 11739 |
return r; |
| 11740 |
if (r > 0) { |
| 11741 |
/* We actually did a factory reset! */ |
| 11742 |
r = remove_efi_variable_factory_reset(); |
| 11743 |
if (r < 0) |
| 11744 |
return r; |
| 11745 |
|
| 11746 |
/* Reload the reduced partition table */ |
| 11747 |
context_unload_partition_table(context); |
| 11748 |
r = context_load_partition_table(context); |
| 11749 |
if (r < 0) |
| 11750 |
return r; |
| 11751 |
} |
| 11752 |
|
| 11753 |
/* Open all files to copy blocks from now, since we want to take their size into consideration */ |
| 11754 |
r = context_open_copy_block_paths( |
| 11755 |
context, |
| 11756 |
loop_device ? loop_device->devno : /* if --image= is specified, only allow partitions on the loopback device */ |
| 11757 |
/* if --root= is specified, don't accept any block device, unless it |
| 11758 |
* was set automatically because we are in the initrd */ |
| 11759 |
arg_root && !arg_image && !arg_relax_copy_block_security ? 0 : |
| 11760 |
(dev_t) -1); /* if neither is specified, make no restrictions */ |
| 11761 |
if (r < 0) |
| 11762 |
return r; |
| 11763 |
|
| 11764 |
r = context_open_btrfs_filesystems(context); |
| 11765 |
if (r < 0) |
| 11766 |
return r; |
| 11767 |
|
| 11768 |
/* Make sure each partition has a unique UUID and unique label */ |
| 11769 |
r = context_acquire_partition_uuids_and_labels(context); |
| 11770 |
if (r < 0) |
| 11771 |
return r; |
| 11772 |
|
| 11773 |
r = context_fstab(context); |
| 11774 |
if (r < 0) |
| 11775 |
return r; |
| 11776 |
|
| 11777 |
r = context_crypttab(context, /* late= */ false); |
| 11778 |
if (r < 0) |
| 11779 |
return r; |
| 11780 |
|
| 11781 |
r = context_update_verity_size(context); |
| 11782 |
if (r < 0) |
| 11783 |
return r; |
| 11784 |
|
| 11785 |
r = context_minimize(context); |
| 11786 |
if (r < 0) |
| 11787 |
return r; |
| 11788 |
|
| 11789 |
if (arg_node_none) { |
| 11790 |
(void) determine_auto_size(context, LOG_INFO, /* ret_current_size= */ NULL, /* ret_foreign_size= */ NULL, /* ret_minimal_size= */ NULL); |
| 11791 |
return 0; |
| 11792 |
} |
| 11793 |
|
| 11794 |
if (arg_size_auto) { |
| 11795 |
r = determine_auto_size(context, LOG_INFO, /* ret_current_size= */ NULL, /* ret_foreign_size= */ NULL, &arg_size); |
| 11796 |
if (r < 0) |
| 11797 |
return r; |
| 11798 |
|
| 11799 |
/* Flush out everything again, and let's grow the file first, then start fresh */ |
| 11800 |
context_unload_partition_table(context); |
| 11801 |
|
| 11802 |
assert(arg_size != UINT64_MAX); |
| 11803 |
r = resize_backing_fd( |
| 11804 |
context->node, |
| 11805 |
&context->backing_fd, |
| 11806 |
node_is_our_loop ? arg_image : NULL, |
| 11807 |
node_is_our_loop ? loop_device : NULL, |
| 11808 |
context->sector_size); |
| 11809 |
if (r < 0) |
| 11810 |
return r; |
| 11811 |
|
| 11812 |
r = context_load_partition_table(context); |
| 11813 |
if (r < 0) |
| 11814 |
return r; |
| 11815 |
} |
| 11816 |
|
| 11817 |
r = context_verify_eltorito_overlap(context); |
| 11818 |
if (r < 0) |
| 11819 |
return r; |
| 11820 |
|
| 11821 |
r = context_ponder(context); |
| 11822 |
if (r == -ENOSPC) { |
| 11823 |
/* When we hit space issues, tell the user the minimal size. */ |
| 11824 |
(void) determine_auto_size(context, LOG_INFO, /* ret_current_size= */ NULL, /* ret_foreign_size= */ NULL, /* ret_minimal_size= */ NULL); |
| 11825 |
return r; |
| 11826 |
} |
| 11827 |
if (r < 0) |
| 11828 |
return r; |
| 11829 |
|
| 11830 |
(void) context_dump(context, /* late= */ false); |
| 11831 |
|
| 11832 |
r = context_write_partition_table(context); |
| 11833 |
if (r < 0) |
| 11834 |
return r; |
| 11835 |
|
| 11836 |
r = context_write_eltorito(context); |
| 11837 |
if (r < 0) |
| 11838 |
return r; |
| 11839 |
|
| 11840 |
r = context_split(context); |
| 11841 |
if (r < 0) |
| 11842 |
return r; |
| 11843 |
|
| 11844 |
r = context_crypttab(context, /* late= */ true); |
| 11845 |
if (r < 0) |
| 11846 |
return r; |
| 11847 |
|
| 11848 |
(void) context_dump(context, /* late= */ true); |
| 11849 |
|
| 11850 |
context_disarm_auto_removal(context); |
| 11851 |
|
| 11852 |
return 0; |
| 11853 |
} |
| 11854 |
|
| 11855 |
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |