]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
homework: Auto-probe luks sector size
authorscarlet-storm <12461256+scarlet-storm@users.noreply.github.com>
Fri, 27 Dec 2024 11:49:34 +0000 (17:19 +0530)
committerscarlet-storm <12461256+scarlet-storm@users.noreply.github.com>
Tue, 17 Feb 2026 12:53:35 +0000 (18:23 +0530)
Auto-probe the luks sector size, if not explicitly
specified in the home record

src/home/homework-luks.c

index e559836cc4a4e560aa84b1d5cf9567d63cda8517..44f020d8ae470c93533aa6a61b0b21a394ac9911 100644 (file)
@@ -1755,6 +1755,7 @@ static int luks_format(
                 const PasswordCache *cache,
                 char **effective_passwords,
                 bool discard,
+                uint64_t sector_size,
                 UserRecord *hr,
                 struct crypt_device **ret) {
 
@@ -1809,7 +1810,7 @@ static int luks_format(
                         &(struct crypt_params_luks2) {
                                 .label = label,
                                 .subsystem = "systemd-home",
-                                .sector_size = user_record_luks_sector_size(hr),
+                                .sector_size = sector_size, /* sector-size of 0 is auto for libcryptsetup */
                                 .pbkdf = &good_pbkdf,
                         });
         if (r < 0)
@@ -2166,7 +2167,7 @@ int home_create_luks(
                 UserRecord **ret_home) {
 
         _cleanup_free_ char *subdir = NULL, *disk_uuid_path = NULL;
-        uint64_t encrypted_size,
+        uint64_t encrypted_size, image_sector_size, luks_sector_size,
                 host_size = 0, partition_offset = 0, partition_size = 0; /* Unnecessary initialization to appease gcc */
         _cleanup_(user_record_unrefp) UserRecord *new_home = NULL;
         sd_id128_t partition_uuid, fs_uuid, luks_uuid, disk_uuid;
@@ -2337,9 +2338,17 @@ int home_create_luks(
                 log_info("Allocating image file completed.");
         }
 
+        if (h->luks_sector_size == UINT64_MAX) {
+                /* If sector size is not specified, select UINT32_MAX, i.e. auto-probe */
+                image_sector_size = UINT32_MAX;
+                /* Let cryptsetup decide if the sector size is not specified in home record */
+                luks_sector_size = 0;
+        } else
+                luks_sector_size = image_sector_size = user_record_luks_sector_size(h);
+
         r = make_partition_table(
                         setup->image_fd,
-                        user_record_luks_sector_size(h),
+                        image_sector_size,
                         user_record_user_name_and_realm(h),
                         partition_uuid,
                         &partition_offset,
@@ -2355,7 +2364,7 @@ int home_create_luks(
                         O_RDWR,
                         partition_offset,
                         partition_size,
-                        user_record_luks_sector_size(h),
+                        image_sector_size,
                         0,
                         LOCK_EX,
                         &setup->loop);
@@ -2375,6 +2384,7 @@ int home_create_luks(
                         cache,
                         effective_passwords,
                         user_record_luks_discard(h) || user_record_luks_offline_discard(h),
+                        luks_sector_size,
                         h,
                         &setup->crypt_device);
         if (r < 0)