bootupd 0.4.1

Bootloader updater
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
use anyhow::{bail, Context, Result};
use camino::Utf8PathBuf;
use cap_std::ambient_authority;
use cap_std::fs::Dir;
use cap_std_ext::dirext::CapStdExtDirExt;
use fn_error_context::context;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use std::process::Command;

use bootc_internal_blockdev::Device;

use crate::bootloader::Bootloader;
use crate::bootupd::{RootContext, Sysroot};
use crate::component::*;
use crate::filetree::FileMetadata;
use crate::filetree::FileTree;
use crate::freezethaw::fsfreeze_thaw_cycle;
use crate::grubconfigs;
use crate::model::*;
use crate::packagesystem;

// TODO: grub bin and modules should be handled differently when we're making this
//       distro-agnostic. See https://github.com/coreos/bootupd/issues/810

// grub2-install file path
pub(crate) const GRUB_BIN: &str = "usr/sbin/grub2-install";

/// The GRUB target directory holding the images and modules which
/// grub2-install embeds into the boot device
#[cfg(target_arch = "x86_64")]
const GRUB_MODULES: &str = "usr/lib/grub/i386-pc";
#[cfg(target_arch = "powerpc64")]
const GRUB_MODULES: &str = "usr/lib/grub/powerpc-ieee1275";

#[cfg(target_arch = "powerpc64")]
fn target_device(device: &Device) -> Result<String> {
    const PREPBOOT_GUID: &str = "9E1A2D38-C612-4316-AA26-8B49521E5A8B";
    /// We make a best-effort to support MBR partitioning too.
    const PREPBOOT_MBR_TYPE: &str = "41";

    if !device.has_children() {
        return Ok(device.path());
    }

    let prepdev = device
        .find_partition_of_type(PREPBOOT_GUID)
        .or_else(|| device.find_partition_of_type(PREPBOOT_MBR_TYPE))
        .ok_or_else(|| {
            anyhow::anyhow!("Failed to find PReP partition with GUID {PREPBOOT_GUID}")
        })?;
    Ok(prepdev.path())
}

/// Hash every file under the GRUB target directory,
/// keyed by `<GRUB_MODULES>/<relative path>`
#[context("Hashing GRUB files in directory at {path}")]
fn hash_grub_dir(
    dir: &Dir,
    path: &str,
    children: &mut BTreeMap<String, FileMetadata>,
) -> Result<()> {
    for entry in dir.entries_utf8()? {
        let entry = entry?;
        let name = entry.file_name().context("getting file name")?;
        let rel_path = format!("{path}/{name}");
        let file_type = entry.file_type()?;
        if file_type.is_file() {
            let meta = FileMetadata::new_from_path(dir, &name)
                .with_context(|| format!("hashing {rel_path}"))?;
            children.insert(rel_path, meta);
        } else if file_type.is_dir() {
            let child = dir.open_dir(&name)?;
            hash_grub_dir(&child, &rel_path, children)?;
        } else {
            bail!("Unsupported file type in the GRUB modules directory: {rel_path}");
        }
    }
    Ok(())
}

/// The BIOS update payload is the GRUB installer binary plus its inputs.
///
/// The images and modules can change without the installer binary changing,
/// and are embedded into the installed boot code. A sysroot without
/// the modules is fingerprinted by the installer binary alone.
#[context("Generating GRUB FileTree from root {}", root.as_ref().display())]
fn grub_filetree(root: impl AsRef<Path>) -> Result<FileTree> {
    let root = root.as_ref();
    let dir = Dir::open_ambient_dir(root, ambient_authority())
        .with_context(|| format!("opening {}", root.display()))?;

    let mut children = BTreeMap::new();
    let meta = FileMetadata::new_from_path(&dir, GRUB_BIN)
        .with_context(|| format!("hashing {GRUB_BIN}"))?;
    children.insert(GRUB_BIN.into(), meta);

    if let Some(grubdir) = dir.open_dir_optional(GRUB_MODULES)? {
        hash_grub_dir(&grubdir, GRUB_MODULES, &mut children)?;
    }

    Ok(FileTree { children })
}

