Debian Codegraph @

pkg/storaged/swap/swap.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 { 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 { useEvent } from "hooks";
13
14 import { StorageCard, StorageDescription, new_card } from "../pages.jsx";
15 import { std_format_action } from "../block/actions.jsx";
16 import {
17 fmt_size, decode_filename, encode_filename,
18 parse_options, unparse_options, extract_option,
19 } from "../utils.js";
20 import { std_lock_action } from "../crypto/actions.jsx";
21
22 const _ = cockpit.gettext;
23
24 async function set_swap_noauto(block, noauto) {
25 for (const conf of block.Configuration) {
26 if (conf[0] == "fstab") {
27 const options = parse_options(decode_filename(conf[1].opts.v));
28 extract_option(options, "defaults");
29 extract_option(options, "noauto");
30 if (noauto)
31 options.push("noauto");
32 if (options.length == 0)
33 options.push("defaults");
34 const new_conf = [
35 "fstab",
36 Object.assign({ }, conf[1],
37 {
38 opts: {
39 t: 'ay',
40 v: encode_filename(unparse_options(options))
41 }
42 })
43 ];
44 await block.UpdateConfigurationItem(conf, new_conf, { });
45 return;
46 }
47 }
48
49 await block.AddConfigurationItem(
50 ["fstab", {
51 dir: { t: 'ay', v: encode_filename("none") },
52 type: { t: 'ay', v: encode_filename("swap") },
53 opts: { t: 'ay', v: encode_filename(noauto ? "noauto" : "defaults") },
54 freq: { t: 'i', v: 0 },
55 passno: { t: 'i', v: 0 },
56 "track-parents": { t: 'b', v: true }
57 }], { });
58 }
59
60 export function make_swap_card(next, backing_block, content_block) {
61 const block_swap = client.blocks_swap[content_block.path];
62
63 async function start() {
64 await block_swap.Start({});
65 await set_swap_noauto(content_block, false);
66 }
67
68 async function stop() {
69 await block_swap.Stop({});
70 await set_swap_noauto(content_block, true);
71 }
72
73 return new_card({
74 title: _("Swap"),
75 next,
76 component: SwapCard,
77 props: { block: content_block, block_swap },
78 actions: [
79 std_lock_action(backing_block, content_block),
80 (block_swap && block_swap.Active
81 ? { title: _("Stop"), action: stop }
82 : null),
83 (block_swap && !block_swap.Active
84 ? { title: _("Start"), action: start }
85 : null),
86 std_format_action(backing_block, content_block),
87 ]
88 });
89 }
90
91 export const SwapCard = ({ card, block, block_swap }) => {
92 const is_active = block_swap && block_swap.Active;
93 let used;
94
95 useEvent(client.swap_sizes, "changed");
96
97 if (is_active) {
98 const samples = client.swap_sizes.data[decode_filename(block.Device)];
99 if (samples)
100 used = fmt_size(samples[0] - samples[1]);
101 else
102 used = _("Unknown");
103 } else {
104 used = "-";
105 }
106
107 return (
108 <StorageCard card={card}>
109 <CardBody>
110 <DescriptionList className="pf-m-horizontal-on-sm">
111 <StorageDescription title={_("Used")} value={used} />
112 </DescriptionList>
113 </CardBody>
114 </StorageCard>
115 );
116 };