Linux: build your own exclusive mini Linux (2) Improve the small Linux function to make it full

Source: Internet
Author: User
Tags network function

Create a small Linux with network functions

First, copy the required basic network commands to a Linux instance.
Copy Ping and ifconfig
To add a network function, you must be driven by the NIC. The NIC Driver is pcnet32.
However, pcnet32 has a dependency module that is MII, and MII has no dependency module,
So we only need to copy the two modules to our
In Linux, check the path of the two modules and use modinfo to view them,
After checking, we found that we need to create a directory named modules under/mnt/sysroot/lib,
Switch to this directory, and then create
The modules directory copies the preceding two modules in the current directory,

cp /lib/modules/2.6.18-164.el5/kernel/drivers/net/pcnet32.ko ./cp /lib/modules/2.6.18-164.el5/kernel/drivers/net/mii.ko ./

After that, we need to mount and use these two modules during startup of small Linux,
The system starts to load additional device drivers at/etc/rc. d/rc. sysinit.
In the script, and assign the IP address, then edit the script
Add the following content to it:

 [  -d  /lib/modules/mii.ko  ]   &&  /sbin/insmod   /lib/modules/mii.ko [  -d  /lib/modules/pcnet32.ko  ]  &&  /sbin/insmod  /lib/modules/pcnet32.ko echo "Load The ethernet card drivers ok"

There is no bash environment variable in this script, so you must use the full path of the command, remember

 echo "set address for lo..." /sbin/ifconfig 127.0.0.1/8  echo "set address for eth0..." /sbin/ifconfig 192.168.0.188/24

 

######################################## ############################
In Linux, use/etc/sysctl. conf to configure kernel parameters:

In which step is the configuration of Linux kernel parameters completed? Completed in the/etc/rc. d/rc. sysinit script, which is used to set Kernel Parameters

if  [[  -x /sbin/sysctl && -r /etc/sysctl.conf  ]] ; then          echo  {1}quot;configuring  kernel parameters............."          /sbin/sysctl -p /etc/sysctl.conf          echo {1}quot;...........kernel parameters  done."fi

 

Add this script to/etc/rc. d. Rc. sysinit to configure kernel parameters when Linux is started.
Then we will find that the file/etc/sysctl. conf does not exist in our small Linux.
Therefore, we need to create this file under it. The format is as follows: Write what you need, not copy it all !!!

# Kernel sysctl configuration file for Red Hat Linux## For binary values, 0 is disabled, 1 is enabled.  See sysctl(8) and# sysctl.conf(5) for more details.# Controls IP packet forwardingnet.ipv4.ip_forward = 0# Controls source route verificationnet.ipv4.conf.default.rp_filter = 1# Do not accept source routingnet.ipv4.conf.default.accept_source_route = 0# Controls the System Request debugging functionality of the kernelkernel.sysrq = 0# Controls whether core dumps will append the PID to the core filename# Useful for debugging multi-threaded applicationskernel.core_uses_pid = 1# Controls the use of TCP syncookiesnet.ipv4.tcp_syncookies = 1# Controls the maximum size of a message, in byteskernel.msgmnb = 65536# Controls the default maxmimum size of a mesage queuekernel.msgmax = 65536# Controls the maximum shared segment size, in byteskernel.shmmax = 4294967295# Controls the maximum number of shared memory segments, in pageskernel.shmall = 268435456

######################################## ###################
How to switch Linux to runlevel 1
The default running level of the system is defined in the/etc/inittab,

ID: 3: initdefault: defines the default running level # system initialization. there are also seven levels of the script definition Si: sysinit:/etc/rc. d/RC. sysinitl0: 0: Wait:/etc/rc. d/RC 0 to enable any service at this level, you only need to disable all services L1: 1: Wait:/etc/rc. d/RC 1l2: 2: Wait:/etc/rc. d/RC 2l3: 3: Wait:/etc/rc. d/RC 3 if Level 3 is selected by default, scripts in the written directory are run to enable the Service L4: 4: Wait:/etc/rc at this level. d/RC 4l5: 5: Wait:/etc/rc. d/RC 5l6: 6: Wait:/etc/rc. d/RC 6

Level 0 1 6 is a special level

Add a line in the/etc/inittab of Linux

# for runlevell1:1:wait:/sbin/init -t1 S

With this line of improper, your small Linux can switch to level 1.

######################################## ########################
Shutdown a Linux instance

Although init 0 can be used to shut down, it is actually implemented by calling halt.
You only need to define the/etc/inittab,

# for runlevell0:0:wait:/etc/rc.d/rc.shutdown

 

To achieve shutdown, you should also do that any data that has not been synchronized to the disk in the memory is synchronized to the disk,
This requires the sync command. Remember to copy the sync command to a Linux instance.
After synchronizing data to a disk, You need to detach all file systems and shut down the system after detaching the disk.
What about the shutdown command?
We now create the script "/etc/rc. d/" RC. shutdown ". [remember to copy chmod to a Linux instance]
Write the following content in this script:

