SBC (Single Board Computer)/Raspberry Pi 4

라즈베리파이, 커널 빌드 (Kernel build) (1)

LEEHANDS 2021. 5. 26. 12:18
반응형

커널(Kernel) 은 운영체제에서 가장 코어가 되는 부분이다.

커널은 각종 드라이버, 메모리, 프로세스 등 관리하는 역할

일반적으로 배포된 커널은 이미 일반적인 많은 기능을 포함하고 있기때문에 직접 컴파일할일이 별로 없다.

 

기본적인 커널에서 제공하지않는 기능을 사용하고싶을 때나 컴퓨터 부품과 관련하여 바뀐 부분이 있을 때 커널을 변경하여 컴파일해볼 필요가 있다.

또는 현재보드에서는 필요없는 드라이버를 지워서 커널을 최적화하여 부팅시간을 빠르게 설정할 수도 있다.

예를들어 특정 하드웨어 관리를 위한 디바이스 드라이버 (Device Driver) 를 작성할 때 커널 모듈 형태로 작성할 수 있다.

커널을 변경하기 위해 가장 첫번째 단계는 커널 소스를 직접 컴파일 해보는 것이다.


리눅스 커널의 주요 구성

1. linux/kernel : 커널의 핵심 소스 코드

2. linux/include : 커널 소스 코드의 헤더파일

3. linux/lib : 커널 내부에서 사용되는 함수 라이브러리

4. linux/drivers: 하드웨어 관리 목적의 디바이스 드라이버


github 에서 커널 소스 內 드라이버를 확인할 수 있다.

 

 

1. LOCAL BUILDING ( 타겟보드에서 직접 컴파일하는 방법 )

 

라즈베리파이 에서 리눅스 커널을 수정하고 이를 적용하려면 커널을 Re-Build 해야한다.

그리고 설치까지 할 수 있어야합니다.

 

시작하기 앞서 Shell 에서 관리자권한(root)을 획득한다.

sudo su

커널 소스를 다운로드 받을 폴더를 생성한다.

mkdir linux

폴더명은 뭐든지 상관없지만 필자는 linux 라고 생성했다.

 

 1-1. 커널 다운로드 전에 필요한 프로그램 설치 (Install utility)

 sudo apt install git bc bison flex libssl-dev make

1-2.  커널 다운로드 (Download Kernel )

cd linux
git clone --depth=1 --branch rpi-4.19.y https://github.com/raspberrypi/linux

라즈베리파이 커널은 github 에 있습니다.

https://github.com/raspberrypi 접속하면 원하는 리눅스 버전으로 다운로드하면됩니다.

필자는 4.19 Version 을 다운로드 받도록하겠습니다.

 

1-3 Kernel configuration

Kernel configuration

Configraution 은 일반적으로 make menuconfig interface 를 통해 수행됩니다.

또는 .config 파일을 수동으로 수정할 수 있지만 어려울 수 있다.

Preparing to configure the kernel

The menuconfig tool requires the ncurses development headers to compile properly. These can be installed with the following command:

sudo apt install libncurses5-dev

Using menuconfig

Once you've got everything set up and ready to go, you can compile and run the menuconfig utility as follows:

make menuconfig

If you're cross-compiling a 32-bit kernel:

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig

this will save to the .config

1-4. 커널 빌드 ( Buiding )

