Managing LVM

  • Create Logical Volume

Firstly check the free space in your HDD:

[root@sunny ~]# parted
GNU Parted 2.1
Using /dev/vda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print free                                                       
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 16.1GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system     Flags
        32.3kB  1049kB  1016kB            Free Space
 1      1049kB  538MB   537MB   primary   ext4            boot
 2      538MB   4732MB  4194MB  primary   ext4
 3      4732MB  8402MB  3670MB  primary   ext4
 4      8402MB  16.1GB  7704MB  extended
 5      8404MB  9453MB  1049MB  logical   linux-swap(v1)
 6      9454MB  10.5GB  1049MB  logical   ext4
 7      10.5GB  11.6GB  1049MB  logical   ext4
 8      11.6GB  12.6GB  1049MB  logical   ext4
        12.6GB  16.1GB  3504MB            Free Space


It will show you that you have how much free space left in your HDD in my case it is 3540MB.

Now exit from parted prompt.

(parted) q                                                              

Now we need to create new partitions for our volume group.

[root@sunny ~]# fdisk /dev/vda

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
First cylinder (24418-31208, default 24418):
Using default value 24418
Last cylinder, +cylinders or +size{K,M,G} (24418-31208, default 31208): +100M

Command (m for help): n
First cylinder (24622-31208, default 24622):
Using default value 24622
Last cylinder, +cylinders or +size{K,M,G} (24622-31208, default 31208): +100M

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

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

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@sunny ~]#

Now you need to reboot your machine or fire a command  partx -a /dev/vda two times so that the kernel is aware that there are two new partitions.

[root@sunny ~]# partx -a /dev/vda

Now we need to create physical volume for both the partitions which you created.

[root@sunny ~]# pvcreate /dev/vda9 /dev/vda10
  Physical volume "/dev/vda9" successfully created
  Physical volume "/dev/vda10" successfully created

Now we need to create a volume group containing both the partitions, in this case the volume group name is redhatvg.

[root@sunny ~]# vgcreate redhatvg /dev/vda9 /dev/vda10
  Volume group "redhatvg" successfully created

Now  we need to create logical volume, in this the size of logical volume is 50MB with name redhatlv in redhatvg volume group.

[root@sunny ~]# lvcreate -L 50M -n redhatlv redhatvg
  Rounding up size to full physical extent 52.00 MiB
  Logical volume "redhatlv" created

Now  we need to format the logical volume which we just created.

[root@sunny ~]# mkfs.ext4 /dev/redhatvg/redhatlv
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
13328 inodes, 53248 blocks
2662 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=54525952
7 block groups
8192 blocks per group, 8192 fragments per group
1904 inodes per group
Superblock backups stored on blocks:
    8193, 24577, 40961

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

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

Now create a directory on which you need to mount that partition.

[root@sunny ~]# mkdir /LV_HDD

Now mount the newly created logical volume to the directory which you created.

[root@sunny ~]# mount /dev/redhatvg/redhatlv /LV_HDD/

Now make a entry which is given below to /etc/fstab file.
/dev/mapper/redhatvg-redhatlv /LV_HDD ext4 rw 0 0

[root@sunny ~]# vi /etc/fstab 

df -h will show you your mounted partition.

