Pular para o conteúdo principal

Setup and Extend/Reduce Flexible Disk Storage with LVM in Linux

Logical Volume Management (LVM) makes it easier to manage disk space. If a file system needs more space, it can be added to its logical volumes from the free spaces in its volume group and the file system can be re-sized as we wish. If a disk starts to fail, replacement disk can be registered as a physical volume with the volume group and the logical volumes extents can be migrated to the new disk without data loss.
Create LVM Storage in Linux
Create LVM Storage in Linux
In a modern world every Server needs more space day by day for that we need to expand depending on our needs. Logical volumes can be use in RAID, SAN. A Physical Disk will be grouped to create a volume Group. Inside volume group we need to slice the space to create Logical volumes. While using logical volumes we can extend across multiple disks, logical volumes or reduce logical volumes in size with some commands without reformatting and re-partitioning the current disk. Volumes can stripes data across multiple disks this can increase the I/O stats.

LVM Features

  1. It is flexible to expand the space at any time.
  2. Any file systems can be installed and handle.
  3. Migration can be used to recover faulty disk.
  4. Restore the file system using Snapshot features to earlier stage. etc…
My Server Setup – Requirements
  1. Operating System – CentOS 6.5 with LVM Installation
  2. Server IP – 192.168.0.200

Creating LVM Disk Storage in Linux

1. We’ve used CentOS 6.5 Operating system using LVM in a Virtual Disk (VDA). Here we can see the Physical Volume (PV), Volume Group (VG), Logical Volume (LV) by using following command.
# pvs 
# vgs
# lvs
Check Physical Volumes
Check Physical Volumes
Here, is the description of each parameters shown in above screenshot.
  1. Physical Disk Size (PV Size)
  2. Disk which used was Virtual Disk vda.
  3. Volume Group Size (VG Size)
  4. Volume Group name (vg_tecmint)
  5. Logical Volume name (LogVol00, LogVol01)
  6. LogVol00 Assigned for sawp with 1GB Size
  7. LogVol01 Assigned for / with 16.5GB
So, from here we come to know that there is not enough free space in VDA disk.
2. For Creating a New Volume Group, we need to add Additional 3 hard disks in this server. It is not Compulsory to use 3 Drives just 1 is Enough to create a new VG and LV inside that vg, I am adding more here for demonstration purpose and for more feature command explanations.
Following are the disks I have added additionally.
sda, sdb, sdc
# fdisk -l
Verify Added Disks
Verify Added Disks
  1. Default Disk using for Operating system (Centos6.5).
  2. Partitions defined in default Disk (vda1 = swap), (vda2 = /).
  3. Additionally added Disks are mentioned as Disk1, Disk2, Disk3.
Each and every Disks are 20 GB in Size. Default PE Size of a Volume Group is 4 MB, Volume group what we are using in this server is configured using default PE.
Volume Group Display
Volume Group Display
  1. VG Name – A Volume Group name.
  2. Format – LVM Architecture Used LVM2.
  3. VG Access – Volume Group is in Read and Write and ready to use.
  4. VG Status – Volume Group can be re-sized, We can Expand more if we need to add more space.
  5. Cur LV – Currently there was 2 Logical volumes in this Volume Group.
  6. CurPV and Act PV – Currently Using Physical Disk was 1 (vda), And its active, so what we can use this volume group.
  7. PE Size – Physical Extends, Size for a disk can be defined using PE or GB size, 4MB is the Default PE size of LVM. For example, if we need to create 5 GB size of logical volume we can use sum of 1280 PE, Don’t you understand what I’m saying ?.
Here the Explanation –> 1024MB = 1GB, if so 1024MB x 5 = 5120PE = 5GB, Now Divide the 5120/4 = 1280, 4 is the Default PE Size.
  1. Total PE – This Volume Group have.
  2. Alloc PE – Total PE Used, full PE already Used, 4482 x 4PE = 17928.
  3. Free PE – Here it’s already used so there was no free PE.
