Proficient in building initramfs stepbystep (6)

Source: Internet
Author: User
Article Title: proficient in building initramfs stepbystep (6 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

20. switch_root command

Except for Systems Based on init ramfs (for example, mini linux in section 4), init ramfs is usually used to prepare for installing the final root file system, the final step is to install the final root file system, and then switch to the new root file system. Previously, ramdisk-based initrd switched to the new root file system using the cmdt_root command, and then uninstalled ramdisk. However, init ramfs is rootfs, while rootfs cannot neither initt_root nor umount. To switch from init ramfs to the new root file system, you need to do the following:

(1) Delete All rootfs content and release space

Find-xdev/-exec rm '{}'';'

(2) install the new root file system and switch

Cd/newmount; mount -- move./; chroot.

(3) append stdin/stdout/stderr to the new/dev/console, and then execute the init program of the new file system.

The above steps are troublesome and an important problem must be solved: the first step is to delete all the commands for deleting rootfs. How can I use these commands to complete other steps in the future? The solution of busybox is to provide the switch_root command to complete all the processing, which is very convenient to use.

The format of the switch_root command is:

Switch_root [-c/dev/console] NEW_ROOT NEW_INIT [ARGUMENTS_TO_INIT]

NEW_ROOT is the Mount directory of the actual root file system. It needs to be mounted to the system before running the switch_root command; NEW_INIT is the path of the init program of the actual root file system, it is usually/sbin/init;-c/dev/console is an optional parameter, used to redirect the device files of the actual root file system, which is generally not used; ARGUMENTS_TO_INIT is the parameter passed to the init program of the actual root file system and is optional.

Note that the switch_root command must be called by the PID = 1 process, that is, it must be directly called by the init program of init ramfs, and cannot be called by other processes derived from init, otherwise, an error occurs. The prompt is:

Switch_root: not rootfs

Likewise, the init script must use the exec command to call the switch_root command. Otherwise, an error occurs:

Switch_root: not rootfs

  21. Practice: Use init ramfs to install the CLFS root file system

Use the switch_root command to switch the hard disk partition of a CLFS root file system. My CLFS is installed in the/dev/sda8 hard disk partition. Let's take this as an example.

We still build it in the previous image directory.

(1) rewrite the init script

#! /Bin/sh

Mount-t proc/proc

Mount-t sysfs/sys

Mdev-s

Mount/dev/sda8/mnt (NOTE: For simplicity, we directly write the CLFS partition to the init script)

Exec switch_root/mnt/sbin/init

[1] [2] Next page

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.