Debian Codegraph @

pkg/storaged/client.js

raw
1 /*
2 * Copyright (C) 2015 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from 'cockpit';
7 import * as PK from 'packagekit';
8 import { superuser } from 'superuser';
9 import { get_manifest_config_matchlist, in_anaconda_mode, read_anaconda_session_storage } from 'utils';
10
11 import * as utils from './utils.js';
12
13 import * as python from "python.js";
14 import { read_os_release } from "os-release.js";
15
16 import inotify_py from "inotify.py";
17 import mount_users_py from "./mount-users.py";
18 import nfs_mounts_py from "./nfs/nfs-mounts.py";
19 import vdo_monitor_py from "./legacy-vdo/vdo-monitor.py";
20 import stratis3_set_key_py from "./stratis/stratis3-set-key.py";
21
22 import { reset_pages } from "./pages.jsx";
23 import { make_overview_page } from "./overview/overview.jsx";
24 import { export_mount_point_mapping } from "./anaconda.jsx";
25
26 import { dequal } from 'dequal/lite';
27
28 import btrfs_tool_py from "./btrfs/btrfs-tool.py";
29
30 /* STORAGED CLIENT
31 */
32
33 function debug() {
34 if (window.debugging == "all" || window.debugging?.includes("storaged")) // not-covered: debugging
35 console.debug.apply(console, arguments); // not-covered: debugging
36 }
37
38 const client = {
39 busy: 0
40 };
41
42 cockpit.event_target(client);
43
44 client.run = async (func) => {
45 if (client.in_anaconda_mode())
46 await btrfs_stop_monitoring();
47 const prom = func() || Promise.resolve();
48 client.busy += 1;
49 await prom.finally(() => {
50 client.busy -= 1;
51 btrfs_start_monitor();
52 client.dispatchEvent("changed");
53 });
54 };
55
56 /* Superuser
57 */
58
59 client.superuser = superuser;
60 client.superuser.reload_page_on_change();
61 client.superuser.addEventListener("changed", () => client.dispatchEvent("changed"));
62
63 /* Metrics
64 */
65
66 function instance_sampler(metrics, source) {
67 let instances;
68 const self = {
69 data: { },
70 close
71 };
72
73 cockpit.event_target(self);
74
75 function handle_meta(msg) {
76 self.data = { };
77 instances = [];
78 for (let m = 0; m < msg.metrics.length; m++) {
79 instances[m] = msg.metrics[m].instances;
80 for (let i = 0; i < instances[m].length; i++)
81 self.data[instances[m][i]] = [];
82 }
83 if (Object.keys(self.data).length > 100) {
84 close();
85 self.data = { };
86 }
87 }
88
89 function handle_data(msg) {
90 let changed = false;
91 for (let s = 0; s < msg.length; s++) {
92 const metrics = msg[s];
93 for (let m = 0; m < metrics.length; m++) {
94 const inst = metrics[m];
95 for (let i = 0; i < inst.length; i++) {
96 if (inst[i] !== null && inst[i] != self.data[instances[m][i]][m]) {
97 changed = true;
98 self.data[instances[m][i]][m] = inst[i];
99 }
100 }
101 }
102 }
103 if (changed)
104 self.dispatchEvent('changed');
105 }
106
107 const channel = cockpit.channel({
108 payload: "metrics1",
109 source: source || "internal",
110 metrics
111 });
112 channel.addEventListener("closed", function (event, error) {
113 console.log("closed", error);
114 });
115 channel.addEventListener("message", function (event, message) {
116 const msg = JSON.parse(message);
117 if (msg.length)
118 handle_data(msg);
119 else
120 handle_meta(msg);
121 });
122
123 function close() {
124 channel.close();
125 }
126
127 return self;
128 }
129
130 /* D-Bus proxies
131 */
132
133 client.time_offset = undefined; /* Number of milliseconds that the server is ahead of us. */
134 client.features = undefined;
135
136 client.storaged_client = undefined;
137
138 function proxy(iface, path) {
139 return client.storaged_client.proxy("org.freedesktop.UDisks2." + iface,
140 "/org/freedesktop/UDisks2/" + path,
141 { watch: true });
142 }
143
144 function proxies(iface) {
145 /* We create the proxies here with 'watch' set to false and
146 * establish a general watch for all of them. This is more
147 * efficient since it reduces the number of D-Bus calls done
148 * by the cache.
149 */
150 return client.storaged_client.proxies("org.freedesktop.UDisks2." + iface,
151 "/org/freedesktop/UDisks2",
152 { watch: false });
153 }
154
155 client.call = function call(path, iface, method, args, options) {
156 return client.storaged_client.call(path, "org.freedesktop.UDisks2." + iface, method, args, options);
157 };
158
159 function init_proxies () {
160 client.mdraids = proxies("MDRaid");
161 client.vgroups = proxies("VolumeGroup");
162 client.lvols = proxies("LogicalVolume");
163 client.drives = proxies("Drive");
164 client.drives_ata = proxies("Drive.Ata");
165 client.blocks = proxies("Block");
166 client.blocks_ptable = proxies("PartitionTable");
167 client.blocks_part = proxies("Partition");
168 client.blocks_lvm2 = proxies("Block.LVM2");
169 client.blocks_pvol = proxies("PhysicalVolume");
170 client.blocks_fsys = proxies("Filesystem");
171 client.blocks_crypto = proxies("Encrypted");
172 client.blocks_swap = proxies("Swapspace");
173 client.iscsi_sessions = proxies("ISCSI.Session");
174 client.vdo_vols = proxies("VDOVolume");
175 client.blocks_fsys_btrfs = proxies("Filesystem.BTRFS");
176 client.jobs = proxies("Job");
177 client.nvme_controller = proxies("NVMe.Controller");
178
179 return client.storaged_client.watch({ path_namespace: "/org/freedesktop/UDisks2" });
180 }
181
182 /* Monitors
183 */
184
185 client.fsys_sizes = instance_sampler([{ name: "mount.used" },
186 { name: "mount.total" }
187 ]);
188
189 client.swap_sizes = instance_sampler([{ name: "swapdev.length" },
190 { name: "swapdev.free" },
191 ], "direct");
192
193 function btrfs_findmnt_poll() {
194 if (!client.btrfs_mounts)
195 client.btrfs_mounts = { };
196
197 const update_btrfs_mounts = output => {
198 const btrfs_mounts = {};
199 try {
200 // Extract the data into a { uuid: { subvolid: { subvol, target } } }
201 const mounts = JSON.parse(output);
202 if ("filesystems" in mounts) {
203 for (const fs of mounts.filesystems) {
204 const subvolid_match = fs.options.match(/subvolid=(?<subvolid>\d+)/);
205 const subvol_match = fs.options.match(/subvol=(?<subvol>[\w\\/]+)/);
206 const ro = fs.options.split(",").indexOf("ro") >= 0;
207
208 if (!subvolid_match && !subvol_match) {
209 console.warn("findmnt entry without subvol and subvolid", fs);
210 break;
211 }
212
213 const { subvolid } = subvolid_match.groups;
214 const { subvol } = subvol_match.groups;
215 const subvolume = {
216 pathname: subvol,
217 id: subvolid,
218 mount_points: [fs.target],
219 rw_mount_points: ro ? [] : [fs.target],
220 };
221
222 if (!(fs.uuid in btrfs_mounts)) {
223 btrfs_mounts[fs.uuid] = { };
224 }
225
226 // We need to handle multiple mounts, they are listed separate.
227 if (subvolid in btrfs_mounts[fs.uuid]) {
228 btrfs_mounts[fs.uuid][subvolid].mount_points.push(fs.target);
229 if (!ro)
230 btrfs_mounts[fs.uuid][subvolid].rw_mount_points.push(fs.target);
231 } else {
232 btrfs_mounts[fs.uuid][subvolid] = subvolume;
233 }
234 }
235 }
236 } catch (exc) {
237 if (exc.message)
238 console.error("unable to parse findmnt JSON output", exc);
239 }
240
241 // Update client state
242 if (!dequal(client.btrfs_mounts, btrfs_mounts)) {
243 client.btrfs_mounts = btrfs_mounts;
244 debug("btrfs_findmnt_poll mounts:", client.btrfs_mounts);
245 client.update();
246 }
247 };
248
249 const findmnt_poll = () => {
250 return cockpit.spawn(["findmnt", "--type", "btrfs", "--mtab", "--poll"], { superuser: "try", err: "message" }).stream(() => {
251 cockpit.spawn(["findmnt", "--type", "btrfs", "--mtab", "-o", "UUID,OPTIONS,TARGET", "--json"],
252 { superuser: "try", err: "message" }).then(output => update_btrfs_mounts(output)).catch(err => {
253 // When there are no btrfs filesystems left this can fail and thus we need to manually reset the mount info.
254 client.btrfs_mounts = {};
255 client.update();
256 if (err.message) {
257 console.error("findmnt exited with an error", err);
258 }
259 });
260 }).catch(err => {
261 console.error("findmnt --poll exited with an error", err);
262 throw new Error("findmnt --poll stopped working");
263 });
264 };
265
266 // This fails when no btrfs filesystem is found with the --mtab option and exits with 1, so that is kinda useless, however without --mtab
267 // we don't get a nice flat structure. So we ignore the errors
268 cockpit.spawn(["findmnt", "--type", "btrfs", "--mtab", "-o", "UUID,OPTIONS,SOURCE,TARGET", "--json"],
269 { superuser: "try", err: "message" }).then(output => {
270 update_btrfs_mounts(output);
271 findmnt_poll();
272 }).catch(err => {
273 // only log error when there is a real issue.
274 if (client.superuser.allowed && err.message) {
275 console.error(`unable to run findmnt ${err}`);
276 }
277 findmnt_poll();
278 });
279 }
280
281 function btrfs_update(data) {
282 if (!client.uuids_btrfs_subvols)
283 client.uuids_btrfs_subvols = { };
284 if (!client.uuids_btrfs_usage)
285 client.uuids_btrfs_usage = { };
286 if (!client.uuids_btrfs_default_subvol)
287 client.uuids_btrfs_default_subvol = { };
288
289 const uuids_subvols = { };
290 const uuids_usage = { };
291 const default_subvol = { };
292
293 for (const uuid in data) {
294 if (data[uuid].error) {
295 console.warn("Error polling btrfs", uuid, data[uuid].error);
296 } else {
297 if (data[uuid].subvolumes) {
298 uuids_subvols[uuid] = [{ pathname: "/", id: 5, parent: null }].concat(data[uuid].subvolumes);
299 }
300 if (data[uuid].usages) {
301 uuids_usage[uuid] = data[uuid].usages;
302 }
303 if (data[uuid].default_subvolume) {
304 default_subvol[uuid] = data[uuid].default_subvolume;
305 }
306 }
307 }
308
309 if (!dequal(client.uuids_btrfs_subvols, uuids_subvols) || !dequal(client.uuids_btrfs_usage, uuids_usage) ||
310 !dequal(client.uuids_btrfs_default_subvol, default_subvol)) {
311 debug("btrfs_pol new subvols:", uuids_subvols);
312 client.uuids_btrfs_subvols = uuids_subvols;
313 client.uuids_btrfs_usage = uuids_usage;
314 debug("btrfs_pol usage:", uuids_usage);
315 client.uuids_btrfs_default_subvol = default_subvol;
316 debug("btrfs_pol default subvolumes:", default_subvol);
317 client.update();
318 }
319 }
320
321 export async function btrfs_tool(args) {
322 return await python.spawn(btrfs_tool_py, args, { superuser: "require" });
323 }
324
325 function btrfs_poll_options() {
326 if (client.in_anaconda_mode())
327 return ["--mount"];
328 else
329 return [];
330 }
331
332 export async function btrfs_poll() {
333 if (!client.superuser.allowed || !client.features.btrfs) {
334 return;
335 }
336
337 const data = JSON.parse(await btrfs_tool(["poll", ...btrfs_poll_options()]));
338 btrfs_update(data);
339 }
340
341 let btrfs_monitor_channel = null;
342
343 function btrfs_start_monitor() {
344 if (!client.superuser.allowed || !client.features.btrfs) {
345 return;
346 }
347
348 if (btrfs_monitor_channel)
349 return;
350
351 const channel = python.spawn(btrfs_tool_py, ["monitor", ...btrfs_poll_options()], { superuser: "require" });
352 let buf = "";
353
354 channel.stream(output => {
355 buf += output;
356 const lines = buf.split("\n");
357 buf = lines[lines.length - 1];
358 if (lines.length >= 2) {
359 const data = JSON.parse(lines[lines.length - 2]);
360 btrfs_update(data);
361 }
362 });
363
364 channel.catch(err => {
365 throw new Error(err.toString());
366 });
367
368 btrfs_monitor_channel = channel;
369 }
370
371 function btrfs_stop_monitoring() {
372 if (btrfs_monitor_channel) {
373 const res = btrfs_monitor_channel.then(() => {
374 btrfs_monitor_channel = null;
375 });
376 btrfs_monitor_channel.close();
377 return res;
378 } else {
379 return Promise.resolve();
380 }
381 }
382
383 function btrfs_start_polling() {
384 debug("starting polling for btrfs subvolumes");
385 client.uuids_btrfs_subvols = { };
386 client.uuids_btrfs_usage = { };
387 client.uuids_btrfs_default_subvol = { };
388 client.btrfs_mounts = { };
389 btrfs_findmnt_poll();
390 btrfs_start_monitor();
391 }
392
393 /* Derived indices.
394 */
395
396 function is_multipath_master(block) {
397 // The master has "mpath" in its device mapper UUID. In the
398 // future, storaged will hopefully provide this information
399 // directly.
400 if (block.Symlinks && block.Symlinks.length) {
401 for (let i = 0; i < block.Symlinks.length; i++)
402 if (utils.decode_filename(block.Symlinks[i]).indexOf("/dev/disk/by-id/dm-uuid-mpath-") === 0)
403 return true;
404 }
405 return false;
406 }
407
408 function is_toplevel_drive(block) {
409 // We consider all Block objects that point to the same Drive
410 // objects to be multipath members for a single actual device.
411 //
412 // However, objects for partitions point to the same Drive object
413 // as the object for the partition table. We have to ignore them.
414
415 if (client.blocks_part[block.path])
416 return false;
417
418 // Also, eMMCs have special partition-like sub-devices that point
419 // to the main Drive. We identify them by their name, just like
420 // UDisks2.
421
422 if (utils.decode_filename(block.Device).match(/\/dev\/mmcblk[0-9]boot[0-9]$/))
423 return false;
424
425 return true;
426 }
427
428 function update_indices() {
429 let path;
430 let block;
431 let mdraid;
432 let vgroup;
433 let pvol;
434 let lvol;
435 let pool;
436 let blockdev;
437 let fsys;
438 let part;
439 let i;
440
441 client.broken_multipath_present = false;
442 client.drives_multipath_blocks = { };
443 client.drives_block = { };
444 for (path in client.drives) {
445 client.drives_multipath_blocks[path] = [];
446 }
447 for (path in client.blocks) {
448 block = client.blocks[path];
449 if (client.drives_multipath_blocks[block.Drive] !== undefined && is_toplevel_drive(block)) {
450 if (is_multipath_master(block))
451 client.drives_block[block.Drive] = block;
452 else
453 client.drives_multipath_blocks[block.Drive].push(block);
454 }
455 }
456 for (path in client.drives_multipath_blocks) {
457 /* If there is no multipath master and only a single
458 * member, then this is actually a normal singlepath
459 * device.
460 */
461
462 if (!client.drives_block[path] && client.drives_multipath_blocks[path].length == 1) {
463 client.drives_block[path] = client.drives_multipath_blocks[path][0];
464 client.drives_multipath_blocks[path] = [];
465 } else {
466 client.drives_multipath_blocks[path].sort(utils.block_cmp);
467 if (!client.drives_block[path])
468 client.broken_multipath_present = true;
469 }
470 }
471
472 client.mdraids_block = { };
473 for (path in client.blocks) {
474 block = client.blocks[path];
475 if (block.MDRaid != "/")
476 client.mdraids_block[block.MDRaid] = block;
477 }
478
479 client.mdraids_members = { };
480 for (path in client.mdraids) {
481 client.mdraids_members[path] = [];
482 }
483 for (path in client.blocks) {
484 block = client.blocks[path];
485 if (client.mdraids_members[block.MDRaidMember] !== undefined)
486 client.mdraids_members[block.MDRaidMember].push(block);
487 }
488 for (path in client.mdraids_members) {
489 client.mdraids_members[path].sort(utils.block_cmp);
490 }
491
492 client.slashdevs_block = { };
493 function enter_slashdev(block, enc) {
494 client.slashdevs_block[utils.decode_filename(enc)] = block;
495 }
496 for (path in client.blocks) {
497 block = client.blocks[path];
498 enter_slashdev(block, block.Device);
499 enter_slashdev(block, block.PreferredDevice);
500 for (i = 0; i < block.Symlinks.length; i++)
501 enter_slashdev(block, block.Symlinks[i]);
502 }
503
504 client.uuids_mdraid = { };
505 for (path in client.mdraids) {
506 mdraid = client.mdraids[path];
507 client.uuids_mdraid[mdraid.UUID] = mdraid;
508 }
509
510 client.vgnames_vgroup = { };
511 for (path in client.vgroups) {
512 vgroup = client.vgroups[path];
513 client.vgnames_vgroup[vgroup.Name] = vgroup;
514 }
515
516 const vgroups_with_dm_pvs = { };
517
518 client.vgroups_pvols = { };
519 for (path in client.vgroups) {
520 client.vgroups_pvols[path] = [];
521 }
522 for (path in client.blocks_pvol) {
523 pvol = client.blocks_pvol[path];
524 if (client.vgroups_pvols[pvol.VolumeGroup] !== undefined) {
525 client.vgroups_pvols[pvol.VolumeGroup].push(pvol);
526 {
527 // HACK - this is needed below to deal with a UDisks2 bug.
528 // https://github.com/storaged-project/udisks/pull/1206
529 const block = client.blocks[path];
530 if (block && utils.decode_filename(block.Device).indexOf("/dev/dm-") == 0)
531 vgroups_with_dm_pvs[pvol.VolumeGroup] = true;
532 }
533 }
534 }
535 function cmp_pvols(a, b) {
536 return utils.block_cmp(client.blocks[a.path], client.blocks[b.path]);
537 }
538 for (path in client.vgroups_pvols) {
539 client.vgroups_pvols[path].sort(cmp_pvols);
540 }
541
542 client.vgroups_lvols = { };
543 for (path in client.vgroups) {
544 client.vgroups_lvols[path] = [];
545 }
546 for (path in client.lvols) {
547 lvol = client.lvols[path];
548 if (client.vgroups_lvols[lvol.VolumeGroup] !== undefined)
549 client.vgroups_lvols[lvol.VolumeGroup].push(lvol);
550 }
551 for (path in client.vgroups_lvols) {
552 client.vgroups_lvols[path].sort(function (a, b) { return a.Name.localeCompare(b.Name) });
553 }
554
555 client.lvols_block = { };
556 for (path in client.blocks_lvm2) {
557 client.lvols_block[client.blocks_lvm2[path].LogicalVolume] = client.blocks[path];
558 }
559
560 client.lvols_pool_members = { };
561 for (path in client.lvols) {
562 if (client.lvols[path].Type == "pool")
563 client.lvols_pool_members[path] = [];
564 }
565 for (path in client.lvols) {
566 lvol = client.lvols[path];
567 if (client.lvols_pool_members[lvol.ThinPool] !== undefined)
568 client.lvols_pool_members[lvol.ThinPool].push(lvol);
569 }
570 for (path in client.lvols_pool_members) {
571 client.lvols_pool_members[path].sort(function (a, b) { return a.Name.localeCompare(b.Name) });
572 }
573
574 function summarize_stripe(lv_size, segments) {
575 const pvs = { };
576 let total_size = 0;
577 for (const [, size, pv] of segments) {
578 if (!pvs[pv])
579 pvs[pv] = 0;
580 pvs[pv] += size;
581 total_size += size;
582 }
583 if (total_size < lv_size)
584 pvs["/"] = lv_size - total_size;
585 return pvs;
586 }
587
588 client.lvols_stripe_summary = { };
589 client.lvols_status = { };
590 for (path in client.lvols) {
591 const struct = client.lvols[path].Structure;
592 const lvol = client.lvols[path];
593
594 // HACK - UDisks2 befopre 2.11 can't find the PVs of a segment
595 // when they are on a device mapper device.
596 //
597 // https://github.com/storaged-project/udisks/pull/1206
598
599 if (!client.at_least("2.11") && vgroups_with_dm_pvs[lvol.VolumeGroup])
600 continue;
601
602 let summary;
603 let status = "";
604 if (lvol.Layout != "thin" && struct && struct.segments) {
605 summary = summarize_stripe(struct.size.v, struct.segments.v);
606 if (summary["/"])
607 status = "partial";
608 } else if (struct && struct.data && struct.metadata &&
609 (struct.data.v.length == struct.metadata.v.length || struct.metadata.v.length == 0)) {
610 summary = [];
611 const n_total = struct.data.v.length;
612 let n_missing = 0;
613 for (let i = 0; i < n_total; i++) {
614 const data_lv = struct.data.v[i];
615 const metadata_lv = struct.metadata.v[i] || { size: { v: 0 }, segments: { v: [] } };
616
617 if (!data_lv.segments || (metadata_lv && !metadata_lv.segments)) {
618 summary = undefined;
619 break;
620 }
621
622 const s = summarize_stripe(data_lv.size.v + metadata_lv.size.v,
623 data_lv.segments.v.concat(metadata_lv.segments.v));
624 if (s["/"])
625 n_missing += 1;
626
627 summary.push(s);
628 }
629 if (n_missing > 0) {
630 status = "partial";
631 if (lvol.Layout == "raid1") {
632 if (n_total - n_missing >= 1)
633 status = "degraded";
634 }
635 if (lvol.Layout == "raid10") {
636 // This is correct for two-way mirroring, which is
637 // the only setup supported by lvm2.
638 if (n_missing > n_total / 2) {
639 // More than half of the PVs are gone -> at
640 // least one mirror has definitely lost both
641 // halves.
642 status = "partial";
643 } else if (n_missing > 1) {
644 // Two or more PVs are lost -> one mirror
645 // might have lost both halves
646 status = "degraded-maybe-partial";
647 } else {
648 // Only one PV is missing -> no mirror has
649 // lost both halves.
650 status = "degraded";
651 }
652 }
653 if (lvol.Layout == "raid4" || lvol.Layout == "raid5") {
654 if (n_missing <= 1)
655 status = "degraded";
656 }
657 if (lvol.Layout == "raid6") {
658 if (n_missing <= 2)
659 status = "degraded";
660 }
661 }
662 }
663 if (summary) {
664 client.lvols_stripe_summary[path] = summary;
665 client.lvols_status[path] = status;
666 }
667 }
668
669 client.stratis_poolnames_pool = { };
670 for (path in client.stratis_pools) {
671 pool = client.stratis_pools[path];
672 client.stratis_poolnames_pool[pool.Name] = pool;
673 }
674
675 client.stratis_pooluuids_pool = { };
676 for (path in client.stratis_pools) {
677 pool = client.stratis_pools[path];
678 client.stratis_pooluuids_pool[pool.Uuid] = pool;
679 }
680
681 client.stratis_pool_blockdevs = { };
682 for (path in client.stratis_pools) {
683 client.stratis_pool_blockdevs[path] = [];
684 }
685 for (path in client.stratis_blockdevs) {
686 blockdev = client.stratis_blockdevs[path];
687 if (client.stratis_pools[blockdev.Pool] !== undefined)
688 client.stratis_pool_blockdevs[blockdev.Pool].push(blockdev);
689 }
690
691 client.stratis_pool_filesystems = { };
692 for (path in client.stratis_pools) {
693 client.stratis_pool_filesystems[path] = [];
694 }
695 for (path in client.stratis_filesystems) {
696 fsys = client.stratis_filesystems[path];
697 if (client.stratis_pools[fsys.Pool] !== undefined)
698 client.stratis_pool_filesystems[fsys.Pool].push(fsys);
699 }
700
701 client.blocks_stratis_fsys = { };
702 for (path in client.stratis_filesystems) {
703 fsys = client.stratis_filesystems[path];
704 block = client.slashdevs_block[fsys.Devnode];
705 if (block)
706 client.blocks_stratis_fsys[block.path] = fsys;
707 }
708
709 client.blocks_stratis_blockdev = { };
710 for (path in client.stratis_blockdevs) {
711 block = client.slashdevs_block[client.stratis_blockdevs[path].PhysicalPath];
712 if (block)
713 client.blocks_stratis_blockdev[block.path] = client.stratis_blockdevs[path];
714 }
715
716 client.blocks_stratis_stopped_pool = { };
717 for (const uuid in client.stratis_manager.StoppedPools) {
718 const devnodes = client.stratis_stopped_pool_devnodes(uuid);
719 for (const d of devnodes) {
720 block = client.slashdevs_block[d];
721 if (block)
722 client.blocks_stratis_stopped_pool[block.path] = uuid;
723 }
724 }
725
726 client.stratis_pool_stats = { };
727 for (path in client.stratis_pools) {
728 const pool = client.stratis_pools[path];
729 const filesystems = client.stratis_pool_filesystems[path];
730
731 const fsys_offsets = [];
732 let fsys_total_used = 0;
733 let fsys_total_size = 0;
734 filesystems.forEach(fs => {
735 fsys_offsets.push(fsys_total_used);
736 fsys_total_used += fs.Used[0] ? Number(fs.Used[1]) : 0;
737 fsys_total_size += Number(fs.Size);
738 });
739
740 const overhead = pool.TotalPhysicalUsed[0] ? (Number(pool.TotalPhysicalUsed[1]) - fsys_total_used) : 0;
741 const pool_total = Number(pool.TotalPhysicalSize) - overhead;
742 let pool_free = pool_total - fsys_total_size;
743
744 // leave some margin since the above computation does not seem to
745 // be exactly right when snapshots are involved.
746 pool_free -= filesystems.length * 1024 * 1024;
747
748 client.stratis_pool_stats[path] = {
749 fsys_offsets,
750 fsys_total_used,
751 fsys_total_size,
752 pool_total,
753 pool_free,
754 };
755 }
756
757 client.blocks_cleartext = { };
758 for (path in client.blocks) {
759 block = client.blocks[path];
760 if (block.CryptoBackingDevice != "/")
761 client.blocks_cleartext[block.CryptoBackingDevice] = block;
762 }
763
764 client.blocks_partitions = { };
765 for (path in client.blocks_ptable) {
766 client.blocks_partitions[path] = [];
767 }
768 for (path in client.blocks_part) {
769 part = client.blocks_part[path];
770 if (client.blocks_partitions[part.Table] !== undefined)
771 client.blocks_partitions[part.Table].push(part);
772 }
773 for (path in client.blocks_partitions) {
774 client.blocks_partitions[path].sort(function (a, b) { return a.Offset - b.Offset });
775 }
776
777 client.iscsi_sessions_drives = { };
778 client.drives_iscsi_session = { };
779 for (path in client.drives) {
780 const block = client.drives_block[path];
781 if (!block)
782 continue;
783 for (const session_path in client.iscsi_sessions) {
784 const session = client.iscsi_sessions[session_path];
785 for (i = 0; i < block.Symlinks.length; i++) {
786 if (utils.decode_filename(block.Symlinks[i]).includes(session.data.target_name)) {
787 client.drives_iscsi_session[path] = session;
788 if (!client.iscsi_sessions_drives[session_path])
789 client.iscsi_sessions_drives[session_path] = [];
790 client.iscsi_sessions_drives[session_path].push(client.drives[path]);
791 }
792 }
793 }
794 }
795
796 client.path_jobs = { };
797 function enter_job(job) {
798 if (!job.Objects || !job.Objects.length)
799 return;
800 job.Objects.forEach(p => {
801 if (!client.path_jobs[p])
802 client.path_jobs[p] = [];
803 client.path_jobs[p].push(job);
804 });
805 }
806 for (path in client.jobs) {
807 enter_job(client.jobs[path]);
808 }
809
810 // UDisks API does not provide a btrfs volume abstraction so we keep track of
811 // volume's by uuid in an object. uuid => [org.freedesktop.UDisks2.Filesystem.BTRFS]
812 // https://github.com/storaged-project/udisks/issues/1232
813 const old_uuids = client.uuids_btrfs_volume;
814 let need_poll = false;
815 client.uuids_btrfs_volume = { };
816 client.uuids_btrfs_blocks = { };
817 for (const p in client.blocks_fsys_btrfs) {
818 const bfs = client.blocks_fsys_btrfs[p];
819 const uuid = bfs.data.uuid;
820 const block_fsys = client.blocks_fsys[p];
821 if (!uuid)
822 continue;
823 if ((block_fsys && block_fsys.MountPoints.length > 0) || !client.uuids_btrfs_volume[uuid]) {
824 client.uuids_btrfs_volume[uuid] = bfs;
825 if (!old_uuids || !old_uuids[uuid])
826 need_poll = true;
827 }
828 if (!client.uuids_btrfs_blocks[uuid])
829 client.uuids_btrfs_blocks[uuid] = [];
830 client.uuids_btrfs_blocks[uuid].push(client.blocks[p]);
831 }
832
833 if (need_poll) {
834 btrfs_poll();
835 }
836 }
837
838 let lvm2_poll_timer = null;
839
840 function update_lvm2_polling(for_visibility) {
841 const need_polling = !cockpit.hidden && !!Object.values(client.vgroups).find(vg => vg.NeedsPolling);
842
843 function poll() {
844 for (const path in client.vgroups) {
845 const vg = client.vgroups[path];
846 if (vg.NeedsPolling) {
847 vg.Poll();
848 }
849 }
850 }
851
852 if (need_polling && lvm2_poll_timer == null) {
853 lvm2_poll_timer = window.setInterval(poll, 2000);
854 if (for_visibility)
855 poll();
856 } else if (!need_polling && lvm2_poll_timer) {
857 window.clearInterval(lvm2_poll_timer);
858 lvm2_poll_timer = null;
859 }
860 }
861
862 client.update = (first_time) => {
863 if (first_time)
864 client.ready = true;
865 if (client.ready) {
866 update_indices();
867 update_lvm2_polling(false);
868 reset_pages();
869 make_overview_page();
870 export_mount_point_mapping();
871 client.dispatchEvent("changed");
872 }
873 };
874
875 function init_model(callback) {
876 function pull_time() {
877 return cockpit.spawn(["date", "+%s"])
878 .then(function (now) {
879 client.time_offset = parseInt(now, 10) * 1000 - new Date().getTime();
880 });
881 }
882
883 async function enable_udisks_features() {
884 if (!client.manager.valid)
885 return;
886
887 try {
888 await client.manager.EnableModule("btrfs", true);
889 client.manager_btrfs = proxy("Manager.BTRFS", "Manager");
890 await client.manager_btrfs.wait();
891 client.features.btrfs = client.manager_btrfs.valid;
892 if (client.features.btrfs)
893 btrfs_start_polling();
894 } catch (error) {
895 console.warn("Can't enable storaged btrfs module", error.toString());
896 }
897
898 try {
899 await client.manager.EnableModule("iscsi", true);
900 client.manager_iscsi = proxy("Manager.ISCSI.Initiator", "Manager");
901 await client.manager_iscsi.wait();
902 client.features.iscsi = (client.manager_iscsi.valid && client.manager_iscsi.SessionsSupported !== false);
903 } catch (error) {
904 console.warn("Can't enable storaged iscsi module", error.toString());
905 }
906
907 try {
908 await client.manager.EnableModule("lvm2", true);
909 client.manager_lvm2 = proxy("Manager.LVM2", "Manager");
910 await client.manager_lvm2.wait();
911 client.features.lvm2 = client.manager_lvm2.valid;
912 } catch (error) {
913 console.warn("Can't enable storaged lvm2 module", error.toString());
914 }
915 }
916
917 async function enable_lvm_create_vdo_feature() {
918 async function exit_code(cmd) {
919 try {
920 await cockpit.spawn(cmd, { err: "ignore", superuser: "try" });
921 return 0;
922 } catch (ex) {
923 return ex.exit_status || 1;
924 }
925 }
926
927 /* We assume that if LVM has the option to format a VDO volume
928 in the kernel, then it will be used and will work. If
929 that's not true, the error message from LVM will hopefully
930 be clear enough to help people figure out what needs to be
931 done.
932 */
933 if (await exit_code(["vdoformat", "--version"]) == 0 ||
934 await exit_code(["lvmconfig", "--list", "allocation/vdo_use_kernel_format"]) == 0) {
935 client.features.lvm_create_vdo = true;
936 }
937 }
938
939 function enable_legacy_vdo_features() {
940 return client.legacy_vdo_overlay.start().then(
941 function (success) {
942 // hack here
943 client.features.legacy_vdo = success;
944 return Promise.resolve();
945 },
946 function () {
947 return Promise.resolve();
948 });
949 }
950
951 function enable_clevis_features() {
952 return cockpit.script("type clevis-luks-bind", { err: "ignore" }).then(
953 function () {
954 client.features.clevis = true;
955 return Promise.resolve();
956 },
957 function () {
958 return Promise.resolve();
959 });
960 }
961
962 function enable_nfs_features() {
963 // mount.nfs might be in */sbin but that isn't always in
964 // $PATH, such as when connecting from CentOS to another
965 // machine via SSH as non-root.
966 const std_path = "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
967 return cockpit.script("type mount.nfs", { err: "message", environ: [std_path] }).then(
968 function () {
969 client.features.nfs = true;
970 client.nfs.start();
971 return Promise.resolve();
972 },
973 function () {
974 return Promise.resolve();
975 });
976 }
977
978 function enable_pk_features() {
979 if (client.in_anaconda_mode()) {
980 client.features.packagekit = false;
981 return Promise.resolve();
982 }
983 return PK.detect().then(function (available) { client.features.packagekit = available });
984 }
985
986 function enable_stratis_feature() {
987 return client.stratis_start().catch(error => {
988 if (error.problem != "not-found")
989 console.warn("Failed to start Stratis support", error);
990 return Promise.resolve();
991 });
992 }
993
994 function enable_features() {
995 client.features = { };
996 return (enable_udisks_features()
997 .then(enable_clevis_features)
998 .then(enable_nfs_features)
999 .then(enable_pk_features)
1000 .then(enable_stratis_feature)
1001 .then(enable_lvm_create_vdo_feature)
1002 .then(enable_legacy_vdo_features));
1003 }
1004
1005 function query_fsys_info() {
1006 const info = {};
1007 return Promise.all(client.manager.SupportedFilesystems.map(fs =>
1008 client.manager.CanFormat(fs).then(canformat_result => {
1009 info[fs] = {
1010 can_format: canformat_result[0],
1011 can_shrink: false,
1012 can_grow: false
1013 };
1014 return client.manager.CanResize(fs)
1015 .then(canresize_result => {
1016 // We assume that all filesystems support
1017 // offline shrinking/growing if they
1018 // support shrinking or growing at all.
1019 // The actual resizing utility will
1020 // temporarily mount the fs if necessary,
1021 if (canresize_result[0]) {
1022 info[fs].can_shrink = !!(canresize_result[1] & 2);
1023 info[fs].shrink_needs_unmount = !(canresize_result[1] & 8);
1024 info[fs].can_grow = !!(canresize_result[1] & 4);
1025 info[fs].grow_needs_unmount = !(canresize_result[1] & 16);
1026 }
1027 })
1028 // ignore unsupported filesystems
1029 .catch(() => {});
1030 }))
1031 ).then(() => info);
1032 }
1033
1034 client.anaconda = read_anaconda_session_storage();
1035
1036 pull_time().then(() => {
1037 read_os_release().then(os_release => {
1038 client.os_release = os_release;
1039
1040 enable_features().then(() => {
1041 query_fsys_info().then((fsys_info) => {
1042 client.fsys_info = fsys_info;
1043
1044 client.storaged_client.addEventListener('notify', () => client.update());
1045
1046 update_indices();
1047 cockpit.addEventListener("visibilitychange", () => update_lvm2_polling(true));
1048 btrfs_poll().then(() => {
1049 client.update(true);
1050 callback();
1051 });
1052 });
1053 });
1054 });
1055 });
1056 }
1057
1058 client.younger_than = function younger_than(version) {
1059 return utils.compare_versions(this.manager.Version, version) < 0;
1060 };
1061
1062 client.at_least = function at_least(version) {
1063 return utils.compare_versions(this.manager.Version, version) >= 0;
1064 };
1065
1066 /* Mount users
1067 */
1068
1069 client.find_mount_users = (target, is_mounted) => {
1070 if (is_mounted === undefined || is_mounted)
1071 return python.spawn(mount_users_py, ["users", target], { superuser: "try", err: "message" }).then(JSON.parse);
1072 else
1073 return Promise.resolve([]);
1074 };
1075
1076 client.stop_mount_users = (users) => {
1077 if (users && users.length > 0) {
1078 return python.spawn(mount_users_py, ["stop", JSON.stringify(users)],
1079 { superuser: "try", err: "message" });
1080 } else
1081 return Promise.resolve();
1082 };
1083
1084 /* Direct mounting and unmounting
1085 *
1086 * We don't use UDisks2 for most of our mounting and unmounting in
1087 * order to get better control over which entry from fstab is
1088 * selected. Once UDisks2 allows that control, we can switch back to
1089 * it.
1090 *
1091 * But note that these functions still require an fstab entry.
1092 */
1093
1094 client.mount_at = (block, target) => {
1095 const entry = block.Configuration.find(c => c[0] == "fstab" && utils.decode_filename(c[1].dir.v) == target);
1096 if (entry)
1097 return cockpit.script('set -e; mkdir -p "$2"; mount "$1" "$2" -o "$3"',
1098 [utils.decode_filename(block.Device), target, utils.get_block_mntopts(entry[1])],
1099 { superuser: "require", err: "message" });
1100 else
1101 return Promise.reject(cockpit.format("Internal error: No fstab entry for $0 and $1",
1102 utils.decode_filename(block.Device),
1103 target));
1104 };
1105
1106 client.unmount_at = (target, users) => {
1107 return client.stop_mount_users(users).then(() => cockpit.spawn(["umount", target],
1108 { superuser: "require", err: "message" }));
1109 };
1110
1111 /* NFS mounts
1112 */
1113
1114 function nfs_mounts() {
1115 const self = {
1116 entries: [],
1117 fsys_sizes: { },
1118
1119 start,
1120
1121 get_fsys_size,
1122 entry_users,
1123
1124 update_entry,
1125 add_entry,
1126 remove_entry,
1127
1128 mount_entry,
1129 unmount_entry,
1130 stop_and_unmount_entry,
1131 stop_and_remove_entry,
1132
1133 find_entry
1134 };
1135
1136 function spawn_nfs_mounts(args) {
1137 return python.spawn([inotify_py, nfs_mounts_py], args, { superuser: "try", err: "message" });
1138 }
1139
1140 function start() {
1141 let buf = "";
1142 spawn_nfs_mounts(["monitor"])
1143 .stream(function (output) {
1144 buf += output;
1145 const lines = buf.split("\n");
1146 buf = lines[lines.length - 1];
1147 if (lines.length >= 2) {
1148 self.entries = JSON.parse(lines[lines.length - 2]);
1149 self.fsys_sizes = { };
1150 client.update();
1151 }
1152 })
1153 .catch(function (error) {
1154 if (error != "closed") {
1155 console.warn(error);
1156 }
1157 });
1158 }
1159
1160 function get_fsys_size(entry) {
1161 const path = entry.fields[1];
1162 if (self.fsys_sizes[path])
1163 return self.fsys_sizes[path];
1164
1165 if (self.fsys_sizes[path] === false)
1166 return null;
1167
1168 self.fsys_sizes[path] = false;
1169 cockpit.spawn(["stat", "-f", "-c", "[ %S, %f, %b ]", path], { err: "message" })
1170 .then(function (output) {
1171 const data = JSON.parse(output);
1172 self.fsys_sizes[path] = [(data[2] - data[1]) * data[0], data[2] * data[0]];
1173 client.update();
1174 })
1175 .catch(function () {
1176 self.fsys_sizes[path] = [0, 0];
1177 client.update();
1178 });
1179
1180 return null;
1181 }
1182
1183 function update_entry(entry, new_fields) {
1184 return spawn_nfs_mounts(["update", JSON.stringify(entry), JSON.stringify(new_fields)]);
1185 }
1186
1187 function add_entry(fields) {
1188 return spawn_nfs_mounts(["add", JSON.stringify(fields)]);
1189 }
1190
1191 function remove_entry(entry) {
1192 return spawn_nfs_mounts(["remove", JSON.stringify(entry)]);
1193 }
1194
1195 function mount_entry(entry) {
1196 return spawn_nfs_mounts(["mount", JSON.stringify(entry)]);
1197 }
1198
1199 function unmount_entry(entry) {
1200 return spawn_nfs_mounts(["unmount", JSON.stringify(entry)]);
1201 }
1202
1203 function stop_and_unmount_entry(users, entry) {
1204 return client.stop_mount_users(users).then(() => unmount_entry(entry));
1205 }
1206
1207 function stop_and_remove_entry(users, entry) {
1208 return client.stop_mount_users(users).then(() => remove_entry(entry));
1209 }
1210
1211 function entry_users(entry) {
1212 return client.find_mount_users(entry.fields[1], entry.mounted);
1213 }
1214
1215 function find_entry(remote, local) {
1216 for (let i = 0; i < self.entries.length; i++) {
1217 if (self.entries[i].fields[0] == remote && self.entries[i].fields[1] == local)
1218 return self.entries[i];
1219 }
1220 }
1221
1222 return self;
1223 }
1224
1225 client.nfs = nfs_mounts();
1226
1227 /* Legacy VDO CLI (RHEL 8), unsupported; newer versions use VDO through LVM API */
1228
1229 function legacy_vdo_overlay() {
1230 const self = {
1231 start,
1232
1233 volumes: [],
1234
1235 by_name: { },
1236 by_dev: { },
1237 by_backing_dev: { },
1238
1239 find_by_block,
1240 find_by_backing_block,
1241
1242 create
1243 };
1244
1245 function cmd(args) {
1246 return cockpit.spawn(["vdo"].concat(args),
1247 {
1248 superuser: "require",
1249 err: "message"
1250 });
1251 }
1252
1253 function update(data) {
1254 self.by_name = { };
1255 self.by_dev = { };
1256 self.by_backing_dev = { };
1257
1258 self.volumes = data.map(function (vol, index) {
1259 const name = vol.name;
1260
1261 function volcmd(args) {
1262 return cmd(args.concat(["--name", name]));
1263 }
1264
1265 const v = {
1266 name,
1267 broken: vol.broken,
1268 dev: "/dev/mapper/" + name,
1269 backing_dev: vol.device,
1270 logical_size: vol.logical_size,
1271 physical_size: vol.physical_size,
1272 index_mem: vol.index_mem,
1273 compression: vol.compression,
1274 deduplication: vol.deduplication,
1275 activated: vol.activated,
1276
1277 set_compression: function(val) {
1278 return volcmd([val ? "enableCompression" : "disableCompression"]);
1279 },
1280
1281 set_deduplication: function(val) {
1282 return volcmd([val ? "enableDeduplication" : "disableDeduplication"]);
1283 },
1284
1285 set_activate: function(val) {
1286 return volcmd([val ? "activate" : "deactivate"]);
1287 },
1288
1289 start: function() {
1290 return volcmd(["start"]);
1291 },
1292
1293 stop: function() {
1294 return volcmd(["stop"]);
1295 },
1296
1297 remove: function() {
1298 return volcmd(["remove"]);
1299 },
1300
1301 force_remove: function() {
1302 return volcmd(["remove", "--force"]);
1303 },
1304
1305 grow_physical: function() {
1306 return volcmd(["growPhysical"]);
1307 },
1308
1309 grow_logical: function(lsize) {
1310 return volcmd(["growLogical", "--vdoLogicalSize", lsize + "B"]);
1311 }
1312 };
1313
1314 self.by_name[v.name] = v;
1315 self.by_dev[v.dev] = v;
1316 self.by_backing_dev[v.backing_dev] = v;
1317
1318 return v;
1319 });
1320
1321 client.update();
1322 }
1323
1324 function start() {
1325 let buf = "";
1326
1327 return cockpit.spawn(["/bin/sh", "-c", "head -1 $(command -v vdo || echo /dev/null)"],
1328 { err: "ignore" })
1329 .then(function (shebang) {
1330 if (shebang != "") {
1331 self.python = shebang.replace(/#! */, "").trim("\n");
1332 cockpit.spawn([self.python, "--", "-"], { superuser: "try", err: "message" })
1333 .input(inotify_py + vdo_monitor_py)
1334 .stream(function (output) {
1335 buf += output;
1336 const lines = buf.split("\n");
1337 buf = lines[lines.length - 1];
1338 if (lines.length >= 2) {
1339 update(JSON.parse(lines[lines.length - 2]));
1340 }
1341 })
1342 .catch(function (error) {
1343 if (error != "closed") {
1344 console.warn(error);
1345 }
1346 });
1347 return true;
1348 } else {
1349 return false;
1350 }
1351 });
1352 }
1353
1354 function some(array, func) {
1355 let i;
1356 for (i = 0; i < array.length; i++) {
1357 const val = func(array[i]);
1358 if (val)
1359 return val;
1360 }
1361 return null;
1362 }
1363
1364 function find_by_block(block) {
1365 function check(encoded) { return self.by_dev[utils.decode_filename(encoded)] }
1366 return check(block.Device) || some(block.Symlinks, check);
1367 }
1368
1369 function find_by_backing_block(block) {
1370 function check(encoded) { return self.by_backing_dev[utils.decode_filename(encoded)] }
1371 return check(block.Device) || some(block.Symlinks, check);
1372 }
1373
1374 function create(options) {
1375 const args = ["create", "--name", options.name,
1376 "--device", utils.decode_filename(options.block.PreferredDevice)];
1377 if (options.logical_size !== undefined)
1378 args.push("--vdoLogicalSize", options.logical_size + "B");
1379 if (options.index_mem !== undefined)
1380 args.push("--indexMem", options.index_mem / (1024 * 1024 * 1024));
1381 if (options.compression !== undefined)
1382 args.push("--compression", options.compression ? "enabled" : "disabled");
1383 if (options.deduplication !== undefined)
1384 args.push("--deduplication", options.deduplication ? "enabled" : "disabled");
1385 if (options.emulate_512 !== undefined)
1386 args.push("--emulate512", options.emulate_512 ? "enabled" : "disabled");
1387 return cmd(args);
1388 }
1389
1390 return self;
1391 }
1392
1393 client.legacy_vdo_overlay = legacy_vdo_overlay();
1394
1395 /* Stratis */
1396
1397 client.stratis_start = () => {
1398 return stratis3_start();
1399 };
1400
1401 // We need to use the same revision for all interfaces, mixing them is
1402 // not allowed. If we need to bump it, it should be bumped here for all
1403 // of them at the same time.
1404 //
1405 // We try all these versions in order, and use the first we find.
1406 //
1407 const stratis3_interface_revisions = [8, 6];
1408
1409 async function stratis3_start() {
1410 const stratis = cockpit.dbus("org.storage.stratis3", { superuser: "try" });
1411
1412 // The rest of the code expects these to be initialized even if no
1413 // stratisd is found.
1414 client.stratis_pools = { };
1415 client.stratis_blockdevs = { };
1416 client.stratis_filesystems = { };
1417
1418 client.stratis_interface_revision = null;
1419 let last_error;
1420 for (const rev of stratis3_interface_revisions) {
1421 client.stratis_manager = stratis.proxy("org.storage.stratis3.Manager.r" + rev, "/org/storage/stratis3");
1422 client.stratis_manager.StoppedPools = {};
1423 try {
1424 await client.stratis_manager.wait();
1425 client.stratis_interface_revision = rev;
1426 break;
1427 } catch (e) {
1428 last_error = e;
1429 }
1430 }
1431
1432 if (!client.stratis_interface_revision)
1433 throw last_error;
1434
1435 client.stratis_store_passphrase = (desc, passphrase) => {
1436 return python.spawn(stratis3_set_key_py, [desc], { superuser: "require" })
1437 .input(passphrase);
1438 };
1439
1440 client.stratis_set_property = (proxy, prop, sig, value) => {
1441 // DBusProxy is smart enough to allow "proxy.Prop
1442 // = value" to just work, but we want to catch any
1443 // error ourselves, and we want to wait for the
1444 // method call to complete.
1445 return stratis.call(proxy.path, "org.freedesktop.DBus.Properties", "Set",
1446 [proxy.iface, prop, cockpit.variant(sig, value)]);
1447 };
1448
1449 client.stratis_stopped_pool_devnodes = (uuid) => {
1450 const devs = client.stratis_manager.StoppedPools[uuid]?.devs;
1451 if (!devs)
1452 return [];
1453 if (devs.t == 'aa{ss}')
1454 return devs.v.map(d => d.devnode);
1455 else if (devs.t == 'aa{sv}')
1456 return devs.v.map(d => d.devnode.v);
1457 else
1458 return [];
1459 };
1460
1461 client.features.stratis = true;
1462 client.stratis_pools = client.stratis_manager.client.proxies("org.storage.stratis3.pool.r" +
1463 client.stratis_interface_revision,
1464 "/org/storage/stratis3",
1465 { watch: false });
1466 client.stratis_blockdevs = client.stratis_manager.client.proxies("org.storage.stratis3.blockdev.r" +
1467 client.stratis_interface_revision,
1468 "/org/storage/stratis3",
1469 { watch: false });
1470 client.stratis_filesystems = client.stratis_manager.client.proxies("org.storage.stratis3.filesystem.r" +
1471 client.stratis_interface_revision,
1472 "/org/storage/stratis3",
1473 { watch: false });
1474
1475 await stratis.watch({ path_namespace: "/org/storage/stratis3" });
1476 client.stratis_manager.client.addEventListener('notify', (event, data) => {
1477 client.update();
1478 });
1479 }
1480
1481 function init_client(manager, callback) {
1482 if (client.manager)
1483 return;
1484
1485 client.storaged_client = manager.client;
1486 client.manager = manager;
1487
1488 init_proxies().then(() => init_model(callback));
1489 }
1490
1491 client.init = function init_storaged(callback) {
1492 const udisks = cockpit.dbus("org.freedesktop.UDisks2", { superuser: "try" });
1493 const udisks_manager = udisks.proxy("org.freedesktop.UDisks2.Manager",
1494 "/org/freedesktop/UDisks2/Manager", { watch: true });
1495
1496 udisks_manager.wait().then(() => init_client(udisks_manager, callback))
1497 .catch(ex => {
1498 console.warn("client.init(): udisks manager proxy failed:", JSON.stringify(ex));
1499 client.features = false;
1500 callback();
1501 });
1502
1503 udisks_manager.addEventListener("changed", () => init_client(udisks_manager, callback));
1504 };
1505
1506 client.wait_for = function wait_for(cond) {
1507 return new Promise(resolve => {
1508 function check() {
1509 const res = cond();
1510 if (res) {
1511 client.removeEventListener("changed", check);
1512 resolve(res);
1513 }
1514 }
1515
1516 client.addEventListener("changed", check);
1517 check();
1518 });
1519 };
1520
1521 client.get_config = (name, def) =>
1522 get_manifest_config_matchlist("storage", name, def, [client.os_release.PLATFORM_ID, client.os_release.ID]);
1523
1524 client.in_anaconda_mode = in_anaconda_mode;
1525
1526 client.strip_mount_point_prefix = (dir) => {
1527 const mpp = client.anaconda?.mount_point_prefix;
1528
1529 if (dir && mpp) {
1530 if (dir.indexOf(mpp) != 0)
1531 return false;
1532
1533 dir = dir.substring(mpp.length);
1534 if (dir == "")
1535 dir = "/";
1536 }
1537
1538 return dir;
1539 };
1540
1541 client.add_mount_point_prefix = (dir) => {
1542 const mpp = client.anaconda?.mount_point_prefix;
1543 if (mpp && dir != "") {
1544 if (dir == "/")
1545 dir = mpp;
1546 else
1547 dir = mpp + dir;
1548 }
1549 return dir;
1550 };
1551
1552 client.should_ignore_device = (devname) => {
1553 return client.anaconda?.available_devices && client.anaconda.available_devices.indexOf(devname) == -1;
1554 };
1555
1556 client.should_ignore_block = (block) => {
1557 return client.should_ignore_device(utils.decode_filename(block.PreferredDevice));
1558 };
1559
1560 export default client;