Monday, 29 April 2013

How to Reduce a Logical Volume in GNU/Linux Using LVM


How to Reduce a Logical Volume in GNU/Linux Using LVM

Logical Volume Management is a vast improvement over standard partitioning schemes. Among many other things, it allows you to decrease the size of a volume without recreating it completely.
All of the required steps must be performed on an unmounted volume. If want to reduce the size of a non-root volume, simply unmount it. For a root volume, you’ll have to boot from a CD. Any modern live or rescue CD should work fine
In this article I will explain how to reduce the Logical Volume in Red Hat Linux system without loss of data.

STEPS:-

1.   Un mount the logical volume: 

umount /mnt/logical_volume1

Ex-
[root@server01 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root     50G   26G   22G  55% /
tmpfs                                         7.8G     0  7.8G   0% /dev/shm
/dev/sda1                                            485M   32M  429M   7% /boot
/dev/mapper/VolGroup-lv_home  491G   55G  412G  12% /home

[root@server01 ~]# umount /home
If this file system is in use, Use the following commands to unmount partition forcefully.

[root@server01 ~]# fuser –km /home
[root@server01 ~]# umount  –f /home
2.   Make a backup of logical Volume.

Steps:
  1. Check the size of the logical volume (LV Size): 
lvdisplay /dev/volume_group1/logical_volume1


Ex- 

--- Logical volume ---
LV Name /dev/mapper/VolGroup-lv_home
VG Name VolGroup
LV UUID AxihqP-Yt8l-5scY-bXNG-Bn5D-K3ms-X7v1Ys
LV Write Access read/write
LV Status available
# open 1
LV Size 200,00 GB
Current LE 6400
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:5

Usually, snapshots are smaller than the original logical volume, but I recommend to use at least the same space.
  1. Create the snapshot: 
lvcreate -L200G -s -n backup_logical_volume1 /dev/volume_group1/logical_volume1

The -s param tells lvcreate to create a snapshot instead of a normal logical volume.

Ex-

lvcreate -L200G -s -n backup_logical_volume  /dev/mapper/VolGroup-lv_home

3.   Check the file system integrity: 

fsck -f -y -v /dev/volume_group1/logical_volume1

Ex-
[root@server01 ~]#   fsck -f -y -v /dev/mapper/VolGroup-lv_home
fsck from util-linux-ng 2.17.2
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

 2634206 inodes used (8.06%)
   62765 non-contiguous files (2.4%)
       7 non-contiguous directories (0.0%)
         # of inodes with ind/dind/tind blocks: 0/0/0
         Extent depth histogram: 2634192/4
16246730 blocks used (12.43%)
       0 bad blocks
       3 large files

 2589225 regular files
   44972 directories
       0 character device files
       0 block device files
       0 fifos
       0 links
       0 symbolic links (0 fast symbolic links)
       0 sockets
--------
 2634197 files


4.   Resize the file system to something smaller than the final size (around 190GB in this case): 

resize2fs /dev/volume_group1/logical_volume1 190G

This Step takes a long time to resize the volume.

Ex-

[root@server01 ~]# resize2fs /dev/mapper/VolGroup-lv_home 190G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/VolGroup-lv_home to 49807360 (4k) blocks.
The filesystem on /dev/mapper/VolGroup-lv_home is now 49807360 blocks long.

5.   Check the filesystem integrity again: 

fsck -f -y -v /dev/volume_group1/logical_volume1

Ex-

fsck -f -y –v /dev/mapper/VolGroup-lv_home

6.   Reduce the logical volume: 

lvreduce -L 85G /dev/volume_group1/logical_volume1

Ex-

[root@server01 ~]# lvreduce -L 195G /dev/mapper/VolGroup-lv_home
  WARNING: Reducing active logical volume to 195.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_home? [y/n]: y
  Reducing logical volume lv_home to 195.00 GiB
  Logical volume lv_home successfully resized


7.   Resize the filesystem to fit the logical volume: 

resize2fs /dev/volume_group1/logical_volume1

Ex-

[root@server01 ~]# resize2fs /dev/mapper/VolGroup-lv_home
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/VolGroup-lv_home to 51118080 (4k) blocks.
The filesystem on /dev/mapper/VolGroup-lv_home is now 51118080 blocks long.

8.   Check the filesystem to know whether the reduction went fine:

 fsck -f -y -v /dev/volume_group1/logical_volume1

Ex-
fsck -f -y –v /dev/mapper/VolGroup-lv_home


9.   Check the size of Logical Volume.

[root@server01 ~]# lvs

  LV      VG       Attr     LSize   Pool Origin Data%  Move Log Copy%  Convert
  lv_home VolGroup -wi-a--- 195.00g
  lv_root VolGroup -wi-ao--  50.00g
  lv_swap VolGroup -wi-ao--   7.84g


10.                Mount the logical volume: 

mount /mnt/logical_volume1

Ex-

mount  /dev/mapper/VolGroup-lv_home  /home



NOTE: if you aren’t careful with the disk space you are using and the final disk space you are going to be using, this operation can cause data loss. I recommend to backup the logical volume before the reduction and to use around 5GB of “safe space” while reducing the filesystem in order to avoid data loss.

Tuesday, 23 April 2013

Redirection of Website to Error Page after Not Responding in Linux OS


Redirection of Website to Error Page after Not Responding in Linux OS

$ vi /home/script
open a file and copy following script on server.

#!/bin/bash
#Please enter IP Address of server and port on which application is running.
# For Ex -   x=`nc -w2 -z 172.16.0.1 80   2>&1`
x=`nc -w2 -z <IP ADDRESS> <PORT>   2>&1`
if [ $? != 0 ];then
        cp -f /etc/httpd/conf/httpd_error.conf /etc/httpd/conf/httpd.conf
        service httpd reload
else
        y=`grep -lir "down1" /etc/httpd/conf/httpd.conf`
if [ $? = 0 ];then
        cp -f /etc/httpd/conf/ httpd_right.conf /etc/httpd/conf/httpd.conf
        service httpd reload
else
        echo "already up"
fi
fi

Configure Script in crontab to check connectivity of Website at every 1 Min.

[root@APP01 conf]# crontab –l
*/1 * * * * /home/script

Configure Error Page in “/etc/httpd/conf/httpd_error.conf ” file in path of /etc/httpd/conf directory and working site in httpd_right.conf file.

[root@APP01 conf]# pwd
/etc/httpd/conf
[root@APP01 conf]# ls /etc/httpd/conf
httpd_error.conf httpd.conf httpd_right

Scripts check down1 keyword in /etc/httpd/conf/httpd_error.conf file so add commented word down1 in it anywhere.

[root@APP01 conf]# cat /etc/httpd/conf/httpd_error.conf | grep down1
##down1



Add Disk Space into XEN Virtual Machine (.img Disk Image)


Add Disk Space into XEN Virtual Machine (.img Disk Image)

Sometimes, you need more space on a virtual machine disk than you thought in the beginning. Hopefully, if your domU disk is in a .img file, you can do it quite easily (you can do it easily with lvm partition too, even if it’s a different method). Here’s how to do it, as root of course:

- Check your partitions in your domU:
df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda2             10G  706M  8.8G   8% /
varrun                2.1G   44K  2.1G   1% /var/run
varlock               2.1G     0  2.1G   0% /var/lock
udev                  2.1G   16K  2.1G   1% /dev
devshm                2.1G     0  2.1G   0% /dev/shm

- create empty file of the size we want to add: (10gb here)
dd if=/dev/zero of=/xen/temp_expand  bs=1024k count=10000

- stop your domU
xm shutdown mydomU

- backup img files
cp -a /xen/domains/mydomU /xen/domains/mydomU-bak

- add the empty file to the img file :
cat /xen/temp_expand >> /xen/domains/mydomU/disk.img
You can repeat this step as many times as you need

- Tell the file system to check and adapt its size:
resize2fs -f /xen/domains/mydomU/disk.img

- restart your domU
xm create -c mydomU.cfg

- check in your domU that everything is ok
df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda2             30G  711M   28G   3% /
varrun                2.1G   44K  2.1G   1% /var/run
varlock               2.1G     0  2.1G   0% /var/lock
udev                  2.1G   20K  2.1G   1% /dev
devshm                2.1G     0  2.1G   0% /dev/shm
Voila! Your disk is now bigger :)

