Tuesday, 30 May 2017

How to Set System Date and Time in Linux / Unix


Set System Date and Time 
 
To change the system date use:
# date {mmddhhmiyyyy.ss}
o
mm – Month
o
dd – Date
o
hh – 24 hour format
o
mi – Minutes
o
yyyy – Year
o
ss – seconds
For example, to set system date to Jan 31
st
2008, 10:19 p.m, 53 seconds
# date 013122192009.53
You can also change system date using set argument as shown below.
# date 013122192009.53
# date +%Y%m%d -s "20090131"
# date -s "01/31/2009 22:19:53"
# date -s "31 JAN 2009 22:19:53"
# date set="31 JAN 2009 22:19:53"
To set the time only:
# date +%T -s "22:19:53"
18
# date +%T%p -s "10:19:53PM"

How to use FIND command in Unix / Linux / AIX

1. Find Files Using Name in Current Directory

Find all the files whose name is tecmint.txt in a current working directory.
# find . -name tecmint.txt
./tecmint.txt

2. Find Files Under Home Directory

Find all the files under /home directory with name tecmint.txt.
# find /home -name tecmint.txt
/home/tecmint.txt

3. Find Files Using Name and Ignoring Case

Find all the files whose name is tecmint.txt and contains both capital and small letters in /home directory.
# find /home -iname tecmint.txt
./tecmint.txt
./Tecmint.txt

4. Find Directories Using Name

Find all directories whose name is Tecmint in / directory.
# find / -type d -name Tecmint
/Tecmint

5. Find PHP Files Using Name

Find all php files whose name is tecmint.php in a current working directory.
# find . -type f -name tecmint.php
./tecmint.php

6. Find all PHP Files in Directory

Find all php files in a directory.
# find . -type f -name "*.php"
./tecmint.php
./login.php
./index.php
Part II – Find Files Based on their Permissions

7. Find Files With 777 Permissions

Find all the files whose permissions are 777.
# find . -type f -perm 0777 -print

8. Find Files Without 777 Permissions

Find all the files without permission 777.
# find / -type f ! -perm 777

9. Find SGID Files with 644 Permissions

Find all the SGID bit files whose permissions set to 644.
# find / -perm 2644

10. Find Sticky Bit Files with 551 Permissions

Find all the Sticky Bit set files whose permission are 551.
# find / -perm 1551

11. Find SUID Files

Find all SUID set files.
# find / -perm /u=s

12. Find SGID Files

Find all SGID set files.
# find / -perm /g=s

13. Find Read Only Files

Find all Read Only files.
# find / -perm /u=r

14. Find Executable Files

Find all Executable files.
# find / -perm /a=x

15. Find Files with 777 Permissions and Chmod to 644

Find all 777 permission files and use chmod command to set permissions to 644.
# find / -type f -perm 0777 -print -exec chmod 644 {} \;

16. Find Directories with 777 Permissions and Chmod to 755

Find all 777 permission directories and use chmod command to set permissions to 755.
# find / -type d -perm 777 -print -exec chmod 755 {} \;

17. Find and remove single File

To find a single file called tecmint.txt and remove it.
# find . -type f -name "tecmint.txt" -exec rm -f {} \;

18. Find and remove Multiple File

To find and remove multiple files such as .mp3 or .txt, then use.
# find . -type f -name "*.txt" -exec rm -f {} \;
OR
# find . -type f -name "*.mp3" -exec rm -f {} \;

19. Find all Empty Files

To find all empty files under certain path.
# find /tmp -type f -empty

20. Find all Empty Directories

To file all empty directories under certain path.
# find /tmp -type d -empty

21. File all Hidden Files

To find all hidden files, use below command.
# find /tmp -type f -name ".*"
Part III – Search Files Based On Owners and Groups

22. Find Single File Based on User

To find all or single file called tecmint.txt under / root directory of owner root.
# find / -user root -name tecmint.txt

23. Find all Files Based on User

To find all files that belongs to user Tecmint under /home directory.
# find /home -user tecmint

24. Find all Files Based on Group

To find all files that belongs to group Developer under /home directory.
# find /home -group developer

25. Find Particular Files of User

To find all .txt files of user Tecmint under /home directory.
# find /home -user tecmint -iname "*.txt"
Part IV – Find Files and Directories Based on Date and Time

26. Find Last 50 Days Modified Files

To find all the files which are modified 50 days back.
# find / -mtime 50

27. Find Last 50 Days Accessed Files

To find all the files which are accessed 50 days back.
# find / -atime 50

28. Find Last 50-100 Days Modified Files

To find all the files which are modified more than 50 days back and less than 100 days.
# find / -mtime +50 –mtime -100

29. Find Changed Files in Last 1 Hour

To find all the files which are changed in last 1 hour.
# find / -cmin -60

30. Find Modified Files in Last 1 Hour

To find all the files which are modified in last 1 hour.
# find / -mmin -60

31. Find Accessed Files in Last 1 Hour

To find all the files which are accessed in last 1 hour.
# find / -amin -60
Part V – Find Files and Directories Based on Size

32. Find 50MB Files

To find all 50MB files, use.
# find / -size 50M

33. Find Size between 50MB – 100MB

To find all the files which are greater than 50MB and less than 100MB.
# find / -size +50M -size -100M

34. Find and Delete 100MB Files

To find all 100MB files and delete them using one single command.
# find / -size +100M -exec rm -rf {} \;

35. Find Specific Files and Delete

Find all .mp3 files with more than 10MB and delete them using one single command.
# find / -type f -name *.mp3 -size +10M -exec rm {} \;
That’s it, We are ending this post here, In our next article we will discuss more about other Linux commands in depth with practical examples. Let us know your opinions on this article using our comment section.

50 Frequently asked question in Unix / Linux / AIX

1. tar command examples

Create a new tar archive.
$ tar cvf archive_name.tar dirname/
Extract from an existing tar archive.
$ tar xvf archive_name.tar
View an existing tar archive.
$ tar tvf archive_name.tar

2. grep command examples

Search for a given string in a file (case in-sensitive search).
$ grep -i "the" demo_file
Print the matched line, along with the 3 lines after it.
$ grep -A 3 -i "example" demo_text
Search for a given string in all files recursively
$ grep -r "ramesh" *

3. find command examples

Find files using file-name ( case in-sensitve find)
# find -iname "MyCProgram.c"
Execute commands on files found by the find command
$ find -iname "MyCProgram.c" -exec md5sum {} \;
Find all empty files in home directory
# find ~ -empty

4. ssh command examples

Login to remote host
ssh -l jsmith remotehost.example.com
Debug ssh client
ssh -v -l jsmith remotehost.example.com
Display ssh client version
$ ssh -V
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003

5. sed command examples

When you copy a DOS file to Unix, you could find \r\n in the end of each line. This example converts the DOS file format to Unix file format using sed command.
$sed 's/.$//' filename
Print file content in reverse order
$ sed -n '1!G;h;$p' thegeekstuff.txt
Add line number for all non-empty-lines in a file
$ sed '/./=' thegeekstuff.txt | sed 'N; s/\n/ /'

6. awk command examples

Remove duplicate lines using awk
$ awk '!($0 in array) { array[$0]; print }' temp
Print all lines from /etc/passwd that has the same uid and gid
$awk -F ':' '$3==$4' passwd.txt
Print only specific field from a file.
$ awk '{print $2,$5;}' employee.txt

7. vim command examples

Go to the 143rd line of file
$ vim +143 filename.txt
Go to the first match of the specified
$ vim +/search-term filename.txt
Open the file in read only mode.
$ vim -R /etc/passwd

8. diff command examples

Ignore white space while comparing.
# diff -w name_list.txt name_list_new.txt

2c2,3
< John Doe --- > John M Doe
> Jason Bourne

9. sort command examples
Sort a file in ascending order
$ sort names.txt
Sort a file in descending order
$ sort -r names.txt
Sort passwd file by 3rd field.
$ sort -t: -k 3n /etc/passwd | more

10. export command examples

To view oracle related environment variables.
$ export | grep ORACLE
declare -x ORACLE_BASE="/u01/app/oracle"
declare -x ORACLE_HOME="/u01/app/oracle/product/10.2.0"
declare -x ORACLE_SID="med"
declare -x ORACLE_TERM="xterm"
To export an environment variable:
$ export ORACLE_HOME=/u01/app/oracle/product/10.2.0

11. xargs command examples

Copy all images to external hard-drive
# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory
Search all jpg images in the system and archive it.
# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
Download all the URLs mentioned in the url-list.txt file
# cat url-list.txt | xargs wget –c

12. ls command examples

Display filesize in human readable format (e.g. KB, MB etc.,)
$ ls -lh
-rw-r----- 1 ramesh team-dev 8.9M Jun 12 15:27 arch-linux.txt.gz
Order Files Based on Last Modified Time (In Reverse Order) Using ls -ltr
$ ls -ltr
Visual Classification of Files With Special Characters Using ls -F
$ ls -F

13. pwd command
pwd is Print working directory. What else can be said about the good old pwd who has been printing the current directory name for ages.

14. cd command examples

Use “cd -” to toggle between the last two directories
Use “shopt -s cdspell” to automatically correct mistyped directory names on cd

15. gzip command examples
To create a *.gz compressed file:
$ gzip test.txt
To uncompress a *.gz file:
$ gzip -d test.txt.gz
Display compression ratio of the compressed file using gzip -l
$ gzip -l *.gz
         compressed        uncompressed  ratio uncompressed_name
              23709               97975  75.8% asp-patch-rpms.txt

16. bzip2 command examples

To create a *.bz2 compressed file:
$ bzip2 test.txt
To uncompress a *.bz2 file:
bzip2 -d test.txt.bz2

