Skip to content

Add support for adding a ramdisk to the disk image #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,31 @@ mod mbr;
mod pxe;

const KERNEL_FILE_NAME: &str = "kernel-x86_64";
const RAMDISK_FILE_NAME: &str = "ramdisk-x86_64";
const BIOS_STAGE_3: &str = "boot-stage-3";
const BIOS_STAGE_4: &str = "boot-stage-4";

/// Create disk images for booting on legacy BIOS systems.
pub struct BiosBoot {
kernel: PathBuf,
ramdisk: Option<PathBuf>,
}

impl BiosBoot {
/// Start creating a disk image for the given bootloader ELF executable.
pub fn new(kernel_path: &Path) -> Self {
Self {
kernel: kernel_path.to_owned(),
ramdisk: None,
}
}

/// Add a ramdisk file to the image
pub fn set_ramdisk(&mut self, ramdisk_path: &Path) -> &mut Self {
self.ramdisk = Some(ramdisk_path.to_owned());
self
}

/// Create a bootable UEFI disk image at the given path.
pub fn create_disk_image(&self, out_path: &Path) -> anyhow::Result<()> {
let bootsector_path = Path::new(env!("BIOS_BOOT_SECTOR_PATH"));
Expand Down Expand Up @@ -61,12 +70,18 @@ impl BiosBoot {
fn create_fat_partition(&self) -> anyhow::Result<NamedTempFile> {
let stage_3_path = Path::new(env!("BIOS_STAGE_3_PATH"));
let stage_4_path = Path::new(env!("BIOS_STAGE_4_PATH"));
let has_rd_path = self.ramdisk.is_some();
let binding = self.ramdisk.as_deref();
let ramdisk_path = binding.unwrap_or(Path::new("no-such-file"));
let kernel_path = self.kernel.as_path();

let mut files = BTreeMap::new();
files.insert(KERNEL_FILE_NAME, self.kernel.as_path());
files.insert(KERNEL_FILE_NAME, kernel_path);
files.insert(BIOS_STAGE_3, stage_3_path);
files.insert(BIOS_STAGE_4, stage_4_path);

if let Some(ramdisk_path) = &self.ramdisk {
files.insert(RAMDISK_FILE_NAME, ramdisk_path);
}
let out_file = NamedTempFile::new().context("failed to create temp file")?;
fat::create_fat_filesystem(files, out_file.path())
.context("failed to create BIOS FAT filesystem")?;
Expand All @@ -78,13 +93,23 @@ impl BiosBoot {
/// Create disk images for booting on UEFI systems.
pub struct UefiBoot {
kernel: PathBuf,
ramdisk: Option<PathBuf>,
}

impl UefiBoot {
/// Start creating a disk image for the given bootloader ELF executable.
pub fn new(kernel_path: &Path) -> Self {
Self {
kernel: kernel_path.to_owned(),
ramdisk: None,
}
}

/// Add a ramdisk file to the disk image
pub fn with_ramdisk(&self, ramdisk_path: &Path) -> Self {
Self {
kernel: self.kernel.clone(),
ramdisk: Some(ramdisk_path.to_owned()),
}
}
Comment on lines +109 to 114
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above: set_ramdisk(&mut self, ramdisk_path: &Path) -> &mut Self.


Expand Down Expand Up @@ -121,10 +146,17 @@ impl UefiBoot {
/// Creates an UEFI-bootable FAT partition with the kernel.
fn create_fat_partition(&self) -> anyhow::Result<NamedTempFile> {
let bootloader_path = Path::new(env!("UEFI_BOOTLOADER_PATH"));

let has_rd_path = self.ramdisk.is_some();
let binding = self.ramdisk.as_deref();
let ramdisk_path = binding.unwrap_or(Path::new("no-such-file"));
let kernel_path = self.kernel.as_path();
let mut files = BTreeMap::new();
files.insert("efi/boot/bootx64.efi", bootloader_path);
files.insert(KERNEL_FILE_NAME, self.kernel.as_path());
files.insert(KERNEL_FILE_NAME, kernel_path);

if has_rd_path {
files.insert(RAMDISK_FILE_NAME, ramdisk_path);
}
Comment on lines +157 to +159
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above (use if let and avoid the additional bindings in lines 151-154).


let out_file = NamedTempFile::new().context("failed to create temp file")?;
fat::create_fat_filesystem(files, out_file.path())
Expand Down