# 最小限OS構成設計(Redox OSベース) ## Redox OS分析結果 ### 1. **最小構成の特定** #### コアコンポーネント(196MiB以下) ```toml # config/minimal.toml から抽出 [packages] base = {} # 基本システム(~20MB) base-initfs = {} # 初期ファイルシステム(~5MB) bootloader = {} # ブートローダー(~2MB) drivers = {} # 最小ドライバー(~15MB) kernel = {} # マイクロカーネル(~5MB) relibc = {} # C標準ライブラリ(~10MB) coreutils = {} # 基本コマンド(~8MB) ion = {} # シェル(~3MB) ``` #### 初期化プロセス ```bash # 00_base: 基本システム初期化 rm -r /tmp # 一時ディレクトリクリア mkdir -m a=rwxt /tmp # 一時ディレクトリ作成 ipcd # IPCデーモン ptyd # PTYデーモン sudo --daemon # Sudoデーモン # 00_drivers: ドライバー初期化 pcid-spawner /etc/pcid.d/ # PCIドライバー ``` ### 2. **AIOS向け最小構成実装** #### ディレクトリ構造 ``` aios-minimal/ ├── kernel/ # Redoxカーネルベース │ ├── Cargo.toml │ ├── src/ │ │ ├── main.rs # カーネルエントリーポイント │ │ ├── memory/ # メモリ管理 │ │ ├── syscall/ # システムコール │ │ └── scheme/ # スキームベースFS ├── bootloader/ # ブートローダー ├── userspace/ # ユーザーランド │ ├── init/ # 初期化プロセス │ ├── shell/ # Rustシェル │ └── coreutils/ # 基本コマンド └── config/ └── minimal.toml # 最小構成設定 ``` #### カーネル最小化(kernel/Cargo.toml) ```toml [package] name = "aios-kernel" version = "0.1.0" edition = "2021" [dependencies] # 必要最小限の依存関係のみ bitflags = "2" spin = "0.9.8" linked_list_allocator = "0.9.0" redox_syscall = { git = "https://gitlab.redox-os.org/redox-os/syscall.git", default-features = false } [features] default = [ "serial_debug", # シリアルデバッグのみ ] # 不要機能を削除 # acpi = [] # ACPI無効化 # multi_core = [] # シングルコアのみ # graphical_debug = [] # グラフィカルデバッグ無効 ``` #### 最小カーネル実装(kernel/src/main.rs) ```rust //! AIOS Minimal Kernel #![no_std] #![no_main] #![feature(allocator_api)] use core::panic::PanicInfo; mod memory; mod syscall; mod scheme; /// カーネルエントリーポイント #[no_mangle] pub extern "C" fn kmain() -> ! { // 基本初期化 memory::init(); scheme::init(); syscall::init(); // 最初のユーザープロセス起動 let init_pid = syscall::exec("/usr/bin/init", &[]).expect("Failed to start init"); // メインループ loop { syscall::sched_yield(); } } #[panic_handler] fn panic(_info: &PanicInfo) -> ! { loop {} } ``` #### シンプルな初期化プロセス(userspace/init/src/main.rs) ```rust //! AIOS Init Process use std::process::Command; fn main() { println!("AIOS Init Starting..."); // 基本デーモン起動 spawn_daemon("ptyd"); // PTY管理 spawn_daemon("sudo"); // 権限管理 // シェル起動 Command::new("/usr/bin/shell") .spawn() .expect("Failed to start shell"); // プロセス管理ループ loop { // ゾンビプロセス回収 std::thread::sleep(std::time::Duration::from_secs(1)); } } fn spawn_daemon(name: &str) { Command::new(format!("/usr/bin/{}", name)) .spawn() .unwrap_or_else(|_| panic!("Failed to start {}", name)); } ``` #### 軽量シェル実装(userspace/shell/src/main.rs) ```rust //! AIOS Minimal Shell use std::io::{self, Write}; use std::process::Command; fn main() { println!("AIOS Shell v0.1.0"); loop { print!("aios> "); io::stdout().flush().unwrap(); let mut input = String::new(); io::stdin().read_line(&mut input).unwrap(); let input = input.trim(); match input { "exit" => break, "help" => show_help(), cmd if !cmd.is_empty() => execute_command(cmd), _ => {} } } } fn execute_command(cmd: &str) { let parts: Vec<&str> = cmd.split_whitespace().collect(); if parts.is_empty() { return; } match Command::new(parts[0]) .args(&parts[1..]) .status() { Ok(status) => { if !status.success() { println!("Command failed with exit code: {:?}", status.code()); } } Err(e) => println!("Failed to execute command: {}", e), } } fn show_help() { println!("Available commands:"); println!(" ls - List files"); println!(" cat - Show file contents"); println!(" echo - Print text"); println!(" help - Show this help"); println!(" exit - Exit shell"); } ``` #### 構成ファイル(config/minimal.toml) ```toml # AIOS Minimal Configuration [general] filesystem_size = 128 # 128MiB(Redoxの196MiBより小さく) prompt = false [packages] aios-kernel = {} aios-init = {} aios-shell = {} aios-coreutils = {} # 最小ファイルシステム [[files]] path = "/usr/lib/init.d/00_base" data = """ mkdir -p /tmp /var/log aios-init """ [[files]] path = "/etc/hostname" data = "aios" # ユーザー設定 [users.root] password = "root" uid = 0 gid = 0 shell = "/usr/bin/aios-shell" ``` ### 3. **メモリ使用量最適化** #### カーネル最適化 - **スタックサイズ**: 4KB(最小限) - **ヒープサイズ**: 1MB(動的拡張) - **バッファサイズ**: 512B(固定) #### ユーザーランド最適化 ```rust // メモリ制限 const MAX_PROCESSES: usize = 16; // 最大プロセス数 const MAX_FILES: usize = 64; // 最大ファイル数 const STACK_SIZE: usize = 8192; // プロセススタック8KB ``` ### 4. **ビルドシステム** #### Makefile ```makefile # AIOS Minimal Build System TARGET = x86_64-unknown-none ARCH = x86_64 CONFIG = minimal .PHONY: all kernel userspace iso clean all: iso kernel: cd kernel && cargo build --release --target $(TARGET) userspace: cd userspace && cargo build --release iso: kernel userspace mkdir -p build/iso cp kernel/target/$(TARGET)/release/aios-kernel build/iso/ cp userspace/target/release/* build/iso/ grub-mkrescue -o aios-minimal.iso build/iso clean: cargo clean rm -rf build/ ``` ### 5. **機能制限とトレードオフ** #### 削除される機能 - マルチコア対応 - ACPI高度機能 - グラフィカルデバッグ - 複雑なファイルシステム - ネットワーク機能 #### 保持される機能 - 基本的なプロセス管理 - シンプルなファイルシステム - 最低限のメモリ管理 - シリアル出力 - 基本的なシステムコール この設計により、**128MB以下の極小OS**を実現できます。