#! /Bin/bash # Sync synchronize data that is not synchronized to the disk in the memory sleep 1 Let it sleep for one second is that the data is completely synchronized umount/dev/sda1 in your system as above in/etc/ you can run the umount-a command to unmount all umount/dev/sda2swapoff-a to close all file systems marked as swap exec/sbin/halt-P. avoid turning off sub-shell

 

In this way, shutdown can be realized in the true sense,

Remember to add these items in your big script for automatic perfection
######################################## #############################
Use the account and password to log on to Linux:
If CP-D is a link, copy the link.
1. Start at least one terminal
2. Print the login prompt to the user
3./etc/passwd,/etc/shadow,/etc/group, and/etc/gshadow
Can we just use the above?
The system identifies users by ID numbers. The correspondence between user accounts and ID numbers is within/etc/passwd.
But if you don't put this information here, doesn't the system know the corresponding relationship?
In this way, the system must provide a mechanism to let the system know where to convert the user's account to the ID.
Instead of finding login, you can find it here. Instead, login calls the library file to convert the user's account and password,
These library files are not closely coupled with login. Therefore, it is up to us to determine which library file to call is not called by login.
One mechanism is implemented. This mechanism is called the name service switch, that is, nsswitch. It does not have only one configuration file in/etc/nsswitch. conf.
You only need to provide this configuration file to login. When login accepts the account entered by the user, it must go to a location to verify the ID number.
In this configuration file, it is defined to be searched according to that mechanism. If it is defined to search by file, login will
Find the corresponding library file, and then find the user's ID from the library file, so that the user can change this mechanism
Therefore, these library files correspond to one mechanism, and there will be a superquery method.
Generally, these library files are stored in/lib.
Actually there are also/usr/lib,
Ls-L/usr/lib | grep libnss
You can see many linked files
Libnss_files.so is searched in passwd.
For us, we only need files. Of course, files must be combined with the master database file
The main library file is libnss3.so, libnssckbi. So, and so, they need to be left, and files must be combined to work
Compat, so we need to retain it,

When we copy the library file, the copy is a link, but the actual copy is the source file, so we need to use the new CP command
CP-d Keep link,
In this way, the file cannot be found after the upgrade, and the link will not be added.

cp /usr/lib/libnss3.so /mnt/sysroot/usr/lib                 libnssckbi.so                 libnssutil3.socp -d         libnss_files.so                 libnss_compat.so

 

Of course, the last two copies are dead links, and you need to copy the files they direct.
These files are in/lib, LS-L/lib | grep libnss
Copy the files and compat files to the corresponding directory.

Then start at least one terminal:
Edit/mnt/sysroot/etc/inittab and add it.

# Run gettys in standard runlevels1:2345:respawn:/sbin/mingetty tty12:2345:respawn:/sbin/mingetty tty23:2345:respawn:/sbin/mingetty tty34:2345:respawn:/sbin/mingetty tty45:2345:respawn:/sbin/mingetty tty56:2345:respawn:/sbin/mingetty tty6

At this time, when we start/etc/rc. d/rc. sysinit, A/bin/bash user is started to log on
It is not allowed to start again. In this case, we should re-edit the file in our small Linux and remove the "/bin/bash" file.
Then we need to print the logon prompt. Now we need a login. We need to put this login under the corresponding directory of our small Linux, that is,/bin/login.
However, this login is dependent on the library file, so we should copy the corresponding library file, that is, CP-D/lib/libcrypt. so.1

CP-D/lib/libcrypt-2.5.socp/lib/libm. so.6 so that you can achieve the sync remember to knock a few more times

Then you need user-related information. If you are not in trouble, define the file yourself, or directly copy the original file.
CP-P reserve original Permissions

Then copy/etc/nsswith. conf is ready for small Linux. Next, enable Level 1. edit/etc/inittab in small Linux and add L1: 1: wait: /sbin/init-T1 s

 

Now you can witness the miracle.
######################################## ########################
Displays the Host Name and logon information.

In the Linux Startup Process, we know that there is a script named for the host, that is,/etc/rc. d/rc. sysinit.
The host name is stored in/etc/sysconfig/network, so we should create this file under it.

Write hostname = little.linux.org in this file.
Then we modified the RC. sysinit script and wrote it in the script.

[ -r /etc/sysconfig/network ]  &&  source /etc/sysconfig/network[ -z $HOSTNAME ] && HOSTNAME = localhostecho "$HOSTNAME" > /proc/sys/kernel/hostname

If you want to print the logon information in/etc/issue, create the following and edit it.
Welcome to little.linux.org.

In Linux, create a file named bash_profile under root and write the file

PS='[\u@\h \W]\

You can,
Then execute the following source ~ /. Bash_profile takes effect

Related Article

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.