2 * integritysetup - setup integrity protected volumes for dm-integrity
4 * Copyright (C) 2017-2020 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2017-2020 Milan Broz
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "cryptsetup.h"
23 #include <uuid/uuid.h>
25 #define PACKAGE_INTEGRITY "integritysetup"
27 #define DEFAULT_ALG_NAME "crc32c"
28 #define MAX_KEY_SIZE 4096
30 static const char *opt_journal_size_str = NULL;
31 static uint64_t opt_journal_size = 0;
32 static int opt_interleave_sectors = 0;
33 static int opt_journal_watermark = 0;
34 static int opt_bitmap_sectors_per_bit = 0;
35 static int opt_journal_commit_time = 0;
36 static int opt_bitmap_flush_time = 0;
37 static int opt_tag_size = 0;
38 static int opt_sector_size = 0;
39 static int opt_buffer_sectors = 0;
41 static int opt_no_wipe = 0;
43 static const char *opt_data_device = NULL;
45 static const char *opt_integrity = DEFAULT_ALG_NAME;
46 static const char *opt_integrity_key_file = NULL;
47 static int opt_integrity_key_size = 0;
49 static const char *opt_journal_integrity = NULL; /* none */
50 static const char *opt_journal_integrity_key_file = NULL;
51 static int opt_journal_integrity_key_size = 0;
53 static const char *opt_journal_crypt = NULL; /* none */
54 static const char *opt_journal_crypt_key_file = NULL;
55 static int opt_journal_crypt_key_size = 0;
57 static int opt_integrity_nojournal = 0;
58 static int opt_integrity_recovery = 0;
59 static int opt_integrity_bitmap = 0;
60 static int opt_integrity_legacy_padding = 0;
62 static int opt_integrity_recalculate = 0;
63 static int opt_allow_discards = 0;
65 static const char **action_argv;
66 static int action_argc;
68 // FIXME: move this to tools and handle EINTR
69 static int _read_mk(const char *file, char **key, int keysize)
73 if (keysize <= 0 || keysize > MAX_KEY_SIZE) {
74 log_err(_("Invalid key size."));
78 *key = crypt_safe_alloc(keysize);
82 fd = open(file, O_RDONLY);
84 log_err(_("Cannot read keyfile %s."), file);
87 if ((read(fd, *key, keysize) != keysize)) {
88 log_err(_("Cannot read %d bytes from keyfile %s."), keysize, file);
95 crypt_safe_free(*key);
100 static int _read_keys(char **integrity_key, struct crypt_params_integrity *params)
102 char *int_key = NULL, *journal_integrity_key = NULL, *journal_crypt_key = NULL;
105 if (integrity_key && opt_integrity_key_file) {
106 r = _read_mk(opt_integrity_key_file, &int_key, opt_integrity_key_size);
109 params->integrity_key_size = opt_integrity_key_size;
112 if (opt_journal_integrity_key_file) {
113 r = _read_mk(opt_journal_integrity_key_file, &journal_integrity_key, opt_journal_integrity_key_size);
115 crypt_safe_free(int_key);
118 params->journal_integrity_key = journal_integrity_key;
119 params->journal_integrity_key_size = opt_journal_integrity_key_size;
122 if (opt_journal_crypt_key_file) {
123 r = _read_mk(opt_journal_crypt_key_file, &journal_crypt_key, opt_journal_crypt_key_size);
125 crypt_safe_free(int_key);
126 crypt_safe_free(journal_integrity_key);
129 params->journal_crypt_key = journal_crypt_key;
130 params->journal_crypt_key_size = opt_journal_crypt_key_size;
134 *integrity_key = int_key;
139 static int _wipe_data_device(struct crypt_device *cd, const char *integrity_key)
141 char tmp_name[64], tmp_path[128], tmp_uuid[40];
146 log_std(_("Wiping device to initialize integrity checksum.\n"
147 "You can interrupt this by pressing CTRL+c "
148 "(rest of not wiped device will contain invalid checksum).\n"));
150 /* Activate the device a temporary one */
151 uuid_generate(tmp_uuid_bin);
152 uuid_unparse(tmp_uuid_bin, tmp_uuid);
153 if (snprintf(tmp_name, sizeof(tmp_name), "temporary-cryptsetup-%s", tmp_uuid) < 0)
155 if (snprintf(tmp_path, sizeof(tmp_path), "%s/%s", crypt_get_dir(), tmp_name) < 0)
158 r = crypt_activate_by_volume_key(cd, tmp_name, integrity_key,
159 opt_integrity_key_size, CRYPT_ACTIVATE_PRIVATE | CRYPT_ACTIVATE_NO_JOURNAL);
163 /* Wipe the device */
165 r = crypt_wipe(cd, tmp_path, CRYPT_WIPE_ZERO, 0, 0, DEFAULT_WIPE_BLOCK,
166 0, &tools_wipe_progress, NULL);
167 if (crypt_deactivate(cd, tmp_name))
168 log_err(_("Cannot deactivate temporary device %s."), tmp_path);
174 static int action_format(int arg)
176 struct crypt_device *cd = NULL;
177 struct crypt_params_integrity params = {
178 .journal_size = opt_journal_size,
179 .interleave_sectors = opt_interleave_sectors,
180 /* in bitmap mode we have to overload these values... */
181 .journal_watermark = opt_integrity_bitmap ? opt_bitmap_sectors_per_bit : opt_journal_watermark,
182 .journal_commit_time = opt_integrity_bitmap ? opt_bitmap_flush_time : opt_journal_commit_time,
183 .buffer_sectors = opt_buffer_sectors,
184 .tag_size = opt_tag_size,
185 .sector_size = opt_sector_size ?: SECTOR_SIZE,
187 char integrity[MAX_CIPHER_LEN], journal_integrity[MAX_CIPHER_LEN], journal_crypt[MAX_CIPHER_LEN];
188 char *integrity_key = NULL, *msg = NULL;
193 r = crypt_parse_hash_integrity_mode(opt_integrity, integrity);
195 log_err(_("No known integrity specification pattern detected."));
198 params.integrity = integrity;
201 if (opt_journal_integrity) {
202 r = crypt_parse_hash_integrity_mode(opt_journal_integrity, journal_integrity);
204 log_err(_("No known integrity specification pattern detected."));
207 params.journal_integrity = journal_integrity;
210 if (opt_journal_crypt) {
211 r = crypt_parse_hash_integrity_mode(opt_journal_crypt, journal_crypt);
213 log_err(_("No known integrity specification pattern detected."));
216 params.journal_crypt = journal_crypt;
219 r = _read_keys(&integrity_key, ¶ms);
223 r = crypt_init_data_device(&cd, action_argv[0], opt_data_device);
227 r = asprintf(&msg, _("This will overwrite data on %s irrevocably."), action_argv[0]);
233 r = yesDialog(msg, _("Operation aborted.\n")) ? 0 : -EINVAL;
238 r = tools_detect_signatures(action_argv[0], 0, &signatures);
242 /* Signature candidates found */
243 if (signatures && ((r = tools_wipe_all_signatures(action_argv[0])) < 0))
246 if (opt_integrity_legacy_padding)
247 crypt_set_compatibility(cd, CRYPT_COMPAT_LEGACY_INTEGRITY_PADDING);
249 r = crypt_format(cd, CRYPT_INTEGRITY, NULL, NULL, NULL, NULL, 0, ¶ms);
250 if (r < 0) /* FIXME: call wipe signatures again */
253 if (!opt_batch_mode && !crypt_get_integrity_info(cd, ¶ms2))
254 log_std(_("Formatted with tag size %u, internal integrity %s.\n"),
255 params2.tag_size, params2.integrity);
258 r = _wipe_data_device(cd, integrity_key);
260 crypt_safe_free(integrity_key);
261 crypt_safe_free(CONST_CAST(void*)params.journal_integrity_key);
262 crypt_safe_free(CONST_CAST(void*)params.journal_crypt_key);
267 static int action_open(int arg)
269 struct crypt_device *cd = NULL;
270 struct crypt_params_integrity params = {
271 /* in bitmap mode we have to overload these values... */
272 .journal_watermark = opt_integrity_bitmap ? opt_bitmap_sectors_per_bit : opt_journal_watermark,
273 .journal_commit_time = opt_integrity_bitmap ? opt_bitmap_flush_time : opt_journal_commit_time,
274 .buffer_sectors = opt_buffer_sectors,
276 uint32_t activate_flags = 0;
277 char integrity[MAX_CIPHER_LEN], journal_integrity[MAX_CIPHER_LEN], journal_crypt[MAX_CIPHER_LEN];
278 char *integrity_key = NULL;
282 r = crypt_parse_hash_integrity_mode(opt_integrity, integrity);
284 log_err(_("No known integrity specification pattern detected."));
287 params.integrity = integrity;
290 if (opt_journal_integrity) {
291 r = crypt_parse_hash_integrity_mode(opt_journal_integrity, journal_integrity);
293 log_err(_("No known integrity specification pattern detected."));
297 params.journal_integrity = journal_integrity;
300 if (opt_journal_crypt) {
301 r = crypt_parse_hash_integrity_mode(opt_journal_crypt, journal_crypt);
303 log_err(_("No known integrity specification pattern detected."));
306 params.journal_crypt = journal_crypt;
309 if (opt_integrity_nojournal || opt_integrity_bitmap)
310 activate_flags |= CRYPT_ACTIVATE_NO_JOURNAL;
311 if (opt_integrity_recovery)
312 activate_flags |= CRYPT_ACTIVATE_RECOVERY;
313 if (opt_integrity_bitmap)
314 activate_flags |= CRYPT_ACTIVATE_NO_JOURNAL_BITMAP;
316 if (opt_integrity_recalculate)
317 activate_flags |= CRYPT_ACTIVATE_RECALCULATE;
318 if (opt_allow_discards)
319 activate_flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
321 r = _read_keys(&integrity_key, ¶ms);
325 if ((r = crypt_init_data_device(&cd, action_argv[0], opt_data_device)))
328 r = crypt_load(cd, CRYPT_INTEGRITY, ¶ms);
332 r = crypt_activate_by_volume_key(cd, action_argv[1], integrity_key,
333 opt_integrity_key_size, activate_flags);
335 crypt_safe_free(integrity_key);
336 crypt_safe_free(CONST_CAST(void*)params.journal_integrity_key);
337 crypt_safe_free(CONST_CAST(void*)params.journal_crypt_key);
342 static int action_close(int arg)
344 struct crypt_device *cd = NULL;
347 r = crypt_init_by_name(&cd, action_argv[0]);
349 r = crypt_deactivate(cd, action_argv[0]);
355 static int action_status(int arg)
357 crypt_status_info ci;
358 struct crypt_active_device cad;
359 struct crypt_params_integrity ip = {};
360 struct crypt_device *cd = NULL;
362 const char *device, *metadata_device;
365 /* perhaps a path, not a dm device name */
366 if (strchr(action_argv[0], '/'))
369 ci = crypt_status(NULL, action_argv[0]);
376 log_std("%s is inactive.\n", action_argv[0]);
378 log_std("%s/%s is inactive.\n", crypt_get_dir(), action_argv[0]);
384 log_std("%s is active%s.\n", action_argv[0],
385 ci == CRYPT_BUSY ? " and is in use" : "");
387 log_std("%s/%s is active%s.\n", crypt_get_dir(), action_argv[0],
388 ci == CRYPT_BUSY ? " and is in use" : "");
390 r = crypt_init_by_name_and_header(&cd, action_argv[0], NULL);
394 log_std(" type: %s\n", crypt_get_type(cd) ?: "n/a");
396 r = crypt_get_active_device(cd, action_argv[0], &cad);
400 /* Print only INTEGRITY (and LUKS2 with integrity) info */
401 r = crypt_get_integrity_info(cd, &ip);
405 log_std(" tag size: %u\n", ip.tag_size);
406 log_std(" integrity: %s\n", ip.integrity ?: "(none)");
407 device = crypt_get_device_name(cd);
408 metadata_device = crypt_get_metadata_device_name(cd);
409 log_std(" device: %s%s\n", device, metadata_device ? " (detached)" : "");
410 if ((backing_file = crypt_loop_backing_file(device))) {
411 log_std(" loop: %s\n", backing_file);
414 if (metadata_device) {
415 log_std(" metadata device: %s\n", metadata_device);
416 if ((backing_file = crypt_loop_backing_file(metadata_device))) {
417 log_std(" loop: %s\n", backing_file);
421 log_std(" sector size: %u bytes\n", crypt_get_sector_size(cd));
422 log_std(" interleave sectors: %u\n", ip.interleave_sectors);
423 log_std(" size: %" PRIu64 " sectors\n", cad.size);
424 log_std(" mode: %s%s\n",
425 cad.flags & CRYPT_ACTIVATE_READONLY ? "readonly" : "read/write",
426 cad.flags & CRYPT_ACTIVATE_RECOVERY ? " recovery" : "");
427 log_std(" failures: %" PRIu64 "\n",
428 crypt_get_active_integrity_failures(cd, action_argv[0]));
429 if (cad.flags & CRYPT_ACTIVATE_NO_JOURNAL_BITMAP) {
430 log_std(" bitmap 512-byte sectors per bit: %u\n", ip.journal_watermark);
431 log_std(" bitmap flush interval: %u ms\n", ip.journal_commit_time);
432 } if (cad.flags & CRYPT_ACTIVATE_NO_JOURNAL) {
433 log_std(" journal: not active\n");
435 log_std(" journal size: %" PRIu64 " bytes\n", ip.journal_size);
436 log_std(" journal watermark: %u%%\n", ip.journal_watermark);
437 log_std(" journal commit time: %u ms\n", ip.journal_commit_time);
438 if (ip.journal_integrity)
439 log_std(" journal integrity MAC: %s\n", ip.journal_integrity);
440 if (ip.journal_crypt)
441 log_std(" journal encryption: %s\n", ip.journal_crypt);
443 if (cad.flags & (CRYPT_ACTIVATE_ALLOW_DISCARDS))
444 log_std(" flags: %s\n",
445 (cad.flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) ? "discards " : "");
455 static int action_dump(int arg)
457 struct crypt_device *cd = NULL;
458 struct crypt_params_integrity params = {};
461 if ((r = crypt_init(&cd, action_argv[0])))
464 r = crypt_load(cd, CRYPT_INTEGRITY, ¶ms);
472 static struct action_type {
475 int required_action_argc;
476 const char *arg_desc;
479 { "format", action_format, 1, N_("<integrity_device>"),N_("format device") },
480 { "open", action_open, 2, N_("<integrity_device> <name>"),N_("open device as <name>") },
481 { "close", action_close, 1, N_("<name>"),N_("close device (remove mapping)") },
482 { "status", action_status, 1, N_("<name>"),N_("show active device status") },
483 { "dump", action_dump, 1, N_("<integrity_device>"),N_("show on-disk information") },
484 { NULL, NULL, 0, NULL, NULL }
487 static void help(poptContext popt_context,
488 enum poptCallbackReason reason __attribute__((unused)),
489 struct poptOption *key,
490 const char *arg __attribute__((unused)),
491 void *data __attribute__((unused)))
493 struct action_type *action;
495 if (key->shortName == '?') {
496 log_std("%s %s\n", PACKAGE_INTEGRITY, PACKAGE_VERSION);
497 poptPrintHelp(popt_context, stdout, 0);
499 "<action> is one of:\n"));
500 for(action = action_types; action->type; action++)
501 log_std("\t%s %s - %s\n", action->type, _(action->arg_desc), _(action->desc));
503 "<name> is the device to create under %s\n"
504 "<integrity_device> is the device containing data with integrity tags\n"),
507 log_std(_("\nDefault compiled-in dm-integrity parameters:\n"
508 "\tChecksum algorithm: %s\n"), DEFAULT_ALG_NAME);
509 poptFreeContext(popt_context);
511 } else if (key->shortName == 'V') {
512 log_std("%s %s\n", PACKAGE_INTEGRITY, PACKAGE_VERSION);
513 poptFreeContext(popt_context);
516 usage(popt_context, EXIT_SUCCESS, NULL, NULL);
519 static int run_action(struct action_type *action)
523 log_dbg("Running command %s.", action->type);
525 r = action->handler(0);
528 return translate_errno(r);
531 int main(int argc, const char **argv)
533 static const char *null_action_argv[] = {NULL};
534 static struct poptOption popt_help_options[] = {
535 { NULL, '\0', POPT_ARG_CALLBACK, help, 0, NULL, NULL },
536 { "help", '?', POPT_ARG_NONE, NULL, 0, N_("Show this help message"), NULL },
537 { "usage", '\0', POPT_ARG_NONE, NULL, 0, N_("Display brief usage"), NULL },
538 { "version",'V', POPT_ARG_NONE, NULL, 0, N_("Print package version"), NULL },
541 static struct poptOption popt_options[] = {
542 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
543 { "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 0, N_("Shows more detailed error messages"), NULL },
544 { "debug", '\0', POPT_ARG_NONE, &opt_debug, 0, N_("Show debug messages"), NULL },
545 { "batch-mode", 'q', POPT_ARG_NONE, &opt_batch_mode, 0, N_("Do not ask for confirmation"), NULL },
546 { "progress-frequency", '\0', POPT_ARG_INT, &opt_progress_frequency, 0, N_("Progress line update (in seconds)"), N_("secs") },
547 { "no-wipe", '\0', POPT_ARG_NONE, &opt_no_wipe, 0, N_("Do not wipe device after format"), NULL },
549 { "data-device", '\0', POPT_ARG_STRING, &opt_data_device, 0, N_("Path to data device (if separated)"), N_("path") },
551 { "journal-size", 'j', POPT_ARG_STRING,&opt_journal_size_str, 0, N_("Journal size"), N_("bytes") },
552 { "interleave-sectors", '\0', POPT_ARG_INT, &opt_interleave_sectors, 0, N_("Interleave sectors"), N_("SECTORS") },
553 { "journal-watermark", '\0', POPT_ARG_INT, &opt_journal_watermark, 0, N_("Journal watermark"),N_("percent") },
554 { "journal-commit-time",'\0', POPT_ARG_INT, &opt_journal_commit_time,0, N_("Journal commit time"), N_("ms") },
555 { "bitmap-sectors-per-bit",'\0', POPT_ARG_INT,&opt_bitmap_sectors_per_bit, 0, N_("Number of 512-byte sectors per bit (bitmap mode)."), NULL },
556 { "bitmap-flush-time", '\0', POPT_ARG_INT, &opt_bitmap_flush_time, 0, N_("Bitmap mode flush time"), N_("ms") },
557 { "tag-size", 't', POPT_ARG_INT, &opt_tag_size, 0, N_("Tag size (per-sector)"), N_("bytes") },
558 { "sector-size", 's', POPT_ARG_INT, &opt_sector_size, 0, N_("Sector size"), N_("bytes") },
559 { "buffer-sectors", '\0', POPT_ARG_INT, &opt_buffer_sectors, 0, N_("Buffers size"), N_("SECTORS") },
561 { "integrity", 'I', POPT_ARG_STRING, &opt_integrity, 0, N_("Data integrity algorithm"), NULL },
562 { "integrity-key-size", '\0', POPT_ARG_INT, &opt_integrity_key_size, 0, N_("The size of the data integrity key"), N_("BITS") },
563 { "integrity-key-file", '\0', POPT_ARG_STRING, &opt_integrity_key_file, 0, N_("Read the integrity key from a file"), NULL },
565 { "journal-integrity", '\0', POPT_ARG_STRING, &opt_journal_integrity, 0, N_("Journal integrity algorithm"), NULL },
566 { "journal-integrity-key-size",'\0', POPT_ARG_INT, &opt_journal_integrity_key_size,0, N_("The size of the journal integrity key"), N_("BITS") },
567 { "journal-integrity-key-file",'\0', POPT_ARG_STRING, &opt_journal_integrity_key_file,0, N_("Read the journal integrity key from a file"), NULL },
569 { "journal-crypt", '\0', POPT_ARG_STRING, &opt_journal_crypt, 0, N_("Journal encryption algorithm"), NULL },
570 { "journal-crypt-key-size", '\0', POPT_ARG_INT, &opt_journal_crypt_key_size, 0, N_("The size of the journal encryption key"), N_("BITS") },
571 { "journal-crypt-key-file", '\0', POPT_ARG_STRING, &opt_journal_crypt_key_file, 0, N_("Read the journal encryption key from a file"), NULL },
573 { "integrity-no-journal", 'D', POPT_ARG_NONE, &opt_integrity_nojournal, 0, N_("Disable journal for integrity device"), NULL },
574 { "integrity-recovery-mode", 'R', POPT_ARG_NONE, &opt_integrity_recovery, 0, N_("Recovery mode (no journal, no tag checking)"), NULL },
575 { "integrity-bitmap-mode", 'B', POPT_ARG_NONE, &opt_integrity_bitmap, 0, N_("Use bitmap to track changes and disable journal for integrity device"), NULL },
576 { "integrity-recalculate", '\0', POPT_ARG_NONE, &opt_integrity_recalculate, 0, N_("Recalculate initial tags automatically."), NULL },
577 { "integrity-legacy-padding", '\0', POPT_ARG_NONE, &opt_integrity_legacy_padding, 0, N_("Use inefficient legacy padding (old kernels)"), NULL },
579 { "allow-discards", '\0', POPT_ARG_NONE, &opt_allow_discards, 0, N_("Allow discards (aka TRIM) requests for device"), NULL },
582 poptContext popt_context;
583 struct action_type *action;
587 crypt_set_log_callback(NULL, tool_log, NULL);
589 setlocale(LC_ALL, "");
590 bindtextdomain(PACKAGE, LOCALEDIR);
593 popt_context = poptGetContext("integrity", argc, argv, popt_options, 0);
594 poptSetOtherOptionHelp(popt_context,
595 _("[OPTION...] <action> <action-specific>"));
598 while ((r = poptGetNextOpt(popt_context)) >= 0) {
602 usage(popt_context, EXIT_FAILURE, poptStrerror(r),
603 poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
605 if (!(aname = poptGetArg(popt_context)))
606 usage(popt_context, EXIT_FAILURE, _("Argument <action> missing."),
607 poptGetInvocationName(popt_context));
610 action_argv = poptGetArgs(popt_context);
611 /* Make return values of poptGetArgs more consistent in case of remaining argc = 0 */
613 action_argv = null_action_argv;
615 /* Count args, somewhat unnice, change? */
616 while (action_argv[action_argc] != NULL)
620 if (!strcmp(aname, "create") && action_argc > 1) {
621 /* create command had historically switched arguments */
622 if (action_argv[0] && action_argv[1]) {
623 const char *tmp = action_argv[0];
624 action_argv[0] = action_argv[1];
625 action_argv[1] = tmp;
628 } else if (!strcmp(aname, "remove")) {
632 for (action = action_types; action->type; action++)
633 if (strcmp(action->type, aname) == 0)
637 usage(popt_context, EXIT_FAILURE, _("Unknown action."),
638 poptGetInvocationName(popt_context));
640 if (action_argc < action->required_action_argc) {
642 snprintf(buf, 128,_("%s: requires %s as arguments"), action->type, action->arg_desc);
643 usage(popt_context, EXIT_FAILURE, buf,
644 poptGetInvocationName(popt_context));
647 if (opt_integrity_recalculate && strcmp(aname, "open"))
648 usage(popt_context, EXIT_FAILURE,
649 _("Option --integrity-recalculate can be used only for open action."),
650 poptGetInvocationName(popt_context));
652 if (opt_allow_discards && strcmp(aname, "open"))
653 usage(popt_context, EXIT_FAILURE,
654 _("Option --allow-discards is allowed only for open operation."),
655 poptGetInvocationName(popt_context));
657 if (opt_interleave_sectors < 0 || opt_journal_watermark < 0 ||
658 opt_journal_commit_time < 0 || opt_tag_size < 0 ||
659 opt_sector_size < 0 || opt_buffer_sectors < 0 ||
660 opt_integrity_key_size < 0 || opt_journal_integrity_key_size < 0 ||
661 opt_journal_crypt_key_size < 0 || opt_bitmap_flush_time < 0 || opt_bitmap_sectors_per_bit < 0)
662 usage(popt_context, EXIT_FAILURE,
663 _("Negative number for option not permitted."),
664 poptGetInvocationName(popt_context));
666 if (strcmp(aname, "format") && (opt_journal_size_str || opt_interleave_sectors ||
667 opt_sector_size || opt_tag_size || opt_no_wipe ))
668 usage(popt_context, EXIT_FAILURE,
669 _("Options --journal-size, --interleave-sectors, --sector-size, --tag-size"
670 " and --no-wipe can be used only for format action."),
671 poptGetInvocationName(popt_context));
673 if (opt_journal_size_str &&
674 tools_string_to_size(NULL, opt_journal_size_str, &opt_journal_size))
675 usage(popt_context, EXIT_FAILURE, _("Invalid journal size specification."),
676 poptGetInvocationName(popt_context));
678 if ((opt_integrity_key_file && !opt_integrity_key_size) ||
679 (!opt_integrity_key_file && opt_integrity_key_size))
680 usage(popt_context, EXIT_FAILURE, _("Both key file and key size options must be specified."),
681 poptGetInvocationName(popt_context));
682 if (!opt_integrity && opt_integrity_key_file)
683 usage(popt_context, EXIT_FAILURE, _("Integrity algorithm must be specified if integrity key is used."),
684 poptGetInvocationName(popt_context));
686 if ((opt_journal_integrity_key_file && !opt_journal_integrity_key_size) ||
687 (!opt_journal_integrity_key_file && opt_journal_integrity_key_size))
688 usage(popt_context, EXIT_FAILURE, _("Both journal integrity key file and key size options must be specified."),
689 poptGetInvocationName(popt_context));
690 if (!opt_journal_integrity && opt_journal_integrity_key_file)
691 usage(popt_context, EXIT_FAILURE, _("Journal integrity algorithm must be specified if journal integrity key is used."),
692 poptGetInvocationName(popt_context));
694 if ((opt_journal_crypt_key_file && !opt_journal_crypt_key_size) ||
695 (!opt_journal_crypt_key_file && opt_journal_crypt_key_size))
696 usage(popt_context, EXIT_FAILURE, _("Both journal encryption key file and key size options must be specified."),
697 poptGetInvocationName(popt_context));
698 if (!opt_journal_crypt && opt_journal_crypt_key_file)
699 usage(popt_context, EXIT_FAILURE, _("Journal encryption algorithm must be specified if journal encryption key is used."),
700 poptGetInvocationName(popt_context));
702 if (opt_integrity_recovery && opt_integrity_bitmap)
703 usage(popt_context, EXIT_FAILURE, _("Recovery and bitmap mode options are mutually exclusive."),
704 poptGetInvocationName(popt_context));
706 if (opt_integrity_bitmap && (opt_journal_integrity_key_file || opt_journal_crypt || opt_journal_watermark || opt_journal_commit_time))
707 usage(popt_context, EXIT_FAILURE, _("Journal options cannot be used in bitmap mode."),
708 poptGetInvocationName(popt_context));
710 if (!opt_integrity_bitmap && (opt_bitmap_flush_time || opt_bitmap_sectors_per_bit))
711 usage(popt_context, EXIT_FAILURE, _("Bitmap options can be used only in bitmap mode."),
712 poptGetInvocationName(popt_context));
716 crypt_set_debug_level(-1);
717 dbg_version_and_cmd(argc, argv);
720 r = run_action(action);
721 poptFreeContext(popt_context);