로깅 메시지 정리

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_rules! 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) => {
{
#[allow(unused_must_use)]

View File

@ -7,25 +7,21 @@ mod link;
mod mem;
mod tm;
mod unix;
use alloc::format;
use alloc::string::String;
use errno::Errno;
use kmsg::init_kmsg;
use libc::{
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,
};
use unix::{
do_chdir, do_execv, do_gettime, do_mkdir, do_mount, do_pivot_root, do_rmdir, do_umount,
};
use unix::{do_chdir, do_execv, 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) {
Err(e) => {
kprintln!("remount \"/\" with private option failed: {}", e);
return Err(());
}
Ok(_) => kprintln!("\"/\" remounted"),
Err(e) => return Err(format!("remount \"/\" with private option failed: {}", e)),
Ok(_) => (),
};
match do_mount(
@ -35,11 +31,8 @@ fn replace_root() -> Result<(), ()> {
MS_LAZYTIME | MS_NODEV | MS_NOSUID | MS_RELATIME,
None,
) {
Err(e) => {
kprintln!("mount \"/run\" failed: {}", e);
return Err(());
}
Ok(_) => kprintln!("\"/run\" mounted"),
Err(e) => return Err(format!("mount \"/run\" failed: {}", e)),
Ok(_) => (),
};
let ovr_dir_structures = [
@ -52,11 +45,8 @@ fn replace_root() -> Result<(), ()> {
for (path, mode) in ovr_dir_structures {
match do_mkdir(path, mode) {
Err(e) => {
kprintln!("mkdir \"{}\" failed: {}", path, e);
return Err(());
}
Ok(_) => kprintln!("\"{}\" created", path),
Err(e) => return Err(format!("mkdir \"{}\" failed: {}", path, e)),
Ok(_) => (),
}
}
@ -67,47 +57,38 @@ fn replace_root() -> Result<(), ()> {
MS_BIND | MS_RDONLY,
None,
) {
Err(e) => {
kprintln!("mount \"/run/overlay/lower\" failed: {}", e);
return Err(());
}
Ok(_) => kprintln!("\"/run/overlay/lower\" mounted"),
Err(e) => return Err(format!("mount \"/run/overlay/lower\" failed: {}", e)),
Ok(_) => (),
};
match do_mount(
Some("rootfs"),
Some("/run/overlay/merged"),
Some("overlay"), MS_RELATIME|MS_LAZYTIME,
Some("/run/overlay/merged"),
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"),
) {
Err(e) => {kprintln!("mount \"/run/overlay/merged\" failed: {}", e);
return Err(());
},
Ok(_) => kprintln!("\"/run/overlay/merged\" mounted"),
};
Err(e) =>return Err(format!("mount \"/run/overlay/merged\" failed: {}", e)),
Ok(_) => (),
};
match do_mkdir("/run/overlay/merged/oldroot", 0o0700) {
Err(e) => {
kprintln!("mkdir \"/run/overlay/merged/oldroot\" failed: {}", e);
return Err(());
return Err(format!(
"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") {
Err(e) => {
kprintln!("pivot_root failed: {}", e);
return Err(());
}
Ok(_) => kprintln!("pivot_root done"),
Err(e) => return Err(format!("pivot_root failed: {}", e)),
Ok(_) => (),
}
match do_chdir("/") {
Err(e) => {
kprintln!("chdir \"/\" failed: {}", e);
return Err(());
}
Ok(_) => kprintln!("chdir \"/\" done"),
Err(e) => return Err(format!("chdir \"/\" failed: {}", e)),
Ok(_) => (),
}
let move_mnt_pairs = [
@ -120,30 +101,21 @@ fn replace_root() -> Result<(), ()> {
required,
) {
(Err(Errno(EINVAL)), true) => (),
(Err(e), _) => {
kprintln!("moving mountpoint \"{}\" failed: {}", src, e);
return Err(());
}
(Ok(_), _) => kprintln!("mountpoint \"{}\" moved", src),
(Err(e), _) => return Err(format!("moving mountpoint \"{}\" failed: {}", src, e)),
(Ok(_), _) => (),
};
}
match do_umount("/oldroot", MNT_DETACH) {
Err(e) => {
kprintln!("umount mountpoint \"/oldroot\" failed: {}", e);
return Err(());
}
Ok(_) => kprintln!("mountpoint \"/oldroot/run\" unmounted"),
Err(e) => return Err(format!("umount mountpoint \"/oldroot\" failed: {}", e)),
Ok(_) => (),
};
let unused_dirs = ["/oldroot", "/run/overlay/merged"];
for path in unused_dirs {
match do_rmdir(path) {
Err(e) => {
kprintln!("remove \"{}\" failed: {}", path, e);
return Err(());
}
Ok(_) => kprintln!("\"{}\" removed", path),
Err(e) => return Err(format!("remove \"{}\" failed: {}", path, e)),
Ok(_) => (),
}
}
@ -153,30 +125,14 @@ fn replace_root() -> Result<(), ()> {
#[no_mangle]
unsafe extern "C" fn main(_argc: c_int, argv: *mut *const u8) -> c_int {
init_kmsg();
let start = match do_gettime() {
Err(err) => {
kprintln!("{}", err);
return EXIT_FAILURE;
}
Ok(v) => v,
};
match replace_root() {
Err(_) => return EXIT_FAILURE,
Ok(_) => kprintln!("rootfs replaced with overlayfs!"),
};
let end = match do_gettime() {
Err(err) => {
kprintln!("{}", err);
kprintln!("failed to replace rootfs: {}", err);
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"];
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) => {
kprintln!("execution \"{}\" failed: {}", init_path, e);
break;
return EXIT_FAILURE;
}
Ok(()) => return EXIT_SUCCESS,
};

View File

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