Monday, August 3, 2015

Linux Booting Procedure


The stages involved in Linux Booting Process are:
BIOS
Boot Loader

    - MBR
    - GRUB
Kernel
Init
Runlevel scripts

BIOS
·         This is the first thing which loads once you power on your machine.
·         When you press the power button of the machine, CPU looks out into ROM for further instruction.
·         The ROM contains JUMP function in the form of instruction which tells the CPU to bring up the BIOS
·         BIOS determines all the list of bootable devices available in the system.
·         Prompts to select bootable device which can be Hard Disk, CD/DVD-ROM, Floppy Drive, USB Flash Memory Stick etc (optional)
·         Operating System tries to boot from Hard Disk where the MBR contains primary boot loader.

Boot Loader 
To be very brief this phase includes loading of the boot loader (MBR and GRUB/LILO) into memory to bring up the kernel.

MBR (Master Boot Record)
·         It is the first sector of the Hard Disk with a size of 512 bytes.
·         The first 434 - 446 bytes are the primary boot loader, 64 bytes for partition table and 6 bytes for MBR validation timestamp.
NOTE: Now MBR directly cannot load the kernel as it is unaware of the filesystem concept and requires a boot loader with file system driver for each supported file systems, so that they can be understood and accessed by the boot loader itself.

To overcome this situation GRUB is used with the details of the filesystem in 
/boot/grub.conf and file system drivers

GRUB (GRand Unified Boot loader)

This loads the kernel in 3 stages

GRUB stage 1: 
·         The primary boot loader takes up less than 512 bytes of disk space in the MBR - too small a space to contain the instructions necessary to load a complex operating system. 
·         Instead the primary boot loader performs the function of loading either the stage 1.5 or stage 2 boot loader.
GRUB Stage 1.5: 
·         Stage 1 can load the stage 2 directly, but it is normally set up to load the stage 1.5. 
·         This can happen when the /boot partition is situated beyond the 1024 cylinder head of the hard drive. 
·         GRUB Stage 1.5 is located in the first 30 KB of Hard Disk immediately after MBR and before the first partition.
·         This space is utilized to store file system drivers and modules.
·         This enabled stage 1.5 to load stage 2 to load from any known location on the file system i.e./boot/grub

GRUB Stage 2:
·         This is responsible for loading kernel from /boot/grub/grub.conf and any other modules needed
·         Loads a GUI interface i.e. splash image located at /grub/splash.xpm.gz with list of available kernels where you can manually select the kernel or else after the default timeout value the selected kernel will boot
The original file is /etc/grub.conf of which you can observe a symlink file at/boot/grub/grub.conf

Sample /boot/grub/grub.conf
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.18-194.26.1.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-194.26.1.el5 ro root=/dev/VolGroup00/root clocksource=acpi_pm divisor=10
        initrd /initrd-2.6.18-194.26.1.el5.img