3. Only vda used, Currently Centos Installed /boot, /, swap, in vda physical disk using lvm there were no space remaining in this disk.
# df -TH
Check the Disk Space
Check the Disk Space
Above image shows the mount Point we are using 18GB fully used for root, so there is no free space available.
4. So let’s, create new physical volume (pv), Volume Group (vg) in the name of tecmint_add_vg and create Logical Volumes (lv) in it, Here we can create 4 Logical Volumes in the name of tecmint_documents, tecmint_manager and tecmint_public.
We can extend the Volume Group of currently using VG to get more space. But here, what we are going to do is to Create new Volume Group and play around it, later we can see how to extend the file systems Volume group which is currently in use.
Before using a new Disk we need to partition the disk using fdisk.
# fdisk -cu /dev/sda
  1. c – Switch off DOS-compatible mode it is Recommend to include this Option.
  2. u – While listing the partition tables it will give us in sector instead of cylinder.
Create New Physical Partitions
Create New Physical Partitions
Next, follow the below steps to create new partition.
  1. Choose n to create new.
  2. Choose p to create a primary partition.
  3. Choose which number of partition we need to create.
  4. Press Enter twice to use the full space of the Disk.
  5. We need to change the type of newly created partition type t.
  6. Which number of partition need to change, choose the number which we created its 1.
  7. Here we need to change the type, we need to create LVM so we going to use the type code of LVM as 8e, if we do not know the type code Press L to list all type codes.
  8. Print the Partition what we created to just confirm.
  9. Here we can see the ID as 8e LINUX LVM.
  10. Write the changes and exit fdisk.
Do the above steps for other 2 disks sdb and sdc to create new partitions. Then Restart the machine to verify the partition table using fdisk command.
# fdisk -l
Verify Partition Table
Verify Partition Table

Creating Physical Volumes

5. Now, it’s time to create Physical Volumes using all 3 disks. Here, I have listed the physical disk using pvs command, only one default pvs is now listed.
# pvs
Then create the new physical disks using command.
# pvcreate /dev/sda1 /dev/sdb1 /dev/sdc1
Once again list the disk to see the newly created Physical disks.
# pvs
Create Physical Volumes
Create Physical Volumes

Creating Volume Groups

6. Create Volume Group in the name of tecmint_add_vg using available free PV Create using PE size 32. To Display the current volume groups, we can see there is one volume group with 1 PV using.
# vgs
This will create the volume group using 32MB PE size in the name of tecmint_add_vg using 3 Physical volumes we created in last steps.
# vgcreate -s 32M tecmint_add_vg /dev/sda1 /dev/sdb1 /dev/sdc1
Next, verify the volume group by running vgs command again.
# vgs
Create Volume Groups
Create Volume Groups
Verify Volume Groups
Verify Volume Groups
Understanding vgs command output:
  1. Volume Group name.
  2. Physical Volumes used in this Volume Group.
  3. Shows free space available in this volume group.
  4. Total Size of the Volume Group.
  5. Logical Volumes inside this volume group, Here we have not yet created so there is 0.
  6. SN = Number of Snapshots the volume group contains. (Later we can create a snapshot).
  7. Status of the Volume group as Writeable, readable, resizeable, exported, partial and clustered, Here it is wz–n- that means w = Writable, z = resizeable..
  8. Number of Physical Volume (PV) used in this Volume Group.
7. To Display more information about volume group use command.
# vgs -v
Check Volume Group Information
Check Volume Group Information
8. To get more information about newly created volume groups, run the following command.
# vgdisplay tecmint_add_vg
List New Volume Groups
List New Volume Groups
  1. Volume group name
  2. LVM Architecture used.
  3. It can be read and write state, ready to use.
  4. This volume group can be resizeable.
  5. No of Physical disk used and they are active.
  6. Volume Group total size.
  7. A Single PE size was 32 here.
  8. Total number of PE available in this volume group.
  9. Currently we have not created any LV inside this VG so its totally free.
  10. UUID of this volume group.