17. unzip command examples
To extract a *.zip compressed file:
$ unzip test.zip
View the contents of *.zip file (Without unzipping it):
$ unzip -l jasper.zip
Archive:  jasper.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
    40995  11-30-98 23:50   META-INF/MANIFEST.MF
    32169  08-25-98 21:07   classes_
    15964  08-25-98 21:07   classes_names
    10542  08-25-98 21:07   classes_ncomp

18. shutdown command examples

Shutdown the system and turn the power off immediately.
# shutdown -h now
Shutdown the system after 10 minutes.
# shutdown -h +10
Reboot the system using shutdown command.
# shutdown -r now
Force the filesystem check during reboot.
# shutdown -Fr now

19. ftp command examples

Both ftp and secure ftp (sftp) has similar commands. To connect to a remote server and download multiple files, do the following.
$ ftp IP/hostname
ftp> mget *.html
To view the file names located on the remote server before downloading, mls ftp command as shown below.
ftp> mls *.html -
/ftptest/features.html
/ftptest/index.html
/ftptest/othertools.html
/ftptest/samplereport.html
/ftptest/usage.html

20. crontab command examples
View crontab entry for a specific user
# crontab -u john -l
Schedule a cron job every 10 minutes.
*/10 * * * * /home/ramesh/check-disk-space

ore crontab examples: Linux Crontab: 15 Awesome Cron Job Examples

21. service command examples

Service command is used to run the system V init scripts. i.e Instead of calling the scripts located in the /etc/init.d/ directory with their full path, you can use the service command.
Check the status of a service:
# service ssh status
Check the status of all the services.
service --status-all
Restart a service.
# service ssh restart

22. ps command examples

ps command is used to display information about the processes that are running in the system.
While there are lot of arguments that could be passed to a ps command, following are some of the common ones.
To view current running processes.
$ ps -ef | more
To view current running processes in a tree structure. H option stands for process hierarchy.
$ ps -efH | more

23. free command examples

This command is used to display the free, used, swap memory available in the system.
Typical free command output. The output is displayed in bytes.
$ free
             total       used       free     shared    buffers     cached
Mem:       3566408    1580220    1986188          0     203988     902960
-/+ buffers/cache:     473272    3093136
Swap:      4000176          0    4000176
If you want to quickly check how many GB of RAM your system has use the -g option. -b option displays in bytes, -k in kilo bytes, -m in mega bytes.
$ free -g
             total       used       free     shared    buffers     cached
Mem:             3          1          1          0          0          0
-/+ buffers/cache:          0          2
Swap:            3          0          3
If you want to see a total memory ( including the swap), use the -t switch, which will display a total line as shown below.
ramesh@ramesh-laptop:~$ free -t
             total       used       free     shared    buffers     cached
Mem:       3566408    1592148    1974260          0     204260     912556
-/+ buffers/cache:     475332    3091076
Swap:      4000176          0    4000176
Total:     7566584    1592148    5974436

24. top command examples

top command displays the top processes in the system ( by default sorted by cpu usage ). To sort top output by any column, Press O (upper-case O) , which will display all the possible columns that you can sort by as shown below.
Current Sort Field:  P  for window 1:Def
Select sort field via field letter, type any other key to return

  a: PID        = Process Id              v: nDRT       = Dirty Pages count
  d: UID        = User Id                 y: WCHAN      = Sleeping in Function
  e: USER       = User Name               z: Flags      = Task Flags
  ........
To displays only the processes that belong to a particular user use -u option. The following will show only the top processes that belongs to oracle user.
$ top -u oracle

25. df command examples
Displays the file system disk space usage. By default df -k displays output in bytes.
$ df -k
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1             29530400   3233104  24797232  12% /
/dev/sda2            120367992  50171596  64082060  44% /home
df -h displays output in human readable form. i.e size will be displayed in GB’s.
ramesh@ramesh-laptop:~$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              29G  3.1G   24G  12% /
/dev/sda2             115G   48G   62G  44% /home
Use -T option to display what type of file system.
ramesh@ramesh-laptop:~$ df -T
Filesystem    Type   1K-blocks      Used Available Use% Mounted on
/dev/sda1     ext4    29530400   3233120  24797216  12% /
/dev/sda2     ext4   120367992  50171596  64082060  44% /home

26. kill command examples

Use kill command to terminate a process. First get the process id using ps -ef command, then use kill -9 to kill the running Linux process as shown below. You can also use killall, pkill, xkill to terminate a unix process.
$ ps -ef | grep vim
ramesh    7243  7222  9 22:43 pts/2    00:00:00 vim

$ kill -9 7243

27. rm command examples
Get confirmation before removing the file.
$ rm -i filename.txt
It is very useful while giving shell metacharacters in the file name argument.
Print the filename and get confirmation before removing the file.
$ rm -i file*
Following example recursively removes all files and directories under the example directory. This also removes the example directory itself.
$ rm -r example

28. cp command examples

Copy file1 to file2 preserving the mode, ownership and timestamp.
$ cp -p file1 file2
Copy file1 to file2. if file2 exists prompt for confirmation before overwritting it.
$ cp -i file1 file2

29. mv command examples

Rename file1 to file2. if file2 exists prompt for confirmation before overwritting it.
$ mv -i file1 file2
Note: mv -f is just the opposite, which will overwrite file2 without prompting.
mv -v will print what is happening during file rename, which is useful while specifying shell metacharacters in the file name argument.
$ mv -v file1 file2

30. cat command examples

You can view multiple files at the same time. Following example prints the content of file1 followed by file2 to stdout.
$ cat file1 file2
While displaying the file, following cat -n command will prepend the line number to each line of the output.
$ cat -n /etc/logrotate.conf
    1 /var/log/btmp {
    2     missingok
    3     monthly
    4     create 0660 root utmp
    5     rotate 1
    6 }

31. mount command examples

To mount a file system, you should first create a directory and mount it as shown below.
# mkdir /u01

# mount /dev/sdb1 /u01
You can also add this to the fstab for automatic mounting. i.e Anytime system is restarted, the filesystem will be mounted.
/dev/sdb1 /u01 ext2 defaults 0 2

32. chmod command examples

chmod command is used to change the permissions for a file or directory.
Give full access to user and group (i.e read, write and execute ) on a specific file.
$ chmod ug+rwx file.txt
Revoke all access for the group (i.e read, write and execute ) on a specific file.
$ chmod g-rwx file.txt
Apply the file permissions recursively to all the files in the sub-directories.
$ chmod -R ug+rwx file.txt

33. chown command examples
chown command is used to change the owner and group of a file. \
To change owner to oracle and group to db on a file. i.e Change both owner and group at the same time.
$ chown oracle:dba dbora.sh
Use -R to change the ownership recursively.
$ chown -R oracle:dba /home/oracle

34. passwd command examples

Change your password from command line using passwd. This will prompt for the old password followed by the new password.
$ passwd
Super user can use passwd command to reset others password. This will not prompt for current password of the user.
# passwd USERNAME
Remove password for a specific user. Root user can disable password for a specific user. Once the password is disabled, the user can login without entering the password.
# passwd -d USERNAME

35. mkdir command examples

Following example creates a directory called temp under your home directory.
$ mkdir ~/temp
Create nested directories using one mkdir command. If any of these directories exist already, it will not display any error. If any of these directories doesn’t exist, it will create them.
$ mkdir -p dir1/dir2/dir3/dir4/

36. ifconfig command examples

Use ifconfig command to view or configure a network interface on the Linux system.
View all the interfaces along with status.
$ ifconfig -a
Start or stop a specific interface using up and down command as shown below.
$ ifconfig eth0 up

$ ifconfig eth0 down

37. uname command examples
Uname command displays important information about the system such as — Kernel name, Host name, Kernel release number,
Processor type, etc.,
Sample uname output from a Ubuntu laptop is shown below.
$ uname -a
Linux john-laptop 2.6.32-24-generic #41-Ubuntu SMP Thu Aug 19 01:12:52 UTC 2010 i686 GNU/Linux

38. whereis command examples

When you want to find out where a specific Unix command exists (for example, where does ls command exists?), you can execute the following command.
$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
When you want to search an executable from a path other than the whereis default path, you can use -B option and give path as argument to it. This searches for the executable lsmk in the /tmp directory, and displays it, if it is available.
$ whereis -u -B /tmp -f lsmk
lsmk: /tmp/lsmk

39. whatis command examples

Whatis command displays a single line description about a command.
$ whatis ls
ls  (1)  - list directory contents

$ whatis ifconfig
ifconfig (8)         - configure a network interface

40. locate command examples

Using locate command you can quickly search for the location of a specific file (or group of files). Locate command uses the database created by updatedb.
The example below shows all files in the system that contains the word crontab in it.
$ locate crontab
/etc/anacrontab
/etc/crontab
/usr/bin/crontab
/usr/share/doc/cron/examples/crontab2english.pl.gz
/usr/share/man/man1/crontab.1.gz
/usr/share/man/man5/anacrontab.5.gz
/usr/share/man/man5/crontab.5.gz
/usr/share/vim/vim72/syntax/crontab.vim

41. man command examples

Display the man page of a specific command.
$ man crontab
When a man page for a command is located under more than one section, you can view the man page for that command from a specific section as shown below.
$ man SECTION-NUMBER commandname
Following 8 sections are available in the man page.
  1. General commands
  2. System calls
  3. C library functions
  4. Special files (usually devices, those found in /dev) and drivers
  5. File formats and conventions
  6. Games and screensavers
  7. Miscellaneous
  8. System administration commands and daemons
For example, when you do whatis crontab, you’ll notice that crontab has two man pages (section 1 and section 5). To view section 5 of crontab man page, do the following.
$ whatis crontab
crontab (1)          - maintain crontab files for individual users (V3)
crontab (5)          - tables for driving cron

$ man 5 crontab

42. tail command examples

