Wednesday, November 6, 2013

Configure Logs for LVM in linux

 Log all LVM commands

[root@test ~]# cp /etc/lvm/lvm.conf /etc/lvm/lvm.conf_backup
[root@test ~]# vi /etc/lvm/lvm.conf


log {

    # Controls the messages sent to stdout or stderr.
    # There are three levels of verbosity, 3 being the most verbose.
    verbose = 0

    # Should we send log messages through syslog?
    # 1 is yes; 0 is no.
    syslog = 1

    # Should we log error and debug messages to a file?
    # By default there is no log file.
    #file = "/var/log/lvm2.log"

    # Should we overwrite the log file each tisme the program is run?
    # By default we append.
    overwrite = 0

    # What level of log messages should we send to the log file and/or syslog?
    # There are 6 syslog-like log levels currently in use - 2 to 7 inclusive.
    # 7 is the most verbose (LOG_DEBUG).
    level = 0

   # Format of output messages
    # Whether or not (1 or 0) to indent messages according to their severity
    indent = 1

    # Whether or not (1 or 0) to display the command name on each line output
    command_names = 0

    # A prefix to use before the message text (but after the command name,
    # if selected).  Default is two spaces, so you can see/grep the severity
    # of each message.
    prefix = "  "

    # To make the messages look similar to the original LVM tools use:
    #   indent = 0
    #   command_names = 1
    #   prefix = " -- "

    # Set this if you want log messages during activation.
    # Don't use this in low memory situations (can deadlock).
    # activation = 0
}

2 modifications to be done

Uncomment the line # file = “/var/log/lvm2.log

Change the level = 0 to a value between 2 and 7.

What level of log messages should we send to the log file and/or syslog?

    # There are 6 syslog-like log levels currently in use - 2 to 7 inclusive.
    # 7 is the most verbose (LOG_DEBUG).

Enter level=7

save and exit.

Just test it with the lvm command and view the logs

This is not the right syntax, just showed here for example.

[root@test ~]# lvcreate VG1
  Please specify either size or extents
  Run `lvcreate --help' for more information.

[root@test ~]# tail -f /var/log/lvm2.log
activate/activate.c:471   Getting target version for striped
ioctl/libdm-iface.c:1687   dm versions   OF   [16384] (*1)
lvcreate.c:363   Please specify either size or extents
lvcreate.c:768   <backtrace>
lvmcmdline.c:623   Run `lvcreate --help' for more information.
lvmcmdline.c:1123   Completed: lvcreate VG1
cache/lvmcache.c:1602   Wiping internal VG cache
metadata/vg.c:74   Freeing VG #orphans_lvm1 at 0x1f09d90.
metadata/vg.c:74   Freeing VG #orphans_pool at 0x1f0e600.
metadata/vg.c:74   Freeing VG #orphans_lvm2 at 0x1f13110.



Restore removed LVM partition

