Create an embedded Linux File System (ramdisk, cramfs, squashfs)

Source: Internet
Author: User

1. What is a file system );

A directory structure contained within a disk drive or disk area. the total available disk space can be composed of one or more filesystems. A filesystem must be mounted before it can be accessed. to mount a filesystem, you must specify a directory to act as the mount point. once mounted, any access to the mount point directory or its subdirectories will access the separate filesystem.

A file system is a directory structure that includes a disk (including a CD, floppy disk, flash disk, and other storage devices) or a partition. An applicable disk device can contain one or more file systems; to access a file system, you must first mount the file system. To mount the file system, you must specify a mount point.

2. Mainly embedded file systems;

* In Linux, rootfs is essential. Ramdisk and direct mounting of the root file system on HD (hard disk) are mainly implemented on the PC. in embedded systems, it is generally started from flash instead of HD, the simplest method is to load rootfs to ram's ramdisk. The more complex method is to directly read cramfs from flash, and more complicated is to partition on flash and build file systems such as jffs2.

* Ramdisk compresses the created rootfs and writes it to flash. during startup, the bootloader loads it to ram, decompress it, And then mounts it /. This method is simple, but the file system in Ram is not compressed, so it needs to occupy a lot of rare resources in embedded systems Ram.

Ramdisk uses memory space to simulate hard disk partitions. ramdisk usually uses the compression of the disk file system and stores it in flash. during system initialization, It is decompressed to SDRAM and mounted to the root file system, in Linux, there are two types of ramdisk. One is to format and load ramdisk, which is supported in Linux kernel 2.0/2.2. The disadvantage is that the size is fixed; the other is supported by the 2.4 kernel. It is implemented through ramfs and cannot be formatted, but it is easy to use. Its size increases or decreases as needed, ramdisk is a common ramdisk Technology in Linux.

* Initrd is the ramdisk format. Before kernel 2.4 was image-initrd, kernel 2.5 introduced cpio-initrd, which greatly simplified the Linux Startup Process and attached the basic philosophy of Linux: keep it simple, stupid (kiss ). however, cpio-initrd is not widely tested as a new format, and image-initrd is mainly used in embedded Linux.

* Cramfs is a simple file system written by Linus. It can be run directly from flash without being loaded to ram, thus saving Ram. However, cramfs is read-only and inconvenient for directories (such as/etc,/var,/tmp) that need to be modified during runtime. Therefore, these directories are generally made into writable FS such as ramfs.

* Squashfs is an enhancement to cramfs. It breaks through some cramfs restrictions and also has advantages in flash and RAM usage. However, according to developers, the performance may be inferior to that of cramfs. This is also a new method. More tests are required before embedded systems are used.

3. Create a directory containing all files;

1. Create a directory rootfs to install the File System

2. Mkdir bin Dev etc lib proc sbin tmp usr VaR

3. ln-Fs bin/busybox linuxrc (using busybox)

4. to the system/dev, pack all the devices and copy them to Dev (the most convenient way). Or, to use mknod to build the desired device, I use the following:
CrW-RW-1 Root 5, 1 2006-02-24 13:12 Console
CrW-RW-1 Root 5, 64 cua0
CrW-RW-1 Root 63, 0 2006-02-24 13:
CrW-RW-1 Root 63, 1 2006-02-24 13:12 DK1
Drwxr-XR-x 2 root Root 4096 flash
BRW-RW-1 Root 3, 0 2006-02-24 13:12 hda
CrW-RW-1 Root 36, 10 2006-02-24 13:12 IPSec
CrW-RW-1 Root 241, 0 2006-02-24 13:12 ixnpe
CrW-RW-1 Root 1, 2 2006-02-24 13:12 kmem
CrW-RW-1 Root 126, 0 2006-02-24 13:12 ledman
Lrwxrwxrwx 1 Root 16 log->/tmp/var/log
CrW-RW-1 Root 1, 1 2006-02-24 13:12 mem
CrW-RW-1 Root 90, 0 2006-02-24 13:12 mtd0
BRW-RW-1 Root 31, 0 2006-02-24 13:12 mtdblock0
BRW-RW-1 Root 31, 1 2006-02-24 13:12 mtdblock1
BRW-RW-1 Root 31, 2 mtdblock2
BRW-RW-1 Root 31, 3 2006-02-24 13:12 mtdblock3
BRW-RW-1 Root 31, 4 mtdblock4
BRW-RW-1 Root 31, 5 mtdblock5
BRW-RW-1 Root 31, 6 mtdblock6
CrW-RW-1 Root 90, 1 2006-02-24 13:12 mtdr0
CrW-RW-1 Root 1, 3 2006-02-24 13:12 null
CrW-RW-1 Root 108, 0 2006-02-24 13:12 PPP
CrW-r -- 1 Root 5, 2 ptmx
Drwxr-XR-x 2 root Root 4096 PTS
CrW-RW-1 Root 2, 0 2006-02-24 13:12 ptyp0
BRW-RW-1 Root 1, 0 2006-02-24 13:12 ram0
CrW-RW-1 Root 1, 8 random
CrW-RW-1 Root 5, 0 2006-02-24 13:12 tty
CrW-RW-1 Root 4, 0 2006-02-24 13:12 tty0
CrW-RW-1 Root 3, 0 2006-02-24 13:12 ttyp0
CrW-RW-1 Root 4, 64 2006-02-24 13:12 ttys0
CrW-RW-1 Root 1, 9 urandom
CrW-RW-1 Root 1, 5 zero

