diff --git a/lib/setup.c b/lib/setup.c index 2c24bd1a..26837ce1 100644 --- a/lib/setup.c +++ b/lib/setup.c @@ -2049,6 +2049,14 @@ int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size) if (r) goto out; + if (new_size & ((dmd.u.crypt.sector_size >> SECTOR_SHIFT) - 1)) { + log_err(cd, _("Device %s size is not aligned to requested sector size (%u bytes).\n"), + crypt_get_device_name(cd), (unsigned)dmd.u.crypt.sector_size); + r = -EINVAL; + goto out; + } + + if (new_size == dmd.size) { log_dbg("Device has already requested size %" PRIu64 " sectors.", dmd.size); diff --git a/tests/compat-test b/tests/compat-test index a3c0b919..29cf1df2 100755 --- a/tests/compat-test +++ b/tests/compat-test @@ -472,6 +472,15 @@ $CRYPTSETUP -q remove $DEV_NAME || fail echo $PWD1 | $CRYPTSETUP -q create $DEV_NAME --hash sha1 --size 100 $LOOPDEV || fail $CRYPTSETUP -q status $DEV_NAME | grep "size:" | grep -q "100 sectors" || fail $CRYPTSETUP -q remove $DEV_NAME || fail +# 4k sector resize (if kernel supports it) +echo $PWD1 | $CRYPTSETUP -q open --type plain $LOOPDEV $DEV_NAME --sector-size 4096 --size 8 >/dev/null 2>&1 +if [ $? -eq 0 ] ; then + $CRYPTSETUP -q status $DEV_NAME | grep "size:" | grep -q "8 sectors" + $CRYPTSETUP -q resize $DEV_NAME --size 16 || fail + $CRYPTSETUP -q status $DEV_NAME | grep "size:" | grep -q "16 sectors" + $CRYPTSETUP -q resize $DEV_NAME --size 9 2>/dev/null && fail + $CRYPTSETUP -q remove $DEV_NAME || fail +fi # verify is ignored on non-tty input echo $PWD1 | $CRYPTSETUP create $DEV_NAME $LOOPDEV --hash sha1 --verify-passphrase 2>/dev/null || fail $CRYPTSETUP -q remove $DEV_NAME || fail