Create the smallest root file system for Linux __linux

Source: Internet
Author: User
Tags bz2 gz file prepare root access
Create the smallest root file system for Linux

When compiling the kernel, you can specify a folder as the root file system for kernel startup, which is called Initramfs in Linux.

The practice is as follows (take i386 as an example)

1. Download kernel files

wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.26.tar.bz2

2. Extract the kernel

Bzip2-d linux-2.6.26.tar.bz2 generates a Linux-2.6.26.tar file, and then

Tar xvf Linux-2.6.26.tar

After decompression, there will be a linux-2.6.26 folder exists

3. Prepare a folder for the Iniramfs file system

Create a folder under the Linux-2.6.26 folder Myinitramfs

Write a test for Hello World, named Hello.c, as follows:

#include <stdio.h>

#include <unistd.h>

int main (int argc,char *argv[])

{

int i = 0;

while (1) {

printf ("Hello World (%d) \ n", i);

}

return 0;

}

Compiling Gcc-static-o init hello.c

Copy init to the Myinitramfs folder.

CP Init myinitramfs/

Because you need to display text, you also need to prepare the console device file under the folder.

mkdir Myinitramfs/dev

Cp-a/dev/console myinitramfs/

4. Compiling the kernel

Under the linux-2.6.26 file, execute make help.

Will see a lot of help information, one of which is i386_defconfig

Execution of Make i386_defconfig generates a. config file.

In order to add the previously prepared folder to the kernel profile, you will also need to reconfigure the config file

Make Config

In the general Setup--->

Initial RAM filesystem and RAM disk (INITRAMFS/INITRD) support (BLK_DEV_INITRD) [y/n/?]

Initramfs source file (s) (Initramfs_source) [Myinitramfs]

, enter the prepared folder.

Once configured, the following definition is available in the. config file

Config_initramfs_source= "Myinitramfs"

Save. config

Make compile kernel

5. Test kernel and Initramfs with QEMU

Qemu-kernel LINUX-2.6.26/ARCH/I386/BOOT/BZIMAGE-INITRD Linux-2.6.26/usr/initramfs_data.cpio.gz/dev/zero

initramfs_data.cpio.gz This file is automatically generated by the kernel, the specific name may differ from the system or the kernel, but the suffix should be. cpio.gz

We've got it started.

In order not to look so boring, we try to show the process through a look at the example.

Well, we still put "Hello World" as the first program to put into the Initramfs. Then you can mount/proc and/sysfs the past. But here we just need to put/init past. The end of the program we use sleeping instead of exiting, this is mainly to consider if the PID 1 program exit, kernel will panic, which will interfere with our view.

#include 

int main (int argc, char *argv[])
{
  printf ("Hello world\n");
  Sleep (999999999);
}

And then, statically compiled, and then we don't have to think about copying the libraries that are needed.

Gcc-static Hello.c-o Hello

If you execute this applet at the command line, it will print Hello World and let it stop there. You can use Ctrl-x to get it out. If Initramfs executes this program, we will see that at the end of the boot messages, a "Hello World" is printed.

Note: If you want to put it on your development Board, remember to use your cross compilation tool. The process of packaging is not a peaceful platform, but binary files require the compiler of the target system.

So, how do we get this program to kernel? Well, there are four basic approaches: the first is to take cpio.gz as a separate package and tell Bootloader where it is, or you can compile Initramfs directly into kernel using one of the following three methods.

Take cipo.gz as a separate file.

Many people like to compile it into the kernel, and if you want to, you can do it. But we're going to do it in a different way now. We can enable the INITRD support of the kernel and then use cpio.gz instead of RAMDisk (INITRD). The smart kernel will automatically detect the type of file, then unzip our compressed package into rootfs; it is not creating a RAM disk, which does not affect the advantage of INITRAMFS memory efficiency.

Because the external INITRAMFS is executed after built-in Initramfs, if two files contain the same name, the standalone file will cover the built-in stuff. This means that you can update or ucstomize your rootfs without having to change your kernel without modifying the kernel.

The other good news is that you don't have to worry about license. You can run NON-GPL programs in Rootfs, or provide NON-GPL firmware for your driver ... Well, if you compile it into the kernel, it's a kernel modification. Make your own Initramfs, just be used, you don't have to publish your source code Oh pro.

So, how to make cpio.gz file. One way to do this is to use CPIO and gzip commands to compress yourself. Of course, you can also use kernel build to do this, if you think it's not that troublesome. The original intention to do, just knock the following code into ...

mkdir Sub
cp Hello sub/init
CD sub find
. | cpio-o-H NEWC | gzip >. /initramfs_data.cpio.gz
cd ...
RM-RF Sub

