Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make rootfs under linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to make rootfs under linux". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, analysis 1. Introduction to file system

In theory, an embedded device does not need a file system if the kernel can run and does not need to run user processes. The file system is simply a kind of directory structure. Because the devices of the linux operating system exist in the form of files in the system, these files are classified and managed and provide an interface with the kernel to form a certain directory structure, that is, the file system. File system is a form of reflecting system for users, which provides users with an interface to detect and control the system.

Root file system, I think the root file system is a special file system, so what is the difference between the root file system and the ordinary file system?

Since the root file system is the first file system that the kernel hangs at startup, the root file system should include the necessary directories and key files for Linux startup.

For example, when Linux starts, you need to have relevant files in the init directory, and when Linux mounts a partition, Linux will definitely look for / etc/fstab this mount file, and so on. The root file system also includes many application bin directories, and any file that is necessary for booting the Linux system can become the root file system.

Linux supports a variety of file systems, including ext2, ext3, vfat, ntfs, iso9660, jffs, yaffs, romfs and nfs. In order to manage all kinds of file systems uniformly, Linux introduces virtual file system VFS (Virtual File System) to provide a unified operation interface and application programming interface for all kinds of file systems.

When Linux starts, the first thing that must be mounted is the root file system; if the system cannot mount the root file system from the specified device, the system will make an error and exit boot. Other file systems can then be mounted automatically or manually. Therefore, different file systems can exist in a system at the same time.

Different file system types have different characteristics, so they have different applications according to the hardware characteristics and system requirements of storage devices. In embedded Linux applications, the main storage devices are RAM (DRAM, SDRAM) and ROM (often using FLASH memory). The commonly used storage device-based file system types include: jffs2, yaffs, cramfs, romfs,ramdisk, ramfs/tmpfs and so on.

two。 File system 2.1 Cramfs:Compressed ROM File System based on FLASH

It is fast and efficient, and its read-only feature helps to protect the file system from damage and improve the reliability of the system. Because of the above characteristics, Cramfs is widely used in embedded systems.

However, its read-only attribute is also a major defect, which makes it impossible for users to expand its content pairs. The Cramfs image is usually placed in Flash.

2.2 jffs2

Jffs2: log flash file system version 2 (Journalling Flash FileSystem v2)

Mainly used for nor flash memory, based on MTD driver layer, characterized by: read-write, support data compression, hash table-based log file system, and provide crash / power-down security protection, provide "write balance" support, and so on.

The main disadvantage is that when the file system is full or nearly full, the running speed of jffs2 is greatly slowed down because of garbage collection. Jffs is not suitable for NAND flash memory mainly because the capacity of NAND flash memory is generally large, which leads to a rapid increase in the memory space occupied by jffs to maintain log nodes. in addition, the jffs file system needs to scan the contents of the entire FLASH when mounting to find all log nodes and establish file structure, which will take a lot of time for large-capacity NAND flash memory.

2.3.yaffs:Yet Another Flash File System

Yaffs/yaffs2 is a log file system designed for embedded systems to use NAND flash memory. Compared with jffs2, it reduces some features (for example, it does not support data compression), so it is faster, has a shorter mount time, and consumes less memory. In addition, it is also a cross-platform file system, in addition to Linux and eCos, but also supports WinCE, pSOS and ThreadX. Yaffs/yaffs2 comes with the driver of NAND chip, and provides the embedded system with API to access the file system directly. Users can operate on the file system directly without using MTD and VFS in Linux. Of course, yaffs can also be used with the MTD driver.

The main difference between yaffs and yaffs2 is that the former only supports small page (512 Bytes) NAND flash memory, while the latter supports large page (2KB) NAND flash memory. At the same time, yaffs2 has greatly improved in memory footprint, garbage collection speed, read / write speed and so on.

2.4. Network file system NFS (Network File System)

NFS is a technology developed and developed by Sun to share files between different machines and different operating systems through the network.

In the development and debugging stage of embedded Linux system, we can use this technology to build a NFS-based root file system on the host, mount it to the embedded device, and easily modify the content of the root file system.

Tool used: mkfs.cramfs,mkfs.jffs2,mkfs.yaffs

Http://sourceforge.net/projects/cramfs/ http://sourceforge.net/projects/jffs2os/ http://sourceforge.net/projects/yaffs/ II. Composition of the root file system 1. Brief introduction to the root file system directory

Bin: basic executable file

Opt: software packages added

Boot: some files needed for startup

Proc: virtual file system for kernel and process information

Dev: device fil

Root:root user directory

Etc: system configuration fil

Sbin: a system management program

Home: user directory

