2 * crypt_reencrypt - crypt utility for offline reencryption
4 * Copyright (C) 2012 Milan Broz All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 /* The code works as follows:
21 * - create backup (detached) headers fo old and new device
22 * - mark original device unusable
23 * - maps two devices, one with old header one with new onto
24 * the _same_ underlying device
25 * - with direct-io reads old device and copy to new device in defined steps
26 * - keps simple off in file (allows restart)
27 * - there is several windows when corruption can happen
30 * dmsetup create x --table "0 $(blockdev --getsz DEV) crypt cipher_null-ecb-null - 0 DEV 0"
32 #define _LARGEFILE64_SOURCE
33 #define _FILE_OFFSET_BITS 64
44 #include <sys/ioctl.h>
50 #include <libcryptsetup.h>
53 #include "cryptsetup.h"
55 static int opt_verbose = 0;
56 static int opt_debug = 0;
57 static const char *opt_cipher = NULL;
58 static const char *opt_hash = NULL;
59 static const char *opt_key_file = NULL;
60 static int opt_iteration_time = 1000;
61 static int opt_batch_mode = 0;
62 static int opt_version_mode = 0;
63 static int opt_random = 0;
64 static int opt_urandom = 0;
65 static int opt_bsize = 4;
66 static int opt_new = 0;
67 static int opt_directio = 0;
68 static int opt_write_log = 0;
69 static const char *opt_new_file = NULL;
71 static const char **action_argv;
73 static volatile int quit = 0;
79 uint64_t device_offset;
80 uint64_t device_shift;
83 enum { FORWARD = 0, BACKWARD = 1 } reencrypt_direction;
85 char header_file_org[PATH_MAX];
86 char header_file_new[PATH_MAX];
87 char log_file[PATH_MAX];
89 char crypt_path_org[PATH_MAX];
90 char crypt_path_new[PATH_MAX];
97 struct timeval start_time, end_time;
100 char MAGIC[] = {'L','U','K','S', 0xba, 0xbe};
101 char NOMAGIC[] = {'L','U','K','S', 0xde, 0xad};
110 __attribute__((format(printf, 5, 6)))
111 static void clogger(struct crypt_device *cd, int level, const char *file,
112 int line, const char *format, ...)
117 va_start(argp, format);
119 if (vasprintf(&target, format, argp) > 0) {
121 crypt_log(cd, level, target);
122 } else if (opt_debug)
123 printf("# %s\n", target);
130 static void _log(int level, const char *msg, void *usrptr __attribute__((unused)))
134 case CRYPT_LOG_NORMAL:
137 case CRYPT_LOG_VERBOSE:
141 case CRYPT_LOG_ERROR:
144 case CRYPT_LOG_DEBUG:
146 printf("# %s\n", msg);
149 fprintf(stderr, "Internal error on logging class for msg: %s", msg);
154 static void _quiet_log(int level, const char *msg, void *usrptr)
156 if (!opt_verbose && (level == CRYPT_LOG_ERROR || level == CRYPT_LOG_NORMAL))
157 level = CRYPT_LOG_VERBOSE;
158 _log(level, msg, usrptr);
161 static void int_handler(int sig __attribute__((__unused__)))
166 static void set_int_block(int block)
168 sigset_t signals_open;
170 sigemptyset(&signals_open);
171 sigaddset(&signals_open, SIGINT);
172 sigaddset(&signals_open, SIGTERM);
173 sigprocmask(block ? SIG_SETMASK : SIG_UNBLOCK, &signals_open, NULL);
176 static void set_int_handler(void)
178 struct sigaction sigaction_open;
180 memset(&sigaction_open, 0, sizeof(struct sigaction));
181 sigaction_open.sa_handler = int_handler;
182 sigaction(SIGINT, &sigaction_open, 0);
183 sigaction(SIGTERM, &sigaction_open, 0);
187 /* The difference in seconds between two times in "timeval" format. */
188 double time_diff(struct timeval start, struct timeval end)
190 return (end.tv_sec - start.tv_sec)
191 + (end.tv_usec - start.tv_usec) / 1E6;
194 static int alignment(int fd)
198 alignment = fpathconf(fd, _PC_REC_XFER_ALIGN);
204 static int device_magic(header_magic set_magic)
207 size_t block_size = 512;
211 devfd = open(rnc.device, O_RDWR | O_DIRECT);
213 return errno == EBUSY ? -EBUSY : -EINVAL;
215 if (posix_memalign((void *)&buf, alignment(devfd), block_size)) {
220 s = read(devfd, buf, block_size);
221 if (s < 0 || s != block_size) {
222 log_verbose(_("Cannot read device %s.\n"), rnc.device);
227 if (set_magic == MAKE_UNUSABLE && !memcmp(buf, MAGIC, MAGIC_L)) {
228 log_dbg("Marking LUKS device %s unusable.", rnc.device);
229 memcpy(buf, NOMAGIC, MAGIC_L);
232 } else if (set_magic == MAKE_USABLE && !memcmp(buf, NOMAGIC, MAGIC_L)) {
233 log_dbg("Marking LUKS device %s usable.", rnc.device);
234 memcpy(buf, MAGIC, MAGIC_L);
236 } else if (set_magic == CHECK_UNUSABLE) {
237 r = memcmp(buf, NOMAGIC, MAGIC_L) ? -EINVAL : 0;
239 rnc.device_uuid = strndup(&buf[0xa8], 40);
245 if (lseek(devfd, 0, SEEK_SET) == -1)
247 s = write(devfd, buf, block_size);
248 if (s < 0 || s != block_size) {
249 log_verbose(_("Cannot write device %s.\n"), rnc.device);
253 log_dbg("LUKS signature check failed for %s.", rnc.device);
256 memset(buf, 0, block_size);
262 static int create_empty_header(const char *new_file, uint64_t size)
267 log_dbg("Creating empty file %s of size %lu.", new_file, (unsigned long)size);
269 if (!(buf = malloc(size)))
271 memset(buf, 0, size);
273 fd = creat(new_file, S_IRUSR|S_IWUSR);
279 if (write(fd, buf, size) < size)
287 static int write_log(void)
289 static char buf[512];
292 //log_dbg("Updating LUKS reencryption log offset %" PRIu64 ".", offset);
293 memset(buf, 0, sizeof(buf));
294 snprintf(buf, sizeof(buf), "# LUKS reencryption log, DO NOT EDIT OR DELETE.\n"
295 "version = %d\nUUID = %s\ndirection = %d\n"
296 "offset = %" PRIu64 "\nshift = %" PRIu64 "\n# EOF\n",
297 1, rnc.device_uuid, rnc.reencrypt_direction,
298 rnc.device_offset, rnc.device_shift);
300 lseek(rnc.log_fd, 0, SEEK_SET);
301 r = write(rnc.log_fd, buf, sizeof(buf));
302 if (r < 0 || r != sizeof(buf))
308 static int parse_line_log(const char *line)
318 if (sscanf(line, "version = %d", &i) == 1) {
320 log_dbg("Log: Unexpected version = %i", i);
323 } else if (sscanf(line, "UUID = %40s", s) == 1) {
324 if (!rnc.device_uuid || strcmp(rnc.device_uuid, s)) {
325 log_dbg("Log: Unexpected UUID %s", s);
328 } else if (sscanf(line, "direction = %d", &i) == 1) {
329 log_dbg("Log: direction = %i", i);
330 rnc.reencrypt_direction = i;
331 } else if (sscanf(line, "offset = %" PRIu64, &u64) == 1) {
332 log_dbg("Log: offset = %" PRIu64, u64);
333 rnc.device_offset = u64;
334 } else if (sscanf(line, "shift = %" PRIu64, &u64) == 1) {
335 log_dbg("Log: shift = %" PRIu64, u64);
336 rnc.device_shift = u64;
343 static int parse_log(void)
345 static char buf[512];
349 s = read(rnc.log_fd, buf, sizeof(buf));
356 end = strchr(start, '\n');
359 if (parse_line_log(start)) {
360 log_err("Wrong log format.\n");
371 static int open_log(void)
376 if(stat(rnc.log_file, &st) < 0) {
377 log_dbg("Creating LUKS reencryption log file %s.", rnc.log_file);
379 // FIXME: move that somewhere else
380 rnc.reencrypt_direction = BACKWARD;
382 flags = opt_directio ? O_RDWR|O_CREAT|O_DIRECT : O_RDWR|O_CREAT;
383 rnc.log_fd = open(rnc.log_file, flags, S_IRUSR|S_IWUSR);
384 if (rnc.log_fd == -1)
389 log_dbg("Log file %s exists, restarting.", rnc.log_file);
390 flags = opt_directio ? O_RDWR|O_DIRECT : O_RDWR;
391 rnc.log_fd = open(rnc.log_file, flags);
392 if (rnc.log_fd == -1)
397 /* Be sure it is correct format */
401 static void close_log(void)
403 log_dbg("Closing LUKS reencryption log file %s.", rnc.log_file);
404 if (rnc.log_fd != -1)
408 static int activate_luks_headers(void)
410 struct crypt_device *cd = NULL, *cd_new = NULL;
413 log_dbg("Activating LUKS devices from headers.");
415 if ((r = crypt_init(&cd, rnc.header_file_org)) ||
416 (r = crypt_load(cd, CRYPT_LUKS1, NULL)) ||
417 (r = crypt_set_data_device(cd, rnc.device)))
420 if ((r = crypt_activate_by_passphrase(cd, rnc.header_file_org,
421 CRYPT_ANY_SLOT, rnc.password, rnc.passwordLen,
422 CRYPT_ACTIVATE_READONLY|CRYPT_ACTIVATE_PRIVATE)) < 0)
425 if ((r = crypt_init(&cd_new, rnc.header_file_new)) ||
426 (r = crypt_load(cd_new, CRYPT_LUKS1, NULL)) ||
427 (r = crypt_set_data_device(cd_new, rnc.device)))
430 if ((r = crypt_activate_by_passphrase(cd_new, rnc.header_file_new,
431 CRYPT_ANY_SLOT, rnc.password, rnc.passwordLen,
432 CRYPT_ACTIVATE_SHARED|CRYPT_ACTIVATE_PRIVATE)) < 0)
440 static int backup_luks_headers(void)
442 struct crypt_device *cd = NULL, *cd_new = NULL;
443 struct crypt_params_luks1 params = {0};
444 char cipher [MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
447 log_dbg("Creating LUKS header backup for device %s.", rnc.device);
448 if ((r = crypt_init(&cd, rnc.device)) ||
449 (r = crypt_load(cd, CRYPT_LUKS1, NULL)))
452 crypt_set_confirm_callback(cd, NULL, NULL);
453 if ((r = crypt_header_backup(cd, CRYPT_LUKS1, rnc.header_file_org)))
456 if ((r = create_empty_header(rnc.header_file_new,
457 crypt_get_data_offset(cd) * 512)))
460 params.hash = opt_hash ?: DEFAULT_LUKS1_HASH;
461 params.data_alignment = crypt_get_data_offset(cd);
462 params.data_device = rnc.device;
464 if ((r = crypt_init(&cd_new, rnc.header_file_new)))
468 crypt_set_rng_type(cd_new, CRYPT_RNG_RANDOM);
469 else if (opt_urandom)
470 crypt_set_rng_type(cd_new, CRYPT_RNG_URANDOM);
472 if (opt_iteration_time)
473 crypt_set_iteration_time(cd_new, opt_iteration_time);
476 r = crypt_parse_name_and_mode(opt_cipher, cipher, NULL, cipher_mode);
478 log_err(_("No known cipher specification pattern detected.\n"));
483 if ((r = crypt_format(cd_new, CRYPT_LUKS1,
484 opt_cipher ? cipher : crypt_get_cipher(cd),
485 opt_cipher ? cipher_mode : crypt_get_cipher_mode(cd),
487 NULL, crypt_get_volume_key_size(cd), ¶ms)))
490 if ((r = crypt_keyslot_add_by_volume_key(cd_new, rnc.keyslot,
491 NULL, 0, rnc.password, rnc.passwordLen)) < 0)
500 static void remove_headers(void)
502 struct crypt_device *cd = NULL;
504 if (crypt_init(&cd, NULL))
506 crypt_set_log_callback(cd, _quiet_log, NULL);
507 (void)crypt_deactivate(cd, rnc.header_file_org);
508 (void)crypt_deactivate(cd, rnc.header_file_new);
512 static int restore_luks_header(const char *backup)
514 struct crypt_device *cd = NULL;
517 r = crypt_init(&cd, rnc.device);
520 crypt_set_confirm_callback(cd, NULL, NULL);
521 r = crypt_header_restore(cd, CRYPT_LUKS1, backup);
528 void print_progress(uint64_t bytes, int final)
530 uint64_t mbytes = bytes / 1024 / 1024;
531 struct timeval now_time;
534 gettimeofday(&now_time, NULL);
535 if (!final && time_diff(rnc.end_time, now_time) < 0.5)
538 rnc.end_time = now_time;
543 tdiff = time_diff(rnc.start_time, rnc.end_time);
547 log_err("\33[2K\rProgress: %5.1f%%, time elapsed %3.1f seconds, %4"
548 PRIu64 " MB written, speed %5.2f MB/s%s",
549 (double)bytes / rnc.device_size * 100,
550 time_diff(rnc.start_time, rnc.end_time),
551 mbytes, (double)(mbytes) / tdiff,
555 static int copy_data_forward(int fd_old, int fd_new, size_t block_size, void *buf, uint64_t *bytes)
559 *bytes = rnc.device_offset;
560 while (!quit && rnc.device_offset < rnc.device_size) {
561 s1 = read(fd_old, buf, block_size);
562 if (s1 < 0 || (s1 != block_size && (rnc.device_offset + s1) != rnc.device_size)) {
563 log_err("Read error, expecting %d, got %d.\n", (int)block_size, (int)s1);
566 s2 = write(fd_new, buf, s1);
568 log_err("Write error, expecting %d, got %d.\n", (int)block_size, (int)s2);
571 rnc.device_offset += s1;
572 if (opt_write_log && write_log() < 0) {
573 log_err("Log write error, some data are perhaps lost.\n");
577 *bytes += (uint64_t)s2;
578 print_progress(*bytes, 0);
581 return quit ? -EAGAIN : 0;
584 static int copy_data_backward(int fd_old, int fd_new, size_t block_size, void *buf, uint64_t *bytes)
586 ssize_t s1, s2, working_block;
587 off64_t working_offset;
589 *bytes = rnc.device_size - rnc.device_offset;
590 while (!quit && rnc.device_offset) {
591 if (rnc.device_offset < block_size) {
593 working_block = rnc.device_offset;
595 working_offset = rnc.device_offset - block_size;
596 working_block = block_size;
599 if (lseek64(fd_old, working_offset, SEEK_SET) < 0 ||
600 lseek64(fd_new, working_offset, SEEK_SET) < 0) {
601 log_err("Cannot seek to device offset.\n");
605 s1 = read(fd_old, buf, working_block);
606 if (s1 < 0 || (s1 != working_block)) {
607 log_err("Read error, expecting %d, got %d.\n", (int)block_size, (int)s1);
610 s2 = write(fd_new, buf, working_block);
612 log_err("Write error, expecting %d, got %d.\n", (int)block_size, (int)s2);
615 rnc.device_offset -= s1;
616 if (opt_write_log && write_log() < 0) {
617 log_err("Log write error, some data are perhaps lost.\n");
621 *bytes += (uint64_t)s2;
622 print_progress(*bytes, 0);
625 return quit ? -EAGAIN : 0;
628 static int copy_data(void)
630 size_t block_size = opt_bsize * 1024 * 1024;
631 int fd_old = -1, fd_new = -1;
636 fd_old = open(rnc.crypt_path_org, O_RDONLY | (opt_directio ? O_DIRECT : 0));
640 fd_new = open(rnc.crypt_path_new, O_WRONLY | (opt_directio ? O_DIRECT : 0));
644 if (lseek(fd_old, rnc.device_offset, SEEK_SET) == -1)
647 if (lseek(fd_new, rnc.device_offset, SEEK_SET) == -1)
651 if (ioctl(fd_old, BLKGETSIZE64, &rnc.device_size) < 0)
654 if (posix_memalign((void *)&buf, alignment(fd_new), block_size)) {
660 // FIXME: all this should be in init
661 if (!rnc.in_progress && rnc.reencrypt_direction == BACKWARD)
662 rnc.device_offset = rnc.device_size;
664 gettimeofday(&rnc.start_time, NULL);
666 if (rnc.reencrypt_direction == FORWARD)
667 r = copy_data_forward(fd_old, fd_new, block_size, buf, &bytes);
669 r = copy_data_backward(fd_old, fd_new, block_size, buf, &bytes);
672 print_progress(bytes, 1);
675 log_err("ERROR during reencryption.\n");
678 log_err("Log write error, ignored.\n");
689 static int initialize_uuid(void)
691 struct crypt_device *cd = NULL;
694 /* Try to load LUKS from device */
695 if ((r = crypt_init(&cd, rnc.device)))
697 crypt_set_log_callback(cd, _quiet_log, NULL);
698 r = crypt_load(cd, CRYPT_LUKS1, NULL);
700 rnc.device_uuid = strdup(crypt_get_uuid(cd));
702 /* Reencryption already in progress - magic header? */
703 r = device_magic(CHECK_UNUSABLE);
709 static int initialize_passphrase(const char *device)
711 struct crypt_device *cd = NULL;
714 if ((r = crypt_init(&cd, device)) ||
715 (r = crypt_load(cd, CRYPT_LUKS1, NULL)) ||
716 (r = crypt_set_data_device(cd, rnc.device)))
719 if ((r = crypt_get_key(_("Enter LUKS passphrase: "),
720 &rnc.password, &rnc.passwordLen,
725 if ((r = crypt_activate_by_passphrase(cd, NULL,
726 CRYPT_ANY_SLOT, rnc.password, rnc.passwordLen, 0) < 0))
738 static int initialize_context(const char *device)
740 log_dbg("Initialising reencryption context.");
744 if (!(rnc.device = strndup(device, PATH_MAX)))
747 if (opt_new_file && !create_uuid()) {
748 log_err("Cannot create fake header.\n");
752 if (initialize_uuid()) {
753 log_err("No header found on device.\n");
757 /* Prepare device names */
758 if (snprintf(rnc.log_file, PATH_MAX,
759 "LUKS-%s.log", rnc.device_uuid) < 0)
761 if (snprintf(rnc.header_file_org, PATH_MAX,
762 "LUKS-%s.org", rnc.device_uuid) < 0)
764 if (snprintf(rnc.header_file_new, PATH_MAX,
765 "LUKS-%s.new", rnc.device_uuid) < 0)
768 /* Paths to encrypted devices */
769 if (snprintf(rnc.crypt_path_org, PATH_MAX,
770 "%s/%s", crypt_get_dir(), rnc.header_file_org) < 0)
772 if (snprintf(rnc.crypt_path_new, PATH_MAX,
773 "%s/%s", crypt_get_dir(), rnc.header_file_new) < 0)
781 static void destroy_context(void)
783 log_dbg("Destroying reencryption context.");
788 if ((rnc.reencrypt_direction == FORWARD &&
789 rnc.device_offset == rnc.device_size) ||
790 rnc.device_offset == 0) {
791 unlink(rnc.log_file);
792 unlink(rnc.header_file_org);
793 unlink(rnc.header_file_new);
796 crypt_safe_free(rnc.password);
799 free(rnc.device_uuid);
802 int run_reencrypt(const char *device)
805 if (initialize_context(device))
808 log_dbg("Running reencryption.");
810 if (!rnc.in_progress) {
811 if ((r = initialize_passphrase(rnc.device)) ||
812 (r = backup_luks_headers()) ||
813 (r = device_magic(MAKE_UNUSABLE)))
816 if ((r = initialize_passphrase(rnc.header_file_org)))
820 if ((r = activate_luks_headers()))
823 if ((r = copy_data()))
826 r = restore_luks_header(rnc.header_file_new);
832 static __attribute__ ((noreturn)) void usage(poptContext popt_context,
833 int exitcode, const char *error,
836 poptPrintUsage(popt_context, stderr, 0);
838 log_err("%s: %s\n", more, error);
839 poptFreeContext(popt_context);
843 static void help(poptContext popt_context,
844 enum poptCallbackReason reason __attribute__((unused)),
845 struct poptOption *key,
846 const char *arg __attribute__((unused)),
847 void *data __attribute__((unused)))
849 usage(popt_context, EXIT_SUCCESS, NULL, NULL);
852 static void _dbg_version_and_cmd(int argc, const char **argv)
856 log_std("# %s %s processing \"", PACKAGE_NAME, PACKAGE_VERSION);
857 for (i = 0; i < argc; i++) {
860 log_std("%s", argv[i]);
865 int main(int argc, const char **argv)
867 static struct poptOption popt_help_options[] = {
868 { NULL, '\0', POPT_ARG_CALLBACK, help, 0, NULL, NULL },
869 { "help", '?', POPT_ARG_NONE, NULL, 0, N_("Show this help message"), NULL },
870 { "usage", '\0', POPT_ARG_NONE, NULL, 0, N_("Display brief usage"), NULL },
873 static struct poptOption popt_options[] = {
874 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
875 { "version", '\0', POPT_ARG_NONE, &opt_version_mode, 0, N_("Print package version"), NULL },
876 { "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 0, N_("Shows more detailed error messages"), NULL },
877 { "debug", '\0', POPT_ARG_NONE, &opt_debug, 0, N_("Show debug messages"), NULL },
878 { "block-size", 'B', POPT_ARG_INT, &opt_bsize, 0, N_("Reencryption block size"), N_("MB") },
879 { "new-header", 'N', POPT_ARG_INT, &opt_new, 0, N_("Create new header, need size on the end of device"), N_("MB") },
880 { "new-crypt", 'f', POPT_ARG_STRING, &opt_new_file, 0, N_("Log suffix for new reencryption file."), NULL },
881 { "cipher", 'c', POPT_ARG_STRING, &opt_cipher, 0, N_("The cipher used to encrypt the disk (see /proc/crypto)"), NULL },
882 { "hash", 'h', POPT_ARG_STRING, &opt_hash, 0, N_("The hash used to create the encryption key from the passphrase"), NULL },
883 { "key-file", 'd', POPT_ARG_STRING, &opt_key_file, 0, N_("Read the key from a file."), NULL },
884 { "iter-time", 'i', POPT_ARG_INT, &opt_iteration_time, 0, N_("PBKDF2 iteration time for LUKS (in ms)"), N_("msecs") },
885 { "batch-mode", 'q', POPT_ARG_NONE, &opt_batch_mode, 0, N_("Do not ask for confirmation"), NULL },
886 { "use-random", '\0', POPT_ARG_NONE, &opt_random, 0, N_("Use /dev/random for generating volume key."), NULL },
887 { "use-urandom", '\0', POPT_ARG_NONE, &opt_urandom, 0, N_("Use /dev/urandom for generating volume key."), NULL },
888 { "use-directio", '\0', POPT_ARG_NONE, &opt_directio, 0, N_("Use direct-io when accesing devices."), NULL },
889 { "write-log", '\0', POPT_ARG_NONE, &opt_write_log, 0, N_("Update log file after every block."), NULL },
892 poptContext popt_context;
895 crypt_set_log_callback(NULL, _log, NULL);
896 log_err("WARNING: this is experimental code, it can completely break your data.\n");
900 setlocale(LC_ALL, "");
901 bindtextdomain(PACKAGE, LOCALEDIR);
904 popt_context = poptGetContext(PACKAGE, argc, argv, popt_options, 0);
905 poptSetOtherOptionHelp(popt_context,
906 N_("[OPTION...] <device>]"));
908 while((r = poptGetNextOpt(popt_context)) > 0) {
914 usage(popt_context, EXIT_FAILURE, poptStrerror(r),
915 poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
916 if (opt_version_mode) {
917 log_std("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
918 poptFreeContext(popt_context);
922 action_argv = poptGetArgs(popt_context);
924 usage(popt_context, EXIT_FAILURE, _("Argument required."),
925 poptGetInvocationName(popt_context));
927 if (opt_random && opt_urandom)
928 usage(popt_context, EXIT_FAILURE, _("Only one of --use-[u]random options is allowed."),
929 poptGetInvocationName(popt_context));
931 if (opt_new && !opt_new_file)
932 usage(popt_context, EXIT_FAILURE, _("You have to use -f with -N."),
933 poptGetInvocationName(popt_context));
937 crypt_set_debug_level(-1);
938 _dbg_version_and_cmd(argc, argv);
941 r = run_reencrypt(action_argv[0]);
943 poptFreeContext(popt_context);
945 /* Translate exit code to simple codes */
947 case 0: r = EXIT_SUCCESS; break;
949 case -EBUSY: r = 5; break;
951 case -ENODEV: r = 4; break;
952 case -ENOMEM: r = 3; break;
953 case -EPERM: r = 2; break;
954 case -EAGAIN: log_err(_("Interrupted by a signal.\n"));
958 default: r = EXIT_FAILURE;