Trim, flush outstanding writes, and freeze/thaw the target mounted filesystem; these steps prepare the filesystem for its first booted use.
(
fsname: &str,
root: &Dir,
path: impl AsRef<Utf8Path>,
)
| 1407 | /// Trim, flush outstanding writes, and freeze/thaw the target mounted filesystem; |
| 1408 | /// these steps prepare the filesystem for its first booted use. |
| 1409 | pub(crate) fn finalize_filesystem( |
| 1410 | fsname: &str, |
| 1411 | root: &Dir, |
| 1412 | path: impl AsRef<Utf8Path>, |
| 1413 | ) -> Result<()> { |
| 1414 | let path = path.as_ref(); |
| 1415 | // fstrim ensures the underlying block device knows about unused space |
| 1416 | Task::new(format!("Trimming {fsname}"), "fstrim") |
| 1417 | .args(["--quiet-unsupported", "-v", path.as_str()]) |
| 1418 | .cwd(root)? |
| 1419 | .run()?; |
| 1420 | // Remounting readonly will flush outstanding writes and ensure we error out if there were background |
| 1421 | // writeback problems. |
| 1422 | Task::new(format!("Finalizing filesystem {fsname}"), "mount") |
| 1423 | .cwd(root)? |
| 1424 | .args(["-o", "remount,ro", path.as_str()]) |
| 1425 | .run()?; |
| 1426 | // Finally, freezing (and thawing) the filesystem will flush the journal, which means the next boot is clean. |
| 1427 | // VFAT has no journal and does not support fsfreeze. Might need to be expanded in the future |
| 1428 | // to also *not* fsfreeze other filesystems. |
| 1429 | let fsdir = root.open_dir(path.as_str())?; |
| 1430 | let st = rustix::fs::fstatfs(fsdir.as_fd())?; |
| 1431 | if st.f_type == libc::MSDOS_SUPER_MAGIC { |
| 1432 | tracing::debug!("Filesystem {fsname} is VFAT, skipping fsfreeze"); |
| 1433 | } else { |
| 1434 | for a in ["-f", "-u"] { |
| 1435 | Command::new("fsfreeze") |
| 1436 | .cwd_dir(root.try_clone()?) |
| 1437 | .args([a, path.as_str()]) |
| 1438 | .run_capture_stderr()?; |
| 1439 | } |
| 1440 | } |
| 1441 | Ok(()) |
| 1442 | } |
| 1443 | |
| 1444 | /// A heuristic check that we were invoked with --pid=host |
| 1445 | fn require_host_pidns() -> Result<()> { |
no test coverage detected