[root@test ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              20G   18G  698M  97% /
/dev/mapper/VG1-backup
                       49G  197M   46G   1% /backup
/dev/mapper/VG1-downloads
                       50G   52M   47G   1% /downloads
/dev/mapper/VG1-home   49G  181M   46G   1% /home
tmpfs                 979M     0  979M   0% /dev/shm
/dev/mapper/VG1-public
                       59G  641M   55G   2% /public

[root@test ~]# lvremove /dev/VG1/public
  Can't remove open logical volume "public"

[root@test ~]# dmsetup info -c /dev/VG1/public
Name             Maj Min Stat Open Targ Event  UUID                                                        
VG1-public       253   7 L--w    1    2      0 LVM-8FQ0SbqlR7hlFHpG2rnxUQ2WvafgEa1yMvxZcKGIIj6Rippe9VdxO4MptcgKaO1O

Device mapper maintains entry for logical drives, so first we need to delete it from there. Then it can't restrict us while removing logical volumes which are in open status.
rom above table you can see Open = 1 which means the logical volume testlv is open status, so we need to delete entry for this logical volume from dmsetup table then only we can delete any logical volume with open status.  let's do it

[root@test /]# dmsetup remove --force /dev/VG1/backup
device-mapper: remove ioctl failed: Device or resource busy
Command failed

[root@test ~]# umount /public/
[root@test ~]# lvremove /dev/VG1/public
Do you really want to remove active logical volume public? [y/n]: y
  Logical volume "public" successfully removed
[root@test ~]# lvs
  LV        VG   Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  backup    VG1  -wi-ao  50.00G
  downloads VG1  -wi-ao  54.00G
  home      VG1  -wi-ao  50.00G
  images    VG1  -wi-a-  50.00G
  stripe    VG1  -wi-a- 128.00M
[root@test ~]# vgcfgrestore --list VG1                        
                                                                   
  File:         /etc/lvm/archive/VG1_00011-1959345004.vg          
  VG name:      VG1                                                
  Description:  Created *before* executing 'lvremove /dev/VG1/snap1'
  Backup Time:  Wed Sep 11 20:23:22 2013                          

 
  File:         /etc/lvm/archive/VG1_00012-1817284231.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvcreate -s --name backup-snap --size 1G /dev/VG1/backup'
  Backup Time:  Wed Sep 11 20:48:21 2013                                                          

 
  File:         /etc/lvm/archive/VG1_00013-1680048821.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvremove /dev/VG1/backup-snap'
  Backup Time:  Mon Sep 16 09:13:22 2013                                

 
  File:         /etc/lvm/archive/VG1_00014-2084989037.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvremove /dev/VG1/dbba'
  Backup Time:  Mon Sep 16 09:13:47 2013                        

 
  File:         /etc/lvm/archive/VG1_00015-1654653445.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvremove /dev/VG1/public_snap'
  Backup Time:  Mon Sep 16 09:14:14 2013                                

 
  File:         /etc/lvm/archive/VG1_00016-1108210628.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvremove /dev/VG1/test1'
  Backup Time:  Mon Sep 16 09:14:25 2013                          

 
  File:         /etc/lvm/archive/VG1_00017-319369726.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvremove /dev/VG1/home1'
  Backup Time:  Mon Sep 16 09:14:35 2013                          

 
  File:         /etc/lvm/archive/VG1_00018-1088715738.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvremove /dev/VG1/home2'
  Backup Time:  Mon Sep 16 09:14:45 2013                          

 
  File:         /etc/lvm/archive/VG1_00019-950166769.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvremove /dev/VG1/dbbackup'
  Backup Time:  Mon Sep 16 09:15:03 2013                            

 
  File:         /etc/lvm/archive/VG1_00020-722332065.vg
  VG name:      VG1                                  
  Description:  Created *before* executing 'lvresize -L -1G /dev/VG1/public'
  Backup Time:  Mon Sep 16 09:20:38 2013                                  


  File:         /etc/lvm/archive/VG1_00021-227400998.vg
  VG name:      VG1
  Description:  Created *before* executing 'lvresize -L +1G /dev/VG1/public'
  Backup Time:  Mon Sep 16 09:21:18 2013


  File:         /etc/lvm/archive/VG1_00022-271935578.vg
  VG name:      VG1
  Description:  Created *before* executing 'vgsplit VG1 test /dev/sda5'
  Backup Time:  Mon Sep 16 10:01:04 2013


  File:         /etc/lvm/archive/VG1_00023-196107041.vg
  VG name:      VG1
  Description:  Created *before* executing 'lvremove /dev/VG1/public'
  Backup Time:  Tue Sep 17 09:35:07 2013


  File:         /etc/lvm/backup/VG1
  VG name:      VG1
  Description:  Created *after* executing 'lvremove /dev/VG1/public'
  Backup Time:  Tue Sep 17 09:35:07 2013

[root@test ~]# vgcfgrestore -f /etc/lvm/archive/VG1_00023-196107041.vg
  Please specify a *single* volume group to restore.

You also need to specify the volume group name

[root@test ~]# vgcfgrestore -f  /etc/lvm/archive/VG1_00023-196107041.vg VG1
  Restored volume group VG1
[root@test ~]# lvs
  LV        VG   Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  backup    VG1  -wi-ao  50.00G
  downloads VG1  -wi-ao  54.00G
  home      VG1  -wi-ao  50.00G
  images    VG1  -wi-a-  50.00G
  public    VG1  -wi---  60.00G
  stripe    VG1  -wi-a- 128.00M

Logical volume public is present now.

lvdisplay

  --- Logical volume ---                                    
  LV Name                /dev/VG1/public                    
  VG Name                VG1                                
  LV UUID                MvxZcK-GIIj-6Rip-pe9V-dxO4-Mptc-gKaO1O
  LV Write Access        read/write                          
  LV Status              NOT available                      
  LV Size                60.00 GB                            
  Current LE             1920                                
  Segments               2                                  
  Allocation             inherit                            
  Read ahead sectors     auto            

The status is not available. Make it active

[root@test ~]# lvchange -a y /dev/VG1/public

[root@test ~]# lvdisplay
--- Logical volume ---                                    
  LV Name                /dev/VG1/public                    
  VG Name                VG1                                
  LV UUID                MvxZcK-GIIj-6Rip-pe9V-dxO4-Mptc-gKaO1O
  LV Write Access        read/write                          
  LV Status              available                          
  # open                 0                                  
  LV Size                60.00 GB                            
  Current LE             1920                                
  Segments               2                                  
  Allocation             inherit                            
  Read ahead sectors     auto                                
  - currently set to     256                                
  Block device           253:1  


Now it is in available state, mount and see the data is present or not.

[root@test ~]# mount /dev/VG1/public /public

[root@test ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              20G   18G  699M  97% /
/dev/mapper/VG1-backup
                       49G  197M   46G   1% /backup
/dev/mapper/VG1-downloads
                       50G   52M   47G   1% /downloads
/dev/mapper/VG1-home   49G  181M   46G   1% /home
tmpfs                 979M     0  979M   0% /dev/shm
/dev/mapper/VG1-public
                       59G  641M   55G   2% /public
[root@test public]# ls
testdir
[root@test public]# cd testdir/
[root@test testdir]# ls
1    11  14  17  2   22  25  28  30  33  36  39  41  44  47  5   52  55  58  60  63  66  69  71  74  77  8   82  85  88  90  93  96  99
10   12  15  18  20  23  26  29  31  34  37  4   42  45  48  50  53  56  59  61  64  67  7   72  75  78  80  83  86  89  91  94  97
100  13  16  19  21  24  27  3   32  35  38  40  43  46  49  51  54  57  6   62  65  68  70  73  76  79  81  84  87  9   92  95  98

the data are present.




Configure Swapiness in Linux

Setting swapiness means that virtual swap memory is not used until the physical memory gets full. To avoid this you can set swapiness value.

Linux system comes with a default of 60, meaning that the swap file will be used fairly often if the memory usage is around half of my RAM.


You can check your system's swappiness value by running:

[root@test ~]# cat /proc/sys/vm/swappiness
60

So If i have 4 GB of RAM, and I like to turn that down to 10 or 15. The swap file will then only be used when my RAM usage is around 85 or 90 percent.

To change the system swappiness value

vim /etc/sysctl.conf as root. Then, change or add this line to the file:

vm.swappiness = 10

Reboot for the change to take effect

You can also change the value while your system is still running

sysctl vm.swappiness=10

You can also clear your swap by running swapoff -a and then swapon -a as root instead of rebooting to achieve the same effect.

To calculate your swap Formula

free -m (total) / 100 = A

A * 10


[root@test ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          3950       2262       1687          0        407        952
-/+ buffers/cache:        903       3047
Swap:         1953          0       1953

    so total is 3950 / 100 = 39.5 * 10 = 395

It means that when 10 % 395 MB of ram left, then it start using swapiness.