/// Errors unless `path` is a file, possibly through a symbolic link.
fn ensure_file(path: &Path) -> Result<()> {
    match path.metadata() {
        Ok(meta) if meta.is_file() => Ok(()),
        Ok(_) => bail!("{} is not a file", path.display()),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            bail!("Failed to find {:?}", path)
        }
        Err(e) => Err(e).with_context(|| format!("checking for {}", path.display())),
    }
}

/// The grub2-install in `process_root` that `install` runs. Errors unless it
/// is a file this process may execute, and the GRUB modules it embeds are a
/// directory holding the `modinfo.sh` it reads first, each of them possibly
/// through a symbolic link.
fn grub_install_bin(process_root: &Path) -> Result<PathBuf> {
    let modules = process_root.join(GRUB_MODULES);
    match modules.metadata() {
        Ok(meta) if meta.is_dir() => {}
        Ok(_) => bail!("{} is not a directory", modules.display()),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            bail!("Failed to find GRUB modules at {}", modules.display())
        }
        Err(e) => {
            return Err(e)
                .with_context(|| format!("checking for GRUB modules at {}", modules.display()))
        }
    }
    ensure_file(&modules.join("modinfo.sh"))?;
    let grub_install = process_root.join(GRUB_BIN);
    ensure_file(&grub_install)?;
    rustix::fs::access(&grub_install, rustix::fs::Access::EXEC_OK)
        .with_context(|| format!("{} cannot be run", grub_install.display()))?;
    Ok(grub_install)
}

/// Errors unless the grub2-install of [`grub_install_bin`] also runs: one
/// with execute permission may not, such as a script whose interpreter is
/// missing. It is asked only for its version, so it writes nothing.
fn check_grub_install(process_root: &Path) -> Result<()> {
    let grub_install = grub_install_bin(process_root)?;
    let mut cmd = Command::new(&grub_install);
    cmd.arg("--version").stdin(std::process::Stdio::null());
    let cmdout = cmd
        .output()
        .with_context(|| format!("{} cannot be run", grub_install.display()))?;
    if !cmdout.status.success() {
        std::io::stderr().write_all(&cmdout.stderr)?;
        bail!("Failed to run {:?}", cmd);
    }
    Ok(())
}

#[derive(Default)]
pub(crate) struct Bios {}

impl Bios {
    // Run grub2-install from the process root, returning that root so
    // callers can identify the installer which was actually used
    fn run_grub_install(&self, dest_root: &str, device: &Device) -> Result<PathBuf> {
        let process_root = PathBuf::from("/");
        let grub_install = grub_install_bin(&process_root)?;

        let mut cmd = Command::new(grub_install);
        let boot_dir = Path::new(dest_root).join("boot");
        // We forcibly inject mdraid1x because it's needed by CoreOS's default of "install raw disk image"
        // We also add part_gpt because in some cases probing of the partition map can fail such
        // as in a container, but we always use GPT.
        #[cfg(target_arch = "x86_64")]
        cmd.args(["--target", "i386-pc"])
            .args(["--boot-directory", boot_dir.to_str().unwrap()])
            .args(["--modules", "mdraid1x part_gpt"])
            .arg(&device.path());

        #[cfg(target_arch = "powerpc64")]
        {
            let device = target_device(device)?;
            cmd.args(&["--target", "powerpc-ieee1275"])
                .args(&["--boot-directory", boot_dir.to_str().unwrap()])
                .arg("--no-nvram")
                .arg(&device);
        }

        let cmdout = cmd.output()?;
        if !cmdout.status.success() {
            std::io::stderr().write_all(&cmdout.stderr)?;
            bail!("Failed to run {:?}", cmd);
        }
        Ok(process_root)
    }
}