You can also use this method to create a brand new blank disk image, and add it as a new disk for you domU:
dd if=/dev/zero of=/xen/domains/mydomU/otherdisk.img bs=1024k count=150000

or

cat /xen/temp_expand >> /xen/domains/mydomU/otherdisk.img

add the new disk (xvda3) to your /etc/xen/mydomU.cfg :

disk        = [
                  'tap:aio:/xen/domains/mydomU/swap.img,xvda1,w',
                  'tap:aio:/xen/domains/mydomU/disk.img,xvda2,w',
                  'tap:aio:/xen/domains/mydomU/otherdisk.img,xvda3,w',
              ]
After rebooting you domU, check if the new disk is available :

fdisk -l /dev/xvda3

Disk /dev/xvda3: 125.8 GB, 125829120000 bytes
255 heads, 63 sectors/track, 15297 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00000000

Disk /dev/xvda3 doesn't contain a valid partition table
You’re now free to create your file system on this disk, and to mount it wherever you want!
$ mkfs.ext3 /dev/xvda3
$ mkdir /mysql
$ mount /dev/xvda3 /mysql/
$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda2             30G  711M   28G   3% /
varrun                2.1G   44K  2.1G   1% /var/run
varlock               2.1G     0  2.1G   0% /var/lock
udev                  2.1G   20K  2.1G   1% /dev
devshm                2.1G     0  2.1G   0% /dev/shm
/dev/xvda3            117G  188M  111G   1% /mysql

