]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/partition/repart.c
Merge pull request #30284 from YHNdnzj/fstab-wantedby-defaultdeps
[thirdparty/systemd.git] / src / partition / repart.c
index 88d4c5186db5fef6aa7de1f723c586ee0e163bc7..1e9284e2e2e855ad71c59e91b7192e6dfae24c9b 100644 (file)
@@ -2205,8 +2205,8 @@ static int context_read_definitions(Context *context) {
                         if (q) {
                                 if (q->priority != p->priority)
                                         return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL),
-                                                        "Priority mismatch (%i != %i) for verity sibling partitions with VerityMatchKey=%s",
-                                                        p->priority, q->priority, p->verity_match_key);
+                                                          "Priority mismatch (%i != %i) for verity sibling partitions with VerityMatchKey=%s",
+                                                          p->priority, q->priority, p->verity_match_key);
 
                                 p->siblings[mode] = q;
                         }
@@ -2331,7 +2331,7 @@ static int context_load_partition_table(Context *context) {
                         return r;
 
                 if (fstat(context->backing_fd, &st) < 0)
-                        return log_error_errno(r, "Failed to stat %s: %m", context->node);
+                        return log_error_errno(errno, "Failed to stat %s: %m", context->node);
 
                 /* Auto-detect sector size if not specified. */
                 r = probe_sector_size_prefer_ioctl(context->backing_fd, &ssz);
@@ -3194,13 +3194,13 @@ static int context_wipe_range(Context *context, uint64_t offset, uint64_t size)
                 errno = 0;
                 r = blkid_do_probe(probe);
                 if (r < 0)
-                        return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe for file systems.");
+                        return log_error_errno(errno_or_else(EIO), "Failed to probe for file systems.");
                 if (r > 0)
                         break;
 
                 errno = 0;
                 if (blkid_do_wipe(probe, false) < 0)
-                        return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to wipe file system signature.");
+                        return log_error_errno(errno_or_else(EIO), "Failed to wipe file system signature.");
         }
 
         return 0;
@@ -3530,7 +3530,7 @@ static int prepare_temporary_file(PartitionTarget *t, uint64_t size) {
 
         if (ftruncate(fd, size) < 0)
                 return log_error_errno(errno, "Failed to truncate temporary file to %s: %m",
-                                        FORMAT_BYTES(size));
+                                       FORMAT_BYTES(size));
 
         t->fd = TAKE_FD(fd);
         t->path = TAKE_PTR(temp);