title Red Hat Enterprise Linux Server (2.6.18-194.11.4.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-194.11.4.el5 ro root=/dev/VolGroup00/root clocksource=acpi_pm divisor=10
        initrd /initrd-2.6.18-194.11.4.el5.img
title Red Hat Enterprise Linux Server (2.6.18-194.11.3.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-194.11.3.el5 ro root=/dev/VolGroup00/root clocksource=acpi_pm divisor=10
        initrd /initrd-2.6.18-194.11.3.el5.img



Kernel
This can be considered the heart of operating system responsible for handling all system processes.

Kernel is loaded in the following stages:
1.    Kernel as soon as it is loaded configures hardware and memory allocated to the system.
2.    Next it uncompresses the initrd image (compressed using zlib into  zImage or bzImage formats) and mounts it and loads all the necessary drivers.
3.    Loading and unloading of kernel modules is done with the help of programs like insmod, and rmmod present in the initrd image.
4.    Looks out for hard disk types be it a LVM or RAID.
5.    Unmounts initrd image and frees up all the memory occupied by the disk image.
6.    Then kernel mounts the root partition as specified in grub.conf as read-only.
7.    Next it runs the init process

Init Process
·         Executes the system to boot into the run level as specified in /etc/inittab
Sample output defining the default boot runlevel inside /etc/inittab
# Default runlevel. The runlevels used by RHS are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
#
id:5:initdefault:
As per above O/P system will boot into runlevel 5

You can check current runlevel details of your system using below command on the terminal
# who -r
         run-level 3  Jan 28 23:29                   last=S
·         Next as per the fstab entry file system's integrity is checked and root partition is re-mounted as read-write (earlier it was mounted as read-only).

Runlevel scripts
8.    A no. of runlevel scripts are defined inside /etc/rc.d/rcx.d
Runlevel  Directory
0 /etc/rc.d/rc0.d
1 /etc/rc.d/rc1.d
2 /etc/rc.d/rc2.d
3 /etc/rc.d/rc3.d
4 /etc/rc.d/rc4.d
5 /etc/rc.d/rc5.d
6 /etc/rc.d/rc6.d
·         Based on the selected runlevel, the init process then executes startup scripts located in subdirectories of the /etc/rc.d directory.
·         Scripts used for runlevels 0 to 6 are located in subdirectories /etc/rc.d/rc0.d through/etc/rc.d/rc6.d, respectively.


·         Lastly, init runs whatever it finds in /etc/rc.d/rc.local (regardless of run level). rc.localis rather special in that it is executed every time that you change run levels.
NOTE: rc.local is not used in all the distros as for example Debian.

Next if everything goes fine you should be able to see the Login Screen on your system.


Tuesday, July 7, 2015

RHEL Logical Volume Manager - Increasing/Decreasing Size

LVM, logical volumes size can be increased or decreased depends on our needs. Logical volume can be shirked/reduced using “lvreduce” command but this will not reduce the filesystem. You need to reduce the filesystem before reducing the logical volume. Logical volume can be extended/increased using “lvextend” command.Once you have extended the logical volume ,then you need to increase the filesystem to the see new space on the mount points.
Most of the Linux/Unix beginners will confuse with term ‘volume’ and ‘filesystem’. Volume is a raw partition like a physical disk.In a order to store data on volume,you need a filesystems on that.(Ex:ext2,ext3,ext4…etc..)


1.Extending the Logical Volume size

Logical volumes can be increased on fly without un-mounting the filesystem. Here we will see how to increase the linear filesystem and volume on Redhat Linux 6.
1.Check the volume group free space before proceeding to the volume expansion.
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize  VFree
  uavg        3   1   0 wz--n-  5.99g 5.89g
  vg_mylinz   1   2   0 wz--n- 19.51g    0
[root@mylinz ~]#
#
From the above output ,we can see volume group “uavg” has 5.89GB free space.


2.We are planning to extend volume “lvol0″ from uavg volumegroup. 
[root@mylinz ~]# lvs
  LV      VG        Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  lvol0   uavg      -wi-ao 100.00m
  lv_root vg_mylinz -wi-ao  16.54g
  lv_swap vg_mylinz -wi-ao   2.97g
[root@mylinz ~]# df -h /vol1/
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg-lvol0
                       97M  5.6M   87M   7% /vol1
[root@mylinz ~]# lvdisplay /dev/uavg/lvol0
  --- Logical volume ---
  LV Name                /dev/uavg/lvol0
  VG Name                uavg
  LV UUID                0rtjKO-3GdM-CeOf-CY8m-fZgH-wjgu-kJivtd
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                100.00 MiB
  Current LE             25
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

[root@mylinz ~]#

3.Extend the logical volume by 100MB.
[root@mylinz ~]# lvextend -L +100M /dev/uavg/lvol0
  Extending logical volume lvol0 to 200.00 MiB
  Logical volume lvol0 successfully resized
[root@mylinz ~]# lvdisplay /dev/uavg/lvol0
  --- Logical volume ---
  LV Name                /dev/uavg/lvol0
  VG Name                uavg
  LV UUID                0rtjKO-3GdM-CeOf-CY8m-fZgH-wjgu-kJivtd
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                200.00 MiB
  Current LE             50
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

[root@mylinz ~]#

4.Check the mountpoint “/vol1″ .The new space is not be reflected there because,Since you didn’t resize the filesystem.
[root@mylinz ~]# df -h /vol1/
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg-lvol0
                       97M  5.6M   87M   7% /vol1
[root@mylinz ~]#

5.Increase the filesystem.
[root@mylinz ~]# resize2fs /dev/uavg/lvol0
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/uavg/lvol0 is mounted on /vol1; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/uavg/lvol0 to 204800 (1k) blocks.
The filesystem on /dev/uavg/lvol0 is now 204800 blocks long.

[root@mylinz ~]#

6.Check the mountpoint now.
[root@mylinz ~]# df -h /vol1
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg-lvol0
                      194M  5.6M  179M   3% /vol1
[root@mylinz ~]#
Wow..We have successfully increased the vol1 from 100MB to 200MB.
The same way you can increase any type of volume layouts.

2.Reducing the Logical Volume size

Volumes can’t be  reduced on the fly.To reduce the volume ,
1. umount the filesystem
2. Reduce the filesystem size 
3. Reduce the logical volume
4. Mount the volume .


Here we will see how to reduce the volume “lvol0″ which is part of volume group “uavg” .

1.List he volume information.
[root@mylinz ~]# lvs
  LV      VG        Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  lvol0   uavg      -wi-ao 200.00m
  lv_root vg_mylinz -wi-ao  16.54g
  lv_swap vg_mylinz -wi-ao   2.97g
[root@mylinz ~]# lvdisplay /dev/uavg/lvol0
  --- Logical volume ---
  LV Name                /dev/uavg/lvol0
  VG Name                uavg
  LV UUID                0rtjKO-3GdM-CeOf-CY8m-fZgH-wjgu-kJivtd
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                200.00 MiB
  Current LE             50
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

[root@mylinz ~]# df -h /vol1
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg-lvol0
                      194M  5.6M  179M   3% /vol1
[root@mylinz ~]#

2.un-mount the filesystem.
[root@mylinz ~]# umount /vol1
[root@mylinz ~]#

3.Reduce the filesystem by 100MB. The current filesystem size is 200MB.
Option 100MB – Final size of the volume.
[root@mylinz ~]# resize2fs /dev/uavg/lvol0 100M
resize2fs 1.41.12 (17-May-2010)
Please run 'e2fsck -f /dev/uavg/lvol0' first.
[root@mylinz ~]#
“resize2fs” required to check the filesystem consistency before reduce the FS.
[root@mylinz ~]# e2fsck -f /dev/uavg/lvol0
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/uavg/lvol0: 11/49400 files (9.1% non-contiguous), 11884/204800 blocks
[root@mylinz ~]# resize2fs /dev/uavg/lvol0 100M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/uavg/lvol0 to 102400 (1k) blocks.
The filesystem on /dev/uavg/lvol0 is now 102400 blocks long.
[root@mylinz ~]#
We have successfully reduced  the filesystem to 100MB.

4.De-activate the volume.
[root@mylinz ~]# lvchange -an /dev/uavg/lvol0

5.Reduce the volume. “-L” – Final size of the volume.
Here i am giving volume final size as 104MB. So that  volume may have some room and  won’t touch the filesystem part.The extra 4MB can be recovered using resize2fs. Its up to you how you want to proceed.
Note:You can also give 100MB. For safer side, its better to give some extra space on the volume level.
Having good backup is recommeded before proceeding to volume resize.
[root@mylinz ~]# lvreduce -L 104M /dev/uavg/lvol0
Do you really want to reduce lvol0? [y/n]: y
  Reducing logical volume lvol0 to 104.00 MiB
  Logical volume lvol0 successfully resized
[root@mylinz ~]#

6.Activate the volume and Mount it.
[root@mylinz ~]# lvchange -ay /dev/uavg/lvol0
[root@mylinz ~]# mount -t ext4 /dev/uavg/lvol0 /vol1
[root@mylinz ~]# df -h /vol1
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg-lvol0
                       97M  5.6M   87M   6% /vol1
[root@mylinz ~]#


Hope now you are familiar with volume re-sizing operation on Logical volume manager.

Monday, July 6, 2015

RHEL Package Management

Package management is one of the important task in Linux Administration.Red Hat Package manager aka RPM provides robust package administration on Redhat Linux.It eliminates the manually building software and made the software installation & maintenance  much easier than before. RPM has lot of front-end tools to resolve the dependencies to install any packages. In this article ,we will see about RPM package manager’s interview questions and Front end tool “yum” related interview questions. RPM&YUM are commonly used in Redhat Linux Environments. 


RPM Package Management 

1. How to check the installed software(rpm’s) on Redhat Linux ?
Answer: rpm -qa package_name (or) rpm -qa |grep package_name*
Ex:
[root@mylinz ~]# rpm -qa yum
yum-3.2.27-14.el6.noarch
[root@mylinz ~]# rpm -qa |grep yum
yum-rhn-plugin-0.9.1-5.el6.noarch
yum-utils-1.1.26-11.el6.noarch
yum-metadata-parser-1.1.2-14.1.el6.x86_64
yum-3.2.27-14.el6.noarch
PackageKit-yum-0.5.8-13.el6.x86_64
PackageKit-yum-plugin-0.5.8-13.el6.x86_64
[root@mylinz ~]#
2. How to install  specific software package using rpm command on Redhat Linux ?
Answer: rpm –ivh package_name 

3. How to remove the software package from on Redhat  Linux ?
Answer: rpm -ev package_name

4.How to upgrade the existing Redhat package using rpm command ?
Answer: rpm -Uvh package_name

5.How to display the list of installed software package’s files ?
Answer: rpm -ql package_name 
Ex:[root@mylinz ~]# rpm -ql yum-rhn-plugin-0.9.1-5.el6.noarch
/etc/yum/pluginconf.d/rhnplugin.conf
/usr/share/doc/yum-rhn-plugin-0.9.1
/usr/share/doc/yum-rhn-plugin-0.9.1/LICENSE
/usr/share/locale/ar/LC_MESSAGES/yum-rhn-plugin.mo
/usr/share/locale/as/LC_MESSAGES/yum-rhn-plugin.mo
/usr/share/locale/bg/LC_MESSAGES/yum-rhn-plugin.mo
/usr/share/locale/bn/LC_MESSAGES/yum-rhn-plugin.mo
6. How to get the installed software information like package version and short description of package ?
Answer: rpm -qi package_name
Ex:
[root@mylinz ~]# rpm -qi yum-rhn-plugin-0.9.1-5.el6.noarch
Name        : yum-rhn-plugin               Relocations: (not relocatable)
Version     : 0.9.1                             Vendor: Red Hat, Inc.
Release     : 5.el6                         Build Date: Fri 14 May 2010 07:18:35 PM IST
Install Date: Fri 01 Jun 2012 02:41:19 AM IST      Build Host: x86-003.build.bos.redhat.com
Group       : System Environment/Base       Source RPM: yum-rhn-plugin-0.9.1-5.el6.src.rpm
Size        : 178193                           License: GPLv2
Signature   : RSA/8, Tue 17 Aug 2010 02:05:09 AM IST, Key ID 199e2f91fd431d51
Packager    : Red Hat, Inc. 
URL         : https://fedorahosted.org/spacewalk
Summary     : RHN support for yum
Description :
This yum plugin provides support for yum to access a Red Hat Network server for
software updates.
[root@mylinz ~]#
7.How to find files belongs to which rpm package ?
Answer:rpm -qf file_location  
Ex:
[root@mylinz ~]# rpm -qf /etc/yum.conf
yum-3.2.27-14.el6.noarch
[root@mylinz ~]#
8.How to display list of configuration files for specific package ?
Answer:rpm -qc package_name
Ex:
[root@mylinz ~]# rpm -qc yum
/etc/logrotate.d/yum
/etc/yum.conf
/etc/yum/version-groups.conf
[root@mylinz ~]#
9.How to display a list of recently installed software ?
Answer:rpm -qa –last
Ex:
[root@mylinz ~]# rpm -qa --last |head
lvm2-cluster-2.02.72-8.el6                    Tue 06 Aug 2013 11:56:50 AM IST
tigervnc-server-1.0.90-0.10.20100115svn3945.el6 Wed 01 May 2013 08:39:24 PM IST
oracle-xe-11.2.0-1.0                          Tue 26 Jun 2012 01:49:05 PM IST
firefox-3.6.9-2.el6                           Tue 19 Jun 2012 02:31:03 PM IST
redhat-bookmarks-6-1.el6                      Tue 19 Jun 2012 02:30:59 PM IST
10.How to get the list of dependencies for specific rpm file ?
Answer:rpm -qpR rpm_file
Ex:
[root@mylinz ~]# rpm -qpR nautilus-sendto-2.28.2-3.el6.x86_64.rpm
/bin/sh
/bin/sh
/bin/sh
GConf2
GConf2
GConf2
libORBit-2.so.0()(64bit)
libatk-1.0.so.0()(64bit)
libbonobo-2.so.0()(64bit)
libbonobo-activation.so.4()(64bit)
11.How to get the list of dependencies for specific package ?
Answer:rpm -qR package_name
Ex:
[root@mylinz ~]# rpm -qR yum
/usr/bin/python
config(yum) = 3.2.27-14.el6
pygpgme
python >= 2.4
python(abi) = 2.6
python-iniparse
python-sqlite
python-urlgrabber >= 3.9.0-8
rpm >= 0:4.4.2
rpm-python
rpmlib(CompressedFileNames) <= 3.0.4-1
rpmlib(FileDigests) <= 4.6.0-1
rpmlib(PartialHardlinkSets) <= 4.0.4-1
rpmlib(PayloadFilesHavePrefix) <= 4.0-1
yum-metadata-parser >= 1.1.0
rpmlib(PayloadIsXz) <= 5.2-1
[root@mylinz ~]#
12.How to remove the package without checking the dependencies on Redhat Linux ?
Answer:rpm -ev –nodeps package_name

Yum – The RPM front-End tool – Interview Questions

1.What is yum  ?
Answer: yum is yellow update manager which is a front end tool for package management.
All the rpm commands activity can be carried out using yum command in automated way.
Yum mechanism will automatically resolve the dependencies unlike rpm commands.
2.How to install packages using yum ?
Answer:yum install package_name

3.How to configure yum repository in Redhat Linux ?
Answer:Here is the step by step guide

4.How to update the package using yum ?
Answer:yum update package_ name

5.How to search the package in yum ?
Answer:yum search package_name

6.How to remove the package using yum ?
Answer:yum remove package_name

7.How to check the updates for yum repository ?
Answer:yum check-update

8.How to update the yum repo ?
Answer:yum update

9.How to get the package information using yum ?
Answer:yum info package_name

10.How to list the installed packages on Redhat linux using yum  command ?
Answer:yum list installed

11.How to know the particular files belongs to which package ?
Answer:yum provides file_path
Ex:
[root@mylinz ~]# yum provides /etc/yum.conf
Loaded plugins: refresh-packagekit, rhnplugin
This system is not registered with RHN.
RHN support will be disabled.
yum-3.2.27-14.el6.noarch : RPM installer/updater
Repo        : local-installation
Matched from:
Filename    : /etc/yum.conf

yum-3.2.27-14.el6.noarch : RPM installer/updater
Repo        : installed
Matched from:
Other       : Provides-match: /etc/yum.conf
12.How to list the enabled repositories ?
Answer:yum repolist