Debian Codegraph @

pkg/storaged/filesystem/mismounting.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.js";
9
10 import { Alert } from "@patternfly/react-core/dist/esm/components/Alert/index.js";
11
12 import {
13 encode_filename,
14 parse_options, unparse_options, extract_option, reload_systemd,
15 set_crypto_auto_option, get_mount_points,
16 BTRFS_TOOL_MOUNT_PATH
17 } from "../utils.js";
18 import { StorageButton } from "../storage-controls.jsx";
19
20 import { mounting_dialog } from "./mounting-dialog.jsx";
21 import { get_cryptobacking_noauto } from "./utils.jsx";
22
23 const _ = cockpit.gettext;
24
25 export function check_mismounted_fsys(backing_block, content_block, fstab_config, subvol) {
26 if (client.in_anaconda_mode())
27 return;
28
29 const block_fsys = content_block && client.blocks_fsys[content_block.path];
30 const [, dir, opts] = fstab_config;
31
32 if (!(block_fsys || dir))
33 return;
34
35 function ignore_mount(m) {
36 // We don't complain about the rootfs, it's probably
37 // configured somewhere else, like in the bootloader.
38 if (m == "/")
39 return true;
40
41 // This is the mount point used for monitoring btrfs filesystems.
42 if (m.startsWith(BTRFS_TOOL_MOUNT_PATH))
43 return true;
44
45 return false;
46 }
47
48 const mounted_at = get_mount_points(client, block_fsys, subvol);
49 const split_options = parse_options(opts);
50 const opt_noauto = extract_option(split_options, "noauto");
51 const opt_noauto_intent = extract_option(split_options, "x-cockpit-never-auto");
52 const opt_systemd_automount = split_options.indexOf("x-systemd.automount") >= 0;
53 const is_mounted = mounted_at.indexOf(dir) >= 0;
54 const other_mounts = mounted_at.filter(m => m != dir && !ignore_mount(m));
55 const crypto_backing_noauto = get_cryptobacking_noauto(client, backing_block);
56
57 let type;
58 if (dir) {
59 if (!is_mounted && other_mounts.length > 0) {
60 if (!opt_noauto)
61 type = "change-mount-on-boot";
62 else
63 type = "mounted-no-config";
64 } else if (crypto_backing_noauto && !opt_noauto)
65 type = "locked-on-boot-mount";
66 else if (!is_mounted && !opt_noauto)
67 type = "mount-on-boot";
68 else if (is_mounted && opt_noauto && !opt_noauto_intent && !opt_systemd_automount)
69 type = "no-mount-on-boot";
70 } else if (other_mounts.length > 0) {
71 type = "mounted-no-config";
72 }
73
74 if (type)
75 return { warning: "mismounted-fsys", type, other: other_mounts[0] };
76 }
77
78 export const MismountAlert = ({ warning, fstab_config, forced_options, backing_block, content_block, subvol }) => {
79 if (!warning)
80 return null;
81
82 const { type, other } = warning;
83 const [old_config, old_dir, old_opts, old_parents] = fstab_config;
84 const split_options = parse_options(old_opts);
85 extract_option(split_options, "noauto");
86 const opt_ro = extract_option(split_options, "ro");
87 const opt_nofail = extract_option(split_options, "nofail");
88 const opt_netdev = extract_option(split_options, "_netdev");
89 const split_options_for_fix_config = split_options.slice();
90 if (forced_options)
91 for (const opt of forced_options)
92 extract_option(split_options, opt);
93
94 function fix_config() {
95 let opts = [];
96 if (type == "mount-on-boot")
97 opts.push("noauto");
98 if (type == "locked-on-boot-mount") {
99 opts.push("noauto");
100 opts.push("x-cockpit-never-auto");
101 }
102 if (opt_ro)
103 opts.push("ro");
104 if (opt_nofail)
105 opts.push("nofail");
106 if (opt_netdev)
107 opts.push("_netdev");
108 if (subvol) {
109 opts.push(`subvol=${subvol.pathname}`);
110 }
111
112 // Add the forced options, but only to new entries. We
113 // don't want to modify existing entries beyond what we
114 // say on the button.
115 if (!old_config && forced_options)
116 opts = opts.concat(forced_options);
117
118 const new_opts = unparse_options(opts.concat(split_options_for_fix_config));
119 let all_new_opts;
120 if (new_opts && old_parents)
121 all_new_opts = new_opts + "," + old_parents;
122 else if (new_opts)
123 all_new_opts = new_opts;
124 else
125 all_new_opts = old_parents;
126
127 let new_dir = old_dir;
128 if (type == "change-mount-on-boot" || type == "mounted-no-config")
129 new_dir = other;
130
131 const new_config = [
132 "fstab", {
133 fsname: old_config ? old_config[1].fsname : undefined,
134 dir: { t: 'ay', v: encode_filename(new_dir) },
135 type: { t: 'ay', v: encode_filename("auto") },
136 opts: { t: 'ay', v: encode_filename(all_new_opts || "defaults") },
137 freq: { t: 'i', v: 0 },
138 passno: { t: 'i', v: 0 },
139 "track-parents": { t: 'b', v: !old_config }
140 }];
141
142 function fixup_crypto_backing() {
143 if (!backing_block)
144 return;
145 if (type == "no-mount-on-boot")
146 return set_crypto_auto_option(backing_block, true);
147 if (type == "locked-on-boot-mount")
148 return set_crypto_auto_option(backing_block, false);
149 }
150
151 function fixup_fsys() {
152 if (old_config)
153 return backing_block.UpdateConfigurationItem(old_config, new_config, {}).then(reload_systemd);
154 else
155 return backing_block.AddConfigurationItem(new_config, {}).then(reload_systemd);
156 }
157
158 return fixup_fsys().then(fixup_crypto_backing);
159 }
160
161 function fix_mount() {
162 const crypto_backing_crypto = client.blocks_crypto[backing_block.path];
163
164 function do_mount() {
165 if (!content_block)
166 mounting_dialog(client, backing_block, "mount", forced_options);
167 else
168 return client.mount_at(content_block, old_dir);
169 }
170
171 function do_unmount(dir) {
172 return client.unmount_at(dir)
173 .then(() => {
174 if (backing_block != content_block)
175 return crypto_backing_crypto.Lock({});
176 });
177 }
178
179 if (type == "change-mount-on-boot")
180 return client.unmount_at(other).then(() => client.mount_at(content_block, old_dir));
181 else if (type == "mount-on-boot")
182 return do_mount();
183 else if (type == "no-mount-on-boot")
184 return do_unmount(old_dir);
185 else if (type == "mounted-no-config")
186 return do_unmount(other);
187 else if (type == "locked-on-boot-mount") {
188 if (backing_block != content_block)
189 return set_crypto_auto_option(backing_block, true);
190 }
191 }
192
193 let text;
194 let fix_config_text;
195 let fix_mount_text;
196
197 if (type == "change-mount-on-boot") {
198 text = cockpit.format(_("The filesystem is currently mounted on $0 but will be mounted on $1 on the next boot."), other, old_dir);
199 fix_config_text = cockpit.format(_("Mount automatically on $0 on boot"), other);
200 fix_mount_text = cockpit.format(_("Mount on $0 now"), old_dir);
201 } else if (type == "mount-on-boot") {
202 text = _("The filesystem is currently not mounted but will be mounted on the next boot.");
203 fix_config_text = _("Do not mount automatically on boot");
204 fix_mount_text = _("Mount now");
205 } else if (type == "no-mount-on-boot") {
206 text = _("The filesystem is currently mounted but will not be mounted after the next boot.");
207 fix_config_text = _("Mount also automatically on boot");
208 fix_mount_text = _("Unmount now");
209 } else if (type == "mounted-no-config") {
210 text = cockpit.format(_("The filesystem is currently mounted on $0 but will not be mounted after the next boot."), other);
211 fix_config_text = cockpit.format(_("Mount automatically on $0 on boot"), other);
212 fix_mount_text = _("Unmount now");
213 } else if (type == "locked-on-boot-mount") {
214 text = _("The filesystem is configured to be automatically mounted on boot but its encryption container will not be unlocked at that time.");
215 fix_config_text = _("Do not mount automatically on boot");
216 fix_mount_text = _("Unlock automatically on boot");
217 }
218
219 return (
220 <Alert variant="warning" isInline
221 title={_("Inconsistent filesystem mount")}>
222 {text}
223 <div className="storage-alert-actions">
224 <StorageButton onClick={fix_config}>{fix_config_text}</StorageButton>
225 { fix_mount_text && <StorageButton onClick={fix_mount}>{fix_mount_text}</StorageButton> }
226 </div>
227 </Alert>);
228 };