Saturday, January 25, 2014

Kernel panic - not syncing: Attempted to kill init! while booting Cent OS/RHEL 6


I encountered this error upon reboot on my CENT OS/RHEL 6 server after I renamed the volume group on my system without syncing with /etc/fstab and /etc/grub.conf.
Same error will occur if there are issues related to these files including /etc/inittab. To resolve the problem, you will need to use a rescue disk. 1. Run rescue disk 2. The / is mounted to /mnt/sysimage # chroot /mnt/sysimage 3. Edit files appropriately: Replace the your old volume group name with new volume group name  in the below files          # vi /etc/fstab # vi /etc/grub.conf # vi /etc/inittab If grub is missing: # /sbin/grub-install /dev/cdrom 4. Exit the rescue disk (It will reboot automagically) # reboot During reboot, SElinux will sync the renamed vg and will take quite some time. After that you can already have a successful logon.

Difference between soft link and hard link

Before understanding the difference between soft link and hard link, we need to understand the concept of soft link and hard link as well as concept of Inode in unix file system. Actually, link concept originated to replicate the file at different location without copying it and it is based on file system. File System is closely related withinode which is unique in system.

How to create Soft Link in Linux?

We will create soft link as well as hard link in two examples and then we will check for difference.

root@hello:~/ctier/shellscript# pwd
/root/hello/shellscript
root@hello:~/ctier/shellscript# ls -li shellscript.sh
5644164 -rwxr-xr-x 1 root root 446 2013-04-17 17:48 shellscript.sh
root@hello:~/ctier/shellscript# ln -s /root/ctier/shellscript/shellscript.sh /root/ctier/test/shells.sh
root@hello:~/ctier/shellscript# ls -li /root/ctier/test/shells.sh
6554654 lrwxrwxrwx 1 root root 16 2013-04-24 22:10 /root/ctier/test/shells.sh -> /root/hello/shellscript/shellscript.sh
root@hello:~/ctier/shellscript#

As per above example, we have created soft link of shellscript.shto this file /root/ctier/test/shells.sh. i.e. new file is /root/ctier/test/shells.sh. If you check inode of new file, it is different. And in long listing, it is showing that it points to some other file. Size of file is different at both the locations. Because in soft link, there is no copy of file, it is just pointing to the original location. If we are trying to open a /root/ctier/test/shells.sh this file, then it will open original file i.e. /root/ctier/shellscript/shellscript.sh. If we made any changes in original file then it will reflect in soft linked file as well.

How to create Hard Link in Linux?

root@hello:~/hello/shellscript# ls -li shellscript.sh
5644164 -rwxr-xr-x 1 root root 446 2013-04-17 17:48 shellscript.sh
root@hello:~/hello/shellscript# mkdir test
root@hello:~/hello/shellscript# ln shellscript.sh ./test/hardlink.sh
root@hello:~/hello/shellscript# ls -li ./test/hardlink.sh
5644164 -rwxr-xr-x 2 root root 446 2013-04-17 17:48 ./test/hardlink.sh
root@hello:~/hello/shellscript#

If you see above example of hardlink then you can understand thatin hard link, file’s new copy is created at new location with its original size. Original file’s changes will reflect in new file. But there is no linking showing in long listing because new file is pointing to the original file’s inode. if we delete any one of file or original file then it will not impact on this hard link. Another file will be accessible.

Difference between Soft Link and Hard Link

Difference
Soft Link
Hard Link
Inode
Inode will be different for both the files
Inode will be same for both the files
Deletion of original file
Deletion of original file, impact on link. Another file will not be accessible.
Deletion of original file, no impact on link. Another file will be accessible.
Time to execute
Access time is slow as compared to hard link
Access time is fast as compared to soft link
Cross File System
In Cross file system, Soft link works
In cross file system, hard link is not working. Needed same file system

LVM - Extend a Volume Group Partition in Linux

                Sometimes, If logs are coming in one in LVM partition and you can't stop application or delete logs. So there is only one way to extend the LVM partition. i.e. Extend Logical Volume. But for extending the logical volume, there is need of enough space (our needed free space) in respective Volume group of that logical volume. Following are the simple steps to extend the Volume Group:
Step1: Check the Volume Group details and free space in that. We have 96MB free space in Volume group (MyVolume). But we need more 500MB space in same Volume Group.
[root@server92 ~]# vgs
  VG                         #PV       #LV       #SN        Attr        VSize                     VFree
  MyVolume       1              1              0              wz--n-   496.00m               96.00m
  vgsrv                   1              3              0              wz--n-                   4.47g                     384.00m
