44
教教教教教教教教教教 教教 Embedded filesystem

教育部顧問式嵌入式軟體聯盟 Embedded filesystem. 2 Outline Introduction to the file system Hard link and soft link Elementary management Embedded Linux file

  • View
    233

  • Download
    1

Embed Size (px)

Citation preview

教育部顧問式嵌入式軟體聯盟

Embedded filesystem

2

Outline

Introduction to the file systemHard link and soft linkElementary managementEmbedded Linux file systemVirtual files ystemThe role of the virtual file system

3

Introduction to the file systemOne of the last operations conducted by the Linu

x kernel during system startup is mounting the rootfilesystem

The root filesystem has been an essential component of all Unix systems from the start

In the Linux, the root filesystem will be mounted under this dictionary “/”

There are some sub-dictionaries and files under /, and these sub-dictionary also have their own sub-dictionaries and files and recursion

4

Introduction to the file systemThe whole file system is like the branches

Root is the beginning, and then branches again and again

This structure is called the hierarchy structure The following table contains the top level dictiona

ries of the root filesystem It is very useful for those apprentices who want to und

erstand the Linux filesystem to be conversant with what are in those dictionaries

5

Introduction to the file systembin Essential user command binaries

boot Static files used by the bootloader

dev Devices and other special files

etc System configuration files, including startup files

home User home dictionaries, including entries for services such as FTP

lib Essential libraries, including the C library, and kernel modules

mnt Mount point for temporarily mounted filesystems

opt Add-on software packages

proc Virtual filesystem for kernel and process information

root Root user’s home dictionary

sbin Essential system administration binaries

tmp Temporary files

usr Secondary hierarchy containing most applications and documents

useful to most users, including the X server

var Variable data stored by daemons and utilities

6

Introduction to the file systemAnything which is used by filesystem appears as

the type of file, including the dictionaries The dictionary is one kind of special filesystem

For all the hardware devices, there are corresponding files on the filesystem

For example : /dev/fd0 is the first floppy disk of the system, and vice versa

7

Introduction to the file systemTerminology you should know firstBlock :

Files is stored in the blocks with fixed size of the disk The size of the block is usually the power of two

Superblock : Stores information concerning a mounted filesystem For disk-based filesystems, this object usually corresp

onds to a filesystem control block stored on disk You may not be able to get the data form the disk if the

superblock is damaged

8

Introduction to the file systemInode :

Stores general information about a specific file For disk-based filesystems, this object usually corresp

onds to a file control block stored on disk Each inode object is associated with an inode number

which uniquely identifies the file within the filesystem

9

Hard link and soft linkWhat is the symbolic link ?There are two kinds of link on the Linux﹕

hard link soft link

The second one is also called symbolic link We will discuss the difference between the hard link an

d soft link

10

Hard link and soft linkWhat is hard link

You must get an inode if you want to store a file on the disk, and then inode will record where the file located in the disk

If you use the hard link, all file will use the same inode. It will point to the same location as the original file

This inode will renew its link count, and the more hard links, the more link counts

All the hard links are equivalent, and you must remove all the hard links associated with this file if you want to remove this file

11

Hard link and soft linkWhat is hard link

For instance : the disk space of file A is still there if I make a hard link as file B for file A and remove file A later. It is because that the disk space is still associated with file B

This disk space will be released if I remove the file B then

We could regard all the files as hard links, and different hard links point to different disk spaces

It is also allowed that different hard links reference to the same disk space

12

Hard link and soft linkWhat is soft link (also called symbolic link)

The difference from hard link is that soft link will make another inode, but it just points to the original name, not the same disk space

This soft link will become ownerless if the original inode was deleted and it is called the dead link

Hard link must be established on the same partition, and it could not be used on dictionaries

Soft link could be established on different partitions, and it could be used on both files and dictionaries

13

Elementary management

pwd It means that 「 Print Working Directory 」 , and it sta

nds that where you are in the file system

cd .. The function of this utility is that changing directory to u

pper directory If you enter 「 cd 」 in the console but no any path, yo

u would go back your home dirctory You could go to the absolute path which you want to if

you add the absolute path behind the cd instruction

14

Elementary managementmkdir dir_name

The result of this instruction will make a new directory under current directory, and the name of the directory is the dir_name which you enter

15

Elementary managementrmdir dir_name

Remove the directory named dir_name If you want to use this command, the directory you wa

nt to remove must contain no file, link and directory You could use the 「 rm 」 command to delete the file

s within the directory which you want to remove You could use the 「 rm -rf 」 command to delete all t

he files within the directory compulsorily

16

Elementary managementFor instance

This command will copy the test to generate a new file named test2.copy

If we replace the 「 cp 」 command with 「 mv 」 , we just change the name of test to test2.copy

The original file will disappear and there is only the new file named test2.copy

cp test test2.copy

17

Elementary management

ls 「 ls 」 command means that list If the target of this command is the directory, and the c

ontent of this directory will be listed

If you want to get the information of the directory it own , you could use the 「 -d 」 parameter

ls –d /root

18

Elementary managementchmod 1

Change the permission mode of the file

You could see some information about the file You could get some prosperities in the first ten column

s If the first one is 『 - 』 , it means it is a regular file If the first one is 『 d 』 , it means it is a directory If the first one is 『 l 』 , it means it is a link

ls -l

19

Elementary management

chmod 2 The following 9 letters were grouped into three

『 user 』 『 group 』 『 others 』

They stand for the permission of these three group Every group has such permission

『 r 』 stands for readable 『 w 』 stands for writable 『 x 』 stands for executable

20

Elementary managementchmod 3

The next number stands for the count of the hard links referenced to this file

The following next two columns stand for who is the user and the group of the user

User column means that one of the users within the system and it usually stands for the one who creates this file

