HOWTO: Kernel Compilation for Newbies

ubuntu官方论坛中,写得如何编译ubuntu的内核的文章。
我昨天重新编译内核时看到的,转过来
 
This guide is aimed at the newbies who are willing to learn something about kernel compilation or just who need a new kernel for incompatibility issues (e.g. DMA issues). This is a STEP-BY-STEP guide, so don’t be afraid of compiling your first kernel, it’s a piece of cake. Moreover if you need to compile any kernel module (nvidia modules, ndiswrapper, etc.) I will also explain how to do it.
 
It takes a while (even an hour) to complete the process described below, so make sure you have enough time to spend.
 
Make sure you have all the repositories enabled

BEFORE YOU START
 
If you have an Nvidia or ATI graphic card and you are using a proprietary driver (i.e. if you have installed the graphic driver before) please do this, otherwise get straight to step 1:
 
Open Terminal or Konsole and type these commands:
 
(if you are using GNOME, the graphic interface that comes with Ubuntu)
sudo gedit /etc/X11/xorg.conf
 
Otherwise
 
(if you are using KDE, the graphic interface that comes with Kubuntu)
sudo kate /etc/X11/xorg.conf
 
Scroll down the text until you find this section (this is my configuration):
 
Section "Device"
Identifier "ATI Technologies, Inc. Radeon 330M/340M/350M (RS200 IGP)"
Driver "ati"
BusID "PCI:1:5:0"
 
Substitute the word in red with “vesa” or “nv” (NVIDIA’s opensource driver) or “ati” (ATI’s opensource driver), make it look like this:
Section "Device"
Identifier "ATI Technologies, Inc. Radeon 330M/340M/350M (RS200 IGP)"
Driver "vesa"
BusID "PCI:1:5:0"
 
Save and exit. Restart the computer and go to the next step.
 
When you have the new kernel working you might want to reinstall the proprietary drivers for your graphic card. Have a look at my guide for the NVIDIA drivers: use method 2 if you want to install them manually OR compile the module with your kernel (as described below) and follow method 1 in my guide.
 
1) Open Terminal or Konsole and type these commands
 
uname -r (so as to see what kernel you are using)
 
NOTE: if you want to compile a vanilla kernel from kernel.org have a look at the end of the guide.Otherwise proceed with step 2.
 
2) sudo apt-get install linux-tree (this will download Ubuntu kernel sources and patches for kernel 2.6.12)
 
3) Open Terminal or Konsole (if it’s not open yet) and type these commands:
 
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install kernel-package
sudo apt-get install gcc (this will install gcc-4.0 for kernel 2.6.13 or superior)
sudo apt-get install gcc-3.4
sudo apt-get install libncurses5
sudo apt-get install libncurses5-dev
sudo apt-get install libqt3-mt-dev
sudo passwd root
(and set the root password which you will need later)
 
cd /usr/src
sudo tar –bzip2 -xvf linux-source-2.6.12.tar.bz2
sudo ln -s /usr/src/linux-source-2.6.12 /usr/src/linux
cd /usr/src/linux
 
NOTE: if the computer says "file exists" when you try type this command "sudo ln -s /usr/src/linux-source-2.6.12 /usr/src/linux", you have to type "sudo rm /usr/src/linux" (this will remove the old link). Now you wont’ have this error and the command "sudo ln -s /usr/src/linux-source-2.6.12 /usr/src/linux" will work.
 
sudo make oldconfig (so as not to compile the entire kernel from scratch)
 
If it makes you any questions just press Enter (so as to select the recommended answer)
 
sudo make menuconfig
 
4) Now you have to use the keyboard to move the cursor over the function or submenu you want and press Enter to select it.
 
Select the 4th option: Processor type and features
 
If you have a multiprocessor system you might want to enable "Symmetric multi-processing support" (SMP): select it with your keyboard and press the spacebar to enable it (a "*" will appear beside it). If you don’t have it don’t enable it.
 
Select Processor Family and choose the right one (i386 in my case) depending on the output of the command “uname -r” you have used before
 
Press the right arrow and select exit
 
If you have more than 900MB RAM then you’ll definitely need this, otherwise (or if you are using Ubuntu 64 bit) skip this step:
Scroll down the text until you find “High Memory Support”.
Select it.
You’ll have three possible choices:
Off
4GB (if you have no more than 4GB RAM)
64GB (if you have more than 4GB RAM)
Select one of these functions and press enter.
Press the right arrow and select exit
 
The following operation is required if you want to enable DMA directly in your kernel. You might want to try this if none of the methods found in this forum works for you, Otherwise skip it and go to Step 5.
NOTE: if you have old hardware which doesn’t support DMA you won’t be able to access this hardware with your new kernel. If this happens go straight to Step 8.
 
Select Device drivers
 
then
 
Select ATA/ATAPI/MFM/RLL support
 
Scroll down the text until you find (and highlight with the keyboard) “Enable DMA only for disks” and disable it by pressing N (now the “*” beside it should disappear, this means it is not selected any more)
 
