Line data Source code
1 : /*
2 : * Copyright (C) 2023 Red Hat, Inc.
3 : * SPDX-License-Identifier: LGPL-2.1-or-later
4 : */
5 :
6 113 : import cockpit from "cockpit";
7 : import client from "../client";
8 :
9 : import { mdraid_name, validate_mdraid_name, get_available_spaces, prepare_available_spaces } from "../utils.js";
10 : import { dialog_open, TextInput, SelectOne, SelectSpaces } from "../dialog.jsx";
11 :
12 113 : const _ = cockpit.gettext;
13 :
14 5 : export function create_mdraid() {
15 5 : function mdraid_exists(name) {
16 1 : for (const p in client.mdraids) {
17 1 : if (mdraid_name(client.mdraids[p]) == name)
18 1 : return true;
19 1 : }
20 5 : return false;
21 5 : }
22 :
23 5 : let name;
24 5 : for (let i = 0; i < 1000; i++) {
25 5 : name = "raid" + i.toFixed();
26 5 : if (!mdraid_exists(name))
27 5 : break;
28 5 : }
29 :
30 5 : dialog_open({
31 5 : Title: _("Create RAID device"),
32 5 : Fields: [
33 5 : TextInput("name", _("Name"), {
34 5 : value: name,
35 5 : validate: validate_mdraid_name,
36 5 : }),
37 5 : SelectOne("level", _("RAID level"),
38 5 : {
39 5 : value: "raid5",
40 5 : choices: [
41 5 : {
42 5 : value: "raid0",
43 5 : title: _("RAID 0 (stripe)")
44 5 : },
45 5 : {
46 5 : value: "raid1",
47 5 : title: _("RAID 1 (mirror)")
48 5 : },
49 5 : {
50 5 : value: "raid4",
51 5 : title: _("RAID 4 (dedicated parity)")
52 5 : },
53 5 : {
54 5 : value: "raid5",
55 5 : title: _("RAID 5 (distributed parity)")
56 5 : },
57 5 : {
58 5 : value: "raid6",
59 5 : title: _("RAID 6 (double distributed parity)")
60 5 : },
61 5 : {
62 5 : value: "raid10",
63 5 : title: _("RAID 10 (stripe of mirrors)")
64 5 : }
65 5 : ]
66 5 : }),
67 5 : SelectOne("chunk", _("Chunk size"),
68 5 : {
69 5 : value: "512",
70 5 : visible: function (vals) {
71 5 : return vals.level != "raid1";
72 5 : },
73 5 : choices: [
74 5 : { value: "4", title: _("4 KiB") },
75 5 : { value: "8", title: _("8 KiB") },
76 5 : { value: "16", title: _("16 KiB") },
77 5 : { value: "32", title: _("32 KiB") },
78 5 : { value: "64", title: _("64 KiB") },
79 5 : { value: "128", title: _("128 KiB") },
80 5 : { value: "512", title: _("512 KiB") },
81 5 : { value: "1024", title: _("1 MiB") },
82 5 : { value: "2048", title: _("2 MiB") }
83 5 : ]
84 5 : }),
85 5 : SelectSpaces("disks", _("Disks"),
86 5 : {
87 5 : empty_warning: _("No disks are available."),
88 4 : validate: function (disks, vals) {
89 1 : const disks_needed = vals.level == "raid6" ? 4 : 2;
90 4 : if (disks.length < disks_needed)
91 1 : return cockpit.format(cockpit.ngettext("At least $0 disk is needed.", "At least $0 disks are needed.", disks_needed),
92 1 : disks_needed);
93 4 : },
94 5 : spaces: get_available_spaces()
95 5 : })
96 5 : ],
97 5 : Action: {
98 5 : Title: _("Create"),
99 4 : action: function (vals) {
100 4 : return prepare_available_spaces(client, vals.disks).then(paths => {
101 4 : return client.manager.MDRaidCreate(paths, vals.level,
102 1 : vals.name, (vals.chunk || 0) * 1024,
103 4 : { });
104 4 : });
105 4 : }
106 5 : }
107 5 : });
108 5 : }
|