-
Notifications
You must be signed in to change notification settings - Fork 154
install: Handle mount points in rootfs operations #1727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ckyrouac
wants to merge
1
commit into
bootc-dev:main
Choose a base branch
from
ckyrouac:var-mount
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+283
−11
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1803,6 +1803,50 @@ pub(crate) async fn install_to_disk(mut opts: InstallToDiskOpts) -> Result<()> { | |
| Ok(()) | ||
| } | ||
|
|
||
| /// Require that a directory contains only mount points (or is empty), recursively. | ||
| /// Returns Ok(()) if all entries in the directory tree are either: | ||
| /// - Mount points (on different filesystems) | ||
| /// - Directories that themselves contain only mount points (recursively) | ||
| /// - The lost+found directory | ||
| /// | ||
| /// Returns an error if any non-mount entry is found. | ||
| /// | ||
| /// This handles cases like /var containing /var/lib (not a mount) which contains | ||
| /// /var/lib/containers (a mount point). | ||
| #[context("Requiring directory contains only mount points")] | ||
| fn require_dir_contains_only_mounts(parent_fd: &Dir, dir_name: &str) -> Result<()> { | ||
| let Some(dir_fd) = parent_fd.open_dir_noxdev(dir_name)? else { | ||
| // The directory itself is a mount point | ||
| return Ok(()); | ||
| }; | ||
|
|
||
| if dir_fd.entries()?.next().is_none() { | ||
| anyhow::bail!("Found empty directory: {dir_name}"); | ||
| } | ||
|
|
||
| for entry in dir_fd.entries()? { | ||
| let entry = DirEntryUtf8::from_cap_std(entry?); | ||
| let entry_name = entry.file_name()?; | ||
|
|
||
| if entry_name == LOST_AND_FOUND { | ||
| continue; | ||
| } | ||
|
|
||
| if dir_name == BOOT && entry_name == crate::bootloader::EFI_DIR { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit of ugly special casing, I wonder if we still need it? If we do...it may be better to keep the special casing in |
||
| continue; | ||
| } | ||
|
|
||
| let etype = entry.file_type()?; | ||
| if etype == FileType::dir() && dir_fd.open_dir_noxdev(&entry_name)?.is_some() { | ||
| require_dir_contains_only_mounts(&dir_fd, &entry_name)?; | ||
| } else { | ||
| anyhow::bail!("Found entry in {dir_name}: {entry_name}"); | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[context("Verifying empty rootfs")] | ||
| fn require_empty_rootdir(rootfs_fd: &Dir) -> Result<()> { | ||
| for e in rootfs_fd.entries()? { | ||
|
|
@@ -1811,17 +1855,11 @@ fn require_empty_rootdir(rootfs_fd: &Dir) -> Result<()> { | |
| if name == LOST_AND_FOUND { | ||
| continue; | ||
| } | ||
| // There must be a boot directory (that is empty) | ||
| if name == BOOT { | ||
| let mut entries = rootfs_fd.read_dir(BOOT)?; | ||
| if let Some(e) = entries.next() { | ||
| let e = DirEntryUtf8::from_cap_std(e?); | ||
| let name = e.file_name()?; | ||
| if matches!(name.as_str(), LOST_AND_FOUND | crate::bootloader::EFI_DIR) { | ||
| continue; | ||
| } | ||
| anyhow::bail!("Non-empty boot directory, found {name}"); | ||
| } | ||
|
|
||
| // Check if this entry is a directory | ||
| let etype = e.file_type()?; | ||
| if etype == FileType::dir() { | ||
| require_dir_contains_only_mounts(rootfs_fd, &name)?; | ||
| } else { | ||
| anyhow::bail!("Non-empty root filesystem; found {name:?}"); | ||
| } | ||
|
|
@@ -2573,4 +2611,101 @@ UUID=boot-uuid /boot ext4 defaults 0 0 | |
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_require_dir_contains_only_mounts() -> Result<()> { | ||
| // Test 1: Empty directory should fail (not a mount point) | ||
| { | ||
| let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; | ||
| td.create_dir("empty")?; | ||
| assert!(require_dir_contains_only_mounts(&td, "empty").is_err()); | ||
| } | ||
|
|
||
| // Test 2: Directory with only lost+found should succeed (lost+found is ignored) | ||
| { | ||
| let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; | ||
| td.create_dir_all("var/lost+found")?; | ||
| assert!(require_dir_contains_only_mounts(&td, "var").is_ok()); | ||
| } | ||
|
|
||
| // Test 3: Directory with a regular file should fail | ||
| { | ||
| let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; | ||
| td.create_dir("var")?; | ||
| td.write("var/test.txt", b"content")?; | ||
| assert!(require_dir_contains_only_mounts(&td, "var").is_err()); | ||
| } | ||
|
|
||
| // Test 4: Nested directory structure with a file should fail | ||
| { | ||
| let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; | ||
| td.create_dir_all("var/lib/containers")?; | ||
| td.write("var/lib/containers/storage.db", b"data")?; | ||
| assert!(require_dir_contains_only_mounts(&td, "var").is_err()); | ||
| } | ||
|
|
||
| // Test 5: boot directory with efi subdirectory should succeed (efi is allowed in boot) | ||
| { | ||
| let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; | ||
| td.create_dir_all("boot/efi")?; | ||
| assert!(require_dir_contains_only_mounts(&td, "boot").is_ok()); | ||
| } | ||
|
|
||
| // Test 6: boot directory with both efi and lost+found should succeed (both are allowed) | ||
| { | ||
| let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; | ||
| td.create_dir_all("boot/efi")?; | ||
| td.create_dir_all("boot/lost+found")?; | ||
| assert!(require_dir_contains_only_mounts(&td, "boot").is_ok()); | ||
| } | ||
|
|
||
| // Test 7: boot directory with grub should fail (grub2 is not a mount and contains files) | ||
| { | ||
| let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; | ||
| td.create_dir_all("boot/grub2")?; | ||
| td.write("boot/grub2/grub.cfg", b"config")?; | ||
| assert!(require_dir_contains_only_mounts(&td, "boot").is_err()); | ||
| } | ||
|
|
||
| // Test 8: Nested empty directories should fail (empty directories are not mount points) | ||
| { | ||
| let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; | ||
| td.create_dir_all("var/lib/containers")?; | ||
| td.create_dir_all("var/log/journal")?; | ||
| assert!(require_dir_contains_only_mounts(&td, "var").is_err()); | ||
| } | ||
|
|
||
| // Test 9: Directory with lost+found and a file should fail (lost+found is ignored, but file is not allowed) | ||
| { | ||
| let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; | ||
| td.create_dir_all("var/lost+found")?; | ||
| td.write("var/data.txt", b"content")?; | ||
| assert!(require_dir_contains_only_mounts(&td, "var").is_err()); | ||
| } | ||
|
|
||
| // Test 10: Directory with a symlink should fail | ||
| { | ||
| let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; | ||
| td.create_dir("var")?; | ||
| td.symlink_contents("../usr/lib", "var/lib")?; | ||
| assert!(require_dir_contains_only_mounts(&td, "var").is_err()); | ||
| } | ||
|
|
||
| // Test 11: Deeply nested directory with a file should fail | ||
| { | ||
| let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; | ||
| td.create_dir_all("var/lib/containers/storage/overlay")?; | ||
| td.write("var/lib/containers/storage/overlay/file.txt", b"data")?; | ||
| assert!(require_dir_contains_only_mounts(&td, "var").is_err()); | ||
| } | ||
|
|
||
| // Test 12: Non-boot directory with efi should fail (special handling only applies to boot/efi) | ||
| { | ||
| let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; | ||
| td.create_dir_all("var/efi")?; | ||
| assert!(require_dir_contains_only_mounts(&td, "var").is_err()); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait why is this an error? It seems to conflict with the doc string.