gpt
gpt copied to clipboard
How do I create new partition table on actual device for example /dev/loop0?
Hi, I read through documentation and I was trying hard to create new partition table on /dev/loop0 to which I have previously attached file. This seems impossible. The documentation only explains how to do it on vector cursor which is NOT very useful in real life. Can you provide example on how to create new group partition table given path, for example "/dev/loop0"?
I tried this and it didn't work - no partition table on external program (kde partition manager)
let mut gdisk = gpt::GptConfig::default()
.initialized(false)
.writable(true)
.logical_block_size(gpt::disk::LogicalBlockSize::Lb512)
.open(diskpath)
.unwrap();
gdisk.update_partitions(
std::collections::BTreeMap::<u32, gpt::partition::Partition>::new()
).expect("failed to initialize blank partition table");
gdisk.add_partition("test1", 1024 * 12, gpt::partition_types::BASIC, 0, None)
.expect("failed to add test1 partition");
let mut mem_device = gdisk.write().expect("failed to write partition table");
I tried this, but I get error: "memory map must have a non-zero length"
let mut file = fs::File::open(diskpath).unwrap();
let mut mmap_options = MmapOptions::new();
let mut mmap = unsafe { mmap_options.map(&file) };
let mut cursor = std::io::Cursor::new(mmap.unwrap());
let gpt = gptman::GPT::new_from(&mut cursor, SECTOR_SIZE as u64, [0xff; 16]).expect("could not make a partition table");
println!("gpt: {:?}", gpt);
I achived what I wanted with this:
let output = Command::new("parted")
.arg(diskpath)
.arg("mklabel")
.arg("gpt")
.output()
.expect("failed to execute parted command");
if !output.status.success() {
eprintln!("Error: {:?}", output);
return Ok(());
}
println!("partition table created successfully");
Hi @segfaultDelirium thanks for you're interest in this crate. Please use the latest release candidate since a few things have changed and 4.0 is mostly ready to be released. using v4.0.0-rc.1 https://docs.rs/gpt/4.0.0-rc.1/gpt/index.html:
// if there is nothing initialized
let mut disk = GptConfig::new()
.writable(true)
.create("/dev/loop0").expect("failed to open /dev/loop0");
// you now have a GptDisk, you can add partitions
// when your partitions are setup you can call write or write_inplace
disk.write().expect("failed to initialize a gpt device on /dev/loop0");
If you already have a std::fs::File you can use the function GptConfig::create_from_device instead of create.
Thank you, sorry that my initial question sounded angry :( I'll try it
Let me know if you still have some questions