Print the last 10 lines of a file by default.
$ tail filename.txt
Print N number of lines from the file named filename.txt
$ tail -n N filename.txt
View the content of the file in real time using tail -f. This is useful to view the log files, that keeps growing. The command can be terminated using CTRL-C.
$ tail -f log-file


43. less command examples
less is very efficient while viewing huge log files, as it doesn’t need to load the full file while opening.
$ less huge-log-file.log
One you open a file using less command, following two keys are very helpful.
CTRL+F – forward one window
CTRL+B – backward one window


44. su command examples
Switch to a different user account using su command. Super user can switch to any other user without entering their password.
$ su - USERNAME
Execute a single command from a different account name. In the following example, john can execute the ls command as raj username. Once the command is executed, it will come back to john’s account.
[john@dev-server]$ su - raj -c 'ls'

[john@dev-server]$
Login to a specified user account, and execute the specified shell instead of the default shell.
$ su -s 'SHELLNAME' USERNAME

45. mysql command examples

mysql is probably the most widely used open source database on Linux. Even if you don’t run a mysql database on your server, you might end-up using the mysql command ( client ) to connect to a mysql database running on the remote server.
To connect to a remote mysql database. This will prompt for a password.
$ mysql -u root -p -h 192.168.1.2
To connect to a local mysql database.
$ mysql -u root -p
If you want to specify the mysql root password in the command line itself, enter it immediately after -p (without any space).

46. yum command examples

To install apache using yum.
$ yum install httpd
To upgrade apache using yum.
$ yum update httpd
To uninstall/remove apache using yum.
$ yum remove httpd

47. rpm command examples

To install apache using rpm.
# rpm -ivh httpd-2.2.3-22.0.1.el5.i386.rpm
To upgrade apache using rpm.
# rpm -uvh httpd-2.2.3-22.0.1.el5.i386.rpm
To uninstall/remove apache using rpm.
# rpm -ev httpd

48. ping command examples

Ping a remote host by sending only 5 packets.
$ ping -c 5 gmail.com

49. date command examples

Set the system date:
# date -s "01/31/2010 23:59:53"
Once you’ve changed the system date, you should syncronize the hardware clock with the system date as shown below.
# hwclock –systohc

# hwclock --systohc –utc

50. wget command examples

The quick and effective method to download software, music, video from internet is using wget command.
$ wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz
Download and store it with a different name.
$ wget -O taglist.zip http://www.vim.org/scripts/download_script.php?src_id=7701

Known NFS Issues in Linux

Known Issues and Workarounds:
RHEL 5.x
  1. Issue 1:
    RHEL5.8: Mount with sec=krb5 displays warning as 'rpc.idmapd/rpc.gssd appears not to be running'.
    Description: Mount displays an error as 'rpc.idmapd/rpc.gssd appears not to be running' though they are running, hence nfs-kerberos fails when it is mounted with sec=krb5.
    Workaround: Update the nfs-utils package to version 1.0.9-66.el5 or later. For more information, see [RHEL5.8] NFSv4 'mount' command produces warnings about RPC services not running.
  2. Issue 2:
    RHEL5.9: chmod, chgrp, and chown commands fail on NFSv3 mounts.
    Workaround: Traverse to the mounted path and then use  chmod, chgrp, and chown commands to work.
  3. Issue 3:
    RHEL 5.10 and RHEL 5.11 : NLM clients can fail to recover locks with pending UNLOCK operations.
    Description: When clients perform NLM lock recovery, RHEL 5.10 and RHEL 5.11 NLM clients can get into a state where they stop issuing pending UNLOCK operations and only reclaim existing locks, leaving the locks with pending UNLOCK operations not unlocked and not reclaimed.
    Workaround: No workaround available in RHEL5. Upgrade to RHEL 6.6 GA clients which properly handle pending UNLOCK operations when performing lock recovery and all locks are cleaned up after recovery.
  4. Issue 4:
    RHEL 5.11:"EIO (errno=5)" hit on RHEL5U11 NFSv3 mounts during Storage Faiover operations.
    Description: RHEL 5.11 does not have the latest SUNRPC layer fixes which results client EIO errors.
    Workaround: No workaround available in RHEL5. Upgrade the client to latest RHEL kernels like RHEL 6.6 GA which addresses this problem and has latest fixes. 
  5. Issue 5:
    RHEL 5.11: "ERROR: No locks available (errno=37)" hit on NFSv3 mounts during Storage Failover operations
    Description: RHEL 5.11 does not have the latest SUNRPC layer fixes which results "error # 37" failure.
    Workaround: No workaround available in RHEL5. Upgrade the client to latest RHEL kernels like RHEL 6.6 GA which addresses this problem and has latest fixes. 
  6. Issue 6:
    The following error messages are reported:
    • /var/log/messages:Aug 16 19:08:17 uscf1plat0 kernel: NFS: v4 server 10.113.49.8 returned a bad sequence-id error!

      grep sequence-id messages.1
      Aug 16 19:08:17 uscf1plat0 kernel: NFS: v4 server 10.113.49.8 returned a bad sequence-id error!
      ...
      Aug 22 01:09:27 uscf1plat0 kernel: NFS: v4 server 10.113.49.8 returned a bad sequence-id error!
    • No logs matching on the storage system
    • Checked pktt: After the client goes into a loop for write call, the storage system returns NFS4ERR_BAD_STATEID
    Workaround: For more information, see Red Hat BUG 620502 - [NetApp 5.6 bug] RHEL NFS clients disconnect from NetApp NFSv4 shares with: v4 server returned a bad sequence-id error!
RHEL 6.x
  1. Issue 1:
    RHEL6.x: nfs-kerberos fails with a permission denied error if allow_weak_crypto is disabled.
    Description: With kinit enabled on these clients, nfs-kerberos authentication will fail and displays kinit: No supported encryption types (config file error?).
    Workaround: Add allow_weak_crypto=yes in /etc/krb5/krb5.conf to get that nfs-kerberos working.
     
  2. Issue 2:
    RHEL6.3/6.4: chmodchgrp, and chown commands fail on NFSv3 mounts.
    Workaround: Traverse to the mounted path and then use  chmodchgrp, and chown commands to work.
     
  3. Issue 3:
    RHEL 6.2/6.3 : Applications running on Redhat 6.2/6.3 clients might not be able to stop writing to files to a NFS mount point. Pressing 'Ctrl C' will not help. RHEL6 (> kernl 2.6.25) and later mount options intr/nointr options have been deprecated. Applications that are hung should be killed using SIGKILL to interrupt.
    Workaround: Use the command kill -9 pid to kill the process.
     
  4. Issue 4:
    RHEL6.3/6.4: The NFSv3 process occasionally exits with 'No locks available' when NFS server crashes.
    Description: NLM is NOT retrying waiting byte lock request after the server crashes and just fails the application with -ENOLCK error.
    Workaround: Upgrade to RHEL 6.3 Errata Kernel (2.6.32-279.46.1.el6). 6.4 Errata Kernel (2.6.32-358.49.1.el6).
     
  5. Issue 5:
    RHEL 6.1/6.2/6.3:
    Description: Application running on RHEL 6.1/6.2/6.3 clients might hang with the BAD_STATID error while NFS v4.0 delegations are enabled
    Workaround: Upgrade to RHEL 6.3 Errata Kernel (2.6.32-279.46.1.el6).
     
  6. Issue 6:
    RHEL 6.4: NFS v4.0 referral directories missing from directory listing on RHEL 6.4 clients.
    Description: RHEL 6.4 client getting lookups for all the volumes but its listing only the volumes of one of the HA storage nodes. Tracked with RedHat found at https://bugzilla.redhat.com/show_bug.cgi?id=963337
     
  7. Issue 7:
    RHEL 6.4: Mount on RHEL 6.4 client succeeds with sec=krb5 though the volume has export ro rule set as sys.
    Description: RHEL 6.4 client switches its security flavor from 'RPCSEC_GSS' to 'AUTH_UNIX' during mount and hence, the mount gets succeeded while mounting with sec=krb5, though the volume has exported with the ro rule set as sys. Similarly, export with ro rule as sys and mount with krb5 also succeeds, since the client switches the security flavor based on the SECINFO reply; but this seems to be a security gap. Tracked with RedHat found at https://bugzilla.redhat.com/show_bug.cgi?id=948145
     
  8. Issue 8:
    RHEL 6.4: NFS v4.0 state recovery can deadlock with permission checking on delegated OPEN.
    Description: The state manager can deadlock on an open owner's nfsv4 sequence ID if you happen to be recovering OPEN state while the client is also checking open permission for a delegated OPEN.
    Workaround: Upgrade to RHEL 6.4 Errata Kernel (2.6.32-358.49.1.el6.x86_64).
     
  9. Issue 9:
    RHEL 6.4: NFSv4.0: Fix handling of revoked delegations by setattr.
    Description_nfs4_do_setattr() will use the delegation stateid if no writeable open file stateid is available. If the server revokes that delegation stateid, then the call to nfs4_handle_exception() will fail to handle the error due to the lack of a struct nfs4_state, and will just convert the error into an EIO.
    Workaround: Upgrade the client to Rhel 6.4 Errata Kernel (2.6.32-358.49.1.el6.x86_64) and above.
   
  • Issue 11:
    RHEL6.x: CDOT: Circular directory structure warning when running chown -R on junctioned volumes via NFSv4. This is being tracked at https://bugzilla.redhat.com/show_bug.cgi?id=1223978.
    Description: when you have a multilevel junction path (/vol1/vol2/vol3), mounted, you might see a recursive directory structure warning while doing setattr or chmod.
    Workaround: upgrade to latest kernel of 6.7.
 
