在之前我曾在从0开始移植linux系统到树莓派4b这篇文章里使用busybox为树莓派创建了一套根文件系统,但是使用busybox构建的根文件系统实在是有点简陋,有很多命令和库都不包含,所以我们可以使用buildroot为它构建更加实用的根文件系统。
下载、配置buildroot
我使用的是buildroot-2024.02版本,可以在Buildroot - Making Embedded Linux Easy下载解压。
也可以使用git拉取代码
git clone --branch=2024.02 https://github.com/buildroot/buildroot --depth=1
cd buildroot/
# 树莓派基础配置
make raspberrypi4_64_defconfig
# 手动添加一些包,可以使用下面的命令进行图形化配置,完全自定义;也可以使用我下面的配置,配置了常用的一些工具以及qt6
make menuconfig
新建配置文件名为my_rasp_defconfig
,位置为buildroot/configs
目录下,内容为
BR2_aarch64=y
BR2_cortex_a72=y
BR2_ARM_FPU_VFPV4=y
BR2_PACKAGE_GLIBC_UTILS=y
BR2_GCC_VERSION_13_X=y
BR2_TOOLCHAIN_BUILDROOT_CXX=y
BR2_TOOLCHAIN_BUILDROOT_FORTRAN=y
BR2_GCC_ENABLE_OPENMP=y
BR2_GCC_ENABLE_GRAPHITE=y
BR2_TOOLCHAIN_GLIBC_GCONV_LIBS_COPY=y
BR2_TARGET_GENERIC_HOSTNAME="l1lpj26"
BR2_INIT_SYSTEMD=y
BR2_SYSTEM_BIN_SH_ZSH=y
BR2_SYSTEM_DHCP="end0"
BR2_SYSTEM_DEFAULT_PATH="/bin:/sbin:/usr/bin:/usr/sbin"
BR2_ENABLE_LOCALE_WHITELIST="zh_CN.UTF-8 en_US.UTF-8"
BR2_GENERATE_LOCALE="zh_CN.UTF-8 en_US.UTF-8"
BR2_SYSTEM_ENABLE_NLS=y
BR2_TARGET_LOCALTIME="Asia/Shanghai"
BR2_ROOTFS_POST_BUILD_SCRIPT="board/raspberrypi4-64/post-build.sh"
BR2_ROOTFS_POST_IMAGE_SCRIPT="board/raspberrypi4-64/post-image.sh"
BR2_PACKAGE_ALSA_UTILS=y
BR2_PACKAGE_FFMPEG=y
BR2_PACKAGE_MOTION=y
BR2_PACKAG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RMWARE_VARIANT_PI4=y
BR2_PACKAGE_RPI_FIRMWARE_CONFIG_FILE="board/raspberrypi4-64/config_4_64bit.txt"
BR2_PACKAGE_LUAJIT=y
BR2_PACKAGE_LUAJIT_COMPAT52=y
BR2_PACKAGE_PYTHON3=y
BR2_PACKAGE_PYTHON3_CURSES=y
BR2_PACKAGE_PYTHON_PIP=y
BR2_PACKAGE_LZ4_PROGS=y
BR2_PACKAGE_CA_CERTIFICATES=y
BR2_PACKAGE_LIBOPENSSL_BIN=y
BR2_PACKAGE_LIBOPENSSL_ENGINES=y
BR2_PACKAGE_LIBCURL_CURL=y
BR2_PACKAGE_LIBCURL_VERBOSE=y
BR2_PACKAGE_LIBCURL_WEBSOCKETS_SUPPORT=y
BR2_PACKAGE_UTF8PROC=y
BR2_PACKAGE_XUTIL_UTIL_MACROS=y
BR2_PACKAGE_IPERF3=y
BR2_PACKAGE_MJPG_STREAMER=y
BR2_PACKAGE_NET_TOOLS=y
BR2_PACKAGE_OPENSSH=y
BR2_PACKAGE_WGET=y
BR2_PACKAGE_WPA_SUPPLICANT=y
BR2_PACKAGE_WPA_SUPPLICANT_WIRED=y
BR2_PACKAGE_WPA_SUPPLICANT_AP_SUPPORT=y
BR2_PACKAGE_WPA_SUPPLICANT_WIFI_DISPLAY=y
BR2_PACKAGE_WPA_SUPPLICANT_AUTOSCAN=y
BR2_PACKAGE_WPA_SUPPLICANT_WPS=y
BR2_PACKAGE_WPA_SUPPLICANT_WPA3=y
BR2_PACKAGE_FILE=y
BR2_PACKAGE_NEOFETCH=y
BR2_PACKAGE_SCREEN=y
BR2_PACKAGE_SUDO=y
BR2_PACKAGE_TIME=y
BR2_PACKAGE_DOCKER_CLI=y
BR2_PACKAGE_DOCKER_CLI_STATIC=y
BR2_PACKAGE_DOCKER_COMPOSE=y
BR2_PACKAGE_DOCKER_ENGINE=y
BR2_PACKAGE_DOCKER_ENGINE_DRIVER_BTRFS=y
BR2_PACKAGE_HTOP=y
BR2_PACKAGE_TAR=y
BR2_PACKAGE_VIM=y
BR2_TARGET_ROOTFS_EXT2=y
BR2_TARGET_ROOTFS_EXT2_4=y
BR2_TARGET_ROOTFS_EXT2_SIZE="800M"
# BR2_TARGET_ROOTFS_TAR is not set
BR2_PACKAGE_HOST_DOSFSTOOLS=y
BR2_PACKAGE_HOST_GENIMAGE=y
BR2_PACKAGE_HOST_KMOD=y
BR2_PACKAGE_HOST_KMOD_XZ=y
BR2_PACKAGE_HOST_MTOOLS=y
在使用这个配置文件时,需要输入命令make my_rasp_defconfig
完成配置
编译生成根文件系统
# 编译过程中确保你的网络稳定,需要下载大量的编译器、库源码,整个过程大概1-2个小时
make -j12
如果使用我上面提供的配置文件进行编译,在编译完成之后可能需要你手动添加一些文件到board/raspberrypi4-64
的目录下,根据提示信息,在https://github.com/buildroot/buildroot/tree/master/board/raspberrypi位置下载对应的文件,只要在output/images/
目录下生成了rootfs.ext2
和rootfs.ext4
两者其一即可,没有必要编译出完整的sdcard.img
镜像文件;如果你有强迫症一定要成功编译完成,可以将编译内核时生成的bcm2711-rpi-4-b.dtb
,Image
,以及编译的u-boot.bin
文件放入output/images/
目录即可
bcm2711-rpi-4-b.dtb
位置:linux/arch/arm64/boot/dts/broadcom/bcm2711-rpi-4-b.dtb
Image
位置:linux/arch/arm64/boot/Image
u-boot.bin
位置:u-boot/u-boot.bin
写入sdcard
参考从0开始移植linux系统到树莓派4b中的格式化SD卡并创建分区这一节,将sd卡格式化为两个分区,在boot分区中放入启动相关的配置文件,u-boot.bin,设备树等文件,之前已经详细说明,就不再赘述。现在主要需要将buildroot构建的根文件系统写入。
# 查看自己的读卡器的设备名,我的是sdb,有两个分区,选择容量大的那一个,类型为Linux,我的是sdb2
sudo fdisk -l
# 先不要挂载sdb2分区,进入bulidroot文件夹的output/images/目录下,打开终端
sudo dd if=rootfs.ext4 of=/dev/sdb2 bs=4M
# 等待写入完成后,还需要执行下面的命令,不然整个系统可用的空间只有buildroot创建时选择根文件系统镜像的大小
sudo resize2fs /dev/sdb2
# 如果上面的命令没有成功执行,报错e2fsck has unsupported feature(s): FEATURE_C12
# 是因为e2fsprogs版本太低,我是通过下载最新的源码手动编译解决的
# 挂载rootfs,进入到编译完成linux内核目录
sudo env PATH=$PATH make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- INSTALL_MOD_PATH=/media/pjw6/rootfs modules_install
执行完毕后就可以弹出读卡器,插入树莓派并通电,需要使用串口进行调试,linux串口调试工具可以使用tabby,应该是可以正常启动的。登陆界面输入root回车即可进入shell。
系统配置
解决git不可用
git config --global http.sslBackend openssl
/boot目录未挂载
vim /etc/fstab
# 在文件末尾写入
/dev/mmcblk0p1 /boot auto defaults 0 0
配置网络(dhcp)
vim /etc/network/interfaces
# 写入
# --------------------------------------
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto end0
iface end0 inet dhcp
#---------------------------------------
vim /etc/systemd/network/end0.network
# 写入
# --------------------------------------
[Match]
Name=end0
[Network]
DHCP=ipv4
# --------------------------------------
配置网络(静态ip,根据你的实际内网ip进行修改)
vim /etc/network/interfaces
# 写入
# --------------------------------------
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto end0
iface end0 inet static
address 192.168.137.153
netmask 255.255.255.0
gateway 192.168.137.1
dns-nameservers 8.8.8.8
#---------------------------------------
vim /etc/systemd/network/end0.network
# 写入
# --------------------------------------
[Match]
Name=end0
[Network]
Address=192.168.137.153/24
Gateway=192.168.137.1
DNS=8.8.8.8
# --------------------------------------
配置ssh可使用root用户登录
vim /etc/ssh/sshd_config
# 修改这三个参数
PasswordAuthentication yes
PermitEmptyPasswords no
PermitRootLogin yes
设置root密码
passwd #设置自己的密码
设置完成后使用reboot
重启系统即可。
QT移植
宿主机编译qt
mkdir qt
cd qt
mkdir qthost
# 下载qt源码,并解压到qtbase文件夹中,不要删除源码
cd qtbase
cmake -GNinja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DINPUT_opengl=es2 -DQT_BUILD_EXAMPLES=OFF -DQT_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=/home/pjw6/Documents/qt/qthost
cmake --build . --parallel 8
cmake --install .
交叉编译qt
cd ..
mkdir qt-cross
cd qt-cross
vim toolcain.cmake
在toolcain.cmake
文件中写入,修改你的TARGET_SYSROOT
和CROSS_COMPILER
路径,对照我的路径在你的buildroot目录下找
cmake_minimum_required(VERSION 3.16)
include_guard(GLOBAL)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(TARGET_SYSROOT /home/pjw6/Documents/kernel/buildroot/output/host/aarch64-buildroot-linux-gnu/sysroot) # 修改这个路径为你使用buildroot编译的sysroot,一般为buildroot/output/host/aarch64-buildroot-linux-gnu/sysroot
set(CROSS_COMPILER /home/pjw6/Documents/kernel/buildroot/output/host/bin/aarch64-linux) # 替换这个为你的交叉编译器前缀,路径一般为buildroot/output/host/bin/
set(CMAKE_SYSROOT ${TARGET_SYSROOT})
set(CMAKE_C_COMPILER ${CROSS_COMPILER}-gcc)
set(CMAKE_CXX_COMPILER ${CROSS_COMPILER}-g++)
set(CMAKE_LIBRARY_ARCHITECTURE aarch64-linux-gnu)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIC -Wl,-rpath-link,${CMAKE_SYSROOT}/usr/lib -L${CMAKE_SYSROOT}/usr/lib/")
set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wl,-rpath-link,${CMAKE_SYSROOT}/usr/lib/-L${CMAKE_SYSROOT}/usr/lib/")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wl,-rpath-link,${CMAKE_SYSROOT}/usr/lib/ -L${CMAKE_SYSROOT}/usr/lib/")
set(QT_COMPILER_FLAGS "-march=armv8-a -mfpu=crypto-neon-fp-armv8 -mtune=cortex-a72 -mfloat-abi=hard")
set(QT_COMPILER_FLAGS_RELEASE "-O2 -pipe")
set(QT_LINKER_FLAGS "-Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_THREAD_LIBS_INIT "-lpthread")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(CMAKE_USE_WIN32_THREADS_INIT 0)
set(CMAKE_USE_PTHREADS_INIT 1)
set(THREADS_PREFER_PTHREAD_FLAG ON)
将之前的qtbase删除掉,重新解压一份到qtbase
dos2unix configure
./configure -release -platform linux-aarch64-gnu-g++ -device linux-aarch64-gnu-g++ -no-opengl -no-openssl -qt-sqlite -qt-zlib -qt-libjpeg -qt-libpng -qt-freetype -qt-pcre -qt-harfbuzz -qt-host-path /home/pjw6/Documents/qt/qt6host -prefix /home/pjw6/Documents/qt/qt-cross -- -DCMAKE_TOOLCHAIN_FILE=/home/pjw6/Documents/qt/qt-cross/toolcain.cmake -DINPUT_opengl=es2 -DQT_BUILD_EXAMPLES=OFF -DQT_BUILD_TESTS=OFF -DQT_FEATURE_eglfs_egldevice=ON -DQT_FEATURE_eglfs_gbm=ON
cmake --build . --parallel 8
cmake --install .
编译成功并且安装后,就可以在qt creator
中创建交叉编译环境,选择qt版本,添加,将/home/pjw6/Documents/qt/qt-cross/bin
下的qmake
选中,选择正确的交叉编译器即可,但是qmake可能有点问题,会出现链接库找不到,头文件找不到的问题。我们可以使用qt-cmake
来构建,只要配置好qt版本和编译器,可以在qt creator中成功构建,也可以在项目目录下使用终端输入/home/pjw6/Documents/qt/qt-cross/bin/qt-cmake
,然后make
即可成功编译
将编译好的qt文件转移到树莓派上,可以成功运行,但是可能会有系统编码问题
解决:
vim .zshrc
# 写入
export LC_ALL=zh_CN.UTF-8
# 写入后重启即可
reboot
评论区