Skip to content

Commit f9033f8

Browse files
committed
Add support for adding a ramdisk to the disk image
1 parent c26220f commit f9033f8

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

src/lib.rs

+38-4
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,30 @@ mod mbr;
1717
mod pxe;
1818

1919
const KERNEL_FILE_NAME: &str = "kernel-x86_64";
20+
const RAMDISK_FILE_NAME: &str = "ramdisk-x86_64";
2021
const BIOS_STAGE_3: &str = "boot-stage-3";
2122
const BIOS_STAGE_4: &str = "boot-stage-4";
2223

2324
/// Create disk images for booting on legacy BIOS systems.
2425
pub struct BiosBoot {
2526
kernel: PathBuf,
27+
ramdisk: Option<PathBuf>,
2628
}
2729

2830
impl BiosBoot {
2931
/// Start creating a disk image for the given bootloader ELF executable.
3032
pub fn new(kernel_path: &Path) -> Self {
3133
Self {
3234
kernel: kernel_path.to_owned(),
35+
ramdisk: None,
36+
}
37+
}
38+
39+
/// Add a ramdisk file to the image
40+
pub fn with_ramdisk(&self, ramdisk_path: &Path) -> Self {
41+
Self {
42+
kernel: self.kernel.to_owned(),
43+
ramdisk: Some(ramdisk_path.to_owned()),
3344
}
3445
}
3546

@@ -61,12 +72,18 @@ impl BiosBoot {
6172
fn create_fat_partition(&self) -> anyhow::Result<NamedTempFile> {
6273
let stage_3_path = Path::new(env!("BIOS_STAGE_3_PATH"));
6374
let stage_4_path = Path::new(env!("BIOS_STAGE_4_PATH"));
75+
let has_rd_path = self.ramdisk.is_some();
76+
let binding = self.ramdisk.as_deref();
77+
let ramdisk_path = binding.unwrap_or(Path::new("no-such-file"));
78+
let kernel_path = self.kernel.as_path();
6479

6580
let mut files = BTreeMap::new();
66-
files.insert(KERNEL_FILE_NAME, self.kernel.as_path());
81+
files.insert(KERNEL_FILE_NAME, kernel_path);
6782
files.insert(BIOS_STAGE_3, stage_3_path);
6883
files.insert(BIOS_STAGE_4, stage_4_path);
69-
84+
if has_rd_path {
85+
files.insert(RAMDISK_FILE_NAME, ramdisk_path);
86+
}
7087
let out_file = NamedTempFile::new().context("failed to create temp file")?;
7188
fat::create_fat_filesystem(files, out_file.path())
7289
.context("failed to create BIOS FAT filesystem")?;
@@ -78,13 +95,23 @@ impl BiosBoot {
7895
/// Create disk images for booting on UEFI systems.
7996
pub struct UefiBoot {
8097
kernel: PathBuf,
98+
ramdisk: Option<PathBuf>,
8199
}
82100

83101
impl UefiBoot {
84102
/// Start creating a disk image for the given bootloader ELF executable.
85103
pub fn new(kernel_path: &Path) -> Self {
86104
Self {
87105
kernel: kernel_path.to_owned(),
106+
ramdisk: None,
107+
}
108+
}
109+
110+
/// Add a ramdisk file to the disk image
111+
pub fn with_ramdisk(&self, ramdisk_path: &Path) -> Self {
112+
Self {
113+
kernel: self.kernel.clone(),
114+
ramdisk: Some(ramdisk_path.to_owned()),
88115
}
89116
}
90117

@@ -121,10 +148,17 @@ impl UefiBoot {
121148
/// Creates an UEFI-bootable FAT partition with the kernel.
122149
fn create_fat_partition(&self) -> anyhow::Result<NamedTempFile> {
123150
let bootloader_path = Path::new(env!("UEFI_BOOTLOADER_PATH"));
124-
151+
let has_rd_path = self.ramdisk.is_some();
152+
let binding = self.ramdisk.as_deref();
153+
let ramdisk_path = binding.unwrap_or(Path::new("no-such-file"));
154+
let kernel_path = self.kernel.as_path();
125155
let mut files = BTreeMap::new();
126156
files.insert("efi/boot/bootx64.efi", bootloader_path);
127-
files.insert(KERNEL_FILE_NAME, self.kernel.as_path());
157+
files.insert(KERNEL_FILE_NAME, kernel_path);
158+
159+
if has_rd_path {
160+
files.insert(RAMDISK_FILE_NAME, ramdisk_path);
161+
}
128162

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

0 commit comments

Comments
 (0)