Group column means that which group the user belongs

21

Elementary managementchmod 4

The follows are the file size and the modified date, and the last one is the name of the file

The following is the screenshot of 「 ls -l 」

22

Embedded Linux file systemIn most embedded Linux systems, where there ar

e no users and no administrators, the rules to build a root filesystem can be loosely interpreted This doesn't mean that all rules can be violated, but it

does mean that breaking some rules will have little to no effect on the system's proper operation

Interestingly, even mainstream commercial distributions for workstations and servers do not always adhere to the established rules for root filesystems.

23

Embedded Linux file systemAll the directories that pertain to providing a multi

user extensible environment, such as /home, /mnt,/opt, and /root, can be omitted

This discussion does not revolve around size issues, but rather functionality

In fact, omitting a directory entry changes little to the resulting size of the root filesystem

24

Embedded Linux file system

Depending on your bootloader and its configuration, you may not need to have a /boot directory This will depend on whether your bootloader c

an retrieve kernel images from your root filesystem before your kernel is booted

25

Embedded Linux file systemAt the extreme, you could omit /proc, which is us

eful only for mounting the virtual filesystem that has the same name

If you are very tight for space, you can configure your kernel without /proc support, but I encourage you to enable it whenever possible

/usr and /var, have a predefined hierarchy of their own, much like that of the root directory

26

Embedded Linux file systemFor the purposes of most embedded Linux syste

ms, nonetheless, the three directories in the /usr we created will suffice /usr/bin /usr/lib /usr/sbin

The directories we created are the bare minimum required for the normal operation of most applications found in an embedded Linux system

27

Embedded Linux file system

BusyBox BusyBox combines tiny versions of many com

mon UNIX utilities into a single small executable. It provides replacements for most of the utilities you usually find in GNU fileutils, shellutils, etc

Enthusiasm for BusyBox stems from the functionality it provides while still remaining a very small-sized application

28

Embedded Linux file system

BusyBox Although BusyBox does not support all the opti

ons provided by the commands it replaces, the subset it provides is sufficient for most typical uses

See the docs directory of the BusyBox distribution for the documentation in a number of different formats

29

Embedded Linux file system

Usage of BusyBox As expected, only one executable was installe

d, /bin/busybox This is the single binary with support for all the

commands configured using Config.h This binary is never called directly Instead, symbolic links bearing the original co

mmands' names have been created to /bin/busybox

30

Embedded Linux file system

Usage of BusyBox Such symbolic links have been created in all th

e directories in which the original commands would be found, including /bin, /sbin, /usr/bin, and /usr/sbin

31

Embedded Linux file system

Usage of BusyBox When you type a command during the syste

m's normal operation, the busybox command is invoked via the symbolic link

In turn, busybox determines the actual command you were invoking using the name being used to run it

/bin/ls, for instance, points to /bin/busybox

32

Embedded Linux file system

Usage of BuxyBox Although this scheme is simple and effective, it

means you can't use arbitrary names for symbolic links

Creating a symbolic link called /bin/dir to either /bin/ls or /bin/busybox will not work, since busybox does not recognize the dir command.

33

Embedded Linux file system

More data Official website : http://www.busybox.net/

34

Embedded Linux file system

Device files Following Unix tradition, every object in a Linux

system is visible as a file, including devices All the device files (device nodes) in a Linux ro

ot filesystem are located in the /dev directory In the embedded Linux system only the entries

required for the system's proper operation should be created

35

Embedded Linux file systemBasic device entries

filename description type Major munber

Minor mumber

Permission bits

mem Physical memory access

char 1 1 600

null Null device char 1 3 666

zero Null byte source char 1 5 666

36

Embedded Linux file systemBasic device entries

filename description type Major number

Minor number

Permission bits

random Nondeterministic random number generator

char 1 8 664

tty0 Current virtual console char 4 0 600

tty1 First virtual console char 4 1 600

ttyS0 First UART serial port char 4 64 600

tty Current TTY device char 5 0 666

console System console char 5 1 600

37

Embedded Linux file systemIn addition to the basic device files, there are a

few compulsory symbolic links that have to be part of your /dev directory

Link name Target

fd /proc/self/fd

stdin fd/0

stdout fd/1

stderr fd/2

38

Embedded Linux file system

Congratulation we have now prepared a basic /dev directory for our target

39

Virtual file systemOne of the keys for Linux to success is its ability t

o coexist comfortably with other systemsBy using the concept called the Virtual Filesyste

m Linux manages to support multiple disk types in the sa

me way other Unix variants do

The idea behind the VFS is to put a wide range of information in the kernel to represent many different types of filesystems

40

Virtual file systemThe five standard Unix file types

Regular files Directories Symbolic links Device files Pipes

This chapter will discuss the first three entries The device files will be discussed in chapter 13 The pipes will be discussed in chapter 19

41

The role of the virtual file system

The Virtual Filesystem (also known as Virtual Filesystem Switch or VFS) is a kernel software

layer that handles all system calls related to a standard Unix filesystem

Its main purpose is providing a common interface to several kinds of filesystems

42

The role of the virtual filesystem

Hard disk of ext2 format

Floppy of MS-DOS

VFS CP

43

The role of the virtual filesystem

An example for the using of VFSUse the “cp” utility , copy the data to the d

isk of ext2 format from the floppy of MS-DOS format

This operation embraces two kinds of filesystem and two different storage devices , which are held together by VFS

44

The role of the Virtual Filesystem

Data writing process

write() sys_write()Write() functionof filesystem

User level VFS filesystem real storage device

The VFS is an abstraction layer between the application program and the filesystem implementations.User level requests the write() function and it must goes through the VFS to get the write() function supported by filesystem to write the data into the real storage device