From 3a5c327a546ff1838e4dc32b8b67a056c1b95f3d Mon Sep 17 00:00:00 2001 From: namilsk Date: Thu, 8 Jan 2026 18:41:34 +0300 Subject: Implementing main functions of init, check TODO.md --- init/src/mounts/fstab.rs | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) (limited to 'init/src/mounts/fstab.rs') diff --git a/init/src/mounts/fstab.rs b/init/src/mounts/fstab.rs index 3ea62e5..c0cfd58 100644 --- a/init/src/mounts/fstab.rs +++ b/init/src/mounts/fstab.rs @@ -1,6 +1,7 @@ -use libc::{self}; +use crate::log::{log_critical_error, log_warning, log_success}; use std::ffi::CString; -use std::{fs, fmt}; +use std::{fmt, fs}; +use libc::syscall; #[derive(Debug)] pub struct FstabEntry { @@ -16,7 +17,7 @@ impl fmt::Display for FstabEntry { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "{} {} {} {} {} {}", + "Source: {}\n Mountpoint: {}\n FSType: {}\n Options: {}\n Dump: {}\n Pass: {}\n", self.source, self.mountpoint, self.fstype, self.options, self.dump, self.pass ) } @@ -41,13 +42,13 @@ impl FstabEntry { let fstype = parts[2].to_string(); let options = parts[3].to_string(); - let dump = parts[4].parse::().map_err(|e| { - format!("Failed to parse dump field '{}': {}", parts[4], e) - })?; + let dump = parts[4] + .parse::() + .map_err(|e| format!("Failed to parse dump field '{}': {}", parts[4], e))?; - let pass = parts[5].parse::().map_err(|e| { - format!("Failed to parse pass field '{}': {}", parts[5], e) - })?; + let pass = parts[5] + .parse::() + .map_err(|e| format!("Failed to parse pass field '{}': {}", parts[5], e))?; Ok(FstabEntry { source, @@ -67,7 +68,7 @@ impl FstabEntry { match Self::parse_line(line) { Ok(entry) => entries.push(entry), Err(_) => { - println!("\x1b[32m * \x1b[0m Warning: Skipping invalid fstab line: {}", line); + log_warning(&format!("Skipping invalid fstab line: {}", line)); continue; } } @@ -113,11 +114,17 @@ impl FstabEntry { Ok((flags, data)) } + pub fn mount(&self) -> Result<(), Box> { - println!("\x1b[32m * \x1b[0m Started mounting {} from {}", self.mountpoint, self.source); - + log_success(&format!("Started mounting {} from {}", self.mountpoint, self.source)); + if self.fstype == "swap" { - println!("\x1b[30m * \x1b[0m Skipping swap: {}", self.source); + log_success(&format!("Filesystem type contains swap, upping it: {}", self.source)); + + unsafe { + syscall(libc::SYS_swapon, &self.source, 0); + } + return Ok(()); } @@ -134,14 +141,14 @@ impl FstabEntry { let source_c = CString::new(&*self.source)?; let target_c = CString::new(&*self.mountpoint)?; let fstype_c = CString::new(&*self.fstype)?; - let data_c = data.map(|s| CString::new(s).expect("\x1b[31m * \x1b[0m Something went wrong while mounting partitions. You are going to rescue mode...")); + let data_c = data.map(|s| CString::new(s)).transpose()?; let data_ptr = data_c .as_ref() .map_or(std::ptr::null(), |s| s.as_ptr() as *const libc::c_void); let ret = unsafe { - libc::syscall( + syscall( libc::SYS_mount, source_c.as_ptr(), target_c.as_ptr(), @@ -152,15 +159,14 @@ impl FstabEntry { }; if ret != 0 { - eprintln!( - "\x1b[31m * \x1b[0m Failed to mount {}: {}", + log_critical_error(&format!( + "Failed to mount {}: {}", self.mountpoint, std::io::Error::last_os_error() - ); + )); } else { - println!("\x1b[32m * \x1b[0m Mounted {}", self.mountpoint); + log_success(&format!("Mounted {}", self.mountpoint)); } Ok(()) } - -} \ No newline at end of file +} -- cgit v1.2.3