Example: mknod console C 5 1 CrW-RW-1 Root 5, 1 Console

5. Copy the compiled busybox to/bin. All other commands except busybox are their links.

Ash chgrp clear dd echo fgrep gzip IP ls modprobe MV Ping PWD sed stty tar true zcat busybox chmod CP egrep grep hostname kill mkdir more netstat ping2file RM sh sync TFTP umount cat chown date dmesg false gunzip ifconfig ln mknod Mount pidof PS rmdir sleep sysinfo touch uname

You can use make menuconfig to increase or decrease all commands in busybox.

6. Similarly, the link of busybox is under/sbin.

Halt ifconfig init insmod klogd losetup lsmod mkswap modprobe reboot rmmod route swapoff Swapon

7. The link of busybox is also under/usr/bin.
Basename dirname env free ID logger reset tail tr tty uptime which xargs
Awk cut du expr head killall mkfifo sort test traceroute uniq WC whoami Yes

The above is almost the most complete link, and each viewer can delete it as appropriate, but the link does not occupy any space!

8. Similarly, all the compiled executable files are placed under/usr/sbin.

9. It is very important to pay attention to/lib.

Find the target directory of your compiling environment, and first use strip to compress the required lib file (except the Strip provided by the compiling environment under the non-target directory). First, set the most basic libc, LD and so on. The link must be the same as the link in target/lib.

Then add the corresponding lib according to the specific application. Do not add unnecessary Lib. Lib occupies space.

10. Add the required configuration file under/etc, and the most important thing is the RFM.
#! /Bin/sh
Export Path =/sbin:/bin:/usr/sbin:/usr/bin:/usr/sbin/scripts

UTC = Yes

Mount-n-t proc/proc
Mount-n-t ramfs/tmp
Mount-n-t devpts/dev/PTS

# Build var Directories
/Bin/mkdir-M 0777/tmp/var
/Bin/mkdir-M 0777/var/lock
/Bin/mkdir-M 0777/var/log
/Bin/mkdir-M 0777/var/run
/Bin/mkdir-M 0777/var/tmp
#/Bin/mkdir-M 0777/tmp/etc

#/Bin/CP-A/usr/etc/*/etc
#/Bin/CP-A/usr/dev/*/dev/

# Loads the NPE Ethernet modules into the kernel.

Insmod/lib/modules/2.6.13.2/Intel/ixp400.ko

# Firmware code for NPE engine.

CAT/etc/ixnpemicrocode. dat>/dev/ixnpe

Insmod/lib/modules/2.6.13.2/Intel/ixp400_eth.ko netdev_max_backlog = 500
Insmod/lib/modules/led. Ko
Insmod/lib/modules/push_button.ko

The specific application below is not added as an example. This is required by the system init.

The other situations are similar. I added a target for the lunksys GPL project in the attachment.

Attachment

4. Generate a ramdisk

#! /Bin/sh
Module_name = ramdisk
Rampath = 'pwd'
Tmppath =$ {rampath}/tmp
Source =$ {rampath}/../Target

If [! -D $ {tmppath}]
Then
Mkdir $ {tmppath}
Fi

If ['whoam '! = 'Root']
Then {
Echo "You shoshould run the shell as root, please rerun as a root ."
Echo "aborting ."
Exit 1
}
Fi

# Clear in TMP path
Rm-RF $ {tmppath}/tmpmnt
Rm-RF $ {tmppath}/ramrootfs
Mkdir $ {tmppath}/tmpmnt

# Clear the old ramdisk
Rm-F $ {rampath}/$ module_name

# Make a temp file which size is suitable
Dd If =/dev/Zero of =$ {tmppath}/ramrootfs BS = 1 k count = 6144

# Create a ext2 filesystem
Mke2fs-F-M 0-I 2000 $ {tmppath}/ramrootfs

# Mount it to tmpmnt/
Mount-o loop-T ext2 $ {tmppath}/ramrootfs $ {tmppath}/tmpmnt

# Copy everything from kernel to this.
CD $ {tmppath}/tmpmnt
Echo $ {source}
CP-AV $ {source }/*.
CD $ {tmppath}

# Unmount it the ext2 filesystem
Umount $ {tmppath}/tmpmnt
Cat $ {tmppath}/ramrootfs | gzip-9>/$ {rampath}/ramdisk
Echo copying ramdisk image to $ {rampath}
Sync

Here is an automatic generation script!

5. Generate a cramfs

Find the cramfs toolchain
. /Mkcramfs-r $ (fs1_dir) $ (fs_name). 1

6. Generate a mksquashfs
Find the toolchain of squashfs.
. /Mksquashfs $ (fs_dir) $ (fs_name)-noappend-be-lzma-no-fragments-Noi

The most difficult and most likely cause of file system creation is the/lib library and/dev. Please pay attention to these two aspects.

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.