Step2: We will create a new LVM partition of 500MB in hard disk in which free space is available and  then we will create its Physical Volume.
[root@server92 ~]# fdisk -cu /dev/sda
Disk /dev/sda: 42.9 GB, 42949672960 bytes
64 heads, 32 sectors/track, 40960 cylinders, total 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00050f52

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      526335      262144   83  Linux
/dev/sda2          526336     9914367     4694016   8e  Linux LVM
/dev/sda3         9914368    10938367      512000    5  Extended
/dev/sda4        10938368    11962367      512000   83  Linux  /// We have already created partition, need
/dev/sda5         9916416    10938367      510976   8e  Linux LVM                                        //to change only type.

Command (m for help): t
Partition number (1-5): 4
Hex code (type L to list codes): 8e
Changed system type of partition 4 to 8e (Linux LVM)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@server92 ~]# pvcreate /dev/sda4
  Physical volume "/dev/sda4" successfully created
[root@server92 ~]#
Step3:  Now add this newly created partition in MyVolume Volume Group. i.e. extend the Volume Group as:
[root@server92 ~]# pvs
  PV                        VG       Fmt  Attr PSize   PFree
  /dev/sda2         vgsrv    lvm2 a-     4.47g 384.00m
  /dev/sda4           lvm2 a-   500.00m 500.00m
  /dev/sda5         MyVolume lvm2 a-   496.00m  96.00m
[root@server92 ~]#
[root@server92 ~]# vgextend MyVolume /dev/sda4
  Volume group "MyVolume" successfully extended
[root@server92 ~]#
[root@server92 ~]# vgs
  VG                        #PV #LV #SN Attr   VSize   VFree
  MyVolume       2   1   0 wz--n- 992.00m 592.00m
  vgsrv                   1   3   0 wz--n-   4.47g 384.00m
[root@server92 ~]#

Now, you have free space of 592 MB in Volume group (MyVolume). And you can extend the logical volume by one more command:
[root@server92 ~]# df -kh
Filesystem                                          Size  Used Avail Use% Mounted on
/dev/mapper/vgsrv-root             3.3G  2.1G  1.1G  67% /
tmpfs                                                    246M     0  246M   0% /dev/shm
/dev/sda1                                           248M   30M  206M  13% /boot
/dev/mapper/vgsrv-home           248M   11M  226M   5% /home
/dev/mapper/MyVolume-MyLogicalVol       388M   11M  358M   3% /MyData
[root@server92 ~]#
[root@server92 ~]# lvextend -L +300M /dev/mapper/MyVolume-MyLogicalVol
  Extending logical volume MyLogicalVol to 700.00 MiB
  Logical volume MyLogicalVol successfully resized
[root@server92 ~]#
[root@server92 ~]#
[root@server92 ~]# resize2fs /dev/mapper/MyVolume-MyLogicalVol
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/MyVolume-MyLogicalVol is mounted on /MyData; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/mapper/MyVolume-MyLogicalVol to 716800 (1k) blocks.
The filesystem on /dev/mapper/MyVolume-MyLogicalVol is now 716800 blocks long.

[root@server92 ~]# df -kh
Filesystem                                          Size  Used Avail Use% Mounted on
/dev/mapper/vgsrv-root             3.3G  2.1G  1.1G  67% /
tmpfs                                                     246M     0  246M   0% /dev/shm
/dev/sda1                                           248M   30M  206M  13% /boot
/dev/mapper/vgsrv-home            248M   11M  226M   5% /home
/dev/mapper/MyVolume-MyLogicalVol       679M   11M  633M   2% /MyData
[root@server92 ~]#

Friday, January 24, 2014

LVM - Resize a Logical Volume Partition in Linux

This is a short tutorial that explains how to resize (reduce/enlarge) a logical volume in Linux.


Logical volumes may be resized dynamically while preserving the data on the volume.

Before we start this exercise of resizing a logical volume, let us look at our LVM setup.

Hard disk 1 - /dev/sda - 2 GB
Volume group - vg_sda
Logical volume - lv_sda - size 2 GB

The address of the logical volume is - /dev/vg_sda/lv_sda

Logical volume is mounted at - /mnt/volume-a.

Reducing a logical volume