Creating Logical Volumes

9. Now, ceate 3 Logical Volumes in the name of tecmint_documents, tecmint_manager and tecmint_public. Here, we can see how to Create Logical Volumes Using PE size and Using GB Size. First, list the Current Logical Volumes using following command.
# lvs
List Current Volume Groups
List Current Volume Groups
10. These Logical Volumes are in vg_tecmint Volume Group. List and see how much free spaces are there to create logical volumes using pvs command.
# pvs
Check Free Space
Check Free Space
11. Volume group size is 54GB and its unused, So we can Create LV in it. Let us divide volume group to equal size to create 3 Logical Volumes. That means 54GB/3 = 18GB, A single Logical Volume will be 18GB in Size after Creation.
Method 1: Creating Logical Volumes using PE Size’s
First let us create Logical Volumes Using Physical Extends (PE) size. We need to know Default PE size assigned for this Volume Group and Total PE available to create new Logical Volumes, Run the command to get the info using.
# vgdisplay tecmint_add_vg
Create New Logical Volume
Create New Logical Volume
  1. Default PE Assigned for this VG is 32MB, Here Single PE size will be 32MB.
  2. Total Available PE is 1725.
Just do and see a little Calculation using bc command.
# bc
1725PE/3 = 575 PE. 
575 PE x 32MB = 18400 --> 18GB
Calculate Disk Space
Calculate Disk Space
Press CRTL+D to Exit from bc. Let us now Create 3 Logical Volumes using 575 PE’s.
# lvcreate -l (Extend size) -n (name_of_logical_volume) (volume_group)

# lvcreate -l 575 -n tecmint_documents tecmint_add_vg

# lvcreate -l 575 -n tecmint_manager tecmint_add_vg

# lvcreate -l 575 -n tecmint_public tecmint_add_vg
  1. -l – Creating using Extent Size
  2. -n – Give a Logical Volume name.
List the Created Logical Volumes using lvs command.
# lvs
List Created Logical Volumes
List Created Logical Volumes
Method 2: Creating Logical Volumes using GB Size’s
While Creating Logical Volume using GB size we cannot get the exact size. So, the better way is to create using extend.
# lvcreate -L 18G -n tecmint_documents tecmint_add_vg

# lvcreate -L 18G -n tecmint_manager tecmint_add_vg

# lvcreate -L 18G -n tecmint_public tecmint_add_vg

# lvcreate -L 17.8G -n tecmint_public tecmint_add_vg
List the Created logical Volumes using lvs command.
# lvs
Verify Created Logical Volumes
Verify Created Logical Volumes
Here, we can see while creating 3rd LV we can’t Round-up to 18GB, It is because of small changes in size, But this issue will be ignored while creating LV using Extend size.

Creating File System

12. For using the logical volumes we need to format. Here I am using ext4 file-system to create the volumes and going to mount under /mnt/.
# mkfs.ext4 /dev/tecmint_add_vg/tecmint_documents

# mkfs.ext4 /dev/tecmint_add_vg/tecmint_public

# mkfs.ext4 /dev/tecmint_add_vg/tecmint_manager
Create Ext4 File System
Create Ext4 File System
13. Let us Create Directories in /mnt and Mount the Logical volumes what we have created file-system.
# mount /dev/tecmint_add_vg/tecmint_documents /mnt/tecmint_documents/

# mount /dev/tecmint_add_vg/tecmint_public /mnt/tecmint_public/

# mount /dev/tecmint_add_vg/tecmint_manager /mnt/tecmint_manager/
List and confirm the Mount point using.
 
