summaryrefslogtreecommitdiff
path: root/init/src/mounts/rescue.rs
diff options
context:
space:
mode:
Diffstat (limited to 'init/src/mounts/rescue.rs')
-rw-r--r--init/src/mounts/rescue.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/init/src/mounts/rescue.rs b/init/src/mounts/rescue.rs
new file mode 100644
index 0000000..ba058f4
--- /dev/null
+++ b/init/src/mounts/rescue.rs
@@ -0,0 +1,50 @@
+use libc::{self};
+use std::ffi::CString;
+use std::fs::create_dir;
+
+
+
+
+pub fn mount_system() -> Result<(), Box<dyn std::error::Error>> {
+ let mounts: &[(&str, &str, Option<&str>)] = &[
+ ("/proc", "proc", None),
+ ("/sys", "sysfs", None),
+ ("/dev", "devtmpfs", Some("devtmpfs")),
+ ("/tmp", "tmpfs", None),
+ ("/run", "tmpfs", None),
+ ];
+
+ unsafe {
+ for &(target, fstype, source) in mounts {
+ let target_c = CString::new(target)?;
+ let fstype_c = CString::new(fstype)?;
+ let source_c = source.map(|s| CString::new(s).unwrap());
+
+ let _ = create_dir(target);
+
+ let source_ptr = source_c.as_ref().map_or(std::ptr::null(), |s| s.as_ptr());
+
+ let ret = libc::syscall(
+ libc::SYS_mount,
+ source_ptr,
+ target_c.as_ptr(),
+ fstype_c.as_ptr(),
+ 0 as libc::c_ulong,
+ std::ptr::null::<libc::c_void>(),
+ );
+
+ if ret != 0 {
+ let errno = errno::errno().0;
+ return Err(format!(
+ "Failed to mount {}: {}",
+ target,
+ std::io::Error::from_raw_os_error(errno)
+ )
+ .into());
+ }
+ println!("\x1b[32m * \x1b[0m Mounting {}...", target );
+ }
+ }
+
+ Ok(())
+}