Reading Notes Building embedded linux system Chapter 6 File system Content

Source: Internet
Author: User

The official description of root fileSystem is FHS (FileSystem hierachy Stardard ). The highest level directories under the root directory have a clear purpose, but many of them are used as multi-user systems, and embedded systems can follow these rules more loose.



Root directory system


/Bin Essential user command binaries
/Boot Static files used by the bootloader -- according to our bootloader and its configuration, it may be deleted. This depends on whether our bootloader can obtain the kernel image from the root file system before the kernel starts.
/Dev Devices and other special files
/Etc System configuration files, including startup files
/Home User home directories-used to expand the User environment, which can be deleted
/Lib Essential libraries, such as the C library, and kernel modules
/Media Mount points for removable media
/Mnt Mount points for temporarily mounted filesystems-delete an Extended user environment
/Opt Add-on software packages-used to expand the user environment, which can be deleted
/Proc Virtual filesystem for kernel and process information
/Root Root user's home directory-this is an extended user environment that can be deleted.
/Sbin Essential system administration binaries
/Sys Virtual filesystem for system information and control (buses, devices, and drivers)
/Tmp Temporary files, chmod 1777 tmp, which is ensured to be deleted by the created user.
/Usr Secondary hierarchy containing most applications and documents useful to most users, including the X server. Create three sub-directories: bin, sbin, and lib. Other word directories such as man, src, and local may not need to be used in embedded OS.
/Var Variable data stored by daemons and utilities, create lib, lock, log, run, tmp, use chmod 1777 tmp

We can create rootfs under $ PRJROOT, and create the above file system. You can even delete/tmp and/var more cruelly, but it may harm other operations. We do not recommend that you do this. The deletion process is only based on whether it is useful and does not affect the size. You can delete/proc and/sys more cruelly if you do not need a virtual file system, but some basic commands, such as ps, mount, ifconfig, and modprobe, need to use/proc, more applications also need to use/sys, unless we limit the scope of use of embedded systems, otherwise do not do this.



Lib Library File


/Bin/sbin/usr/bin/usr/sbin. /Bin is used to save the basic (or basic) binary files of users and system administrators, And/sbin is used to save the binary files that are not required by system administrators, the differences between/usr/bin and/usr/sbin are similar. They do not save basic binary files. Save in/lib for system startup and Application to basic commands, while/usr/lib saves other lib. For example, perl5 has lib in/usr/lib/perl5.

Lib has four formats:

  • LibLIBRARY_NAME-GLIBC_VERSION.so, such as libc-2.9.so, libcidn-2.9.so

  • LibLIBRARY_NAME.so.MAJOR_REVISION_VERSION, such as libc. so.6-> libc-2.9.so, libcidn. so.1-> libcidn-2.9.so

  • LibLIBRARY_NAME.so, for example, libcidn. so-> libcidn. so.1

  • Static Library: libLIBRARY_NAME.a, such as libc. a, libcrypt.

-Rwxr-xr-x 1 wei 105210 05-15 libcrypt-2.9.so

-Rw-r -- 1 wei 139684 05-15 libcrypt.

Lrwxrwxrwx 1 wei 13 05-15 16:09 libcrypt. so-> libcrypt. so.1

Lrwxrwxrwx 1 wei 15 05-15 16:09 libcrypt. so.1-> libcrypt-2.9.so

Compared with these naming methods, glibc library is more special, it dynamic link library ld format for ld-GLIBC_VERSION.so, ld-linux. so. MARHOR_REVISION_VERSION (ARM, i386, m68k) or ld. so. MAJOR_REVISION_VERSION (MIPS, PowerPC ). This is not a real library. ld. so is triggered by the ELF binary loader to load the Dynamic Linked Library into the storage space of the application.

In the embedded operating system, if we want to view the dynamic link library used by the application on the host, we need to use the readelf of cross-platform to replace ldd, such as powerpc-linux-readelf.


For the target file system, copy the relevant (required) lib to rootfs/lib. The libLIBRARY_NAME-GLIBC_VERSION.so and liblibrary_name.so.major_revision_version are required. File System creation includes: Lib, kernel modules, and kernel Images


# Copy all lib files to the target Lib. Of course, you can select only the required parts.
CD $ {target_prefix}/lib

Cp *-*. So $ {prjroot}/rootfs/lib

CP-D *. So. [* 0-9] $ {prjroot}/rootfs/lib

CP libsegfault. So libmemusage. So libpcprofile. So $ {prjroot}/rootfs/lib

