6 #include "libbcachefs/checksum.h"
8 #include "libbcachefs.h"
10 static void unlock_usage(void)
12 puts("bcachefs unlock - unlock an encrypted filesystem so it can be mounted\n"
13 "Usage: bcachefs unlock [OPTION] device\n"
16 " -c Check if a device is encrypted\n"
17 " -h Display this help and exit\n"
18 "Report bugs to <linux-bcache@vger.kernel.org>");
21 int cmd_unlock(int argc, char *argv[])
26 while ((opt = getopt(argc, argv, "ch")) != -1)
37 char *dev = arg_pop();
39 die("Please supply a device");
42 die("Too many arguments");
44 struct bch_opts opts = bch2_opts_empty();
46 opt_set(opts, noexcl, true);
47 opt_set(opts, nochanges, true);
49 struct bch_sb_handle sb;
50 int ret = bch2_read_super(dev, &opts, &sb);
52 die("Error opening %s: %s", dev, strerror(-ret));
54 if (!bch2_sb_is_encrypted(sb.sb))
55 die("%s is not encrypted", dev);
60 char *passphrase = read_passphrase("Enter passphrase: ");
62 bch2_add_key(sb.sb, passphrase);
65 memzero_explicit(passphrase, strlen(passphrase));
70 int cmd_set_passphrase(int argc, char *argv[])
72 struct bch_opts opts = bch2_opts_empty();
76 die("Please supply one or more devices");
78 opt_set(opts, nostart, true);
81 * we use bch2_fs_open() here, instead of just reading the superblock,
82 * to make sure we're opening and updating every component device:
85 c = bch2_fs_open(argv + 1, argc - 1, opts);
87 die("Error opening %s: %s", argv[1], strerror(-PTR_ERR(c)));
89 struct bch_sb_field_crypt *crypt = bch2_sb_get_crypt(c->disk_sb.sb);
91 die("Filesystem does not have encryption enabled");
93 struct bch_encrypted_key new_key;
94 new_key.magic = BCH_KEY_MAGIC;
96 int ret = bch2_decrypt_sb_key(c, crypt, &new_key.key);
98 die("Error getting current key");
100 char *new_passphrase = read_passphrase_twice("Enter new passphrase: ");
101 struct bch_key passphrase_key = derive_passphrase(crypt, new_passphrase);
103 if (bch2_chacha_encrypt_key(&passphrase_key, __bch2_sb_key_nonce(c->disk_sb.sb),
104 &new_key, sizeof(new_key)))
105 die("error encrypting key");
106 crypt->key = new_key;
113 int cmd_remove_passphrase(int argc, char *argv[])
115 struct bch_opts opts = bch2_opts_empty();
119 die("Please supply one or more devices");
121 opt_set(opts, nostart, true);
122 c = bch2_fs_open(argv + 1, argc - 1, opts);
124 die("Error opening %s: %s", argv[1], strerror(-PTR_ERR(c)));
126 struct bch_sb_field_crypt *crypt = bch2_sb_get_crypt(c->disk_sb.sb);
128 die("Filesystem does not have encryption enabled");
130 struct bch_encrypted_key new_key;
131 new_key.magic = BCH_KEY_MAGIC;
133 int ret = bch2_decrypt_sb_key(c, crypt, &new_key.key);
135 die("Error getting current key");
137 crypt->key = new_key;