准备工作
编译环境ubuntu 18.04,硬件设备树莓派3b,
编译环境前需要安装的包
crossbuild-essential-arm64
bison
flex
device-tree-compiler
pkg-config
ncurses-dev
libssl-dev
git
使用apt安装以上所有包
获取树莓派kernel代码
以4.14为例
git clone --depth 1 --branch rpi-4.14.y https://github.com/raspberrypi/linux.git
获取树莓派firmware地址
其中bootcode.bin start.elf是最小启动必要文件
编译指令
指定编译输出目录ob_4.14, -C内核代码目录,O需要全路径
make -C linux/ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- O=$......./ob_4.14 bcmrpi3_defconfig
make -C linux/ -j4 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- O=$......./ob_4.14
编译耗费一定时间这里先建一个目录存放boot文件
mkdir u-base-boot
编译完成,复制内核以及设备树到刚建立的目录
cp arch/arm64/boot/Image u-base-boot/kernel-4.14.y.img
cp arch/arm64/boot/dts/broadcom/bcm2710-rpi-3-b.dtb u-base-boot/
创建config.txt
//config.txt
# Serial console output!
enable_uart=1
# 64bit-mode
arm_64bit=1
# Uncomment below to use your custom named kernel
kernel=kernel-4.14.y.img
# For i2c & spi
dtparam=i2c_arm=on
dtparam=spi=on
# Enable audio (loads snd_bcm2835)
dtparam=audio=on
创建cmdline.txt
//cmd.txt
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait net.ifnames=0
net.ifnames=0参数作用将网卡命名方式从enx+[mac地址]变更为传统的eth0方式
到此你应该能得到以下这些文件
bootcode.bin
start.elf
fixup.dat
config.txt
cmdline.txt
kernel-4.14.y.img
bcm2710-rpi-3-b.dtb
复制这些文件到 u-base-boot目录待用
获取ubuntu-base版本地址
本次选择是ubuntu-base-18.10-base-arm64.tar.gz
创建一个目录存放rootfs
mkdir u-base-18.10-rootfs
wget http://cdimage.ubuntu.com/ubuntu-base/releases/18.10/release/ubuntu-base-18.10-base-arm64.tar.gz
sudo tar xzvf ubuntu-base-18.10-base-arm64.tar.gz
返回前面编译kernel的目录,安装kernel module和firmware到rootfs
sudo make -C linux/ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- O=$......./ob_4.14 modules_install INSTALL_MOD_PATH=u-base-18.10-rootfs
删除拷贝过去的build和source的ln
sudo find u-base-18.10-rootfs/lib/modules/ -name build |xargs sudo rm -rf
sudo find u-base-18.10-rootfs/lib/modules/ -name source |xargs sudo rm -rf
使用chroot配置rootfs,在x86环境需要安装qemu-user-static
sudo apt-get install qemu-user-static
如果是64bit的rootfs请使用qemu-aarch64-static,32位qemu-arm-static
sudo cp /usr/bin/qemu-aarch64-static u-base-18.10-rootfs/usr/bin/
复制编译机器的resolv.conf到rootfs下,联网需要
sudo cp -av /run/resolvconf/resolv.conf u-base-18.10-rootfs/etc/resolv.conf
替换更新源,本例选择的是阿里源
vi sources.list
deb http://mirrors.aliyun.com/ubuntu-ports/ cosmic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ cosmic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ cosmic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ cosmic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ cosmic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu-ports/ cosmic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu-ports/ cosmic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu-ports/ cosmic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu-ports/ cosmic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu-ports/ cosmic-backports main restricted universe multiverse
sudo cp sources.list u-base-18.10-rootfs/etc/apt/
chroot模拟环境配置rootfs
sudo chroot u-base-18.10-rootfs/
useradd -G sudo -m -s /bin/bash pi
echo pi:pi | chpasswd
passwd root
echo Ubuntu-RaspberryPi > /etc/hostname
echo 127.0.0.1 localhost > /etc/hosts
echo 127.0.0.1 Ubuntu-raspi >> /etc/hosts
安装最简包
apt-get install dialog perl
apt-get install locales
apt-get install sudo systemd kmod ifupdown net-tools ethtool udev wireless-tools iputils-ping resolvconf wget apt-utils wpasupplicant nano
编辑fstab,注意内容中的空白是tab不是空格
nano /etc/fstab
/dev/mmcblk0p2 / ext4 defaults,noatime 0 1
退出出模拟环境
exit
#为镜像分800M空间,后续通过resize2fs扩展即可
sudo dd if=/dev/zero of=ubuntu-raspi-image.img bs=800K count=1024
#镜像分区
sudo fdisk ubuntu-raspi-image.img
Device Boot Start End Sectors Size Id Type
ubuntu-raspi-image.img1 2048 133120 131073 64M c W95 FAT32 (LBA)
ubuntu-raspi-image.img2 135168 1638399 1503232 734M 83 Linux
sudo losetup -o $((512*2048)) --sizelimit $((512*133120)) /dev/loop14 ubuntu-raspi-image.img
sudo losetup -o $((512*135168)) --sizelimit $((512*1638399)) /dev/loop15 ubuntu-raspi-image.img
#格式化分区
sudo mkfs.vfat -F 32 -n boot /dev/loop14
sudo mkfs.ext4 -L root /dev/loop15
sudo mount /dev/loop14 /mnt/boot
sudo mount /dev/loop15 /mnt/rootfs
sudo cp -R boot-4.14/* /mnt/boot/
sudo cp -aR u-base-18.10-rootfs/* /mnt/rootfs/
sync
sudo umount /dev/loop14
sudo umount /dev/loop15
sudo losetup --detach /dev/loop14
sudo losetup --detach /dev/loop15