# df -h
Mount Logical Volumes
Mount Logical Volumes
Permanent Mounting
It’s now temporarily mounted, for permanent mount we need to add the entry in fstab, for that let us get the mount entry from mtab using
# cat /etc/mtab
We need to make slight changes in fstab entry while entering the mount entry contents copies from mtab, we need to change the rw to defaults
# vim /etc/fstab
Our fstab Entry want to be similar to below sample. Save and exit from fstab using wq!.
/dev/mapper/tecmint_add_vg-tecmint_documents    /mnt/tecmint_documents  ext4    defaults 0 0
/dev/mapper/tecmint_add_vg-tecmint_public       /mnt/tecmint_public     ext4    defaults 0 0
/dev/mapper/tecmint_add_vg-tecmint_manager      /mnt/tecmint_manager    ext4    defaults 0 0
Get mtab Mount Entry
Get mtab Mount Entry
Open fstab File
Open fstab File
Add Auto Mount Entry
Add Auto Mount Entry
Execute the command mount -a to check for the fstab entry before restart.
# mount -av
Verify fstab Entry
Verify fstab Entry
Here we have seen how to setup flexible storage with logical volumes by using physical disk to physical volume, physical volume to volume group, volume group to logical volumes.


Now, we are going to see how to extend volume group, extend and reduce a logical volume. Here we can reduce or extend the partitions in Logical volume management (LVM) also called as flexible volume file-system.
Extend/Reduce LVMs in Linux
Extend/Reduce LVMs in Linux
When do we need to reduce volume?
May be we need to create a separate partition for any other use or we need to expand the size of any low space partition, if so we can reduce the large size partition and we can expand the low space partition very easily by the following simple easy steps.
My Server Setup – Requirements
  1. Operating System – CentOS 6.5 with LVM Installation
  2. Server IP – 192.168.0.200

How to Extend Volume Group and Reduce Logical Volume

Logical Volume Extending

Currently, we have One PV, VG and 2 LV. Let’s list them one by one using following commands.
# pvs
# vgs
# lvs
Logical Volume Extending
Logical Volume Extending
There are no free space available in Physical Volume and Volume group. So, now we can’t extend the lvm size, for extending we need to add one physical volume (PV), and then we have to extend the volume group by extending the vg. We will get enough space to extend the Logical volume size. So first we are going to add one physical volume.
For adding a new PV we have to use fdisk to create the LVM partition.
# fdisk -cu /dev/sda
  1. To Create new partition Press n.
  2. Choose primary partition use p.
  3. Choose which number of partition to be selected to create the primary partition.
  4. Press 1 if any other disk available.
  5. Change the type using t.
  6. Type 8e to change the partition type to Linux LVM.
  7. Use p to print the create partition ( here we have not used the option).
  8. Press w to write the changes.
Restart the system once completed.
Create LVM Partition
Create LVM Partition
List and check the partition we have created using fdisk.
# fdisk -l /dev/sda
Verify LVM Partition
Verify LVM Partition
Next, create new PV (Physical Volume) using following command.
# pvcreate /dev/sda1
Verify the pv using below command.
# pvs
Create Physical Volume
Create Physical Volume

Extending Volume Group

Add this pv to vg_tecmint vg to extend the size of a volume group to get more space for expanding lv.
# vgextend vg_tecmint /dev/sda1
Let us check the size of a Volume Group now using.
# vgs
Extend Volume Group
Extend Volume Group
We can even see which PV are used to create particular Volume group using.
# pvscan
Check Volume Group
Check Volume Group
Here, we can see which Volume groups are under Which Physical Volumes. We have just added one pv and its totally free. Let us see the size of each logical volume we have currently before expanding it.
Check All Logical Volume
Check All Logical Volume
  1. LogVol00 defined for Swap.
  2. LogVol01 defined for /.
  3. Now we have 16.50 GB size for / (root).
  4. Currently there are 4226 Physical Extend (PE) available.
