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