Ji Kangzi asked Confucius about politics. Confucius said to him, “A politician is a man of integrity. Son handsome with positive, which dare not straight?” The Analects of Confucius: Yan Yuan

A hundred blog series. This is:

V66. Xx HongMeng kernel source code analysis (the root file system) | on first/File system on

File system related sections are as follows:

  • V62. Xx HongMeng kernel source code analysis concept (file) | why everything is the file
  • V63. Xx HongMeng kernel source code analysis (file system) | said in books management file system
  • V64. Xx HongMeng kernel source code analysis (inode) | who is the most important concept of file system
  • V65. Xx HongMeng kernel source code analysis (mount directory) | why need to mount the file system
  • V66. Xx HongMeng kernel source code analysis (the root file system) | on first/File system on
  • V67. Xx HongMeng kernel source code analysis (character device) | bytes read/write device for the unit
  • V68. Xx HongMeng kernel source code analysis file system (VFS) | the foundation of the harmonious coexistence
  • V69. Xx HongMeng kernel source code analysis (file handle) | why do you call a handle?
  • V70. Xx HongMeng kernel source code analysis (pipe file) | how to reduce the data flow cost

The FHS | file system hierarchy standard

  • Mentioned in the kernel [mounted directory article] to compatible with the file system’s difference, raises the concept of a directory tree, the tree is like building blocks pieced together by each file system, any file system only need to mount on a directory to butt in, kernel abstracts the mount of unified interface, the file system to implement these interfaces. Since the directory is so important, it needs to be standardized management, UNIx-like follow the FHS specification, hongmeng also follow.

  • Filesystem Hierarchy Standard (FHS) defines the main directories and contents in the Linux operating system. FHS is maintained by the Linux Foundation. The current version is version 3.0 and was released in 2015. The basic directories are as follows:

    /root directory /home home folder /etc The main configuration files of the system are stored in this directory. /root Home folder of the system administrator /bin Can be used by root and common accounts. /sbin These commands are root only /opt /dev for installing third-party applications. Any devices and interface devices are stored in this directory as files. /proc A virtual filesystem. /media is a removable device/MNT temporarily mounts some additional devices/SRV after some network services are started, / TMP Indicates the directory where files are temporarily stored by running programs. The system will delete /usr/bin/ from the directory where /usr/bin/ is stored. /usr/include/: C/C++ /usr/lib/ /usr/local/ : The system administrator installs and downloads software on the local host. You are advised to install the software in the /usr/sbin/ directory. Commands not required for normal system operation /usr/share/ : the directory for storing shared files /usr/src/ : The directory for storing common source files /var This directory is used for normal changeable files /var/cache/ : Some temporary files generated during the running of the application program. /var/lib/ : directory for storing required data files during the running of the application program. /var/lock/ : Directory for storing file resources that can be used by only one application at a time. Directory for storing login files /var/mail/ : directory for storing personal email addresses /var/run/ : directory for storing PID data after some programs or services are started. /var/spool/ : Directory for storing data that is queued to be used by other applicationsCopy the code

What is a root file system

There are a lot of articles on the web, but most of them are a copy of the first file system that is mounted when the kernel is booted. This is true, but to redefine the concept, the so-called root file system is the file system that is first mounted to the root directory /. Core is the root directory /. / directory and not belongs to which the file system, otherwise it is chicken and egg question, so don’t be up for a circle, it with other file systems do not have any distinction, but first it to give account for the pit /, subsequent to only hang the directories below it, eventually forming the entire directory tree.

