How to Add User Files to Buildroot Image on OK3562 with Linux 5.10.198

1. Introduction

This article provides a method to include files, programs, applications, libraries, etc. required by user products into the source code and then compile them into the image. This can avoid the operation of separately copying programs to the development board after flashing the image.

2. Modification Methods

2.1 Method 1: Directly Put into Source Code for Compilation

Placement Path Explanation:

User programs can be copied to the directory

/OK3562linuxsource/buildroot/board/forlinx/ok3562/fsoverlay/usr/bin.

Configuration files can be copied to the directory

/OK3562linuxsource/buildroot/board/forlinx/ok3562/fsoverlay/etc.

Library files can be copied to the directory

/OK3562linuxsource/buildroot/board/forlinx/ok3562/fsoverlay/lib.

Users can also create their own folders in the

/OK3562linuxsource/buildroot/board/forlinx/ok3562/fsoverlay/ directory to store their files.

Take the example of copying the compressed package

aarch64buildrootlinuxgnu_sdkbuildroot.tar.gz:

(1) Copy the file to the specified path

Create a directory named test under the path

/OK3562linuxsource/buildroot/board/forlinx/ok3562/fsoverlay/, and put the compressed package aarch64buildrootlinuxgnu_sdkbuildroot.tar.gz into this directory.

The Method of Adding User Files to the Buildroot Image on OK3562 5.10.198

(2) Compile the source code

Execute the fullcompilation command ./build.sh all. After compilation, you can see that the test directory is generated in OK3562linuxfs/rootfs, which contains the compressed package.

The Method of Adding User Files to the Buildroot Image on OK3562 5.10.198

(3) Verification on the development board

After booting the development board with the compiled update.img, it is found that the test directory exists in the file system.

The Method of Adding User Files to the Buildroot Image on OK3562 5.10.198

In conclusion, the test is successful.

This method is permanently effective. You only need to perform the operation once. Later, when you modify the device tree or driver configuration in the source code and recompile the image, the added files will still be included.

(4) Deleting the added files

When deleting, note that in addition to deleting the files added in /OK3562linuxsource/buildroot/board/forlinx/ok3562/fsoverlay/, you also need to check if there are any remaining files in /OK3562linuxsource/buildroot/output. For example, during the compilation of the abovementioned compressed package,
test/aarch64buildrootlinuxgnu_sdkbuildroot.tar.gz is also created in /OK3562linuxsource/buildroot/output/OK3562_Linux/target/, and these files also need to be deleted manually.

Recompilation is required after deletion.

2.2 Method 2: Mount rootfs.ext2

If the source code has been compiled, or you only want to temporarily add files to a single image, you can use the method of mounting rootfs.ext2.

Path of rootfs.ext2: OK3562linuxsource/buildroot/output/OK3562_Linux/images

(1) Mount rootfs.ext2 and transfer files into it

forlinx@ubuntu:~/work/OK3562-linux-source$ cd buildroot/output/OK3562_Linux/images
forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images$ ls
rootfs.cpio  rootfs.cpio.gz  rootfs.ext2  rootfs.ext4  rootfs.squashfs  rootfs.tar
forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images$ mkdir rootfs
forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images$ sudo mount rootfs.ext2 rootfs
[sudo] forlinx password: 
forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images$ cd rootfs/
forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images/rootfs$ ls
bin  busybox.fragment  dev  etc  home  info  lib  lib64  linuxrc  lost+found  media  mnt  oem  opt  proc  root  run  sbin  sys  test  tmp  userdata  usr  var
forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images/rootfs$ sudo cp /mnt/hgfs/share/aarch64-buildroot-linux-gnu_sdk-buildroot.tar.gz ./
forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images/rootfs$ cd ..
forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images$ sudo umount rootfs

In conclusion, files can be directly added to the compiled image.

Note: The default size of rootfs.ext2 is 1.7G. If you use this method to add files that are too large, you will get an error: cp: Error writing './aarch64-buildroot-linux-gnu_sdk-buildroot.tar.gz' : No space on device. You can expand rootfs.ext2 using the following commands before adding files. Note that you need to unmount rootfs.ext2 before executing these commands:

forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images$ dd if=/dev/zero bs=1M count=0 seek=2000 of=rootfs.ext2		// The command does not write any data to rootfs.ext2, but simply extends the size of the file to 2G.
0+0 records in
0+0 records out
0 bytes copied, 0.000348683 s, 0.0 kB/s
forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images$ e2fsck -f rootfs.ext2
e2fsck 1.46.5 (30-Dec-2021)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory structure
Pass 4: Checking reference counts
Pass 5: Checking group summary information
rootfs: 9137/70128 files (0.1% non-contiguous), 249143/280320 blocks
forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images$ resize2fs rootfs.ext2
resize2fs 1.46.5 (30-Dec-2021)
Resize the file system on rootfs.ext2 to 512,000 blocks (4k per block).
The file system on rootfs.ext2 is now 512,000 blocks (4k per block).
forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images$ ls -lh
Total 3.5G
drwxrwxr-x 2 forlinx forlinx 4.0K  1月  7 09:30 rootfs
-rw-r--r-- 1 forlinx forlinx 906M  1月  7 11:13 rootfs.cpio
-rw-r--r-- 1 forlinx forlinx 385M  1月  7 11:15 rootfs.cpio.gz
-rw-r--r-- 1 forlinx forlinx 2.0G  1月  7 11:21 rootfs.ext2
lrwxrwxrwx 1 forlinx forlinx   11  1月  7 11:15 rootfs.ext4 -> rootfs.ext2
drwxrwxr-x 2 forlinx forlinx 4.0K  1月  7 10:02 rootfs_old
-rw-r--r-- 1 forlinx forlinx 383M  1月  7 11:15 rootfs.squashfs
-rw-r--r-- 1 forlinx forlinx 920M  1月  7 11:16 rootfs.tar
forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images$ sudo mount rootfs.ext2 rootfs
forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images$ cd rootfs/
forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images/rootfs$ sudo cp /mnt/hgfs/share/aarch64-buildroot-linux-gnu_sdk-buildroot.tar.gz ./
forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images/rootfs$ cd ..
forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images$ sudo umount rootfs

(2) Update the image

forlinx@ubuntu:~/work/OK3562-linux-source/buildroot/output/OK3562_Linux/images$ cd ../../../../
forlinx@ubuntu:~/work/OK3562-linux-source$ ./build.sh updateimg

(3) Verification on the development board

Development board verification

The Method of Adding User Files to the Buildroot Image on OK3562 5.10.198

In conclusion, the test is successful.

This method is only valid once. If the source code is recompiled later, the modification fails and needs to be operated again.




Dear friends, we have created an exclusive embedded technical exchange group on Facebook, where our experts share the latest technological trends and practical skills. Join us and grow together!