RHEL 7.x
Best Practices:
See TR-4067 for 'Clustered Data ONTAP NFS Best Practice and Implementation Guide'.


  • Issue 12:
    RHEL6.x:  "MIT krb5 gss_accept_sec_context() implementation used by SECD rejects an incoming token if the ticket end-time expires before the server current time, with no Kerberos clock skew consideration.This is being tracked at http://krbdev.mit.edu/rt/Ticket/Display.html?id=8268
    Description: "If the check triggers, gss_accept_sec_context() returns GSS_S_CREDENTIALS_EXPIRED on GSS context refresh which is incorrect as itsuggests that the NFS client's verifier_cred_handle has expired.
    The check fails and ""Permission Denied"" is returned to the application when:
    1) The server clock is ahead of the client clock, but within the server's configured Kerberos clock skew.
    2) The maximum service ticket lifetime is less than the maximum TGT lifetime.
    3) The NFS client is sending requests on a Kerberos share during the ""clock skew window"" when the client service ticket is good according to the client clock, and expired according to the server clock.
    This results in a ""Permissions Denied"" error being returned to the application during the clock skew window. In this case, the server will reject the NFS request with an AUTH_ERROR which triggers a GSS context refresh attempt on the client. The client will use the existing server service ticket for the refresh which is rejected by the server with the GSS_S_CREDENTIALS_EXPIRED error. Note that if the service ticket is refreshable, this situation heals itself once the service ticket expires on the client, forcing the client to refresh the service ticket." Workaround: The workaround is to configure the KDC maximum service ticket lifetime to be equal to the TGT maximum lifetime. In this case, the client when asked to refresh the service ticket, it first must refresh the TGT which on the Linux NFS client takes the Kerberos clock skew into consideration.
  • Issue 13:
    RHEL6.3,6.4,6.5:  RHEL NFS client hangs and I/O outages with older RHEL 6 kernels and NFSv4.1 (BURT:814789).
    Description: When using NFSv4.1 with Red Hat Enterprise Linux (RHEL) NFS clients running olderRHEL 6 kernels such as RHEL 6.4 and RHEL 6.3, you might encounter client issues such as hangs & long I/O outages when performing NetApp-specific controller tasks such as LIF migrations, that require clients to recover state under heavy workloads.
    Workaround: The above issues have been addressed in the latest RHEL 6.5.z errata packages described below:
    1) kernel-2.6.32-431.29.2.el6
    2) nfs-utils-1.2.3-39.el6_5.3
    3) libtirpc-0.2.1-6.el6_5.2
     To avoid these issues, it is recommended to upgrade clients to these respective RHEL 6.5.z errata packages.
  • Issue 14:
    RHEL 6.4,6.5,6.5z:  RHEL NFS client I/O outages and errors (BURT:866544).
    Description: I/O outages and errors are observed on Red Hat Enterprise Linux (RHEL) NFS clients running older kernels such as RHEL 6.5. This is seen for all NFS versions including NFSv3, v4.0 & v4.1.
    Workaround: The above issues have now been addressed in the RHEL 6.6 GA kernel-2.6.32-504.el6. It is recommended to upgrade clients to RHEL 6.6 GA to obtain these fixes and avoid encountering the above issues.
    1. Issue 1:
      RHEL7.0x: RHEL 7.0 NFS client issues & fixes (BURT:915798)
      Description:NFS client I/O errors, kernel crashes, permission-denied errors on Kerberized mount points and an intermittent data corruption issue have been observed on older Red Hat Enterprise Linux (RHEL) kernels such as RHEL 7.0. This is seen across all NFS versions including NFS v3, v4.0 & v4.1.
      Workaround:The above issues are now addressed in the RHEL 7.1 GA kernel-3.10.0-229.el7. Upgrade clients to this RHEL 7.1 GA release to access these fixes and thereby avoid encountering the above issues.
       
    2. Issue 2:
      RHEL7.x: on pNFS mounts umount does not destroy session to DS after getting EIO error. This is being tracked at https://bugzilla.redhat.com/show_bug.cgi?id=1234986.
      Description:On RHEL 7.x clients, if you are mounted using pNFS, and you do an umount command, the client might fail to delete session with the server.
      Workaround: upgrade to latest kernel version of 7.1z or 7.2 ( > kernel 3.10.0-320 ) .
    3. Issue 3: RHEL7.x:  Received EIO because of ADMIN_REVOKED on the SETATTR on Rhel7.1 with pNFS . This is being tracked at https://bugzilla.redhat.com/show_bug.cgi?id=1214410
      Description: On RHEL 7.x clients, if you are mounted using pNFS and the server has to go through a failover (takeover/giveback), you might see EIO error ( error=5)
      Workaround: upgrade to latest kernel version of 7.1z or 7.2 ( > kernel 3.10.0-289)
    4. Issue 4:
      RHEL7.1: NFS IO for vols on disaster side cluster intermittently fails post MCC SO. This is being tracked at https://bugzilla.redhat.com/show_bug.cgi?id=1240790
      Description: On RHEL 7.x client, if you are mounted using NFSv4 and the storage system has to go through a Metrocluster switchover, you might see and EIO (error=5).
      Workaround:upgrade to latest kernel version of 7.1z or 7.2 ( > kernel 3.10.0-295)
    5. Issue 5:
      RHEL7.x: CDOT: Circular directory structure warning when running chown -R on junctioned volumes via NFSv4. This is being tracked at https://bugzilla.redhat.com/show_bug.cgi?id=1225090.
      Description: when you have a multilevel junction path (/vol1/vol2/vol3), mounted, you might see a recursive directory structure warning while doing setattr or chmod.
      Workaround: upgrade to latest kernel of 7.1
    6. Issue 6:
      RHEL7.1: NFSv4 sharelocks not cleared after closing files following LIF migrate with new RHEL7.1 clients.This is being tracked at https://bugzilla.redhat.com/show_bug.cgi?id=1263376
      Description: If there are files open and Locks on an NFSv4.x mount and a server goes through a LIF migrate or an SFO, you might see locks not being cleared. this might lead data corruption.
      Workaround: use kernel verion 3.10.0-320 or above
    7. Issue 7:
      RHEL6.x/7.x:  "MIT krb5 gss_accept_sec_context() implementation used by SECD rejects an incoming token if the ticket end-time expires before the server current time, with no Kerberos clock skew consideration.This is being tracked at http://krbdev.mit.edu/rt/Ticket/Display.html?id=8268
      Description: "If the check triggers, gss_accept_sec_context() returns GSS_S_CREDENTIALS_EXPIRED on GSS context refresh which is incorrect as itsuggests that the NFS client's verifier_cred_handle has expired.
      The check fails and ""Permission Denied"" is returned to the application when:
      1) The server clock is ahead of the client clock, but within the server's configured Kerberos clock skew.
      2) The maximum service ticket lifetime is less than the maximum TGT lifetime.
      3) The NFS client is sending requests on a Kerberos share during the ""clock skew window"" when the client service ticket is good according to the client clock, and expired according to the server clock.
      This results in a ""Permissions Denied"" error being returned to the application during the clock skew window. In this case, the server will reject the NFS request with an AUTH_ERROR which triggers a GSS context refresh attempt on the client. The client will use the existing server service ticket for the refresh which is rejected by the server with the GSS_S_CREDENTIALS_EXPIRED error. Note that if the service ticket is refreshable, this situation heals itself once the service ticket expires on the client, forcing the client to refresh the service ticket."
      Workaround: The workaround is to configure the KDC maximum service ticket lifetime to be equal to the TGT maximum lifetime. In this case, the client when asked to refresh the service ticket, it first must refresh the TGT which on the Linux NFS client takes the Kerberos clock skew into consideration.

Linux NFS troubleshoot

This is intended as a step-by-step guide to what to do when things go wrong using NFS. Usually trouble first rears its head on the client end, so this diagnostic will begin there.

7.1. Unable to See Files on a Mounted File System

First, check to see if the file system is actually mounted. There are several ways of doing this. The most reliable way is to look at the file /proc/mounts, which will list all mounted filesystems and give details about them. If this doesn't work (for example if you don't have the /proc filesystem compiled into your kernel), you can type mount -f although you get less information.
If the file system appears to be mounted, then you may have mounted another file system on top of it (in which case you should unmount and remount both volumes), or you may have exported the file system on the server before you mounted it there, in which case NFS is exporting the underlying mount point (if so then you need to restart NFS on the server).
If the file system is not mounted, then attempt to mount it. If this does not work, see Symptom 3.

7.2. File requests hang or timeout waiting for access to the file

This usually means that the client is unable to communicate with the server. See Symptom 3 letter b.

7.3. Unable to mount a file system

