This guide provides detailed steps to build a custom Linux kernel and Busybox to create a minimalistic and customizable Linux operating system. Follow the steps below to set up your custom environment.
Ensure that the required dependencies are installed on your system. Run the following command to install necessary packages:
debian: sudo apt-get install bzip2 git vim make gcc libncurses-dev flex bison bc cpio libelf-dev libssl-dev syslinux dosfstools nano git
fedora: sudo dnf install bzip2 git vim make gcc ncurses-devel flex bison bc cpio elfutils-libelf-devel openssl-devel syslinux dosfstools nano git
Clone the Linux Kernel repository from Linus Torvalds' GitHub repository:
git clone --depth 1 https://github.com/torvalds/linux.git
cd linux
Run the following command to configure the kernel:
make menuconfig
This command opens a configuration menu. Add the desired configurations to the kernel and save the changes.
Build the kernel using the following command. Adjust the -j
parameter according to your processor count:
make -j 4
The compiled kernel image will be located at arch/x86/boot/bzImage
.
Create a directory to store boot files and copy the kernel image to it:
mkdir /boot-files
cp arch/x86/boot/bzImage /boot-files
Clone the Busybox repository for the userspace utilities:
git clone --depth 1 https://git.busybox.net/busybox
cd busybox
Configure Busybox to build a static binary:
make menuconfig
Select "Build static binary" from the configuration menu and save the changes.
Build Busybox with the following command. Adjust the -j
parameter according to your processor count:
make -j 4
Create an initial RAM filesystem (initramfs) to be loaded by the kernel after boot:
mkdir /boot-files/initramfs
make CONFIG_PREFIX=/boot-files/initramfs install
cd /boot-files/initramfs/
nano init
Add the following content to the init
file:
#!/bin/sh
/bin/sh
Remove the linuxrc
file:
rm linuxrc
Make the init
file executable:
chmod +x init
Create the initramfs archive:
find . | cpio -o -H newc > ../init.cpio
Install syslinux and create a bootable file system:
sudo apt-get install syslinux
dd if=/dev/zero of=boot bs=1M count=50
ls
sudo apt-get install dosfstools
mkfs -t fat boot
syslinux boot
mkdir m && mount boot m
cp bzImage init.cpio m
Unmount the boot file system:
umount m
Now, your boot file is ready to boot into the x86_64 architecture. In the boot section, add the following:
boot: /bzImage -initrd=/init.cpio
This configuration will initiate the boot process for your custom Linux OS.