Press the right arrow and select exit
 
5) Keep on selecting Exit until it asks you whether you want to save your new configuration or not. Answer yes.
 
Now you are back to the command line, type:
 
6) su
CC=gcc-3.4
export CC
exit
CC=gcc-3.4
export CC
 
7) DO THIS ONLY IF YOU NEED SOME RESTRICTED MODULE otherwise go to Point 8
 
There will be no restricted modules for the kernel you are going to compile. However if you need them you can pick the modules you need and compile them together with your kernel.
 
Get the source package/s (from Ubuntu’s repositories or from the web) and extract it under /usr/src
 
For example if you need ndiswrapper and or the nvidia drivers:
 
sudo apt-get install ndiswrapper-utils
 
sudo apt-get install ndiswrapper-source
 
sudo apt-get install nvidia-kernel-source
OR
sudo apt-get install nvidia-legacy-kernel-source (if you have a old card which belongs to the list in the “Nvidia Legacy Cards” section at the end of the guide)
 
[If you have got the package from the web (and not from the repositories) extract the file wherever you want and then type: sudo mv /path_to_the_folder_of_the_extracted_file /usr/src ]
 
cd /usr/src/
 
sudo tar –bzip2 -xvf ndiswrapper-source.tar.bz2
 
sudo tar -xvf nvidia-kernel-source.tar.gz
OR
sudo tar -xvf nvidia-legacy-kernel-source.tar.gz(if you have a old card which belongs to the list in the “Notes section” at the end of the guide)
 
Repeat the commands you’ve just typed for any other module you want to add (you can add as many modules as you need)
 
When you have finished to extract any other additional module you have to type the following command in order to compile your kernel image, headers and modules.
 
cd /usr/src/linux
 
sudo make-kpkg clean
 
sudo make-kpkg –initrd –append-to-version=-custom kernel_image kernel_headers modules_image
 
NOTE: you can put whatever you want instead of “custom”
 
8 ) If you have followed point 7, skip this step and proceed to step 9
cd /usr/src/linux
 
sudo make-kpkg clean
 
sudo make-kpkg –initrd –append-to-version=-custom kernel_image kernel_headers
 
NOTE: you can put whatever you want instead of “custom”
 
e.g. sudo make-kpkg –initrd –append-to-version=-alberto kernel_image kernel_headers
 
The system will ask you this question:
 
“By default, I assume you know what you are doing, and I
apologize for being so annoying. Should I abort[Ny]?”
 
Answer no by either typing N and then pressing Enter, or just by pressing Enter (as N is the recommended answer)
 
Well now you’ll have to wait at least 45min. The process will use 100% of your CPU so try to leave your computer alone, go have a tea or something else just to keep you away from your computer for a while.
 
9) After the (long) process type this in the command line (Terminal or Konsole)
 
cd /usr/src
 
ls
 
You’ll see a list of the names of the files in the folder as well as the names of your new kernel image , kernel headers (and modules if you have followed Point 7); they should look (approximately)like these ones:
 
kernel-image-2.6.12-custom_10.00.Custom_i386.deb
kernel-headers-2.6.12-custom_10.00.Custom_i386.deb
 
(you will also see the modules like the ones below if you have followed Point 7)
ndiswrapper-modules-2.6.12-custom_1.1-4ubuntu2+10.00.Custom_i386.deb
nvidia-kernel-2.6.12-custom_1.0.7667-0ubuntu3+10.00.Custom_i386.deb
 
Now install them by typing these commands (change the name of the files according to the ones you have seen after the output of the command “ls”):
 
sudo dpkg -i kernel-image-2.6.12-custom_10.00.Custom_i386.deb
 
sudo dpkg -i kernel-headers-2.6.12-custom_10.00.Custom_i386.deb
 
sudo dpkg -i name_of_the_module (only if you have followed point 7) (there might be several modules so you have to use this command for every module you have compiled)
e.g.
sudo dpkg -i ndiswrapper-modules-2.6.12-custom_1.1-4ubuntu2+10.00.Custom_i386.deb
sudo dpkg -i nvidia-kernel-2.6.12-custom_1.0.7667-0ubuntu3+10.00.Custom_i386.deb
 
REMEMBER NOT TO UNINSTALL your previous kernel (just in case anything goes wrong) (i.e. don’t do anything else apart from following the instructions)
 
10) Ok, now it’s time to see if they work.
 
Restart your computer.
 
Then if you want to see if DMA is active (if you have enabled it in the kernel as described before) type (in Terminal or Konsole):
 
sudo hdparm -d /dev/hda (to check if your harddisk has DMA enabled)
sudo hdparm -d /dev/cdrom (to check if your cd-reader has DMA enabled)
 
Don’t you worry if it gives you an error as output but the performance of your harddisk and cdreader have improved (try to transfer a file from a CD to your harddisk and see if it the system slows down).
 
If everything is ok, then Congratulations you have compiled your first kernel successfully!
 