Understanding the above, it is easy to understand the following questions:

  • A system can have multiple different file systems, and who makes the root file system depends on who the kernel wants to make the root file system at startup.
  • File systems can exist on many media, such as hard disk (MMC), flash (Flash), and RAM (RAM). Each medium has its most appropriate file system. MMC is usually (FAT,ext), Flash includes (JFFs2), and ram (Proc, SYS, TMPFS,ramfs).
  • A file system can be simple or complex, as long as it implements the three types of interfaces defined by the kernel:
    • Mount interface:MountOps ops
    • operationinodeNode interface:VnodeOps *vop
    • operationfileInterface:file_operations_vfs *fop, what this interface actually does underneathinodeThe data block pointed to.
  • After all, the kernel must have a file system to mount to/. The directory structure of the hongmenggan file system is as follows:
    . ├ ─ ─ app ├ ─ ─ bin │ ├ ─ ─ init │ ├ ─ ─ shell │ └ ─ ─ TFTP ├ ─ ─ data │ └ ─ ─ system │ └ ─ ─ param ├ ─ ─ etc ├ ─ ─ lib │ ├ ─ ─ libc++. So │ └ ─ ─ the libc. So ├ ─ ─ system │ ├ ─ ─ external │ └ ─ ─ internal └ ─ ─ usr ├ ─ ─ bin └ ─ ─ libCopy the code

Where do these numbers come from? For example :libc.so this C library function, immediately after the start of the need to use, which needs to be made externally, burned to the specified location of flash. Also note that the root file system created by Hongmeng does not have a /dev directory, which is explained in detail in the device files section.

Creating a root file system

Take the LiteOS_A kernel as an example, which provides a way to make a root file system:

Turing @ ubuntu: / home/openharmony/code - v1.1.1 - LTS/kernel/liteos_a $make a help ------------------------------------------------------- 1.====make help: get help infomation of make 2.====make: make a debug version based the .config 3.====make debug: make a debug version based the .config 4.====make release: make a release version for all platform 5.====make release PLATFORM=xxx: make a release version only for platform xxx 6.====make rootfsdir: make a original rootfs dir 7.====make rootfs FSTYPE=***: make a original rootfs img 8.====make test: make the testsuits_app and put it into the rootfs dir 9.====make test_apps FSTYPE=***: make a rootfs img with the testsuits_app in it xxx should be one of (hi3516cv300 hi3516ev200 hi3556av100/cortex-a53_aarch32 hi3559av100/cortex-a53_aarch64) *** should be one of (jffs2)Copy the code

Make rootfs, FSTYPE supports JFFS2, VFAT file formats

What happens when make Rootfs FSTYPE=jffs2

Check the kernel/liteos_a/Makefile

# make rootfs FSTYPE=jffs2 Everything from here $(rootfs): $(ROOTFSDIR) # depend on ROOTFSDIR $(HIDE)$(LITEOSTOPDIR)/tools/scripts/make_rootfs/rootfsimg.sh $(ROOTFS_DIR) $(FSTYPE) $(HIDE) CD $(ROOTFS_DIR)/.. && zip -r $(ROOTFS_ZIP) $(ROOTFS) # File rootfs.zipCopy the code