There are two common errors that mount produces when it is unable to mount a volume. These are:
  1. failed, reason given by server: Permission denied This means that the server does not recognize that you have access to the volume.
    • Check your /etc/exports file and make sure that the volume is exported and that your client has the right kind of access to it. For example, if a client only has read access then you have to mount the volume with the ro option rather than the rw option.
    • Make sure that you have told NFS to register any changes you made to /etc/exports since starting nfsd by running the exportfs command. Be sure to type exportfs -ra to be extra certain that the exports are being re-read.
    • Check the file /proc/fs/nfs/exports and make sure the volume and client are listed correctly. (You can also look at the file /var/lib/nfs/xtab for an unabridged list of how all the active export options are set.) If they are not, then you have not re-exported properly. If they are listed, make sure the server recognizes your client as being the machine you think it is. For example, you may have an old listing for the client in /etc/hosts that is throwing off the server, or you may not have listed the client's complete address and it may be resolving to a machine in a different domain. One trick is login to the server from the client via ssh or telnet; if you then type who, one of the listings should be your login session and the name of your client machine as the server sees it. Try using this machine name in your /etc/exports entry. Finally, try to ping the client from the server, and try to ping the server from the client. If this doesn't work, or if there is packet loss, you may have lower-level network problems.
    • It is not possible to export both a directory and its child (for example both /usr and /usr/local). You should export the parent directory with the necessary permissions, and all of its subdirectories can then be mounted with those same permissions.
  2. RPC: Program Not Registered (or another "RPC" error) This means that the client does not detect NFS running on the server. This could be for several reasons.
    • First, check that NFS actually is running on the server by typing rpcinfo -p on the server. You should see something like this:
      program vers proto   port
         100000    2   tcp    111  portmapper
         100000    2   udp    111  portmapper
         100011    1   udp    749  rquotad
         100011    2   udp    749  rquotad
         100005    1   udp    759  mountd
         100005    1   tcp    761  mountd
         100005    2   udp    764  mountd
         100005    2   tcp    766  mountd
         100005    3   udp    769  mountd
         100005    3   tcp    771  mountd
         100003    2   udp   2049  nfs
         100003    3   udp   2049  nfs
         300019    1   tcp    830  amd
         300019    1   udp    831  amd
         100024    1   udp    944  status
         100024    1   tcp    946  status
         100021    1   udp   1042  nlockmgr
         100021    3   udp   1042  nlockmgr
         100021    4   udp   1042  nlockmgr
         100021    1   tcp   1629  nlockmgr
         100021    3   tcp   1629  nlockmgr
         100021    4   tcp   1629  nlockmgr
      
      This says that we have NFS versions 2 and 3, rpc.statd version 1, network lock manager (the service name for rpc.lockd) versions 1, 3, and 4. There are also different service listings depending on whether NFS is travelling over TCP or UDP. UDP is usually (but not always) the default unless TCP is explicitly requested. If you do not see at least portmapper, nfs, and mountd, then you need to restart NFS. If you are not able to restart successfully, proceed to Symptom 9.
    • Now check to make sure you can see it from the client. On the client, type rpcinfo -pserver where server is the DNS name or IP address of your server. If you get a listing, then make sure that the type of mount you are trying to perform is supported. For example, if you are trying to mount using Version 3 NFS, make sure Version 3 is listed; if you are trying to mount using NFS over TCP, make sure that is registered. (Some non-Linux clients default to TCP). Type man rpcinfo for more details on how to read the output. If the type of mount you are trying to perform is not listed, try a different type of mount.
      If you get the error No Remote Programs Registered, then you need to check your /etc/hosts.allow and /etc/hosts.deny files on the server and make sure your client actually is allowed access. Again, if the entries appear correct, check /etc/hosts (or your DNS server) and make sure that the machine is listed correctly, and make sure you can ping the server from the client. Also check the error logs on the system for helpful messages: Authentication errors from bad /etc/hosts.allow entries will usually appear in /var/log/messages, but may appear somewhere else depending on how your system logs are set up. The man pages for syslog can help you figure out how your logs are set up. Finally, some older operating systems may behave badly when routes between the two machines are asymmetric. Try typing tracepath [server] from the client and see if the word "asymmetric" shows up anywhere in the output. If it does then this may be causing packet loss. However asymmetric routes are not usually a problem on recent linux distributions.
      If you get the error Remote system error - No route to host, but you can ping the server correctly, then you are the victim of an overzealous firewall. Check any firewalls that may be set up, either on the server or on any routers in between the client and the server. Look at the man pages for ipchains, netfilter, and ipfwadm, as well as the IPChains-HOWTO and the Firewall-HOWTO for help.

7.4. I do not have permission to access files on the mounted volume

