오래간만에 잠자고 있던 BeaglePlay board를 다시 꺼냈다. 그 이유는 이전 posting에서 열심히 설명했던, Rust를 Embedded Linux 환경에서 돌려보기 위해서이다. 😎
Linux BeagleBone 6.6.32-ti-arm64-r7 #1 SMP PREEMPT_DYNAMIC Wed Jul 3 19:49:27 UTC 2024 aarch64 GNU/Linux
Hello, world!
지난 35년 동안의 리눅스 커널(Linux Kernel) 개발 역사상 가장 중요한 전환점 중 하나는 Rust가 6.1 버전부터 메인라인에 병합되기 시작했다는 사실이다. 메모리 안전성(Memory Safety)과 Data Race 방지를 컴파일 타임에 보장하는 Rust 언어의 특성은, 오랫동안 C 언어가 지배해 온 커널 공간(Kernel Space)에서의 안정성을 획기적으로 향상시킬 만한 잠재력을 가지고 있다고 말할 수 있다.
Linux는 전통적으로 GCC(GNU Compiler Collection)와 GNU Make 기반의 생태계에 최적화되어 있는반면, Rust-for-Linux는 LLVM/Clang 툴체인, bindgen을 통한 FFI(Foreign Function Interface) 자동 생성, 그리고 Rust 컴파일러(rustc)와 커널 버전 간의 엄격한 버전 동기화를 전제로 설계되었다.
리눅스 커널에서의 Rust 지원은 이제 실험적 단계에서 성숙기로 넘어가는 과정(2025년 12월에 공식으로 사용하기로 결정)에 있는데, 각 커널 버전은 특정 버전의 Rust 컴파일러를 엄격하게 요구하고 있다. 이는 Rust 언어 자체가 아직 C 언어처럼 ABI(Application Binary Interface)가 완전히 고정되지 않았고, 커널 내부에서 사용하는 불안정 기능(Unstable Features)들이 컴파일러 버전에 따라 달라지기 때문이다.
전통적인 Linux 커널 빌드는 CROSS_COMPILE 변수를 통해 gcc를 사용한다. 그러나 Rust 커널 모듈을 빌드하기 위해서는 LLVM 툴체인이 사실상 필수적이다.
Bindgen과 libclang: 커널의 C 헤더 파일을 Rust 코드가 이해할 수 있는 바인딩으로 변환해주는 도구인
bindgen은libclang에 의존하여 C 코드를 파싱한다. 즉, Clang의 프론트엔드 기능이 호스트 머신에 반드시 존재해야 한다.LLVM=1 옵션: 커널 커뮤니티는 Rust 지원 커널을 빌드할 때 C 코드와 Rust 코드 모두를 LLVM/Clang으로 빌드하는 것을 "가장 잘 지원되는 설정(Best Supported Setup)"으로 권장하고 있다. GCC를 사용하여 C를 빌드하고 Rust만
rustc로 빌드하는 혼합 방식도 이론적으로 가능하지만, 링커(Linker) 호환성 문제나 LTO(Link Time Optimization) 구성에서 복잡한 문제가 발생할 수 있다.일반적인 Rust 애플리케이션 개발과 달리, 커널은
no_std환경(운영체제 없는 환경)에서 동작한다. 따라서 커널은 Rust의 표준 라이브러리 중 가장 핵심적인core와alloc라이브러리를 자신의 빌드 옵션에 맞춰 재컴파일해주어야 한다. 이를 위해서는 호스트 머신에 단순히rustc바이너리만 있어서는 안 되며, Rust 표준 라이브러리의 **소스 코드(rust-src)**가 반드시 설치되어 있어야 한다.
init/Kconfig 파일에 정의된 RUST 옵션의 depends on 조건이 충족되지 않았기 때문인데, 주요 조건은 다음과 같다.depends on!MODVERSIONS
depends on!GCC_PLUGINS
depends on!RANDSTRUCT
이 조건들을 하나씩 만족시켜야 비로소 메뉴에
Rust support 체크박스가 나타난다고 한다.Linux beagleplay 6.10.14 #1 SMP PREEMPT_DYNAMIC Tue Feb 17 21:44:38 KST 2026 aarch64 GNU/Linux
total 2324
drwxrwxr-x 2 1000 1000 260 Feb 20 07:01 .
drwxrwxr-x 11 1000 1000 280 Feb 20 07:39 ..
-rw-rw-r-- 1 1000 1000 241720 Feb 20 07:01 rust_configfs.ko
-rw-rw-r-- 1 1000 1000 280352 Feb 20 07:01 rust_debugfs.ko
-rw-rw-r-- 1 1000 1000 316336 Feb 20 07:01 rust_debugfs_scoped.ko
-rw-rw-r-- 1 1000 1000 251520 Feb 20 07:01 rust_dma.ko
-rw-rw-r-- 1 1000 1000 181072 Feb 20 07:01 rust_driver_auxiliary.ko
-rw-rw-r-- 1 1000 1000 136344 Feb 20 07:01 rust_driver_faux.ko
-rw-rw-r-- 1 1000 1000 214584 Feb 20 07:01 rust_driver_pci.ko
-rw-rw-r-- 1 1000 1000 209392 Feb 20 07:01 rust_driver_platform.ko
-rw-rw-r-- 1 1000 1000 151840 Feb 20 07:01 rust_minimal.ko
-rw-rw-r-- 1 1000 1000 198304 Feb 20 07:01 rust_misc_device.ko
-rw-rw-r-- 1 1000 1000 165424 Feb 20 07:01 rust_print.ko
[ 859.812741] rust_minimal: Rust minimal sample (init)
[ 859.812971] rust_minimal: Am I built-in? false
/test #
/test # insmod ./rust_driver_platform.ko
rust_driver_platform 12288 0 - Live 0xffffc75c46f8b000
rust_minimal 12288 0 - Live 0xffffc75c46f85000
rootfs on / type rootfs (rw,size=989308k,nr_inodes=247327)
devtmpfs on /dev type devtmpfs (rw,relatime,size=989308k,nr_inodes=247327,mode=755)
none on /proc type proc (rw,relatime)
none on /sys type sysfs (rw,relatime)
~ # mount -t 9p -o trans=virtio modules ./test2
~ # df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 989308 0 989308 0% /dev
modules 1921724544 1627240448 196792064 89% /test2
rootfs on / type rootfs (rw,size=989308k,nr_inodes=247327)
devtmpfs on /dev type devtmpfs (rw,relatime,size=989308k,nr_inodes=247327,mode=755)
none on /proc type proc (rw,relatime)
none on /sys type sysfs (rw,relatime)
modules on /test2 type 9p (rw,relatime,access=client,trans=virtio)
~ # cd test2/
/test2 # ls -la
total 2328
drwxrwxr-x 2 1000 1000 4096 Feb 21 07:02 .
drwxrwxr-x 12 1000 1000 300 Feb 21 07:03 ..
-rw-rw-r-- 1 1000 1000 241720 Feb 21 07:02 rust_configfs.ko
-rw-rw-r-- 1 1000 1000 280352 Feb 21 07:02 rust_debugfs.ko
-rw-rw-r-- 1 1000 1000 316336 Feb 21 07:02 rust_debugfs_scoped.ko
-rw-rw-r-- 1 1000 1000 251520 Feb 21 07:02 rust_dma.ko
-rw-rw-r-- 1 1000 1000 181072 Feb 21 07:02 rust_driver_auxiliary.ko
-rw-rw-r-- 1 1000 1000 136344 Feb 21 07:02 rust_driver_faux.ko
-rw-rw-r-- 1 1000 1000 214584 Feb 21 07:02 rust_driver_pci.ko
-rw-rw-r-- 1 1000 1000 209392 Feb 21 07:02 rust_driver_platform.ko
-rw-rw-r-- 1 1000 1000 151840 Feb 21 07:02 rust_minimal.ko
-rw-rw-r-- 1 1000 1000 198304 Feb 21 07:02 rust_misc_device.ko
-rw-rw-r-- 1 1000 1000 165424 Feb 21 07:02 rust_print.ko






























댓글 없음:
댓글 쓰기