From 452467661ebd6cfdfa059be403ae477b02fee4eb Mon Sep 17 00:00:00 2001 From: "daniel.zatovic" Date: Thu, 3 Feb 2022 21:25:36 +0100 Subject: [PATCH] Support --device-size option for plain devices. --- man/cryptsetup.8 | 4 ++-- src/cryptsetup.c | 9 ++++++++- src/cryptsetup_args.h | 2 +- tests/compat-test-args | 3 ++- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/man/cryptsetup.8 b/man/cryptsetup.8 index 7c87003c..c9a4d305 100644 --- a/man/cryptsetup.8 +++ b/man/cryptsetup.8 @@ -220,8 +220,8 @@ Opens (creates a mapping with) backed by device . \fB\fR can be [\-\-hash, \-\-cipher, \-\-verify-passphrase, \-\-sector\-size, \-\-key-file, \-\-keyfile-offset, \-\-key-size, -\-\-offset, \-\-skip, \-\-size, \-\-readonly, \-\-shared, \-\-allow\-discards, -\-\-refresh] +\-\-offset, \-\-skip, \-\-device\-size, \-\-size, \-\-readonly, +\-\-shared, \-\-allow\-discards, \-\-refresh] Example: 'cryptsetup open \-\-type plain /dev/sda10 e1' maps the raw encrypted device /dev/sda10 to the mapped (decrypted) device diff --git a/src/cryptsetup.c b/src/cryptsetup.c index de4439d8..5d48e2a4 100644 --- a/src/cryptsetup.c +++ b/src/cryptsetup.c @@ -145,7 +145,6 @@ static int action_open_plain(void) .hash = ARG_SET(OPT_HASH_ID) ? ARG_STR(OPT_HASH_ID) : DEFAULT_PLAIN_HASH, .skip = ARG_UINT64(OPT_SKIP_ID), .offset = ARG_UINT64(OPT_OFFSET_ID), - .size = ARG_UINT64(OPT_SIZE_ID), .sector_size = ARG_UINT32(OPT_SECTOR_SIZE_ID) ?: SECTOR_SIZE }; char *password = NULL; @@ -230,6 +229,11 @@ static int action_open_plain(void) pmode = cipher_mode; } + if (ARG_SET(OPT_DEVICE_SIZE_ID)) + params.size = ARG_UINT64(OPT_DEVICE_SIZE_ID) / SECTOR_SIZE; + else if (ARG_SET(OPT_SIZE_ID)) + params.size = ARG_UINT64(OPT_SIZE_ID); + r = crypt_format(cd, CRYPT_PLAIN, pcipher, pmode, NULL, NULL, @@ -2738,6 +2742,9 @@ static const char * verify_open(void) (strncmp(device_type, "luks", 4) && strcmp(device_type, "tcrypt") && strcmp(device_type, "bitlk")))) return _("Option --test-passphrase is allowed only for open of LUKS, TCRYPT and BITLK devices."); + if (ARG_SET(OPT_DEVICE_SIZE_ID) && ARG_SET(OPT_SIZE_ID)) + return _("Options --device-size and --size cannot be combined."); + /* "open --type tcrypt" and "tcryptDump" checks are identical */ return verify_tcryptdump(); } diff --git a/src/cryptsetup_args.h b/src/cryptsetup_args.h index a348ca22..73454d3f 100644 --- a/src/cryptsetup_args.h +++ b/src/cryptsetup_args.h @@ -56,7 +56,7 @@ #define OPT_ALIGN_PAYLOAD_ACTIONS { FORMAT_ACTION, REENCRYPT_ACTION } #define OPT_ALLOW_DISCARDS_ACTIONS { OPEN_ACTION } #define OPT_DEFERRED_ACTIONS { CLOSE_ACTION } -#define OPT_DEVICE_SIZE_ACTIONS { RESIZE_ACTION, REENCRYPT_ACTION } +#define OPT_DEVICE_SIZE_ACTIONS { OPEN_ACTION, RESIZE_ACTION, REENCRYPT_ACTION } #define OPT_DISABLE_VERACRYPT_ACTIONS { OPEN_ACTION, TCRYPTDUMP_ACTION } #define OPT_HOTZONE_SIZE_ACTIONS { REENCRYPT_ACTION } #define OPT_INTEGRITY_ACTIONS { FORMAT_ACTION, REENCRYPT_ACTION } diff --git a/tests/compat-test-args b/tests/compat-test-args index b80b2966..faeddd00 100755 --- a/tests/compat-test-args +++ b/tests/compat-test-args @@ -277,7 +277,8 @@ exp_fail reencrypt DEV --reduce-device-size 64m --device-size 100g # exp_fail open DEV --encrypt # exp_fail open DEV NAME --device-size 32m # exp_fail open DEV NAME --size 100 -exp_fail open DEV NAME --device-size 32m --type plain +exp_pass open DEV NAME --device-size 32m --type plain +exp_fail open DEV NAME --device-size $((32*1024*1024+1)) --type plain exp_pass open DEV NAME --size 100 --type plain exp_fail open DEV NAME --size 100 --device-size $((512*100)) --type plain exp_fail reencrypt DEV --device-size $((32*1024*1024+1))