Now we are going to expand the / partition LogVol01. After expanding we can list out the size as above for confirmation. We can extend using GB or PE as I have explained it in LVM PART-I, here I’m using PE to extend.
For getting the available Physical Extend size run.
# vgdisplay
Check Available Physical Size
Check Available Physical Size
There are 4607 free PE available = 18GB Free space available. So we can expand our logical volume up-to 18GB more. Let us use the PE size to extend.
# lvextend -l +4607 /dev/vg_tecmint/LogVol01
Use + to add the more space. After Extending, we need to re-size the file-system using.
# resize2fs /dev/vg_tecmint/LogVol01
Expand Logical Volume
Expand Logical Volume
  1. Command used to extend the logical volume using Physical extends.
  2. Here we can see it is extended to 34GB from 16.51GB.
  3. Re-size the file system, If the file-system is mounted and currently under use.
  4. For extending Logical volumes we don’t need to unmount the file-system.
Now let’s see the size of re-sized logical volume using.
# lvdisplay
Resize Logical Volume
Resize Logical Volume
  1. LogVol01 defined for / extended volume.
  2. After extending there is 34.50GB from 16.50GB.
  3. Current extends, Before extending there was 4226, we have added 4607 extends to expand so totally there are 8833.
Now if we check the vg available Free PE it will be 0.
# vgdisplay
See the result of extending.
# pvs
# vgs
# lvs
Verify Resize Partition
Verify Resize Partition
  1. New Physical Volume added.
  2. Volume group vg_tecmint extended from 17.51GB to 35.50GB.
  3. Logical volume LogVol01 extended from 16.51GB to 34.50GB.
Here we have completed the process of extending volume group and logical volumes. Let us move towards some interesting part in Logical volume management.

Reducing Logical Volume (LVM)

Here we are going to see how to reduce the Logical Volumes. Everyone say its critical and may end up with disaster while we reduce the lvm. Reducing lvm is really interesting than any other part in Logical volume management.
  1. Before starting, it is always good to backup the data, so that it will not be a headache if something goes wrong.
  2. To Reduce a logical volume there are 5 steps needed to be done very carefully.
  3. While extending a volume we can extend it while the volume under mount status (online), but for reduce we must need to unmount the file system before reducing.
Let’s wee what are the 5 steps below.
  1. unmount the file system for reducing.
  2. Check the file system after unmount.
  3. Reduce the file system.
  4. Reduce the Logical Volume size than Current size.
  5. Recheck the file system for error.
  6. Remount the file-system back to stage.
For demonstration, I have created separate volume group and logical volume. Here, I’m going to reduce the logical volume tecmint_reduce_test. Now its 18GB in size. We need to reduce it to 10GB without data-loss. That means we need to reduce 8GB out of 18GB. Already there is 4GB data in the volume.
18GB ---> 10GB
While reducing size, we need to reduce only 8GB so it will roundup to 10GB after the reduce.
# lvs
Reduce Logical Volume
Reduce Logical Volume
Here we can see the file-system information.
# df -h
Check File System Size
Check File System Size
  1. The size of the Volume is 18GB.
  2. Already it used upto 3.9GB.
  3. Available Space is 13GB.
First unmount the mount point.
# umount -v /mnt/tecmint_reduce_test/
Unmount Parition
Unmount Parition
Then check for the file-system error using following command.
# e2fsck -ff /dev/vg_tecmint_extra/tecmint_reduce_test
Scan Parition for Errors
Scan Parition for Errors
Note: Must pass in every 5 steps of file-system check if not there might be some issue with your file-system.
Next, reduce the file-system.
# resize2fs /dev/vg_tecmint_extra/tecmint_reduce_test 10GB
Reduce File System
Reduce File System
Reduce the Logical volume using GB size.
# lvreduce -L -8G /dev/vg_tecmint_extra/tecmint_reduce_test
Reduce Logical Partition
Reduce Logical Partition
To Reduce Logical volume using PE Size we need to Know the size of default PE size and total PE size of a Volume Group to put a small calculation for accurate Reduce size.
# lvdisplay vg_tecmint_extra
Here we need to do a little calculation to get the PE size of 10GB using bc command.
1024MB x 10GB = 10240MB or 10GB

