diff options
| author | namilsk <namilsk@namilsk.tech> | 2026-01-08 18:41:34 +0300 |
|---|---|---|
| committer | namilsk <namilsk@namilsk.tech> | 2026-01-08 18:41:34 +0300 |
| commit | 3a5c327a546ff1838e4dc32b8b67a056c1b95f3d (patch) | |
| tree | 2aac7d8b53dda2104a926b1fa53129db78dd410d /init/src/mounts | |
| parent | 2cad2077b647770aac103360cbd28b29c513db6c (diff) | |
Implementing main functions of init, check TODO.md
Diffstat (limited to 'init/src/mounts')
| -rw-r--r-- | init/src/mounts/fstab.rs | 48 | ||||
| -rw-r--r-- | init/src/mounts/rescue.rs | 3 |
2 files changed, 29 insertions, 22 deletions
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::<u32>().map_err(|e| { - format!("Failed to parse dump field '{}': {}", parts[4], e) - })?; + let dump = parts[4] + .parse::<u32>() + .map_err(|e| format!("Failed to parse dump field '{}': {}", parts[4], e))?; - let pass = parts[5].parse::<u32>().map_err(|e| { - format!("Failed to parse pass field '{}': {}", parts[5], e) - })?; + let pass = parts[5] + .parse::<u32>() + .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<dyn std::error::Error>> { - 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 +} diff --git a/init/src/mounts/rescue.rs b/init/src/mounts/rescue.rs index 84517ed..ad80a84 100644 --- a/init/src/mounts/rescue.rs +++ b/init/src/mounts/rescue.rs @@ -1,3 +1,4 @@ +use crate::log::log_success; use std::ffi::CString; use std::fs::create_dir; @@ -38,7 +39,7 @@ pub fn mount_system() -> Result<(), Box<dyn std::error::Error>> { ) .into()); } - println!("\x1b[32m * \x1b[0m Mounting {}...", target); + log_success(&format!("Mounting {}...", target)); } } |