11 ) If anything went wrong (I don’t know a reason for which it would) you could switch back to your previous kernel: while your computer is booting press "ESC" repeatedly while your computer is booting until GRUB menu appears and you can choose kernel 2.6.12 again (using your keyboard arrows). Once you enter Ubuntu type these commands in order to uninstall the new kernel (remember to put the name of the kernel you have created)
 
follow this example and put the name of the kernel you have created instead of “custom”
 
sudo dpkg -r kernel-image-2.6.12-custom
 
sudo dpkg -r kernel-headers-2.6.12-custom
 
(in my case, sudo dpkg -r kernel-image-2.6.12-alberto)
 
NOTE you DON’T have to type the full name of the file .deb which you created
 
e.g. put “kernel-image-2.6.12-custom” instead of “kernel-image-2.6.12-custom_10.00.Custom_i386.deb”
 
And restart your computer.
 
Enjoy!
 
Alberto
 
————————————————————————–
 
NOTE: HOWTO compile from a vanilla kernel from kernel.org
 
If you want to compile from a vanilla kernel from kernel.org something need to be changed in my guide:
 
Skip Point 2
You have to download it from www.kernel.org (try the latest stable kernel source)
 
When you get to Point 3 of the guide and you get to the following lines you have to modify them in this way:
 
cd /home/your_username_folder/directory_where_you_put_the_downloaded_kernel (instead of cd /usr/src) (e.g. "cd /home/alberto/download" in my case)
sudo tar –bzip2 -xvf linux-source-2.6.12.tar.bz2 /usr/src (use the name of the file you downloaded)
sudo ln -s /usr/src/linux-source-2.6.12 /usr/src/linux (use the name of the file you downloaded)
cd /usr/src/linux
 
OPTIONAL PATCH (if you don’t need it you can continue with the instructions of Point 3)
 
At this point if you want to patch your vanilla kernel you have to:
 
1)Download the patch (I recommend you Kolivas’ patches: go to http://ck.kolivas.org/patches/2.6/ and choose the folder with the name which matches your kernel version (e.g. if you are going to compile "kernel 2.6.14" get to http://ck.kolivas.org/patches/2.6/2.6.14/). You will see other folders with different versions of the patch: I suggest you to select the latest version (2.6.14-ck3 in this case). Then you have to choose the file "patch-2.6.14-ck3.bz2" if you have a desktop computer (or laptop) or "patch-2.6.14-cks3.bz2" if you have a server.
 
NOTE about KOLIVAS patch: if you want to use Kolivas patches you have to download the 1st stable version of a series of kernels and apply the latest patch to it. I’d better explain it with an example: if you are interested in 2.6.14.x series you have to download the source of 2.6.14 (NOT of 2.6.14.4 or 2.6.14.3, etc.) and to apply the latest Kolivas patch (as it will include the patches which make the difference between e.g. version 2.6.14 and 2.6.14.4)
 
2)cd /usr/src/linux
 
3)sudo bzcat /home/your_user_name/folder_in_which_you_downloaded_the_file/patch-2.6.14-ck3.bz2 | patch -p1
(put the name of the file you have downloaded instead of the word I have put in red)
 
And in Point 4 (this is the 1st thing to do at the beginning of point 4):
 
Get to "File Systems".
 
Select your filesystem (ext3, reiserfs, etc.) with the cursor.( ext3 is the filesystem used by Ubuntu by default, so if you chose automatic partitioning when you installed Ubuntu Hoary then "ext3" is definitely your filesystem)
 
Then press the spacebar on the desired filesystem (a "*" will appear beside it). (Make sure there’s a "*" beside it instead of a "M". In this way the support for you filesystem will be built directly in the kernel instead of being built as a module and you will not get a "kernel panic")
 
For example:
select "ext3 journalling filesystem support" and press the spacebar (a "*" will appear beside it).
 
Press the right arrow and select exit.
 
Then you can go on with the instructions of Point 4.
 
If you want to compile a kernel 2.6.13 or higher you have to skip Point 6.
 
The rest of the HOWTO is ok.
 
————————————————————————–
 
NVIDIA LEGACY CARDS
 
Below are the legacy GPUs that are no longer supported in the unified driver.
These GPUs will continue to be maintained through the special legacy NVIDIA
GPU driver releases.
 
NVIDIA chip name Device PCI ID
——————————- ——————————-
RIVA TNT 0x0020
RIVA TNT2/TNT2 Pro 0x0028
RIVA TNT2 Ultra 0x0029
Vanta/Vanta LT 0x002C
RIVA TNT2 Model 64/Model 64 Pro 0x002D
Aladdin TNT2 0x00A0
GeForce 256 0x0100
GeForce DDR 0x0101
Quadro 0x0103
GeForce2 GTS/GeForce2 Pro 0x0150
GeForce2 Ti 0x0151
GeForce2 Ultra 0x0152
Quadro2 Pro 0x0153

Leave a Reply

Your email address will not be published.