Debian Codegraph @

pkg/storaged/filesystem/mounting-dialog.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 client from "../client.js";
8
9 import React from "react";
10 import { FormHelperText } from "@patternfly/react-core/dist/esm/components/Form/index.js";
11 import { HelperText, HelperTextItem, } from "@patternfly/react-core/dist/esm/components/HelperText/index.js";
12 import { ExclamationTriangleIcon, InfoCircleIcon } from "@patternfly/react-icons";
13
14 import {
15 encode_filename,
16 parse_options, unparse_options, extract_option, reload_systemd,
17 set_crypto_options, is_mounted_synch,
18 get_active_usage, teardown_active_usage,
19 } from "../utils.js";
20
21 import {
22 dialog_open,
23 TextInput, PassInput, CheckBoxes, SelectOne,
24 TeardownMessage,
25 init_teardown_usage
26 } from "../dialog.jsx";
27 import { init_existing_passphrase, unlock_with_type } from "../crypto/keyslots.jsx";
28 import { initial_tab_options } from "../block/format-dialog.jsx";
29
30 import {
31 is_mounted, get_fstab_config,
32 is_valid_mount_point
33 } from "./utils.jsx";
34
35 const _ = cockpit.gettext;
36
37 export const mount_options = (opt_ro, extra_options, is_visible) => {
38 return CheckBoxes("mount_options", _("Mount options"),
39 {
40 visible: vals => !client.in_anaconda_mode() && (!is_visible || is_visible(vals)),
41 value: {
42 ro: opt_ro,
43 extra: extra_options || false
44 },
45 fields: [
46 {
47 title: _("Mount read only"),
48 tag: "ro",
49 },
50 { title: _("Custom mount options"), tag: "extra", type: "checkboxWithInput" },
51 ]
52 });
53 };
54
55 export const mount_explanation = {
56 local:
57 <FormHelperText>
58 <HelperText>
59 <HelperTextItem>
60 {_("Mounts before services start")}
61 </HelperTextItem>
62 <HelperTextItem>
63 {_("Appropriate for critical mounts, such as /var")}
64 </HelperTextItem>
65 <HelperTextItem icon={<ExclamationTriangleIcon className="ct-icon-exclamation-triangle" />}>
66 {_("Boot fails if filesystem does not mount, preventing remote access")}
67 </HelperTextItem>
68 </HelperText>
69 </FormHelperText>,
70 nofail:
71 <FormHelperText>
72 <HelperText>
73 <HelperTextItem>
74 {_("Mounts in parallel with services")}
75 </HelperTextItem>
76 <HelperTextItem icon={<InfoCircleIcon className="ct-icon-info-circle" />}>
77 {_("Boot still succeeds when filesystem does not mount")}
78 </HelperTextItem>
79 </HelperText>
80 </FormHelperText>,
81 netdev:
82 <FormHelperText>
83 <HelperText>
84 <HelperTextItem>
85 {_("Mounts in parallel with services, but after network is available")}
86 </HelperTextItem>
87 <HelperTextItem icon={<InfoCircleIcon className="ct-icon-info-circle" />}>
88 {_("Boot still succeeds when filesystem does not mount")}
89 </HelperTextItem>
90 </HelperText>
91 </FormHelperText>,
92 never:
93 <FormHelperText>
94 <HelperText>
95 <HelperTextItem>
96 {_("Does not mount during boot")}
97 </HelperTextItem>
98 <HelperTextItem>
99 {_("Useful for mounts that are optional or need interaction (such as passphrases)")}
100 </HelperTextItem>
101 </HelperText>
102 </FormHelperText>,
103 };
104
105 export const at_boot_input = (at_boot, is_visible) => {
106 const init = at_boot || (client.in_anaconda_mode() ? "local" : "nofail");
107 return SelectOne("at_boot", _("At boot"),
108 {
109 visible: vals => !client.in_anaconda_mode() && (!is_visible || is_visible(vals)),
110 value: init,
111 explanation: mount_explanation[init],
112 choices: [
113 {
114 value: "local",
115 title: _("Mount before services start"),
116 },
117 {
118 value: "nofail",
119 title: _("Mount without waiting, ignore failure"),
120 },
121 {
122 value: "netdev",
123 title: _("Mount after network becomes available, ignore failure"),
124 },
125 {
126 value: "never",
127 title: _("Do not mount"),
128 },
129 ]
130 });
131 };
132
133 export function update_at_boot_input(dlg, vals, trigger) {
134 if (trigger == "at_boot")
135 dlg.set_options("at_boot", { explanation: mount_explanation[vals.at_boot] });
136 }
137
138 export function mounting_dialog(client, block, mode, forced_options, subvol) {
139 const block_fsys = client.blocks_fsys[block.path];
140 const [old_config, old_dir, old_opts, old_parents] = get_fstab_config(block, true, subvol);
141 const options = old_config ? old_opts : initial_tab_options(client, block, true);
142
143 const old_dir_for_display = client.strip_mount_point_prefix(old_dir);
144 if (old_dir_for_display === false)
145 return Promise.reject(_("This device can not be used for the installation target."));
146
147 const split_options = parse_options(options);
148 extract_option(split_options, "noauto");
149 const opt_never_auto = extract_option(split_options, "x-cockpit-never-auto");
150 const opt_ro = extract_option(split_options, "ro");
151 const opt_nofail = extract_option(split_options, "nofail");
152 const opt_netdev = extract_option(split_options, "_netdev");
153 if (forced_options)
154 for (const opt of forced_options)
155 extract_option(split_options, opt);
156 const extra_options = unparse_options(split_options);
157
158 const is_filesystem_mounted = is_mounted(client, block, subvol);
159
160 function maybe_update_config(new_dir, new_opts, passphrase, passphrase_type, crypto_unlock_readonly) {
161 let new_config = null;
162 let all_new_opts;
163
164 if (new_opts && old_parents)
165 all_new_opts = new_opts + "," + old_parents;
166 else if (new_opts)
167 all_new_opts = new_opts;
168 else
169 all_new_opts = old_parents;
170
171 if (new_dir != "") {
172 if (new_dir[0] != "/")
173 new_dir = "/" + new_dir;
174 new_config = [
175 "fstab", {
176 fsname: old_config ? old_config[1].fsname : undefined,
177 dir: { t: 'ay', v: encode_filename(new_dir) },
178 type: { t: 'ay', v: encode_filename("auto") },
179 opts: { t: 'ay', v: encode_filename(all_new_opts || "defaults") },
180 freq: { t: 'i', v: 0 },
181 passno: { t: 'i', v: 0 },
182 "track-parents": { t: 'b', v: !old_config }
183 }];
184 }
185
186 function undo() {
187 if (!old_config && new_config)
188 return block.RemoveConfigurationItem(new_config, {});
189 else if (old_config && !new_config)
190 return block.AddConfigurationItem(old_config, {});
191 else if (old_config && new_config && (new_dir != old_dir || new_opts != old_opts)) {
192 return block.UpdateConfigurationItem(new_config, old_config, {});
193 }
194 }
195
196 function get_block_fsys() {
197 if (block_fsys)
198 return Promise.resolve(block_fsys);
199 else
200 return client.wait_for(() => (client.blocks_cleartext[block.path] &&
201 client.blocks_fsys[client.blocks_cleartext[block.path].path]));
202 }
203
204 function maybe_mount() {
205 if (mode == "mount" || (mode == "update" && is_filesystem_mounted)) {
206 return (get_block_fsys()
207 .then(block_fsys => {
208 const block = client.blocks[block_fsys.path];
209 return (client.mount_at(block, new_dir)
210 .catch(error => {
211 // systemd might have mounted the filesystem for us after
212 // unlocking, because fstab told it to. Ignore any error
213 // from mounting in that case. This only happens when this
214 // code runs to fix a inconsistent mount.
215 return (is_mounted_synch(block)
216 .then(mounted_at => {
217 if (mounted_at == new_dir)
218 return;
219 return (undo()
220 .then(() => {
221 if (is_filesystem_mounted)
222 return client.mount_at(block, old_dir);
223 })
224 .catch(ignored_error => {
225 console.warn("Error during undo:", ignored_error);
226 })
227 .then(() => Promise.reject(error)));
228 }));
229 }));
230 }));
231 } else
232 return Promise.resolve();
233 }
234
235 async function maybe_unlock() {
236 if (mode == "mount" || (mode == "update" && is_filesystem_mounted)) {
237 let crypto = client.blocks_crypto[block.path];
238 const backing = client.blocks[block.CryptoBackingDevice];
239
240 if (backing && block.ReadOnly != crypto_unlock_readonly) {
241 // We are working on a open crypto device, but it
242 // has the wrong readonly-ness. Close it so that we can reopen it below.
243 crypto = client.blocks_crypto[backing.path];
244 await crypto.Lock({});
245 }
246
247 if (crypto) {
248 try {
249 await unlock_with_type(client, client.blocks[crypto.path],
250 passphrase, passphrase_type, crypto_unlock_readonly);
251 return await client.wait_for(() => client.blocks_cleartext[crypto.path]);
252 } catch (error) {
253 passphrase_type = null;
254 dlg.set_values({ needs_explicit_passphrase: true });
255 throw error;
256 }
257 }
258 }
259
260 return block;
261 }
262
263 function maybe_lock() {
264 if (mode == "unmount" && !subvol && !client.in_anaconda_mode()) {
265 const crypto_backing = client.blocks[block.CryptoBackingDevice];
266 const crypto_backing_crypto = crypto_backing && client.blocks_crypto[crypto_backing.path];
267 if (crypto_backing_crypto) {
268 return crypto_backing_crypto.Lock({});
269 } else
270 return Promise.resolve();
271 }
272 }
273
274 // We need to reload systemd twice: Once at the beginning so
275 // that it is up to date with whatever is currently in fstab,
276 // and once at the end to make it see our changes. Otherwise
277 // systemd might do some uexpected mounts/unmounts behind our
278 // backs.
279
280 return (reload_systemd()
281 .then(() => teardown_active_usage(client, usage))
282 .then(maybe_unlock)
283 .then(content_block => {
284 if (!old_config && new_config)
285 return (content_block.AddConfigurationItem(new_config, {})
286 .then(maybe_mount));
287 else if (old_config && !new_config)
288 return content_block.RemoveConfigurationItem(old_config, {});
289 else if (old_config && new_config)
290 return (content_block.UpdateConfigurationItem(old_config, new_config, {})
291 .then(maybe_mount));
292 else if (new_config && !is_mounted(client, block))
293 return maybe_mount();
294 })
295 .then(maybe_lock)
296 .then(reload_systemd));
297 }
298
299 let at_boot;
300 if (opt_never_auto)
301 at_boot = "never";
302 else if (opt_netdev)
303 at_boot = "netdev";
304 else if (opt_nofail)
305 at_boot = "nofail";
306 else
307 at_boot = "local";
308
309 let fields = null;
310 if (mode == "mount" || mode == "update") {
311 fields = [
312 TextInput("mount_point", _("Mount point"),
313 {
314 value: old_dir_for_display,
315 validate: val => is_valid_mount_point(client,
316 block,
317 client.add_mount_point_prefix(val),
318 mode == "update" && !is_filesystem_mounted,
319 mode == "update",
320 subvol)
321 }),
322 mount_options(opt_ro, extra_options, null),
323 at_boot_input(at_boot),
324 ];
325
326 fields = fields.concat([
327 PassInput("passphrase", _("Passphrase"),
328 {
329 visible: vals => vals.needs_explicit_passphrase,
330 validate: val => !val.length && _("Passphrase cannot be empty"),
331 })
332 ]);
333 }
334
335 const mode_title = {
336 mount: _("Mount filesystem"),
337 unmount: _("Unmount filesystem $0"),
338 update: _("Mount configuration")
339 };
340
341 const mode_action = {
342 mount: _("Mount"),
343 unmount: _("Unmount"),
344 update: _("Save")
345 };
346
347 function do_unmount() {
348 let opts = [];
349 opts.push("noauto");
350 if (opt_ro)
351 opts.push("ro");
352 if (opt_never_auto)
353 opts.push("x-cockpit-never-auto");
354 if (opt_nofail)
355 opts.push("nofail");
356 if (opt_netdev)
357 opts.push("_netdev");
358 if (forced_options)
359 opts = opts.concat(forced_options);
360 if (extra_options)
361 opts = opts.concat(extra_options);
362 return (maybe_set_crypto_options(null, false, null, null)
363 .then(() => maybe_update_config(old_dir, unparse_options(opts))));
364 }
365
366 let passphrase_type;
367
368 function maybe_set_crypto_options(readonly, auto, nofail, netdev) {
369 if (client.blocks_crypto[block.path]) {
370 return set_crypto_options(block, readonly, auto, nofail, netdev);
371 } else if (client.blocks_crypto[block.CryptoBackingDevice]) {
372 return set_crypto_options(client.blocks[block.CryptoBackingDevice], readonly, auto, nofail, netdev);
373 } else
374 return Promise.resolve();
375 }
376
377 const usage = get_active_usage(client, block.path, null, null, false, subvol);
378
379 function update_explicit_passphrase(vals_ro) {
380 const backing = client.blocks[block.CryptoBackingDevice];
381 let need_passphrase = (block.IdUsage == "crypto" && mode == "mount");
382 if (backing) {
383 // XXX - take subvols into account.
384 if (block.ReadOnly != vals_ro)
385 need_passphrase = true;
386 }
387 dlg.set_values({ needs_explicit_passphrase: need_passphrase && !passphrase_type });
388 }
389
390 const dlg = dialog_open({
391 Title: cockpit.format(mode_title[mode], old_dir_for_display),
392 Fields: fields,
393 Teardown: TeardownMessage(usage, old_dir || true),
394 update: function (dlg, vals, trigger) {
395 update_at_boot_input(dlg, vals, trigger);
396 if (trigger == "mount_options")
397 update_explicit_passphrase(vals.mount_options.ro);
398 },
399 Action: {
400 Title: mode_action[mode],
401 disable_on_error: usage.Teardown,
402 action: function (vals) {
403 if (mode == "unmount") {
404 return do_unmount();
405 } else if (mode == "mount" || mode == "update") {
406 let opts = [];
407 if ((mode == "update" && !is_filesystem_mounted) || vals.at_boot == "never")
408 opts.push("noauto");
409 if (vals.mount_options?.ro)
410 opts.push("ro");
411 if (vals.at_boot == "never")
412 opts.push("x-cockpit-never-auto");
413 if (vals.at_boot == "nofail")
414 opts.push("nofail");
415 if (vals.at_boot == "netdev")
416 opts.push("_netdev");
417 if (forced_options)
418 opts = opts.concat(forced_options);
419 if (vals.mount_options?.extra)
420 opts = opts.concat(parse_options(vals.mount_options.extra));
421 // XXX - take subvols into account.
422 const crypto_unlock_readonly = vals.mount_options?.ro ?? opt_ro;
423 return (maybe_update_config(client.add_mount_point_prefix(vals.mount_point),
424 unparse_options(opts),
425 vals.passphrase,
426 passphrase_type,
427 crypto_unlock_readonly)
428 .then(() => maybe_set_crypto_options(vals.mount_options?.ro,
429 opts.indexOf("noauto") == -1,
430 vals.at_boot == "nofail",
431 vals.at_boot == "netdev")));
432 }
433 }
434 },
435 Inits: [
436 init_teardown_usage(client, usage, old_dir || true),
437 init_existing_passphrase(block, true, type => {
438 passphrase_type = type;
439 update_explicit_passphrase(dlg.get_value("mount_options")?.ro ?? opt_ro);
440 }),
441 ]
442 });
443 }