Tuesday, 6 August 2013

RedHat Linux : Logical Volume Manager

In this Article, we will understand and learn how to Create, Display and Resize Physical Volume , Volume Group , Logical Volume in Logical Volume Manager with examples in details.

What is Logical Volume Manager or LVM ?
Logical Volume Manager or LVM provides a method of allocating space on mass-storage devices that is more flexible than conventional partitioning schemes. With LVM or Logical Volume Manager, a
hard drive or set of hard drives is allocated to one or more Physical Volume Groupical volumes. Logical Volume Manager (LVM) physical volumes can be placed on other block devices which might span two or more disks.
The Physical Volumes are combined into Logical Volumes, with the exception of the /boot/partition. The/boot/partition cannot be on a Logical Volume group because the boot loader cannot read it.The Logical Volume Manager available in almost all Unix flavour like HP-UX, IBM AIX and OS/2 operating systems etc.
Logical Volume Manager or LVM Terms
  • Physical Volume:  A physical volume (PV) is another name for a regular physical disk partition that is used or will be used by LVM or  Logical Volume Manager  as you can see in the below diagram.
  • Volume Group:  Any number of physical volumes  or PV on different disk drives can be added  together into a volume group (VG) as you can see in the below diagram..
  • Logical Volumes: Volume Groups  or VG  must then be subdivided into Logical volumes or LV. Each logical volume can be individually formatted as if it were a regular Linux partition. A logical volume is, therefore, like a virtual partition on your virtual disk drive. as you can see in the below diagram..

The Logical Volume Manager or LVM features:
  1. Resize volume groups online by absorbing new physical volumes (PV) or ejecting existing ones.
  2. Resize logical volumes (LV) online by concatenating extents onto them or truncating extents from them.
  3. Create read-only snapshots of logical volumes (LVM1).
  4. Create read-write snapshots of logical volumes (LVM2).
  5. Stripe whole or parts of logical volumes across multiple PVs, in a fashion similar to RAID 0.
  6. Mirror whole or parts of logical volumes, in a fashion similar to RAID 1.
  7. Move online logical volumes between PVs.
  8. Split or merge volume groups in situ (as long as no logical volumes span the split). This can be useful when migrating whole logical volumes to or from offline storage.
  9. The LVM will also work in a shared-storage cluster (where disks holding the PVs are shared between multiple host computers), but requires an additional daemon to propagate state changes between cluster nodes.