Task : Reduce the logical volume /dev/vg_sda/lv_sda from 2 GiB to 1 GiB size.

Reducing a logical volume involves following 3 steps namely -

  1. Unmount the logical volume (if it is mounted)
  2. Reduce the file system residing on the logical volume. And finally,
  3. Reduce the logical volume.

Step 1 - Unmount the logical volume


If the logical volume is not mounted anywhere, you can skip this step. 

Since our logical volume has been mounted at /mnt/volume-a, we have to unmount it.

# umount /mnt/volume-a

Step 2 - Reduce the file system residing on the logical volume


First run e2fsck to check the file system as follows -

# e2fsck -f /dev/vg_sda/lv_sda

Next resize the filesystem on the logical volume to 1 GiB size using the resize2fscommand.

# resize2fs /dev/vg_sda/lv_sda 1G
Resizing the filesystem on /dev/vg_sda/lv_sda to 262144 (4k) blocks.
The filesystem on /dev/vg_sda/lv_sda is now 262144 blocks long.

Step 3 - Reduce the logical volume


To reduce the size of a logical volume, we use the lvreduce LVM command.

Now lets reduce the logical volume /dev/vg_sda/lv_sda from 2 GiB to 1 GiB.

# lvreduce -L 1G /dev/vg_sda/lv_sda
    WARNING: Reducing active logical volume to 1.00 GiB
    THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_sda? [y/n]: y
    Reducing logical volume lv_sda to 1.00 GiB
    Logical volume lv_sda successfully resized.
                                                            OR
# lvreduce -L -500M  /dev/vg_sda/lv_sda
    WARNING: Reducing active logical volume to 1.00 GiB
    THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_sda? [y/n]: y
    Reducing logical volume lv_sda to 1.00 GiB
    Logical volume lv_sda successfully resized.               Note:This will decrease the logical volume 500 MB from the 2GB new lv is 1.5GB


You should be careful when reducing a logical volume's size, because data in the reduced part is lost.

Expanding a logical volume


Steps for expanding/growing a logical volume are exact opposite of those for shrinking /reducing the logical volume.(unmount the partion  is not required while expanding the size of logical volume)

This involves -

  1. Enlarge the logical volume. Then,
  2. Resize the filesystem to the new size of your logical volume.

Step 1: Enlarge the logical volume


lvextend is the LVM command you use to enlarge/expand a logical volume.

So to expand the logical volume to 2 GiB size, we run the following command -

# lvextend -L +1G /dev/vg_sda/lv_sda
    Extending logical volume lv_sda to 2.00 GiB
    Logical volume lv_sda successfully resized.
                                                                    
                                                              OR

# lvextend -L 2G /dev/vg_sda/lv_sda
    Extending logical volume lv_sda to 2.00 GiB
    Logical volume lv_sda successfully resized.

The '+' in '+1G' indicate the value is added to the actual size of the logical volume. Without the + sign, the value is taken as an absolute one.

Step 2: Resize the filesystem

First check the filesystem using e2fsck command -

# e2fsck -f /dev/vg_sda/lv_sda

Now resize the filesystem using the resize2fs command as follows -

# resize2fs /dev/vg_sda/lv_sda 2G
Resizing the filesystem on /dev/vg_sda/lv_sda to 523264 (4k) blocks.
The filesystem on /dev/vg_sda/lv_sda is now 523264 blocks long.

List of commands


To recap, following are the commands we have used in this tutorial.

lvreduce - Reduce the size of a logical volume.

lvextend - Extend the size of a logical volume.

e2fsck - Check the filesystem prior to making changes to it.

resize2fs - A tool to resize ext2 / ext3 / ext4 file systems.

Wednesday, January 22, 2014

Logical Volume Manager (LVM)

Logical Volume Manager (LVM)


This article describes a basic logic behind a Linux logical volume manager by showing real examples of configuration and usage. Although Debian Linux will be used for this tutorial, you can also apply the same command line syntax with other Linux distributions such as Red Hat, Mandriva, SuSe Linux and others.

1. This is what we are going to do

Logical Volume Manager

Scenario:

In this example let us
  1. Create 3 partitions of size each 100MB.
  2. Convert them into physical  volumes.
  3. Combine physical volumes into volume group.
  4. Finally create a logical volume from the volume group.

Create Partitions

