|
| 1 | +// SPDX-FileCopyrightText: Copyright © 2025 Serpent OS Developers |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MPL-2.0 |
| 4 | + |
| 5 | +use disks::{BasicDisk, DiskInit}; |
| 6 | +use log::{debug, error, info, warn}; |
| 7 | +use std::{ |
| 8 | + fs::File, |
| 9 | + io, |
| 10 | + os::fd::{AsFd, AsRawFd}, |
| 11 | + path::{Path, PathBuf}, |
| 12 | +}; |
| 13 | +use thiserror::Error; |
| 14 | + |
| 15 | +pub use gpt; |
| 16 | +use linux_raw_sys::ioctl::BLKPG; |
| 17 | +use nix::libc; |
| 18 | + |
| 19 | +/// Errors that can occur during partition operations |
| 20 | +#[derive(Error, Debug)] |
| 21 | +pub enum Error { |
| 22 | + /// IO operation error |
| 23 | + #[error("IO error: {0}")] |
| 24 | + Io(#[from] io::Error), |
| 25 | + /// GPT-specific error |
| 26 | + #[error("GPT error: {0}")] |
| 27 | + Gpt(#[from] gpt::GptError), |
| 28 | +} |
| 29 | + |
| 30 | +/// Represents a block device partition for IOCTL operations |
| 31 | +#[repr(C)] |
| 32 | +struct BlkpgPartition { |
| 33 | + start: i64, |
| 34 | + length: i64, |
| 35 | + pno: i32, |
| 36 | + devname: [u8; 64], |
| 37 | + volname: [u8; 64], |
| 38 | +} |
| 39 | + |
| 40 | +/// IOCTL structure for partition operations |
| 41 | +#[repr(C)] |
| 42 | +struct BlkpgIoctl { |
| 43 | + op: i32, |
| 44 | + flags: i32, |
| 45 | + datalen: i32, |
| 46 | + data: *mut BlkpgPartition, |
| 47 | +} |
| 48 | + |
| 49 | +const BLKPG_ADD_PARTITION: i32 = 1; |
| 50 | +const BLKPG_DEL_PARTITION: i32 = 2; |
| 51 | + |
| 52 | +/// Adds a new partition to the specified block device |
| 53 | +/// |
| 54 | +/// # Arguments |
| 55 | +/// * `fd` - File descriptor for the block device |
| 56 | +/// * `partition_number` - Number to assign to the new partition |
| 57 | +/// * `start` - Starting offset in bytes |
| 58 | +/// * `length` - Length of partition in bytes |
| 59 | +/// |
| 60 | +/// # Returns |
| 61 | +/// `io::Result<()>` indicating success or failure |
| 62 | +pub(crate) fn add_partition<F>(fd: F, partition_number: i32, start: i64, length: i64) -> io::Result<()> |
| 63 | +where |
| 64 | + F: AsRawFd, |
| 65 | +{ |
| 66 | + info!( |
| 67 | + "➕ Adding partition {} (start: {}, length: {})", |
| 68 | + partition_number, start, length |
| 69 | + ); |
| 70 | + let mut part = BlkpgPartition { |
| 71 | + start, |
| 72 | + length, |
| 73 | + pno: partition_number, |
| 74 | + devname: [0; 64], |
| 75 | + volname: [0; 64], |
| 76 | + }; |
| 77 | + |
| 78 | + let mut ioctl = BlkpgIoctl { |
| 79 | + op: BLKPG_ADD_PARTITION, |
| 80 | + flags: 0, |
| 81 | + datalen: std::mem::size_of::<BlkpgPartition>() as i32, |
| 82 | + data: &mut part, |
| 83 | + }; |
| 84 | + |
| 85 | + let res = unsafe { libc::ioctl(fd.as_raw_fd(), BLKPG as _, &mut ioctl) }; |
| 86 | + if res < 0 { |
| 87 | + let err = io::Error::last_os_error(); |
| 88 | + error!("❌ Failed to add partition: {}", err); |
| 89 | + return Err(err); |
| 90 | + } |
| 91 | + info!("✅ Successfully added partition {}", partition_number); |
| 92 | + Ok(()) |
| 93 | +} |
| 94 | + |
| 95 | +/// Deletes a partition from the specified block device |
| 96 | +/// |
| 97 | +/// # Arguments |
| 98 | +/// * `fd` - File descriptor for the block device |
| 99 | +/// * `partition_number` - Number of the partition to delete |
| 100 | +/// |
| 101 | +/// # Returns |
| 102 | +/// `io::Result<()>` indicating success or failure |
| 103 | +pub(crate) fn delete_partition<F>(fd: F, partition_number: i32) -> io::Result<()> |
| 104 | +where |
| 105 | + F: AsRawFd, |
| 106 | +{ |
| 107 | + warn!("🗑️ Attempting to delete partition {}", partition_number); |
| 108 | + let mut part = BlkpgPartition { |
| 109 | + start: 0, |
| 110 | + length: 0, |
| 111 | + pno: partition_number, |
| 112 | + devname: [0; 64], |
| 113 | + volname: [0; 64], |
| 114 | + }; |
| 115 | + |
| 116 | + let mut ioctl = BlkpgIoctl { |
| 117 | + op: BLKPG_DEL_PARTITION, |
| 118 | + flags: 0, |
| 119 | + datalen: std::mem::size_of::<BlkpgPartition>() as i32, |
| 120 | + data: &mut part, |
| 121 | + }; |
| 122 | + |
| 123 | + let res = unsafe { libc::ioctl(fd.as_raw_fd(), BLKPG as _, &mut ioctl) }; |
| 124 | + if res < 0 { |
| 125 | + let err = io::Error::last_os_error(); |
| 126 | + error!("❌ Failed to delete partition {}: {}", partition_number, err); |
| 127 | + return Err(err); |
| 128 | + } |
| 129 | + info!("✅ Successfully deleted partition {}", partition_number); |
| 130 | + Ok(()) |
| 131 | +} |
| 132 | + |
| 133 | +/// Updates kernel partition representations to match the GPT table |
| 134 | +/// |
| 135 | +/// # Arguments |
| 136 | +/// * `path` - Path to the block device |
| 137 | +/// |
| 138 | +/// # Returns |
| 139 | +/// `Result<(), Error>` indicating success or partition operation failure |
| 140 | +pub fn sync_gpt_partitions<P: AsRef<Path>>(path: P) -> Result<(), Error> { |
| 141 | + info!("🔄 Syncing GPT partitions for {:?}", path.as_ref()); |
| 142 | + let file = File::open(&path)?; |
| 143 | + |
| 144 | + // Read GPT table |
| 145 | + debug!("📖 Reading GPT table..."); |
| 146 | + let gpt = gpt::GptConfig::new().writable(false).open(&path)?; |
| 147 | + let partitions = gpt.partitions(); |
| 148 | + let block_size = 512; |
| 149 | + info!( |
| 150 | + "📊 Found {} partitions with block size {}", |
| 151 | + partitions.len(), |
| 152 | + block_size |
| 153 | + ); |
| 154 | + |
| 155 | + warn!("🗑️ Deleting existing partitions..."); |
| 156 | + |
| 157 | + // Find the disk for enumeration purposes |
| 158 | + let base_name = path |
| 159 | + .as_ref() |
| 160 | + .file_name() |
| 161 | + .ok_or(Error::Io(io::Error::from(io::ErrorKind::InvalidInput)))? |
| 162 | + .to_string_lossy() |
| 163 | + .to_string(); |
| 164 | + let disk = BasicDisk::from_sysfs_path(&PathBuf::from("/sys/class/block"), &base_name) |
| 165 | + .ok_or(Error::Io(io::Error::from(io::ErrorKind::InvalidInput)))?; |
| 166 | + |
| 167 | + for partition in disk.partitions() { |
| 168 | + let _ = delete_partition(file.as_raw_fd(), partition.number as i32); |
| 169 | + } |
| 170 | + |
| 171 | + // Add partitions from GPT |
| 172 | + info!("➕ Adding new partitions from GPT..."); |
| 173 | + for (i, partition) in partitions.iter() { |
| 174 | + add_partition( |
| 175 | + file.as_fd(), |
| 176 | + *i as i32, |
| 177 | + partition.first_lba as i64 * block_size, |
| 178 | + (partition.last_lba - partition.first_lba + 1) as i64 * block_size, |
| 179 | + )?; |
| 180 | + } |
| 181 | + |
| 182 | + info!("✨ GPT partition sync completed successfully"); |
| 183 | + Ok(()) |
| 184 | +} |
0 commit comments