Whamcloud - gitweb
libext2fs: automatically enable meta_bg to avoid filling up BG 0
[tools/e2fsprogs.git] / contrib / add_ext4_encrypt.c
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 (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
46                                        EXT4_FEATURE_INCOMPAT_ENCRYPT)) {
47                 fs->super->s_feature_incompat |= EXT4_FEATURE_INCOMPAT_ENCRYPT;
48                 fs->super->s_encrypt_algos[0] =
49                         EXT4_ENCRYPTION_MODE_AES_256_XTS;
50                 fs->super->s_encrypt_algos[1] =
51                         EXT4_ENCRYPTION_MODE_AES_256_CTS;
52                 ext2fs_mark_super_dirty(fs);
53                 printf("Ext4 encryption enabled on %s\n", argv[1]);
54         } else
55                 printf("Ext4 encryption already enabled on %s\n", argv[1]);
56
57         retval = ext2fs_close(fs);
58         if (retval) {
59                 com_err(argv[0], retval, "while trying to close '%s'",
60                         argv[1]);
61                 exit(1);
62         }
63         return (0);
64 }
65