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