123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- use lazy_static::lazy_static;
- use crate::{cfg, env, mount};
- use crate::log::{logger};
- use std::sync::atomic::{AtomicBool, Ordering};
- use nix::mount::MsFlags;
- use crate::op::makesure::mkdir_p;
- use crate::swap::swap_on;
- lazy_static!{
- static ref PREPARE_OK_SYSFS: AtomicBool = AtomicBool::new(false);
- static ref PREPARE_OK_PROC: AtomicBool = AtomicBool::new(false);
- static ref PREPARE_OK_BOOTFS: AtomicBool = AtomicBool::new(false);
- static ref PREPARE_OK_TMP_DEVTMPFS: AtomicBool = AtomicBool::new(false);
- static ref PREPARE_OK_DEVTMPFS: AtomicBool = AtomicBool::new(false);
- static ref PREPARE_OK_DEVPTS: AtomicBool = AtomicBool::new(false);
- static ref PREPARE_OK_RUNFS: AtomicBool = AtomicBool::new(false);
- static ref PREPARE_OK_MOVE_SYSFS: AtomicBool = AtomicBool::new(false);
- static ref PREPARE_OK_MOVE_PROC: AtomicBool = AtomicBool::new(false);
- static ref PREPARE_OK_MOVE_BOOTFS: AtomicBool = AtomicBool::new(false);
- static ref PREPARE_OK_MOVE_TMP_DEVTMPFS: AtomicBool = AtomicBool::new(false);
- static ref PREPARE_OK_MOVE_RUNFS: AtomicBool = AtomicBool::new(false);
- static ref PREPARE_OK_SWAPON: AtomicBool = AtomicBool::new(false);
- }
- pub fn mount_sysfs() {
- if PREPARE_OK_SYSFS.load(Ordering::Relaxed) {
- logger().warn("mount", "The sysfs is already mounted and cannot be repeated.");
- return;
- }
- match mount::mount_sysfs() {
- Ok(_) => {
- logger().info("mount", "sysfs mounted at '/sys'.");
- }
- Err(err) => {
- logger().fatal("mount", format!("mount sysfs error: {:?}", err).as_str());
- }
- }
- PREPARE_OK_SYSFS.store(true, Ordering::Relaxed);
- }
- pub fn mount_proc() {
- if PREPARE_OK_PROC.load(Ordering::Relaxed) {
- logger().warn("mount", "The proc is already mounted and cannot be repeated.");
- return;
- }
- match mount::mount_proc() {
- Ok(_) => {
- logger().info("mount", "sysfs mounted at '/sys'.");
- }
- Err(err) => {
- logger().fatal("mount", format!("mount sysfs error: {:?}", err).as_str());
- }
- }
- PREPARE_OK_PROC.store(true, Ordering::Relaxed);
- }
- pub fn mount_bootfs() {
- if PREPARE_OK_BOOTFS.load(Ordering::Relaxed) {
- logger().warn("mount", "The '/boot' is already mounted and cannot be repeated.");
- return;
- }
- let bootdev = env::get_bootdev();
- let bootfs = env::get_bootfs();
- logger().info("mount", format!("boot_device={}, fstype={}", bootdev, bootfs).as_str());
- match mount::mount_custom(Some(bootdev.as_str()), "/boot", Some(bootfs.as_str()), None, MsFlags::MS_RDONLY) {
- Ok(_) => {
- logger().info("mount", format!("dev '{}' ({}) mounted at '/boot'", bootdev, bootfs).as_str());
- }
- Err(err) => {
- logger().fatal("mount", format!("mount '/boot' error: {:?}", err).as_str());
- }
- }
- PREPARE_OK_BOOTFS.store(true, Ordering::Relaxed);
- }
- pub fn mount_temporary_devtmpfs() {
- if PREPARE_OK_TMP_DEVTMPFS.load(Ordering::Relaxed) {
- logger().warn("mount", "The devtmpfs for initrd temporary usage is already mounted and cannot be repeated.");
- return;
- }
- match mount::mount_devtmpfs("/dev","1M", "0755") {
- Ok(_) => {
- logger().info("mount", "devtmpfs mounted at '/dev'.");
- }
- Err(err) => {
- logger().fatal("mount", format!("mount devtmpfs for initrd temporary usage error: {:?}", err).as_str());
- }
- }
- PREPARE_OK_TMP_DEVTMPFS.store(true, Ordering::Relaxed);
- }
- pub fn mount_runfs(opt: &str) {
- if PREPARE_OK_RUNFS.load(Ordering::Relaxed) {
- logger().warn("mount", "The '/run' is already mounted and cannot be repeated.");
- return;
- }
- match mount::mount_runfs(opt) {
- Ok(_) => {
- logger().info("mount", "'/run' mounted.");
- }
- Err(err) => {
- logger().fatal("mount", format!("mount '/run' error: {:?}", err).as_str());
- }
- }
- PREPARE_OK_RUNFS.store(true, Ordering::Relaxed);
- }
- pub fn mount_runfs_from_cfg() {
- let opt = cfg::CFG.get().get_cfg().get_runfs_option();
- mount_runfs(opt.as_str())
- }
- pub fn move_sysfs(target: &str) {
- if PREPARE_OK_MOVE_SYSFS.load(Ordering::Relaxed) {
- logger().warn("mount", "The sysfs is already moved and cannot be repeated.");
- return;
- }
- mkdir_p(target);
- match mount::move_mount("/sys", target) {
- Ok(_) => {
- logger().info("mount", format!("sysfs mount point moved to '{}'.", target).as_str());
- }
- Err(err) => {
- logger().fatal("mount", format!("move sysfs mount error: {:?}", err).as_str());
- }
- }
- PREPARE_OK_MOVE_SYSFS.store(true, Ordering::Relaxed);
- }
- pub fn move_proc(target: &str) {
- if PREPARE_OK_MOVE_PROC.load(Ordering::Relaxed) {
- logger().warn("mount", "The proc is already moved and cannot be repeated.");
- return;
- }
- mkdir_p(target);
- match mount::move_mount("/proc", target) {
- Ok(_) => {
- logger().info("mount", format!("proc mount point moved to '{}'.", target).as_str());
- }
- Err(err) => {
- logger().fatal("mount", format!("move proc mount error: {:?}", err).as_str());
- }
- }
- PREPARE_OK_MOVE_PROC.store(true, Ordering::Relaxed);
- }
- pub fn move_bootfs(target: &str) {
- if PREPARE_OK_MOVE_BOOTFS.load(Ordering::Relaxed) {
- logger().warn("mount", "The '/boot' is already moved and cannot be repeated.");
- return;
- }
- mkdir_p(target);
- match mount::move_mount("/boot", target) {
- Ok(_) => {
- logger().info("mount", format!("'/boot' mount point moved to '{}'.", target).as_str());
- }
- Err(err) => {
- logger().fatal("mount", format!("move '/boot' mount error: {:?}", err).as_str());
- }
- }
- PREPARE_OK_MOVE_BOOTFS.store(true, Ordering::Relaxed);
- }
- pub fn move_temporary_devtmpfs(newroot: &str) {
- if PREPARE_OK_MOVE_TMP_DEVTMPFS.load(Ordering::Relaxed) {
- logger().warn("mount", "The temporary devtmpfs for initrd is already moved and cannot be repeated.");
- return;
- }
- if !PREPARE_OK_TMP_DEVTMPFS.load(Ordering::Relaxed) {
- logger().warn("mount", "The temporary devtmpfs for initrd has been never mounted.");
- return;
- }
- let move_target = format!("{}/mnt/old-dev", newroot);
- mkdir_p(move_target.as_str());
- match mount::move_mount("/dev", move_target.as_str()) {
- Ok(_) => {
- logger().info("mount", format!("'/dev' mount point moved to '{}'.", move_target).as_str());
- PREPARE_OK_MOVE_TMP_DEVTMPFS.store(true, Ordering::Relaxed);
- }
- Err(err) => {
- logger().warn("mount", format!("move '/dev' mount error: {:?}", err).as_str());
- }
- }
- }
- pub fn move_run(target: &str) {
- if PREPARE_OK_MOVE_RUNFS.load(Ordering::Relaxed) {
- logger().warn("mount", "The '/run' is already moved and cannot be repeated.");
- return;
- }
- mkdir_p(target);
- match mount::move_mount("/run", target) {
- Ok(_) => {
- logger().info("mount", format!("'/run' mount point moved to '{}'.", target).as_str());
- }
- Err(err) => {
- logger().fatal("mount", format!("move '/run' mount error: {:?}", err).as_str());
- }
- }
- PREPARE_OK_MOVE_RUNFS.store(true, Ordering::Relaxed);
- }
- pub fn turn_on_swap() {
- if PREPARE_OK_SWAPON.load(Ordering::Relaxed) {
- logger().warn("swap", "swap is already turned on and cannot be repeated.");
- return;
- }
- let swap_dev = cfg::CFG.get().get_cfg().get_swap_dev();
- match swap_dev {
- None => {
- logger().info("swap", "no swap specified in config file. ignored.")
- }
- Some(sw) => {
- logger().info("swap", format!("turn on swap on '{}'...", sw.as_str()).as_str());
- swap_on(sw.as_str());
- PREPARE_OK_SWAPON.store(true, Ordering::Relaxed);
- }
- }
- }
|