记录:Linux交叉编译arm64内核

准备材料

  • 系统:ubuntu 18.04
  • 源码:linux 5.4.xxx
    地址:https://github.com/unifreq/linux-5.4.y
  • x86编译工具: clang
    地址:https://github.com/llvm/llvm-project/releases
  • 交叉编译工具:gcc-linaro
    地址:https://releases.linaro.org/components/toolchain/binaries/latest-7/aarch64-linux-gnu/
    新版本:https://snapshots.linaro.org/gnu-toolchain/11.0-2021.03-1/aarch64-linux-gnu/
  • arm64 根系统
    地址:https://mirrors.tuna.tsinghua.edu.cn/armbian-releases/_rootfs/

一、安装依赖

sudo apt install -y gcc make libncurses5-dev build-essential pkg-config libc6-dev bison flex libelf-dev \
 lz4 qemu-user-static bc libssl-dev

二、下载编译工具及源码

cd ~
git clone --depth 1 https://github.com/unifreq/linux-5.4.y
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.1/clang+llvm-12.0.1-x86_64-linux-gnu-ubuntu-16.04.tar.xz
wget https://releases.linaro.org/components/toolchain/binaries/latest-7/aarch64-linux-gnu/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu.tar.xz
wget https://mirrors.tuna.tsinghua.edu.cn/armbian-releases/_rootfs/bullseye-minimal-arm64.4be3a094cff25025c3f5058d796959bf.tar.lz4

三、配置编译环境

sudo mkdir /opt/bullseye-rootfs
lz4 -d bullseye-minimal-arm64.4be3a094cff25025c3f5058d796959bf.tar.lz4 | sudo tar -xf - -C /opt/bullseye-rootfs
sudo tar -xf clang+llvm-12.0.1-x86_64-linux-gnu-ubuntu-16.04.tar.xz -C /opt
sudo tar -xf gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu.tar.xz -C /opt
sudo mv /opt/clang+llvm-12.0.1-x86_64-linux-gnu-ubuntu- /opt/clang
sudo mv /opt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu /opt/aarch64-linux-gnu
#sudo 不用输入密码
sudo sh -c "echo 'liangjc ALL=(ALL:ALL) NOPASSWD: ALL' >> /etc/sudoers.d/liangjc"

四、编译内核

cd linux-5.4.y
#从arm64设备上找到“config”配置文件并复制到本地“.config”
scp [email protected]:/boot/config-5.4.142-liangjc-03+ .config
export ARCH=arm64
export CROSS_COMPILE=/opt/aarch64-linux-gnu/bin/aarch64-linux-gnu-
export CC=/opt/clang/bin/clang
export LD=/opt/clang/bin/ld.lld
#自定义内核
make -j$(nporc) CC=$CC LD=$LD menuconfig
#开始编译
make -j$(nporc) CC=$CC LD=$LD

五、制作initrd和uinitrd

sudo make modules_install
sudo mv /lib/modules/5.4.142-liangjc-03+ /opt/bullseye-rootfs/lib/modules
sudo mkdir /opt/bullseye-rootfs/{proc,sys,host}

sudo vim /opt/chmout.sh
#!/bin/bash  
function mnt() {
    echo "MOUNTING"
    sudo mount -t proc /proc ${2}proc
    sudo mount -t sysfs /sys ${2}sys
    sudo mount -o bind /dev ${2}dev
    sudo mount -o bind /run ${2}run
    sudo mount --bind / ${2}host
    sudo chroot ${2}
}
function umnt() {
    echo "UNMOUNTING"
    sudo umount ${2}proc
    sudo umount ${2}sys
    sudo umount ${2}dev
    sudo umount ${2}run
    sudo umount ${2}host
}
if [ "$1" == "-m" ] && [ -n "$2" ] ;then
    mnt $1 $2
elif [ "$1" == "-u" ] && [ -n "$2" ];then
    umnt $1 $2
else
    echo ""
    echo "Either 1'st, 2'nd or both parameters were missing"
    echo ""
    echo "1'st parameter can be one of these: -m(mount) OR -u(umount)"
    echo "2'nd parameter is the full path of rootfs directory(with trailing '/')"
    echo ""
    echo "For example: ./chmount.sh -m /media/sdcard/"
    echo ""
fi

#虚拟arm64系统
sudo cp /usr/bin/qemu-arm-static /opt/bullseye-rootfs/usr/bin/
sudo cp /usr/bin/qemu-aarch64-static /opt/bullseye-rootfs/usr/bin/
sudo chmod +x /opt/chmout.sh
/opt/chmout.sh -m /opt/bullseye-rootfs
  cp /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime
  mkinitramfs -k -c xz -o /boot/initrd.img-5.4.142-liangjc-03+ 5.4.142-liangjc-03+
  mkimage -A arm64 -O linux -T ramdisk -C none -a 0 -e 0 -n uInitrd -d /boot/initrd.img-5.4.142-liangjc-03+ /boot/uInitrd-5.4.142-liangjc-03+
  exit
/opt/chmout.sh -u /opt/bullseye-rootfs

六、打包

#复制相关文件
mkdir -p output{boot,dtb,modules}
cp -a /opt/bullseye-rootfs/lib/modules/5.4.142-liangjc-03+ output/modules
cp -a /opt/bullseye-rootfs/boot/{initrd.img-5.4.142-liangjc-03+,uInitrd-5.4.142-liangjc-03+} output/boot/
cp -a arch/arm64/boot/Image output/boot/zImage-5.4.142-liangjc-03+
cp -a .config output/boot/config-5.4.142-liangjc-03+
cp -a System.map output/boot/System.map-5.4.142-liangjc-03+
cp -a arch/arm64/boot/dts/amlogic/*.dtb output/dtb
#压缩打包
cd output/boot
tar -zcf ../boot-5.4.142-liangjc-03+.tar.gz .
cd ../dtb
tar -zcf ../dtb-5.4.142-liangjc-03+.tar.gz .
cd ../modules
tar -zcf ../modules-5.4.142-liangjc-03+.tar.gz .
  • 至此编译打包完成,把生成的三个tar.gz压缩包复制到arm64设备中进行覆盖升级。

附录:clang交叉编译

wget https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.1/clang+llvm-12.0.1-aarch64-linux-gnu.tar.xz
sudo tar -xf clang+llvm-12.0.1-aarch64-linux-gnu.tar.xz -C /opt
sudo mv clang+llvm-12.0.1-aarch64-linux-gnu clang-aarch64-linux-gnu
#自定义内核模块
make -j$(nproc) ARCH=arm64 \
CROSS_COMPILE=/opt/aarch64-linux-gnu/bin/aarch64-linux-gnu- \
CLANG_TRIPLE=/opt/clang-aarch64-linux-gnu/bin/ \
CC=/opt/clang/bin/clang LD=/opt/clang/bin/ld.lld \
menuconfig
#编译
make -j$(nproc) ARCH=arm64 \
CROSS_COMPILE=/opt/aarch64-linux-gnu/bin/aarch64-linux-gnu- \
CLANG_TRIPLE=/opt/clang-aarch64-linux-gnu/bin/ \
CC=/opt/clang/bin/clang LD=/opt/clang/bin/ld.lld

你可能感兴趣的:(记录:Linux交叉编译arm64内核)