Compile the Linux kernel and run a minimal file system

Development environment: ubuntu 14.04

Linux source code version: linux-4.9.229

Busybox source code version: busybox-1.30.0

qemu-system-x86_64 version: 2.0.0

This article teaches you to complete the following process:

1. Download linux and compile the linux kernel source code

2. Compile busybox

3. Make a minimum root file system

4.qemu starts your compiled kernel and root file system

Download address of linux source code:

The version I chose is 4.9.229

Download it locally, unzip it, and then enter the linux-4.9.229 Directory:

1. Specify the hardware architecture.

In order to reduce the process of installing the compiler, I use x86 in this example because my development environment is x86. If you want to compile the kernel of arm, specify ARCH=arm and install the cross compiler.

# export ARCH=x86

2. Configure board config, which is x86_64_defconfig. OK, we're ready. The menu is x86_64_defconfig

# make  x86_64_defconfig

3. Configure kernel

This step is actually to fine tune the menu in step 2. We need the kernel to support ramdisk driver, so we need to select the following configuration:

General setup  --->
       ----> [*] Initial RAM filesystem and RAM disk (initramfs/initrd) support
    Device Drivers  --->
       [*] Block devices  --->
               <*>   RAM block device support
               (65536) Default RAM disk size (kbytes)

4. Compile kernel

# make

The compiled kernel is located in arch/x86_64/boot/bzImage

Download the buysbox source code at:

The version I use is busybox-1.30.0

1. Unzip the buysbox source code

# tar xvf busybox-1.30.0.tar.bz2

2. Configure the buysbox source code

Here, we configure busybox for static compilation, so that busybox does not need additional dynamic link libraries when running.

# make menuconfig
Busybox Settings  --->
      Build Options  --->
            [*] Build BusyBox as a static binary (no shared libs)

3. Compilation and installation

#make && make install

4. The compiled busybox is installed in the root directory of the source code_ The install directory is. Let's enter_ Install directory. Add some necessary files or directories. The relevant shell commands are as follows:

# mkdir etc dev mnt
# mkdir -p proc sys tmp mnt
# mkdir -p etc/init.d/
# vim etc/fstab
proc        /proc           proc         defaults        0        0
tmpfs       /tmp            tmpfs      defaults        0        0
sysfs       /sys            sysfs        defaults        0        0
# vim etc/init.d/rcS
echo -e "Welcome to tinyLinux"
/bin/mount -a
echo -e "Remounting the root filesystem"
mount  -o  remount,rw  /
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
# chmod 755 etc/init.d/rcS
# vim etc/inittab
::cttlaltdel:/bin/umount -a -r
# chmod 755 etc/inittab
# cd dev
# mknod console c 5 1
# mknod null c 1 3
# mknod tty1 c 4 1 

Such a smallest and complete file system that can be booted by the kernel will live.

Create a root file system image file.


1. Create an empty image file first;

2. Then format the image file into ext3 format;

3. Mount the image file and copy the root file system to the mount directory;

4. Uninstall the image file.

5. Make it into gzip package.

rm -rf rootfs.ext3
rm -rf fs
dd if=/dev/zero of=./rootfs.ext3 bs=1M count=32
mkfs.ext3 rootfs.ext3
mkdir fs
mount -o loop rootfs.ext3 ./fs
cp -rf ./_install/* ./fs
umount ./fs
gzip --best -c rootfs.ext3 > rootfs.img.gz 

The final file system image name is rootfs.img.gz

With the kernel and file system images ready, a happy moment is coming:

Start our own compiled kernel and file system through qemu simulator:

# qemu-system-x86_64 \
  -kernel ./linux-4.9.229/arch/x86_64/boot/bzImage  \
  -initrd ./busybox-1.30.0/rootfs.img.gz   \
  -append "root=/dev/ram init=/linuxrc"  \
  -serial file:output.txt

Such a complete minimum linux system is up:

Well, after the above steps, you can compile a kernel and file system yourself. With this foundation, you can freely modify the linux kernel code and run it for verification.

Tags: Linux Windows bios

Posted on Mon, 29 Nov 2021 16:22:35 -0500 by keefe007