Hi Guys,
I am going to discuss a very very famous part of the LINUX called LVM (Logical Volume Management).
Before we start making LVM, lets learn some other basic things.
- To implement LVM we go step by step and make Physical Volume first.
- Then, we will make Volume Group.
- Then, finally the Logical Volume
- Logical Volume have the partition type ID "8e"
Similarly, swap has "82", raid has "fd", ntfs has "7", extended has "5", Linux has "83" etc etc.
Prerequistes:-
* A machine with a Linux Installation (In my case it is RHEL5)
* Free space on hard-disk to create the new partitions
Commands we will be using:
- pvcreate
- vgcreate
- lvcreate
- pvscan
- vgscan
- lvscan
- pvdisplay
- vgdisplay
- lvdisplay
- lvextend
- lvreduce
- resize2fs
So before reading further, please go through the manuals of these commands. So that you can be aware of the commands working.
I - ADD A LOGICAL PARTITION
Steps to go:-
1) Create new partition on the hard-disk with the command below:
# fdisk /dev/sda (in case if you have a SATA hard-disk)
or
# fdisk /dev/hda (in case if you have a IDE hard-disk)
press "m" now and read all the shortcuts now.
Now, press "n" to create a new partition. It will ask whether to create "primary partition" or "extended". If you already have 4 primary parition including 1 extended then it won't ask it and will directly create a logical partition in extended partition.
It will ask for first cylinder, keep it blank and press enter. Then, it will ask for last cylinder, put your partition size as "+1024M" (if specifying in MBs) or "+1G" (if specifying in GBs).
2) Now, change the partition ID. By default, every partition gets the partition ID as "83". So to change it press "t". "t" means toggle.
It will ask for the partition number of which ID has to be changed. Put the number of the newly created partition's number.
Then, it will ask the partition ID and put "8e" as the partition ID now.
3) Now, to write changes to the disk press "w". It will write changes to the disk and will automatically quit the program "fdisk".
4) Now, run the program "partprobe" to send information to the kernel about the newly added paritions.
# partprobe /dev/sda
or
# partprobe /dev/hda
NOTE:- I have created 3 logical paritions using the procedure above as SDA5, SDA6, SDA7. SDA4 is my extended partition. And i will be using this scheme only within the example.
5) Now, creating physical volume with the 3 new logical volume partitions (sda5,sda6,sda7)as:
# pvcreate /dev/sda{5,6,7}
Now, run the command "pvscan", "pvdisplay" and see results.
6) Now, creating volume group named as "vg1":
# vgcreate vg1 /dev/sda{5,6,7}
Now, run the command "vgscan", "vgdisplay" and see results.
7) Now, Finally creating the logical volume named as "lv1":
# lvcreate --size +500M --name lv1 vg1
Here, we are creating a logical volume "lv1" in the volume group "vg1" which we just created using the 3 new partitions. You can provide size either in MBs or GBs using M or G in the size parameter.
Now, run the command "lvscan", "lvdisplay" and see results.
8) Now, Format the new LVM as:
# mkfs.ext3 /dev/vg1/lv1
or
# mke2fs -j /dev/vg1/lv1
9) Now, Mounting LVM at a mount point "/lv1" as:
# mkdir /lv1
# mount -t ext3 /dev/vg1/lv1 /lv1
or you can add a new entry to the file "/etc/fstab". So that this partition is auto-mounted everytime on the mount point "/lv1".
# vim /etc/fstab
/dev/vg1/lv1 ext3 defaults 1 2
:wq
save and exit
10) Now, run "mount -a" and check the parition:
# mount -a
# cd /lv1
# df -h
II - EXTENDING A LOGICAL PARTITION
Resizng a LVM is very crucial task because there is a lot risk of "data corruption" if we didn't do it in right manner.
So for extending a LVM. First thing we need to remember is the sequence of it commands to be run.
1) First, umount the logical volume.
# umount /lv1
2) Now, extending the LVM:
# lvextend --size +50M /dev/vg1/lv1
Putting a + in front of 50M tells that LVM has to be extended by 50M. Means 50M has to added to the LVM.
3) Now, run file system check:
# e2fsck -f /dev/vg1/lv1
4) Finally, resizing the LVM:
# resize2fs /dev/vg1/lv1
III - REDUCING A LOGICAL PARTITION
Again, the sequence of commands is very important.
1) Umount the LVM as we did above.
2) Run file system check to avoid data corruption.
# e2fsck -f /dev/vg1/lv1
3) Resizing partition, let we had a LVM partition of 500M and we extended that by 50M and now reducing it to 300M:
# resize2fs /dev/vg1/lv1 300M
NOTE:- Remember, we are not using + here before 300M since we are reducing the size.
4) Finally, reducing LVM:
# lvreduce --size 300M /dev/vg1/lv1
5) mount -a
Similarly, we can extend physical volumes and volume groups.
EXTENDING PHYSICAL VOLUME:-
Create a new partition on hard-disk same as we did in making LVM and run the command as:
# pvcreate /dev/sda8
EXTENDING VOLUME GROUP:-
# vgextend vg1 /dev/sda8
Finished.....enjoy deploying LVM....:)
No comments:
Post a Comment