Posted by: kezhong | July 30, 2009

Modifying Linux Kernel Parameters at Runtime

Linux provides a very flexible approach to system administrators that they can modify runtime kernel parameters without compiling and installing a new kernel or module. It is implemented by the virtual file system /proc. Most of Linux kernel parameters can be found in the directory /proc/sys. You can use either the echo command with redirection or the sysctl command to modify these kernel parameters.

For example, I can use two methods as below, if I want to enable the IP forwarding temporarily.
# echo 1 > /proc/sys/net/ipv4/ip_forward
or
# sysctl -w net.ipv4.ip_forward=1

After modifying, it takes effect at once. But when you reboot the system or restart the network service, it will recover the default value 0 again.

If you want to make it effective permanently, you have to either save the above echo command line to the file /etc/rc.d/rc.local or modify the file /etc/sysctl.conf as below.

Modify the file /etc/sysctl.conf, change the value 0 to 1.
  net.ipv4.ip_forward = 1
Then save the file. When the system boot up, the initial script /etc/rc.d/rc.sysinit will read the content of the file /etc/sysctl.conf. But the sysctl.conf file will not take effect immediately when you use this method. If you want, you can issue the command with –p parameter.
# sysctl –p

How to correspond the relationship between the kernel file in the directory /proc/sys and the variable of the file sysctl.conf? Because all possible kernel parameters are in the directory /proc/sys, you can set the variables of the file sysctl.conf omitting the previous part (/proc/sys), and then change the all “/” to “.”.

For example,
  /proc/sys/net/ipv4/ip_forward     –>       net.ipv4.ip_forward
  /proc/sys/kernel/hostname         –>       kernel.hostname
  /proc/sys/kernel/domainname      –>       kernel.domainname
  /proc/sys/fs/file-max                 –>       fs.file-max

 

Reference
Administer Linux on the fly

Posted by: kezhong | July 25, 2009

Patching the Linux Kernel

As my last blog, I had built a new kernel which its release was 2.6.30. I would like to upgrade it to the current stable release 2.6.30.2 today. I have two approaches, one is building a new kernel like the last blog, and another is patching the kernel. In this test, I used the second one.

Upgrade from release 2.6.30 to 2.6.30.2
1.Backup the existing source tree
# cd /usr/src
# tar cvfj linux-2.6.30.tar.bz2 linux-2.6.30
2.Get and apply the patch
# wget http://www.kernel.org/pub/linux/kernel/v2.6/patch-2.6.30.2.bz2
# cd linux-2.6.30
# bzcat ../patch-2.6.30.2.bz2 | patch –p1
patching file Documentation/sysctl/vm.txt
patching file Makefile
patching file arch/alpha/include/asm/percpu.h
… …
# cd ..
# mv linux-2.6.30 linux-2.6.30.2
3.Recompile and config
# cd linux-2.6.30.2
# make oldconfig
# make clean
# make bzImage
# cp arch/x86/boot/bzImage /boot/vmlinuz-2.6.30.2
# chmod a+x /boot/vmlinuz-2.6.30.2
# cp System.map /boot/System.map-2.6.30.2
# make modules
# make modules_install 
# mkinitrd /boot/initrd-2.6.30.2.img 2.6.30.2
# vi /boot/grub/menu.list
title Fedora (2.6.30.2)
            root (hd0,0)
            kernel /vmlinuz-2.6.30.2 ro root=/dev/mapper/VolGroup-lv_root rhgb quiet
            initrd /initrd-2.6.30.2.img
 
4.Reboot and verify
# reboot
# uname –r
2.6.30.2

Upgrade from release 2.6.30.2 to 2.6.30.3
I found the latest stable release 2.6.30.3 come out, when I wanted to post this article. So I decided to upgrade my system to the newest release.
1.Apply the patch in reverse
# cd /usr/src
# wget http://www.kernel.org/pub/linux/kernel/v2.6/patch-2.6.30.2.bz2
# cd linux-2.6.30.2
# bzcat ../patch-2.6.30.2.bz2 | patch –p1 -R
2.Get and apply the patch
# cd ..
# wget http://www.kernel.org/pub/linux/kernel/v2.6/patch-2.6.30.3.bz2
# mv linux-2.6.30.2 linux-2.6.30.3
# cd linux-2.6.30.3
# bzcat ../patch-2.6.30.3.bz2 | patch –p1
3.Recompile and config
# make oldconfig
# make clean
# make bzImage
# cp arch/x86/boot/bzImage /boot/vmlinuz-2.6.30.3
# chmod a+x /boot/vmlinuz-2.6.30.3
# cp System.map /boot/System.map-2.6.30.3
# make modules
# make modules_install 
# mkinitrd /boot/initrd-2.6.30.3.img 2.6.30.3
# vi /boot/grub/menu.list
title Fedora (2.6.30.3)
            root (hd0,0)
            kernel /vmlinuz-2.6.30.3 ro root=/dev/mapper/VolGroup-lv_root rhgb quiet
            initrd /initrd-2.6.30.3.img
 