This could be one of two problems. If it is a write permission problem, check the export options on the server by looking at /proc/fs/nfs/exports and make sure the filesystem is not exported read-only. If it is you will need to re-export it read/write (don't forget to run exportfs -ra after editing /etc/exports). Also, check /proc/mounts and make sure the volume is mounted read/write (although if it is mounted read-only you ought to get a more specific error message). If not then you need to re-mount with the rw option.
The second problem has to do with username mappings, and is different depending on whether you are trying to do this as root or as a non-root user.
If you are not root, then usernames may not be in sync on the client and the server. Type id [user] on both the client and the server and make sure they give the same UID number. If they don't then you are having problems with NIS, NIS+, rsync, or whatever system you use to sync usernames. Check group names to make sure that they match as well. Also, make sure you are not exporting with the all_squash option. If the user names match then the user has a more general permissions problem unrelated to NFS.
If you are root, then you are probably not exporting with the no_root_squash option; check /proc/fs/nfs/exports or /var/lib/nfs/xtab on the server and make sure the option is listed. In general, being able to write to the NFS server as root is a bad idea unless you have an urgent need -- which is why Linux NFS prevents it by default. See Section 6, “Security and NFS” for details.
If you have root squashing, you want to keep it, and you're only trying to get root to have the same permissions on the file that the user nobody should have, then remember that it is the server that determines which uid root gets mapped to. By default, the server uses the UID and GID of nobody in the /etc/passwd file, but this can also be overridden with the anonuid and anongid options in the /etc/exports file. Make sure that the client and the server agree about which UID nobody gets mapped to.

7.5. When I transfer really big files, NFS takes over all the CPU cycles on the server and it screeches to a halt

This is a problem with the fsync() function in 2.2 kernels that causes all sync-to-disk requests to be cumulative, resulting in a write time that is quadratic in the file size. If you can, upgrading to a 2.4 kernel should solve the problem. Also, exporting with the no_wdelay option forces the program to use o_sync() instead, which may prove faster.

7.6. Strange error or log messages

  1. Messages of the following format:
    Jan 7 09:15:29 server kernel: fh_verify: mail/guest permission failure, acc=4, error=13
    Jan 7 09:23:51 server kernel: fh_verify: ekonomi/test permission failure, acc=4, error=13
    
    These happen when a NFS setattr operation is attempted on a file you don't have write access to. The messages are harmless.
  2. The following messages frequently appear in the logs:
    kernel: nfs: server server.domain.name not responding, still trying
    kernel: nfs: task 10754 can't get a request slot
    kernel: nfs: server server.domain.name OK
    
    The "can't get a request slot" message means that the client-side RPC code has detected a lot of timeouts (perhaps due to network congestion, perhaps due to an overloaded server), and is throttling back the number of concurrent outstanding requests in an attempt to lighten the load. The cause of these messages is basically sluggish performance. See Section 5, “Optimizing NFS Performance” for details.
  3. After mounting, the following message appears on the client:
    nfs warning: mount version older than kernel
    
    It means what it says: You should upgrade your mount package and/or am-utils. (If for some reason upgrading is a problem, you may be able to get away with just recompiling them so that the newer kernel features are recognized at compile time).
  4. Errors in startup/shutdown log for lockd
    nfslock: rpc.lockd startup failed
    
    They are harmless. Older versions of rpc.lockd needed to be started up manually, but newer versions are started automatically by nfsd. Many of the default startup scripts still try to start up lockd by hand, in case it is necessary. You can alter your startup scripts if you want the messages to go away.
  5. The following message appears in the logs:
    kmem_create: forcing size word alignment - nfs_fh
    
    This results from the file handle being 16 bits instead of a multiple of 32 bits, which makes the kernel grimace. It is harmless.

7.7. Real permissions don't match what's in /etc/exports

/etc/exports is very sensitive to whitespace - so the following statements are not the same:
/export/dir hostname(rw,no_root_squash) 
/export/dir hostname (rw,no_root_squash)
The first will grant hostname rw access to /export/dir without squashing root privileges. The second will grant hostname rw privileges with root squash and it will grant everyone else read/write access, without squashing root privileges. Nice huh?

7.8. Flaky and unreliable behavior

Simple commands such as ls work, but anything that transfers a large amount of information causes the mount point to lock.
This could be one of two problems:
  1. It will happen if you have ipchains on at the server and/or the client and you are not allowing fragmented packets through the chains. Allow fragments from the remote host and you'll be able to function again. See Section 5, “Optimizing NFS Performance” for details on how to do this.
  2. You may be using a larger rsize and wsize in your mount options than the server supports. Try reducing rsize and wsize to 1024 and seeing if the problem goes away. If it does, then increase them slowly to a more reasonable value.

7.9. nfsd won't start

Check the file /etc/exports and make sure root has read permission. Check the binaries and make sure they are executable. Make sure your kernel was compiled with NFS server support. You may need to reinstall your binaries if none of these ideas helps.

7.10. File Corruption When Using Multiple Clients

If a file has been modified within one second of its previous modification and left the same size, it will continue to generate the same inode number. Because of this, constant reads and writes to a file by multiple clients may cause file corruption. Fixing this bug requires changes deep within the filesystem layer, and therefore it is a 2.5 item.

RedhatLinux SAN Migration using LVM

What would be best way to do the SAN Migration in Redhat Linux LVM? Would the pvmove will work ?I would recommend you to go for LVM mirroring since its a safest method and zero downtime to perform this activity.First you need to add the new SAN LUNS in volume group and mirror the volume using those new disks.Once the synchronization is done,you can remove old LUNS from the volume group.

Assume the your diskgroup name is “lin” and volume name is lvol0(Mirrored) and /dev/sdb & /dev/sdc coming  from same storage.
[root@mylinz ~]# pvs -a -o +devices
PV VG Fmt Attr PSize PFree Devices
/dev/sda2 vg_mylinz lvm2 a- 19.51g 0 /dev/sda2(0)
/dev/sda2 vg_mylinz lvm2 a- 19.51g 0 /dev/sda2(4234)
/dev/sdb lin lvm2 a- 508.00m 508.00m
/dev/sdc lin lvm2 a- 508.00m 508.00m
/dev/sdd lin lvm2 a- 508.00m 508.00m
/dev/sde lvm2 a- 512.00m 512.00m

[root@mylinz ~]# vgs
VG #PV #LV #SN Attr VSize VFree
lin 3 0 0 wz--n- 1.49g 1.49g
vg_mylinz 1 2 0 wz--n- 19.51g 0
Assume lvol0 is mirrored and this volume used sdb & sdc from old SAN box.
[root@mylinz ~]# lvs -a -o +devices
LV VG Attr LSize Origin Snap% Move Log Copy% Convert Devices
lvol0 lin mwi-a- 52.00m 100.00 lvol0_mimage_0(0),lvol0_mimage_1(0)
[lvol0_mimage_0] lin iwi-ao 52.00m /dev/sdb(0)
[lvol0_mimage_1] lin iwi-ao 52.00m /dev/sdc(0)
lv_root vg_mylinz -wi-ao 16.54g /dev/sda2(0)
lv_swap vg_mylinz -wi-ao 2.97g /dev/sda2(4234)
Here I am removing one lun which is coming from old storage as removing /dev/sdb.
Assume Device:/dev/sdd from new storage.
[root@mylinz ~]# lvconvert -m0 --mirrorlog core /dev/lin/lvol0 /dev/sdb
Logical volume lvol0 converted.
[root@mylinz ~]# lvs -a -o +devices
LV VG Attr LSize Origin Snap% Move Log Copy% Convert Devices
lvol0 lin -wi-a- 52.00m /dev/sdc(0)
lv_root vg_mylinz -wi-ao 16.54g /dev/sda2(0)
lv_swap vg_mylinz -wi-ao 2.97g /dev/sda2(4234)
Here i am mirroring volume with new LUN which is coming from new SAN.
[root@mylinz ~]# lvconvert -m1 --mirrorlog core /dev/lin/lvol0 /dev/sdd
lin/lvol0: Converted: 100.0%
[root@mylinz ~]# lvs -a -o +devices
LV VG Attr LSize Origin Snap% Move Log Copy% Convert Devices
lvol0 lin mwi-a- 52.00m 100.00 lvol0_mimage_0(0),lvol0_mimage_1(0)
[lvol0_mimage_0] lin iwi-ao 52.00m /dev/sdc(0)----------------------------> Now we can remove this one.
[lvol0_mimage_1] lin iwi-ao 52.00m /dev/sdd(0)
lv_root vg_mylinz -wi-ao 16.54g /dev/sda2(0)
lv_swap vg_mylinz -wi-ao 2.97g /dev/sda2(4234)
Now we can remove the LUN which is coming from old SAN as removing /dev/sdc.
[root@mylinz ~]# lvconvert -m0 --mirrorlog core /dev/lin/lvol0 /dev/sdc
Logical volume lvol0 converted.
[root@mylinz ~]# lvs -a -o +devices
LV VG Attr LSize Origin Snap% Move Log Copy% Convert Devices
lvol0 lin -wi-a- 52.00m /dev/sdd(0)
lv_root vg_mylinz -wi-ao 16.54g /dev/sda2(0)
lv_swap vg_mylinz -wi-ao 2.97g /dev/sda2(4234)
Now your volume is completely migrated to new SAN.You may think this some what lengthy process but very safe. Repeat the same for other volumes.

Thank you for reading this article.
Please leave a comment if you have any doubt ,i will get back to you as soon as possible.

How to Install LVM on Linux and Disk Operations

LVM will be installed by default on Redhat Linux installation.But sometimes if you select the minimum package installation method,LVM will not be installed.So you need to install LVM package separately .Here we will see the LVM installation method and if its already installed how to verify it .Logical volume manager has two versions and those are 1.LVM-1 2.LVM-2 . In this tutorials, we are going  see about LVM2 which is default volume manager in Redhat Linux newer releases.
In later part we will see about disk operations on LVM. That will cover bringing the disk under LVM control,Replacing failed disk and Removing the disk from LVM.
1.1 Installation of LVM
1.Verify LVM is installed or not on the server.
[root@mylinz ~]#  rpm -qa |grep -i lvm
lvm2-libs-2.02.72-8.el6.x86_64
lvm2-2.02.72-8.el6.x86_64
[root@mylinz ~]#
 
2.Here LVM is already installed.If its not already installed,use yum to install LVM.
Here is the procedure to configure yum repo.
[root@mylinz ~]#  yum install lvm2*
Loaded plugins: refresh-packagekit, rhnplugin
This system is not registered with RHN.
RHN support will be disabled.
Setting up Install Process
Package lvm2-libs-2.02.72-8.el6.x86_64 already installed and latest version
Package lvm2-2.02.72-8.el6.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package lvm2-cluster.x86_64 0:2.02.72-8.el6 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================
 Package        Arch         Version        Repository             Size
==============================================================================
Installing:
lvm2-cluster   x86_64     2.02.72-8.el6   local-installation       304 k
Transaction Summary
==============================================================================
Install       1 Package(s)
Upgrade       0 Package(s)

Total download size: 304 k
Installed size: 580 k
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : lvm2-cluster-2.02.72-8.el6.x86_64                1/1
Installed:
  lvm2-cluster.x86_64 0:2.02.72-8.el6
Complete!
[root@mylinz ~]#
As per about output,LVM2 packages are already installed.But there is other package called lvm2-cluster is installed now.Its cluster logical volume manager.

3.Verify the installed LVM packages.
[root@mylinz ~]# rpm -qa |grep -i lvm
lvm2-libs-2.02.72-8.el6.x86_64
lvm2-cluster-2.02.72-8.el6.x86_64
lvm2-2.02.72-8.el6.x86_64
[root@mylinz ~]#
4.Check the LVM version.
[root@mylinz ~]# lvm version
  LVM version:     2.02.72(2) (2010-07-28)
  Library version: 1.02.53 (2010-07-28)
  Driver version:  4.17.0
[root@mylinz ~]#

5.Use “help” sub command to see the list of commands on LVM.
[root@mylinz ~]# lvm help
  Available lvm commands:
  Use 'lvm help ' for more information

  dumpconfig      Dump active configuration
  formats         List available metadata formats
  help            Display help for commands
  lvchange        Change the attributes of logical volume(s)
  lvconvert       Change logical volume layout
  lvcreate        Create a logical volume
  lvdisplay       Display information about a logical volume
  lvextend        Add space to a logical volume
  lvmchange       With the device mapper, this is obsolete and does nothing.
  lvmdiskscan     List devices that may be used as physical volumes
  lvmsadc         Collect activity data
  lvmsar          Create activity report
  lvreduce        Reduce the size of a logical volume
  lvremove        Remove logical volume(s) from the system
  lvrename        Rename a logical volume
  lvresize        Resize a logical volume
  lvs             Display information about logical volumes
  lvscan          List all logical volumes in all volume groups
  pvchange        Change attributes of physical volume(s)
  pvresize        Resize physical volume(s)
  pvck            Check the consistency of physical volume(s)
  pvcreate        Initialize physical volume(s) for use by LVM
  pvdata          Display the on-disk metadata for physical volume(s)
  pvdisplay       Display various attributes of physical volume(s)
  pvmove          Move extents from one physical volume to another
  pvremove        Remove LVM label(s) from physical volume(s)
  pvs             Display information about physical volumes
  pvscan          List all physical volumes
  segtypes        List available segment types
  vgcfgbackup     Backup volume group configuration(s)
  vgcfgrestore    Restore volume group configuration
  vgchange        Change volume group attributes
  vgck            Check the consistency of volume group(s)
  vgconvert       Change volume group metadata format
  vgcreate        Create a volume group
  vgdisplay       Display volume group information
  vgexport        Unregister volume group(s) from the system
  vgextend        Add physical volumes to a volume group
  vgimport        Register exported volume group with system
  vgmerge         Merge volume groups
  vgmknodes       Create the special files for volume group devices in /dev
  vgreduce        Remove physical volume(s) from a volume group
  vgremove        Remove volume group(s)
  vgrename        Rename a volume group
  vgs             Display information about volume groups
  vgscan          Search for all volume groups
  vgsplit         Move physical volumes into a new or existing volume group
  version         Display software and driver version information
[root@mylinz ~]#

1.2 LVM DISK OPERATIONS
 If you want to use LVM, then you have to bring the disks in to LVM control using “pvcreate” command. 
1.List the physical disks.
[root@mylinz ~]#
[root@mylinz ~]# fdisk -l |grep /dev/ |grep -v dm
Disk /dev/sdd doesn't contain a valid partition table
Disk /dev/sde doesn't contain a valid partition table
Disk /dev/sdf doesn't contain a valid partition table
Disk /dev/sdg doesn't contain a valid partition table
Disk /dev/dm-0 doesn't contain a valid partition table
Disk /dev/dm-1 doesn't contain a valid partition table
Disk /dev/sda: 21.5 GB, 21474836480 bytes
/dev/sda1   *           1          64      512000   83  Linux
/dev/sda2              64        2611    20458496   8e  Linux LVM
Disk /dev/sdb: 536 MB, 536870912 bytes
/dev/sdb1               1         512      524272   8e  Linux LVM
Disk /dev/sdc: 536 MB, 536870912 bytes
/dev/sdc1               1         512      524272   8e  Linux LVM
Disk /dev/sdd: 536 MB, 536870912 bytes
Disk /dev/sde: 536 MB, 536870912 bytes
Disk /dev/sdf: 5368 MB, 5368709120 bytes
Disk /dev/sdg: 106 MB, 106954752 bytes
[root@mylinz ~]#

2.Use “fdisk” to label disks with LVM flags.
[root@mylinz ~]# fdisk /dev/sdd
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x57899b26.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
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-512, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-512, default 512):
Using default value 512

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

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@mylinz ~]#

3.Verify disk label in fdisk.
[root@mylinz ~]# fdisk -l /dev/sdd

Disk /dev/sdd: 536 MB, 536870912 bytes
64 heads, 32 sectors/track, 512 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x57899b26

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1               1         512      524272   8e  Linux LVM
[root@mylinz ~]#