# If we do not need all the Lib libraries and select some useful components, we can perform the following operations:
CD $ {target_prefix}/lib

For file in libc libcrypt libdl libm libpthread libresolv libutil

Do

CP $ file-*. So $ {prjroot}/rootfs/lib

CP-d $ file. So. [* 0-9] $ {prjroot}/rootfs/lib

Done

CP-d ld *. So * $ {prjroot}/rootfs/lib

# If libnss _ *. So is selected, among which libnss_file and libnss_dns are commonly used, place the nsswitch. conf file in/etc and configure it as needed.
Cp $ {PRJROOT}/build-tools/src-glibc/glibc-20081113T2206/nss/nsswitch. conf $ {PRJROOT}/rootfs/etc

# The lib file is relatively large and can be cropped Using strip. As shown in the following figure, the original size is 12.488 MB, and the size after sorting is 2.812 MB, greatly reducing the space. Of course, if we are not sensitive to the size of the space and the storage capacity is growing, we can skip this step.
I586-linux-strip $ {PRJROOT}/rootfs/lib/*. so

# Copy the kernel module and add modprobe under etc. conf file (used to give the module parameters, the steps are not very detailed, maybe echo >$ {PRJROOT}/rootfs/etc/modprobe. conf ).
Cp-a $ {PRJROOT}/images/myproject/modules-2.6.29-menlow/* $ {PRJROOT}/rootfs

Echo >$ {PRJROOT}/rootfs/etc/modprobe. conf

# Copy-related kernel image
Mkdir $ {PRJROOT}/rootfs/boot

Cp $ {PRJROOT}/images/myproject/bzImage-2.6.29-menlow $ {PRJROOT}/rootfs/boot/vmlinuz -- 2.6.29-menlow

Cp $ {PRJROOT}/images/myproject/System. map-2.6.29-menlow $ {PRJROOT}/rootfs/boot/

Cp $ {PRJROOT}/images/myproject/2.6.29-menlow. config $ {PRJROOT}/rootfs/boot/config-2.6.29-menlow



Device Files


Ethernet is not used as a device file, which is special. Static device files only need to be created once.
[Wei @ wei rootfs] $ ls/dev/ttyS1 *-l

Crw-rw ---- 1 root uucp 4, 65 05-31 08:55/dev/ttyS1

The first letter indicates the type, c Indicates character, B Indicates block, 4 indicates major number, and 65 indicates minor number. This is the dependency of the kernel for driver management.

Create a static device file:
Cd $ {PRJROOT}/rootfs/dev

[Wei @ wei dev] $ su-m

? Ü ?? £ O

[Root @ wei dev] # mknod-m 600 mem c 1 1

[Root @ wei dev] # mknod-m 666 null c 1 3

[Root @ wei dev] # mknod-m 666 zero c 1 5

[Root @ wei dev] # mknod-m 644 random c 1 8

[Root @ wei dev] # mknode-m 600 tty0 c 4 0

[Root @ wei dev] # mknod-m 600 tty1 c 4 1

[Root @ wei dev] # mknod-m 600 ttyS0 c 4 64

[Root @ wei dev] # mknod-m 666 tty c 5 0

[Root @ wei dev] # mknod-m 600 consol c 5 1

[Root @ wei dev] # chmod 644 random

[Root @ wei dev] # chmod 600 tty0 tty1 ttyS0

[Wei @ wei dev] $ ln-s/proc/self/fd

[Wei @ wei dev] $ ln-s fd/0 stdin

[Wei @ wei dev] $ ln-s fd/1 stdout

[Wei @ wei dev] $ ln-s fd/2 stderr

In linux2.4 a few years ago, 10 thousand device files were found in/dev, but in 2.6, dynamic device files were greatly reduced. A problem with static device files is that we cannot check the/dev file to determine whether the device actually exists or is valid. Therefore, a dynamic device file system is introduced, only available devices are available in/dev. In linux2.6, udev is used to mount the device to/sys so that the device, driver, and other information can be stored in user space, he overcame many shortcomings in Hot Plug scripts/sbin/hotplug and eventually replaced it.

Udev needs to be installed

[Wei @ wei udev-142] $./autogen. sh

[Wei @ wei udev-142] $ make CROSS_COMPILE = i586-linux-DESTDIR =$ {PRJROOT}/rootfs install

In rootfs/lib/udev/, rootfs/etc/udev/, rootfs/etc/scsi_id.config

Udev waits for the uevents from the kernel when the device is inserted or detached, and searches for matching in udev's rules based on the message content or device information in/sys. If yes, the device file is generated or deleted, the driver is loaded or uninstalled, or the user space program is reminded. This information is transmitted through netlink socket.



Busybox


There are a large number of Unix commands in Linux, so we can't expect to compile them separately through cross compilation. We can use some system apps, such as busybox. If busybox is compiled in moblin, You need to download ncurses-devel

Busybox and kernel are similar in configuration. If the default value is used, defconfig or menuconfig can be used for configuration.


Busybox settings --->

Build option --->

[*] Build busybox as a static binary (no shared libs) This is for a small system busybox and a small part of the Lib, without the entire C library, making the system simpler and smaller. I don't think this is necessary in moblin.

Linux system utilities ---> (checked, the following is open by default)

[*] Mdev

[*] Support/etc/mdev. conf

[] Support Command Execution At device addition/removal

You must select the mdev option. Otherwise, udev cannot be enabled.


Run make install to compile. After compilation, the _ install directory will appear, including the bin, sbin, and usr directories and a linuxrc file.

$ Make arc = x86 cross_compile = i586-linux-config_prefix =$ {prjroot}/rootfs install


Busybox is a tool integration. It directs the ls, ps, and other command links to busybox. busybox can provide the functions of these tools. We don't need to compile these tool sets separately.

[Wei @ wei bin] $ pwd

/Home/wei/workspace/mywork/moblin/rootfs/bin

[Wei @ wei bin] $ ls-l

?? 1460

Lrwxrwxrwx 1 wei 7 06-03 11: 48 addgroup-> busybox

Lrwxrwxrwx 1 wei 7 06-03 11: 48 adduser-> busybox

Lrwxrwxrwx 1 wei 7 06-03 11: 48 ash-> busybox

-Rwxr-xr-x 1 wei 1490596 06-03 busybox

Lrwxrwxrwx 1 wei 7 06-03 11: 48 cat-> busybox

Lrwxrwxrwx 1 wei 7 06-03 11: 48 catv-> busybox

Lrwxrwxrwx 1 wei 7 06-03 11: 48 chattr-> busybox

Lrwxrwxrwx 1 wei 7 06-03 11: 48 chgrp-> busybox

Lrwxrwxrwx 1 wei 7 06-03 :48 chmod-> busybox

Lrwxrwxrwx 1 wei 7 06-03 11: 48 chown-> busybox

Similarly, commands in sbin/, usr/bin, usr/sbin also point to the busybox

In the etc/profile


[Wei @ wei rootfs] $ cd etc

[Wei @ wei etc] $ cat profile

# Set Path

PATH =/bin:/sbin:/usr/bin:/usr/sbin

Export PATH

# Set other libary path enter the location of other libraries that are not lib.

# LD_LIBRARY_PATH =

# Export LD_LIBRARY_PATH



System Startup


There are two main methods: System V init and busybox. Moblin uses system V init. We try to start moblin and busybox as the system respectively:

The original sysvinit 2.86.

$ Cd $ {PRJROOT}/sysapps/sysvinit-2.86/src

$ Make CC = i586-linux-gcc

$ Make BIN_OWNER = "$ (id-un)" BIN_GROUP = "$ (id-gn)" ROOT =$ {PRJROOT}/rootfs install

You can direct the original bootlogd, halt, init, killall5, last, mesg, mountpoint, pidof, poweroff, reboot, runlevel, shutdown, suloing, telinit, utmpdmp, update the compiling result of wall to sysvinit. We do not use the root permission for compilation, so we need to specify the user and user group. The embedded operating system is generally not a multi-user system, so it is not sensitive to user permission settings. If we need multiple users, we can use the root permission, but we need to be very careful. Any operation under the root permission requires caution. We can also compile the command with a common user, and manually modify the permissions of these commands in rootfs.

Busybox has inittab in example. We can copy it to the rootfs/etc directory. According to initab:

: Sysinit:/etc/init. d/rcS

We have created the init. d directory, the RFM file, and the RFM file under etc:

#! /Bin/sh

# Set Path
PATH =/bin:/sbin:/usr/bin:/usr/sbin

# Remount the root filesystem in read-write (requires/etc/fstab)
Mount-n-o remount, rw/

# Mount/proc filesystem
Mount/proc

# Start the network interface
#/Sbin/ifconfig eth0 192.168.1.2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.