Coder Social home page Coder Social logo

richardstephens / gptman Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rust-disk-partition-management/gptman

0.0 0.0 0.0 313 KB

A GPT manager that allows you to copy partitions from one disk to another and more

License: MIT License

Shell 0.22% Rust 99.78%

gptman's Introduction

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

gptman

A library that allows managing GUID partition tables.

A CLI tool for Linux that allows you to copy a partition from one disk to another and more.

Features

  • Read/Write GPT from 512 and 4096 bytes sector size disks
  • Create a new GPT on a disk (-i, --init)
  • Insert/delete a partition in the table (n, d)
  • Align partitions automatically (a)
  • Resize a partition (r)
  • Copy/clone a partition from one disk and insert it to another (c)
  • Change partition type (and list by category) (t)
  • Fix partitions order (f)
  • Change disk GUID (i)
  • Change partition name (L)
  • Change partition GUID (u)
  • Toggle legacy BIOS bootable (A)
  • Toggle no block IO protocol (B)
  • Toggle required partition flag (R)
  • Toggle attributes (S)
  • Customize columns to print (-o, --output)
  • Print raw data of disklabel (D)
  • Swap partition indexes (s)
  • Randomize disk's GUID and all partition's GUID (Z)
  • Copy/clone all partitions from one disk and insert it to another (C)
  • Write protective MBR
  • Check how empty a partition physically is (number of empty blocks) (z)

Installation

  • With curl:

    curl -sSL http://gptman.cecton.com | sh -
  • CLI:

    cargo install --features=cli gptman

    Statically linked:

    cargo install --features=cli --target=x86_64-unknown-linux-musl gptman
  • Library:

    Cargo.toml:

    [dependencies]
    gptman = "1"

Library Usage

Reading all the partitions of a disk:

let mut f = std::fs::File::open("tests/fixtures/disk1.img")
    .expect("could not open disk");
let gpt = gptman::GPT::find_from(&mut f)
    .expect("could not find GPT");

println!("Disk GUID: {:?}", gpt.header.disk_guid);

for (i, p) in gpt.iter() {
    if p.is_used() {
        println!("Partition #{}: type = {:?}, size = {} bytes, starting lba = {}",
            i,
            p.partition_type_guid,
            p.size().unwrap() * gpt.sector_size,
            p.starting_lba);
    }
}

Creating new partitions:

let mut f = std::fs::File::open("tests/fixtures/disk1.img")
    .expect("could not open disk");
let mut gpt = gptman::GPT::find_from(&mut f)
    .expect("could not find GPT");

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

gpt[free_partition_number] = gptman::GPTPartitionEntry {
    partition_type_guid: [0xff; 16],
    unique_partition_guid: [0xff; 16],
    starting_lba,
    ending_lba,
    attribute_bits: 0,
    partition_name: "A Robot Named Fight!".into(),
};

Creating a new partition table with one entry that fills the entire disk:

let ss = 512;
let data = vec![0; 100 * ss as usize];
let mut cur = std::io::Cursor::new(data);
let mut gpt = gptman::GPT::new_from(&mut cur, ss as u64, [0xff; 16])
    .expect("could not create partition table");

gpt[1] = gptman::GPTPartitionEntry {
    partition_type_guid: [0xff; 16],
    unique_partition_guid: [0xff; 16],
    starting_lba: gpt.header.first_usable_lba,
    ending_lba: gpt.header.last_usable_lba,
    attribute_bits: 0,
    partition_name: "A Robot Named Fight!".into(),
};

gptman's People

Contributors

cecton avatar dependabot-preview[bot] avatar bgilbert avatar github-actions[bot] avatar mmstick avatar dependabot[bot] avatar smalis-msft avatar bobo1239 avatar kianmeng avatar nicholasbishop avatar yozhgoor avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.