Friday, 12 April 2013

ROUTING and GATEWAY Configuration (Red Hat, Ubuntu and Windows)


ROUTING and GATEWAY

Gateway:  Gateway is a network point that acts as an entrance to another network. A gateway is required to convert Data packets from one protocol format to another Protocol format, as it connects two dissimilar networks.

Router: A router is connected to two different networks and passes packets between them. Router and Gateway both are sometimes used interchangeably but the difference is that Gateway operates on 7th layer of the OSI Model and the Router works on the 3rd layer of the OSI model.

Here we will describe some routing configuration on different platforms.

Linux Red Hat
1.    To view the current routing table run “route -n
[root@cdacqmail ~]# route -n
Kernel IP routing table
Destination       Gateway                       Genmask           Flags   Metric   Ref    Use Iface
169.254.95.0      0.0.0.0                         255.255.255.0   U          1           0        0 usb0
220.156.188.0     0.0.0.0                         255.255.254.0   U          0           0        0 eth1
169.254.0.0        0.0.0.0                         255.255.0.0        U          1002      0        0 eth0
169.254.0.0        0.0.0.0                         255.255.0.0        U          1003      0        0 eth1
10.0.0.0             0.0.0.0                         255.0.0.0           U          0            0        0 eth0
0.0.0.0               220.156.188.145           0.0.0.0              UG       0           0        0 eth1

2. Adding a Default Gateway

route add default   gw  220.156.188.145
3.    To add a route refers to the command below.

route add -net 10.226.16.0 netmask 255.255.240.0 gw 10.226.0.201

4.    To delete a route refer to the command below.
route del -net 10.226.16.0 netmask 255.255.240.0 gw 10.226.0.201
The routing information above is not persistent across reboots. After a reboot, the routing information will be lost and you need to add them in again.
5.    To make the routing information persistent, add the “route add” line as seen above into the /etc/rc.local file.
Sample /etc/rc.local file.
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.


touch /var/lock/subsys/local
route add -net 10.226.16.0 netmask 255.255.240.0 gw 10.226.0.201


6.    Reject Routing to a Particular Host or Network

route add -host 192.168.1.51 reject

7.    If you want to reject an entire network (192.168.1.1 – 192.168.1.255), then add the following entry.
 route add -net 192.168.1.0 netmask 255.255.255.0 reject



UBUNTU

1.    Adding Route

route add -net $NET netmask $MASK gw $GATEWAY
route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.254


You need to add above syntax to post-up command as follows:
post-up command
post-up route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.254

Given command will run after bringing the interface up.

Example


