Implementing Own Operating System(OS) (Part 1)

Dineshika Sivapalaraja
4 min readOct 1, 2022

--

An operating system (OS) is software that is used to manage tasks between computer hardware and software. Linux, Unix, macOS, and Windows are the most popular Operating systems nowadays.

In this article, I am going to take consider implementing my own operating system using Ubuntu. However here I am implementing OS in a virtual way using an Oracle VM Virtual box.

You can see the steps to install Ubuntu on the virtual box using below YouTube link.

Although in these steps, we have to include some packages in this Virtual Box. Nasm, Genisoimage, Bochs, and Bochs-SDL are the most important packages for implementing an own OS.

Below you can see the command to install those packages using the terminal.

sudo apt-get install build-essential nasm genisoimage bochs bochs-sdl

This OS is created by C programming and GCC. I choose C for creating an operating system because it requires very good control over the generated code and easy access to memory. Although, this OS is created with assembly codes. That’s why we install the NASM package previously.

Steps to Booting an Operating system

Step 01 Compiling the Operating System

This is the first Assembly code to implement an OS.

loader.s

Next, using NASM assembler this loader.s file changes into loader.o file.

Command is:

nasm -f elf32 loader.s

Step 02 Linking the Kernel

To create an executable file, the code must now be linked, which involves more consideration than when linking typical programs. We want GRUB to load the kernel at a memory address larger than or equal to (1MB).

link.ld

The executable can now be linked with the following command:

ld -T link.ld -melf_i386 loader.o -o kernel.elf

The final executable file is kernel.elf.

Step 03 Obtaining GRUB

The GRUB bootloader stage2_eltorito is download and copy through below link.

https://lukas-eisenhauer.de/git/lukas/treeos/blob/ea218aff4af327c2f5b4341caad24c70666d401a/iso/boot/grub/stage2_eltorito

Step 04 Building an ISO Image

I will create the kernel ISO image with the program genisoimage. A folder must first be created that contains the files that will be on the ISO image. The following commands create the folder and copy the files to their correct places:

mkdir -p iso/boot/grub              # create the folder structure
cp stage2_eltorito iso/boot/grub/ # copy the bootloader
cp kernel.elf iso/boot/ # copy the kernel

A configuration file menu.lst for GRUB must be created. This file tells GRUB where the kernel is located and configures some options:

default=0
timeout=0

title os
kernel /boot/kernel.elf

Place the file menu.lst in the folder iso/boot/grub/. The contents of the iso folder should now look like the following figure:

iso
|-- boot
|-- grub
| |-- menu.lst
| |-- stage2_eltorito
|-- kernel.elf

Although, the ISO image can then be generated with the following command:

genisoimage -R                              \
-b boot/grub/stage2_eltorito \
-no-emul-boot \
-boot-load-size 4 \
-A os \
-input-charset utf8 \
-quiet \
-boot-info-table \
-o os.iso \
iso

If you do these steps correctly then you can obtain some files like the below snap.

Step 05 Running Bochs

From now onwards I can run the OS in the Bochs emulator using the os.iso ISO image. Bochs needs a configuration file to start, below code refer as one example:

bochsrc.txt

Once we saved the configuration in a file named bochsrc.txt then we can run Bochs with the following command:

bochs -f bochsrc.txt -q

After that, we can see Bochs starting and displaying a console with some information from GRUB on it.

Using the continue command we can see this below screen. Although it shows our booting is successful.

Once your OS booting successfully happens, then you must have 9 files in that folder. you can see the files using the below snap.

I hope this article will be useful for implementing an OS.

Keep learning!

Thank you.

--

--