foreword
-
You can refer to ubuntu 20.04 qemu linux6.0.1 to make ext4 root file system
-
Because the glib library under the aarch64 platform needs to be verified, the test environment for Linux qemu arm64 has been rebuilt
-
In this article, start to make the rootfs root file system, let the Linux system on the qemu arm64 platform run
development environment
-
win10 64位 VMware Workstation Pro 16
-
ubuntu 20.04
-
qemu (virtual ARM development board), virt arm64 platform
-
Linux 6.3.8
Download and unzip busybox
-
Currently using busybox, you can quickly create common commands required by the Linux root file system, so here is the latest version of busybox:
busybox-1.36.1.tar.bz2
-
The official download address of busybox: , you can download the latest version
https://busybox.net/downloads/busybox-1.36.1.tar.bz2
directly from the official websitehttps://busybox.net/downloads/
-
Put the downloaded busybox in ubuntu and unzip it
$ ls
busybox-1.36.1.tar.bz2
$ tar xjvf busybox-1.36.1.tar.bz2
compile busybox
-
linux gcc
It is emphasized here that if one-time compilation is required, a cross-compilation toolchain is required . For example,aarch64-linux-gnu-xxx
in such a toolchain, Linux-related header files are integrated. -
Compile
busybox-1.36.1
, the recommended linux gcc is:gcc-linaro-12.2.1-2023.04-x86_64_aarch64-linux-gnu.tar.xz
-
Here is the qemu emulator, you can compile it directly, you don’t need menuconfig to manually modify the configuration for the time being
-
The compilation command is as follows:
/* menuconfig 配置,直接保存即可 */
$ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig
/* 创建 busybox 输出的路径 */
$ mkdir /home/zhangsz/linux/rootfs/1016
/* 编译, install,这样就可以把 busybox 的编译产物放在CONFIG_PREFIX的路径下 */
$ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- CONFIG_PREFIX=/home/zhangsz/linux/rootfs/1016/ install
The product compiled by busybox:
$ cd /home/zhangsz/linux/rootfs/1016/
$ ls
bin linuxrc sbin usr
- Note that the product directly compiled by busybox still lacks a lot of lib libraries and commands, which need to be continuously enriched. Some things related to the Linux kernel startup console need to be supplemented
perfect rootfs
-
First create the rootfs directory, and then copy all busybox products to rootfs
-
First create a new directory under rootfs , copy the library files under
lib
the gcc cross-compilation toolchain to the directorylibc/lib/
rootfs/lib
-
sudo cp -rf ../../tools/gcc-linaro-12.2.1-2023.04-x86_64_aarch64-linux-gnu/aarch64-linux-gnu/libc/lib/* rootfs/lib
-
Execute the generated
modules_install
command in the Linux kernel directory: -
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- INSTALL_MOD_PATH=../_install modules_install
-
lib
Then copy all the files in the Linux module directory torootfs/lib
the following
make root file system
-
The process of making the Linux root file system directory is actually similar for each platform. For different platforms, choose the corresponding compiler.
-
Here we use a script to quickly create a root file system directory template, and then copy the compiled product of busybox and the lib in the gcc cross-compilation environment to package it into a customized file system image
-
create_rootfs.sh
Generate each directory structure of rootfs
#!/bin/bash
echo "------Create rootfs start...--------"
rootfs=$1
sudo rm -rfv $rootfs
sudo mkdir -v $rootfs
cd $rootfs
echo "--------Create root,dev....----------"
sudo mkdir root dev etc boot tmp var sys proc lib mnt home usr
sudo mkdir etc/init.d etc/rc.d
echo "make node in dev/console dev/null"
sudo mknod -m 600 dev/console c 5 1
sudo mknod -m 600 dev/null c 1 3
# create etc config /etc/inittab
echo -e "::sysinit:/etc/init.d/rcS " >etc/inittab
echo -e "::askfirst:-/bin/sh " >>etc/inittab
echo -e "::ctrlaltdel:/sbin/reboot " >>etc/inittab
echo -e "::shutdown:/bin/umount -a -r " >>etc/inittab
# create etc config /etc/init.d/rcs
echo -e "#! /bin/sh " >etc/init.d/rcS
echo -e "mount -t sysfs none /sys " >>etc/init.d/rcS
echo -e "mount -t proc none /proc " >>etc/init.d/rcS
echo -e "mount -t tmpfs tmpfs /tmp" >>etc/init.d/rcS
echo -e "mdev -s " >>etc/init.d/rcS
chmod +x etc/init.d/rcS
# create etc config /etc/fstab
echo -e "proc /proc proc defaults 0 0 " >etc/fstab
echo -e "sysfs /sys sysfs defaults 0 0 " >>etc/fstab
echo -e "devtmpfs /dev devtmpfs defaults 0 0 " >>etc/fstab
echo -e "tmpfs /tmp tmpfs defaults 0 0 " >>etc/fstab
echo -e "tmpfs /var tmpfs defaults 0 0 " >>etc/fstab
cd ..
echo "-------make rootfs done---------"
make_boot.sh
Script: make rootfs ext4 mirror
#!/bin/bash
echo "---------- make boot.img ------------"
img_ext=".img"
img_mnt="_mnt"
boot_img=$1${
img_ext}
boot_img_mnt=$1${
img_mnt}
echo ${
boot_img}
echo ${
boot_img_mnt}
sudo rm -rf ${
boot_img}
sudo rm -rf ${
boot_img_mnt}
dd if=/dev/zero of=${
boot_img} bs=1M count=1024
mkfs.ext4 ${
boot_img}
sudo mkdir ${
boot_img_mnt}
sudo mount ${
boot_img} ${
boot_img_mnt}
sudo cp -rv $1/* ${boot_img_mnt}
sudo cp -rv rootfs/* ${boot_img_mnt}
sudo umount ${boot_img_mnt}
echo "------------ make boot.img end ---------"
boot_qemu.sh
:qemu-system-aarch64
start qemu via
#!/bin/bash
echo "---------- boot qemu ----------"
echo $1
sudo qemu-system-aarch64 \
-M virt \
-cpu cortex-a57 \
-smp 4 \
-m 4G \
-kernel Image.gz \
-drive format=raw,file=$1 \
-nographic \
-append "noinitrd root=/dev/vda rw console=ttyAMA0 init=/linuxrc ignore_loglevel" \
run.sh
: To run qemu, you can run it for the first time , and you can start itsudo ./run.sh
later bysudo ./boot_qemu.sh rootfs_qemu.img
#!/bin/bash
source create_rootfs.sh rootfs_qemu
source make_boot.sh rootfs_qemu
source boot_qemu.sh rootfs_qemu.img
start qemu
-
Copy the Linux kernel, currently
linux-6.3.8
the kernel image file:linux-6.3.8/arch/arm64/boot/Image.gz
to the startup directory of qemu -
The directory where qemu starts is as follows:
- The above scripts for making rootfs and starting qemu, the Linux kernel image
Image.gz
, and the one created aboverootfs
, based on thisrootfs
to generate the ext4 image required by qemu
-rwxr-xr-x 1 zhangsz zhangsz 292 6月 18 17:22 boot_qemu.sh
-rwxr-xr-x 1 zhangsz zhangsz 1335 6月 18 12:39 create_rootfs.sh
-rw-r--r-- 1 zhangsz zhangsz 13141574 6月 18 12:37 Image.gz
-rwxr-xr-x 1 zhangsz zhangsz 517 6月 18 12:39 make_boot.sh
-rw-r--r-- 1 zhangsz zhangsz 463 6月 18 18:05 readme.md
drwxr-xr-x 7 zhangsz zhangsz 4096 6月 18 18:00 rootfs
-rwxr-xr-x 1 zhangsz zhangsz 118 6月 18 12:39 run.sh
-
The first run: the image will be made according to the rootfs
rootfs_qemu.img
, and the qemu startup script will be calledboot_qemu.sh
to start. The execution command is:sudo ./run.sh
-
The startup information is as follows:
- The process Linux shell command line, the Linux ext4 root file system based on the qemu arm64 platform, runs correctly
summary
-
The above is based on the linux6.3.8 qemu arm64 platform to make the ext4 root file system and compile the Linux kernel. Refer to the ubuntu 20.04 qemu arm64 linux6.3.8 development environment to build
-
qemu can be used for some technical evaluation and function verification in the early stage of embedded Linux system development, especially the function development verification of some hardware platform-independent software. Using qemu to simulate and run will greatly improve the efficiency of development
-
Currently, qemu can not only run Linux, but also run on systems such as rt-smart