4.Use pvcreate command to bring the disk under LVM control.
[root@mylinz ~]# pvcreate /dev/sdd1
  Physical volume "/dev/sdd1" successfully created
[root@mylinz ~]#

5.If you are getting any error while bringing the disk to LVM control,use force flag.
[root@mylinz ~]# pvcreate -f /dev/sdd1
  Physical volume "/dev/sdd1" successfully created
[root@mylinz ~]#


6.Verify your work.
[root@mylinz ~]# pvs /dev/sdd1
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sdd1       lvm2 a-   511.98m 511.98m
[root@mylinz ~]#


How to replace a failed DISK in LVM ? 
Now most of the Linux servers are using SAN boxes for data storage.So the disk failure rate is very less compare to internal disks. But in some cases customer would like use internal disks for the LVM volumes.Here we will see how to replace a failed disk if the volume is mirrored. If any disk got failed for non-mirror volume then there is no way to recover the data and its better to remove the volume and recreate it with good disk. High Level Plan:
1.Find out which disk is failed and what are the mirrored volumes are affected
2.Remove the volume from the diskgroup forcefully
3.Replace a new disk
4.Add a new mirror to the volumes which are affected.

Linux – LVM – Volume Group Operations

We have already seen the basics of logical volume manager structure on the previous article.Here we are going to see about Logical volume manager’s  volume group operations and management. Volume group is high level container in LVM which contains one or more physical disk or LUNS. A volume group can span in to multiple disks, whether internal or external disks. External disks are typically SAN but could be external SCSI or ISCSI disks.According to the filesystem requirements ,you can add or remove disk from volume group easily.This flexibility provides volumes can be re-sized dynamically. 





How to create new volume group ?
1.List the physical disks which were brought under logical volume manager control using pvcreate command.
[root@mylinz ~]# pvs
  PV         VG        Fmt  Attr PSize   PFree
  /dev/sda2  vg_mylinz lvm2 a-    19.51g      0
  /dev/sdd1            lvm2 a-   511.98m 511.98m
  /dev/sde             lvm2 a-   512.00m 512.00m
  /dev/sdf             lvm2 a-     5.00g   5.00g
[root@mylinz ~]#
 
2.Create a new volume group using disk /dev/sdd1.Here the volumegroup name is “uavg”.
[root@mylinz ~]# vgcreate uavg /dev/sdd1
  Volume group "uavg" successfully created
[root@mylinz ~]#

3.Verify the new volume group.
[root@mylinz ~]# vgs uavg
  VG   #PV #LV #SN Attr   VSize   VFree
  uavg   1   0   0 wz--n- 508.00m 508.00m
[root@mylinz ~]#
[root@mylinz ~]# pvs |grep uavg
  /dev/sdd1  uavg      lvm2 a-   508.00m 508.00m
[root@mylinz ~]#

4.For detailed volume group information,use below mentioned command.
[root@mylinz ~]# vgdisplay uavg
  --- Volume group ---
  VG Name               uavg
  System ID
  Format                lvm2
  Metadata Areas        1
  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                1
  Act PV                1
  VG Size               508.00 MiB
  PE Size               4.00 MiB
  Total PE              127
  Alloc PE / Size       0 / 0
  Free  PE / Size       127 / 508.00 MiB
  VG UUID               c87FyZ-5DND-oQ3n-iTh1-Vb1f-nBML-vUBUE9
[root@mylinz ~]#

How to rename volume Group ? can we rename the VG on fly ?
We can rename the volume group in Redhat Linux using vgrename command.The rename can be done on the fly without any impact. Here i am going to show the vgrename and proving that no impact on this activity.

1.List the currently mounted from volume group “uavg”
[root@mylinz ~]# df -h /mnt
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg-ualvol1
                       51M  4.9M   43M  11% /mnt
[root@mylinz ~]#

2.List the available volume group.
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg        1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
3.Rename the volume group using “vgrename” command.
[root@mylinz ~]# vgrename uavg uavg_new
  Volume group "uavg" successfully renamed to "uavg_new"
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg_new    1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]#

4.Check the volume status .You can see still volume is available for operation.
[root@mylinz ~]# df -h /mnt
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg-ualvol1
                       51M  4.9M   43M  11% /mnt
[root@mylinz ~]#
[root@mylinz ~]# cd /mnt
[root@mylinz mnt]# touch 3 4 5 6
[root@mylinz mnt]# ls -lrt
total 18
drwx------. 2 root root 12288 Aug  6 22:55 lost+found
-rw-r--r--. 1 root root     0 Aug  7 00:32 1
-rw-r--r--. 1 root root     0 Aug  7 00:32 7
-rw-r--r--. 1 root root     0 Aug 12 21:17 6
-rw-r--r--. 1 root root     0 Aug 12 21:17 5
-rw-r--r--. 1 root root     0 Aug 12 21:17 4
-rw-r--r--. 1 root root     0 Aug 12 21:17 3
[root@mylinz mnt]#

5.But the volume is still reflecting the old device.This can be removed after remounting the volume.This can be down when you have down time for the server.Please don;t forget to update “fstab” according to the new volume group name.
[root@mylinz ~]# umount /mnt
[root@mylinz ~]# mount -t ext4 /dev/mapper/uavg_new-ualvol1 /mnt
[root@mylinz ~]# df -h /mnt
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg_new-ualvol1
                       51M  4.9M   43M  11% /mnt
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg_new    1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]#

How to Extend the volume group ?
Volume group can be extend on the fly by adding new disks or LUNS to the existing volume group.

1.List the volume group .
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg        1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]#
2.List the available physical volumes for extending the volume group “uavg”.
Check out if you have any doubt to create new physical volume from new disks or LUNS.
[root@mylinz ~]# pvs
  PV         VG        Fmt  Attr PSize   PFree
  /dev/sda2  vg_mylinz lvm2 a-    19.51g      0
  /dev/sdd1  uavg      lvm2 a-   508.00m 456.00m
  /dev/sde             lvm2 a-   512.00m 512.00m
  /dev/sdf             lvm2 a-     5.00g   5.00g
[root@mylinz ~]#
3.Let me choose “sde” to extend volume group “uavg”.
[root@mylinz ~]# vgextend uavg /dev/sde
  Volume group "uavg" successfully extended
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize    VFree
  uavg        2   1   0 wz--n- 1016.00m 964.00m
  vg_mylinz   1   2   0 wz--n-   19.51g      0
[root@mylinz ~]#
From the above output ,you can see volume group “uavg” has been extended successfully. 

4.Check the “pvs” command output. /dev/sde will show as part of “uavg” now.
[root@mylinz ~]# pvs
  PV         VG        Fmt  Attr PSize   PFree
  /dev/sda2  vg_mylinz lvm2 a-    19.51g      0
  /dev/sdd1  uavg      lvm2 a-   508.00m 456.00m
  /dev/sde   uavg      lvm2 a-   508.00m 508.00m
  /dev/sdf             lvm2 a-     5.00g   5.00g
[root@mylinz ~]#

How to scan a disks for LVM ? 
You need to scan a LVM disks whenever there is a hardware changes on your server.Hardware changes may be a newly added or removed disks which will be hotplug disks or new disks added to SAN systems.
[root@mylinz ~]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "uavg" using metadata type lvm2
  Found volume group "vg_mylinz" using metadata type lvm2
[root@mylinz ~]#

How to decrease the volume group size ? or How to remove disks from LVM ?
Disks can be removed from volume group if its not used for any volumes.

1.First find out the disks which we are planning to remove it from volume group  is not used for any volumes using below mentioned commands.
[root@mylinz ~]# lvs -a -o +devices
LV      VG        Attr   LSize  Origin Snap%  Move Log Copy%  Convert Devices
ualvol1 uavg      -wi-a- 52.00m                                      /dev/sdd1(0)
lv_root vg_mylinz -wi-ao 16.54g                                      /dev/sda2(0)
lv_swap vg_mylinz -wi-ao  2.97g                                   /dev/sda2(4234)
[root@mylinz ~]#  pvs  -a -o +devices |grep uavg
  /dev/sdd1              uavg      lvm2 a-   508.00m 456.00m /dev/sdd1(0)
  /dev/sdd1              uavg      lvm2 a-   508.00m 456.00m
  /dev/sde               uavg      lvm2 a-   508.00m 508.00m
  /dev/uavg/ualvol1                     --        0       0
[root@mylinz ~]#
From the above commands output,we can see disk “sde” is not used for any volumes (lvs command output) in volume group “uavg” . 

2.Check the disk details. From this details you can confirm ,PE (i.e physical extends) are not used in VG. (Total PE=127 & Free PE=127). 
[root@mylinz ~]# pvdisplay /dev/sde
  --- Physical volume ---
  PV Name               /dev/sde
  VG Name               uavg
  PV Size               512.00 MiB / not usable 4.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              127
  Free PE               127
  Allocated PE          0
  PV UUID               FadWLT-LjD8-v8VB-pboY-eZbK-vYpE-ZWq0i9
[root@mylinz ~]#
3.List the volume group details before removing the physical volume.
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize    VFree
  uavg        2   1   0 wz--n- 1016.00m 964.00m
  vg_mylinz   1   2   0 wz--n-   19.51g      0
[root@mylinz ~]# vgdisplay uavg
  --- Volume group ---
  VG Name               uavg
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  9
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               1016.00 MiB
  PE Size               4.00 MiB
  Total PE              254
  Alloc PE / Size       13 / 52.00 MiB
  Free  PE / Size       241 / 964.00 MiB
  VG UUID               c87FyZ-5DND-oQ3n-iTh1-Vb1f-nBML-vUBUE9
[root@mylinz ~]#

