homework: Use same sector size when probing the device

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
This commit is contained in:
scarlet-storm
2026-02-17 18:23:35 +05:30
parent 118cccf97f
commit 292525dd20
+18 -1
View 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");