backup: Configuring the kernel environment from scratch

Configuring kernel environment

Compiling kernel

  1. Download kernel source code: https://www.kernel.org/

  2. Install necessary dependencies

    1
    2
    sudo apt-get update
    sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc
  3. Unzip and enter the directory

    1
    make menuconfig

    ps: There may be errors that require flex and bison, apt-get install can fix them

    Nothing should be changed, just save

    1
    2
    3
    4
    5
    6
    enter kernel hacking
    Select the following items
    Kernel debugging
    Compile-time checks and compiler options —> Compile the kernel with debug info和Compile the kernel with frame pointers
    KGDB
    Then save and exit

    ps: Remember not to make the terminal window too small when make menuconfig. Otherwise you will be prompted and not allowed to complete the next steps.

  4. ```bash
    make bzImage

    Setup is 17244 bytes (padded to 17408 bytes).
    System is 7666 kB
    CRC 5c77cbfe
    Kernel: arch/x86/boot/bzImage is ready (#1)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24


    ### Add a simple syscall

    Helloworld again

    Create a `helloworld` directory in the root of the source code

    ```bash
    $ pwd
    /home/test/test_kernel/linux-xxxxxxx/helloworld
    $ tree
    .
    ├── helloworld.c
    └── Makefile
    $ cat helloworld.c
    #include <linux/kernel.h>

    asmlinkage long sys_helloworld(void){
    printk("{==kernel==} hello world\n");
    return 0;
    }
    $ cat Makefile
    obj-y=helloworld.o

Then go to the Makefile in the root of the source code and add helloworld/

1570110418013.png

Then go to include/linux/syscalls.h and add the function prototype

1570110666526.png

Add system call numbers to arch/x86/entry/syscalls/syscall_32.tbl and arch/x86/entry/syscalls/syscall_64.tbl

1570110860880.png

1570111062848.png

After that, compile the kernel

1
make bzImage

It will be able to get bzImage in ./arch/x86/boot/

Compile busybox

As usual, download from the official website https://busybox.net/

Unzip and enter the directory

1
make menuconfig

Selected Build static binary (no shared libs) within Settings

1
make install

After compiling, a _install directory will appear, then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
$ cd _install
$ mkdir proc
$ mkdir sys
$ touch init
$ chmod +x init
$ cat init
#!/bin/sh
echo "{==DBG==} INIT SCRIPT"
mkdir /tmp
mount -t proc none /proc
mount -t sysfs none /sys
mount -t debugfs none /sys/kernel/debug
mount -t tmpfs none /tmp
# insmod /xxx.ko # load ko
mdev -s # We need this to find /dev/sda later
echo -e "{==DBG==} Boot took $(cut -d' ' -f1 /proc/uptime) seconds"
setsid /bin/cttyhack setuidgid 1000 /bin/sh #normal user
# exec /bin/sh #root

// By the way, write a test case and put it in _install
$ touch test.c
$ cat test.c
// gcc test.c -static -o test
#include <unistd.h>

int main(void){
syscall(1337);
return 0;
}
$ gcc test.c -static -o test

// Write a script to package rootfs.img
$ cd ../../
$ touch makeimg
$ chmod +x makeimg
$ cat makeimg
#!/bin/sh
echo "Generate rootfs.img"
cd busybox-x.xx.x/_install
find . | cpio -o --format=newc > ../../rootfs.img

$ ls
busybox-x.xx.x linux-x.x.x qemu-x.xx.x makeimg

$ ./makeimg
$ ls
busybox-x.xx.x linux-x.x.x qemu-x.xx.x makeimg rootfs.img

Install qumu

Here using source code compilation

1
2
3
4
5
6
wget https://download.qemu.org/qemu-4.1.0.tar.xz
tar xvJf qemu-4.1.0.tar.xz
cd qemu-4.1.0
./configure
make
make install

qemu boot

Get a script from veritas501

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ ls
busybox-x.xx.x linux-x.x.x qemu-x.xx.x makeimg rootfs.img

$ touch runqemu
$ chmod +x runqemu
$ cat runqemu
#!/bin/sh
qemu-system-x86_64 \
-m 64M \
-kernel /home/test/test_kernel/linux-x.x.x/arch/x86/boot/bzImage \
-initrd /home/test/test_kernel/rootfs.img \
-append "root=/dev/ram rw console=ttyS0 oops=panic panic=1 kalsr" \
-netdev user,id=t0, -device e1000,netdev=t0,id=nic0 \
-nographic \
-monitor /dev/null \
-smp cores=2,threads=1 \
-enable-kvm \
-cpu kvm64,+smep \
# -gdb tcp::1234 \
# -S

Run Script

1
$ ./runqemu

1570264965223.png

Done!

Reference

kernel环境配置

As well, thanks for MiGo and Aris’ guidance OWO

Other possible problems

Freely write

pkg-config not found

sudo apt-get install pkg-config

glib-2.40 gthread-2.0 is required to compile QEMU

Use apt-cache search all | grep glib to find glib, can find glib’s name is libglib2.0-dev, then apt install libglib2.0-dev

ERROR: pixman >= 0.21.8 not present.
Please install the pixman devel package.

Solution:
Use apt-cache search pixman to find, then apt install libpixman-1-dev

Virtual machines remember to enable CPU virtualization

VNC server running on 127.0.0.1:5900

sudo apt-get install libsdl1.2-dev

sudo apt-get install gcc libsdl1.2-dev zlib1g-dev libasound2-dev pkg-config libgnutls-dev pciutils-dev

sudo apt-get install libsdl2-dev

sudo apt-get install libsdl2-2.0

sudo apt install libelf-dev

Author

ACce1er4t0r

Posted on

2022-03-09

Updated on

2023-04-22

Licensed under