Overview
- Get the final files into a directory eg.
target
and treat it as root(/
) directory of target board/system. - Generate users eg.
root
,admin
,ssh
, etc - Generate device lists
- Clean few files
- Generate ext image using
mkfs
Taking reference from buildroot common.mk
and ext2.mk
scripts/makedevs -d output/build/buildroot-fs/full_devices_table.txt output/build/buildroot-fs/ext2/target
mkfs.ext4 -d output/build/buildroot-fs/ext2/target -r 1 -N 0 -m 5 -L rootfs -O ^64bit output/images/rootfs.ext2 2G
# Fakeroot execution complete
/opt/rk3588_nvrx/host/sbin/resize2fs -M output/images/rootfs.ext2
/opt/rk3588_nvrx/host/sbin/e2fsck -fy /output/images/rootfs.ext2
/opt/rk3588_nvrx/host/sbin/resize2fs -M /output/images/rootfs.ext2
Fakeroot Script
#!/bin/sh
set -ex
chown -h -R 0:0 $(TARGET)
$(HOST_DIR)/bin/makedevs -d full_devices_table.txt $(TARGET)
$(HOST_DIR)/sbin/mkfs.ext4 -d $(TARGET) -r 1 -N 0 -m 5 -L "rootfs" -O ^64bit rootfs.ext2 "2G"
Post EXT4
$(HOST_DIR)/sbin/resize2fs -M rootfs.ext2
$(HOST_DIR)/sbin/e2fsck -fy rootfs.ext2
$(HOST_DIR)/sbin/tune2fs -m 5 rootfs.ext2
$(HOST_DIR)/sbin/resize2fs -M rootfs.ext2
Testing on Host
sudo mount -o loop path/to/rootfs.ext4 /tmp/fs_path
Makedevs
Creates a batch of special files as specified in a device table.
Full Device Table
/bin/busybox f 4755 0 0 - - - - -
/usr/bin/fusermount f 4755 0 0 - - - - -
/usr/bin/fusermount3 f 4755 0 0 - - - - -
/var/empty d 755 root root - - - - -
# See package/makedevs/README for details
#
# This device table is used to assign proper ownership and permissions
# on various files. It doesn't create any device file, as it is used
# in both static device configurations (where /dev/ is static) and in
# dynamic configurations (where devtmpfs, mdev or udev are used).
#
# <name> <type> <mode> <uid> <gid> <major> <minor> <start> <inc> <count>
/dev d 755 0 0 - - - - -
/tmp d 1777 0 0 - - - - -
/etc d 755 0 0 - - - - -
/root d 700 0 0 - - - - -
/var/www d 755 33 33 - - - - -
/etc/shadow f 600 0 0 - - - - -
/etc/passwd f 644 0 0 - - - - -
/etc/network/if-up.d d 755 0 0 - - - - -
/etc/network/if-pre-up.d d 755 0 0 - - - - -
/etc/network/if-down.d d 755 0 0 - - - - -
/etc/network/if-post-down.d d 755 0 0 - - - - -
# uncomment this to allow starting x as non-root
#/usr/X11R6/bin/Xfbdev f 4755 0 0 - - - - -
Mkusers
Full Users Table
sshd -1 sshd -1 * /var/empty - - SSH drop priv user
- - input -1 * - - - Input device group
- - render -1 * - - - DRI rendering nodes
- - kvm -1 * - - - kvm nodes
Fakeroot
mke2fs (mkfs.ext4)
resize2fs
e2fsck
tune2fs
PatchELF
- For more info: https://github.com/NixOS/patchelf
- PatchELF is a simple utility for modifying existing ELF executables and libraries
$HOST_PATH/bin/patchelf --make-rpath-relative --no-standard-lib-dirs FILE_NAME
Tarball creation
cd output/target; find -print0 | LC_ALL=C sort -z | tar --pax-option=exthdr.name=%%d/PaxHeaders/%%f,atime:=0,ctime:=0 -cf output/images/rootfs.tar --null --xattrs-include='\''*'\'' --no-recursion -T - --numeric-owner
xz -9 -C crc32 -c -T 2 buildroot-2023.02/output/images/rootfs.tar > output/images/rootfs.tar.xz
This will efficiently store sparse files extracted from a tarball
tar xf archive.tar -S
TODO
- Study
mkuser
script of buildroot and make your own - fallocate -d
- fusermount
- pivotroot
- Buildroot strips all the binary, object files before making ext image: it removes rpath (local host_path) from elf files using
patchelf
References
- what-is-a-sparse-file-and-why-do-we-need-it: https://stackoverflow.com/questions/43126760/what-is-a-sparse-file-and-why-do-we-need-it
- https://superuser.com/questions/605196/how-to-create-ext2-image-without-superuser-rights
- https://cirosantilli.com/linux-kernel-module-cheat/#buildroot-hello-world
- QEMU: https://github.com/buildroot/buildroot/blob/2024.08.1/board/qemu/aarch64-virt/readme.txt