diff options
author | HimbeerserverDE <himbeerserverde@gmail.com> | 2022-12-25 19:19:22 +0100 |
---|---|---|
committer | HimbeerserverDE <himbeerserverde@gmail.com> | 2022-12-25 19:19:22 +0100 |
commit | 0e09f61377fa8dd9f2476e92d1eaefe0e68bccba (patch) | |
tree | 1a3f2ef52eb6c845b9450013c13149a1eb726851 | |
parent | 38cb831d584b74a9e6d82f34d10f155edd11e34e (diff) |
create root fs
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/main.rs | 41 |
2 files changed, 37 insertions, 6 deletions
@@ -11,3 +11,5 @@ clap = { version = "4.0.29", features = ["derive"] } fatfs = "0.3.5" fscommon = "0.1.1" nix = { version = "0.26.1", features = ["ioctl"] } +squashfs-ng = "0.1.2" +temp-file = "0.1.7" diff --git a/src/main.rs b/src/main.rs index 83e9c97..2388166 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,8 @@ use anyhow::bail; use clap::Parser; use fatfs::{FatType, FormatVolumeOptions}; use fscommon::StreamSlice; +use squashfs_ng::write::{Source as SqsSource, SourceData as SqsSourceData, Writer as SqsWriter}; +use std::collections::HashMap; use std::fs::{File, OpenOptions}; use std::io::{Read, Seek, SeekFrom, Write}; use std::os::unix::fs::PermissionsExt; @@ -75,7 +77,7 @@ fn write_mbr_partition_table(file: &mut File, dev_size: u64) -> anyhow::Result<( file.write_all(SQUASHFS)?; file.write_all(INVALID_CHS)?; file.write_all(&(2048 + 256 * MiB / 512).to_le_bytes())?; - file.write_all(&(dev_size as u32 / 512 - 8192 - 256 * MiB / 512).to_le_bytes())?; + file.write_all(&(dev_size as u32 / 512 - 2048 - 256 * MiB / 512).to_le_bytes())?; // Partition 3 (unused) file.write_all(NOPART)?; @@ -90,17 +92,19 @@ fn write_mbr_partition_table(file: &mut File, dev_size: u64) -> anyhow::Result<( } fn partition(file: &mut File, dev_size: u64) -> anyhow::Result<()> { + const ROOT_START: u64 = (2048 * 512 + 256 * MiB) as u64; + let root_end = ROOT_START + (dev_size as u32 - 2048 * 512 - 256 * MiB) as u64; + write_mbr_partition_table(file, dev_size)?; - let mut boot_partition = StreamSlice::new( - file.try_clone()?, - 2048 * 512, - (2048 * 512 + 256 * MiB - 1).into(), - )?; + let mut boot_partition = StreamSlice::new(file.try_clone()?, 2048 * 512, ROOT_START - 1)?; + let mut root_partition = StreamSlice::new(file.try_clone()?, ROOT_START, root_end)?; write_boot(&mut boot_partition)?; write_mbr(file)?; + write_root(&mut root_partition)?; + Ok(()) } @@ -194,6 +198,31 @@ fn write_mbr(file: &mut File) -> anyhow::Result<()> { Ok(()) } +fn write_root(partition: &mut StreamSlice<File>) -> anyhow::Result<()> { + let mut partition_buf = Vec::new(); + partition.read_to_end(&mut partition_buf)?; + + let tmp_file = temp_file::with_contents(&partition_buf); + + let mut writer = SqsWriter::open(tmp_file.path())?; + + let init_file = File::open("init")?; + writer.add(SqsSource { + data: SqsSourceData::File(Box::new(init_file)), + uid: 0, + gid: 0, + mode: 0o755, + modified: 0, + xattrs: HashMap::new(), + flags: 0, + })?; + + writer.finish()?; + + println!("Root filesystem created successfully"); + Ok(()) +} + fn overwrite_device(file: &mut File, overwrite: String) -> anyhow::Result<()> { partition_device(file, overwrite)?; Ok(()) |