Use fdisk command to create and manage partions.
To view the existing partitions use following command
[root@server ~]# fdisk -l
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007b12c
   Device Boot      Start         End      Blocks   Id  System
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ac451
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         128     1024000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2             128         291     1310720   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             291        1045     6052864   83  Linux
The above output shows us two physical hard disks. The /dev/sda contains three partitions and no space to create additional partions. And the second drive /dev/sdb contains no partions yet. So let us use the second one in this tutorial.
Now let us create three partions of each size 100MB using fdisk command.
[root@server ~]# fdisk /dev/sdb 
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1044, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +100M
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (15-1044, default 15): 
Using default value 15
Last cylinder, +cylinders or +size{K,M,G} (15-1044, default 1044): +100M
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (29-1044, default 29): 
Using default value 29
Last cylinder, +cylinders or +size{K,M,G} (29-1044, default 1044): +100M
To check whether the partions have been created use the parameter “p”.
Command (m for help): p
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007b12c
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15          28      112455   83  Linux
/dev/sdb3              29          42      112455   83  Linux
Save the newly created partions.
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
Update the kernel to save the changes without restarting the system.
[root@server ~]# partprobe 
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy).  As a result, it may not reflect all of your changes until after reboot.
Again we will check the existing partitions using fdisk command.
[root@server ~]# fdisk -l
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007b12c
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15          28      112455   83  Linux
/dev/sdb3              29          42      112455   83  Linux
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ac451
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         128     1024000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2             128         291     1310720   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             291        1045     6052864   83  Linux
The above output shows three partions has been created in the /dev/sdb disk. If fdisk -l doesn’t show the output reboot to take effect.

Create Physical Volumes

Now create physical volumes using the command pvcreate.
[root@server ~]# pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb3 
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created
  Physical volume "/dev/sdb3" successfully created
We can use like this also
[root@server ~]# pvcreate /dev/sdb{1,2,3} 

To verify the newly created physical volumes use the command pvdisplay.
[root@server ~]# pvdisplay 
  "/dev/sdb1" is a new physical volume of "109.79 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               109.79 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               jQl5F4-DyLj-SkHu-4lhZ-J3nQ-zax9-aT8sc4
   
  "/dev/sdb2" is a new physical volume of "109.82 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2
  VG Name               
  PV Size               109.82 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               i4MHvw-8hYB-Fwz8-fxTL-G3mu-fl5E-zGYhDO
   
  "/dev/sdb3" is a new physical volume of "109.82 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb3
  VG Name               
  PV Size               109.82 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               99qkNw-3oAw-vXwg-WE6U-zyKO-Ffs3-rDSqUY

Create Volume Groups

Create a new volume group called vg1 using two physical volumes /dev/sdb1 and /dev/sdb2 using the command vgcreate.
[root@server ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2 
  Volume group "vg1" successfully created
We can use like this also
[root@server ~]# vgcreate vg1 /dev/sdb{1,2}
To verify the volume group has been created or not use the command vgdisplay.
[root@server ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg1
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               216.00 MiB
  PE Size               4.00 MiB
  Total PE              54
  Alloc PE / Size       0 / 0   
  Free  PE / Size       54 / 216.00 MiB
  VG UUID               ds3OtP-DMUx-33nN-HDar-eqNj-uIED-41gjqI

Create Logical Volume

To create logical volume use the command lvcreate. Let us create a logical volume called lv1 with size 200MB.
[root@server ~]# lvcreate -L 200M vg1 -n lv1
  Logical volume "lv1" created
Verify the logical volume is created or not using command lvdisplay.
[root@server ~]# lvdisplay 
  --- Logical volume ---
  LV Name                /dev/vg1/lv1
  VG Name                vg1
  LV UUID                dgLZ79-JZdn-NUSF-fUS1-YVFk-36qs-iuafhE
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                200.00 MiB
  Current LE             50
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

Format and Mount the logical volume

Now format the newly created logical volume and mount it in the /mnt directory or wherever you want.
[root@server ~]# mkfs.ext4 /dev/vg1/lv1 
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks: 
 8193, 24577, 40961, 57345, 73729
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
And mount the logical volume in the /mnt mount point.
[root@server ~]# mount /dev/vg1/lv1 /mnt/
Now the logical volume is successfully mounted in /mnt. You can use the new logical volume to store your datas.
[root@server ~]# cd /mnt/
[root@server mnt]# touch file1 file2 file3
[root@server mnt]# mkdir dir1 dir2 dir3
[root@server mnt]# ls
dir1  dir2  dir3  file1  file2  file3  lost+found