BarryServer : Projects

A set of pages about projects I do
// BarryServer : Projects / Orion / develop

// Related

Developing Orion

In order to develop Orion, you'll need to have a build environment specific to Orion. If you're developing from inside Orion, you can skip these steps since the pre-installed tools are already built to target the system. This includes the Orion toolchain of GCC and Binutils, and a general environment that simulates that of Orion. Since Orion is a Unix-like, you just need to set up a system root, so you don't override your own system. All Orion build systems will look for the SYSROOT environment variable and will prefix all install paths with it. If this variable is not found, it is assumed to be your system's root directory, and if you're not running Orion, things might break. If you want to skip this guide, this script will do it all automatically. Execute the following to use it:
mkdir Orion
cd Orion
export SYSROOT=$PWD/sysroot
wget https://files.barryserver.net/orion/setup.sh
chmod +x setup.sh
./setup.sh
This will setup a working directory that can house all of the Orion codebase, installs the Orion toolchain, and builds a disk image

System Root

Orion requires a system root to install everything into, and also needs a disk image of this that it can boot with. The system root is just a folder on your host system that will become the / directory on Orion. A good method is to generate a blank Ext2FS image, then to mount it, and use that as your system root. This can be done very simply with the following commands:
mkfs.ext2 -b 4096 orion.img 16384
sudo mount orion.img /mnt
export SYSROOT=/mnt
This will generate a 64MB Ext2FS image, since 16384*4096 is 64MB - change the last number to suit your needs.

Since this sysroot is empty, you'll want to install the system headers and some other files needed for the OS to run. You can do this by manually creating the system files in your system root:
mkdir dev etc proc root
echo "orion" > etc/hostname
echo "root:password:0:0::/root:/bin/sh" > etc/passwd
echo "root:0:root" > etc/group
Once these are in place, you can install the system headers. You'll need to clone the OrionLibC git repo, and run:
make install-headers

Orion Toolchain

The Orion toolchain is based on GCC and Binutils, since they're both free and open-source, and support all the features needed to compile the system. Depending what system you're running, you'll need to install some prerequisite tools to enable you to build both packages. You need to install Nasm, Texinfo, Flex, Bison, GMP, MPC and MPFR, all of which can be found through your package manager.
In Arch:
sudo pacman -S base-devel gmp libmpc mpfr mtools nasm
In Debian:
sudo apt-get install build-essential bison flex libgmp3-dev libmpc-dev libmpfr-dev texinfo

GCC and Binutils both rely on autotools to configure the build, so you'll need to install the correct version of autoconf and automake. The current versions used in the Orion toolchain are GCC 12.2.0 and Binutils 2.39, which both need autoconf 2.69 and automake 1.15.1. You can install both of them by building from source easily:
wget https://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
wget https://ftp.gnu.org/gnu/automake/automake-1.15.1.tar.gz
tar zxvf autoconf-2.69.tar.gz
tar zxvf automake-1.15.1.tar.gz
cd autoconf-2.69
./configure
make
sudo make install
cd ../automake-1.15.1
./configure
make
sudo make install
cd ..
rm -rf auto*
Similarly you'll want to download the GCC and Binutils source code:
wget https://ftp.gnu.org/gnu/gcc/gcc-12.2.0/gcc-12.2.0.tar.gz
wget https://ftp.gnu.org/gnu/binutils/binutils-2.39.tar.gz
tar zxvf gcc-12.2.0.tar.gz
tar zxvf binutils-2.39.tar.gz
rm *.tar.gz

Before you build the sources, you need to configure them for the build. Part of this process includes selecting the machine they'll target. The Orion toolchain will need to target Orion, and even though the kernel may be built freestanding it still needs stdint.h from the target. GCC and Binutils do not support Orion out of the box, so you can patch in support with the following commands:
wget https://files.barrsyerver.net/orion/binutils.patch
wget https://files.barryserver.net/orion/gcc.patch
patch -t -p 0 < binutils.patch
patch -t -p 0 < gcc.patch
cd binutils-2.39/ld/
automake
cd ../../gcc-12.2.0/libstdc++-v3/
autoconf
cd ../../

Finally to build GCC and Binutils for Orion:
mkdir -p binutils-orion-build
cd binutils-orion-build
../binutils-2.39/configure --target=i686-orion --with-sysroot=$SYSROOT --disable-nls --disable-werror
make
sudo make install
cd ..

mkdir -p gcc-orion-build
cd gcc-orion-build
../gcc-12.2.0/configure --target=i686-orion --disable-nls --enable-languages=c --with-sysroot=$SYSROOT
make all-gcc
make all-target-libgcc
sudo make install-gcc
sudo make install-target-libgcc
cd ..
You should now be able to invoke the i686-orion-* suite of tools.

Building and Installing

Now you've got the toolchain and environment setup, you can build and install the kernel, libc, userspace and desktop. This requires you clone the Orion, OrionUserspace and OrionDesktop git repos. You should also have a copy of the OrionLibC repo (you probably already cloned this). The kernel can be built instantly since it only relies on the system headers being installed. The userspace cannot be built until the LibC has been built and installed. All three repos can be built and installed with:
make install

Due to header interdependencies, you need to build certain parts of the system before others. The most important detail is to have all of the system (kernel and libc) headers installed. To install these you need to run:
make install-headers
This must be done in both the kernel and libc repo before user space is built. The kernel can be compiled on its own, and it is recommended to install the kernel headers when building and installing it. The libc depends on the kernel headers, but not the kernel itself. You should build and install the libc and the libc headers at the same time. When the libc is installed, the userspace programs can be built. Since they depend on linking with the libc.a library, you need to have built and installed libc and libc headers before this.

Testing

If you're developing from inside Orion, you can just reboot (if it was the kernel), or test your userspace programs on the machine. If you're not inside Orion, you'll need to use a virtual machine. Currently, there are two options for testing your disk image in a virtual machine: install a bootloader and boot the image, or use an emulator that can boot the kernel directly. QEMU fits this second category, and is easy to use. You can just pass the -kernel option and the location of the kernel binary. You'll always want to attach the Ext2FS image of the system root so the kernel actually has something to do. QEMU allows you to specify the number of CPU cores and memory too, which are also done below:
qemu-system-i386 -kernel $SYSROOT/boot/orion -drive format=raw,file=orion.img,if=ide -smp 2,maxcpus=2 -m 4G -enable-kvm -cpu host -debugcon stdio -display sdl

If you want to install a bootloader, GRUB is a good choice. You'll need to generate a disk image first, and you can copy the Ext2FS image to one of the partitions. Follow the standard GRUB installation instructions targetting your system root. Similar to QEMU, tell GRUB the location of the kernel binary in the config file:
set timeout=0
set default=0

menuentry "Orion" {
        multiboot /boot/orion
        boot
}
When you've completed this, you should just be able to boot the raw disk image.

Before you boot you may want to change the file ownership of the system root:
sudo chown -R 0:0 $SYSROOT
You may need to flush the disk image so QEMU can see the latest version of files using the disk image. To do this run:
sync