diff --git a/core/sysmaster/src/main.rs b/core/sysmaster/src/main.rs index f00b502839498d6519f87956b54fd2fb1c4eefeb..0bc6b9bf81ffbc94f20317e4ebe32da5d2ec1cc1 100644 --- a/core/sysmaster/src/main.rs +++ b/core/sysmaster/src/main.rs @@ -51,7 +51,6 @@ use nix::sys::signal::{self, SaFlags, SigAction, SigHandler, SigSet, Signal}; use std::cell::RefCell; use std::convert::TryFrom; use std::env::{self}; -use std::fs; use std::os::unix::process::CommandExt; use std::path::Path; use std::process::{exit, Command}; @@ -347,13 +346,19 @@ fn remount_sysroot() { let root_path = "/"; - //check if the '/' has the write permission - if let Ok(md) = fs::metadata(root_path) { - let permissions = md.permissions(); - if !permissions.readonly() { - return; - } - }; + //check if the '/' file system has the write permission + use libc::{statvfs, ST_RDONLY}; + use std::ffi::CString; + + let c_path = CString::new(root_path).unwrap(); + + let mut statbuf: statvfs = unsafe { std::mem::zeroed() }; + + unsafe { statvfs(c_path.as_ptr(), &mut statbuf) }; + + if statbuf.f_flag & ST_RDONLY != 1 { + return; + } if let Ok(lines) = read_lines(FSTAB_PATH) { for item_raw in lines.flatten() { diff --git a/core/sysmaster/src/mount/setup.rs b/core/sysmaster/src/mount/setup.rs index 56efdac46bfa276cf57e4a26ce259232453a2dfa..f191070f535596853ef3a946ebfd9f26c33eddb7 100644 --- a/core/sysmaster/src/mount/setup.rs +++ b/core/sysmaster/src/mount/setup.rs @@ -31,7 +31,7 @@ use std::{ path::{Path, PathBuf}, }; -const EARLY_MOUNT_NUM: u8 = 3; +const EARLY_MOUNT_NUM: u8 = 4; type Callback = fn() -> bool; @@ -61,11 +61,38 @@ lazy_static! { source: String::from("devtmpfs"), target: String::from("/dev"), fs_type: String::from("devtmpfs"), - options: Some("mode=755,size=4m,nr_inodes=64K".to_string()), + options: Some("mode=755,size=4m,nr_inodes=1m".to_string()), flags: MsFlags::MS_NOSUID | MsFlags::MS_STRICTATIME, callback: None, mode: MountMode::MNT_FATAL | MountMode::MNT_IN_CONTAINER, }, + MountPoint { + source: String::from("tmpfs"), + target: String::from("/run"), + fs_type: String::from("tmpfs"), + options: Some("mode=755,size=20%,nr_inodes=800K".to_string()), + flags: MsFlags::MS_NOSUID | MsFlags::MS_NODEV | MsFlags::MS_STRICTATIME, + callback: None, + mode: MountMode::MNT_FATAL | MountMode::MNT_IN_CONTAINER, + }, + MountPoint { + source: String::from("devpts"), + target: String::from("/dev/pts"), + fs_type: String::from("devpts"), + options: Some("mode=620,gid=5".to_string()), + flags: MsFlags::MS_NOSUID | MsFlags::MS_NOEXEC, + callback: None, + mode: MountMode::MNT_IN_CONTAINER, + }, + MountPoint { + source: String::from("tmpfs"), + target: String::from("/dev/shm"), + fs_type: String::from("tmpfs"), + options: Some("mode=1777".to_string()), + flags: MsFlags::MS_NOSUID | MsFlags::MS_NOEXEC | MsFlags::MS_STRICTATIME, + callback: None, + mode: MountMode::MNT_FATAL | MountMode::MNT_IN_CONTAINER, + }, // table.push(MountPoint { // source: String::from("securityfs"), // target: String::from("/sys/kernel/security"), @@ -387,9 +414,9 @@ pub fn mount_setup_early() -> Result<()> { Ok(()) } -/// mount the point of all the mount_table +/// mount the point of all the mount_table except the early mount point pub fn mount_setup() -> Result<()> { - for table in MOUNT_TABLE.iter() { + for table in MOUNT_TABLE.iter().skip(EARLY_MOUNT_NUM as usize) { table.mount()?; } diff --git a/init/mount.rs b/init/mount.rs index 9aca221381ca73ab0d1810d69809ae58673873c9..b0842dbaf360a98711b72864fd9d25980bae396b 100644 --- a/init/mount.rs +++ b/init/mount.rs @@ -10,7 +10,7 @@ // NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. // See the Mulan PSL v2 for more details. -use nix::mount::MsFlags; +use nix::mount::{self, MntFlags, MsFlags}; use std::{fs, os::unix::fs::MetadataExt, path::Path}; pub fn setup_mount_early() { @@ -34,7 +34,7 @@ pub fn setup_mount_early() { "/dev", Some("devtmpfs"), MsFlags::MS_NOSUID | MsFlags::MS_STRICTATIME, - Some("mode=755,size=4m,nr_inodes=64K"), + Some("mode=755,size=4m,nr_inodes=1m"), ), ( Some("tmpfs"), @@ -54,12 +54,18 @@ pub fn setup_mount_early() { } } - if !is_mount_point(target) { - if let Err(e) = nix::mount::mount(source, target, fstype, flags, data) { - println!("Failed to mount {}: {}", target.display(), e); + if is_mount_point(target) { + // umount first as these filesystemd should be remount + if let Err(e) = mount::umount2(target, MntFlags::MNT_DETACH) { + println!("umount2 {} failed:{}", target.display(), e); + continue; } } + if let Err(e) = nix::mount::mount(source, target, fstype, flags, data) { + println!("Failed to mount {}: {}", target.display(), e); + } + println!( "Mounting {:?} to {:?} of type {:?} with {:?}", source, target, fstype, flags