Basic_os is an experimental and educational project focused on building a minimal operating system kernel using the Rust programming language. It leverages Rust's powerful safety guarantees and low-level capabilities to create a robust and reliable foundation for an OS.
This project serves as an excellent starting point for anyone interested in bare-metal programming, understanding the internals of an operating system, or exploring Rust's potential beyond application development. It focuses on the essential components required to boot a custom kernel and interact directly with hardware.
- Bare-metal Rust Development: Core kernel logic written entirely in Rust, interacting directly with hardware without an underlying OS.
- Custom Target Configuration: Utilizes a custom target specification (
rust_os.json) for precise control over the compilation environment, essential for OS development. - Cargo-managed Project: Leverages Cargo for robust dependency management, building, and testing of the kernel.
- Minimalist Design: Aims for a lean and efficient kernel, focusing on fundamental OS primitives.
- Modular Structure: Organized codebase to facilitate understanding and expansion of OS components.
OS Development Tools (External):
To get Basic_os up and running in an emulated environment, follow these steps.
Ensure you have the following installed on your system:
- Rust Toolchain: Install
rustupto manage Rust versions.curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
- Specific Rust Toolchain: This project requires a specific Rust toolchain version and components as defined in
rust-toolchain.toml. Install it:rustup override set $(cat rust-toolchain.toml | grep -o 'channel = "[^"]*"' | cut -d'"' -f2) rustup target add x86_64-unknown-none # Add the bare-metal target for building
cargo-binutils: Essential for working with custom targets and generating bootable images.cargo install cargo-binutils rustup component add llvm-tools-preview
- QEMU: A generic and open-source machine emulator and virtualizer, used to run the compiled OS kernel.
# On Debian/Ubuntu sudo apt install qemu-system-x86 # On Fedora sudo dnf install qemu-system-x86 # On macOS with Homebrew brew install qemu
-
Clone the repository
git clone https://github.com/BSZKaneki/Basic_os.git cd Basic_os -
Add custom target specification The project uses a custom target named
rust_os.json. You need to tell Cargo about this target.rustup target add --json rust_os.json
Note: This command is a placeholder; typically, you'd place the
rust_os.jsonfile in a.cargo/config.tomldefined build path, or a tool likebootloaderhandles this automatically. -
Build the kernel Compile the kernel for the custom
rust_ostarget.cargo build --target rust_os.json
This command will generate the kernel executable in
target/rust_os/debug/basic_os. For a release build:cargo build --release --target rust_os.json
-
Create a bootable image This step typically involves using
bootimage(a common Rust OS dev crate) orcargo-binutilsto link the kernel with a bootloader. Assuming the project is set up to usebootimageimplicitly (as is common forBasic_os-style projects):cargo bootimage --target rust_os.json
If
bootimageis not integrated, you might need manual steps involvingobjcopyfromcargo-binutils:# Example using cargo-objcopy, actual command might vary based on bootloader cargo objcopy -- -O binary target/rust_os/debug/basic_os target/basic_os.binThis will produce a bootable image (e.g.,
bootimage-basic_os.bin) in yourtarget/rust_os/debugortarget/x86_64-rust_os/debugdirectory. -
Run in QEMU Execute the bootable image using QEMU:
qemu-system-x86_64 -drive format=raw,file=target/rust_os/debug/bootimage-basic_os # Or wherever the bootimage is locatedThis will launch a QEMU window, where you should see the output from your kernel.
Basic_os/
├── .cargo/ # Cargo configuration directory
├── .gitignore # Git ignore file
├── Cargo.lock # Locked dependencies for Cargo
├── Cargo.toml # Project manifest and dependencies
├── rust-toolchain.toml # Specifies the Rust toolchain version
├── rust_os.json # Custom target specification for the OS
├── src/ # Kernel source code
│ └── main.rs # Main entry point of the kernel
└── tests/ # Integration and unit tests
└── ... # Test files
To build the kernel for development:
cargo build --target rust_os.jsonFor an optimized release build:
cargo build --release --target rust_os.jsonAfter building, run the generated bootable image with QEMU:
qemu-system-x86_64 -drive format=raw,file=target/rust_os/debug/bootimage-basic_osReplace target/rust_os/debug/bootimage-basic_os with the actual path to your bootable image.
This project includes tests to ensure the correctness of kernel components.
# Run all tests
cargo testNote: Due to the nature of OS development, many kernel tests might require specific execution environments or custom test runners not directly invoked by cargo test. The tests/ directory likely contains integration tests or specific unit tests that can run in a hosted environment.