Compile the kernel to make a small Linux system

Source: Internet
Author: User

    1. Objective

Today I will give you how to customize a Linux system, that is, compile the kernel, then why compile the kernel?

Kernel, is the core of an operating system. It is responsible for managing the system's processes, memory, device drivers, files, and network system management, which determines the performance and stability of the system. Linux as a free software, with the support of the vast number of enthusiasts, the kernel version is constantly updated. The new kernel fixes bugs in the old kernel and adds a lot of new features. If users want to use these new features, or want to tailor a more efficient, stable kernel to their own system, they need to recompile the kernel

The kernel source code for the open source operating system (mainly Linux) is compiled natively, which is called Kernel compilation.

The main purpose of compiling the kernel is to change the kernel's default options, such as the kernel originally does not support a hardware, because the kernel's corresponding option default state is disactivated, need to change.

Compile the kernel will have more choices, some features need to be enabled, and some features do not need to start, enable some of the features we need to turn off those useless features, according to their own requirements to customize and optimize the kernel, reduce space, custom a less code of the kernel, but also the speed and performance of the system to run

Second, there are some love friends who study and study, you can try more. Then we'll show you how to compile the kernel.

The first thing to do is to prepare the compilation environment, CENTOS6 installed an operating system (and the machine has been installed on the build environment), kernel files, virtual machine software, I use the things VMware

1, first we add a hard disk on the CENTOS6, it is easy to wait for the small Linux partition, add the hard disk in the case of system shutdown to add

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/85/4A/wKioL1efNj2xp6g5AACzUUCRitw322.jpg-wh_500x0-wm_3 -wmp_4-s_3090780358.jpg "title=" 2.jpg "alt=" Wkiol1efnj2xp6g5aaczuucritw322.jpg-wh_50 "/>

Add a SCSI type of disk, the size of 5G, when adding a disk, the last to choose the location of the disk storage, I specify the table here, so that you can find it, you could also put in a place you find. file that ends in VMDK when the hard disk file

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/85/4B/wKioL1efNwbwrxJ1AAEmFC2gn8A528.jpg-wh_500x0-wm_3 -wmp_4-s_3008008961.jpg "title=" 3.jpg "alt=" Wkiol1efnwbwrxj1aaemfc2gn8a528.jpg-wh_50 "/>


Add a good hard disk after we opened CENTOS6 this virtual machine, after the boot fdisk-l to see if the hard drive has not loaded into, because we add a second disk so the number is/DEV/SDB, find the disk after we began to partition, and format, create two directories, Easy for us to store the system files, I am here to create boot and sysroot in the/MNT directory

Fdisk-l See if the hard drive is loaded, execute the following command after loading

Partition

Fdisk/dev/sdb

N-->P-->1---Enter--->+100m---->w

N-->P-->1---Enter--->+512m---->w

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/85/4B/wKiom1efOG6CIiTWAAB24z9rmQ8905.jpg-wh_500x0-wm_3 -wmp_4-s_291297044.jpg "title=" 3.jpg "alt=" Wkiom1efog6ciitwaab24z9rmq8905.jpg-wh_50 "/>

This allows two partitions to be divided, and the partition completes the format command

Mke2fs-t EXT4/DEV/SDB1

Mke2fs-t EXT4/DEV/SDB2

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/85/4B/wKioL1efOOfjiQVaAADKKtsFj2o732.jpg-wh_500x0-wm_3 -wmp_4-s_1440135568.jpg "title=" 4.jpg "alt=" Wkiol1efoofjiqvaaadkktsfj2o732.jpg-wh_50 "/>

Format complete, create mount point mount partition

Mkdir/mnt/{boot,sysroot}

Mount/dev/sdb1/mnt/boot

Mount/dev/sdb2/mnt/sysroot

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/85/4B/wKioL1efOV6RzaVWAABjoc2-5O0457.jpg-wh_500x0-wm_3 -wmp_4-s_1616413401.jpg "title=" 5.jpg "alt=" Wkiol1efov6rzavwaabjoc2-5o0457.jpg-wh_50 "/>


Create Grub

Grub-install--root-directory=/mnt/dev/sdb, after grub installation is complete, check/mnt/boot/below there is a directory called Grub, if there is a description has been installed successfully, if not present, reinstall GRUB

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/85/4B/wKiom1efOl-BksSMAABeWOmF0l0714.jpg-wh_500x0-wm_3 -wmp_4-s_2324068321.jpg "title=" 6.jpg "alt=" Wkiom1efol-bkssmaabewomf0l0714.jpg-wh_50 "/>

We mount the/mnt/boot directory is we make new system inside the boot partition, Sysroot directory is the root partition, the normal system root partition under a lot of directories, then we also want to create a directory under Sysroot

