12 个版本

0.5.2 2023 年 3 月 27 日
0.5.1 2022 年 12 月 1 日
0.5.0 2022 年 10 月 14 日
0.4.2 2022 年 1 月 11 日
0.1.0 2019 年 8 月 31 日

#369文件系统

Download history 991/week @ 2024-04-23 768/week @ 2024-04-30 862/week @ 2024-05-07 864/week @ 2024-05-14 818/week @ 2024-05-21 979/week @ 2024-05-28 975/week @ 2024-06-04 838/week @ 2024-06-11 967/week @ 2024-06-18 795/week @ 2024-06-25 745/week @ 2024-07-02 1136/week @ 2024-07-09 904/week @ 2024-07-16 1288/week @ 2024-07-23 976/week @ 2024-07-30 1193/week @ 2024-08-06

4,562 每月下载量
用于 4 crates

MIT/Apache

82KB
1K SLoC

Rust Latest Version Rust 1.56+ License Docs.rs LOC Dependency Status

mbrman

Rust 中的 MBR 分区管理

库使用

允许管理 MBR 分区表的库。

特性

  • 创建主分区和逻辑卷
  • 删除主分区和逻辑卷
  • 自动生成逻辑卷的 EBR(或可手动提供)
  • 如果设置了磁盘几何形状,则在写入磁盘时将自动计算分区的 CHS 地址

示例

读取磁盘上的所有分区

let mut f = std::fs::File::open("tests/fixtures/disk1.img")
    .expect("could not open disk");
let mbr = mbrman::MBR::read_from(&mut f, 512)
    .expect("could not find MBR");

println!("Disk signature: {:?}", mbr.header.disk_signature);

for (i, p) in mbr.iter() {
    // NOTE: The first four partitions are always provided by iter()
    if p.is_used() {
        println!("Partition #{}: type = {:?}, size = {} bytes, starting lba = {}",
            i,
            p.sys,
            p.sectors * mbr.sector_size,
            p.starting_lba);
    }
}

创建和删除主分区

let mut f = std::fs::File::open("tests/fixtures/disk1.img")
    .expect("could not open disk");
let mut mbr = mbrman::MBR::read_from(&mut f, 512)
    .expect("could not find MBR");

let free_partition_number = mbr.iter().find(|(i, p)| p.is_unused()).map(|(i, _)| i)
    .expect("no more places available");
let sectors = mbr.get_maximum_partition_size()
    .expect("no more space available");
let starting_lba = mbr.find_optimal_place(sectors)
    .expect("could not find a place to put the partition");

mbr[free_partition_number] = mbrman::MBRPartitionEntry {
    boot: mbrman::BOOT_INACTIVE,        // boot flag
    first_chs: mbrman::CHS::empty(),    // first CHS address (only useful for old computers)
    sys: 0x83,                          // Linux filesystem
    last_chs: mbrman::CHS::empty(),     // last CHS address (only useful for old computers)
    starting_lba,                       // the sector where the partition starts
    sectors,                            // the number of sectors in that partition
};

mbr[free_partition_number] = mbrman::MBRPartitionEntry::empty();

// NOTE: no modification is committed to the disk until we call mbr.write_into()

从空磁盘创建新的分区表

let ss = 512; // sector size
let data = vec![0; 100 * ss as usize];
let mut cur = std::io::Cursor::new(data);

let mut mbr = mbrman::MBR::new_from(&mut cur, ss as u32, [0xff; 4])
    .expect("could not create partition table");

// NOTE: commit the change to the in-memory buffer
mbr.write_into(&mut cur);

向磁盘添加新的逻辑卷

let ss = 512; // sector size
let data = vec![0; 100 * ss as usize];
let mut cur = std::io::Cursor::new(data);

let mut mbr = mbrman::MBR::new_from(&mut cur, ss as u32, [0xff; 4])
    .expect("could not create partition table");

mbr[1] = mbrman::MBRPartitionEntry {
    boot: mbrman::BOOT_INACTIVE,        // boot flag
    first_chs: mbrman::CHS::empty(),    // first CHS address (only useful for old computers)
    sys: 0x0f,                          // extended partition with LBA
    last_chs: mbrman::CHS::empty(),     // last CHS address (only useful for old computers)
    starting_lba: 1,                    // the sector where the partition starts
    sectors: mbr.disk_size - 1,         // the number of sectors in that partition
};

// this helper function will do all the hard work for you
// here it creates a logical volume with Linux filesystem that occupies the entire disk
// NOTE: you will lose 1 sector because it is used by the EBR
mbr.push(0x83, 1, mbr.disk_size - 1);

// NOTE: commit the change to the in-memory buffer
mbr.write_into(&mut cur);

手动向磁盘添加新的逻辑卷

这仅在您需要确切指定 EBR 和分区本身的位置时才有用。

let ss = 512; // sector size
let data = vec![0; 100 * ss as usize];
let mut cur = std::io::Cursor::new(data);

let mut mbr = mbrman::MBR::new_from(&mut cur, ss as u32, [0xff; 4])
    .expect("could not create partition table");

mbr[1] = mbrman::MBRPartitionEntry {
    boot: mbrman::BOOT_INACTIVE,        // boot flag
    first_chs: mbrman::CHS::empty(),    // first CHS address (only useful for old computers)
    sys: 0x0f,                          // extended partition with LBA
    last_chs: mbrman::CHS::empty(),     // last CHS address (only useful for old computers)
    starting_lba: 1,                    // the sector where the partition starts
    sectors: mbr.disk_size - 1,         // the number of sectors in that partition
};

// NOTE: mbrman won't check the consistency of the partition you have created manually
mbr.logical_partitions.push(
    mbrman::LogicalPartition {
        // this is the actual partition entry for the logical volume
        partition: mbrman::MBRPartitionEntry {
            boot: mbrman::BOOT_INACTIVE,
            first_chs: mbrman::CHS::empty(),
            sys: 0x83,
            last_chs: mbrman::CHS::empty(),
            starting_lba: 2,                    // the sector index 1 is used by the EBR
            sectors: mbr.disk_size - 2,
        },
        // this is the absolute LBA address of the EBR
        absolute_ebr_lba: 1,
        // the number of sectors in the first EBR is never known
        ebr_sectors: None,
        // empty boot sector in the EBR
        bootstrap_code: [0; 446],
        // this is the absolute CHS address of the EBR (only used by old computers)
        ebr_first_chs: mbrman::CHS::empty(),                // only for old computers
        // this is the absolute CHS address of the last EBR (only used by old computers)
        // NOTE: this is not know the first EBR
        ebr_last_chs: None,
    }
);

// NOTE: commit the change to the in-memory buffer
mbr.write_into(&mut cur);

依赖项

~1.5–2.1MB
~51K SLoC