Creating a Simple Linux System
You need to have a few tools installed, specifically a cross compiler, QEMU, and GRUB. You'll also need Flex, Bison,
make, and OpenSSL.
To install a cross compiler, go to https://wiki.osdev.org/GCC_Cross-Compiler#External_Links and download one for the target
I recommend installing into a directory such as
/usr/cross and adding those paths to the
You'll also need to create some symlinks:
To install the other packages on Arch Linux:
For Ubuntu or Debian:
( @firefish help)
Building the Linux kernel from source
First, we need to build the Linux kernel from source. You could just use
/boot/vmlinuz but I will show you how to build it from source so that we have full control over the build.
First, download it, and extract the tarball:
Now we need to create the configuration for the
And now, we build the compressed linux kernel:
Now, wait for it to finish building.
Once it's finished building, copy the resulting kernel:
And exit the directory:
You can test the kernel with:
The kernel will print a lot of messages, then it'll panic about not being able to find a VFS. This is because we haven't created a initial ramdisk yet!
Creating the initial RAM filesystem
When a linux computer boots, a bootloader loads a linux kernel, and something which is known as an initial ramdisk. The initial ramdisk usually contains a filesystem. The Linux kernel tries to run the init script in that filesystem
/init, and when it fails, it panics.
Creating the filesystem hiearchy
First, we need to create the filesystem hiearchy:
We could theoretically compile glibc and every GNU coreutils. But for simplicity, we'll use busybox, which puts many of these programs into one executable.
We can download a busybox binary with:
And to create a symlink to busybox for the shell:
Creating the init script
We need to create the
/init script, which is the executable that will be run on startup.
Put this into
./initramfs/init. I'll explain every part of this in the comments:
Now we need to create the cpio archive and gzip it (credits go to this tutorial):
Finally, we can test this in QEMU with:
You should be dropped into an interactive shell!
Creating a CD image
This is great, but how do we run this on real hardware? The answer is that we need to create a CD image.
We can use
grub-mkrescue for this.
First, create the CD root directory:
Then, we need to create a grub configuration. Put this into
Now copy the files into the
And create the CD image:
To test this in QEMU:
And you're done!
When I run it in a vm I get an initramfs error! :(
[ 6.928259] Failed to execute /init (error -2)
[ 6.931476] Run /sbin/init as init process
[ 6.936972] Run /etc/init as init process
[ 6.940077] Run /bin/init as init process
[ 6.942810] Run /bin/sh as init process
[ 6.955143] Kernel panic - not syncing: No working init found. Try passing init= option to kernel. See Linux Documentation/admin-guide/init.rst for guidance.