Debian Codegraph @

pkg/storaged/stratis/stopped-pool.jsx

raw
1 /*
2 * Copyright (C) 2023 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from "cockpit";
7 import React from "react";
8 import client from "../client";
9
10 import { CardHeader, CardBody } from "@patternfly/react-core/dist/esm/components/Card/index.js";
11 import { DescriptionList } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js";
12 import { List, ListItem } from "@patternfly/react-core/dist/esm/components/List/index.js";
13
14 import { VolumeIcon } from "../icons/gnome-icons.jsx";
15 import {
16 StorageCard, StorageDescription, PageTable,
17 new_page, new_card, PAGE_CATEGORY_VIRTUAL,
18 get_crossrefs
19 } from "../pages.jsx";
20 import { dialog_open, PassInput } from "../dialog.jsx";
21 import { std_reply, with_stored_passphrase } from "./utils.jsx";
22 import { get_stored_keydescs } from "./pool";
23
24 import * as python from "python.js";
25 import stratis3_start_pool_py from "./stratis3-start-pool.py";
26
27 const _ = cockpit.gettext;
28
29 /* Starting a pool has to deal with a number of possibilities:
30
31 API REVISION:
32
33 r6: Can only start V1 pools, takes unlock method as argument and
34 passphrase in keyring.
35
36 r8: Can start both V1 and V2 pools, takes passphrase via FD,
37 unlock method determined by whether or not there is a
38 FD.
39
40 METADATA FORMAT:
41
42 V1: Can have at most one passphrase and/or at most one clevis info,
43 which are communicated in the StoppedPools manager property of both
44 the r6 and r8 API.
45
46 V2: Can have zero or more passphrases and clevis infos, a summary of which is
47 communicated in the StoppedPools property.
48 */
49
50 function is_v1_pool(uuid) {
51 if (client.stratis_interface_revision < 8)
52 return true;
53
54 const stopped_info = client.stratis_manager.StoppedPools[uuid];
55 return !stopped_info.metadata_version.v[0] || stopped_info.metadata_version.v[1] == 1;
56 }
57
58 function stratis_r8_manager_start_pool(uuid, slot, passphrase) {
59 const p = python.spawn(
60 stratis3_start_pool_py,
61 [uuid, slot, passphrase ? 0 : "-"],
62 { superuser: "require" });
63 if (passphrase)
64 p.input(passphrase);
65 return p;
66 }
67
68 function get_v1_keydesc(uuid) {
69 const stopped_info = client.stratis_manager.StoppedPools[uuid];
70 const kd = stopped_info.key_description;
71 return kd.v[1].v[1];
72 }
73
74 async function manager_start_pool(uuid, unlock_method, passphrase) {
75 if (client.stratis_interface_revision < 8) {
76 const start = () => {
77 return client.stratis_manager.StartPool(
78 uuid, "uuid",
79 unlock_method ? [true, unlock_method] : [false, ""])
80 .then(std_reply);
81 };
82 if (passphrase) {
83 const keydesc = get_v1_keydesc(uuid);
84 return with_stored_passphrase(client, keydesc, passphrase, start);
85 } else {
86 return start();
87 }
88 } else {
89 return await stratis_r8_manager_start_pool(uuid, unlock_method ? "any" : "-", passphrase);
90 }
91 }
92
93 function start_pool(uuid, show_devs) {
94 const stopped_info = client.stratis_manager.StoppedPools[uuid];
95 const devs = client.stratis_stopped_pool_devnodes(uuid);
96
97 async function prompt_for_passphrase() {
98 if (client.stratis_interface_revision < 8) {
99 const key_desc = get_v1_keydesc(uuid);
100 const stored_keydescs = await get_stored_keydescs();
101 if (stored_keydescs.includes(key_desc))
102 return await client.stratis_manager.StartPool(uuid, "uuid", [true, "keyring"]).then(std_reply);
103 }
104
105 dialog_open({
106 Title: _("Unlock encrypted Stratis pool"),
107 Body: (show_devs &&
108 <>
109 <p>{_("Provide the passphrase for the pool on these block devices:")}</p>
110 <List>{devs.map(d => <ListItem key={d}>{d}</ListItem>)}</List>
111 <br />
112 </>),
113 Fields: [
114 PassInput("passphrase", _("Passphrase"), { })
115 ],
116 Action: {
117 Title: _("Unlock"),
118 action: function(vals) {
119 return manager_start_pool(uuid, "keyring", vals.passphrase);
120 }
121 }
122 });
123 }
124
125 let have_passphrase;
126 let have_clevis;
127
128 if (is_v1_pool(uuid)) {
129 const kd = stopped_info.key_description;
130 have_passphrase = kd && kd.v[0] && kd.v[1].v[0];
131 const ci = stopped_info.clevis_info;
132 have_clevis = ci && ci.v[0] && ci.v[1].v[0];
133 } else {
134 const features = stopped_info.features.v[0] ? stopped_info.features.v[1] : { };
135 const encrypted = features.encryption;
136 have_passphrase = features.key_description_present;
137 have_clevis = features.clevis_present;
138
139 // stratisd 3.8.0 never sets the "key_description_present"
140 // or "clevis_present" flags, so we have to try everything
141 // when the pool is encrypted. (stratisd 3.8.1 and younger
142 // will set at least one of them for encrypted pools.)
143 //
144 if (encrypted && !have_passphrase && !have_clevis)
145 have_passphrase = have_clevis = true;
146 }
147
148 if (!have_passphrase && !have_clevis) {
149 // Not an encrypted pool, just start it
150 return manager_start_pool(uuid, null, null);
151 } else if (have_passphrase && have_clevis) {
152 return manager_start_pool(uuid, "clevis", null).catch(prompt_for_passphrase);
153 } else if (!have_passphrase && have_clevis) {
154 return manager_start_pool(uuid, "clevis", null);
155 } else if (have_passphrase && !have_clevis) {
156 return prompt_for_passphrase();
157 }
158 }
159
160 export function make_stratis_stopped_pool_page(parent, uuid) {
161 if (client.in_anaconda_mode())
162 return;
163
164 const pool_card = new_card({
165 title: _("Stratis pool"),
166 type_extra: _("stopped"),
167 next: null,
168 page_location: ["pool", uuid],
169 page_name: client.stratis_manager.StoppedPools[uuid]?.name?.v || uuid,
170 page_icon: VolumeIcon,
171 page_category: PAGE_CATEGORY_VIRTUAL,
172 component: StoppedStratisPoolCard,
173 props: { uuid },
174 actions: [
175 { title: _("Start"), action: from_menu => start_pool(uuid, from_menu), spinner: true },
176 ],
177 });
178
179 new_page(parent, pool_card);
180 }
181
182 const StoppedStratisPoolCard = ({ card, uuid }) => {
183 return (
184 <StorageCard card={card}>
185 <CardBody>
186 <DescriptionList className="pf-m-horizontal-on-sm">
187 <StorageDescription title={_("UUID")} value={uuid} />
188 </DescriptionList>
189 </CardBody>
190 <CardHeader><strong>{_("Block devices")}</strong></CardHeader>
191 <PageTable
192 emptyCaption={_("No block devices found")}
193 aria-label={_("Stratis block devices")}
194 crossrefs={get_crossrefs(uuid)} />
195 </StorageCard>
196 );
197 };