10240MB / 4PE = 2048PE
Press CRTL+D to exit from BC.
Calculate PE Size
Calculate PE Size
Reduce the size using PE.
# lvreduce -l -2048 /dev/vg_tecmint_extra/tecmint_reduce_test
Reduce Size Using PE
Reduce Size Using PE
Re-size the file-system back, In this step if there is any error that means we have messed-up our file-system.
# resize2fs /dev/vg_tecmint_extra/tecmint_reduce_test
Resize File System
Resize File System
Mount the file-system back to same point.
# mount /dev/vg_tecmint_extra/tecmint_reduce_test /mnt/tecmint_reduce_test/
Mount File System
Mount File System
Check the size of partition and files.
# lvdisplay vg_tecmint_extra
Here we can see the final result as the logical volume was reduced to 10GB size.
Verify Logical Volume Size
Verify Logical Volume Size
In this article, we have seen how to extend the volume group, logical volume and reduce the logical volume. In the next part (Part III), we will see how to take a Snapshot of logical volume and restore it to earlier stage.

Comentários

Postagens mais visitadas deste blog

Upgrading Iomega ix2-200 to Cloud Edition

You just got your ix2-200 from eBay and there are no disks inside the NAS. Or you have a brand new ix2-200 -yet you could not afford Cloud Edition. No problem. With just a USB stick and a SATA adapter or desktop PC, you will easily upgrade your ix2-200 to ix2-200 Cloud Edition. Not only your ix2-200 will have a brand new interface and Cloud options, but also will become Mac OS X Lion compatible! What do we need? Decrypted! ix2-200 Cloud Edition Firmware 3.1.12.47838 S endSpace or RapidShare * USB Flash Drive with at least 2 GB capacity and LED indicator** SATA to USB adapter or desktop PC Toothpick or paperclip Preparing Hard Drives Preparing hard drives is the first step because you have to wipe all the data inside the hard drives and make them just like brand new. We used 2 x Seagate 2 TB 5900 RPM Drives. Backup any files if you have and then remove both disks from ix2-200 and attach them to SATA to USB adapter or your desktop PC's SATA port. Using

Cuckoo com Vmware Esxi

Cuckoo is an open-source malware analysis platform using sandboxing technology. The tool allows people like us to analyze malicious binaries in an isolated environment. Since Cuckoo is commonly used with Oracle VirtualBox as its virtualization platform, a majority of online documentation is focused on configuration using VirtualBox. PlantainStan and I decided to test running Cuckoo on ESXi and document our success. This guide will help with the basic configuration of ensuring Cuckoo properly interacts with ESXi. We will continue to update this post as we make continue to make an even more baller Cuckoo environment! Note: In order to successfully interact with vSphere's API, you will need the VMWare ESX Standard license. API functionality is required for Cuckoo to work with ESX. Configure ESX Since this guide is not a "how to" on installing ESXi, we will assume that you have successfully installed the hypervisor on your system. There

CentOS7 with Snort Barnyard2 Snorby PulledPork SElinux

This post is about how to install Snort "stack" on CentOS7 with potentially all the latest libs an stuff. Here I will install and configure everything to run Snort as IDS. I will write another post shortly how to run it as IPS - INLINE. System details: [ root@nfsec-ids-01 ~ ] # cat /etc/redhat-release CentOS Linux release 7.3.1611 ( Core ) [ root@nfsec-ids-01 ~ ] # uname -a Linux nfsec-ids-01.nfsec.co.uk 3.10.0-514.2.2.el7.x86_64 #1 SMP Tue Dec 6 23:06:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux Snort 2.9.9.0 Installation of snort is very basic: yum install https://www.snort.org/downloads/snort/daq-2.0.6-1.centos7.x86_64.rpm yum install https://www.snort.org/downloads/snort/snort-2.9.9.0-1.centos7.x86_64.rpm Register at Snort and download registered rule set: mkdir /usr/local/src/snortrules cd /usr/local/src/snortrules wget https://www.snort.org/rules/snortrules-snapshot-2990.tar.gz?oinkcode = < oinkcode > tar -zxvf snort