Tmp: temporary fil

Lib: library fil

Usr: application

Mnt: the mount point of the mount file system

Var: a temporary file that stores system logs or some service programs

two。 Directories that need to be migrated in embedded environment

Each top-level directory in the root file system has a specific purpose and purpose, but not all directories are needed in an embedded environment, we only create a few directories that we need:

/ bin / sbin / etc / proc / tmp / var / dev / mnt

The Linux root file system should include at least the following.

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Basic file system structure, including some necessary directories such as: / dev,/proc,/bin,/etc,/lib,/usr,/tmp, etc.

The library functions needed for the basic program to run, such as glibc.

Basic system configuration files, such as script files such as rc.sysinit,inittab.

Necessary device file support: / dev/hd*,/dev/tty*,/dev/fd0.

Basic applications such as sh,ls,cp,mv, etc.

3. The work that needs to be done in the migration

Put the global configuration file in the / etc directory.

Put the device file information in the / dev directory, and the device name can exist as a symbolic link in / dev or other devices in the / devs subdirectory.

The operating system core is located at / or / boot. If the operating system core does not exist as a file of the file system, it is not applied.

The directory where the library is stored is / lib.

The directory for storing the compiled executable files and commands of the system is / bin,/sbin,/usr.

3. Default preset condition 1) Cross-compilation tool

Cross-compiler needs to be pre-installed. The installation version of arm-none-linux-gnueabi-gcc is as follows: by default, the installation directory in ubuntu is:

/ home/peng/toolchain/gcc-4.6.4/2) tftp server

The development board needs tftp server to download kernel image and device tree. The configuration information is as follows:

Peng@ubuntu:~$ cat / etc/default/tftpd-hpa # / etc/default/tftpd-hpa TFTP_USERNAME= "tftp" TFTP_DIRECTORY= "/ tftpboot" TFTP_ADDRESS= "0.0.0.0pur69" TFTP_OPTIONS= "--secure"

The server root directory is * * / tftpboot**

3) nfs server

After the kernel is started, the file system needs to be mounted through the nfs server. The nfs server is set as follows:

Peng@ubuntu:~$ cat / etc/exports # / etc/exports: the access control list for filesystems which may be exported # to NFS clients. See exports (5). # # Example for NFSv2 and NFSv3: # / srv/homes hostname1 (rw,sync,no_subtree_check) hostname2 (ro,sync,no_subtree_check) # # Example for NFSv4: # / srv/nfs4 gss/krb5i (rw,sync,fsid=0,crossmnt,no_subtree_check) # / srv/nfs4/homes gss/krb5i (rw,sync,no_subtree_check) # / source/rootfs * (rw,sync No_subtree_check) fourth, file system creation step 1, source code download

The version we choose is the busybox-1.22.1.tar.bz2 download path:

Http://busybox.net/downloads/2, decompress the source code $tar xvf busybox-1.22.1.tar.bz23, enter the source code directory $cd busybox-1.22.14, and configure the source code

Choose to make a static library and set the prefix arm-none-linux-gnueabi- of the interactive compilation tool chain. If it is another tool chain, just fill in the example.

$make menuconfig Busybox Settings-- > Build Options-- > [*] Build BusyBox as a static binary (no shared libs) [] Force NOMMU build [] Build with Large File Support (for accessing files > 2 GB) (arm-none-linux-gnueabi-) Cross Compiler prefix () Additional CFLAGS5, compile $make6, install

The default installation path for busybox is _ install in the source directory.

$make install7, enter the installation directory

The following 4 files or folders are created by default

$cd _ install $ls bin linuxrc sbin usr8, create other required directories $mkdir dev etc mnt proc var tmp sys root9, add libraries

The cross-tool chain we installed contains the libraries we need that can be used on the development board. Just copy them to the _ install directory:

$cp / home/linux/toolchain/gcc-4.6.4/arm-arm1176jzfssf-linux-gnueabi/lib/. -a

Modify file permissions and delete symbol tables in static and shared library files

