| 1 |
/* |
| 2 |
* Copyright (C) 2023 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import client from "./client.js"; |
| 7 |
|
| 8 |
import { decode_filename } from "./utils.js"; |
| 9 |
import { parse_subvol_from_options } from "./btrfs/utils.jsx"; |
| 10 |
|
| 11 |
function parse_parent_from_options(options) { |
| 12 |
const parent_match = options.match(/x-parent=(?<parent>[\w\\-]+)/); |
| 13 |
if (parent_match) { |
| 14 |
return parent_match.groups.parent; |
| 15 |
} else |
| 16 |
return null; |
| 17 |
} |
| 18 |
|
| 19 |
function uuid_equal(a, b) { |
| 20 |
return a.replace("-", "").toUpperCase() == b.replace("-", "").toUpperCase(); |
| 21 |
} |
| 22 |
|
| 23 |
function device_name(block) { |
| 24 |
// Prefer symlinks in /dev/stratis/. |
| 25 |
return (block.Symlinks.map(decode_filename).find(n => n.indexOf("/dev/stratis/") == 0) || |
| 26 |
decode_filename(block.PreferredDevice)); |
| 27 |
} |
| 28 |
|
| 29 |
export function remember_passphrase(block, passphrase) { |
| 30 |
if (!client.in_anaconda_mode()) |
| 31 |
return; |
| 32 |
|
| 33 |
if (!window.isSecureContext) |
| 34 |
return; |
| 35 |
|
| 36 |
try { |
| 37 |
const passphrases = JSON.parse(window.sessionStorage.getItem("cockpit_passphrases")) || { }; |
| 38 |
passphrases[device_name(block)] = passphrase; |
| 39 |
window.sessionStorage.setItem("cockpit_passphrases", JSON.stringify(passphrases)); |
| 40 |
} catch { |
| 41 |
console.warn("Can't record passphrases"); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
export function export_mount_point_mapping() { |
| 46 |
if (!client.in_anaconda_mode()) |
| 47 |
return; |
| 48 |
|
| 49 |
function tab_info(config, for_parent) { |
| 50 |
let dir; |
| 51 |
let subvols; |
| 52 |
|
| 53 |
for (const c of config) { |
| 54 |
if (c[0] == "fstab") { |
| 55 |
const o = decode_filename(c[1].opts.v); |
| 56 |
if (for_parent && !uuid_equal(parse_parent_from_options(o), for_parent)) |
| 57 |
continue; |
| 58 |
|
| 59 |
const d = client.strip_mount_point_prefix(decode_filename(c[1].dir.v)); |
| 60 |
if (d) { |
| 61 |
const sv = parse_subvol_from_options(o); |
| 62 |
if (sv) { |
| 63 |
if (sv.pathname) { |
| 64 |
if (!subvols) |
| 65 |
subvols = { }; |
| 66 |
subvols[sv.pathname] = { dir: d }; |
| 67 |
} |
| 68 |
} else if (!dir) { |
| 69 |
dir = d; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
if (dir || subvols) |
| 76 |
return { |
| 77 |
type: "filesystem", |
| 78 |
dir, |
| 79 |
subvolumes: subvols |
| 80 |
}; |
| 81 |
|
| 82 |
for (const c of config) { |
| 83 |
if (c[0] == "crypttab") { |
| 84 |
const o = decode_filename(c[1].options.v); |
| 85 |
if (for_parent && !uuid_equal(parse_parent_from_options(o), for_parent)) |
| 86 |
continue; |
| 87 |
|
| 88 |
const device = decode_filename(c[1].device.v); |
| 89 |
let content_info; |
| 90 |
if (device.startsWith("UUID=")) { |
| 91 |
content_info = tab_info(config, device.substring(5)); |
| 92 |
} |
| 93 |
|
| 94 |
return { |
| 95 |
type: "crypto", |
| 96 |
content: content_info, |
| 97 |
}; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
function block_info(block) { |
| 103 |
if (block.IdUsage == "filesystem") { |
| 104 |
return tab_info(block.Configuration); |
| 105 |
} else if (block.IdUsage == "other" && block.IdType == "swap") { |
| 106 |
return { |
| 107 |
type: "swap", |
| 108 |
}; |
| 109 |
} else if (block.IdUsage == "crypto") { |
| 110 |
const cleartext_block = client.blocks_cleartext[block.path]; |
| 111 |
|
| 112 |
let content_info; |
| 113 |
if (cleartext_block) |
| 114 |
content_info = block_info(cleartext_block); |
| 115 |
else { |
| 116 |
const block_crypto = client.blocks_crypto[block.path]; |
| 117 |
if (block_crypto) |
| 118 |
content_info = tab_info(block_crypto.ChildConfiguration, block.IdUUID); |
| 119 |
} |
| 120 |
|
| 121 |
if (content_info) { |
| 122 |
return { |
| 123 |
type: "crypto", |
| 124 |
cleartext_device: cleartext_block && device_name(cleartext_block), |
| 125 |
content: content_info, |
| 126 |
}; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
const mpm = { }; |
| 132 |
for (const p in client.blocks) { |
| 133 |
const b = client.blocks[p]; |
| 134 |
mpm[device_name(b)] = block_info(b); |
| 135 |
} |
| 136 |
|
| 137 |
// Add inactive logical volumes |
| 138 |
for (const vg_p in client.vgroups) { |
| 139 |
const vg = client.vgroups[vg_p]; |
| 140 |
for (const lv of client.vgroups_lvols[vg_p] || []) { |
| 141 |
const b = client.lvols_block[lv.path]; |
| 142 |
const dev_name = "/dev/" + vg.Name + "/" + lv.Name; |
| 143 |
if (!b && !mpm[dev_name]) { |
| 144 |
const info = tab_info(lv.ChildConfiguration, lv.UUID); |
| 145 |
if (info) |
| 146 |
mpm[dev_name] = info; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
window.sessionStorage.setItem("cockpit_mount_points", JSON.stringify(mpm)); |
| 152 |
} |