Interpretation of the

  • Compile the entire kernel object file

    $(LITEOS_TARGET): $(__LIBS) sysroot $(HIDE)touch $(LOSCFG_ENTRY_SRC) # Compile makefile $(HIDE)for dir in $(LITEOS_SUBDIRS); \ do $(MAKE) -C $$dir all || exit 1; \ done # Generate liteos.map $(LD) $(LITEOS_LDFLAGS) $(LITEOS_TABLES_LDFLAGS) $(LITEOS_DYNLDFLAGS) - map =$(OUT)/[email protected] -o $(OUT)/$@ --start-group $(LITEOS_LIBDEP) --end-group # $(SIZE) -t --common $(OUT)/lib/*.a >$(OUT)/[email protected] $(OBJCOPY) -o binary $(OUT) / $@ $(LITEOS_TARGET_DIR) / $@. Bin # generate liteos. Bin file $(OBJDUMP) - t $(OUT) / $@ | sort > $(OUT) / $@ sym. Sorted $(OBJDUMP) -d $(OUT)/$@ >$(OUT)/[email protected]Copy the code
  • Use $(APPS) to compile APPS in the kernel/ liteos_A/APPS directory, such as init,shell, TFTP. These APPS are also called built-in APPS.

    $(APPS): $(LITEOS_TARGET) sysroot $(HIDE)$(MAKE) -c apps all -c: Enter the apps directory.Copy the code
  • Run tools/scripts/make_rootfs/rootfsdir.sh to create directories (/bin, /app, /lib) in the public system.

    Mkdir -p ${ROOTFS_DIR}/bin ${ROOTFS_DIR}/lib ${ROOTFS_DIR}/usr/bin ${ROOTFS_DIR}/usr/lib ${ROOTFS_DIR}/etc  \ ${ROOTFS_DIR}/app ${ROOTFS_DIR}/data ${ROOTFS_DIR}/proc ${ROOTFS_DIR}/dev ${ROOTFS_DIR}/data/system ${ROOTFS_DIR}/data/system/param \ ${ROOTFS_DIR}/system ${ROOTFS_DIR}/system/internal ${ROOTFS_DIR}/system/external ${OUT_DIR}/bin ${OUT_DIR}/libs if [ -d "${BIN_DIR}" ] && [ "$(ls -A "${BIN_DIR}")" != "" ]; then cp -f ${BIN_DIR}/* ${ROOTFS_DIR}/bin if [ -e ${BIN_DIR}/shell ] && [ "${BIN_DIR}/shell" ! = "${OUT_DIR}/bin/shell" ]; Then cp -f ${BIN_DIR}/shell ${OUT_DIR}/bin/shell # "${BIN_DIR}/tftp" != "${OUT_DIR}/bin/tftp" ]; Then cp -f ${BIN_DIR}/ TFTP ${OUT_DIR}/bin/ TFTP # Copy the.so library to the root file system /lib cp -f ${LIB_DIR}/* ${OUT_DIR}/libsCopy the code
  • Use prepare to create a musl directory and copy the C/C ++ libraries into it

    prepare: $(HIDE)mkdir -p $(OUT)/musl ifeq ($(LOSCFG_COMPILER_CLANG_LLVM), $(HIDE)cp -f $$($(CC) --target=$(LLVM_TARGET) --sysroot=$(SYSROOT_PATH) $(LITEOS_CFLAGS) -print-file-name=libc.so) $(OUT)/musl # $(HIDE)cp -f $$($(GPP) --target=$(LLVM_TARGET) --sysroot=$(SYSROOT_PATH) $(LITEOS_CXXFLAGS) -print-file-name=libc++. So) $(OUT)/musl # else $(HIDE)cp -f $(LITEOS_COMPILER_PATH)/target/usr/lib/libc.so $(OUT)/musl $(HIDE)cp -f $(LITEOS_COMPILER_PATH)/arm-linux-musleabi/lib/libstdc++.so.6 $(OUT)/musl $(HIDE)cp -f $(LITEOS_COMPILER_PATH)/arm-linux-musleabi/lib/libgcc_s.so.1 $(OUT)/musl $(STRIP) $(OUT)/musl/* endifCopy the code
  • Tools /scripts/make_rootfs/rootfsimg.sh Generates an image file rootfs_jffs2.img and invokes mkfs.jffs2 to create an image in jFFs2 format.

    ROOTFS_IMG=${ROOTFS_DIR}"_"${FSTYPE}".img" JFFS2_TOOL=mkfs.jffs2 # Linux create jFFs2 image tool WIN_JFFS2_TOOL=mkfs.jffs2.exe Chmod -r 755 ${ROOTFS_DIR} if [-f "${ROOTFS_DIR}/bin/init"]; then chmod 700 ${ROOTFS_DIR}/bin/init 2> /dev/null fi if [ -f "${ROOTFS_DIR}/bin/shell" ]; then chmod 700 ${ROOTFS_DIR}/bin/shell 2> /dev/null fi if [ "${FSTYPE}" = "jffs2" ]; then if [ "${system}" != "Linux" ] ; then tool_check ${WIN_JFFS2_TOOL} ${WIN_JFFS2_TOOL} -q -o ${ROOTFS_IMG} -d ${ROOTFS_DIR} --pagesize=4096 else tool_check  ${JFFS2_TOOL} ${JFFS2_TOOL} -q -o ${ROOTFS_IMG} -d ${ROOTFS_DIR} --pagesize=4096 fi elif [ "${FSTYPE}" = "yaffs2" ]; then # to do fiCopy the code
  • Finally, zip command is used to package rootfs into rootfs.zip, so the production process of Hongmenggen system is completed. An out directory will be added with the following contents:

    Turing @ ubuntu: / home/openharmony/code - v1.1.1 - LTS/kernel/liteos_a/out/hi3518ev300 $ls bin lib liteos liteos. Asm liteos. Bin  liteos.map liteos.sym.sorted musl obj rootfs rootfs_jffs2.img rootfs.zipCopy the code
    • rootfsFor the production of hongmenggen file system
    • rootfs_jffs2.imgFor the image file, can burn toflashIn the.

The boot process

The code to start the root file system is listed here

STATIC UINT32 OsSystemInitTaskCreate(VOID)
{
    UINT32 taskID;
    TSK_INIT_PARAM_S sysTask;

    (VOID)memset_s(&sysTask, sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));
    sysTask.pfnTaskEntry = (TSK_ENTRY_FUNC)SystemInit;
    sysTask.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    sysTask.pcName = "SystemInit";
    sysTask.usTaskPrio = LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO;
    sysTask.uwResved = LOS_TASK_STATUS_DETACHED;
#if (LOSCFG_KERNEL_SMP == YES)
    sysTask.usCpuAffiMask = CPUID_TO_AFFI_MASK(ArchCurrCpuid());
#endif
    return LOS_TaskCreate(&taskID, &sysTask);
}
Copy the code

Interpretation of the

  • First, the kernel opens a callSystemInitTask to process system initialization code, task entry function isSystemInit
  • SystemInitLayer upon layer call toMountPartitions, mount partitions.
    SystemInit(void) ... OsMountRootfs() AddPartitions MountPartitions() #define ROOT_DEV_NAME "/dev/spinorblk0" #define ROOT_DIR_NAME  "/" ret = mount(ROOT_DEV_NAME, ROOT_DIR_NAME, fsType, mountFlags, NULL); //Copy the code

    Device FileWill be detailed/dev/spinorblk0The source, simply put, of the root file system is burned innor flashOn the first partition of the media device, partition name/dev/spinorblk0It’s just a “virtual” device file name with an actual file system behind it. Now hang it on/Up, and it turns out to benor flashThe first partition becomes the root file system.

Intensive reading of the kernel source code

Four code stores synchronous annotation kernel source code, >> view the Gitee repository

Analysis of 100 blogs. Dig deep into the core

Add comments to hongmeng kernel source code process, sort out the following article. Content based on the source code, often in life scene analogy as much as possible into the kernel knowledge of a scene, with a pictorial sense, easy to understand memory. It’s important to speak in a way that others can understand! The 100 blogs are by no means a bunch of ridiculously difficult concepts being put forward by Baidu. That’s not interesting. More hope to make the kernel become lifelike, feel more intimate. It’s hard, it’s hard, but there’s no turning back. 😛 and code bugs need to be constantly debug, there will be many mistakes and omissions in the article and annotation content, please forgive, but will be repeatedly amended, continuous update. Xx represents the number of modifications, refined, concise and comprehensive, and strive to create high-quality content.

Compile build The fundamental tools Loading operation Process management
Compile environment

The build process

Environment script

Build tools

Designed.the gn application

Ninja ninja

Two-way linked list

Bitmap management

In the stack way

The timer

Atomic operation

Time management

The ELF format

The ELF parsing

Static link

relocation

Process image

Process management

Process concept

Fork

Special process

Process recycling

Signal production

Signal consumption

Shell editor

Shell parsing

Process of communication Memory management Ins and outs Task management
spinlocks

The mutex

Process of communication

A semaphore

Incident control

The message queue

Memory allocation

Memory management

Memory assembly

The memory mapping

Rules of memory

Physical memory

Total directory

Scheduling the story

Main memory slave

The source code comments

Source structure

Static site

The clock task

Task scheduling

Task management

The scheduling queue

Scheduling mechanism

Thread concept

Concurrent parallel

The system calls

Task switching

The file system Hardware architecture
File concept

The file system

The index node

Mount the directory

Root file system

Character device

VFS

File handle

Pipeline file

Compilation basis

Assembly and the cords

Working mode

register

Anomaly over

Assembly summary

Interrupt switch

Interrupt concept

Interrupt management

HongMeng station | into a little bit every day, the original is not easy, welcome to reprint, please indicate the source.