@@ -3687,7 +3687,6 @@ static int partition_encrypt(Context *context, Partition *p, PartitionTarget *ta
         _cleanup_free_ char *hp = NULL, *vol = NULL, *dm_name = NULL;
         const char *passphrase = NULL;
         size_t passphrase_size = 0;
-        TPM2Flags flags = 0;
         const char *vt;
         int r;
 
@@ -3712,9 +3711,8 @@ static int partition_encrypt(Context *context, Partition *p, PartitionTarget *ta
 
                 /* Weird cryptsetup requirement which requires the header file to be the size of at least one
                  * sector. */
-                r = ftruncate(fileno(h), luks_params.sector_size);
-                if (r < 0)
-                        return log_error_errno(r, "Failed to grow temporary LUKS header file: %m");
+                if (ftruncate(fileno(h), luks_params.sector_size) < 0)
+                        return log_error_errno(errno, "Failed to grow temporary LUKS header file: %m");
         } else {
                 if (asprintf(&dm_name, "luks-repart-%08" PRIx64, random_u64()) < 0)
                         return log_oom();
@@ -3747,14 +3745,15 @@ static int partition_encrypt(Context *context, Partition *p, PartitionTarget *ta
                         return log_error_errno(r, "Failed to set data offset: %m");
         }
 
-        r = sym_crypt_format(cd,
-                         CRYPT_LUKS2,
-                         "aes",
-                         "xts-plain64",
-                         SD_ID128_TO_UUID_STRING(p->luks_uuid),
-                         NULL,
-                         VOLUME_KEY_SIZE,
-                         &luks_params);
+        r = sym_crypt_format(
+                        cd,
+                        CRYPT_LUKS2,
+                        "aes",
+                        "xts-plain64",
+                        SD_ID128_TO_UUID_STRING(p->luks_uuid),
+                        NULL,
+                        VOLUME_KEY_SIZE,
+                        &luks_params);
         if (r < 0)
                 return log_error_errno(r, "Failed to LUKS2 format future partition: %m");
 
@@ -3782,6 +3781,7 @@ static int partition_encrypt(Context *context, Partition *p, PartitionTarget *ta
                 size_t secret_size, blob_size, pubkey_size = 0, srk_buf_size = 0;
                 ssize_t base64_encoded_size;
                 int keyslot;
+                TPM2Flags flags = 0;
 
                 if (arg_tpm2_public_key_pcr_mask != 0) {
                         r = tpm2_load_pcr_public_key(arg_tpm2_public_key, &pubkey, &pubkey_size);
@@ -4547,7 +4547,7 @@ static int do_copy_files(Context *context, Partition *p, const char *root) {
 
                 rfd = open(root, O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
                 if (rfd < 0)
-                        return rfd;
+                        return -errno;
 
                 sfd = chase_and_open(*source, arg_copy_source, CHASE_PREFIX_ROOT, O_PATH|O_DIRECTORY|O_CLOEXEC|O_NOCTTY, NULL);
                 if (sfd < 0)
@@ -4849,6 +4849,17 @@ static int context_mkfs(Context *context) {
                 if (r < 0)
                         return r;
 
+                /* The mkfs binary we invoked might have removed our temporary file when we're not operating
+                 * on a loop device, so let's make sure we open the file again to make sure our file
+                 * descriptor points to any potential new file. */
+
+                if (t->fd >= 0 && t->path && !t->loop) {
+                        safe_close(t->fd);
+                        t->fd = open(t->path, O_RDWR|O_CLOEXEC);
+                        if (t->fd < 0)
+                                return log_error_errno(errno, "Failed to reopen temporary file: %m");
+                }
+
                 log_info("Successfully formatted future partition %" PRIu64 ".", p->partno);
 
                 /* If we're writing to a loop device, we can now mount the empty filesystem and populate it. */
@@ -6031,8 +6042,9 @@ static int context_open_copy_block_paths(
                 if (S_ISREG(st.st_mode))
                         size = st.st_size;
                 else if (S_ISBLK(st.st_mode)) {
-                        if (ioctl(source_fd, BLKGETSIZE64, &size) != 0)
-                                return log_error_errno(errno, "Failed to determine size of block device to copy from: %m");
+                        r = blockdev_get_device_size(source_fd, &size);
+                        if (r < 0)
+                                return log_error_errno(r, "Failed to determine size of block device to copy from: %m");
                 } else
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified path to copy blocks from '%s' is not a regular file, block device or directory, refusing: %m", opened);
 
@@ -6264,10 +6276,10 @@ static int context_minimize(Context *context) {
                 d = loop_device_unref(d);
 
                 /* Erase the previous filesystem first. */
-                if (ftruncate(fd, 0))
+                if (ftruncate(fd, 0) < 0)
                         return log_error_errno(errno, "Failed to erase temporary file: %m");
 
-                if (ftruncate(fd, fsz))
+                if (ftruncate(fd, fsz) < 0)
                         return log_error_errno(errno, "Failed to truncate temporary file to %s: %m", FORMAT_BYTES(fsz));
 
                 if (arg_offline <= 0) {
@@ -6359,7 +6371,7 @@ static int context_minimize(Context *context) {
                         return log_error_errno(errno, "Failed to open temporary file %s: %m", temp);
 
                 if (fstat(fd, &st) < 0)
-                        return log_error_errno(r, "Failed to stat temporary file: %m");
+                        return log_error_errno(errno, "Failed to stat temporary file: %m");
 
                 log_info("Minimal partition size of verity hash partition %s is %s",
                          strna(hint), FORMAT_BYTES(st.st_size));
@@ -7354,8 +7366,9 @@ static int resize_backing_fd(
 
                 assert(loop_device);
 
-                if (ioctl(*fd, BLKGETSIZE64, &current_size) < 0)
-                        return log_error_errno(errno, "Failed to determine size of block device %s: %m", node);
+                r = blockdev_get_device_size(*fd, &current_size);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to determine size of block device %s: %m", node);
         } else {
                 r = stat_verify_regular(&st);
                 if (r < 0)
@@ -7567,9 +7580,9 @@ static int run(int argc, char *argv[]) {
                 if (!d)
                         return log_oom();
 
-                r = search_and_access(d, F_OK, arg_root, CONF_PATHS_USR_STRV("systemd/repart/definitions"), &dp);
+                r = search_and_access(d, F_OK, NULL, CONF_PATHS_USR_STRV("systemd/repart/definitions"), &dp);
                 if (r < 0)
-                        return log_error_errno(errno, "DDI type '%s' is not defined: %m", arg_make_ddi);
+                        return log_error_errno(r, "DDI type '%s' is not defined: %m", arg_make_ddi);
 
                 if (strv_consume(&arg_definitions, TAKE_PTR(dp)) < 0)
                         return log_oom();