According to the traditional method of using INITRD, put the above generated initramfs_data.cpio.gz in the place to go (don't ask me where I want to put, I do not know), it will be at the end of the boot for you to print a beautiful "Hello World", Then wait for some time and reboot.

Give it a try.

If it's not working, as usual you should check to see if initial RAMDisk support is selected, and then look at whether your init program is statically linked, and see if it is executing permissions again, or whether the name is right. You can use the following command to extract any Initramfs files to the current folder: Zcat initramfs_data.cpio.gz | Cpio-i-d-h newc--no-absolute-filenames

Compile the Initramfs into the kernel.

The easiest way to use INITRAMFS is to replace the empty one in the kernel with the cpio.gz that has been done. This is 2.6 kernel, so you don't have to make any special settings.

Kernel's config option has a config_initramfs_source (i.e. General Setup--->initramfs source file (s) in Menuconfig). This option points to all the files that are needed to pack Initramfs in the kernel. By default, this option is left blank, so when the kernel is compiled, the Initramfs is empty, which is what the aforementioned Rootfs does.

Config_initramfs_source can be an absolute path, or it can be a relative path that starts with the kernel ' top build dir (where you typed build or make). There are three goals to target: a cpio.gz that has been done, or a folder that has all the content ready for the cpio.gz, or a text configuration file. The third approach is the most flexible, and we'll start by introducing the three methods in turn.

1 Use a cpio.gz file that is already done

If you are already have your own initramfs_data.cpio.gz file (because you created it yourself, or saved the cpio.gz file Produ CED by a previous kernel builds), can point config_initramfs_source at it and the kernel build would autodetect the file Type and link it into the resulting kernel image.

can also leave config_initramfs_source empty, and instead copy your cpio.gz file to usr/initramfs_data.cpio.gz in your Kernel ' s build directory. The kernel ' s makefile won ' t generate a new archive if it doesn ' t need to.

Either way, if you build a kernel like this can boot it without supplying a external initrd image, and it ' ll still fi Nish its boot by running your init program out of Rootfs. This is packaging method #2, if you are like to try it now.

2 Specifies to the kernel a file or folder

If Config_initramfs_source points to a directory, the kernel'll archive it up for you. This is a very easy way to create a INITRAMFS archive and #3.

Interestingly, the kernel build doesn ' t is the standard CPIO command to create INITRAMFS archives. You are don ' t even need to have the any Cpio tools installed in your build system. Instead the kernel build (in usr/makefile) generates a-text file describing the directory with the script gen_initramfs_l Ist.sh ", and then feeds that descript-a program called" Gen_init_cpio "(built from C source in the kernel's USR directo RY), which create the Cpio archive. This is looks something like the following:

scripts/gen_initramfs_list.sh $CONFIG _initramfs_source > Usr/initramfs_list
usr/gen_init_cpio Usr/initramfs _list > Usr/initramfs_data.cpio
gzip usr/initramfs_data.cpio

To Package/Hello World program, your could simply copy it into its own directory, name it ' init ', point Config_initr Amfs_source at directory, and rebuild the kernel. The resulting kernel should end it boot by printing "Hello World". And if you are need to tweak the contents of this directory, rebuilding the kernel would re-package the contents of that direct Ory if anything has changed.

The downside of this (is) that it if your Initramfs has device nodes, or cares about file ownership and permissions, You are need to is able to create those things into directory for it to copy. This is hard to does if you haven ' t got root access, or are using a cross-compile-like environment. That's where the fourth and final method comes in.

3 Use configuration file initramfs_list to tell the kernel where Initramfs is

This is the most flexible method. The kernel ' s gen_initramfs_list.sh script creates a text description file listing the contents of Initramfs, and Gen_init_ Cpio uses this file to produce a archive. This is a standard text file, easily editable, and containing one line per file. Each line starts with a keyword indicating what type of entry it describes.

The config file to create we "Hello World" Initramfs only needs a:

File/init Usr/hello 500 0 0

This is takes the "hello" and packages it shows up as/init in Rootfs with permissions, with UID and GID 0. It expects to find the source file "Hello" in a "USR" subdirectory under the kernel's build directory. (If you are building the kernel in a different directory than the source directory, this path would is relative to the Buil Directory d, not the source directory.

To try it yourself, copy "Hello" into usr in the kernel ' s build directory, copy the above configuration line to its own fi Le, use ' Make menuconfig ' to ' point Config_initramfs_source ' to ' that file, run the kernel build, and test boot the new Kerne L. Alternately, can put the "hello" file in its own directory and use "scripts/gen_initramfs_list.sh dirname" to creat e a configuration file (where DirName is the path to your directory, from the kernel ' s build directory). For large projects, your may want to generate a starting configuration with the script, and then customize it with any text Editor.

This is configuration file can also specify device nodes (with the "nod" keyword), directories ("dir"), Symbolic links ("Slin K "), named FIFO pipes (" Pipe "), and UNIX domain sockets (" sock "). Full documentation to this file ' s format is available by running ' Usr/gen_init_cpio ' (with no arguments) after a kernel bu Ild.

A more complicated example containing device nodes symlinks and could look like this:

  Dir/dev 755 0 0
  nod/dev/console 644 0 0 C 5 1 nod/dev/loop0
  644 0 0 B 7 0
  dir/bin 755
  1000 1000 Slink  /bin/sh BusyBox 777 0 0
  file/bin/busybox initramfs/busybox 755 0 0
  dir/proc 755 0 0
  dir/sys 755 0 0
  dir /MNT 755 0 0
  file/init initramfs/init.sh 755 0 0

One significant advantage of the configuration file method are that any regular user can create one, specifying ownership a ND permissions and the creation of device nodes in Initramfs, without "any special permissions on the" build system. Creating a Cpio archive using the Cpio command line tool, or pointing the kernel builds at directory A, requires a Director Y that contains everything Initramfs would contain. The configuration file method merely requires a few source files to get data from, and a description file.

This also comes into handy cross-compiling from other environments such as Cygwin, where is the local filesystem could not even B e capable of reproducing everything Initramfs should have in it.

To sum up

These four ways of providing content to rootfs all have one thing in common: A series of files are extracted to the rootfs when the kernel is started, and if kernel can find the executable file "/init", kernel will run it; This means that kernel will not bother Where does "root=" point to?

In addition, once the INIT process inside the INITRAMFS runs, kernel thinks the boot is complete. Next, Init will control the universe. It has a thunderbolt invincible dedicated process ID reserved for it #1, the entire system will be the next to create it. Also, its status will be inalienable, ah hum, PID 1 exit, the system will panic.

Next I'll introduce some of the other things that the INIT program can do in Rootfs.


Uboot has been burned successfully, the Linux kernel is also burned, and the root file system can be packaged and burned. But if it's cumbersome to pack and burn every kernel or program file, the next step is to use TFTP and NFS to get uboot to automatically load the kernel and mount the root file system from the Ubuntu file system.

First install TFTP:
TFTP and NFS is to facilitate debugging, the Development Board connected to the network cable can be connected to the PC TFTP, download the compiled Linux kernel uimage, hooked up on the PC NFS Network File system as the root file system, so that you can compile the program files can be directly published to the NFS folder, Then you can run it directly on the Development Board.

(XINETD is a network service main program, typically to be installed with TFTP)
root@ubuntu:/# apt-get Install xinetd tftpd tftp
Reading Package Lists ... Done
Building Dependency Tree
Reading state information ... Done
The following packages were automatically installed and are no longer required:
Ssl-cert
Use the ' Apt-get autoremove ' to remove them.
The following NEW packages would be installed:
TFTP tftpd xinetd
0 upgraded, 3 newly installed, 0 to-remove and not upgraded.
Need to get 0b/185kb of archives.
After this operation, 578kB of additional disk space would be used.
Selecting previously deselected package tftp.
(Reading database ...) 40278 files and directories currently installed.)
Unpacking tftp (from .../tftp_0.17-17ubuntu1_i386.deb) ...
Selecting previously deselected package xinetd.
Unpacking xinetd (from .../xinetd_1%3a2.3.14-7ubuntu2_i386.deb) ...
Selecting previously deselected package tftpd.
Unpacking tftpd (from .../tftpd_0.17-17ubuntu1_i386.deb) ...
Processing triggers for man-db ...
Setting up TFTP (0.17-17UBUNTU1) ...
Setting up xinetd (1:2.3.14-7UBUNTU2) ...
* Stopping Internet SuperServer xinetd [OK]
* Starting Internet SuperServer xinetd [OK]

Setting up TFTPD (0.17-17UBUNTU1) ...
---------IMPORTANT information for XINETD USERS----------
The following line is added to your/etc/inetd.conf file:

TFTP dgram UDP wait nobody/usr/sbin/tcpd/usr/sbin/in.tftpd/srv/tftp

If you are are indeed using xinetd, you'll have to convert the
Above into/etc/xinetd.conf format, and add it manually. The
/usr/share/doc/xinetd/readme. Debian for more information.
Suggested entry (automatically converted using Itox):

Service TFTP
{
Socket_type = Dgram
protocol = UDP
Wait = yes
user = Nobody
Must use Option-daemon_dir if your use TCPD
-----------------------------------------------------------

To create a directory:
root@ubuntu:/# mkdir Tftpboot
root@ubuntu:~# chmod O+w/tftpboot
root@ubuntu:~# Chown-r Nobody/tftpboot

To edit a TFTP configuration file:
root@ubuntu:/# vi/etc/xinetd.d/tftp
Press I into edit mode and enter the following:
Service TFTP
{
Socket_type = Dgram
protocol = UDP
Wait = yes
user = root
Server =/USR/SBIN/IN.TFTPD
Server_args =-s/tftpboot
Disable = no
Per_source = 11
CPS = 100 2
Flags = IPV4
}
Press ESC, enter: Wq command to save exit.

Reboot xinetd:
root@ubuntu:/#/etc/init.d/xinetd Reload
* Reloading Internet SuperServer configuration xinetd [OK]
root@ubuntu:/#/etc/init.d/xinetd Restart
* Stopping Internet SuperServer xinetd [OK]
* Starting Internet SuperServer xinetd [OK]

Find and test TFTP:
root@ubuntu:/

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.