@@ -17,19 +17,30 @@ mod mbr;
17
17
mod pxe;
18
18
19
19
const KERNEL_FILE_NAME : & str = "kernel-x86_64" ;
20
+ const RAMDISK_FILE_NAME : & str = "ramdisk-x86_64" ;
20
21
const BIOS_STAGE_3 : & str = "boot-stage-3" ;
21
22
const BIOS_STAGE_4 : & str = "boot-stage-4" ;
22
23
23
24
/// Create disk images for booting on legacy BIOS systems.
24
25
pub struct BiosBoot {
25
26
kernel : PathBuf ,
27
+ ramdisk : Option < PathBuf > ,
26
28
}
27
29
28
30
impl BiosBoot {
29
31
/// Start creating a disk image for the given bootloader ELF executable.
30
32
pub fn new ( kernel_path : & Path ) -> Self {
31
33
Self {
32
34
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 ( ) ) ,
33
44
}
34
45
}
35
46
@@ -61,12 +72,18 @@ impl BiosBoot {
61
72
fn create_fat_partition ( & self ) -> anyhow:: Result < NamedTempFile > {
62
73
let stage_3_path = Path :: new ( env ! ( "BIOS_STAGE_3_PATH" ) ) ;
63
74
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 ( ) ;
64
79
65
80
let mut files = BTreeMap :: new ( ) ;
66
- files. insert ( KERNEL_FILE_NAME , self . kernel . as_path ( ) ) ;
81
+ files. insert ( KERNEL_FILE_NAME , kernel_path ) ;
67
82
files. insert ( BIOS_STAGE_3 , stage_3_path) ;
68
83
files. insert ( BIOS_STAGE_4 , stage_4_path) ;
69
-
84
+ if has_rd_path {
85
+ files. insert ( RAMDISK_FILE_NAME , ramdisk_path) ;
86
+ }
70
87
let out_file = NamedTempFile :: new ( ) . context ( "failed to create temp file" ) ?;
71
88
fat:: create_fat_filesystem ( files, out_file. path ( ) )
72
89
. context ( "failed to create BIOS FAT filesystem" ) ?;
@@ -78,13 +95,23 @@ impl BiosBoot {
78
95
/// Create disk images for booting on UEFI systems.
79
96
pub struct UefiBoot {
80
97
kernel : PathBuf ,
98
+ ramdisk : Option < PathBuf > ,
81
99
}
82
100
83
101
impl UefiBoot {
84
102
/// Start creating a disk image for the given bootloader ELF executable.
85
103
pub fn new ( kernel_path : & Path ) -> Self {
86
104
Self {
87
105
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 ( ) ) ,
88
115
}
89
116
}
90
117
@@ -121,10 +148,17 @@ impl UefiBoot {
121
148
/// Creates an UEFI-bootable FAT partition with the kernel.
122
149
fn create_fat_partition ( & self ) -> anyhow:: Result < NamedTempFile > {
123
150
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 ( ) ;
125
155
let mut files = BTreeMap :: new ( ) ;
126
156
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
+ }
128
162
129
163
let out_file = NamedTempFile :: new ( ) . context ( "failed to create temp file" ) ?;
130
164
fat:: create_fat_filesystem ( files, out_file. path ( ) )
0 commit comments