Home | History | Annotate | Download | only in contrib
      1 /*
      2  * Basic progam to add ext4 encryption to a file system
      3  *
      4  * Copyright 2015, Google, Inc.
      5  *
      6  * %Begin-Header%
      7  * This file may be redistributed under the terms of the GNU Public
      8  * License.
      9  * %End-Header%
     10  */
     11 
     12 #include <stdio.h>
     13 #include <string.h>
     14 #include <unistd.h>
     15 #include <stdlib.h>
     16 #include <time.h>
     17 #include <sys/types.h>
     18 #include <sys/time.h>
     19 
     20 #include <ext2fs/ext2_fs.h>
     21 #include <ext2fs/ext2fs.h>
     22 
     23 int main (int argc, char *argv[])
     24 {
     25 	errcode_t	retval = 0;
     26 	ext2_filsys	fs;
     27 
     28 	setbuf(stdout, NULL);
     29 	setbuf(stderr, NULL);
     30 	initialize_ext2_error_table();
     31 
     32 	if (argc != 2) {
     33 		fprintf(stderr, "%s: Usage <device|filesystem>\n", argv[0]);
     34 		exit(1);
     35 	}
     36 
     37 	retval = ext2fs_open(argv[1], EXT2_FLAG_RW, 0, 0,
     38 			     unix_io_manager, &fs);
     39 
     40 	if (retval) {
     41 		com_err(argv[0], retval, "while trying to open '%s'",
     42 			argv[1]);
     43 		exit(1);
     44 	}
     45 	if (!ext2fs_has_feature_encrypt(fs->super)) {
     46 		ext2fs_set_feature_encrypt(fs->super);
     47 		fs->super->s_encrypt_algos[0] =
     48 			EXT4_ENCRYPTION_MODE_AES_256_XTS;
     49 		fs->super->s_encrypt_algos[1] =
     50 			EXT4_ENCRYPTION_MODE_AES_256_CTS;
     51 		ext2fs_mark_super_dirty(fs);
     52 		printf("Ext4 encryption enabled on %s\n", argv[1]);
     53 	} else
     54 		printf("Ext4 encryption already enabled on %s\n", argv[1]);
     55 
     56 	retval = ext2fs_close(fs);
     57 	if (retval) {
     58 		com_err(argv[0], retval, "while trying to close '%s'",
     59 			argv[1]);
     60 		exit(1);
     61 	}
     62 	return (0);
     63 }
     64 
     65