$chmod + w lib $chmod + w lib/* $rm lib/*.a $arm-none-linux-gnueabi-strip lib/*

Delete unwanted libraries and ensure that the size of all libraries is no more than 8m

$du-mh lib/10, add system boot file

Add the file inittab under etc, and the file content is as follows:

# this is run first except when booting in single-user mode. :: sysinit:/etc/init.d/rcS # / bin/sh invocations on selected ttys # start an "askfirst" shell on the console (whatever that may be):: askfirst:-/bin/sh # stuff to do when restarting the init process:: restart:/sbin/init # stuff to do before rebooting:: ctrlaltdel:/sbin/reboot

Add the file fstab under etc, and the file content is as follows:

# device mount-point type options dump fsck order proc / proc proc defaults 0 0 tmpfs / tmp tmpfs defaults 0 0 sysfs / sys sysfs defaults 0 0 tmpfs / dev tmpfs defaults 0 0

[note] here we mount the file system with three proc, sysfs, and tmpfs. In the kernel, proc and sysfs are supported by default, but tmpfs is not supported, so we need to make sure that the kernel has tmpfs support.

Modify the kernel configuration:

$make menuconfig File systems-- > Pseudo filesystems-- > [*] Virtual memory filesystem support (former shm fs) [*] Tmpfs POSIX Access Control Lists

Recompile the kernel

Create an init.d directory under etc and a rcS file under init.d. The contents of the rcS file are:

#! / bin/sh # This is the first script called by init process / bin/mount-a Mount all file systems established by fstab echo / sbin/mdev > / proc/sys/kernel/hotplug / sbin/mdev-s

Add executable permissions to rcS:

$chmod + x init.d/rcS

Add the profile file under etc with the following contents:

#! / bin/sh export HOSTNAME=farsight export USER=root export HOME=root export PS1= "[$USER@$HOSTNAME\ W]\ #" PATH=/bin:/sbin:/usr/bin:/usr/sbin LD_LIBRARY_PATH=/lib:/usr/lib:$LD_LIBRARY_PATH export PATH LD_LIBRARY_PATH mknod dev/console c 5 1 this file node is required

Important: if the size of the newly created file system exceeds 8m, delete unwanted library files, such as C++ library.

11. test

The finished root file system can be mounted to ubuntu through nfs after the development board is started.

1. Delete previous / source/rootfs

$sudo rm-rf / source/rootfs

two。 Copy our newly created root file system to the / source/rootfs directory

$sudo mkdir / source/rootfs $sudo cp _ install/* / source/rootfs-a

3. Set the uboot environment variable

# setenv serverip 192.168.9.120 # setenv ipaddr 192.168.9.233 # setenv bootcmd tftp 41000000 uImage\; tftp 42000000 exynos4412-fs4412.dtb\; bootm 41000000-42000000 # setenv bootargs root=/dev/nfs nfsroot=192.168.9.120:/source/rootfs rw console=ttySAC2,115200 init=/linuxrc ip=192.168.9.233 # saveenv

Restart the development board to see if it can be mounted and function properly.

5. Making ramdisk file system

After passing the NFS test, you can make the ramdisk file system as follows:

1. Create an image file with a size of 8m $cd ~ dd if=/dev/zero of=ramdisk bs=1k count=8192 (ramdsik is 8m) If: input file Of: output file2, format the image file as ext2 $mkfs.ext2-F ramdisk3, create an initrd directory under mount as the mount point $sudo mkdir / mnt/initrd4, and mount the disk image file to / mnt/initrd

Note that the ramdisk here cannot be stored in the rootfs directory

$sudo mount-t ext2 ramdisk / mnt/initrd5, copy all the contents of the tested file system to the / mnt/initrd directory $sudo cp / source/rootfs/* / mnt/initrd-a

If the copy encounters an error, you need to delete unwanted libraries again, such as C++ library

6. Uninstall / mnt/initrd$ sudo umount / mnt/initrd7, compress ramdisk to ramdisk.gz$ gzip-- best-c ramdisk > ramdisk.gz8, format it to the format recognized by uboot and copy it to $mkimage-n "ramdisk"-An arm-O linux-T ramdisk-C gzip-d ramdisk.gz ramdisk.img $cp ramdisk.img / tftpboot9 under / tftpboot, and configure the kernel to support RAMDISK.

After making initrd.img.gz, you need to configure the kernel to support RAMDISK as the boot file system.

Make menuconfig File systems-> Second extended fs support Device Drivers SCSI device support-> SCSI disk support Block devices-> RAM block device support (16) Default number of RAM disks (8192) Default RAM disk size (kbytes) (modified to 8m) General setup-> [*] Initial RAM filesystem and RAM disk (initramfs/initrd) support

Recompile the kernel and copy it to / tftpboot

10. Reset the startup parameters on the U-BOOT command line: # setenv bootcmd tftp 41000000 uImage\; tftp 42000000 exynos4412-fs4412.dtb\; tftp 43000000 ramdisk.img\; bootm 41000000 43000000 42000000 # saveenv

Restart the development board to see if it starts normally.

[note] because the commands of each development board will be different, the setting of the uboot command should be operated by the manufacturer's manual.

This is the end of "how to make rootfs under linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report