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 #define _LARGEFILE64_SOURCE
21 #define _FILE_OFFSET_BITS 64
22 #define SECTOR_SIZE 512
33 #include <sys/ioctl.h>
40 #include "cryptsetup.h"
42 static int opt_verbose = 0;
43 static int opt_debug = 0;
44 static const char *opt_cipher = NULL;
45 static const char *opt_hash = NULL;
46 static const char *opt_key_file = NULL;
47 static long opt_keyfile_size = 0;
48 static long opt_keyfile_offset = 0;
49 static int opt_iteration_time = 1000;
50 static int opt_batch_mode = 0;
51 static int opt_version_mode = 0;
52 static int opt_random = 0;
53 static int opt_urandom = 0;
54 static int opt_bsize = 4;
55 static int opt_directio = 0;
56 static int opt_fsync = 0;
57 static int opt_write_log = 0;
58 static int opt_tries = 3;
59 static int opt_key_slot = CRYPT_ANY_SLOT;
61 static const char **action_argv;
63 static volatile int quit = 0;
70 uint64_t device_offset;
71 uint64_t device_shift;
74 enum { FORWARD = 0, BACKWARD = 1 } reencrypt_direction;
76 char header_file_org[PATH_MAX];
77 char header_file_new[PATH_MAX];
78 char log_file[PATH_MAX];
80 char crypt_path_org[PATH_MAX];
81 char crypt_path_new[PATH_MAX];
91 struct timeval start_time, end_time;
92 uint64_t restart_bytes;
95 char MAGIC[] = {'L','U','K','S', 0xba, 0xbe};
96 char NOMAGIC[] = {'L','U','K','S', 0xde, 0xad};
105 __attribute__((format(printf, 5, 6)))
106 static void clogger(struct crypt_device *cd, int level, const char *file,
107 int line, const char *format, ...)
112 va_start(argp, format);
114 if (vasprintf(&target, format, argp) > 0) {
116 crypt_log(cd, level, target);
117 } else if (opt_debug)
118 printf("# %s\n", target);
125 static void _log(int level, const char *msg, void *usrptr __attribute__((unused)))
129 case CRYPT_LOG_NORMAL:
132 case CRYPT_LOG_VERBOSE:
136 case CRYPT_LOG_ERROR:
139 case CRYPT_LOG_DEBUG:
141 printf("# %s\n", msg);
144 fprintf(stderr, "Internal error on logging class for msg: %s", msg);
149 static void _quiet_log(int level, const char *msg, void *usrptr)
151 if (!opt_verbose && (level == CRYPT_LOG_ERROR || level == CRYPT_LOG_NORMAL))
152 level = CRYPT_LOG_VERBOSE;
153 _log(level, msg, usrptr);
156 static void int_handler(int sig __attribute__((__unused__)))
161 static void set_int_block(int block)
163 sigset_t signals_open;
165 sigemptyset(&signals_open);
166 sigaddset(&signals_open, SIGINT);
167 sigaddset(&signals_open, SIGTERM);
168 sigprocmask(block ? SIG_SETMASK : SIG_UNBLOCK, &signals_open, NULL);
171 static void set_int_handler(void)
173 struct sigaction sigaction_open;
175 memset(&sigaction_open, 0, sizeof(struct sigaction));
176 sigaction_open.sa_handler = int_handler;
177 sigaction(SIGINT, &sigaction_open, 0);
178 sigaction(SIGTERM, &sigaction_open, 0);
182 /* The difference in seconds between two times in "timeval" format. */
183 static double time_diff(struct timeval start, struct timeval end)
185 return (end.tv_sec - start.tv_sec)
186 + (end.tv_usec - start.tv_usec) / 1E6;
189 static int alignment(int fd)
193 alignment = fpathconf(fd, _PC_REC_XFER_ALIGN);
199 static int device_magic(struct reenc_ctx *rc, header_magic set_magic)
205 devfd = open(rc->device, O_RDWR | O_DIRECT);
207 return errno == EBUSY ? -EBUSY : -EINVAL;
209 if (posix_memalign((void *)&buf, alignment(devfd), SECTOR_SIZE)) {
210 log_err(_("Allocation of aligned memory failed.\n"));
215 s = read(devfd, buf, SECTOR_SIZE);
216 if (s < 0 || s != SECTOR_SIZE) {
217 log_err(_("Cannot read device %s.\n"), rc->device);
222 if (set_magic == MAKE_UNUSABLE && !memcmp(buf, MAGIC, MAGIC_L)) {
223 log_verbose(_("Marking LUKS device %s unusable.\n"), rc->device);
224 memcpy(buf, NOMAGIC, MAGIC_L);
227 } else if (set_magic == MAKE_USABLE && !memcmp(buf, NOMAGIC, MAGIC_L)) {
228 log_verbose(_("Marking LUKS device %s usable.\n"), rc->device);
229 memcpy(buf, MAGIC, MAGIC_L);
231 } else if (set_magic == CHECK_UNUSABLE) {
232 r = memcmp(buf, NOMAGIC, MAGIC_L) ? -EINVAL : 0;
234 rc->device_uuid = strndup(&buf[0xa8], 40);
240 if (lseek(devfd, 0, SEEK_SET) == -1)
242 s = write(devfd, buf, SECTOR_SIZE);
243 if (s < 0 || s != SECTOR_SIZE) {
244 log_err(_("Cannot write device %s.\n"), rc->device);
248 log_dbg("LUKS signature check failed for %s.", rc->device);
251 memset(buf, 0, SECTOR_SIZE);
257 static int create_empty_header(const char *new_file, const char *old_file)
264 if (stat(old_file, &st) == -1 ||
265 (st.st_mode & S_IFMT) != S_IFREG ||
266 (st.st_size > 16 * 1024 * 1024))
270 log_dbg("Creating empty file %s of size %lu.", new_file, (unsigned long)size);
272 if (!(buf = malloc(size)))
274 memset(buf, 0, size);
276 fd = creat(new_file, S_IRUSR|S_IWUSR);
282 if (write(fd, buf, size) < size)
290 static int write_log(struct reenc_ctx *rc)
294 memset(rc->log_buf, 0, SECTOR_SIZE);
295 snprintf(rc->log_buf, SECTOR_SIZE, "# LUKS reencryption log, DO NOT EDIT OR DELETE.\n"
296 "version = %d\nUUID = %s\ndirection = %d\n"
297 "offset = %" PRIu64 "\nshift = %" PRIu64 "\n# EOF\n",
298 1, rc->device_uuid, rc->reencrypt_direction,
299 rc->device_offset, rc->device_shift);
301 lseek(rc->log_fd, 0, SEEK_SET);
302 r = write(rc->log_fd, rc->log_buf, SECTOR_SIZE);
303 if (r < 0 || r != SECTOR_SIZE) {
304 log_err(_("Cannot write reencryption log file.\n"));
311 static int parse_line_log(struct reenc_ctx *rc, const char *line)
317 /* whole line is comment */
321 if (sscanf(line, "version = %d", &i) == 1) {
323 log_dbg("Log: Unexpected version = %i", i);
326 } else if (sscanf(line, "UUID = %40s", s) == 1) {
327 if (!rc->device_uuid || strcmp(rc->device_uuid, s)) {
328 log_dbg("Log: Unexpected UUID %s", s);
331 } else if (sscanf(line, "direction = %d", &i) == 1) {
332 log_dbg("Log: direction = %i", i);
333 rc->reencrypt_direction = i;
334 } else if (sscanf(line, "offset = %" PRIu64, &u64) == 1) {
335 log_dbg("Log: offset = %" PRIu64, u64);
336 rc->device_offset = u64;
337 } else if (sscanf(line, "shift = %" PRIu64, &u64) == 1) {
338 log_dbg("Log: shift = %" PRIu64, u64);
339 rc->device_shift = u64;
346 static int parse_log(struct reenc_ctx *rc)
351 s = read(rc->log_fd, rc->log_buf, SECTOR_SIZE);
353 log_err(_("Cannot read reencryption log file.\n"));
357 rc->log_buf[SECTOR_SIZE - 1] = '\0';
360 end = strchr(start, '\n');
363 if (parse_line_log(rc, start)) {
364 log_err("Wrong log format.\n");
375 static void close_log(struct reenc_ctx *rc)
377 log_dbg("Closing LUKS reencryption log file %s.", rc->log_file);
378 if (rc->log_fd != -1)
384 static int open_log(struct reenc_ctx *rc)
386 int flags, create_new;
389 if (!stat(rc->log_file, &st))
391 else if (errno == ENOENT)
397 log_dbg("Creating LUKS reencryption log file %s.", rc->log_file);
398 flags = opt_directio ? O_RDWR|O_CREAT|O_DIRECT : O_RDWR|O_CREAT;
399 rc->log_fd = open(rc->log_file, flags, S_IRUSR|S_IWUSR);
400 if (rc->log_fd == -1)
403 log_dbg("Log file %s exists, restarting.", rc->log_file);
404 flags = opt_directio ? O_RDWR|O_DIRECT : O_RDWR;
405 rc->log_fd = open(rc->log_file, flags);
406 if (rc->log_fd == -1)
411 if (posix_memalign((void *)&rc->log_buf, alignment(rc->log_fd), SECTOR_SIZE)) {
412 log_err(_("Allocation of aligned memory failed.\n"));
417 if (create_new && write_log(rc) < 0) {
422 /* Be sure it is correct format */
423 return parse_log(rc);
426 static int activate_luks_headers(struct reenc_ctx *rc)
428 struct crypt_device *cd = NULL, *cd_new = NULL;
431 log_dbg("Activating LUKS devices from headers.");
433 if ((r = crypt_init(&cd, rc->header_file_org)) ||
434 (r = crypt_load(cd, CRYPT_LUKS1, NULL)) ||
435 (r = crypt_set_data_device(cd, rc->device)))
438 if ((r = crypt_activate_by_passphrase(cd, rc->header_file_org,
439 opt_key_slot, rc->p[rc->keyslot].password, rc->p[rc->keyslot].passwordLen,
440 CRYPT_ACTIVATE_READONLY|CRYPT_ACTIVATE_PRIVATE)) < 0)
443 if ((r = crypt_init(&cd_new, rc->header_file_new)) ||
444 (r = crypt_load(cd_new, CRYPT_LUKS1, NULL)) ||
445 (r = crypt_set_data_device(cd_new, rc->device)))
448 if ((r = crypt_activate_by_passphrase(cd_new, rc->header_file_new,
449 opt_key_slot, rc->p[rc->keyslot].password, rc->p[rc->keyslot].passwordLen,
450 CRYPT_ACTIVATE_SHARED|CRYPT_ACTIVATE_PRIVATE)) < 0)
457 log_err(_("Activation of temporary devices failed.\n"));
461 static int backup_luks_headers(struct reenc_ctx *rc)
463 struct crypt_device *cd = NULL, *cd_new = NULL;
464 struct crypt_params_luks1 params = {0};
465 char cipher [MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
468 log_dbg("Creating LUKS header backup for device %s.", rc->device);
470 if ((r = crypt_init(&cd, rc->device)) ||
471 (r = crypt_load(cd, CRYPT_LUKS1, NULL)))
474 crypt_set_confirm_callback(cd, NULL, NULL);
475 if ((r = crypt_header_backup(cd, CRYPT_LUKS1, rc->header_file_org)))
477 log_verbose(_("LUKS header backup of device %s created.\n"), rc->device);
479 if ((r = create_empty_header(rc->header_file_new, rc->header_file_org)))
482 params.hash = opt_hash ?: DEFAULT_LUKS1_HASH;
483 params.data_alignment = crypt_get_data_offset(cd);
484 params.data_device = rc->device;
486 if ((r = crypt_init(&cd_new, rc->header_file_new)))
490 crypt_set_rng_type(cd_new, CRYPT_RNG_RANDOM);
491 else if (opt_urandom)
492 crypt_set_rng_type(cd_new, CRYPT_RNG_URANDOM);
494 if (opt_iteration_time)
495 crypt_set_iteration_time(cd_new, opt_iteration_time);
498 r = crypt_parse_name_and_mode(opt_cipher, cipher, NULL, cipher_mode);
500 log_err(_("No known cipher specification pattern detected.\n"));
505 if ((r = crypt_format(cd_new, CRYPT_LUKS1,
506 opt_cipher ? cipher : crypt_get_cipher(cd),
507 opt_cipher ? cipher_mode : crypt_get_cipher_mode(cd),
509 NULL, crypt_get_volume_key_size(cd), ¶ms)))
511 log_verbose(_("New LUKS header for device %s created.\n"), rc->device);
513 for (i = 0; i < MAX_SLOT; i++) {
514 if (!rc->p[i].password)
516 if ((r = crypt_keyslot_add_by_volume_key(cd_new, i,
517 NULL, 0, rc->p[i].password, rc->p[i].passwordLen)) < 0)
519 log_verbose(_("Activated keyslot %i.\n"), r);
526 log_err(_("Creation of LUKS backup headers failed.\n"));
530 static void remove_headers(struct reenc_ctx *rc)
532 struct crypt_device *cd = NULL;
534 log_dbg("Removing headers.");
536 if (crypt_init(&cd, NULL))
538 crypt_set_log_callback(cd, _quiet_log, NULL);
539 (void)crypt_deactivate(cd, rc->header_file_org);
540 (void)crypt_deactivate(cd, rc->header_file_new);
544 static int restore_luks_header(struct reenc_ctx *rc)
546 struct crypt_device *cd = NULL;
549 log_dbg("Restoring header for %s from %s.", rc->device, rc->header_file_new);
551 r = crypt_init(&cd, rc->device);
553 crypt_set_confirm_callback(cd, NULL, NULL);
554 r = crypt_header_restore(cd, CRYPT_LUKS1, rc->header_file_new);
559 log_err(_("Cannot restore LUKS header on device %s.\n"), rc->device);
561 log_verbose(_("LUKS header on device %s restored.\n"), rc->device);
565 static void print_progress(struct reenc_ctx *rc, uint64_t bytes, int final)
567 unsigned long long mbytes, eta;
568 struct timeval now_time;
571 gettimeofday(&now_time, NULL);
572 if (!final && time_diff(rc->end_time, now_time) < 0.5)
575 rc->end_time = now_time;
580 tdiff = time_diff(rc->start_time, rc->end_time);
584 mbytes = (bytes - rc->restart_bytes) / 1024 / 1024;
585 mib = (double)(mbytes) / tdiff;
589 eta = (unsigned long long)(rc->device_size / 1024 / 1024 / mib - tdiff);
591 /* vt100 code clear line */
593 log_err(_("Progress: %5.1f%%, ETA %02llu:%02llu, "
594 "%4llu MiB written, speed %5.1f MiB/s%s"),
595 (double)bytes / rc->device_size * 100,
596 eta / 60, eta % 60, mbytes, mib,
600 static int copy_data_forward(struct reenc_ctx *rc, int fd_old, int fd_new,
601 size_t block_size, void *buf, uint64_t *bytes)
605 log_dbg("Reencrypting in forward direction.");
607 if (lseek64(fd_old, rc->device_offset, SEEK_SET) < 0 ||
608 lseek64(fd_new, rc->device_offset, SEEK_SET) < 0) {
609 log_err(_("Cannot seek to device offset.\n"));
613 rc->restart_bytes = *bytes = rc->device_offset;
615 if (write_log(rc) < 0)
618 while (!quit && rc->device_offset < rc->device_size) {
619 s1 = read(fd_old, buf, block_size);
620 if (s1 < 0 || ((size_t)s1 != block_size &&
621 (rc->device_offset + s1) != rc->device_size)) {
622 log_dbg("Read error, expecting %d, got %d.",
623 (int)block_size, (int)s1);
627 s2 = write(fd_new, buf, s1);
629 log_dbg("Write error, expecting %d, got %d.",
630 (int)block_size, (int)s2);
634 rc->device_offset += s1;
635 if (opt_write_log && write_log(rc) < 0)
638 if (opt_fsync && fsync(fd_new) < 0) {
639 log_dbg("Write error, fsync.");
643 *bytes += (uint64_t)s2;
644 print_progress(rc, *bytes, 0);
647 return quit ? -EAGAIN : 0;
650 static int copy_data_backward(struct reenc_ctx *rc, int fd_old, int fd_new,
651 size_t block_size, void *buf, uint64_t *bytes)
653 ssize_t s1, s2, working_block;
654 off64_t working_offset;
656 log_dbg("Reencrypting in backward direction.");
658 if (!rc->in_progress) {
659 rc->device_offset = rc->device_size;
660 rc->restart_bytes = 0;
663 rc->restart_bytes = rc->device_size - rc->device_offset;
664 *bytes = rc->restart_bytes;
667 if (write_log(rc) < 0)
670 while (!quit && rc->device_offset) {
671 if (rc->device_offset < block_size) {
673 working_block = rc->device_offset;
675 working_offset = rc->device_offset - block_size;
676 working_block = block_size;
679 if (lseek64(fd_old, working_offset, SEEK_SET) < 0 ||
680 lseek64(fd_new, working_offset, SEEK_SET) < 0) {
681 log_err(_("Cannot seek to device offset.\n"));
685 s1 = read(fd_old, buf, working_block);
686 if (s1 < 0 || (s1 != working_block)) {
687 log_dbg("Read error, expecting %d, got %d.",
688 (int)block_size, (int)s1);
692 s2 = write(fd_new, buf, working_block);
694 log_dbg("Write error, expecting %d, got %d.",
695 (int)block_size, (int)s2);
699 rc->device_offset -= s1;
700 if (opt_write_log && write_log(rc) < 0)
703 if (opt_fsync && fsync(fd_new) < 0) {
704 log_dbg("Write error, fsync.");
708 *bytes += (uint64_t)s2;
709 print_progress(rc, *bytes, 0);
712 return quit ? -EAGAIN : 0;
715 static int copy_data(struct reenc_ctx *rc)
717 size_t block_size = opt_bsize * 1024 * 1024;
718 int fd_old = -1, fd_new = -1;
723 log_dbg("Data copy preparation.");
725 fd_old = open(rc->crypt_path_org, O_RDONLY | (opt_directio ? O_DIRECT : 0));
727 log_err(_("Cannot open temporary LUKS header file.\n"));
731 fd_new = open(rc->crypt_path_new, O_WRONLY | (opt_directio ? O_DIRECT : 0));
733 log_err(_("Cannot open temporary LUKS header file.\n"));
738 if (ioctl(fd_old, BLKGETSIZE64, &rc->device_size) < 0) {
739 log_err(_("Cannot get device size.\n"));
743 if (posix_memalign((void *)&buf, alignment(fd_new), block_size)) {
744 log_err(_("Allocation of aligned memory failed.\n"));
750 gettimeofday(&rc->start_time, NULL);
752 if (rc->reencrypt_direction == FORWARD)
753 r = copy_data_forward(rc, fd_old, fd_new, block_size, buf, &bytes);
755 r = copy_data_backward(rc, fd_old, fd_new, block_size, buf, &bytes);
758 print_progress(rc, bytes, 1);
761 log_err(_("Interrupted by a signal.\n"));
763 log_err(_("IO error during reencryption.\n"));
775 static int initialize_uuid(struct reenc_ctx *rc)
777 struct crypt_device *cd = NULL;
780 log_dbg("Initialising UUID.");
782 /* Try to load LUKS from device */
783 if ((r = crypt_init(&cd, rc->device)))
785 crypt_set_log_callback(cd, _quiet_log, NULL);
786 r = crypt_load(cd, CRYPT_LUKS1, NULL);
788 rc->device_uuid = strdup(crypt_get_uuid(cd));
790 /* Reencryption already in progress - magic header? */
791 r = device_magic(rc, CHECK_UNUSABLE);
797 static int init_passphrase1(struct reenc_ctx *rc, struct crypt_device *cd,
798 const char *msg, int slot_check)
800 int r = -EINVAL, slot, retry_count;
802 slot = (slot_check == CRYPT_ANY_SLOT) ? 0 : slot_check;
804 retry_count = opt_tries ?: 1;
805 while (retry_count--) {
806 r = crypt_get_key(msg, &rc->p[slot].password,
807 &rc->p[slot].passwordLen,
808 0, 0, NULL /*opt_key_file*/,
813 r = crypt_activate_by_passphrase(cd, NULL, slot_check,
814 rc->p[slot].password, rc->p[slot].passwordLen, 0);
817 crypt_safe_free(rc->p[slot].password);
818 rc->p[slot].password = NULL;
819 rc->p[slot].passwordLen = 0;
821 if (r < 0 && r != -EPERM)
827 log_err(_("No key available with this passphrase.\n"));
832 static int init_keyfile(struct reenc_ctx *rc, struct crypt_device *cd, int slot_check)
836 slot = (slot_check == CRYPT_ANY_SLOT) ? 0 : slot_check;
837 r = crypt_get_key(NULL, &rc->p[slot].password, &rc->p[slot].passwordLen,
838 opt_keyfile_offset, opt_keyfile_size, opt_key_file, 0, 0, cd);
842 r = crypt_activate_by_passphrase(cd, NULL, slot_check,
843 rc->p[slot].password, rc->p[slot].passwordLen, 0);
846 * Allow keyslot only if it is last slot or if user explicitly
847 * specify whch slot to use (IOW others will be disabled).
849 if (r >= 0 && opt_key_slot == CRYPT_ANY_SLOT &&
850 crypt_keyslot_status(cd, r) != CRYPT_SLOT_ACTIVE_LAST) {
851 log_err(_("Key file can be used only with --key-slot or with "
852 "exactly one key slot active.\n"));
857 crypt_safe_free(rc->p[slot].password);
858 rc->p[slot].password = NULL;
859 rc->p[slot].passwordLen = 0;
861 log_err(_("No key available with this passphrase.\n"));
869 static int initialize_passphrase(struct reenc_ctx *rc, const char *device)
871 struct crypt_device *cd = NULL;
872 crypt_keyslot_info ki;
876 log_dbg("Passhrases initialization.");
878 if ((r = crypt_init(&cd, device)) ||
879 (r = crypt_load(cd, CRYPT_LUKS1, NULL)) ||
880 (r = crypt_set_data_device(cd, rc->device))) {
886 r = init_keyfile(rc, cd, opt_key_slot);
887 } else if (rc->in_progress) {
888 r = init_passphrase1(rc, cd, _("Enter any LUKS passphrase: "), CRYPT_ANY_SLOT);
889 } else for (i = 0; i < MAX_SLOT; i++) {
890 ki = crypt_keyslot_status(cd, i);
891 if (ki != CRYPT_SLOT_ACTIVE && ki != CRYPT_SLOT_ACTIVE_LAST)
894 snprintf(msg, sizeof(msg), _("Enter LUKS passphrase for key slot %u: "), i);
895 r = init_passphrase1(rc, cd, msg, i);
901 return r > 0 ? 0 : r;
904 static int initialize_context(struct reenc_ctx *rc, const char *device)
906 log_dbg("Initialising reencryption context.");
910 if (!(rc->device = strndup(device, PATH_MAX)))
913 if (initialize_uuid(rc)) {
914 log_err(_("Device %s is not a valid LUKS device.\n"), device);
918 /* Prepare device names */
919 if (snprintf(rc->log_file, PATH_MAX,
920 "LUKS-%s.log", rc->device_uuid) < 0)
922 if (snprintf(rc->header_file_org, PATH_MAX,
923 "LUKS-%s.org", rc->device_uuid) < 0)
925 if (snprintf(rc->header_file_new, PATH_MAX,
926 "LUKS-%s.new", rc->device_uuid) < 0)
929 /* Paths to encrypted devices */
930 if (snprintf(rc->crypt_path_org, PATH_MAX,
931 "%s/%s", crypt_get_dir(), rc->header_file_org) < 0)
933 if (snprintf(rc->crypt_path_new, PATH_MAX,
934 "%s/%s", crypt_get_dir(), rc->header_file_new) < 0)
939 if (open_log(rc) < 0) {
940 log_err(_("Cannot open reencryption log file.\n"));
944 if (!rc->in_progress) {
946 rc->reencrypt_direction = FORWARD;
948 rc->reencrypt_direction = BACKWARD;
949 rc->device_offset = (uint64_t)~0;
956 static void destroy_context(struct reenc_ctx *rc)
960 log_dbg("Destroying reencryption context.");
965 if ((rc->reencrypt_direction == FORWARD &&
966 rc->device_offset == rc->device_size) ||
967 rc->device_offset == 0) {
968 unlink(rc->log_file);
969 unlink(rc->header_file_org);
970 unlink(rc->header_file_new);
973 for (i = 0; i < MAX_SLOT; i++)
974 crypt_safe_free(rc->p[i].password);
977 free(rc->device_uuid);
980 static int run_reencrypt(const char *device)
983 struct reenc_ctx rc = {};
985 if (initialize_context(&rc, device))
988 log_dbg("Running reencryption.");
990 if (!rc.in_progress) {
991 if ((r = initialize_passphrase(&rc, rc.device)) ||
992 (r = backup_luks_headers(&rc)) ||
993 (r = device_magic(&rc, MAKE_UNUSABLE)))
996 if ((r = initialize_passphrase(&rc, rc.header_file_new)))
1000 if ((r = activate_luks_headers(&rc)))
1003 if ((r = copy_data(&rc)))
1006 r = restore_luks_header(&rc);
1008 destroy_context(&rc);
1012 static __attribute__ ((noreturn)) void usage(poptContext popt_context,
1013 int exitcode, const char *error,
1016 poptPrintUsage(popt_context, stderr, 0);
1018 log_err("%s: %s\n", more, error);
1019 poptFreeContext(popt_context);
1023 static void help(poptContext popt_context,
1024 enum poptCallbackReason reason __attribute__((unused)),
1025 struct poptOption *key,
1026 const char *arg __attribute__((unused)),
1027 void *data __attribute__((unused)))
1029 usage(popt_context, EXIT_SUCCESS, NULL, NULL);
1032 static void _dbg_version_and_cmd(int argc, const char **argv)
1036 log_std("# %s %s processing \"", PACKAGE_NAME, PACKAGE_VERSION);
1037 for (i = 0; i < argc; i++) {
1040 log_std("%s", argv[i]);
1045 int main(int argc, const char **argv)
1047 static struct poptOption popt_help_options[] = {
1048 { NULL, '\0', POPT_ARG_CALLBACK, help, 0, NULL, NULL },
1049 { "help", '?', POPT_ARG_NONE, NULL, 0, N_("Show this help message"), NULL },
1050 { "usage", '\0', POPT_ARG_NONE, NULL, 0, N_("Display brief usage"), NULL },
1053 static struct poptOption popt_options[] = {
1054 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
1055 { "version", '\0', POPT_ARG_NONE, &opt_version_mode, 0, N_("Print package version"), NULL },
1056 { "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 0, N_("Shows more detailed error messages"), NULL },
1057 { "debug", '\0', POPT_ARG_NONE, &opt_debug, 0, N_("Show debug messages"), NULL },
1058 { "block-size", 'B', POPT_ARG_INT, &opt_bsize, 0, N_("Reencryption block size"), N_("MiB") },
1059 { "cipher", 'c', POPT_ARG_STRING, &opt_cipher, 0, N_("The cipher used to encrypt the disk (see /proc/crypto)"), NULL },
1060 { "hash", 'h', POPT_ARG_STRING, &opt_hash, 0, N_("The hash used to create the encryption key from the passphrase"), NULL },
1061 { "key-file", 'd', POPT_ARG_STRING, &opt_key_file, 0, N_("Read the key from a file."), NULL },
1062 { "iter-time", 'i', POPT_ARG_INT, &opt_iteration_time, 0, N_("PBKDF2 iteration time for LUKS (in ms)"), N_("msecs") },
1063 { "batch-mode", 'q', POPT_ARG_NONE, &opt_batch_mode, 0, N_("Do not ask for confirmation"), NULL },
1064 { "tries", 'T', POPT_ARG_INT, &opt_tries, 0, N_("How often the input of the passphrase can be retried"), NULL },
1065 { "use-random", '\0', POPT_ARG_NONE, &opt_random, 0, N_("Use /dev/random for generating volume key."), NULL },
1066 { "use-urandom", '\0', POPT_ARG_NONE, &opt_urandom, 0, N_("Use /dev/urandom for generating volume key."), NULL },
1067 { "use-directio", '\0', POPT_ARG_NONE, &opt_directio, 0, N_("Use direct-io when accesing devices."), NULL },
1068 { "use-fsync", '\0', POPT_ARG_NONE, &opt_fsync, 0, N_("Use fsync after each block."), NULL },
1069 { "write-log", '\0', POPT_ARG_NONE, &opt_write_log, 0, N_("Update log file after every block."), NULL },
1070 { "key-slot", 'S', POPT_ARG_INT, &opt_key_slot, 0, N_("Use only this slot (others will be disabled)."), NULL },
1071 { "keyfile-offset", '\0', POPT_ARG_LONG, &opt_keyfile_offset, 0, N_("Number of bytes to skip in keyfile"), N_("bytes") },
1072 { "keyfile-size", 'l', POPT_ARG_LONG, &opt_keyfile_size, 0, N_("Limits the read from keyfile"), N_("bytes") },
1075 poptContext popt_context;
1078 crypt_set_log_callback(NULL, _log, NULL);
1082 setlocale(LC_ALL, "");
1083 bindtextdomain(PACKAGE, LOCALEDIR);
1084 textdomain(PACKAGE);
1086 popt_context = poptGetContext(PACKAGE, argc, argv, popt_options, 0);
1087 poptSetOtherOptionHelp(popt_context,
1088 N_("[OPTION...] <device>]"));
1090 while((r = poptGetNextOpt(popt_context)) > 0) {
1096 usage(popt_context, EXIT_FAILURE, poptStrerror(r),
1097 poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
1098 if (opt_version_mode) {
1099 log_std("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1100 poptFreeContext(popt_context);
1104 if (!opt_batch_mode)
1105 log_err(_("WARNING: this is experimental code, it can completely break your data.\n"));
1107 action_argv = poptGetArgs(popt_context);
1109 usage(popt_context, EXIT_FAILURE, _("Argument required."),
1110 poptGetInvocationName(popt_context));
1112 if (opt_random && opt_urandom)
1113 usage(popt_context, EXIT_FAILURE, _("Only one of --use-[u]random options is allowed."),
1114 poptGetInvocationName(popt_context));
1116 if (opt_bsize < 1 || opt_bsize > 64)
1117 usage(popt_context, EXIT_FAILURE,
1118 _("Only values between 1MiB and 64 MiB allowed for reencryption block size."),
1119 poptGetInvocationName(popt_context));
1123 crypt_set_debug_level(-1);
1124 _dbg_version_and_cmd(argc, argv);
1127 r = run_reencrypt(action_argv[0]);
1129 poptFreeContext(popt_context);
1131 /* Translate exit code to simple codes */
1133 case 0: r = EXIT_SUCCESS; break;
1135 case -EBUSY: r = 5; break;
1137 case -ENODEV: r = 4; break;
1138 case -ENOMEM: r = 3; break;
1139 case -EPERM: r = 2; break;
1144 default: r = EXIT_FAILURE;