Debian Codegraph @

pkg/storaged/filesystem/utils.jsx

raw
1 /*
2 * Copyright (C) 2016 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import React from "react";
7 import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js";
8
9 import cockpit from "cockpit";
10 import client from "../client.js";
11
12 import {
13 block_name, decode_filename,
14 parse_options, extract_option,
15 get_fstab_config_with_client,
16 find_children_for_mount_point,
17 get_mount_points,
18 } from "../utils.js";
19 import { parse_subvol_from_options } from "../btrfs/utils.jsx";
20 import { StorageLink } from "../storage-controls.jsx";
21
22 import { mounting_dialog } from "./mounting-dialog.jsx";
23
24 const _ = cockpit.gettext;
25
26 /* is mounted with an entry in fstab */
27 export function is_mounted(client, block, subvol) {
28 const block_fsys = client.blocks_fsys[block.path];
29 const mounted_at = get_mount_points(client, block_fsys, subvol);
30 const [, dir] = get_fstab_config(block, false, subvol);
31 if (dir) {
32 return mounted_at.indexOf(dir) >= 0;
33 } else
34 return null;
35 }
36
37 export function get_fstab_config(block, also_child_config, subvol) {
38 return get_fstab_config_with_client(client, block, also_child_config, subvol);
39 }
40
41 function nice_block_name(block) {
42 return block_name(client.blocks[block.CryptoBackingDevice] || block);
43 }
44
45 function find_blocks_for_mount_point(client, mount_point, self_block, self_subvol) {
46 function same_btrfs_volume(a, b) {
47 return (client.blocks_fsys_btrfs[a.path] &&
48 client.blocks_fsys_btrfs[b.path] &&
49 client.blocks_fsys_btrfs[a.path].data.uuid == client.blocks_fsys_btrfs[b.path].data.uuid);
50 }
51
52 function same_btrfs_subvol(a, b) {
53 return (a && b &&
54 ((a.pathname && a.pathname == b.pathname) ||
55 (a.id && a.id == b.id)));
56 }
57
58 function is_self(b, subvol) {
59 if (client.blocks_fsys_btrfs[self_block.path])
60 return same_btrfs_volume(b, self_block) && same_btrfs_subvol(subvol, self_subvol);
61 else
62 return self_block && (b == self_block || client.blocks[b.CryptoBackingDevice] == self_block);
63 }
64
65 function fmt_block_and_subvol(block, subvol) {
66 if (subvol)
67 return cockpit.format(_("btrfs subvolume $0 of $1"),
68 (subvol.pathname == "/" || subvol.id == 5) ? "top-level" : subvol.pathname || subvol.id,
69 block.IdLabel || block.IdUUID);
70 else
71 return nice_block_name(block);
72 }
73
74 const blocks = [];
75 const seen_uuids = {};
76
77 for (const p in client.blocks) {
78 const b = client.blocks[p];
79 for (const c of b.Configuration) {
80 if (c[0] == "fstab") {
81 let dir = decode_filename(c[1].dir.v);
82 if (dir[0] != "/")
83 dir = "/" + dir;
84 const subvol = parse_subvol_from_options(decode_filename(c[1].opts.v));
85 if (dir == mount_point && !is_self(b, subvol)) {
86 if (!seen_uuids[b.IdUUID]) {
87 seen_uuids[b.IdUUID] = true;
88 blocks.push(fmt_block_and_subvol(b, subvol));
89 }
90 }
91 }
92 }
93 }
94
95 return blocks;
96 }
97
98 export function is_valid_mount_point(client, block, val, will_not_mount, allow_empty, subvol) {
99 if (val === "") {
100 if (!will_not_mount && !allow_empty)
101 return _("Mount point cannot be empty");
102 return null;
103 }
104
105 const other_blocks = find_blocks_for_mount_point(client, val, block, subvol);
106 if (other_blocks.length > 0)
107 return cockpit.format(_("Mount point is already used for $0"), other_blocks.join(", "));
108
109 if (!will_not_mount) {
110 const children = find_children_for_mount_point(client, val, block, subvol);
111 if (Object.keys(children).length > 0)
112 return <>
113 {_("Filesystems are already mounted below this mountpoint.")}
114 {Object.keys(children).map(m => <div key={m}>
115 {cockpit.format("• $0 on $1", nice_block_name(children[m]),
116 client.strip_mount_point_prefix(m))}
117 </div>)}
118 {_("Please unmount them first.")}
119 </>;
120 }
121 }
122
123 export function get_cryptobacking_noauto(client, block) {
124 const crypto_backing = block.IdUsage == "crypto" ? block : client.blocks[block.CryptoBackingDevice];
125 if (!crypto_backing)
126 return false;
127
128 const crypto_config = crypto_backing.Configuration.find(c => c[0] == "crypttab");
129 if (!crypto_config)
130 return false;
131
132 const crypto_options = decode_filename(crypto_config[1].options.v).split(",")
133 .map(o => o.trim());
134 return crypto_options.indexOf("noauto") >= 0;
135 }
136
137 export function edit_mount_point(block, forced_options, subvol) {
138 mounting_dialog(client, block, "update", forced_options, subvol);
139 }
140
141 export const MountPoint = ({ fstab_config, forced_options, backing_block, content_block, subvol }) => {
142 const is_filesystem_mounted = content_block && is_mounted(client, content_block, subvol);
143 const [, old_dir, old_opts] = fstab_config;
144 const split_options = parse_options(old_opts);
145 extract_option(split_options, "noauto");
146 const opt_ro = extract_option(split_options, "ro");
147 const opt_never_auto = extract_option(split_options, "x-cockpit-never-auto");
148 const opt_nofail = extract_option(split_options, "nofail");
149 const opt_netdev = extract_option(split_options, "_netdev");
150 if (forced_options)
151 for (const opt of forced_options)
152 extract_option(split_options, opt);
153
154 let mount_point_text = null;
155 if (old_dir) {
156 mount_point_text = client.strip_mount_point_prefix(old_dir);
157 let opt_texts = [];
158 if (opt_ro)
159 opt_texts.push(_("read only"));
160 if (opt_never_auto)
161 opt_texts.push(_("never mount at boot"));
162 else if (opt_netdev)
163 opt_texts.push(_("after network"));
164 else if (opt_nofail)
165 opt_texts.push(_("ignore failure"));
166 else
167 opt_texts.push(_("stop boot on failure"));
168 opt_texts = opt_texts.concat(split_options);
169 if (opt_texts.length && !client.in_anaconda_mode()) {
170 mount_point_text = cockpit.format("$0 ($1)", mount_point_text, opt_texts.join(", "));
171 }
172 }
173
174 let extra_text = null;
175 if (client.in_anaconda_mode()) {
176 if (!old_dir)
177 extra_text = _("The filesystem has no assigned mount point.");
178 } else {
179 if (!is_filesystem_mounted) {
180 if (!old_dir)
181 extra_text = _("The filesystem has no permanent mount point.");
182 else
183 extra_text = _("The filesystem is not mounted.");
184 } else if (backing_block != content_block) {
185 if (!opt_never_auto)
186 extra_text = _("The filesystem will be unlocked and mounted on the next boot. This might require inputting a passphrase.");
187 }
188 }
189
190 if (!mount_point_text) {
191 mount_point_text = extra_text;
192 extra_text = null;
193 }
194
195 if (extra_text)
196 extra_text = <><br />{extra_text}</>;
197
198 return (
199 <>
200 <Flex>
201 { mount_point_text &&
202 <FlexItem>{ mount_point_text }</FlexItem>
203 }
204 <FlexItem>
205 <StorageLink onClick={() => edit_mount_point(content_block || backing_block,
206 forced_options, subvol)}>
207 {_("edit")}
208 </StorageLink>
209 </FlexItem>
210 </Flex>
211 { extra_text }
212 </>);
213 };
214
215 export const mount_point_text = (mount_point, mounted) => {
216 let mp_text;
217 if (mount_point) {
218 mp_text = client.strip_mount_point_prefix(mount_point);
219 if (mp_text == false)
220 return null;
221 if (!mounted && !client.in_anaconda_mode())
222 mp_text = mp_text + " " + _("(not mounted)");
223 } else {
224 if (client.in_anaconda_mode())
225 mp_text = _("(no assigned mount point)");
226 else
227 mp_text = _("(not mounted)");
228 }
229 return mp_text;
230 };