daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
ed91e9
From 28008eb0bafcb3765e0ece7cb78710066f0ac2b8 Mon Sep 17 00:00:00 2001
ed91e9
From: Daan De Meyer <daan.j.demeyer@gmail.com>
ed91e9
Date: Sat, 12 Aug 2023 13:28:10 +0200
ed91e9
Subject: [PATCH 1/3] mkfs-util: Set sector size environment variables when
ed91e9
 invoking mkfs.ext4
ed91e9
ed91e9
Let's inform mkfs.ext4 about the sector size we're using.
ed91e9
---
ed91e9
 src/shared/mkfs-util.c | 16 +++++++++++++++-
ed91e9
 1 file changed, 15 insertions(+), 1 deletion(-)
ed91e9
ed91e9
diff --git a/src/shared/mkfs-util.c b/src/shared/mkfs-util.c
ed91e9
index 179bce6af6..8038abb945 100644
ed91e9
--- a/src/shared/mkfs-util.c
ed91e9
+++ b/src/shared/mkfs-util.c
ed91e9
@@ -268,7 +268,7 @@ int make_filesystem(
ed91e9
                 char * const *extra_mkfs_args) {
ed91e9
 
ed91e9
         _cleanup_free_ char *mkfs = NULL, *mangled_label = NULL;
ed91e9
-        _cleanup_strv_free_ char **argv = NULL;
ed91e9
+        _cleanup_strv_free_ char **argv = NULL, **env = NULL;
ed91e9
         _cleanup_(unlink_and_freep) char *protofile = NULL;
ed91e9
         char vol_id[CONST_MAX(SD_ID128_UUID_STRING_MAX, 8U + 1U)] = {};
ed91e9
         int r;
ed91e9
@@ -385,6 +385,15 @@ int make_filesystem(
ed91e9
                 if (root && strv_extend_strv(&argv, STRV_MAKE("-d", root), false) < 0)
ed91e9
                         return log_oom();
ed91e9
 
ed91e9
+                if (sector_size > 0)
ed91e9
+                        FOREACH_STRING(s, "MKE2FS_DEVICE_SECTSIZE", "MKE2FS_DEVICE_PHYS_SECTSIZE") {
ed91e9
+                                if (strv_extend(&env, s) < 0)
ed91e9
+                                        return log_oom();
ed91e9
+
ed91e9
+                                if (strv_extendf(&env, "%"PRIu64, sector_size) < 0)
ed91e9
+                                        return log_oom();
ed91e9
+                        }
ed91e9
+
ed91e9
         } else if (streq(fstype, "btrfs")) {
ed91e9
                 argv = strv_new(mkfs,
ed91e9
                                 "-q",
ed91e9
@@ -496,6 +505,11 @@ int make_filesystem(
ed91e9
         if (r == 0) {
ed91e9
                 /* Child */
ed91e9
 
ed91e9
+                STRV_FOREACH_PAIR(k, v, env)
ed91e9
+                        if (setenv(*k, *v, /* replace = */ true) < 0) {
ed91e9
+                                log_error_errno(r, "Failed to set %s=%s environment variable: %m", *k, *v);
ed91e9
+                                _exit(EXIT_FAILURE);
ed91e9
+                        }
ed91e9
                 execvp(mkfs, argv);
ed91e9
 
ed91e9
                 log_error_errno(errno, "Failed to execute %s: %m", mkfs);
ed91e9
-- 
ed91e9
2.41.0
ed91e9
ed91e9
ed91e9
From 8a2ee7ec8c200464302693456561f99bf92f0df4 Mon Sep 17 00:00:00 2001
ed91e9
From: Daan De Meyer <daan.j.demeyer@gmail.com>
ed91e9
Date: Sat, 12 Aug 2023 13:29:50 +0200
ed91e9
Subject: [PATCH 2/3] mkfs-util: Pass sector size to mkfs.f2fs
ed91e9
ed91e9
Let's pass the sector size to mkfs.f2fs as well.
ed91e9
---
ed91e9
 src/shared/mkfs-util.c | 8 ++++++++
ed91e9
 1 file changed, 8 insertions(+)
ed91e9
ed91e9
diff --git a/src/shared/mkfs-util.c b/src/shared/mkfs-util.c
ed91e9
index 8038abb945..1f73ff3742 100644
ed91e9
--- a/src/shared/mkfs-util.c
ed91e9
+++ b/src/shared/mkfs-util.c
ed91e9
@@ -419,6 +419,14 @@ int make_filesystem(
ed91e9
                                 "-t", one_zero(discard),
ed91e9
                                 node);
ed91e9
 
ed91e9
+                if (sector_size > 0) {
ed91e9
+                        if (strv_extend(&argv, "-w") < 0)
ed91e9
+                                return log_oom();
ed91e9
+
ed91e9
+                        if (strv_extendf(&argv, "%"PRIu64, sector_size) < 0)
ed91e9
+                                return log_oom();
ed91e9
+                }
ed91e9
+
ed91e9
         } else if (streq(fstype, "xfs")) {
ed91e9
                 const char *j;
ed91e9
 
ed91e9
-- 
ed91e9
2.41.0
ed91e9
ed91e9
ed91e9
From 8ecfc1f65ae27c0a44afd2e28a30a9fa03377cdc Mon Sep 17 00:00:00 2001
ed91e9
From: Daan De Meyer <daan.j.demeyer@gmail.com>
ed91e9
Date: Sat, 12 Aug 2023 13:30:46 +0200
ed91e9
Subject: [PATCH 3/3] repart: Use 4096 as the fallback sector size for
ed91e9
 verity/luks/filesystems
ed91e9
ed91e9
When we don't know the sector size of the actual block device, because
ed91e9
we're building an image in a loopback file and no sector size was specified
ed91e9
explicitly, let's use 4096 as the sector size for filesystems, verity and
ed91e9
LUKS. This should be the most compatible option, since 4096 will also work
ed91e9
on devices with sector size 512 or 2048.
ed91e9
ed91e9
For the actual GPT partition table size, we stick with 512 as the default
ed91e9
value since UEFI firmware and the kernel will only try to read the GPT
ed91e9
partition table from the first LBA on the device and the sector size for
ed91e9
most devices is still 512. It can also be trivially modified when copying
ed91e9
the image to another device using --copy-from + --sector-size.
ed91e9
---
ed91e9
 src/partition/repart.c | 60 +++++++++++++++++++++++++++++++-----------
ed91e9
 1 file changed, 44 insertions(+), 16 deletions(-)
ed91e9
ed91e9
diff --git a/src/partition/repart.c b/src/partition/repart.c
ed91e9
index 0431c6a86b..1796b9ed35 100644
ed91e9
--- a/src/partition/repart.c
ed91e9
+++ b/src/partition/repart.c
ed91e9
@@ -96,6 +96,10 @@
ed91e9
 /* LUKS2 volume key size. */
ed91e9
 #define VOLUME_KEY_SIZE (512ULL/8ULL)
ed91e9
 
ed91e9
+/* Use 4K as the default filesystem sector size because as long as the partitions are aligned to 4K, the
ed91e9
+ * filesystems will then also be compatible with sector sizes 512, 1024 and 2048. */
ed91e9
+#define DEFAULT_FILESYSTEM_SECTOR_SIZE 4096ULL
ed91e9
+
ed91e9
 /* Note: When growing and placing new partitions we always align to 4K sector size. It's how newer hard disks
ed91e9
  * are designed, and if everything is aligned to that performance is best. And for older hard disks with 512B
ed91e9
  * sector size devices were generally assumed to have an even number of sectors, hence at the worst we'll
ed91e9
@@ -267,8 +271,7 @@ typedef struct Context {
ed91e9
         uint64_t start, end, total;
ed91e9
 
ed91e9
         struct fdisk_context *fdisk_context;
ed91e9
-        uint64_t sector_size;
ed91e9
-        uint64_t grain_size;
ed91e9
+        uint64_t sector_size, grain_size, fs_sector_size;
ed91e9
 
ed91e9
         sd_id128_t seed;
ed91e9
 
ed91e9
@@ -1988,7 +1991,7 @@ static int context_load_partition_table(Context *context) {
ed91e9
         sd_id128_t disk_uuid;
ed91e9
         size_t n_partitions;
ed91e9
         unsigned long secsz;
ed91e9
-        uint64_t grainsz;
ed91e9
+        uint64_t grainsz, fs_secsz = DEFAULT_FILESYSTEM_SECTOR_SIZE;
ed91e9
         int r;
ed91e9
 
ed91e9
         assert(context);
ed91e9
@@ -2002,20 +2005,31 @@ static int context_load_partition_table(Context *context) {
ed91e9
         if (!c)
ed91e9
                 return log_oom();
ed91e9
 
ed91e9
-        if (arg_sector_size > 0)
ed91e9
+        if (arg_sector_size > 0) {
ed91e9
                 r = fdisk_save_user_sector_size(c, /* phy= */ 0, arg_sector_size);
ed91e9
-        else {
ed91e9
+                fs_secsz = arg_sector_size;
ed91e9
+        } else {
ed91e9
                 uint32_t ssz;
ed91e9
+                struct stat st;
ed91e9
 
ed91e9
                 r = context_open_and_lock_backing_fd(context, context->node);
ed91e9
                 if (r < 0)
ed91e9
                         return r;
ed91e9
 
ed91e9
+                if (fstat(context->backing_fd, &st) < 0)
ed91e9
+                        return log_error_errno(r, "Failed to stat %s: %m", context->node);
ed91e9
+
ed91e9
                 /* Auto-detect sector size if not specified. */
ed91e9
                 r = probe_sector_size_prefer_ioctl(context->backing_fd, &ssz;;
ed91e9
                 if (r < 0)
ed91e9
                         return log_error_errno(r, "Failed to probe sector size of '%s': %m", context->node);
ed91e9
 
ed91e9
+                /* If we found the sector size and we're operating on a block device, use it as the file
ed91e9
+                 * system sector size as well, as we know its the sector size of the actual block device and
ed91e9
+                 * not just the offset at which we found the GPT header. */
ed91e9
+                if (r > 0 && S_ISBLK(st.st_mode))
ed91e9
+                        fs_secsz = ssz;
ed91e9
+
ed91e9
                 r = fdisk_save_user_sector_size(c, /* phy= */ 0, ssz);
ed91e9
         }
ed91e9
         if (r < 0)
ed91e9
@@ -2041,8 +2055,9 @@ static int context_load_partition_table(Context *context) {
ed91e9
                         return log_error_errno(errno, "Failed to stat block device '%s': %m", context->node);
ed91e9
 
ed91e9
                 if (S_ISREG(st.st_mode) && st.st_size == 0) {
ed91e9
-                        /* User the fallback values if we have no better idea */
ed91e9
-                        context->sector_size = arg_sector_size ?: 512;
ed91e9
+                        /* Use the fallback values if we have no better idea */
ed91e9
+                        context->sector_size = fdisk_get_sector_size(c);
ed91e9
+                        context->fs_sector_size = fs_secsz;
ed91e9
                         context->grain_size = 4096;
ed91e9
                         return /* from_scratch = */ true;
ed91e9
                 }
ed91e9
@@ -2318,6 +2333,7 @@ add_initial_free_area:
ed91e9
         context->end = last_lba;
ed91e9
         context->total = nsectors;
ed91e9
         context->sector_size = secsz;
ed91e9
+        context->fs_sector_size = fs_secsz;
ed91e9
         context->grain_size = grainsz;
ed91e9
         context->fdisk_context = TAKE_PTR(c);
ed91e9
 
ed91e9
@@ -3271,7 +3287,7 @@ static int partition_encrypt(Context *context, Partition *p, const char *node) {
ed91e9
 #if HAVE_LIBCRYPTSETUP && HAVE_CRYPT_SET_DATA_OFFSET && HAVE_CRYPT_REENCRYPT_INIT_BY_PASSPHRASE && HAVE_CRYPT_REENCRYPT
ed91e9
         struct crypt_params_luks2 luks_params = {
ed91e9
                 .label = strempty(ASSERT_PTR(p)->new_label),
ed91e9
-                .sector_size = ASSERT_PTR(context)->sector_size,
ed91e9
+                .sector_size = ASSERT_PTR(context)->fs_sector_size,
ed91e9
                 .data_device = node,
ed91e9
         };
ed91e9
         struct crypt_params_reencrypt reencrypt_params = {
ed91e9
@@ -3310,7 +3326,7 @@ static int partition_encrypt(Context *context, Partition *p, const char *node) {
ed91e9
                 return log_error_errno(r, "Failed to create temporary LUKS header file: %m");
ed91e9
 
ed91e9
         /* Weird cryptsetup requirement which requires the header file to be the size of at least one sector. */
ed91e9
-        r = ftruncate(fileno(h), context->sector_size);
ed91e9
+        r = ftruncate(fileno(h), luks_params.sector_size);
ed91e9
         if (r < 0)
ed91e9
                 return log_error_errno(r, "Failed to grow temporary LUKS header file: %m");
ed91e9
 
ed91e9
@@ -3549,8 +3565,8 @@ static int partition_format_verity_hash(
ed91e9
                                 .flags = CRYPT_VERITY_CREATE_HASH,
ed91e9
                                 .hash_name = "sha256",
ed91e9
                                 .hash_type = 1,
ed91e9
-                                .data_block_size = context->sector_size,
ed91e9
-                                .hash_block_size = context->sector_size,
ed91e9
+                                .data_block_size = context->fs_sector_size,
ed91e9
+                                .hash_block_size = context->fs_sector_size,
ed91e9
                                 .salt_size = 32,
ed91e9
                         });
ed91e9
         if (r < 0) {
ed91e9
@@ -4090,7 +4106,7 @@ static int context_mkfs(Context *context) {
ed91e9
                 }
ed91e9
 
ed91e9
                 r = make_filesystem(partition_target_path(t), p->format, strempty(p->new_label), root,
ed91e9
-                                    p->fs_uuid, arg_discard, context->sector_size, NULL);
ed91e9
+                                    p->fs_uuid, arg_discard, context->fs_sector_size, NULL);
ed91e9
                 if (r < 0)
ed91e9
                         return r;
ed91e9
 
ed91e9
@@ -5423,8 +5439,14 @@ static int context_minimize(Context *context) {
ed91e9
                                 return r;
ed91e9
                 }
ed91e9
 
ed91e9
-                r = make_filesystem(d ? d->node : temp, p->format, strempty(p->new_label), root, fs_uuid,
ed91e9
-                                    arg_discard, context->sector_size, NULL);
ed91e9
+                r = make_filesystem(d ? d->node : temp,
ed91e9
+                                    p->format,
ed91e9
+                                    strempty(p->new_label),
ed91e9
+                                    root,
ed91e9
+                                    fs_uuid,
ed91e9
+                                    arg_discard,
ed91e9
+                                    context->fs_sector_size,
ed91e9
+                                    NULL);
ed91e9
                 if (r < 0)
ed91e9
                         return r;
ed91e9
 
ed91e9
@@ -5477,8 +5499,14 @@ static int context_minimize(Context *context) {
ed91e9
                 if (r < 0 && r != -ENOENT && !ERRNO_IS_PRIVILEGE(r))
ed91e9
                         return log_error_errno(r, "Failed to make loopback device of %s: %m", temp);
ed91e9
 
ed91e9
-                r = make_filesystem(d ? d->node : temp, p->format, strempty(p->new_label), root, p->fs_uuid,
ed91e9
-                                    arg_discard, context->sector_size, NULL);
ed91e9
+                r = make_filesystem(d ? d->node : temp,
ed91e9
+                                    p->format,
ed91e9
+                                    strempty(p->new_label),
ed91e9
+                                    root,
ed91e9
+                                    p->fs_uuid,
ed91e9
+                                    arg_discard,
ed91e9
+                                    context->fs_sector_size,
ed91e9
+                                    NULL);
ed91e9
                 if (r < 0)
ed91e9
                         return r;
ed91e9
 
ed91e9
-- 
ed91e9
2.41.0
ed91e9