impl Component for Bios {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn name(&self) -> &'static str {
        self.component_type().into()
    }

    fn component_type(&self) -> ComponentType {
        ComponentType::Bios
    }

    fn is_bootloader_supported(&self, bootloader: Bootloader) -> bool {
        matches!(bootloader, Bootloader::Grub)
    }

    /// Refuses to install without a grub2-install that starts and the GRUB
    /// modules, which `install` runs from the running system, not from
    /// `src_root`.
    fn check_payload(&self, _src_root: &str, _bootloader: Bootloader) -> Result<()> {
        check_grub_install(Path::new("/"))
    }

    fn install(
        &self,
        src_root: &str,
        dest_root: &str,
        device: Option<&Device>,
        _update_firmware: bool,
        bootloader: Bootloader,
    ) -> Result<InstalledContent> {
        if !self.is_bootloader_supported(bootloader) {
            anyhow::bail!("{bootloader} cannot be installed for {}", self.name());
        }

        let device =
            device.ok_or_else(|| anyhow::anyhow!("BIOS component requires a target device"))?;
        let src_dir = Dir::open_ambient_dir(src_root, ambient_authority())
            .with_context(|| format!("opening source directory {src_root}"))?;
        let Some(meta) = self.get_component_update(&src_dir, Some(bootloader))? else {
            anyhow::bail!("No update metadata for component {} found", self.name());
        };

        // Record the installer which was actually used, which comes from
        // the process root rather than from src_root (when they are not equivalent)
        let root = self
            .run_grub_install(dest_root, device)
            .with_context(|| format!("installing GRUB on {}", device.path()))?;

        Ok(InstalledContent {
            meta,
            filetree: Some(grub_filetree(&root)?),
            adopted_from: None,
        })
    }

    fn generate_update_metadata(&self, sysroot_path: &str) -> Result<Option<ContentMetadata>> {
        let grub_install = Path::new(sysroot_path).join(GRUB_BIN);
        if !grub_install.exists() {
            println!("Failed to find {:?}", grub_install);
            return Ok(None);
        }

        // Query the rpm database and list the package and build times for /usr/sbin/grub2-install
        let meta = packagesystem::query_files(sysroot_path, [&grub_install])?;
        write_update_metadata(sysroot_path, self.component_update_data_name(), &meta)?;
        Ok(Some(meta))
    }

    fn query_adopt(&self, devices: &Option<Vec<Device>>) -> Result<Option<Adoptable>> {
        #[cfg(target_arch = "x86_64")]
        if crate::efi::is_efi_booted()? && devices.is_none() {
            log::debug!("Skip BIOS adopt");
            return Ok(None);
        }
        crate::component::query_adopt_state()
    }

    // Backup the current grub.cfg and replace with new static config
    // - Backup "/boot/loader/grub.cfg" to "/boot/grub2/grub.cfg.bak"
    // - Remove symlink "/boot/grub2/grub.cfg"
    // - Replace "/boot/grub2/grub.cfg" symlink with new static "grub.cfg"
    fn migrate_static_grub_config(&self, sysroot_path: &str, destdir: &Dir) -> Result<()> {
        let grub = "boot/grub2";
        // sysroot_path is /, destdir is Dir of /
        let grub_config_path = Utf8PathBuf::from(sysroot_path).join(grub);
        let grub_config_dir = destdir.open_dir(grub).context("Opening boot/grub2")?;

        let grub_config = grub_config_path.join(grubconfigs::GRUBCONFIG);

        if !grub_config.exists() {
            anyhow::bail!("Could not find '{}'", grub_config);
        }

        let mut current_config;
        // If /boot/grub2/grub.cfg is not symlink, we need to keep going
        if !grub_config.is_symlink() {
            println!("'{}' is not a symlink", grub_config);
            current_config = grub_config.clone();
        } else {
            // If /boot/grub2/grub.cfg is symlink to /boot/loader/grub.cfg,
            // backup it to /boot/grub2/grub.cfg.bak
            // Get real file for symlink /boot/grub2/grub.cfg
            let real_config = grub_config_dir.read_link(grubconfigs::GRUBCONFIG)?;
            let real_config =
                Utf8PathBuf::from_path_buf(real_config).expect("Path should be valid UTF-8");
            // Resolve symlink location
            current_config = grub_config_path.clone();
            current_config.push(real_config);
        }

        let backup_config = grub_config_path.join(grubconfigs::GRUBCONFIG_BACKUP);
        if !backup_config.exists() {
            // Backup the current GRUB config which is hopefully working right now
            println!(
                "Creating a backup of the current GRUB config '{}' in '{}'...",
                current_config, backup_config
            );
            std::fs::copy(&current_config, &backup_config)
                .context("Failed to backup GRUB config")?;
        }

        crate::grubconfigs::install(&destdir, None, None, true, None)?;

        // Remove the real config if it is symlink and will not
        // if /boot/grub2/grub.cfg is file
        if current_config != grub_config {
            println!("Removing {}", current_config);

            // current_config is an absolute path of the form
            //
            // /boot/grub2/../loader/grub.cfg
            //
            // as it's a RELATIVE symlink from /boot/grub2/grub.cfg -> /boot/loader/grub.cfg
            match std::fs::remove_file(current_config.as_std_path()) {
                Ok(..) => { /* No op */ }
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                    println!("{} not found for removal", current_config);
                }
                Err(e) => Err(e).with_context(|| format!("Removing {current_config}"))?,
            };
        }

        // Synchronize the filesystem containing /boot/grub2 to disk.
        fsfreeze_thaw_cycle(grub_config_dir.reopen_as_ownedfd()?)?;

        Ok(())
    }

    fn adopt_update(
        &self,
        rootcxt: &RootContext,
        update: &ContentMetadata,
        with_static_config: bool,
        bootloader: Bootloader,
    ) -> Result<Option<InstalledContent>> {
        if !self.is_bootloader_supported(bootloader) {
            log::debug!(
                "Skip BIOS adopt: bootloader {} is not supported",
                bootloader.efi_component_name()
            );
            return Ok(None);
        }

        let bios_devices = rootcxt.device.find_colocated_bios_boot()?;
        let Some(meta) = self.query_adopt(&bios_devices)? else {
            return Ok(None);
        };

        // Install grub onto each parent (whole-disk) device. For BIOS boot on
        // multi-disk setups (e.g. RAID), grub must be written to every backing
        // disk's MBR/BIOS-boot partition so the system can boot from any one.
        for parent in rootcxt.device.find_all_roots()? {
            self.run_grub_install(rootcxt.path.as_str(), &parent)?;
            log::debug!("Installed grub modules on {}", parent.path());
        }

        if with_static_config {
            // Install the static config if the OSTree bootloader is not set.
            if let Some(bootloader) = crate::ostreeutil::get_ostree_bootloader()? {
                println!(
                    "ostree repo 'sysroot.bootloader' config option is currently set to: '{bootloader}'",
                );
            } else {
                println!("ostree repo 'sysroot.bootloader' config option is not set yet");
                self.migrate_static_grub_config(rootcxt.path.as_str(), &rootcxt.sysroot)?;
            };
        }
        Ok(Some(InstalledContent {
            meta: update.clone(),
            filetree: Some(grub_filetree(&rootcxt.path)?),
            adopted_from: Some(meta.version),
        }))
    }

    fn query_update(
        &self,
        sysroot: &Dir,
        bootloader: Bootloader,
    ) -> Result<Option<ContentMetadata>> {
        if !self.is_bootloader_supported(bootloader) {
            return Ok(None);
        }

        self.get_component_update(sysroot, Some(bootloader))
    }

    fn query_requires_update(&self, sysroot: &Dir) -> Result<()> {
        // Failed as expected if booted with BIOS and no update metadata
        if !sysroot.exists("sys/firmware/efi") {
            anyhow::bail!("Failed to find BIOS update metadata");
        }
        Ok(())
    }

    fn run_update(&self, rootcxt: &RootContext, _: &InstalledContent) -> Result<InstalledContent> {
        let updatemeta = self
            .query_update(&rootcxt.sysroot, Bootloader::Grub)?
            .expect("update available");

        for parent in rootcxt.device.find_all_roots()? {
            self.run_grub_install(rootcxt.path.as_str(), &parent)?;
            log::debug!("Installed grub modules on {}", parent.path());
        }

        let adopted_from = None;
        Ok(InstalledContent {
            meta: updatemeta,
            filetree: Some(grub_filetree(&rootcxt.path)?),
            adopted_from,
        })
    }

    fn validate(&self, _: &InstalledContent, _device: &Device) -> Result<ValidationResult> {
        Ok(ValidationResult::Skip)
    }

    fn get_efi_vendor(&self, _: &Path) -> Result<Option<String>> {
        Ok(None)
    }

    fn has_changed_content(
        &self,
        sysroot: Sysroot<'_>,
        current: &InstalledContent,
        _bootloader: Bootloader,
    ) -> Result<bool> {
        let updated = grub_filetree(sysroot.path)?;
        if let Some(t) = current.filetree.as_ref() {
            Ok(!t.is_content_eq(&updated))
        } else {
            // Fallback path for existing installs which don't store a filetree for the BIOS component
            log::warn!("no filetree available for the current install, assuming updatable");
            Ok(true)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::packagesystem::Module;
    use camino::Utf8Path;
    use chrono::Utc;
    use std::path::PathBuf;

    /// Make `program`, found in `PATH`, the grub2-install of `root`: a link
    /// to it, since a script of our own could not run where the temporary
    /// directory is mounted noexec.
    fn link_grub_install(root: &Path, program: &str) -> Result<PathBuf> {
        let path = std::env::var_os("PATH").context("PATH is unset")?;
        let program = std::env::split_paths(&path)
            .map(|dir| dir.join(program))
            .find(|candidate| candidate.is_file())
            .with_context(|| format!("no {program} in PATH"))?;
        let bin = root.join(GRUB_BIN);
        std::fs::create_dir_all(bin.parent().unwrap())?;
        std::os::unix::fs::symlink(program, &bin)?;
        Ok(bin)
    }

    #[test]
    fn test_grub_install_bin() -> Result<()> {
        use std::os::unix::fs::PermissionsExt;
        let td = tempfile::tempdir()?;
        let root = td.path();
        let modules = root.join(GRUB_MODULES);
        let err = grub_install_bin(root).unwrap_err();
        assert_eq!(
            err.to_string(),
            format!("Failed to find GRUB modules at {}", modules.display())
        );
        std::fs::create_dir_all(modules.parent().unwrap())?;
        std::fs::write(&modules, "")?;
        let err = grub_install_bin(root).unwrap_err();
        assert!(err.to_string().ends_with("is not a directory"), "{err}");
        std::fs::remove_file(&modules)?;
        // An empty directory lacks what grub2-install reads first.
        std::fs::create_dir_all(&modules)?;
        let err = grub_install_bin(root).unwrap_err();
        assert!(err.to_string().contains("modinfo.sh"), "{err}");
        std::fs::write(modules.join("modinfo.sh"), "")?;
        let err = grub_install_bin(root).unwrap_err();
        assert!(err.to_string().contains(GRUB_BIN), "{err}");
        let bin = root.join(GRUB_BIN);
        std::fs::create_dir_all(&bin)?;
        let err = grub_install_bin(root).unwrap_err();
        assert!(err.to_string().ends_with("is not a file"), "{err}");
        std::fs::remove_dir(&bin)?;
        std::fs::write(&bin, "")?;
        let err = grub_install_bin(root).unwrap_err();
        assert!(err.to_string().ends_with("cannot be run"), "{err}");
        // Nor is execute permission enough for a script whose interpreter is
        // missing. Where the directory is mounted noexec, the permission
        // check refuses it already.
        std::fs::write(&bin, "#!/nonexistent\n")?;
        std::fs::set_permissions(&bin, std::fs::Permissions::from_mode(0o755))?;
        let err = check_grub_install(root).unwrap_err();
        assert!(err.to_string().ends_with("cannot be run"), "{err}");
        std::fs::remove_file(&bin)?;
        // One that starts has to succeed too.
        assert_eq!(link_grub_install(root, "false")?, bin);
        assert_eq!(grub_install_bin(root)?, bin);
        let err = check_grub_install(root).unwrap_err();
        assert!(err.to_string().starts_with("Failed to run"), "{err}");
        std::fs::remove_file(&bin)?;
        link_grub_install(root, "true")?;
        assert_eq!(grub_install_bin(root)?, bin);
        check_grub_install(root)?;
        Ok(())
    }

    #[test]
    fn test_query_updatable_content_differs() -> Result<()> {
        let sysroot = tempfile::tempdir()?;
        let sysroot_path = Utf8Path::from_path(sysroot.path()).expect("valid UTF-8");

        // Create update payload
        std::fs::create_dir_all(sysroot_path.join("usr/sbin"))?;
        std::fs::write(sysroot_path.join(GRUB_BIN), "grub2-install new")?;
        std::fs::create_dir_all(sysroot_path.join(BOOTUPD_UPDATES_DIR))?;
        let meta = ContentMetadata {
            timestamp: Utc::now(),
            version: "grub2 2.12-7.el9".into(),
            versions: Some(vec![Module {
                name: "grub2".into(),
                evr: "2.12-7.el9".into(),
            }]),
            #[cfg(efi_arch)]
            default_bootloader: None,
        };
        write_update_metadata(sysroot_path.as_str(), PathBuf::from("BIOS.json"), &meta)?;

        // A payload with the content that is currently installed
        let old_payload = tempfile::tempdir()?;
        std::fs::create_dir_all(old_payload.path().join("usr/sbin"))?;
        std::fs::write(old_payload.path().join(GRUB_BIN), "grub2-install old")?;

        let sysroot_dir = Dir::open_ambient_dir(sysroot_path, ambient_authority())?;
        let sysroot = Sysroot {
            dir: &sysroot_dir,
            path: sysroot_path,
        };
        let bios = Bios::default();

        // Changed content wins even though the metadata is identical
        let inst = InstalledContent {
            meta: meta.clone(),
            filetree: Some(grub_filetree(old_payload.path())?),
            adopted_from: None,
        };
        let (update, updatable) = bios.query_updatable(sysroot, &inst, Bootloader::Grub)?;
        assert!(update.is_some());
        assert!(matches!(updatable, ComponentUpdatable::Upgradable));

        // Installs from before filetrees were stored, update once
        let inst = InstalledContent {
            meta: meta.clone(),
            filetree: None,
            adopted_from: None,
        };
        let (_, updatable) = bios.query_updatable(sysroot, &inst, Bootloader::Grub)?;
        assert!(matches!(updatable, ComponentUpdatable::Upgradable));

        // Identical content, no update
        let inst = InstalledContent {
            meta: meta.clone(),
            filetree: Some(grub_filetree(sysroot.path)?),
            adopted_from: None,
        };
        let (_, updatable) = bios.query_updatable(sysroot, &inst, Bootloader::Grub)?;
        assert!(matches!(updatable, ComponentUpdatable::AtLatestVersion));

        Ok(())
    }

    #[test]
    fn test_grub_filetree_includes_modules() -> Result<()> {
        let payload = tempfile::tempdir()?;
        std::fs::create_dir_all(payload.path().join("usr/sbin"))?;
        std::fs::write(payload.path().join(GRUB_BIN), "grub2-install")?;
        let modules = payload.path().join(GRUB_MODULES);
        std::fs::create_dir_all(&modules)?;
        std::fs::write(modules.join("kernel.img"), "kernel")?;
        let fonts = modules.join("fonts");
        std::fs::create_dir_all(&fonts)?;
        std::fs::write(fonts.join("unicode.pf2"), "font")?;

        let tree = grub_filetree(payload.path())?;
        assert!(tree.children.contains_key(GRUB_BIN));
        assert!(tree
            .children
            .contains_key(&format!("{GRUB_MODULES}/kernel.img")));

        // The fingerprint changes when a module changes but grub2-install
        // does not
        std::fs::write(
            payload.path().join(GRUB_MODULES).join("kernel.img"),
            "kernel new",
        )?;
        let changed = grub_filetree(payload.path())?;
        assert!(!tree.is_content_eq(&changed));

        // Nested file is keyed by its full path under the modules dir
        let tree = grub_filetree(payload.path())?;
        assert!(tree
            .children
            .contains_key(&format!("{GRUB_MODULES}/fonts/unicode.pf2")));

        // A change in the nested dir changes the tree
        std::fs::write(fonts.join("unicode.pf2"), "font new")?;
        let changed = grub_filetree(payload.path())?;
        assert!(!tree.is_content_eq(&changed));

        Ok(())
    }

    #[test]
    fn test_grub_filetree_refuses_symlinks_in_modules() -> Result<()> {
        let payload = tempfile::tempdir()?;
        std::fs::create_dir_all(payload.path().join("usr/sbin"))?;
        std::fs::write(payload.path().join(GRUB_BIN), "grub2-install")?;
        let modules = payload.path().join(GRUB_MODULES);
        std::fs::create_dir_all(&modules)?;
        std::os::unix::fs::symlink("target", modules.join("link"))?;

        let err = grub_filetree(payload.path())
            .expect_err("a symlink in the modules directory should be refused");
        assert!(format!("{err:#}").contains("Unsupported file type"));
        Ok(())
    }
}