4.Now we are ready to remove the “/dev/sde” from volume group “uavg”.
[root@mylinz ~]# vgreduce uavg /dev/sde
  Removed "/dev/sde" from volume group "uavg"
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg        1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]# vgdisplay uavg
  --- Volume group ---
  VG Name               uavg
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  10
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               508.00 MiB
  PE Size               4.00 MiB
  Total PE              127
  Alloc PE / Size       13 / 52.00 MiB
  Free  PE / Size       114 / 456.00 MiB
  VG UUID               c87FyZ-5DND-oQ3n-iTh1-Vb1f-nBML-vUBUE9
[root@mylinz ~]#
From the above outputs ,you can see #PV reduced to “1”  and volume group size also reduced. 

How to activate and Deactivate the volume group ? 
By default volume group will be in active mode. But some circumstances,you need to put the volume group in disabled mode or inactive mode thus unknown to Linux kernel. Here we will see how to activate and deactivate the volume group.

1.List the volume groups.
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg        1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]#

2.Deactive the volume group “uavg”
[root@mylinz ~]# vgchange -a n uavg
  0 logical volume(s) in volume group "uavg" now active
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg        1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]#
you can not deactivate VG if any opened volumes from that volumegroup. You have to unmount all the volumes from the volume group before deactivating it .You will get below error if any volumes in opened state .
[root@mylinz ~]# vgchange -a n uavg
  Can't deactivate volume group "uavg" with 1 open logical volume(s)
[root@mylinz ~]#

3.Check the volume status.It will be in “Not Available” status.
[root@mylinz ~]# lvdisplay /dev/uavg/ualvol1
  --- Logical volume ---
  LV Name                /dev/uavg/ualvol1
  VG Name                uavg
  LV UUID                6GB8TR-ih7d-vg7J-xCLE-A8OH-gmwy-3XLyOb
  LV Write Access        read/write
  LV Status              NOT available
  LV Size                52.00 MiB
  Current LE             13
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto

[root@mylinz ~]#

4.You can activate the volume group use same  command with different options.
[root@mylinz ~]# vgchange -a y uavg
  1 logical volume(s) in volume group "uavg" now active
[root@mylinz ~]# lvdisplay /dev/uavg/ualvol1
  --- Logical volume ---
  LV Name                /dev/uavg/ualvol1
  VG Name                uavg
  LV UUID                6GB8TR-ih7d-vg7J-xCLE-A8OH-gmwy-3XLyOb
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                52.00 MiB
  Current LE             13
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

[root@mylinz ~]#

5.Mount the volume .We are back to normal operation.
[root@mylinz ~]# mount -t ext4 /dev/mapper/uavg-ualvol1 /mnt
[root@mylinz ~]#

How to backup & restore the LVM volumegroup metadata ?
Automatically Metadata backups and archives are created whenever you create new volume group Linux system.By default backup stored in /etc/lvm/backup and archives are stored in /etc/lvm/archive .We can also manually backup the lvm configuration using “vgcfgbackup” command.

1.Run “vgcfgbackup” command to take new configuration backup for volume group “uavg”.
[root@mylinz ~]# vgcfgbackup uavg
  Volume group "uavg" successfully backed up.
[root@mylinz ~]#

2.You can find the new configuration file under the below mentioned location.
[root@mylinz ~]# cd /etc/lvm/
[root@mylinz lvm]# ls -lrt
total 36
-rw-r--r--. 1 root root 21744 Aug 18  2010 lvm.conf
drwx------. 2 root root  4096 Aug 12 23:57 archive
drwx------. 2 root root  4096 Aug 13 00:27 backup
drwx------. 2 root root  4096 Aug 13 00:27 cache
[root@mylinz lvm]# cd backup/
[root@mylinz backup]# ls -lrt
total 8
-rw-------. 1 root root 1474 Jun  3  2012 vg_mylinz
-rw-------. 1 root root 1164 Aug 13 00:27 uavg
[root@mylinz backup]# file uavg
uavg: ASCII text
[root@mylinz backup]#
[root@mylinz backup]# more uavg
# Generated by LVM2 version 2.02.72(2) (2010-07-28): Tue Aug 13 00:27:46 2013

contents = "Text Format Volume Group"
version = 1

description = "Created *after* executing 'vgcfgbackup /root/uavg.meta.bck uavg'"

creation_host = "mylinz"        # Linux mylinz 2.6.32-71.el6.x86_64 #1 SMP Wed Sep 1 01:33:01 EDT 2010 x86_64
creation_time = 1376333866      # Tue Aug 13 00:27:46 2013

uavg {
        id = "c87FyZ-5DND-oQ3n-iTh1-Vb1f-nBML-vUBUE9"
        seqno = 10
        status = ["RESIZEABLE", "READ", "WRITE"]
        flags = []
        extent_size = 8192              # 4 Megabytes
        max_lv = 0
        max_pv = 0
        metadata_copies = 0

3.To restore the volume group meta data,you below command.
[root@mylinz ~]# vgcfgrestore uavg
  Restored volume group uavg
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg        1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]#

How to export/Move  the volume group to other Linux node ?
Complete LVM volume group can be moved from one system to another system using vg commands.Here we will see step by step guide for this migration.
1.Unmount all the volumes from volume group which needs to be migrated.

2.Make the volume group inactive using “vgchange” command to ensure there will no I/O to the VG.
[root@mylinz ~]# vgchange -a n uavg
  0 logical volume(s) in volume group "uavg" now active
[root@mylinz ~]#


3.Export the volumegroup .
[root@mylinz ~]# vgexport uavg
  Volume group "uavg" successfully exported
[root@mylinz ~]#

4.You can verify the exported status using “pvscan” command.
[root@mylinz ~]# pvscan
  PV /dev/sdd1    is in exported VG uavg [508.00 MiB / 456.00 MiB free]
  PV /dev/sda2   VG vg_mylinz       lvm2 [19.51 GiB / 0    free]
  PV /dev/sde                       lvm2 [512.00 MiB]
  PV /dev/sdf                       lvm2 [5.00 GiB]
  Total: 4 [25.50 GiB] / in use: 2 [20.00 GiB] / in no VG: 2 [5.50 GiB]
[root@mylinz ~]#

5.Now assign the disks from SAN level to the system where you want to import the volume group.

6.Scan the disks and make the disks available for VG import. 
Check out the Disks or LUN scanning procedure in Redhat Linux.
7.Import the volume group.
[root@mylinz ~]# vgimport uavg
  Volume group "uavg" successfully imported
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg        1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]#
8.Activate the volume group for normal operation.
[root@mylinz ~]# vgchange -a y uavg
  1 logical volume(s) in volume group "uavg" now active
[root@mylinz ~]#
How to recreate the device files for LVM volumes ?
Due to server crash or other reason, we may loose the LVM device files and volume group directory . In those situation ,you need to recreate what you have lost. LVM provides command called “vgmknodes” which will help you to recreate those missing files.Here is a small experiment.

1.Let me remove device file for volume “uavg-ualvol1”  which is part of VG “uavg” . 
[root@mylinz ~]# cd /dev/mapper/
[root@mylinz mapper]# ls -lrt
total 0
crw-rw----. 1 root root 10, 58 Aug  5 19:28 control
lrwxrwxrwx. 1 root root      7 Aug  5 19:28 vg_mylinz-lv_root -> ../dm-0
lrwxrwxrwx. 1 root root      7 Aug  5 19:28 vg_mylinz-lv_swap -> ../dm-1
lrwxrwxrwx. 1 root root      7 Aug 13 00:47 uavg-ualvol1 -> ../dm-2
[root@mylinz mapper]# rm uavg-ualvol1
rm: remove symbolic link `uavg-ualvol1'? y
[root@mylinz mapper]#
2.Let me move the “/etc/lvm” to .old.
[root@mylinz etc]# mv lvm lvm.old
[root@mylinz etc]# ls -lrt |grep lvm
drwx------.  5 root   root     4096 Jun  1  2012 lvm.old
[root@mylinz etc]#
3.Let me run “vgmknodes” and see whether this command is able to recreate the removed device file and lvm directory .
[root@mylinz etc]# vgmknodes

4.Check whether devices files are created or not. 
[root@mylinz mapper]# ls -lrt
total 0
crw-rw----. 1 root root  10, 58 Aug  5 19:28 control
lrwxrwxrwx. 1 root root       7 Aug  5 19:28 vg_mylinz-lv_root -> ../dm-0
lrwxrwxrwx. 1 root root       7 Aug  5 19:28 vg_mylinz-lv_swap -> ../dm-1
brw-rw----. 1 root disk 253,  2 Aug 13 00:54 uavg-ualvol1
[root@mylinz mapper]#
wow…its recreated. 

5.Let me check /etc/lvm directory is created or not .
[root@mylinz etc]# ls -lrt |grep lvm
drwx------.  5 root   root     4096 Jun  1  2012 lvm.old
drwxr-xr-x.  3 root   root     4096 Aug 13 00:54 lvm
[root@mylinz etc]#
Awesome…Its recreated. 

How to remove the volume group ?
You can remove the volume group using vgremove command.
If any volumes from the volume group in mounted status ,you will get below error .
[root@mylinz ~]# vgremove uavg
Do you really want to remove volume group "uavg" containing 1 logical volumes? [y/n]: y
  Can't remove open logical volume "ualvol1"
[root@mylinz ~]#

Un-mount the volume and  remove the volume group.
[root@mylinz ~]# vgremove uavg
Do you really want to remove volume group "uavg" containing 1 logical volumes? [y/n]: y
[root@mylinz ~]#
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]#
Hope this article shared enough information about LVM2 volume group administration. 
You can also split and combine volume groups in LVM2 like veritas volume manager.

Please leave a comment if you have any doubt on this . Share it in social networks to reach all the Linux administrators and beginners. 

What is PS2 - Continuation Interactive Prompt in Linux

A very long command can be broken down to multiple lines by giving \ at the end of the line. The default interactive prompt for a multi-lin...