Cd/mnt/sysroot
MKDIR-PV Etc/{rc.d,init.d} bin sbin root home dev proc sys lib lib64 var/log src usr/{local,share} boot


2. Download kernel files

We go to the kernel of the official site to download http://www.kernel.org, here I show you are 2.6.13 kernel compilation, friends to try to download and your current system kernel version of the kernel files to do.

3. Release the source code

From the official when the downloaded kernel is a compressed file, the kernel of the source code is placed in the compressed file, if you download to your Windows machine inside we can use Xmanger xftp to upload the kernel files to the virtual machine, I am here to wear the/root directory below

Extract the source code into the/usr/src/directory, unzip the complete we create a link file, link the linux-version (kernel version) to the Linux

TAR-XF Tar XF linux-3.13.6.tar.xz-c/usr/src (The decompression process is a bit slow, wait patiently)

When the unzip is complete, link the files to the Linux

LN-SV Linux-3.13.6/linux

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/85/4A/wKioL1efNSjzqZF7AAApYMbvnao211.jpg-wh_500x0-wm_3 -wmp_4-s_258819155.jpg "title=" 1.jpg "alt=" Wkiol1efnsjzqzf7aaapymbvnao211.jpg-wh_50 "/>

Before compiling the kernel, make sure that your CENTOS6 installs the build installation environment, if you do not create a Yum repository, and then install the compiled installation environment, mainly install "development Tools" and "Server Platform development", I've already installed it here.

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/85/4B/wKioL1efO0mhOzeXAAA4C03NDMA899.jpg-wh_500x0-wm_3 -wmp_4-s_2402075107.jpg "title=" 7.jpg "alt=" Wkiol1efo0mhozexaaa4c03ndma899.jpg-wh_50 "/>



After the installation of the compilation environment is complete, we go to the source directory,/usr/src/linux, and then execute make allnoconfig to generate a. confgi file

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/85/4B/wKiom1efO--jj9IbAABbrikuIiU364.jpg-wh_500x0-wm_3 -wmp_4-s_2306717360.jpg "title=" 8.jpg "alt=" Wkiom1efo--jj9ibaabbrikuiiu364.jpg-wh_50 "/>



Then execute make menuconfig (here to remind, if a friend is linked to the virtual machine with Xshell, need to be xshell full screen, or will be error, because you are not enough width, and can not hit the GUI interface)

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/85/4B/wKioL1efPKWRkhaUAADRZ8H5-H0114.jpg-wh_500x0-wm_3 -wmp_4-s_2061385995.jpg "title=" 9.jpg "alt=" Wkiol1efpkwrkhauaadrz8h5-h0114.jpg-wh_50 "/>

Select an item using a space, when the display is * is compiled into the kernel, M is made into modules, below I will list the options to check (all into the kernel), return to the upper menu using the ESC key

If the target system is going to be 64-bit, then we need to tick this option.

64-bit kernel

Plus the local kernel version number

Genral Setup---> Local version-append to kernel release go in and edit like el6-1.1.1


Enable kernel module mount

Enable loadable Module Support


Processor type and characteristics, this is based on your machine's CPU type to choose, Mine is core duo, so

Processor type and features--> under this menu select symmetric multi-processing Support
(Multi-processor support) also processor family (generic-x86-64)--->core 2/newer Xeon


Enable PCI bus

Bus options (PCI etc.) -->PCI Support (tick)


Device drivers

Device Drivers-->fusion MPT Device Support (NEW)--Fusion MPT logging Facility
Device Drivers--->SCSI device support-->scsi device support and SCSI disk support (tick))
Device Drivers-->fusion MPT Device Support (NEW)-->fusion MPT scsihost Drivers for SPI and Fusion MPT misc Device (IOCT L) driver (tick on)


File system

FileSystem-->the Extended 4 (EXT4) filesystem



Executable file format

Executable file formats/emulations--> Kernel support for ELF binaries, Kernel support for scripts starting with #! , Write ELF core dumps with partial segments (NEW)


Install the keyboard driver

Device drivers--> Input device support-->keyboards---> at keyboard (default is American keyboard)


Mouse driver

Device drivers--> Input Device Support--->mice----> PS/2 Mouse (If this is the PS/2 keyboard option, the USB driver is required if it is a USB-type mouse)



Device drivers--> USB Support--->support for host-side usb/xhci HCD (USB 3.0) support and EHCI HCD (USB 2.0) Support Also OHCI HCD (USB 1.1) Support (check that one is available)


Compiling device files
Device Drivers--->generic Driver Options--Maintain a DEVTMPFS filesystem to mount At/dev/automount Devtmpfs a T/dev, after the kernel mounted the Rootfs


Configuring network Features


