-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdisks.rs
More file actions
335 lines (315 loc) · 13.3 KB
/
disks.rs
File metadata and controls
335 lines (315 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
use ratatui::Frame;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Paragraph, Wrap};
use crate::app::AppState;
pub(super) fn render(frame: &mut Frame, app: &mut AppState, area: Rect) {
let mode = match app.disks_mode_index {
0 => "Best-effort partition layout",
1 => "Manual Partitioning",
_ => "Pre-mounted configuration",
};
// Description header
let mut desc_lines = vec![Line::from(Span::styled(
"Description",
Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD),
))];
desc_lines.push(Line::from("Disk partitioning divides a storage device into independent sections for system management. The root partition (/) holds essential OS files, home (/home) stores user data, boot (/boot) contains files needed to start the system, and swap ([SWAP]) provides virtual memory to supplement RAM, aiding stability and hibernation."));
if app.disks_mode_index == 1 {
// Manual Partitioning: vertical split then 50/50 columns
let vchunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Percentage(45), Constraint::Percentage(55)])
.split(area);
let description = Paragraph::new(desc_lines)
.block(
Block::default()
.borders(Borders::ALL)
.title(" Description "),
)
.wrap(Wrap { trim: true });
frame.render_widget(description, vchunks[0]);
// Split the lower area horizontally
let cols = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
.split(vchunks[1]);
// Left pane: general info
let mut left_info: Vec<Line> = Vec::new();
left_info.push(Line::from(format!("Disk mode: {mode}")));
if let Some(dev) = &app.disks_selected_device {
left_info.push(Line::from(format!("Selected drive: {dev}")));
}
if let Some(label) = &app.disks_label {
left_info.push(Line::from(format!("Label: {label}")));
}
left_info.push(Line::from(format!(
"Wipe: {}",
if app.disks_wipe { "Yes" } else { "No" }
)));
if let Some(align) = &app.disks_align {
left_info.push(Line::from(format!("Align: {align}")));
}
let has_btrfs_root = app.disks_partitions.iter().any(|p| {
p.role
.as_deref()
.map(|r| r.eq_ignore_ascii_case("ROOT"))
.unwrap_or(false)
&& p.fs.as_deref() == Some("btrfs")
});
if has_btrfs_root {
let btrfs_preset_label = match app.btrfs_subvolume_preset {
1 => "Standard (@, @home, @snapshots)",
2 => "Extended (@, @home, @var_log, @snapshots)",
_ => "Flat (no subvolumes)",
};
left_info.push(Line::from(format!(
"Btrfs subvolumes: {btrfs_preset_label}"
)));
}
let left_block = Paragraph::new(left_info)
.block(Block::default().borders(Borders::ALL).title(" Info "))
.wrap(Wrap { trim: true });
frame.render_widget(left_block, cols[0]);
// Right pane: partitions
let mut right_lines: Vec<String> = Vec::new();
for p in &app.disks_partitions {
let role = p.role.clone().unwrap_or_default();
let fs = p.fs.clone().unwrap_or_default();
let start = p.start.clone().unwrap_or_default();
let size = p.size.clone().unwrap_or_default();
let flags = if p.flags.is_empty() {
String::new()
} else {
p.flags.join(",")
};
let mp = p.mountpoint.clone().unwrap_or_default();
let enc = if p.encrypt.unwrap_or(false) {
" enc"
} else {
""
};
let mut line = String::new();
if !role.is_empty() {
line.push_str(&format!("({role}) "));
}
if !fs.is_empty() {
line.push_str(&format!("{fs} "));
}
if !start.is_empty() || !size.is_empty() {
line.push_str(&format!("[{start}..{size}] "));
}
if !flags.is_empty() {
line.push_str(&format!("flags:{flags} "));
}
if !mp.is_empty() {
line.push_str(&format!("-> {mp} "));
}
line.push_str(enc);
if line.is_empty() {
line = "(empty)".into();
}
right_lines.push(line.trim().to_string());
}
let mut left_lines: Vec<String> = Vec::new();
if let Some(dev) = &app.disks_selected_device
&& let Ok(output) = std::process::Command::new("lsblk")
.args([
"-J",
"-b",
"-o",
"NAME,PATH,TYPE,SIZE,FSTYPE,START,PHY-SEC,LOG-SEC",
])
.output()
&& output.status.success()
&& let Ok(json) = serde_json::from_slice::<serde_json::Value>(&output.stdout)
&& let Some(blockdevices) = json.get("blockdevices").and_then(|v| v.as_array())
{
for devnode in blockdevices {
let path = devnode.get("path").and_then(|v| v.as_str()).unwrap_or("");
if !path.starts_with(dev) {
continue;
}
let sector_size = devnode
.get("phy-sec")
.and_then(|v| v.as_u64())
.or_else(|| devnode.get("log-sec").and_then(|v| v.as_u64()))
.unwrap_or(512);
if let Some(children) = devnode.get("children").and_then(|v| v.as_array()) {
for ch in children {
let ch_type = ch.get("type").and_then(|v| v.as_str()).unwrap_or("");
if ch_type != "part" {
continue;
}
let name = ch.get("name").and_then(|v| v.as_str()).unwrap_or("");
let size_b = ch.get("size").and_then(|v| v.as_u64()).unwrap_or(0);
let start_sectors = ch.get("start").and_then(|v| v.as_u64()).unwrap_or(0);
let start_b = start_sectors.saturating_mul(sector_size);
let end_b = start_b.saturating_add(size_b);
let fs = ch.get("fstype").and_then(|v| v.as_str()).unwrap_or("");
left_lines.push(format!(
"{} {} [{}..{}] {}",
name,
crate::app::AppState::human_bytes(size_b),
start_b,
end_b,
fs
));
}
}
}
}
let right_block = Block::default().borders(Borders::ALL).title(" Partitions ");
frame.render_widget(right_block.clone(), cols[1]);
let right_inner = right_block.inner(cols[1]);
let mut combined: Vec<String> = Vec::new();
if !left_lines.is_empty() {
combined.push("Existing:".into());
combined.extend(left_lines.into_iter().map(|s| format!("- {s}")));
}
if !right_lines.is_empty() {
combined.push("Created:".into());
combined.extend(right_lines.into_iter().map(|s| format!("- {s}")));
}
if combined.is_empty() {
combined.push("(none)".into());
}
let part_p = Paragraph::new(combined.join("\n")).wrap(Wrap { trim: true });
frame.render_widget(part_p, right_inner);
return;
}
if app.disks_mode_index == 0 {
let mut info_lines = vec![Line::from(Span::styled(
"Info",
Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD),
))];
let bl = match app.bootloader_index {
0 => "systemd-boot",
1 => "GRUB",
2 => crate::app::bootloader::EFISTUB_SLUG_LABEL,
3 => "Limine",
_ => "other",
};
info_lines.push(Line::from(format!("Bootloader: {bl}")));
let fw = if app.is_uefi() { "UEFI" } else { "BIOS" };
info_lines.push(Line::from(format!("Firmware: {fw}")));
if let Some(dev) = &app.disks_selected_device {
info_lines.push(Line::from(format!("Selected drive: {dev}")));
}
info_lines.push(Line::from("Planned layout:"));
let swap_gib = app.swap_size_mib as f64 / 1024.0;
if app.is_uefi() {
info_lines.push(Line::from("- gpt: 1024MiB EFI (FAT, ESP) -> /boot"));
if app.swap_enabled {
info_lines.push(Line::from(format!("- swap: {swap_gib:.2}GiB")));
}
let enc = if app.disk_encryption_type_index == 1 {
" (LUKS)"
} else {
""
};
info_lines.push(Line::from(format!("- root: btrfs{enc} (rest)")));
} else {
info_lines.push(Line::from("- gpt: 1MiB bios_boot [bios_grub]"));
if app.swap_enabled {
info_lines.push(Line::from(format!("- swap: {swap_gib:.2}GiB")));
}
let enc = if app.disk_encryption_type_index == 1 {
" (LUKS)"
} else {
""
};
info_lines.push(Line::from(format!("- root: btrfs{enc} (rest)")));
if bl != "GRUB" && bl != "Limine" {
info_lines.push(Line::from(
"Warning: Selected bootloader requires UEFI; choose GRUB or Limine for BIOS.",
));
}
}
let btrfs_preset_label = match app.btrfs_subvolume_preset {
1 => "Standard (@, @home, @snapshots)",
2 => "Extended (@, @home, @var_log, @snapshots)",
_ => "Flat (no subvolumes)",
};
info_lines.push(Line::from(format!(
"Btrfs subvolumes: {btrfs_preset_label}"
)));
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Percentage(45), Constraint::Percentage(55)])
.split(area);
let description = Paragraph::new(desc_lines)
.block(
Block::default()
.borders(Borders::ALL)
.title(" Description "),
)
.wrap(Wrap { trim: true });
frame.render_widget(description, chunks[0]);
let info = Paragraph::new(info_lines)
.block(Block::default().borders(Borders::ALL).title(" Info "))
.wrap(Wrap { trim: true });
frame.render_widget(info, chunks[1]);
} else {
// Pre-mounted mode info panel
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Percentage(35), Constraint::Percentage(65)])
.split(area);
let description = Paragraph::new(desc_lines)
.block(
Block::default()
.borders(Borders::ALL)
.title(" Description "),
)
.wrap(Wrap { trim: true });
frame.render_widget(description, chunks[0]);
let mut info_lines = vec![Line::from(Span::styled(
"Pre-mounted Configuration",
Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD),
))];
info_lines.push(Line::from(
"Filesystems should be mounted at /mnt before proceeding.",
));
info_lines.push(Line::from(""));
// Pre-mounted mount/swap data (refreshed in AppState, not here — see refresh_pre_mounted_probe_cache)
if app.pre_mounted_cache_findmnt_failed {
info_lines.push(Line::from(Span::styled(
"No mounts detected under /mnt",
Style::default().fg(Color::Red),
)));
info_lines.push(Line::from(
"Mount your filesystems at /mnt first, then select this mode.",
));
} else {
info_lines.push(Line::from(Span::styled(
"Detected mounts:",
Style::default().add_modifier(Modifier::BOLD),
)));
for line in &app.pre_mounted_cache_mount_lines {
info_lines.push(Line::from(line.clone()));
}
}
if !app.pre_mounted_cache_swap_devices.is_empty() {
info_lines.push(Line::from(""));
info_lines.push(Line::from(Span::styled(
"Active swap:",
Style::default().add_modifier(Modifier::BOLD),
)));
for dev in &app.pre_mounted_cache_swap_devices {
info_lines.push(Line::from(format!(" {dev}")));
}
}
let info = Paragraph::new(info_lines)
.block(Block::default().borders(Borders::ALL).title(" Info "))
.wrap(Wrap { trim: true });
frame.render_widget(info, chunks[1]);
}
}