liveBook · Manning

Boot image

As explained in Rust in Action: Systems programming concepts and techniques

bootimage is a Rust crate that automates building bootable images containing an operating-system kernel and its bootloader.

A boot image is a bootable disk image containing an operating-system kernel and its bootloader. An ordinary executable is not enough when the operating system it targets does not yet exist. The Rust bootimage crate builds the image so the kernel can be started in a virtual machine or booted on hardware. section 11.1.1 section 11.1.2

For a new kernel target, the build has several jobs:

  • Define the target machine’s conventions, including its CPU architecture. section 11.2.1
  • Compile Rust’s core library and the kernel for that target. section 11.2.1
  • Build a bootloader that loads the kernel, then start it in a virtual environment. bootimage automates these steps. section 11.2.1
Listing 11.1 Project metadata for fledgeos-0
[package]
name = "fledgeos"
version = "0.1.0"
authors = ["Tim McNamara <author@rustinaction.com>"]
edition = "2018"
 
[dependencies]
bootloader = "0.9"
x86_64 = "0.13"
 
[package.metadata.bootimage]
build-command = ["build"]
 
run-command = [                 #1
  "qemu-system-x86_64", "-drive", "format=raw,file={}"
]

Cargo.toml supplies bootimage instructions: build-command selects cargo build, while run-command starts QEMU with the generated image instead of running the kernel executable directly. section 11.2.3

Listing 11.3 Extra build-time configuration for cargo
[build]
target = "fledge.json"
 
[unstable]
build-std = ["core", "compiler_builtins"]
build-std-features = ["compiler-builtins-mem"]
 
[target.'cfg(target_os = "none")']
runner = "bootimage runner"

The Cargo configuration selects a custom target, requests a build of core and compiler_builtins, and sets bootimage runner for the target without an operating system. section 11.2.3

First boot in QEMU

Run cargo +nightly run in the configured kernel project. The shown run starts qemu-system-x86_64 with a generated bootimage-fledgeos.bin disk image. section 11.2.1

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Walks through target, core, kernel, and bootloader build steps, then shows configuration without detailing bootimage internals.
Examples
Uses a configured Rust kernel project whose cargo run command starts QEMU with a generated .bin image.
What it assumes
Assumes some familiarity with Rust targets while supplying setup and verification steps for the required tools.
What you can do after
Operate/configure it to build a bootable Rust kernel image and start that image in QEMU.
How it connects
Connects boot images to compiler targets, Rust core, bootloaders, Cargo, and QEMU.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.