]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
homework: Use same sector size when probing the device
authorscarlet-storm <12461256+scarlet-storm@users.noreply.github.com>
Sat, 28 Dec 2024 03:43:34 +0000 (09:13 +0530)
committerscarlet-storm <12461256+scarlet-storm@users.noreply.github.com>
Tue, 17 Feb 2026 12:53:35 +0000 (18:23 +0530)
If there is an explicit sector size specified in the user record,
use the same when probing the file using libblkid. The default
is 512 bytes, which will not be able to find the signatures, if the
partition table on regular file was created assuming 4k sectors

src/home/homework-luks.c

index 3ee16fe81598a406ae1779e3554973c9aee0dc5c..5e6d782fdbbe58319bf2577899595b221693625f 100644 (file)
@@ -666,6 +666,7 @@ static int luks_validate(
                 int fd,
                 const char *label,
                 sd_id128_t partition_uuid,
+                uint64_t sector_size,
                 sd_id128_t *ret_partition_uuid,
                 uint64_t *ret_offset,
                 uint64_t *ret_size) {
@@ -683,6 +684,7 @@ static int luks_validate(
         assert(label);
         assert(ret_offset);
         assert(ret_size);
+        assert(sector_size > 0);
 
         r = dlopen_libblkid();
         if (r < 0)
@@ -697,6 +699,12 @@ static int luks_validate(
         if (r != 0)
                 return errno_or_else(ENOMEM);
 
+        /* Set probing sector size if explicitly specified */
+        if (sector_size != UINT32_MAX) {
+                r = sym_blkid_probe_set_sectorsize(b, sector_size);
+                if (r != 0)
+                        return errno_or_else(EINVAL);
+        }
         (void) sym_blkid_probe_enable_superblocks(b, 1);
         (void) sym_blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
         (void) sym_blkid_probe_enable_partitions(b, 1);
@@ -778,6 +786,7 @@ static int luks_validate(
         if ((uint64_t) size > UINT64_MAX / 512U)
                 return -EINVAL;
 
+        /* libblkid returns partitions sizes in count of 512-sectors. This does not necessarily need to match the device sector size */
         *ret_offset = offset * 512U;
         *ret_size = size * 512U;
         *ret_partition_uuid = found_partition_uuid;
@@ -1401,7 +1410,15 @@ int home_setup_luks(
                 if (!subdir)
                         return log_oom();
 
-                r = luks_validate(setup->image_fd, user_record_user_name_and_realm(h), h->partition_uuid, &found_partition_uuid, &offset, &size);
+                r = luks_validate(
+                                setup->image_fd,
+                                user_record_user_name_and_realm(h),
+                                h->partition_uuid,
+                                /* if sector size is not specified, select UINT32_MAX, i.e. auto-probe */
+                                h->luks_sector_size == UINT64_MAX ? UINT32_MAX : user_record_luks_sector_size(h),
+                                &found_partition_uuid,
+                                &offset,
+                                &size);
                 if (r < 0)
                         return log_error_errno(r, "Failed to validate disk label: %m");