First let’s see the brief Steps of Creating Logical Volume Manager.
Step 1: List the LUNS: By this step we will identify the LUNs that can be use for creation of VOLUME group ( Explained with Example later section )
#fdisk -l 2> /dev/null | grep Disk
Step 2: Create Partition: By this step we will create partition on free LUNs
#fdisk /dev/hda# or fdisk /dev/sda# or /dev/dm#
Step 3: Create Physical Volumes
pvcreate   /dev/hda#    /dev/hda#
Step 4: Create Volume Group VG1  with two Physical Volumes
vgcreate   VG1   /dev/hda#    /dev/hda#
Step 5: Change Volume  Group to ACTIVE(optional)
vgchange   -a   y    VG1
Step 6: Create Logical VolumeLV1  of 600 MB in size in Volume Group VG1
lvcreate     -L   +600M    -n    LV1    VG1
Step 7  : Create the file system
mkfs -t ext3  /dev/VG1/LV1
If you are creating filesystem for application then you can might use
mkfs.ext3 -m 0 /dev/VG1/LV1 
(this will remove the over head 5% that is reserved for the super-user. Useful when you’re creating file system for the application.)
To turn off filesytem check set the maximum count to 0 with the ‘-c‘ option.(Optional).
tune2fs -c -1 /dev/VG1/LV1
( -c = maximum mount counts. If max-mount-counts is 0 or -1, the number of times the filesystem is mounted will be disregarded by e2fsck(8) and the kernel. The interval between check will still be active, so suppressing mount counts wont cause any problem)
Step 8:Create a directory
mkdir -p /mnt/data
Step 9: Make an entry in /etc/fstab for auto mount on each reboot of the server
/dev/VG1/LV1  /mnt/data              ext3                defaults                 0 0
Step 10: Activate  and Mount the new filesystem
mount  /dev/VG1/LV1 /mnt/data
Step 11 : Check the newly mounted Logical Volume
df -h /mnt/data
For Short details output of Physical Volume , Local Volume and Volume Group in Logical Volume Manager use below command.
#pvscan
#lvscan
#vgscan
For Long Full Details output of Physical Volume , Local Volume and Volume Group in Logical Volume Manager use below command.
#pvdisplay
#lvdisplay
#vgdisplay
RESIZING of Logical Volume Manager
Extending a file system  : Note Only on RHEL 5.3 and above you can directly increase filesystem without unmount.
Step 1: Extend the Logical Volume
lvextend    -L    +200M      /dev/VG1/LV1
Step 2: Re size it as below so that Kernal
resize2fs      /dev/VG1/LV1
Example of How to Create, Display and Resize PV , VG , LV
You have one or more Physical Volumes (/dev/sdb1 – /dev/sde1 in our example), and on these Physical Volumes you create one or more volume groups (e.g. fileserver), and in each volume group you can create one or more logical volumes. If you use multiple physical volumes, each logical volume can be bigger than one of the underlying physical volumes.
It is a good practice to not allocate the full space to logical volumes, but leave some space unused. That way you can enlarge one or more logical volumes later on if you feel the need for it.
Scan the newly assigned LUNs by SAN team by Running below command against each HBA adapter.
echo “- – -” > /sys/class/scsi_host/host0/scan
Check the avaible disk on the server with following two commands.
fdisk -l 
Let’s find out about our hard disks using fdisk -l: The output looks like this:
webmanual01:~# fdisk -l Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytesDevice Boot Start End Blocks Id System
/dev/sda1 * 1 18 144553+ 83 Linux
/dev/sda2 19 2450 19535040 83 Linux
/dev/sda4 2451 2610 1285200 82 Linux swap / SolarisDisk /dev/sdb: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytesDisk /dev/sdb doesn’t contain a valid partition tableDisk /dev/sdc: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytesDisk /dev/sdc doesn’t contain a valid partition tableDisk /dev/sdd: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytesDisk /dev/sdd doesn’t contain a valid partition tableDisk /dev/sde: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytesDisk /dev/sde doesn’t contain a valid partition tableDisk /dev/sdf: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytesDisk /dev/sdf doesn’t contain a valid partition table 
There are no partitions yet on /dev/sdb – /dev/sdf
In the above you can there is no partitions yet on /dev/sdb – /dev/sdf means these LUNS are unused and can be use for a new Logical Volume Manager
Create Partion
On this example, we will create the partitions /dev/sdb1, /dev/sdc1, /dev/sdd1, and /dev/sde1 with 25 GB  and leave /dev/sdf untouched for now. It will act our hard disks had only 25GB of space instead of 80GB for now, therefore we assign 25GB to /dev/sdb1, /dev/sdc1, /dev/sdd1, and /dev/sde1:
fdisk /dev/sdb
webmanual01:~# fdisk /dev/sdbThe number of cylinders for this disk is set to 10443.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)Command (m for help): <– m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition’s system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)Command (m for help): <– n
Command action
e extended
p primary partition (1-4)
<– p
Partition number (1-4): <– 1
First cylinder (1-10443, default 1): <– < Press ENTER> 
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-10443, default 10443): <– +25000MCommand (m for help): <– t
Selected partition 1
Hex code (type L to list codes): <– L0 Empty 1e Hidden W95 FAT1 80 Old Minix be Solaris boot
1 FAT12 24 NEC DOS 81 Minix / old Lin bf Solaris
2 XENIX root 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
5 Extended 41 PPC PReP Boot 85 Linux extended c7 Syrinx
6 FAT16 42 SFS 86 NTFS volume set da Non-FS data
7 HPFS/NTFS 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / .
8 AIX 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility
9 AIX bootable 4f QNX4.x 3rd part 8e Linux LVM df BootIt
a OS/2 Boot Manag 50 OnTrack DM 93 Amoeba e1 DOS access
b W95 FAT32 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
c W95 FAT32 (LBA) 52 CP/M 9f BSD/OS e4 SpeedStor
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
f W95 Ext’d (LBA) 54 OnTrackDM6 a5 FreeBSD ee EFI GPT
10 OPUS 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
11 Hidden FAT12 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b
12 Compaq diagnost 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor a9 NetBSD f4 SpeedStor
16 Hidden FAT16 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fd Linux raid auto
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fe LANstep
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid ff BBT
1c Hidden W95 FAT3 75 PC/IX
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!
Now we do the same for the hard disks /dev/sdc – /dev/sde:
fdisk /dev/sdc
fdisk /dev/sdd
fdisk /dev/sde
Verify 
Run fdisk -l again. The output should look like this:
webmanual01:~# fdisk -lDisk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytesDevice Boot Start End Blocks Id System
/dev/sda1 * 1 18 144553+ 83 Linux
/dev/sda2 19 2450 19535040 83 Linux
/dev/sda4 2451 2610 1285200 82 Linux swap / SolarisDisk /dev/sdb: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytesDevice Boot Start End Blocks Id System
/dev/sdb1 1 3040 24418768+ 8e Linux LVMDisk /dev/sdc: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytesDevice Boot Start End Blocks Id System
/dev/sdc1 1 3040 24418768+ 8e Linux LVMDisk /dev/sdd: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytesDevice Boot Start End Blocks Id System
/dev/sdd1 1 3040 24418768+ 8e Linux LVMDisk /dev/sde: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytesDevice Boot Start End Blocks Id System
/dev/sde1 1 3040 24418768+ 8e Linux LVMDisk /dev/sdf: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdf doesn’t contain a valid partition table
Create PV
Now create PV on partition  created  on  the last step
webmanual01:~# pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
  Physical volume ”/dev/sdb1″ successfully created
  Physical volume ”/dev/sdc1″ successfully created
  Physical volume ”/dev/sdd1″ successfully created
  Physical volume ”/dev/sde1″ successfully
