-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpartitioning.rs
More file actions
298 lines (269 loc) · 11.1 KB
/
partitioning.rs
File metadata and controls
298 lines (269 loc) · 11.1 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
// DEPRECATED: Superseded by `crate::core::storage::StoragePlan` + `StoragePlanner`.
// Kept temporarily so existing integration tests in tests/logic.rs still compile.
// Remove once those tests migrate to the storage planner API.
use crate::core::state::AppState;
#[derive(Clone, Debug)]
pub struct PartitionPlan {
pub commands: Vec<String>,
}
impl PartitionPlan {
pub fn new(commands: Vec<String>) -> Self {
Self { commands }
}
}
pub struct PartitioningService;
impl PartitioningService {
pub fn build_plan(state: &AppState, device: &str) -> PartitionPlan {
let mut part_cmds: Vec<String> = Vec::new();
// Check if manual partitioning mode is selected
let is_manual_mode = state.disks_mode_index == 1;
if is_manual_mode && !state.disks_partitions.is_empty() {
// Manual partitioning mode: process the disks_partitions vector
Self::build_manual_partition_plan(state, device, &mut part_cmds);
} else {
// Best-effort automatic partitioning mode
Self::build_automatic_partition_plan(state, device, &mut part_cmds);
}
// Debug summary (no raw command strings)
let label = state.disks_label.clone().unwrap_or_else(|| "gpt".into());
let align = state.disks_align.clone().unwrap_or_else(|| "1MiB".into());
if is_manual_mode {
let mut specs = Vec::new();
for p in state.disks_partitions.iter() {
if let Some(spec_device) = &p.name
&& spec_device != device
{
continue;
}
let role = p.role.clone().unwrap_or_else(|| "OTHER".into());
let fs = p.fs.clone().unwrap_or_else(|| "ext4".into());
let size = p.size.clone().unwrap_or_else(|| "?".into());
specs.push(format!("{role}:{fs}:{size}"));
}
state.debug_log(&format!(
"partitioning: manual device={} label={} wipe={} align={} specs_count={} [{}]",
device,
label,
state.disks_wipe,
align,
specs.len(),
specs.join(", ")
));
} else {
state.debug_log(&format!(
"partitioning: auto device={} label={} wipe={} align={} uefi={} swap={} luks={}",
device,
label,
state.disks_wipe,
align,
state.is_uefi(),
state.swap_enabled,
state.disk_encryption_type_index == 1
));
}
PartitionPlan::new(part_cmds)
}
fn build_automatic_partition_plan(state: &AppState, device: &str, part_cmds: &mut Vec<String>) {
// NOTE: LVM/RAID and btrfs subvolume layouts are handled by StoragePlanner (Phase 4-6).
let label = state.disks_label.clone().unwrap_or_else(|| "gpt".into());
if state.disks_wipe {
part_cmds.push(format!("wipefs -a {device}"));
}
let align = state.disks_align.clone().unwrap_or_else(|| "1MiB".into());
part_cmds.push(format!("parted -s {device} mklabel {label}"));
part_cmds.push(format!("partprobe {device} || true"));
part_cmds.push("udevadm settle".into());
let mut next_start = align.clone();
// If system boots via UEFI, always create an ESP as partition 1
if state.is_uefi() {
part_cmds.push(format!(
"parted -s {device} mkpart ESP fat32 {next_start} 1025MiB"
));
part_cmds.push(format!("parted -s {device} set 1 esp on"));
part_cmds.push(format!(
"mkfs.fat -F 32 {}",
Self::get_partition_path(device, 1)
));
next_start = "1025MiB".into();
} else {
part_cmds.push(format!(
"parted -s {device} mkpart biosboot {next_start} 2MiB"
));
part_cmds.push(format!("parted -s {device} set 1 bios_grub on"));
next_start = "2MiB".into();
}
let swap_part_num: u32 = 2;
let root_part_num: u32 = if state.swap_enabled { 3 } else { 2 };
let swap_part_path = Self::get_partition_path(device, swap_part_num);
let root_part_path = Self::get_partition_path(device, root_part_num);
if state.swap_enabled {
let swap_start_mib: u64 = if state.is_uefi() { 1025 } else { 2 };
let swap_end_mib = swap_start_mib + state.swap_size_mib;
let swap_end = format!("{swap_end_mib}MiB");
part_cmds.push(format!(
"parted -s {device} mkpart swap linux-swap {next_start} {swap_end}"
));
part_cmds.push(format!("mkswap {swap_part_path}"));
next_start = swap_end;
}
part_cmds.push(format!(
"parted -s {device} mkpart root btrfs {next_start} 100%"
));
let luks = state.disk_encryption_type_index == 1;
if luks {
// Legacy string plan (bash -lc each line). Real installs use StoragePlanner + InstallCmd.
part_cmds.push(
"modprobe -q dm_crypt 2>/dev/null || modprobe -q dm-crypt 2>/dev/null || true"
.into(),
);
part_cmds.push(format!(
"cryptsetup luksFormat --type luks2 -q {root_part_path}"
));
part_cmds.push("udevadm settle".into());
part_cmds.push(format!(
"cryptsetup open --type luks {root_part_path} cryptroot"
));
part_cmds.push("mkfs.btrfs -f /dev/mapper/cryptroot".into());
} else {
part_cmds.push(format!("mkfs.btrfs -f {root_part_path}"));
}
}
fn build_manual_partition_plan(state: &AppState, device: &str, part_cmds: &mut Vec<String>) {
let label = state.disks_label.clone().unwrap_or_else(|| "gpt".into());
if state.disks_wipe {
part_cmds.push(format!("wipefs -a {device}"));
}
part_cmds.push(format!("parted -s {device} mklabel {label}"));
part_cmds.push(format!("partprobe {device} || true"));
part_cmds.push("udevadm settle".into());
// Sort partitions by start position to ensure correct order
let mut sorted_partitions = state.disks_partitions.clone();
sorted_partitions.sort_by(|a, b| {
let start_a = a
.start
.as_ref()
.and_then(|s| s.parse::<u64>().ok())
.unwrap_or(0);
let start_b = b
.start
.as_ref()
.and_then(|s| s.parse::<u64>().ok())
.unwrap_or(0);
start_a.cmp(&start_b)
});
let mut partition_number = 1;
for spec in &sorted_partitions {
// Skip partitions not for this device
if let Some(spec_device) = &spec.name
&& spec_device != device
{
continue;
}
let role = spec.role.as_deref().unwrap_or("OTHER");
let fs = spec.fs.as_deref().unwrap_or("ext4");
let start = spec.start.as_deref().unwrap_or("0");
let size = spec.size.as_deref().unwrap_or("100%");
let start_str = crate::core::storage::bytes_to_parted_unit(start);
let end_str = crate::core::storage::manual_disk_spec_end_for_parted(start, size)
.unwrap_or_else(|_| crate::core::storage::bytes_to_parted_unit(size));
// Create partition
let part_type = match role {
"BOOT" | "EFI" => "ESP",
"SWAP" => "linux-swap",
_ => "primary",
};
part_cmds.push(format!(
"parted -s {device} mkpart {part_type} {fs} {start_str} {end_str}"
));
// Set partition flags based on role
match role {
"BOOT" | "EFI" => {
part_cmds.push(format!("parted -s {device} set {partition_number} esp on"));
}
"BIOS_BOOT" => {
part_cmds.push(format!(
"parted -s {device} set {partition_number} bios_grub on"
));
}
_ => {}
}
// Format the partition
let partition_path = Self::get_partition_path(device, partition_number);
match fs {
"fat32" | "fat16" | "fat12" => {
let fat_type = match fs {
"fat32" => "32",
"fat16" => "16",
"fat12" => "12",
_ => "32",
};
part_cmds.push(format!("mkfs.fat -F {fat_type} {partition_path}"));
}
"linux-swap" => {
part_cmds.push(format!("mkswap {partition_path}"));
}
"btrfs" => {
part_cmds.push(format!("mkfs.btrfs -f {partition_path}"));
}
"ext4" => {
part_cmds.push(format!("mkfs.ext4 -F {partition_path}"));
}
"ext3" => {
part_cmds.push(format!("mkfs.ext3 -F {partition_path}"));
}
"ext2" => {
part_cmds.push(format!("mkfs.ext2 -F {partition_path}"));
}
"xfs" => {
part_cmds.push(format!("mkfs.xfs -f {partition_path}"));
}
"f2fs" => {
part_cmds.push(format!("mkfs.f2fs -f {partition_path}"));
}
_ => {
part_cmds.push(format!("mkfs.ext4 -F {partition_path}"));
}
}
partition_number += 1;
}
part_cmds.push(format!("partprobe {device} || true"));
part_cmds.push("udevadm settle".into());
}
fn get_partition_path(device: &str, partition_number: u32) -> String {
// Handle devices that end with a digit (like /dev/nvme0n1)
if device
.chars()
.last()
.map(|c| c.is_ascii_digit())
.unwrap_or(false)
{
format!("{device}p{partition_number}")
} else {
format!("{device}{partition_number}")
}
}
pub fn execute_plan(plan: PartitionPlan) -> Result<(), String> {
for c in plan.commands {
let status = std::process::Command::new("bash")
.arg("-lc")
.arg(&c)
.status();
match status {
Ok(st) if st.success() => {}
Ok(_) => {
return Err(format!(
"Command failed: {}",
crate::common::utils::redact_command_for_logging(&c)
));
}
Err(_) => {
return Err(format!(
"Failed to run: {}",
crate::common::utils::redact_command_for_logging(&c)
));
}
}
}
Ok(())
}
}