Networking Support---> Networking options---> option too many, use a graph to indicate

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/85/4C/wKioL1efVEuzIiqnAAC3T_yeljI055.jpg-wh_500x0-wm_3 -wmp_4-s_3716509424.jpg "title=" 10.jpg "alt=" Wkiol1efveuziiqnaac3t_yelji055.jpg-wh_50 "/>

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/85/4C/wKioL1efVVzQqzOTAADp8h7ADmE344.jpg-wh_500x0-wm_3 -wmp_4-s_1804408233.jpg "title=" 11.jpg "alt=" Wkiol1efvvzqqzotaadp8h7adme344.jpg-wh_50 "/>

Unix:socket monitoring interface (socket interface)/unix domain sockets


Device Drivers-->network Device Support--Ethernet driver Support (NEW)

My network card is Intel, so choose

Network Core Driver Support (NEW)

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/85/4C/wKiom1efVpLxpwOzAADd0vOps-s419.jpg-wh_500x0-wm_3 -wmp_4-s_2368157011.jpg "title=" 12.jpg "alt=" Wkiom1efvplxpwozaadd0vops-s419.jpg-wh_50 "/>


After completing the above customization, we will start compiling and installing the Execute make Bzimage

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/85/4C/wKioL1efV6ihF73QAADh75Uix-c831.jpg-wh_500x0-wm_3 -wmp_4-s_292794526.jpg "title=" 13.jpg "alt=" Wkiol1efv6ihf73qaadh75uix-c831.jpg-wh_50 "/>

The compiled files are copied to the/mnt/boot/directory in/usr/src/linux/arch/x86/boot/bzimage

cp/usr/src/linux/arch/x86/boot/bzimage/mnt/boot/


4. Edit some configuration files

Cd/mnt/boot/grub

Compiling the GRUB configuration file

Cd/mnt/boot/grub

Vim grub.conf

Default=0
Timeout=3
Title This is my first Linux (3.13.2)
Root (hd0,0)
Kernel/baimage ro root=/mnt/dev/sda2 Init=/bin/bash


To edit the startup program

cd/mnt/sysroot/sbin/

VIM Init

#!/bin/bash
Echo-e "\twelcome to \033[34mmy first linux\033[0m"
Mount-n-T Proc/proc
Mount-n-T Sysfs Sysfs/sys
Mount-n-T Devtmpfs None/dev
Mount-n-O remount,rw/dev/sda2
/bin/bash


5, the use of script has been a number of commands to the custom host up, the script content is as follows, the main function of the installed system of the command and library files to the target system, #!/bin/bash
#
Target=/mnt/sysroot

Clearcmd () {
If which $cmd &>/dev/null; Then
Cmdpath= ' which--skip-alias $cmd '
Else
echo "No such command"
Return 5
Fi
}

Cmdcopy () {
Cmddir= ' DirName $ '
[-D ${target}${cmddir}] | | Mkdir-p ${target}${cmddir}
[-F ${target}${1}] | | CP $ ${target}${cmddir}
}

Libcopy () {
For Lib in ' LDd $ | Grep-o "/[^[:space:]]\{1,\}"; Do
Libdir= ' DirName $lib '
[-D ${target}${libdir}] | | Mkdir-p ${target}${libdir}
[-F ${target}${lib}] | | CP $lib ${target}${libdir}
Done
}

While true; Do
Read-p "Enter a command:" cmd
If ["$cmd" = = ' quit ']; then
echo "Quit"
Exit 0
Fi
Clearcmd $cmd
[$?-eq 5] && continue

Cmdcopy $cmdPath
Libcopy $cmdPath
Done

After writing, give the script a execute permission, and then you will be prompted to lose the command you want to migrate, such as Bash,cat, Fdisk, Mount LS, touch, ifconfig, route, netstat, tree, mkdir, Rmdir,halt, Shu Tdown,reboot, etc.

It is best to link the/mnt/sysroot/bin/bash to the file named sh

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/85/4D/wKiom1efW2jyTvpQAACdw8rXbB8432.jpg-wh_500x0-wm_3 -wmp_4-s_975089412.jpg "title=" 13.jpg "alt=" Wkiom1efw2jytvpqaacdw8rxbb8432.jpg-wh_50 "/>



6, create a new virtual machine on VMware, will make a good system of disk loading washing virtual machine inside, and then boot up. The previous creation I omitted, to create the disk I put below, select the system version is to choose the 2.x64 bit kernel, we compile this side of the 2.6.x kernel







After you put the created disk in, it starts booting. If you encounter any errors on the way, please leave a message


At this point a small Linux installation is complete.

This article from "Operation and maintenance Career" blog, reproduced please contact the author!

Compiling the kernel to make a small Linux system

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.