4.Reboot and verify
# reboot
# uname –r
2.6.30.3 

According to Wikipedia, the patch file is a text file that consists of a list of differences and is produced by running the related diff program with the original and updated file as arguments. With my experience, when you want to build a new kernel, you can choose the method either building a new kernel with original source code or patching the kernel. They have their own advantages.

If you choose the patching method, you should consider the relationship between base stable release and others. For example, assume that your current stable release is 2.6.26.2, if you want to upgrade it to 2.6.26.8 through patching, the first you should do is downgrading (reversing) it to its base stable release 2.6.26, and then do the others. Another example, assume that your current stable release is 2.6.26.8, if you want to upgrade it to 2.6.27.1 through patching, you should downgrade it to its base stable release 2.6.26, patch the kernel to 2.6.27, and then patch it to 2.6.27.1.

Posted by: kezhong | July 19, 2009

Building a New Kernel

Linux is probably the only operating system that has a new kernel every so often. The most common reason to upgrade a kernel is to take advantage of some newer device drivers to handle some specific devices or you need to upgrade due to a bug in the kernel.

The release of my Fedora system was 2.6.29.4. I checked it through the following command.
# uname –r
2.6.29.4-167.fc11.i686.PAE 

Then I upgraded it to 2.6.30 as below steps. 

1.Download source from kernel.org
# cd /usr/src
# wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.30.tar.bz2 

2.Unpack the source file
#tar xvjf linux-2.6.30.tar.bz2 

3.Create configuration file
Firstly, I installed the “kernel-devel”, “kernel-headers”, and “gcc” packages.
# yum install kernel-devel kernel-headers gcc -y
# cd linux-2.6.30
# cp /usr/src/kernels/2.6.29.5-191.fc11.i586/.config .
# make oldconfig 

4.Compile kernel and copy the new kernel to the /boot directory
# make dep
# make clean
# make bzImage
# cp arch/x86/boot/bzImage /boot/vmlinuz-2.6.30
# chmod a+x /boot/vmlinuz-2.6.30
# cp System.map /boot/System.map-2.6.30 

5.Compile and install kernel modules
# make modules
# make modules_install 

6.Create a new initial ramdisk
# mkinitrd /boot/initrd-2.6.30.img 2.6.30
Note: The format of the mkinitrd command: mkinitrd <image file> <kernel version> 

7.Update GRUB
Edit the /boot/grub/menu.list file, add the following lines into it.
# vi /boot/grub/menu.list
title Fedora (2.6.30)
            root (hd0,0)
            kernel /vmlinuz-2.6.30 ro root=/dev/mapper/VolGroup-lv_root rhgb quiet
            initrd /initrd-2.6.30.img 

8.Reboot and verify

Reference
SYA710 Building a New Kernel

When I did the RAID 10 test, I didn’t add the “UUID” information into /etc/mdadm.conf file initially. After I reboot the system, it was unable to boot.

I inputted the password and entered the maintenance mode. I tried to delete the line I had added from the /etc/fstab file, but the file is in real-only mode and I could not to modify it.  After searched the solution in the internet, I did the following four steps. The problem had been solved.

1. Check the filesystems.
(Repair filesystem) 1 # fsck –a
… …
fsck.ext4: No such file or directory while trying to open /dev/datavg/datalv
… … 

2. Remount the root file system and make it writable.
(Repair filesystem) 2 # mount –o remount,rw /
EXT4 FS on dm-0, internal journal on dm-0:8 

3. Edit the /etc/fstab file, delete the line I had added, save and quit.
(Repair filesystem) 3 # vi /etc/fstab 

4. Reboot the system.
(Repair filesystem) 4 # reboot

Posted by: kezhong | July 11, 2009

Manage a RAID10 Array

RAID10 means the combination of RAID 0 and RAID 1. It absorbs the advantages of RAID0 and RAID1 that provides mirroring and better performance. It also gives better performance than RAID 5 while a failed drive remains unreplaced. So it is popularly used in real working environment.

In my virtual machine, I had installed Fedora 11 as my last article. Before switched on, I created six virtual disks with the same size (5G). Then I began to do the test for creating RAID10.

Create a RAID10 array
After switched on my virtual machine, I checked the devices.
[root@localhost ~]# ls /dev/sd*
/dev/sda    /dev/sda2  /dev/sdb    /dev/sdb2   /dev/sdc  /dev/sde  /dev/sdg        
/dev/sda1  /dev/sda3  /dev/sdb1  /dev/sdb3   /dev/sdd  /dev/sdf  /dev/sdh 

