| 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 { for_each_async, reload_systemd, encode_filename } from "../utils.js"; |
| 11 |
import { dialog_open } from "../dialog.jsx"; |
| 12 |
import { TangKeyVerification } from "../crypto/tang.jsx"; |
| 13 |
|
| 14 |
const _ = cockpit.gettext; |
| 15 |
|
| 16 |
export function validate_pool_name(pool, name) { |
| 17 |
if (name == "") |
| 18 |
return _("Name can not be empty."); |
| 19 |
if ((!pool || name != pool.Name) && client.stratis_poolnames_pool[name]) |
| 20 |
return _("A pool with this name exists already."); |
| 21 |
} |
| 22 |
|
| 23 |
export function std_reply(result, code, message) { |
| 24 |
if (code) |
| 25 |
return Promise.reject(message); |
| 26 |
else |
| 27 |
return Promise.resolve(result); |
| 28 |
} |
| 29 |
|
| 30 |
export function with_keydesc(client, pool, func) { |
| 31 |
if (!pool.KeyDescription || |
| 32 |
!pool.KeyDescription[0] || |
| 33 |
!pool.KeyDescription[1][0]) { |
| 34 |
return func(false); |
| 35 |
} else { |
| 36 |
const keydesc = pool.KeyDescription[1][1]; |
| 37 |
return client.stratis_manager.ListKeys() |
| 38 |
.catch(() => []) // not-covered: internal error |
| 39 |
.then(keys => func(keydesc, keys.indexOf(keydesc) >= 0)); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
export function with_stored_passphrase(client, keydesc, passphrase, func) { |
| 44 |
return client.stratis_store_passphrase(keydesc, passphrase) |
| 45 |
.then(func) |
| 46 |
.finally(() => { |
| 47 |
return client.stratis_manager.UnsetKey(keydesc) |
| 48 |
.then(std_reply) |
| 49 |
.catch(ex => { console.warn("Failed to remove passphrase from key ring", ex.toString()) }); // not-covered: internal error |
| 50 |
}); |
| 51 |
} |
| 52 |
|
| 53 |
export function get_unused_keydesc(client, desc_prefix) { |
| 54 |
return client.stratis_manager.ListKeys() |
| 55 |
.catch(() => []) // not-covered: internal error |
| 56 |
.then(keys => { |
| 57 |
let desc; |
| 58 |
for (let i = 0; i < 1000; i++) { |
| 59 |
desc = desc_prefix + (i > 0 ? "." + i.toFixed() : ""); |
| 60 |
if (keys.indexOf(desc) == -1) |
| 61 |
break; |
| 62 |
} |
| 63 |
return desc; |
| 64 |
}); |
| 65 |
} |
| 66 |
|
| 67 |
export function confirm_tang_trust(url, adv, action) { |
| 68 |
dialog_open({ |
| 69 |
Title: _("Verify key"), |
| 70 |
Body: <TangKeyVerification url={url} adv={adv} />, |
| 71 |
Action: { |
| 72 |
Title: _("Trust key"), |
| 73 |
action |
| 74 |
} |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
export function check_stratis_warnings(client, enter_warning) { |
| 79 |
for (const p in client.stratis_pools) { |
| 80 |
const blockdevs = client.stratis_pool_blockdevs[p] || []; |
| 81 |
const pool = client.stratis_pools[p]; |
| 82 |
if (blockdevs.some(bd => bd.NewPhysicalSize[0] && Number(bd.NewPhysicalSize[1]) > Number(bd.TotalPhysicalSize))) |
| 83 |
enter_warning(p, { warning: "unused-blockdevs" }); |
| 84 |
if (pool.AvailableActions && pool.AvailableActions !== "fully_operational") |
| 85 |
enter_warning(p, { warning: "not-fully-operational" }); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
function teardown_block(block) { |
| 90 |
return for_each_async(block.Configuration, c => block.RemoveConfigurationItem(c, {})); |
| 91 |
} |
| 92 |
|
| 93 |
export function destroy_filesystem(fsys) { |
| 94 |
const block = client.slashdevs_block[fsys.Devnode]; |
| 95 |
const pool = client.stratis_pools[fsys.Pool]; |
| 96 |
|
| 97 |
return teardown_block(block).then(() => pool.DestroyFilesystems([fsys.path]).then(std_reply)); |
| 98 |
} |
| 99 |
|
| 100 |
export function validate_fs_name(fsys, name, filesystems) { |
| 101 |
if (name == "") |
| 102 |
return _("Name can not be empty."); |
| 103 |
if (!fsys || name != fsys.Name) { |
| 104 |
for (const fs of filesystems) { |
| 105 |
if (fs.Name == name) |
| 106 |
return _("A filesystem with this name exists already in this pool."); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
export function set_mount_options(path, vals, forced_options) { |
| 112 |
let mount_options = []; |
| 113 |
|
| 114 |
if (vals.variant == "nomount" || vals.at_boot == "never") |
| 115 |
mount_options.push("noauto"); |
| 116 |
if (vals.mount_options?.ro) |
| 117 |
mount_options.push("ro"); |
| 118 |
if (vals.at_boot == "never") |
| 119 |
mount_options.push("x-cockpit-never-auto"); |
| 120 |
if (vals.at_boot == "nofail") |
| 121 |
mount_options.push("nofail"); |
| 122 |
if (vals.at_boot == "netdev") |
| 123 |
mount_options.push("_netdev"); |
| 124 |
if (vals.mount_options?.extra) |
| 125 |
mount_options.push(vals.mount_options.extra); |
| 126 |
|
| 127 |
mount_options = mount_options.concat(forced_options); |
| 128 |
|
| 129 |
let mount_point = vals.mount_point; |
| 130 |
if (mount_point == "") |
| 131 |
return Promise.resolve(); |
| 132 |
if (mount_point[0] != "/") |
| 133 |
mount_point = "/" + mount_point; |
| 134 |
mount_point = client.add_mount_point_prefix(mount_point); |
| 135 |
|
| 136 |
const config = |
| 137 |
["fstab", |
| 138 |
{ |
| 139 |
dir: { t: 'ay', v: encode_filename(mount_point) }, |
| 140 |
type: { t: 'ay', v: encode_filename("auto") }, |
| 141 |
opts: { t: 'ay', v: encode_filename(mount_options.join(",") || "defaults") }, |
| 142 |
freq: { t: 'i', v: 0 }, |
| 143 |
passno: { t: 'i', v: 0 }, |
| 144 |
} |
| 145 |
]; |
| 146 |
|
| 147 |
function udisks_block_for_stratis_fsys() { |
| 148 |
const fsys = client.stratis_filesystems[path]; |
| 149 |
return fsys && client.slashdevs_block[fsys.Devnode]; |
| 150 |
} |
| 151 |
|
| 152 |
return client.wait_for(udisks_block_for_stratis_fsys) |
| 153 |
.then(block => { |
| 154 |
// HACK - need a explicit "change" event |
| 155 |
return block.Rescan({}) |
| 156 |
.then(() => { |
| 157 |
return client.wait_for(() => client.blocks_fsys[block.path]) |
| 158 |
.then(fsys => { |
| 159 |
return block.AddConfigurationItem(config, {}) |
| 160 |
.then(reload_systemd) |
| 161 |
.then(() => { |
| 162 |
if (vals.variant != "nomount") |
| 163 |
return client.mount_at(block, mount_point); |
| 164 |
else |
| 165 |
return Promise.resolve(); |
| 166 |
}); |
| 167 |
}); |
| 168 |
}); |
| 169 |
}); |
| 170 |
} |