Create VG
Now let’s create a VG with name vgtest with PV created on last step
vgcreate  vgtest /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
Creating LV
Create a Logical Volume lvtesta of space 1 G on VG vgtesta
root@webmanual:/root> lvcreate -L +1024M /dev/vgtesta -n lvtesta
Incorrect metadata area header checksum
Logical volume “lvtesta” created
Checking LV properties
root@webmanual:/root> lvdisplay /dev/vgtesta/lvtestaIncorrect metadata area header checksum
— Logical volume —
LV Name /dev/vgtesta/lvtesta
VG Name vgtesta
LV UUID sXOjN8-cWnW-7DKf-Pm32-kuKU-z7ww-hkGGgw
LV Write Access read/write
LV Status available
# open 0
LV Size 1.00 GB
Current LE 1
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:58
Creating a directory to mount the LV created on previous step
root@webmanual:/root> mkdir /tmp/testinga
root@webmanual:/root> ls -l /tmp/testinga
total 0
Create a filesystem type  ext3 on the LV lvtesta
root@webmanual:/root> mkfs -t ext3 /dev/vgtesta/lvtesta
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
131072 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
Make an entry in /etc/fstab and Mount the filesystem 
root@webmanual:/root> vi /etc/fstab
Press ESC+G then ESC + O
/dev/vgtesta/lvtesta /tmp/testinga ext3    defaults        0 0 <— Make enrty like this
root@webmanual:/root> mount /dev/vgtesta/lvtesta /tmp/testinga
Verify the Newly Mounted Filesystem
root@webmanual:/root> df -h /tmp/testinga
/dev/mapper/vgtesta-lvtesta 1008M   34M  924M   4% /tmp/testinga
Extend the newly created  Logical Volume or Filesystem
root@webmanual:/root> lvextend -L+1G /dev/vgtesta/lvtesta
Incorrect metadata area header checksum
Extending logical volume lvtesta to 2.00 GB
Logical volume lvtesta successfully resized
root@webmanual:/root>
Resize the Logical Volume so that OS makes the necessarily changes on corresponding files
root@webmanual:/root> resize2fs  /dev/vgtesta/lvtestaresize2fs 1.39 (29-May-2006)
Filesystem at /dev/vgtesta/lvtesta is mounted on /tmp/testinga; on-line resizing required
Performing an on-line resize of /dev/mapper/VolGroup00-ebilv01 to 5242880 (4k) blocks.
The filesystem on/dev/vgtesta/lvtesta is now 5242880 blocks long.
Sometimes in OLD Linux Resize failed and asked to run e2fsck . Then  un mount the filesystem and run e2fsck as below 
root@webmanual:/root> e2fsck -f /dev/vgtesta/lvtesta
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vgtesta/lvtesta: 11/262144 files (9.1% non-contiguous), 16822/524288 blocks
Resize will work now
root@webmanual:/root> resize2fs -p /dev/vgtesta/lvtesta 1G
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vgtesta/lvtesta to 262144 (4k) blocks.
Begin pass 3 (max = 16)
Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/vgtesta/lvtesta is now 262144 blocks long.
Reducing LV
root@webmanual:/root> lvreduce -L -1G /dev/vgtesta/lvtesta
Incorrect metadata area header checksum
WARNING: Reducing active logical volume to 1.00 GB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvtesta? [y/n]: y
Reducing logical volume lvtesta to 1.00 GB
Logical volume lvtesta successfully
root@webmanual:/root> df -h /tmp/testinga
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vgtesta-lvtesta
1008M 34M 934M 4% /tmp/testinga
root@webmanual:/root>

No comments :

Post a Comment