From the above list, I found the six drives: sdc, sdd, sde, sdf, sdg, and sdh. Then I partitioned the drives respectively. List them again.
[root@localhost ~]# ls /dev/sd*
/dev/sda    /dev/sda3  /dev/sdb2   /dev/sdc1   /dev/sde    /dev/sdf1  /dev/sdh    
/dev/sda1  /dev/sdb    /dev/sdb3  /dev/sdd     /dev/sde1  /dev/sdg   /dev/sdh1
/dev/sda2  /dev/sdb1  /dev/sdc    /dev/sdd1   /dev/sdf    /dev/sdg1  

Create a RAID10 array using these six disks.
[root@localhost ~]# mdadm –C /dev/md3 –l10 –n6 /dev/sd[c,d,e,f,g,h]1
mdadm: array /dev/md3 started.

Check out if the synchronization had finished.
[root@localhost ~]# cat /proc/mdstat
Personalities: [raid1] [raid10]
md3 :  active raid10 sdh1[5] sdg1[4] sdf1[3] sde1[2] sdd1[1] sdc1[0]
            15711168 blocks 64K chunks 2 near-copies [6/6] [UUUUUU]
            [>…..] resync = 3.9% (619392/15711168) finish=2.4min speed=103232K/sec
md0 :   active raid1 sdb1[0] sda1[1]
            204736 blocks [2/2] [UU]
md1 :   active raid1 sdb2[0] sda2[1]
            1048512 blocks [2/2] [UU]
md2 :   active raid1 sdb3[0] sda3[1]
            7132416 blocks [2/2] [UU]
unused devices: <none> 

After the synchronization had finished, I tried to start the array manually but did not pass.
[root@localhost ~]# mdadm –A /dev/md3
mdadm: /dev/md3 not identified in config file. 

Then I found the UUID of the array with the command as below.
[root@localhost ~]# mdadm –detail /dev/md3
… …
UUID : 791bfd4c:df96f9e3:bfe78010:bc810f04
… … 

Add the following line to /etc/mdadm.conf in order that the system can find the array after reboot. If you did not do, you would not find md3 using “cat /proc/mdstat” command and occur unbootable problem when you finished making filesystem and configured it into /etc/fstab file after you reboot the system. If the unbootable problem occur, you can find the solution from my next article “Solving an unbootable problem caused by modifying fstab“. 
[root@localhost ~]# vi /etc/mdadm.conf
ARRAY /dev/md3 level=raid10 num-devices=6 UUID=791bfd4c:df96f9e3:bfe78010:bc810f04

Create LVM on RAID10 array
Create a physical volume on the array.
[root@localhost ~]# pvcreate /dev/md3
  Physical volume “/dev/md3” successfully created

Create a volume group on the pv.
[root@localhost ~]# vgcreate datavg /dev/md3
  Volume group “datavg” successfully createdCreate a logical volume within the vg.
[root@localhost ~]# lvcreate –n datafs1 –size 5G datavg
  Logical volume “datafs1” created

 

Make a filesystem within the lv.
[root@localhost ~]# mkfs.ext4 /dev/datavg/datafs1

Mount the filesystem.
[root@localhost ~]# mkdir /datafs1
[root@localhost ~]# mount /dev/datavg/datafs1  /datafs1

Check if it had been mounted.
[root@localhost ~]# df –Th
Filesystem       Type    Size      Used    Avail    Use%   Mounted on
/dev/mapper/rootvg-rootfs
                        ext4      6.7G    3.1G    3.3G    49%     /
/dev/md0          ext3      194M   14M     170M   8%       /boot
/dev/mapper/datavg-datafs1
                        ext4      5.0G    138M   4.6G    3%       /datafs1

Edit the /etc/fstab file, add the following line to make the filesystem mount automatically after reboot.
[root@localhost ~]# vi /etc/fstab
/dev/mapper/datavg-datafs1  /datafs1   ext4  defaults  1 3 

Replace a failure disk in RAID10 array
Switch off the virtual machine, remove a disk (/dev/sde) from RAID 10, add a new disk, and then turn on the virtual machine. I checked it out and found a disk missing.
[root@localhost ~]# cat /proc/mdstat
Personalities: [raid1] [raid10]
md3 :  active raid10 sdc1[0] sdh1[5] sdg1[4] sdf1[3] sdd1[1]
            15711168 blocks 64K chunks 2 near-copies [6/5] [UU_UUU]
md0 :   active raid1 sdb1[0] sda1[1]
            204736 blocks [2/2] [UU]
md1 :   active raid1 sdb2[0] sda2[1]
            1048512 blocks [2/2] [UU]
md2 :   active raid1 sdb3[0] sda3[1]
            7132416 blocks [2/2] [UU]
unused devices: <none> 

Partition the new disk as above.

Add the new disk to the existing RAID10 array.
[root@localhost ~]# mdadm /dev/md3 –add /dev/sde1

  

References
Manage a Linux RAID 10 Storage Server
Basic RAID Organizations

Older Posts »

Categories