Skip to content

Commit f2c8d4c

Browse files
authored
Merge pull request #931 from HuijingHei/add-grubenv
grubconfig: create `boot/grub2/grubenv` if not existing
2 parents 354e410 + fc63e30 commit f2c8d4c

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

.cci.jenkinsfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ parallel build: {
1717
}
1818
stage("Unit tests") {
1919
shwrap("""
20+
dnf install -y grub2-tools-minimal
2021
cargo test
2122
""")
2223
}

.github/workflows/rust.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ jobs:
3232
uses: dtolnay/rust-toolchain@v1
3333
with:
3434
toolchain: stable
35+
- name: Install grub2
36+
run: |
37+
set -xeu
38+
dnf install -y grub2-tools-minimal
3539
- name: Cache build artifacts
3640
uses: Swatinem/rust-cache@v2
3741
- name: cargo build
@@ -49,6 +53,10 @@ jobs:
4953
uses: dtolnay/rust-toolchain@v1
5054
with:
5155
toolchain: stable
56+
- name: Install grub2
57+
run: |
58+
set -xeu
59+
dnf install -y grub2-tools-minimal
5260
- name: Cache build artifacts
5361
uses: Swatinem/rust-cache@v2
5462
- name: cargo build (release)
@@ -74,6 +82,10 @@ jobs:
7482
toolchain: ${{ env.MSRV }}
7583
- name: Cache build artifacts
7684
uses: Swatinem/rust-cache@v2
85+
- name: Install grub2
86+
run: |
87+
set -xeu
88+
dnf install -y grub2-tools-minimal
7789
- name: cargo build (release)
7890
run: cargo build --all-targets --release
7991
- name: cargo test (release)
@@ -111,6 +123,10 @@ jobs:
111123
uses: dtolnay/rust-toolchain@v1
112124
with:
113125
toolchain: ${{ matrix.channel }}
126+
- name: Install grub2
127+
run: |
128+
set -xeu
129+
dnf install -y grub2-tools-minimal
114130
- name: Cache build artifacts
115131
uses: Swatinem/rust-cache@v2
116132
- name: cargo build

src/grubconfigs.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
use std::fmt::Write;
2+
use std::os::unix::io::AsRawFd;
23
use std::path::{Path, PathBuf};
34

45
use anyhow::{anyhow, Context, Result};
6+
use bootc_utils::CommandRunExt;
57
use fn_error_context::context;
68
use openat_ext::OpenatDirExt;
79

810
/// The subdirectory of /boot we use
911
const GRUB2DIR: &str = "grub2";
1012
const CONFIGDIR: &str = "/usr/lib/bootupd/grub2-static";
1113
const DROPINDIR: &str = "configs.d";
14+
const GRUBENV: &str = "grubenv";
1215

1316
/// Install the static GRUB config files.
1417
#[context("Installing static GRUB configs")]
@@ -62,6 +65,8 @@ pub(crate) fn install(
6265
.context("Copying grub-static.cfg")?;
6366
println!("Installed: grub.cfg");
6467

68+
write_grubenv(&bootdir).context("Create grubenv")?;
69+
6570
let uuid_path = if write_uuid {
6671
let target_fs = if boot_is_mount { bootdir } else { target_root };
6772
let bootfs_meta = crate::filesystem::inspect_filesystem(target_fs, ".")?;
@@ -104,6 +109,24 @@ pub(crate) fn install(
104109
Ok(())
105110
}
106111

112+
#[context("Create file boot/grub2/grubenv")]
113+
fn write_grubenv(bootdir: &openat::Dir) -> Result<()> {
114+
let grubdir = &bootdir.sub_dir(GRUB2DIR).context("Opening boot/grub2")?;
115+
116+
if grubdir.exists(GRUBENV)? {
117+
return Ok(());
118+
}
119+
let editenv = Path::new("/usr/bin/grub2-editenv");
120+
if !editenv.exists() {
121+
anyhow::bail!("Failed to find {:?}", editenv);
122+
}
123+
124+
std::process::Command::new(editenv)
125+
.args([GRUBENV, "create"])
126+
.current_dir(format!("/proc/self/fd/{}", grubdir.as_raw_fd()))
127+
.run_with_cmd_context()
128+
}
129+
107130
#[cfg(test)]
108131
mod tests {
109132
use super::*;
@@ -124,4 +147,15 @@ mod tests {
124147
assert!(td.exists("boot/efi/EFI/fedora/grub.cfg")?);
125148
Ok(())
126149
}
150+
#[test]
151+
fn test_write_grubenv() -> Result<()> {
152+
let td = tempfile::tempdir()?;
153+
let tdp = td.path();
154+
std::fs::create_dir_all(tdp.join("boot/grub2"))?;
155+
let td = openat::Dir::open(&tdp.join("boot"))?;
156+
write_grubenv(&td)?;
157+
158+
assert!(td.exists("grub2/grubenv")?);
159+
Ok(())
160+
}
127161
}

0 commit comments

Comments
 (0)