로깅 메시지 정리

This commit is contained in:
Sangbum Kim 2024-03-10 02:21:43 +09:00
parent af8bbab8bf
commit 69c5eacda7
3 changed files with 59 additions and 88 deletions

View File

@ -26,6 +26,21 @@ pub(crate) fn init_kmsg() {
#[macro_export] #[macro_export]
macro_rules! kprintln { macro_rules! kprintln {
() => { $crate::kprintln!("") }; () => { $crate::kprintln!("") };
($fmt: expr) => {
{
#[allow(unused_must_use)]
unsafe {
let mut stm = libc_print::__LibCWriter::new($crate::kmsg::LOG_FD);
let buf=alloc::format!("init-wrapper: {}",$fmt);
for line in buf.lines(){
stm.write_str(&line);
}
if (!$crate::kmsg::LOG_FD_KMSG) {
stm.write_nl();
}
}
}
};
($fmt: tt) => { ($fmt: tt) => {
{ {
#[allow(unused_must_use)] #[allow(unused_must_use)]

View File

@ -7,25 +7,21 @@ mod link;
mod mem; mod mem;
mod tm; mod tm;
mod unix; mod unix;
use alloc::format;
use alloc::string::String;
use errno::Errno; use errno::Errno;
use kmsg::init_kmsg; use kmsg::init_kmsg;
use libc::{ use libc::{
c_int, EACCES, EINVAL, ENOENT, ENOEXEC, ENOTDIR, EXIT_FAILURE, EXIT_SUCCESS, MNT_DETACH, c_int, EACCES, EINVAL, ENOENT, ENOEXEC, ENOTDIR, EXIT_FAILURE, EXIT_SUCCESS, MNT_DETACH,
MS_BIND, MS_LAZYTIME, MS_MOVE, MS_NODEV, MS_NOSUID, MS_PRIVATE, MS_RDONLY, MS_REC, MS_RELATIME, MS_BIND, MS_LAZYTIME, MS_MOVE, MS_NODEV, MS_NOSUID, MS_PRIVATE, MS_RDONLY, MS_REC, MS_RELATIME,
}; };
use unix::{ use unix::{do_chdir, do_execv, do_mkdir, do_mount, do_pivot_root, do_rmdir, do_umount};
do_chdir, do_execv, do_gettime, do_mkdir, do_mount, do_pivot_root, do_rmdir, do_umount,
};
fn replace_root() -> Result<(), ()> { fn replace_root() -> Result<(), String> {
match do_mount(None, Some("/"), None, MS_REC | MS_PRIVATE, None) { match do_mount(None, Some("/"), None, MS_REC | MS_PRIVATE, None) {
Err(e) => { Err(e) => return Err(format!("remount \"/\" with private option failed: {}", e)),
kprintln!("remount \"/\" with private option failed: {}", e); Ok(_) => (),
return Err(());
}
Ok(_) => kprintln!("\"/\" remounted"),
}; };
match do_mount( match do_mount(
@ -35,11 +31,8 @@ fn replace_root() -> Result<(), ()> {
MS_LAZYTIME | MS_NODEV | MS_NOSUID | MS_RELATIME, MS_LAZYTIME | MS_NODEV | MS_NOSUID | MS_RELATIME,
None, None,
) { ) {
Err(e) => { Err(e) => return Err(format!("mount \"/run\" failed: {}", e)),
kprintln!("mount \"/run\" failed: {}", e); Ok(_) => (),
return Err(());
}
Ok(_) => kprintln!("\"/run\" mounted"),
}; };
let ovr_dir_structures = [ let ovr_dir_structures = [
@ -52,11 +45,8 @@ fn replace_root() -> Result<(), ()> {
for (path, mode) in ovr_dir_structures { for (path, mode) in ovr_dir_structures {
match do_mkdir(path, mode) { match do_mkdir(path, mode) {
Err(e) => { Err(e) => return Err(format!("mkdir \"{}\" failed: {}", path, e)),
kprintln!("mkdir \"{}\" failed: {}", path, e); Ok(_) => (),
return Err(());
}
Ok(_) => kprintln!("\"{}\" created", path),
} }
} }
@ -67,11 +57,8 @@ fn replace_root() -> Result<(), ()> {
MS_BIND | MS_RDONLY, MS_BIND | MS_RDONLY,
None, None,
) { ) {
Err(e) => { Err(e) => return Err(format!("mount \"/run/overlay/lower\" failed: {}", e)),
kprintln!("mount \"/run/overlay/lower\" failed: {}", e); Ok(_) => (),
return Err(());
}
Ok(_) => kprintln!("\"/run/overlay/lower\" mounted"),
}; };
match do_mount( match do_mount(
@ -80,34 +67,28 @@ fn replace_root() -> Result<(), ()> {
Some("overlay"), MS_RELATIME|MS_LAZYTIME, Some("overlay"), MS_RELATIME|MS_LAZYTIME,
Some("lowerdir=/run/overlay/lower,upperdir=/run/overlay/upper,workdir=/run/overlay/work,redirect_dir=on,uuid=on,metacopy=on,volatile"), Some("lowerdir=/run/overlay/lower,upperdir=/run/overlay/upper,workdir=/run/overlay/work,redirect_dir=on,uuid=on,metacopy=on,volatile"),
) { ) {
Err(e) => {kprintln!("mount \"/run/overlay/merged\" failed: {}", e); Err(e) =>return Err(format!("mount \"/run/overlay/merged\" failed: {}", e)),
return Err(()); Ok(_) => (),
},
Ok(_) => kprintln!("\"/run/overlay/merged\" mounted"),
}; };
match do_mkdir("/run/overlay/merged/oldroot", 0o0700) { match do_mkdir("/run/overlay/merged/oldroot", 0o0700) {
Err(e) => { Err(e) => {
kprintln!("mkdir \"/run/overlay/merged/oldroot\" failed: {}", e); return Err(format!(
return Err(()); "mkdir \"/run/overlay/merged/oldroot\" failed: {}",
e
))
} }
Ok(_) => kprintln!("\"/run/overlay/merged/oldroot\" created"), Ok(_) => (),
} }
match do_pivot_root("/run/overlay/merged", "/run/overlay/merged/oldroot") { match do_pivot_root("/run/overlay/merged", "/run/overlay/merged/oldroot") {
Err(e) => { Err(e) => return Err(format!("pivot_root failed: {}", e)),
kprintln!("pivot_root failed: {}", e); Ok(_) => (),
return Err(());
}
Ok(_) => kprintln!("pivot_root done"),
} }
match do_chdir("/") { match do_chdir("/") {
Err(e) => { Err(e) => return Err(format!("chdir \"/\" failed: {}", e)),
kprintln!("chdir \"/\" failed: {}", e); Ok(_) => (),
return Err(());
}
Ok(_) => kprintln!("chdir \"/\" done"),
} }
let move_mnt_pairs = [ let move_mnt_pairs = [
@ -120,30 +101,21 @@ fn replace_root() -> Result<(), ()> {
required, required,
) { ) {
(Err(Errno(EINVAL)), true) => (), (Err(Errno(EINVAL)), true) => (),
(Err(e), _) => { (Err(e), _) => return Err(format!("moving mountpoint \"{}\" failed: {}", src, e)),
kprintln!("moving mountpoint \"{}\" failed: {}", src, e); (Ok(_), _) => (),
return Err(());
}
(Ok(_), _) => kprintln!("mountpoint \"{}\" moved", src),
}; };
} }
match do_umount("/oldroot", MNT_DETACH) { match do_umount("/oldroot", MNT_DETACH) {
Err(e) => { Err(e) => return Err(format!("umount mountpoint \"/oldroot\" failed: {}", e)),
kprintln!("umount mountpoint \"/oldroot\" failed: {}", e); Ok(_) => (),
return Err(());
}
Ok(_) => kprintln!("mountpoint \"/oldroot/run\" unmounted"),
}; };
let unused_dirs = ["/oldroot", "/run/overlay/merged"]; let unused_dirs = ["/oldroot", "/run/overlay/merged"];
for path in unused_dirs { for path in unused_dirs {
match do_rmdir(path) { match do_rmdir(path) {
Err(e) => { Err(e) => return Err(format!("remove \"{}\" failed: {}", path, e)),
kprintln!("remove \"{}\" failed: {}", path, e); Ok(_) => (),
return Err(());
}
Ok(_) => kprintln!("\"{}\" removed", path),
} }
} }
@ -153,30 +125,14 @@ fn replace_root() -> Result<(), ()> {
#[no_mangle] #[no_mangle]
unsafe extern "C" fn main(_argc: c_int, argv: *mut *const u8) -> c_int { unsafe extern "C" fn main(_argc: c_int, argv: *mut *const u8) -> c_int {
init_kmsg(); init_kmsg();
let start = match do_gettime() {
Err(err) => {
kprintln!("{}", err);
return EXIT_FAILURE;
}
Ok(v) => v,
};
match replace_root() { match replace_root() {
Err(_) => return EXIT_FAILURE,
Ok(_) => kprintln!("rootfs replaced with overlayfs!"),
};
let end = match do_gettime() {
Err(err) => { Err(err) => {
kprintln!("{}", err); kprintln!("failed to replace rootfs: {}", err);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
Ok(v) => v, Ok(_) => kprintln!("rootfs replaced with overlayfs!"),
}; };
let elapsed = end - start;
kprintln!("processed in {:?}", elapsed);
// overwrite arg0
let init_candidates = ["/sbin/init", "/usr/sbin/init", "/usr/lib/systemd/systemd"]; let init_candidates = ["/sbin/init", "/usr/sbin/init", "/usr/lib/systemd/systemd"];
for init_path in init_candidates { for init_path in init_candidates {
@ -187,7 +143,7 @@ unsafe extern "C" fn main(_argc: c_int, argv: *mut *const u8) -> c_int {
} }
Err(e) => { Err(e) => {
kprintln!("execution \"{}\" failed: {}", init_path, e); kprintln!("execution \"{}\" failed: {}", init_path, e);
break; return EXIT_FAILURE;
} }
Ok(()) => return EXIT_SUCCESS, Ok(()) => return EXIT_SUCCESS,
}; };

View File

@ -1,17 +1,16 @@
use crate::tm::{new_timespec, Timespec};
use alloc::ffi::CString; use alloc::ffi::CString;
use core::borrow::BorrowMut;
use core::ptr::null; use core::ptr::null;
use errno::{errno, Errno}; use errno::{errno, Errno};
use libc::{ use libc::{c_void, chdir, execv, mkdir, mode_t, mount, rmdir, syscall, umount2, SYS_pivot_root};
c_void, chdir, clock_gettime, execv, mkdir, mode_t, mount, rmdir, syscall, umount2,
SYS_pivot_root, CLOCK_BOOTTIME,
};
#[cfg(test)]
use crate::tm::{new_timespec, Timespec};
#[cfg(test)]
use core::borrow::BorrowMut;
#[cfg(test)] #[cfg(test)]
use libc::{ use libc::{
c_uint, makedev, mkdir, mknod, mode_t, mount, readlinkat, rmdir, strlen, syscall, umount2, c_uint, clock_gettime, makedev, mkdir, mknod, mode_t, mount, readlinkat, rmdir, strlen,
unlink, SYS_pivot_root, AT_FDCWD, CLOCK_BOOTTIME, S_IFCHR, syscall, umount2, unlink, SYS_pivot_root, AT_FDCWD, CLOCK_BOOTTIME, CLOCK_BOOTTIME, S_IFCHR,
}; };
pub(crate) type SystemResult = Result<(), Errno>; pub(crate) type SystemResult = Result<(), Errno>;
@ -74,7 +73,7 @@ pub(crate) fn do_umount(path: &str, flags: i32) -> SystemResult {
} }
#[cfg(test)] #[cfg(test)]
pub(crate) fn do_readlink(path: &str) -> Result<CString, Errno> { pub(crate) fn do_readlink(path: &str) -> Result<&str, Errno> {
// has ownership // has ownership
let raw_path = CString::new(path).unwrap(); let raw_path = CString::new(path).unwrap();
unsafe { unsafe {
@ -172,6 +171,7 @@ pub(crate) fn do_pivot_root(new_root: &str, put_old: &str) -> SystemResult {
} }
} }
#[cfg(test)]
pub(crate) fn do_gettime() -> Result<Timespec, Errno> { pub(crate) fn do_gettime() -> Result<Timespec, Errno> {
let mut time = new_timespec(); let mut time = new_timespec();