Monday, August 15, 2011

Logical Volume management (LVM)

LOGICAL VOLUME MANAGER
------------------------------------

Logical volume management provides a higher-level view of the disk storage
on a computer system than the traditional view of disks and partitions.
This gives the system administrator much more flexibility in allocating storage
to applications and users. Below method shows how to create partitions using LVM
--------------------------------------------------------------------------------

[root@rac1 ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 209 MB, 209715200 bytes
64 heads, 32 sectors/track, 200 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes

Disk /dev/sdb doesn't contain a valid partition table
[root@rac1 ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. 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)

Command (m for help): n
Command action
e extended
p primary partition (1-4) ----> hit p
Partition number (1-4): 1
First cylinder (1-200, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-200, default 200):
Using default value 200

Command (m for help): t

Selected partition 1
Hex code (type L to list codes): l

0 Empty 1e Hidden W95 FAT1 80 Old Minix c1 DRDOS/sec (FAT-
1 FAT12 24 NEC DOS 81 Minix / old Lin c4 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 82 Linux swap c6 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 83 Linux c7 Syrinx
4 FAT16 <32M 40 Venix 80286 84 OS/2 hidden C: da Non-FS data
5 Extended 41 PPC PReP Boot 85 Linux extended db CP/M / CTOS / .
6 FAT16 42 SFS 86 NTFS volume set de Dell Utility
7 HPFS/NTFS 4d QNX4.x 87 NTFS volume set df BootIt
8 AIX 4e QNX4.x 2nd part 8e Linux LVM e1 DOS access
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e3 DOS R/O
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e4 SpeedStor
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS eb BeOS fs
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi ee EFI GPT
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ef EFI (FAT-12/16/
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD f0 Linux/PA-RISC b
10 OPUS 55 EZ-Drive a7 NeXTSTEP f1 SpeedStor
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f4 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f2 DOS secondary
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot fb VMware VMFS
16 Hidden FAT16 63 GNU HURD or Sys b7 BSDI fs fc VMware VMKCORE
17 Hidden HPFS/NTF 64 Novell Netware b8 BSDI swap fd Linux raid auto
18 AST SmartSleep 65 Novell Netware bb Boot Wizard hid fe LANstep
1b Hidden W95 FAT3 70 DiskSecure Mult be Solaris boot ff BBT
1c Hidden W95 FAT3 75 PC/IX bf Solaris
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@rac1 ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 209 MB, 209715200 bytes
64 heads, 32 sectors/track, 200 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 200 204799+ 8e Linux LVM

Note the id and system (8e Linux LVM)

Now we prepare our new partitions for LVM:
-----------------------------------------------
[root@rac1 ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created

note: to remove you can use pvremove command

Now run pvdisplay command to see the current state of your physical volume
--------------------------------------------------------------------------

[root@rac1 ~]# pvdisplay
"/dev/sdb1" is a new physical volume of "200.00 MB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 200.00 MB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID dfcH5C-A7sh-RKDw-dL80-8ln4-nb7l-9N157z

Create a volume group. In my case I created a group called "datavgrp"
---------------------------------------------------------------------
[root@rac1 ~]# vgcreate datavgrp /dev/sdb1
Volume group "datavgrp" successfully created

Vgdisplay command will show the status of your volume group
------------------------------------------------------------
[root@rac1 ~]# vgdisplay
--- Volume group ---
VG Name datavgrp
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 196.00 MB
PE Size 4.00 MB
Total PE 49
Alloc PE / Size 0 / 0
Free PE / Size 49 / 196.00 MB
VG UUID iyNgDB-6q11-3g1d-AcBN-OMYX-zK0f-wwuoWB

Now create the logical volumes
-------------------------------
[root@rac1 ~]# lvcreate --name bkp_vol1 --size 20M datavgrp
Logical volume "bkp_vol1" created

[root@rac1 ~]# lvcreate --name bkp_vol2 --size 100M datavgrp
Logical volume "bkp_vol2" created

[root@rac1 ~]# lvcreate --name bkp_vol3 --size 70M datavgrp
Rounding up size to full physical extent 72.00 MB
Logical volume "bkp_vol3" created

[root@rac1 ~]# lvscan
ACTIVE '/dev/datavgrp/bkp_vol1' [20.00 MB] inherit
ACTIVE '/dev/datavgrp/bkp_vol2' [100.00 MB] inherit
ACTIVE '/dev/datavgrp/bkp_vol3' [72.00 MB] inherit

now we have three logical volumes, but we don't have any filesystems in them,
and without a filesystem we can't save anything in them. Therefore we create
an ext3 filesystem

lvdisplay will give you the details of logical volume
------------------------------------------------------
[root@rac1 dev]# lvdisplay
--- Logical volume ---
LV Name /dev/datavgrp/bkp_vol1
VG Name datavgrp
LV UUID jJUDn4-eJzb-SvPz-sWIz-XmWM-PC9s-eYqWWi
LV Write Access read/write
LV Status available
# open 0
LV Size 20.00 MB
Current LE 5
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

--- Logical volume ---
LV Name /dev/datavgrp/bkp_vol2
VG Name datavgrp
LV UUID 3t2bAn-GPEu-iQCD-YK2J-D3oZ-nJ2X-LsjZFJ
LV Write Access read/write
LV Status available
# open 0
LV Size 100.00 MB
Current LE 25
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1

--- Logical volume ---
LV Name /dev/datavgrp/bkp_vol3
VG Name datavgrp
LV UUID zvIUK8-feaL-N3v9-Vk0e-foq9-NdHu-qz3J1P
LV Write Access read/write
LV Status available
# open 0
LV Size 72.00 MB
Current LE 18
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2

Make the filesystem
--------------------

[root@rac1 dev]# mkfs.ext3 /dev/datavgrp/bkp_vol1
mke2fs 1.35 (28-Feb-2004)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
5136 inodes, 20480 blocks
1024 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=20971520
3 block groups
8192 blocks per group, 8192 fragments per group
1712 inodes per group
Superblock backups stored on blocks:
8193

Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@rac1 dev]# [root@rac1 dev]# mkfs.ext3 /dev/datavgrp/bkp_vol1
mke2fs 1.35 (28-Feb-2004)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
5136 inodes, 20480 blocks
1024 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=20971520
3 block groups
8192 blocks per group, 8192 fragments per group
1712 inodes per group
Superblock backups stored on blocks:
8193

Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done


[root@rac1 dev]# mkfs.ext3 /dev/datavgrp/bkp_vol1
mke2fs 1.35 (28-Feb-2004)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
5136 inodes, 20480 blocks
1024 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=20971520
3 block groups
8192 blocks per group, 8192 fragments per group
1712 inodes per group
Superblock backups stored on blocks:
8193

Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

Repeate the same for the other volumes

mkfs.ext3 /dev/datavgrp/bkp_vol2
mkfs.ext3 /dev/datavgrp/bkp_vol3


Now we can mount the volumes Or configure the mount points in /etc/fstab
---------------------------------------------------------------------------


mount /dev/datavgrp/bkp_vol1 /var/backup1
mount /dev/datavgrp/bkp_vol2 /var/backup3
mount /dev/datavgrp/bkp_vol3 /var/backup3

------------------X-----------------X------------------X----------------------X