1use std::{
4 fs,
5 mem::MaybeUninit,
6 os::fd::{AsFd, OwnedFd},
7 process::Command,
8};
9
10use anyhow::{Context, Result, anyhow};
11use bootc_utils::CommandRunExt;
12use camino::Utf8Path;
13use cap_std_ext::{cap_std::fs::Dir, cmdext::CapStdExtCommandExt};
14use fn_error_context::context;
15use rustix::{
16 mount::{MoveMountFlags, OpenTreeFlags},
17 net::{
18 AddressFamily, RecvFlags, SendAncillaryBuffer, SendAncillaryMessage, SendFlags,
19 SocketFlags, SocketType,
20 },
21 process::WaitOptions,
22 thread::Pid,
23};
24use serde::Deserialize;
25
26pub mod tempmount;
28
29pub const PID1: Pid = const {
31 match Pid::from_raw(1) {
32 Some(v) => v,
33 None => panic!("Expected to parse pid1"),
34 }
35};
36
37#[derive(Deserialize, Debug)]
39#[serde(rename_all = "kebab-case")]
40#[allow(dead_code)]
41pub struct Filesystem {
42 pub source: String,
45 pub target: String,
47 #[serde(rename = "maj:min")]
49 pub maj_min: String,
50 pub fstype: String,
52 pub options: String,
54 pub uuid: Option<String>,
56 pub children: Option<Vec<Filesystem>>,
58}
59
60#[derive(Deserialize, Debug, Default)]
62pub struct Findmnt {
63 pub filesystems: Vec<Filesystem>,
65}
66
67pub fn run_findmnt(args: &[&str], cwd: Option<&Dir>, path: Option<&str>) -> Result<Findmnt> {
69 let mut cmd = Command::new("findmnt");
70 if let Some(cwd) = cwd {
71 cmd.cwd_dir(cwd.try_clone()?);
72 }
73 cmd.args([
74 "-J",
75 "-v",
76 "--output=SOURCE,TARGET,MAJ:MIN,FSTYPE,OPTIONS,UUID",
78 ])
79 .args(args)
80 .args(path);
81 let o: Findmnt = cmd.log_debug().run_and_parse_json()?;
82 Ok(o)
83}
84
85fn findmnt_filesystem(args: &[&str], cwd: Option<&Dir>, path: &str) -> Result<Filesystem> {
87 let o = run_findmnt(args, cwd, Some(path))?;
88 o.filesystems
89 .into_iter()
90 .next()
91 .ok_or_else(|| anyhow!("findmnt returned no data for {path}"))
92}
93
94#[context("Inspecting filesystem {path}")]
95pub fn inspect_filesystem(path: &Utf8Path) -> Result<Filesystem> {
98 findmnt_filesystem(&["--mountpoint"], None, path.as_str())
99}
100
101#[context("Inspecting filesystem")]
102pub fn inspect_filesystem_of_dir(d: &Dir) -> Result<Filesystem> {
105 findmnt_filesystem(&["--mountpoint"], Some(d), ".")
106}
107
108#[context("Inspecting filesystem by UUID {uuid}")]
109pub fn inspect_filesystem_by_uuid(uuid: &str) -> Result<Filesystem> {
111 findmnt_filesystem(&["--source"], None, &(format!("UUID={uuid}")))
112}
113
114pub fn is_mounted_in_pid1_mountns(path: &str) -> Result<bool> {
117 let o = run_findmnt(&["-N"], None, Some("1"))?;
118
119 let mounted = o.filesystems.iter().any(|fs| is_source_mounted(path, fs));
120
121 Ok(mounted)
122}
123
124pub fn is_source_mounted(path: &str, mounted_fs: &Filesystem) -> bool {
126 if mounted_fs.source.contains(path) {
127 return true;
128 }
129
130 if let Some(ref children) = mounted_fs.children {
131 for child in children {
132 if is_source_mounted(path, child) {
133 return true;
134 }
135 }
136 }
137
138 false
139}
140
141pub fn mount(dev: &str, target: &Utf8Path) -> Result<()> {
143 Command::new("mount")
144 .args([dev, target.as_str()])
145 .run_inherited_with_cmd_context()
146}
147
148pub fn mount_typed(dev: &str, fstype: &str, target: &Utf8Path) -> Result<()> {
156 Command::new("mount")
157 .args(["-t", fstype, dev, target.as_str()])
158 .run_inherited_with_cmd_context()
159}
160
161#[context("Comparing filesystems at {path} and /proc/1/root/{path}")]
166pub fn is_same_as_host(path: &Utf8Path) -> Result<bool> {
167 let path = Utf8Path::new("/").join(path);
169
170 let devstat = rustix::fs::statvfs(path.as_std_path())?;
173 let hostpath = Utf8Path::new("/proc/1/root").join(path.strip_prefix("/")?);
174 let hostdevstat = rustix::fs::statvfs(hostpath.as_std_path())?;
175 tracing::trace!(
176 "base mount id {:?}, host mount id {:?}",
177 devstat.f_fsid,
178 hostdevstat.f_fsid
179 );
180 Ok(devstat.f_fsid == hostdevstat.f_fsid)
181}
182
183#[allow(unsafe_code)]
186#[context("Opening mount tree from pid")]
187pub fn open_tree_from_pidns(
188 pid: rustix::process::Pid,
189 path: &Utf8Path,
190 recursive: bool,
191) -> Result<OwnedFd> {
192 let (sock_parent, sock_child) = rustix::net::socketpair(
194 AddressFamily::UNIX,
195 SocketType::STREAM,
196 SocketFlags::CLOEXEC,
197 None,
198 )
199 .context("socketpair")?;
200 const DUMMY_DATA: &[u8] = b"!";
201 match unsafe { libc::fork() } {
202 0 => {
203 drop(sock_parent);
207
208 let pidlink = fs::File::open(format!("/proc/{}/ns/mnt", pid.as_raw_nonzero()))?;
210 rustix::thread::move_into_link_name_space(
211 pidlink.as_fd(),
212 Some(rustix::thread::LinkNameSpaceType::Mount),
213 )
214 .context("setns")?;
215
216 let recursive = if recursive {
218 OpenTreeFlags::AT_RECURSIVE
219 } else {
220 OpenTreeFlags::empty()
221 };
222 let fd = rustix::mount::open_tree(
223 rustix::fs::CWD,
224 path.as_std_path(),
225 OpenTreeFlags::OPEN_TREE_CLOEXEC | OpenTreeFlags::OPEN_TREE_CLONE | recursive,
226 )
227 .context("open_tree")?;
228
229 let fd = fd.as_fd();
231 let fds = [fd];
232 let mut buffer = [MaybeUninit::uninit(); rustix::cmsg_space!(ScmRights(1))];
233 let mut control = SendAncillaryBuffer::new(&mut buffer);
234 let pushed = control.push(SendAncillaryMessage::ScmRights(&fds));
235 assert!(pushed);
236 let ios = std::io::IoSlice::new(DUMMY_DATA);
237 rustix::net::sendmsg(sock_child, &[ios], &mut control, SendFlags::empty())?;
238 std::process::exit(0)
240 }
241 -1 => {
242 let e = std::io::Error::last_os_error();
244 anyhow::bail!("failed to fork: {e}");
245 }
246 n => {
247 let pid = rustix::process::Pid::from_raw(n).unwrap();
249 drop(sock_child);
250 let mut cmsg_space = vec![MaybeUninit::uninit(); rustix::cmsg_space!(ScmRights(1))];
252 let mut cmsg_buffer = rustix::net::RecvAncillaryBuffer::new(&mut cmsg_space);
253 let mut buf = [0u8; DUMMY_DATA.len()];
254 let iov = std::io::IoSliceMut::new(buf.as_mut());
255 let mut iov = [iov];
256 let nread = rustix::net::recvmsg(
257 sock_parent,
258 &mut iov,
259 &mut cmsg_buffer,
260 RecvFlags::CMSG_CLOEXEC,
261 )
262 .context("recvmsg")?
263 .bytes;
264 anyhow::ensure!(nread == DUMMY_DATA.len());
265 assert_eq!(buf, DUMMY_DATA);
266 let r = cmsg_buffer
268 .drain()
269 .filter_map(|m| match m {
270 rustix::net::RecvAncillaryMessage::ScmRights(f) => Some(f),
271 _ => None,
272 })
273 .flatten()
274 .next()
275 .ok_or_else(|| anyhow::anyhow!("Did not receive a file descriptor"))?;
276 let st = rustix::process::waitpid(Some(pid), WaitOptions::empty())?
278 .expect("Wait status")
279 .1;
280 if let Some(0) = st.exit_status() {
281 Ok(r)
282 } else {
283 anyhow::bail!("forked helper failed: {st:?}");
284 }
285 }
286 }
287}
288
289pub fn bind_mount_from_pidns(
292 pid: Pid,
293 src: &Utf8Path,
294 target: &Utf8Path,
295 recursive: bool,
296) -> Result<()> {
297 let src = open_tree_from_pidns(pid, src, recursive)?;
298 rustix::mount::move_mount(
299 src.as_fd(),
300 "",
301 rustix::fs::CWD,
302 target.as_std_path(),
303 MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH,
304 )
305 .context("Moving mount")?;
306 Ok(())
307}
308
309pub fn ensure_mirrored_host_mount(path: impl AsRef<Utf8Path>) -> Result<()> {
312 let path = path.as_ref();
313 std::fs::create_dir_all(path)?;
316 if is_same_as_host(path)? {
317 tracing::debug!("Already mounted from host: {path}");
318 return Ok(());
319 }
320 tracing::debug!("Propagating host mount: {path}");
321 bind_mount_from_pidns(PID1, path, path, true)
322}