[root@sunny ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/vda2             3.9G  266M  3.4G   8% /
tmpfs                 805M  272K  805M   1% /dev/shm
/dev/vda1             504M   37M  443M   8% /boot
/dev/vda6             985M   18M  918M   2% /home
/dev/vda7             985M   18M  917M   2% /tmp
/dev/vda3             3.4G  2.0G  1.3G  62% /usr
/dev/vda8             985M  100M  835M  11% /var
/dev/mapper/redhatvg-redhatlv
                       51M  4.9M   43M  11% /LV_HDD

  • Extending logical volume

Firstly check the size of your present logical volume.

[root@sunny ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/vda2             3.9G  266M  3.4G   8% /
tmpfs                 805M  272K  805M   1% /dev/shm
/dev/vda1             504M   37M  443M   8% /boot
/dev/vda6             985M   18M  918M   2% /home
/dev/vda7             985M   18M  917M   2% /tmp
/dev/vda3             3.4G  2.0G  1.3G  62% /usr
/dev/vda8             985M  100M  835M  11% /var
/dev/mapper/redhatvg-redhatlv
                       51M  4.9M   43M  11% /LV_HDD

Firstly unmount your mounted partition.

[root@sunny ~]# umount /LV_HDD/

Now fire a command for checking and repair your file-system  forcefully.

[root@sunny ~]# e2fsck  -f /dev/redhatvg/redhatlv
e2fsck 1.41.12 (17-May-2010)
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/redhatvg/redhatlv: 11/13328 files (9.1% non-contiguous), 6627/53248 blocks

Now we will extend our present logical volume from 50MB to 80MB.

[root@sunny ~]# lvextend -L 80M /dev/redhatvg/redhatlv
  Extending logical volume redhatlv to 80.00 MiB
  Logical volume redhatlv successfully resized

Now we need to resize our partition via resize2fs (this will resize ext2, ext3, or ext4 file systems. It can be used to enlarge or shrink an unmounted file system located on device).
 
[root@sunny ~]# resize2fs -f /dev/redhatvg/redhatlv
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/redhatvg/redhatlv to 81920 (1k) blocks.
The filesystem on /dev/redhatvg/redhatlv is now 81920 blocks long.

Now we need to mount the partition again.

[root@sunny ~]# mount /dev/redhatvg/redhatlv /LV_HDD/

Now check the size of the partition which you just expanded.

[root@sunny ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/vda2             3.9G  266M  3.4G   8% /
tmpfs                 805M  272K  805M   1% /dev/shm
/dev/vda1             504M   37M  443M   8% /boot
/dev/vda6             985M   18M  918M   2% /home
/dev/vda7             985M   18M  917M   2% /tmp
/dev/vda3             3.4G  2.0G  1.3G  62% /usr
/dev/vda8             985M  100M  835M  11% /var
/dev/mapper/redhatvg-redhatlv
                       78M  5.3M   69M   8% /LV_HDD
[root@sunny ~]#

  • Reducing logical volume

Currently the size of your logical volume (redhatlv) is 80MB and now we will reduce it by 40MB.

[root@sunny ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/vda2             3.9G  266M  3.4G   8% /
tmpfs                 805M  272K  805M   1% /dev/shm
/dev/vda1             504M   37M  443M   8% /boot
/dev/vda6             985M   18M  918M   2% /home
/dev/vda7             985M   18M  917M   2% /tmp
/dev/vda3             3.4G  2.0G  1.3G  62% /usr
/dev/vda8             985M  100M  835M  11% /var
/dev/mapper/redhatvg-redhatlv
                       78M  5.3M   69M   8% /LV_HDD

Firstly unmount the mounted partition.

[root@sunny ~]# umount /LV_HDD/

Now fire a command for checking and repair your file-system  forcefully.

[root@sunny ~]# e2fsck -f /dev/redhatvg/redhatlv
e2fsck 1.41.12 (17-May-2010)
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/redhatvg/redhatlv: 11/19040 files (9.1% non-contiguous), 7765/81920 blocks

Now we need to resize our partition via resize2fs (this will resize ext2, ext3, or ext4 file systems. It can be used to enlarge or shrink an unmounted file system located on device).

[root@sunny ~]# resize2fs -f /dev/redhatvg/redhatlv 40M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/redhatvg/redhatlv to 40960 (1k) blocks.
The filesystem on /dev/redhatvg/redhatlv is now 40960 blocks long.

Now we will reduce our present logical volume from 80MB to 40MB.

[root@sunny ~]# lvreduce -L 40M /dev/redhatvg/redhatlv
  WARNING: Reducing active logical volume to 40.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce redhatlv? [y/n]: y
  Reducing logical volume redhatlv to 40.00 MiB
  Logical volume redhatlv successfully resized

Now mount this partition again.

[root@sunny ~]# mount /dev/redhatvg/redhatlv /LV_HDD/

Check the size of your mounted partition (/dev/mapper/redhatvg-redhatlv 39M  4.7M   33M  13% /LV_HDD)

[root@sunny ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/vda2             3.9G  266M  3.4G   8% /
tmpfs                 805M  272K  805M   1% /dev/shm
/dev/vda1             504M   37M  443M   8% /boot
/dev/vda6             985M   18M  918M   2% /home
/dev/vda7             985M   18M  917M   2% /tmp
/dev/vda3             3.4G  2.0G  1.3G  62% /usr
/dev/vda8             985M  100M  835M  11% /var
/dev/mapper/redhatvg-redhatlv
                       39M  4.7M   33M  13% /LV_HDD
[root@sunny ~]#

  • Removing a logical volume
Check if the partition is mounted or not ?

[root@sunny ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/vda2             3.9G  266M  3.4G   8% /
tmpfs                 805M  272K  805M   1% /dev/shm
/dev/vda1             504M   37M  443M   8% /boot
/dev/vda6             985M   18M  918M   2% /home
/dev/vda7             985M   18M  917M   2% /tmp
/dev/vda3             3.4G  2.0G  1.3G  62% /usr
/dev/vda8             985M  100M  835M  11% /var
/dev/mapper/redhatvg-redhatlv
                        39M  4.7M   33M  13% /LV_HDD

Check which logical volume you need to remove, fire a command lvdisplay it will show you all the logical volumes present in your HDD.

[root@sunny ~]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/redhatvg/redhatlv
  VG Name                redhatvg
  LV UUID                bDS0d3-677K-Kmok-LiT7-4M5z-VBji-TcGgbq
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                40.00 MiB
  Current LE             10
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
  
Unmount the partition which are being used.

[root@sunny ~]# umount /LV_HDD/

Now fire a command to remove the specified logical volume.

[root@sunny ~]# lvremove /dev/redhatvg/redhatlv
Do you really want to remove active logical volume redhatlv? [y/n]: y
  Logical volume "redhatlv" successfully removed

Now check whether the specified logical volume have been removed or not.

[root@sunny ~]# lvdisplay
[root@sunny ~]# <No result as there is no logical volume now>

  •  Removing a volume group
Firstly remove the logical volume via above steps then check for exact volume group names which are there in your HDD.

[root@sunny ~]# vgdisplay
  --- Volume group ---
  VG Name               redhatvg
  System ID            
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               192.00 MiB
  PE Size               4.00 MiB
  Total PE              48
  Alloc PE / Size       0 / 0  
  Free  PE / Size       48 / 192.00 MiB
  VG UUID               Y2z30G-uRNe-ZZ5t-eHjA-uTA5-p3CT-4zpvf1
  
Now fire a command vgremove followed by redhatvg (the volume group name).

[root@sunny ~]# vgremove redhatvg
   Volume group "redhatvg" successfully removed

Now we need to remove physical volume which we created.

[root@sunny ~]# pvremove /dev/vda9 /dev/vda10
  Labels on physical volume "/dev/vda9" successfully wiped
  Labels on physical volume "/dev/vda10" successfully wiped

Now remove the entry from /etc/fstab file.
/dev/mapper/redhatvg-redhatlv /LV_HDD ext4 rw 0 0

[root@sunny ~]# vi /etc/fstab

Comments

Popular posts from this blog

Installing Tomcat8 on RHEL6

Manage existing resources via Helm

Configuration and setting-up of AIDE on RHEL6