1use ext4_mkfs::{mkfs, FsType, IoBlockDevice, MkfsConfig};
6use std::fs::{File, OpenOptions};
7use std::io::{Seek, SeekFrom, Write};
8
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10 let path = "test_disk.img";
11 let size: u64 = 10 * 1024 * 1024; println!("Creating disk image: {} ({} bytes)", path, size);
14
15 {
17 let mut file = File::create(path)?;
18 file.seek(SeekFrom::Start(size - 1))?;
19 file.write_all(&[0])?;
20 file.sync_all()?;
21 }
22
23 let file = OpenOptions::new().read(true).write(true).open(path)?;
25
26 let device = IoBlockDevice::new(file, 512, size);
28
29 let config = MkfsConfig::new()
31 .fs_type(FsType::Ext4)
32 .block_size(4096)
33 .label("test_volume");
34
35 println!("Formatting as ext4...");
36 mkfs(device, config)?;
37
38 println!("Done! You can verify with: file {} or dumpe2fs {}", path, path);
39
40 Ok(())
41}