Skip to content

Commit

Permalink
Merge pull request #79 from hnez/motd-umount-mount
Browse files Browse the repository at this point in the history
motd: use explicit umount/mount instead of remount
  • Loading branch information
hnez committed Sep 11, 2024
2 parents a519eb3 + e0339af commit e79b017
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/motd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::path::Path;

use anyhow::Result;
use futures::FutureExt;
use nix::errno::Errno;
use nix::mount::MsFlags;

use crate::dut_power::OutputState;
Expand All @@ -17,6 +18,13 @@ mod setup {
pub(super) const VAR_RUN_TACD: &str = "demo_files/var/run/tacd";
pub(super) const ETC: &str = "demo_files/etc";

/// umount stub for demo_mode that works without root permissions
///
/// (by doing nothing).
pub(super) fn umount(_target: &std::path::Path) -> nix::Result<()> {
Err(nix::errno::Errno::EINVAL)
}

/// mount stub for demo_mode that works without root permissions
///
/// (by doing nothing).
Expand All @@ -33,7 +41,7 @@ mod setup {

#[cfg(not(feature = "demo_mode"))]
mod setup {
pub(super) use nix::mount::mount;
pub(super) use nix::mount::{mount, umount};
pub(super) const VAR_RUN_TACD: &str = "/var/run/tacd";
pub(super) const ETC: &str = "/etc";
}
Expand Down Expand Up @@ -243,7 +251,14 @@ impl Motd {
// Create the motd file in /var/run/tacd.
let runtime_motd = File::create(&path_runtime_motd)?;

// Bind (re)mount /var/run/tacd/motd to /etc/motd.
// Try to unmount the bind mount at /etc/motd before trying to set up a new one.
// Filter out the expected error for when /etc/motd is not a bind mount yet.
umount(&path_etc_motd).or_else(|err| match err {
Errno::EINVAL => Ok(()),
_ => Err(err),
})?;

// Bind mount /var/run/tacd/motd to /etc/motd.
// The benefit over writing to /etc/motd directly is that we do not
// hammer the eMMC as much.
// The benefit over a symlink is that the bind-mount does not persist
Expand All @@ -255,7 +270,7 @@ impl Motd {
Some(&path_runtime_motd),
&path_etc_motd,
None::<&str>,
MsFlags::MS_BIND | MsFlags::MS_REMOUNT,
MsFlags::MS_BIND,
None::<&str>,
)?;

Expand Down

0 comments on commit e79b017

Please sign in to comment.