1*4882a593Smuzhiyun// -*- mode:doc; -*- 2*4882a593Smuzhiyun// vim: set syntax=asciidoc: 3*4882a593Smuzhiyun 4*4882a593Smuzhiyun[[rootfs-custom]] 5*4882a593Smuzhiyun=== Customizing the generated target filesystem 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunBesides changing the configuration through +make *config+, 8*4882a593Smuzhiyunthere are a few other ways to customize the resulting target filesystem. 9*4882a593Smuzhiyun 10*4882a593SmuzhiyunThe two recommended methods, which can co-exist, are root filesystem 11*4882a593Smuzhiyunoverlay(s) and post build script(s). 12*4882a593Smuzhiyun 13*4882a593SmuzhiyunRoot filesystem overlays (+BR2_ROOTFS_OVERLAY+):: 14*4882a593Smuzhiyun+ 15*4882a593SmuzhiyunA filesystem overlay is a tree of files that is copied directly 16*4882a593Smuzhiyun over the target filesystem after it has been built. To enable this 17*4882a593Smuzhiyun feature, set config option +BR2_ROOTFS_OVERLAY+ (in the +System 18*4882a593Smuzhiyun configuration+ menu) to the root of the overlay. You can even specify 19*4882a593Smuzhiyun multiple overlays, space-separated. If you specify a relative path, 20*4882a593Smuzhiyun it will be relative to the root of the Buildroot tree. Hidden 21*4882a593Smuzhiyun directories of version control systems, like +.git+, +.svn+, +.hg+, 22*4882a593Smuzhiyun etc., files called +.empty+ and files ending in +~+ are excluded from 23*4882a593Smuzhiyun the copy. 24*4882a593Smuzhiyun+ 25*4882a593SmuzhiyunWhen +BR2_ROOTFS_MERGED_USR+ is enabled, then the overlay must not 26*4882a593Smuzhiyun contain the '/bin', '/lib' or '/sbin' directories, as Buildroot will 27*4882a593Smuzhiyun create them as symbolic links to the relevant folders in '/usr'. In 28*4882a593Smuzhiyun such a situation, should the overlay have any programs or libraries, 29*4882a593Smuzhiyun they should be placed in '/usr/bin', '/usr/sbin' and '/usr/lib'. 30*4882a593Smuzhiyun+ 31*4882a593SmuzhiyunAs shown in xref:customize-dir-structure[], the recommended path for 32*4882a593Smuzhiyun this overlay is +board/<company>/<boardname>/rootfs-overlay+. 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunPost-build scripts (+BR2_ROOTFS_POST_BUILD_SCRIPT+):: 35*4882a593Smuzhiyun+ 36*4882a593SmuzhiyunPost-build scripts are shell scripts called 'after' Buildroot builds 37*4882a593Smuzhiyun all the selected software, but 'before' the rootfs images are 38*4882a593Smuzhiyun assembled. To enable this feature, specify a space-separated list of 39*4882a593Smuzhiyun post-build scripts in config option +BR2_ROOTFS_POST_BUILD_SCRIPT+ (in 40*4882a593Smuzhiyun the +System configuration+ menu). If you specify a relative path, it 41*4882a593Smuzhiyun will be relative to the root of the Buildroot tree. 42*4882a593Smuzhiyun+ 43*4882a593SmuzhiyunUsing post-build scripts, you can remove or modify any file in your 44*4882a593Smuzhiyun target filesystem. You should, however, use this feature with care. 45*4882a593Smuzhiyun Whenever you find that a certain package generates wrong or unneeded 46*4882a593Smuzhiyun files, you should fix that package rather than work around it with some 47*4882a593Smuzhiyun post-build cleanup scripts. 48*4882a593Smuzhiyun+ 49*4882a593SmuzhiyunAs shown in xref:customize-dir-structure[], the recommended path for 50*4882a593Smuzhiyun this script is +board/<company>/<boardname>/post_build.sh+. 51*4882a593Smuzhiyun+ 52*4882a593SmuzhiyunThe post-build scripts are run with the main Buildroot tree as current 53*4882a593Smuzhiyun working directory. The path to the target filesystem is passed as the 54*4882a593Smuzhiyun first argument to each script. If the config option 55*4882a593Smuzhiyun +BR2_ROOTFS_POST_SCRIPT_ARGS+ is not empty, these arguments will be 56*4882a593Smuzhiyun passed to the script too. All the scripts will be passed the exact 57*4882a593Smuzhiyun same set of arguments, it is not possible to pass different sets of 58*4882a593Smuzhiyun arguments to each script. 59*4882a593Smuzhiyun+ 60*4882a593SmuzhiyunIn addition, you may also use these environment variables: 61*4882a593Smuzhiyun 62*4882a593Smuzhiyun - +BR2_CONFIG+: the path to the Buildroot .config file 63*4882a593Smuzhiyun - +CONFIG_DIR+: the directory containing the .config file, and 64*4882a593Smuzhiyun therefore the top-level Buildroot Makefile to use (which is 65*4882a593Smuzhiyun correct for both in-tree and out-of-tree builds) 66*4882a593Smuzhiyun - +HOST_DIR+, +STAGING_DIR+, +TARGET_DIR+: see 67*4882a593Smuzhiyun xref:generic-package-reference[] 68*4882a593Smuzhiyun - +BUILD_DIR+: the directory where packages are extracted and built 69*4882a593Smuzhiyun - +BINARIES_DIR+: the place where all binary files (aka images) are 70*4882a593Smuzhiyun stored 71*4882a593Smuzhiyun - +BASE_DIR+: the base output directory 72*4882a593Smuzhiyun 73*4882a593SmuzhiyunBelow three more methods of customizing the target filesystem are 74*4882a593Smuzhiyundescribed, but they are not recommended. 75*4882a593Smuzhiyun 76*4882a593SmuzhiyunDirect modification of the target filesystem:: 77*4882a593Smuzhiyun+ 78*4882a593SmuzhiyunFor temporary modifications, you can modify the target filesystem 79*4882a593Smuzhiyun directly and rebuild the image. The target filesystem is available 80*4882a593Smuzhiyun under +output/target/+. After making your changes, run +make+ to 81*4882a593Smuzhiyun rebuild the target filesystem image. 82*4882a593Smuzhiyun+ 83*4882a593SmuzhiyunThis method allows you to do anything to the target filesystem, but if 84*4882a593Smuzhiyun you need to clean your Buildroot tree using +make clean+, these 85*4882a593Smuzhiyun changes will be lost. Such cleaning is necessary in several cases, 86*4882a593Smuzhiyun refer to xref:full-rebuild[] for details. This solution is therefore 87*4882a593Smuzhiyun only useful for quick tests: _changes do not survive the +make clean+ 88*4882a593Smuzhiyun command_. Once you have validated your changes, you should make sure 89*4882a593Smuzhiyun that they will persist after a +make clean+, using a root filesystem 90*4882a593Smuzhiyun overlay or a post-build script. 91*4882a593Smuzhiyun 92*4882a593SmuzhiyunCustom target skeleton (+BR2_ROOTFS_SKELETON_CUSTOM+):: 93*4882a593Smuzhiyun+ 94*4882a593SmuzhiyunThe root filesystem image is created from a target skeleton, on top of 95*4882a593Smuzhiyun which all packages install their files. The skeleton is copied to the 96*4882a593Smuzhiyun target directory +output/target+ before any package is built and 97*4882a593Smuzhiyun installed. The default target skeleton provides the standard Unix 98*4882a593Smuzhiyun filesystem layout and some basic init scripts and configuration files. 99*4882a593Smuzhiyun+ 100*4882a593SmuzhiyunIf the default skeleton (available under +system/skeleton+) does not 101*4882a593Smuzhiyun match your needs, you would typically use a root filesystem overlay or 102*4882a593Smuzhiyun post-build script to adapt it. However, if the default skeleton is 103*4882a593Smuzhiyun entirely different than what you need, using a custom skeleton may be 104*4882a593Smuzhiyun more suitable. 105*4882a593Smuzhiyun+ 106*4882a593SmuzhiyunTo enable this feature, enable config option 107*4882a593Smuzhiyun +BR2_ROOTFS_SKELETON_CUSTOM+ and set +BR2_ROOTFS_SKELETON_CUSTOM_PATH+ 108*4882a593Smuzhiyun to the path of your custom skeleton. Both options are available in the 109*4882a593Smuzhiyun +System configuration+ menu. If you specify a relative path, it will 110*4882a593Smuzhiyun be relative to the root of the Buildroot tree. 111*4882a593Smuzhiyun+ 112*4882a593SmuzhiyunCustom skeletons don't need to contain the '/bin', '/lib' or '/sbin' 113*4882a593Smuzhiyun directories, since they are created automatically during the build. 114*4882a593Smuzhiyun When +BR2_ROOTFS_MERGED_USR+ is enabled, then the custom skeleton must 115*4882a593Smuzhiyun not contain the '/bin', '/lib' or '/sbin' directories, as Buildroot 116*4882a593Smuzhiyun will create them as symbolic links to the relevant folders in '/usr'. 117*4882a593Smuzhiyun In such a situation, should the skeleton have any programs or 118*4882a593Smuzhiyun libraries, they should be placed in '/usr/bin', '/usr/sbin' and 119*4882a593Smuzhiyun '/usr/lib'. 120*4882a593Smuzhiyun+ 121*4882a593SmuzhiyunThis method is not recommended because it duplicates the entire 122*4882a593Smuzhiyun skeleton, which prevents taking advantage of the fixes or improvements 123*4882a593Smuzhiyun brought to the default skeleton in later Buildroot releases. 124*4882a593Smuzhiyun 125*4882a593SmuzhiyunPost-fakeroot scripts (+BR2_ROOTFS_POST_FAKEROOT_SCRIPT+):: 126*4882a593Smuzhiyun+ 127*4882a593SmuzhiyunWhen aggregating the final images, some parts of the process requires 128*4882a593Smuzhiyun root rights: creating device nodes in `/dev`, setting permissions or 129*4882a593Smuzhiyun ownership to files and directories... To avoid requiring actual root 130*4882a593Smuzhiyun rights, Buildroot uses +fakeroot+ to simulate root rights. This is not 131*4882a593Smuzhiyun a complete substitute for actually being root, but is enough for what 132*4882a593Smuzhiyun Buildroot needs. 133*4882a593Smuzhiyun+ 134*4882a593SmuzhiyunPost-fakeroot scripts are shell scripts that are called at the 'end' of 135*4882a593Smuzhiyun the fakeroot phase, 'right before' the filesystem image generator is 136*4882a593Smuzhiyun called. As such, they are called in the fakeroot context. 137*4882a593Smuzhiyun+ 138*4882a593SmuzhiyunPost-fakeroot scripts can be useful in case you need to tweak the 139*4882a593Smuzhiyun filesystem to do modifications that are usually only available to the 140*4882a593Smuzhiyun root user. 141*4882a593Smuzhiyun+ 142*4882a593Smuzhiyun.Note: 143*4882a593SmuzhiyunIt is recommended to use the existing mechanisms to set file permissions 144*4882a593Smuzhiyun or create entries in `/dev` (see xref:customize-device-permission[]) or 145*4882a593Smuzhiyun to create users (see xref:customize-users[]) 146*4882a593Smuzhiyun+ 147*4882a593Smuzhiyun.Note: 148*4882a593SmuzhiyunThe difference between post-build scripts (above) and fakeroot scripts, 149*4882a593Smuzhiyun is that post-build scripts are not called in the fakeroot context. 150*4882a593Smuzhiyun+ 151*4882a593Smuzhiyun.Note: 152*4882a593SmuzhiyunUsing `fakeroot` is not an absolute substitute for actually being root. 153*4882a593Smuzhiyun `fakeroot` only ever fakes the file access rights and types (regular, 154*4882a593Smuzhiyun block-or-char device...) and uid/gid; these are emulated in-memory. 155*4882a593Smuzhiyun 156*4882a593Smuzhiyuninclude::customize-device-permission-tables.txt[] 157