Create a simple Linux general idea on VMware
Boot
Root
Initrd.gz grub vmlinuz-2.6.18-308.el5
Bin sbin lib etc proc sys dev boot
With the above content, we can run a very simple Linux. We only need to add various configuration files to it to start the various services we need. Make some preparations before you prepare. 1. Add a new IDE disk to VMware
2. Partition the disk,/dev/hdb1/dev/hdb2, and format it as an ext3 file system.
3. Mount/dev/hdb1 to/mnt/boot; mount/dev/hdb2 to/mnt/sysroot
After completing the prerequisites, we can create boot and root respectively! Let's get started.
Create a boot partition:
●Initrd.gz
Initrd.gz is actually a small root, which also contains bin sbin lib etc proc sys dev mnt directories and an important script file init, implement the Conversion Function from this small root to our real root. The ultimate guarantee is that our linux system is small enough, so we can compress all the above into initrd.gz.
However, to ensure that our linux system can implement various functions, it is not enough to add only directories. Therefore, we use busybox to virtualize various programs in bin sbin usr. Therefore, we need to create a busybox first.
Download the stable version of busybox‑busybox-1.20.2.tar.bz2.
Tar-xf busybox-1.20.2.tar.bz2-C/tmp unzip busybox-1.20.2.tar.bz2 to the/tmp directory
Cd/tmp/busybox-1.20.2 switch to the/tmp/busybox-1.20.2 directory
Make menuconfig uses the graphic window to compile busybox
After entering the graphic interface, it is important to make the library file on which busybox depends into busybox itself, which is only provided for busybox scheduling;
Make install make sure that the command is used in the/tmp/busybox-1.20.2/directory
However, we will find that the installation is not successful.
So I need to download a later kernel header file and put it in/tmp/busybox-1.20.2/include/mtd/
Because I have a linux-2.6.38.5 kernel header file on the virtual machine, so I can copy it directly to the directory above.
Mkdir/tmp/busybox-1.20.2/include/mtd create mtd directory
Cd/tmp/busybox-1.20.2/include/mtd/switch to this directory
Cp/usr/src/linux-2.6.38.5/include/mtd/ubi-user.h./copy the header file to the current directory
Cd/tmp/busybox-1.20.2 and then switch back to the original busybox directory
Make install
After the installation is complete, A _ install directory will be generated under the/tmp/busybox-1.20.2/directory, which indicates that our busybox installation is successful, in addition, the _ install directory contains the bin sbin usr directory and various program scripts we need.
Cp./_ install/*/mnt/boot-a: Copy all the files in the _ install directory to the/mnt/boot directory.
Switch cd/mnt/boot to the/mnt/boot directory, that is, the boot of our small linux
Now we can start writing our true initrd.gz
We found that many directories are missing under the/mnt/boot directory, So we manually add several
Mkdir etc proc sys lib/modules/dev mnt/sysroot tmp-p create mnt/sysroot to achieve root Switching
Create our dev Device File conlole and null
Mknod dev/console c 5 1
Mknod dev/null c 1 3
To drive our root file system properly, we need to copy the ext3 module to the/mnt/boot/lib/modules directory because initrd.gz has an ext3 module that can drive the root partition.
Modinfo ext3
Modinfo jbd check whether jbd depends on other modules
Put the modules of ext3 and jbd under the corresponding/mnt/lib/modules/directory.
Cp/lib/modules/2.6.18-308. el5/kernel/fs/ext3/ext3.ko/mnt/lib/modules/
Cp/lib/modules/2.6.18-308. el5/kernel/fs/jbd. ko/mnt/lib/modules/
The core of logging.
Vim/mnt/boot/init create init script
#! /Bin/sh
Mount-t proc/proc mount proc on the small root to/proc, the file system is proc
Mount-t sysfs/sys mount sysfs on a small root to/sysfs, and its file system is sysfs
Insmod/lib/modules/jbd. ko mount the jbd Module
Insmod/lib/modules/ext3.ko load the ext3 Module
Mdev-s, one of the features provided by busybox, is used to detect all device files and kernel ing files under dev proc
Mount-t ext3/dev/hda2/mnt/sysroot mount root to/mnt/sysroot
Exec switch_root/mnt/sysroot/sbin/init completes conversion from a small root to a real root
Chmod + x init grant init execution permission
The following content is displayed:
We can package and compress all the content, just as we can say, initrd.gz.
Find. | cpio-H newc -- quiet-o | gzip-9>/mnt/boot/initrd.gz
● Grub
The grub directory contains files for various stages required to search for and start the kernel. These files do not need to be written. You only need to use the grub-install command; but we need to write grub's configuration file grub. conf to set some parameters;
Grub-install -- root-directory =/mnt/dev/hdb create the grub directory, specify the parent directory of our boot directory as/mnt, and Boot on the/dev/hdb Disk
Switch cd/mnt/boot/grub to the grub directory.
Let's compile the configuration file grub. conf.
Vim/mnt/boot/grub. conf
Default = 0 indicates the number of kernels to be started by default. Because our small linux has only one kernel, you can simply enter 0.
Timeout = 10: specifies the duration of the kernel to be selected. The default value is 10 seconds.
Title YINSHUO Linux this is the title of our kernel and can be written at will
Root (hd0, 0) indicates the location of the partition where the kernel is located. This indicates that the first partition on the first disk is located.
Kernel/vmlinuz-2.6.18-308.el5 this is where our kernel is
Initrd/initrd.gz this is the location of our initrd File
At this point, our grub file has been configured.
● Vmlinuz-2.6.18-308.el5
Because the compilation of the kernel is complex and slow, I did not compile the new kernel myself, but used the original vmlinuz-2.6.18-308.el5 kernel, so I just copied the original/boot/vmlinuz-2.6.18-308.el5 to the/mnt/boot/directory, so the modules in the kernel are not modified;
Cp/boot/vmlinuz-2.6.18-308.el5/mnt/boot/vmlinuz-2.6.18-308.el5 copies to/mnt/boot/directory
So far, all the boot-related files have been created, and the files with the real root partition have been created.
Create a root partition:
● Bin sbin lib etc proc sys dev boot
When making initrd.gz, we used busybox to virtualize all the scripts we needed in the bin, sbin, and usr directories, we also use the same method when creating a real root partition.
This content is mentioned in initrd.gz.
Cp/tmp/busybox-1.20.2/_ install/*/mnt/sysroot/-
Cd/mnt/sysroot
Rm linuxrc-f
Mkdir-p proc sys etc/rc. d/init. d tmp dev/pts boot var/log lib/modules
Mknod/mnt/sysroot/dev/console c 5 1
Mknod/mnt/sysroot/dev/null c 1 3
After the kernel Initialization is complete, it is necessary to manage the init process. The init configuration file is the/etc/inittab file. The/etc/inittab defines the default startup level of the system and the initialization of the operating system, virtual terminal startup and other content, and OS initialization requires a very important configuration file, that is,/etc/rc. d/rc. sysinit, which is the core file that can be started by the operating system. Therefore, we also need to create/mnt/sysroot/etc/inittab,/mnt/sysroot/etc/fstab,/mnt/sysroot/etc/sysctl under the corresponding directory in small linux. conf, And/mnt/sysroot/etc/rc. d/rc. sysinit files
Edit the/mnt/sysroot/etc/inittab file first.
Vim/mnt/sysroot/etc/inittab create inittab File
: Sysinit:/etc/rc. d/rc. sysinit indicates that the system initialization is executed and the/etc/rc. d/rc. sysinit script is used.
: Respawn:-/bin/sh indicates that once the system exits/bin/sh, it will immediately restart/bin/sh.
: Shutdown:/bin/umount-a-r indicates that when shutdown is executed, the system detaches all file systems.
Next, write the/mnt/sysroot/etc/fstab file, which is used to mount all devices in/etc/fstab
Vim/mnt/sysroot/etc/fstab create/fstab file
Set parameters in fstab
Proc/proc defaults 0 0 means to mount proc to/proc, the file system type is proc, the option is the default option, no backup or check
Sysfs/sys sysfs defaults 0 0 indicates that sysfs is mounted to/sys, the file system type is sysfs, and the option is the default option, neither backup nor check
/Dev/hda1/boot ext3 defaults 0 0 indicates mounting/dev/hda1 to/boot. The file system type is ext3, and the options are the default options. Do not back up or check.
/Dev/hda2/ext3 defaults 0 0 indicates mounting/dev/hda2 to/. The file system type is ext3. The option is the default option, and the check is not performed without backup.
In/mnt/sysroot/etc/rc. d/rc. using the mount-a command in sysinit means that the devices we set in/etc/fstab will be automatically mounted and mounted to the corresponding locations during system initialization and startup.
Then we will compile the Kernel Parameter file/mnt/sysroot/etc/sysctl. conf.
Vim/mnt/sysroot/etc/sysctl. conf
Net. ipv4.ip _ forward = 1 1 indicates that the network is enabled.
Kernel. hostname = yinshuo. linuxtest: Set the host name to yinshuo. linuxtest.
We then follow the previous methods to find the ext3 and jbd modules to copy the pcnet32 module and its dependent mii module. They are the virtual NIC Driver of VMware.
Cp/lib/modules/2.6.18-308. el5/kernel/drivers/net/pcnet32.ko/mnt/sysroot/lib/modules/pcnet32.ko
Cp/lib/modules/2.6.18-308. el5/kernel/drivers/net/mii. ko/mnt/sysroot/lib/modules/mii. ko
Now let's write the system initialization script file/mnt/sysroot/etc/rc. d/rc. sysinit
Vim/mnt/sysroot/etc/rc. d/rc. sysinit
#! /Bin/sh
Echo-e "\ t \ 033 [31 mYinShuo little Linux \ 033 [0 m" display YinShuo little Linux in red
Mount-t proc/proc mount proc to/proc, the file system type is proc
Mount-t sysfs/sys mount sysfs to/sys, the file system type is sysfs
Mount-o remount, rw/re-mount the root file system in read/write mode
Mdev-s detects all devices and creates a device file under dev.
Mount-a mount all devices under/etc/fstab
Run sysctl-p to make the Kernel Parameter file/etc/sysctl. conf take effect immediately.
Insmod/lib/modules/mii. ko load kernel module mii
Insmod/lib/modules/pcnet32.ko load kernel module pcnet32
Ifconfig lo 127.0.0.1/8 set the local loopback ip address = 127.0.0.1 netmask = 255.0.0.0
Ifconfig eth0 172.16.106.9/16 set the ip address of the network adapter eth0 = 172.16.106.9 netmask = 255.255.255.0.0
Chmod + x/mnt/sysroot/etc/rc. d/rc. sysinit grant/mnt/sysroot/etc/rc. d/rc. sysinit execution permission
So far, we have created the root partition.
Create a virtual machine:
Create a new virtual machine named linuxtext
Use the IDE disk we created earlier
Select our IDE disk
Our small virtual machines have been created.
The procedure for starting Linux:
Steps:
①: Grub will first look for the location of the kernel. According to the configuration information in the grub. conf file, it will prompt us to select which kernel. This selection interface will take 10 seconds.
②: The last step of the handler is to convert your little root to the real root, and complete your mission. Then the kernel starts the first process init of the user space, then the kernel initialization ends.
③: In the user space, the content in/etc/inittab will be executed first, and/etc/rc will be started first. d/rc. sysinit initializes the operating system, including displaying "YinShuo little Linux", mounting pseudo files, and remounting our root file system in read/write mode; test all hardware devices, create device files, Mount all content in/etc/fstab, set Nic options and set host names, and the operating system initialization ends.
④/Etc/rc. d/rc. after the sysinit task is completed, the/etc/inittab starts the second item and directly enters the/bin/sh interface to provide the user interaction interface. We can use various commands.
This is our simple Linux operating process. Let's wait and see!
Witness a miracle:
Run our Linux
It indicates that we have succeeded!
Let's verify it.
Ifconfig
Hostname
Verify whether there is busybox and whether the file can be created with the execution permission in small linux.
Ls/bin view/bin content
Touch/tmp/chenggongde create the chenggongde file in the/tmp/directory.
So far, a simple Linux has come to an end. Of course, it is very simple, and there are many things that need to be improved, such as kernel compilation and user login, which have not been solved. The purpose of this Linux production is to understand and learn the Linux Startup Process and provide a clearer framework for Linux.