Skip to content

Commit 1f7064d

Browse files
Johan-Liebert1allisonkarlitskaya
authored andcommitted
Allow passing absolute paths to initrd and vmlinuz
Grub needs absolute paths to initrd and vmlinuz if we do not have `/boot` in a boot partition, which we do not in bootc. Add param `boot_subdir` which acts like a subdirectory in the boot directory in case the boot partition is mounted in another directory. Signed-off-by: Pragyan Poudyal <[email protected]>
1 parent 78777f3 commit 1f7064d

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

crates/cfsctl/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ async fn main() -> Result<()> {
279279
entry,
280280
&id,
281281
bootdir,
282+
None,
282283
entry_id.as_deref(),
283284
&cmdline_refs,
284285
)?;

crates/composefs-boot/src/bootloader.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl<ObjectID: FsVerityHashValue> Type1Entry<ObjectID> {
144144
// This is a bit of a strange operation: for each file mentioned in the bootloader entry, move
145145
// the file into the given 'entry_id' pathname and rename the entry file itself to
146146
// "{entry_id}.conf".
147-
pub fn relocate(&mut self, entry_id: &str) {
147+
pub fn relocate(&mut self, boot_subdir: Option<&str>, entry_id: &str) {
148148
self.filename = Box::from(format!("{entry_id}.conf").as_ref());
149149
for line in &mut self.entry.lines {
150150
for key in ["linux", "initrd", "efi"] {
@@ -159,7 +159,14 @@ impl<ObjectID: FsVerityHashValue> Type1Entry<ObjectID> {
159159

160160
let new = format!("/{entry_id}/{basename}");
161161
let range = substr_range(line, value).unwrap();
162-
line.replace_range(range, &new);
162+
163+
let final_entry_path = if let Some(boot_subdir) = boot_subdir {
164+
format!("/{boot_subdir}{new}")
165+
} else {
166+
new.clone()
167+
};
168+
169+
line.replace_range(range, &final_entry_path);
163170

164171
if let Some(file) = file {
165172
self.files.insert(new.into_boxed_str(), file);

crates/composefs-boot/src/write_boot.rs

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@ use crate::{
1616
pub fn write_t1_simple<ObjectID: FsVerityHashValue>(
1717
mut t1: Type1Entry<ObjectID>,
1818
bootdir: &Path,
19+
boot_subdir: Option<&str>,
1920
root_id: &ObjectID,
2021
cmdline_extra: &[&str],
2122
repo: &Repository<ObjectID>,
2223
) -> Result<()> {
24+
let bootdir = if let Some(subdir) = boot_subdir {
25+
let subdir_path = Path::new(subdir);
26+
bootdir.join(subdir_path.strip_prefix("/").unwrap_or(subdir_path))
27+
} else {
28+
bootdir.to_path_buf()
29+
};
30+
2331
t1.entry
2432
.adjust_cmdline(Some(&root_id.to_hex()), cmdline_extra);
2533

@@ -63,35 +71,78 @@ pub fn write_t2_simple<ObjectID: FsVerityHashValue>(
6371
Ok(())
6472
}
6573

74+
/// Writes boot entry to the boot partition
75+
///
76+
/// # Arguments
77+
///
78+
/// * repo - The composefs repository
79+
/// * entry - Boot entry variant to be written
80+
/// * root_id - The content hash of the generated EROFS image id
81+
/// * boot_partition - Path to the boot partition/directory
82+
/// * boot_subdir - If `Some(path)`, the path is prepended to `initrd` and `linux` keys in the BLS entry
83+
///
84+
/// For example, if `boot_partition = "/boot"` and `boot_subdir = Some("1")` ,
85+
/// the BLS entry will contain
86+
///
87+
/// ```text
88+
/// linux /boot/1/<entry_id>/linux
89+
/// initrd /boot/1/<entry_id>/initrd
90+
/// ```
91+
///
92+
/// If `boot_partition = "/boot"` and `boot_subdir = None` , the BLS entry will contain
93+
///
94+
/// ```text
95+
/// linux /<entry_id>/linux
96+
/// initrd /<entry_id>/initrd
97+
/// ```
98+
///
99+
/// * entry_id - In case of a BLS entry, the name of file to be generated in `loader/entries`
100+
/// * cmdline_extra - Extra kernel command line arguments
101+
///
66102
pub fn write_boot_simple<ObjectID: FsVerityHashValue>(
67103
repo: &Repository<ObjectID>,
68104
entry: BootEntry<ObjectID>,
69105
root_id: &ObjectID,
70-
bootdir: &Path,
106+
boot_partition: &Path,
107+
boot_subdir: Option<&str>,
71108
entry_id: Option<&str>,
72109
cmdline_extra: &[&str],
73110
) -> Result<()> {
74111
match entry {
75112
BootEntry::Type1(mut t1) => {
76113
if let Some(name) = entry_id {
77-
t1.relocate(name);
114+
t1.relocate(boot_subdir, name);
78115
}
79-
write_t1_simple(t1, bootdir, root_id, cmdline_extra, repo)?;
116+
write_t1_simple(
117+
t1,
118+
boot_partition,
119+
boot_subdir,
120+
root_id,
121+
cmdline_extra,
122+
repo,
123+
)?;
80124
}
81125
BootEntry::Type2(mut t2) => {
82126
if let Some(name) = entry_id {
83127
t2.rename(name);
84128
}
85129
ensure!(cmdline_extra.is_empty(), "Can't add --cmdline args to UKIs");
86-
write_t2_simple(t2, bootdir, root_id, repo)?;
130+
write_t2_simple(t2, boot_partition, root_id, repo)?;
87131
}
88132
BootEntry::UsrLibModulesUki(_entry) => todo!(),
89133
BootEntry::UsrLibModulesVmLinuz(entry) => {
90134
let mut t1 = entry.into_type1(entry_id);
91135
if let Some(name) = entry_id {
92-
t1.relocate(name)?;
136+
t1.relocate(boot_subdir, name);
93137
}
94-
write_t1_simple(t1, bootdir, root_id, cmdline_extra, repo)?;
138+
write_t1_simple(
139+
t1,
140+
boot_partition,
141+
boot_subdir,
142+
root_id,
143+
cmdline_extra,
144+
repo,
145+
)?;
95146
}
96147
};
97148

0 commit comments

Comments
 (0)