Edit /etc/network/interfaces, enter:
$ sudo vi /etc/network/interfaces

 
#---------------------------------------#
# Feel free to change IP and gateway    #
# as per your local setup and routing   #
# policy                                #
# Last edited by root @ 23/Oct/2012     #
#---------------------------------------#
 
#--------------------------------------------#
# Setup the loopback network interface (lo0) #
#--------------------------------------------#
auto lo
iface lo inet loopback
 
#--------------------------------------------#
# Setup eth0 - connected to private LAN/VLAN #
#--------------------------------------------#
auto eth0
allow-hotplug eth0
iface eth0 inet static
        address 10.70.201.5
        netmask 255.255.255.192
        ### Ubuntu Linux add persistent route command ###
        post-up route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.70.201.6
 
#----------------------------------------#
# Setup eth1 - connected to the Internet #
#----------------------------------------#
auto eth1
allow-hotplug eth1
iface eth1 inet static
        address 205.153.203.98
        netmask 255.255.255.248
        ### Ubuntu Linux - This is your default gateway ###
        gateway 205.153.203.97
 
Save and close the file.
2.    You need to restart the networking as follows:



$ sudo service networking restart



OR  as root user:


# service networking restart

WINDOWS Server

1.    You can check routing in windows by following step:

C:\Documents and Settings\jitendrakumar>route print

IPv4 Route Table
===========================================================================
Interface List
0x1 ........................... MS TCP Loopback interface
0x20003 ...00 23 ae 77 ee 57 ...... Broadcom NetLink (TM) Gigabit Ethernet
===========================================================================
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0     10.226.0.201     10.226.1.206     10
         10.0.0.0    255.255.240.0       10.0.0.206     10.226.1.206     10
       10.0.0.206  255.255.255.255        127.0.0.1        127.0.0.1     10
       10.226.0.0    255.255.240.0     10.226.1.206     10.226.1.206     10
     10.226.1.206  255.255.255.255        127.0.0.1        127.0.0.1     10
   10.255.255.255  255.255.255.255     10.226.1.206     10.226.1.206     10
        127.0.0.0        255.0.0.0        127.0.0.1        127.0.0.1      1
       172.20.0.0    255.255.254.0     172.20.0.206     10.226.1.206     10
       172.20.0.0      255.255.0.0       172.20.0.1     10.226.1.206      1
     172.20.0.206  255.255.255.255        127.0.0.1        127.0.0.1     10
   172.20.255.255  255.255.255.255     10.226.1.206     10.226.1.206     10
        224.0.0.0        240.0.0.0     10.226.1.206     10.226.1.206     10
  255.255.255.255  255.255.255.255     10.226.1.206     10.226.1.206      1
Default Gateway:      10.226.0.201
===========================================================================
Persistent Routes:
  Network Address          Netmask  Gateway Address  Metric
172.20.0.0         255.255.0.0       172.20.0.1       1


2.    Adding Route in Window server

route  add <destination> mask <subnet mask> <gateway> metric <lowest number wins> if <interface>
Ex-
route ADD 157.0.0.0 MASK 155.0.0.0 157.55.80.1 METRIC 2 IF 2



3.    Deleting route in windows server

route delete 157.0.0.0 MASK 155.0.0.0 157.55.80.1 METRIC 2 IF 2           

4.    Adding Persistent Route.

Routes added by using the -p option are stored in the Windows registry under the following key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip \Parameters\PersistentRoutes

route \[-p\] add <destination> mask <subnet mask> <gateway> metric <lowest number wins> if <interface>


metric Metric : Specifies an integer cost metric (ranging from 1 to 9999) for the route, which is used when choosing among multiple routes in the routing table that most closely match the destination address of a packet being forwarded. The route with the lowest metric is chosen. The metric can reflect the number of hops, the speed of the path, path reliability, path throughput, or administrative properties.
if Interface : Specifies the interface index for the interface over which the destination is reachable. For a list of interfaces and their corresponding interface indexes, use the display of the route print command. You can use either decimal or hexadecimal values for the interface index. For hexadecimal values, precede the hexadecimal number with0x. When the if parameter is omitted, the interface is determined from the gateway address.