make -j4 zImage modules dtbs
sudo make modules_install
sudo cp arch/arm/boot/dts/*.dtb /boot/
sudo cp arch/arm/boot/dts/overlays/*.dtb* /boot/overlays/
sudo cp arch/arm/boot/dts/overlays/README /boot/overlays/
sudo cp arch/arm/boot/zImage /boot/$KERNEL.img

Raspberry Pi 2/3/4에서 -j4 플래그는 4 개의 코어 모두에서 작업을 분할하여 컴파일 속도를 크게 높입니다.

 

2. 크로스 컴파일 (Cross Compile)

첫째, 적합한 Linux 크로스 컴파일 호스트가 필요합니다. 우분투를 사용하는 경향이 있습니다. Raspberry Pi OS도 데비안 배포판이기 때문에 명령 줄과 같은 많은 측면이 유사하다는 것을 의미합니다.

 

2-1. Install required dependencies and toolchain

교차 컴파일을위한 소스를 빌드하려면 다음을 실행하여 머신에 필요한 종속성이 있는지 확인하십시오.

sudo apt install git bc bison flex libssl-dev make libc6-dev libncurses5-dev

Install the 32-bit toolchain for a 32-bit kernel

sudo apt install crossbuild-essential-armhf

Or, install the 64-bit toolchain for a 64-bit kernel

sudo apt install crossbuild-essential-arm64

 

2-2. Get sources

To download the minimal source tree for the current branch, run:

git clone --depth=1 https://github.com/raspberrypi/linux

 

2-3. Build Configuration

Enter the following commands to build the sources and Device Tree files:

a. Raspberry Pi 4: KERNEL=kernel71

b. Raspberry Pi 2,3: KERNEL=kernel7

c. Raspberry PI 1, Zero, Zero W: KERNEL=kernel

 

Default config

cd linux
KERNEL=kernel7l
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2711_defconfig

Run Menu config

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig

2-4. Build kernel with config

For all 32-bit builds

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs

2-5. Install directly onto the SD card

커널을 빌드했으면이를 Raspberry Pi에 복사하고 모듈을 설치해야합니다. 이것은 SD 카드 리더를 사용하여 직접 수행하는 것이 가장 좋습니다.
먼저 SD 카드를 연결하기 전후에 lsblk를 사용하여 식별하십시오. 다음과 같이 끝나야합니다.

sdb
   sdb1
   sdb2
mkdir mnt
mkdir mnt/fat32
mkdir mnt/ext4
sudo mount /dev/sda1 mnt/fat32
sudo mount /dev/sda2 mnt/ext4

with sdb1 being the FAT (boot) partition, and sdb2 being the ext4 filesystem (root) partition.

 

2-6. install the kernel modules onto the SD card

For 32-bit

sudo env PATH=$PATH make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=mnt/ext4 modules_install

Finally, copy the kernel and Device Tree blobs onto the SD card, making sure to back up your old kernel:

sudo cp mnt/fat32/$KERNEL.img mnt/fat32/$KERNEL-backup.img
sudo cp arch/arm/boot/zImage mnt/fat32/$KERNEL.img
sudo cp arch/arm/boot/dts/*.dtb mnt/fat32/
sudo cp arch/arm/boot/dts/overlays/*.dtb* mnt/fat32/overlays/
sudo cp arch/arm/boot/dts/overlays/README mnt/fat32/overlays/
sudo umount mnt/fat32
sudo umount mnt/ext4

 

3. 커널 빌드 ( Build Kernel )

커널을 빌드하기위해 쉘 스크립터를 작성하자. 

#!/bin/bash
 
echo "configure build output path"
 
KERNEL_TOP_PATH="$( cd "$(dirname "$0")" ; pwd -P )"
OUTPUT="$KERNEL_TOP_PATH/out"
echo "$OUTPUT"
 
KERNEL=kernel7
BUILD_LOG="$KERNEL_TOP_PATH/rpi_build_log.txt"

echo "move kernel source"
cd linux

echo "make defconfig"
make O=$OUTPUT bcm2709_defconfig
 
echo "kernel build"
make O=$OUTPUT zImage modules dtbs -j4 2>&1 | tee $BUILD_LOG

간략한 문법 설명

1. #!/bin/bash

==> #! 는 스크립트를 실행할 쉘을 지정하는 선언문이다 . 즉 사용하려는 명령어 해석기가 bash shell

2. KERNEL_TOP_PATH="$( cd "$(dirname "$0")" ; pwd -P )"

==> 현재 작업 폴더를 저장하는 라인. 즉 /home/pi/linux 가 들어가게 된다.

반응형