April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Categories

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Managing Physical and Logical Volumes

Contents

    Introduction
    Create Logical Volumes
    Delete Logical Volumes

Introduction

The following article presents an overview of the commands used to manage physical and logical volumes for use with Logical Volume Manager (LVM) in Linux.

Before considering the various commands for LVM, lets first look at just what is meant by some of the terminology of LVM.

A logical volume lives in a volume group that is made up of one or more physical volumes. All volume groups are part of the Logical Volume Manager. Here is a table that lists the three types of Logical Volume’s.

Logical Volume Types Logical Volume     Physical Device     File System
volume group     one or more disks     vgdisplay
physical volume group     physical extents on a drive     pvdisplay
logical volume group     multiple physical volume groups, one or more disks     lvdisplay

The following table provides an overview of some of the commands used in LVM and the functions they service.

LVM Commands Command     LVM Function
vgcreate     Create a Volume Group. (Create a subset of the overall LVM)
pvcreate     Create a Physical Volume, assign to Volume Group. (Specify a disk for inclusion in the overall LVM)
vgextend     Add a new physical disk to a volume group.
lvcreate     Create a Logical Volume. (Storage area for related files that is part of a Volume Group. A Volume Group consists of many logical volumes)
lvextend     Increase the size of a Logical Volume.
lvreduce     Decrease the size of a Logical Volume.
lvremove     Removes a Logical Volume. (Frees the storage area set aside for a logical volume)
vgreduce     Reduce a Volume Group. (Reduces the number of disks in a Volume Group)
vgremove     Remove a Volume Group. (Removes the designation of a group of disks as a Volume Group)
vgdisplay     Volume Group Display. (Displays information about one or more Volume Groups)
pvdisplay     Physical Volume Display. (Displays information about one or more Volume Groups)
lvdisplay     Logical Volume Display. (Displays information about one or more Logical Volumes)
Create Logical Volumes

The following subsections describe the commands used for creating logical volumes.
Partition Types

Before using a hard disk as a physical volume, decide if the physical volume will use the entire disk (/dev/sdc) or a disk partition (/dev/sdc1).

To create the physical volume using a partition, set the partition type to 0x8e (Linux LVM) using fdisk or some other similar program.

Each logical volume in this section will use the entire hard disk. This requires that no partition table exists on the disk. When using the whole disk, the partition table must be erased, which will effectively destroy all data on that disk. An existing partition table can be removed by zeroing the first sector on the disk using the dd command.

      

The following commands will destroy the partition table on the disk being operated on. Be very sure it is the correct disk before continuing.

[root@testnode1 ~]# dd if=/dev/zero of=/dev/sdc bs=512 count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.00379734 seconds, 135 kB/s

[root@testnode1 ~]# dd if=/dev/zero of=/dev/sdd bs=512 count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.00200099 seconds, 256 kB/s

Verify that the partition table has been removed from both hard disks.

[root@testnode1 ~]# fdisk -l | grep ‘^Disk \/dev\/sd[cd][^:]’
Disk /dev/sdc doesn’t contain a valid partition table
Disk /dev/sdd doesn’t contain a valid partition table
Disk /dev/dm-0 doesn’t contain a valid partition table
Disk /dev/dm-1 doesn’t contain a valid partition table

Initialize Physical Volumes

Use the pvcreate command to initialize a block device to be used as a physical volume. The following commands will initialize the whole disk for each hard disk.

[root@testnode1 ~]# pvcreate /dev/sdc
  Physical volume “/dev/sdc” successfully created

[root@testnode1 ~]# pvcreate /dev/sdd
  Physical volume “/dev/sdd” successfully created

      

When using a partition, run pvcreate on the partition.

[root@testnode1 ~]# pvcreate /dev/sdc1
  Physical volume “/dev/sdc1” successfully created

[root@testnode1 ~]# pvcreate /dev/sdd1
  Physical volume “/dev/sdd1” successfully created

This creates a volume group descriptor at the start of the /dev/sdc1 and /dev/sdd1 partition.

Use the lvmdiskscan command to scan for block devices and verify that the two hard disks can be used as physical volumes.

[root@testnode1 ~]# lvmdiskscan | grep ‘\/dev\/sd[cd]’
  /dev/sdc                           [       36.00 GB] LVM physical volume
  /dev/sdd                           [       36.00 GB] LVM physical volume

Create Volume Group

Use the vgcreate command to create a volume group from one or more physical volumes. The vgcreate command creates a new volume group by name (vg_oradata and vg_orafra for example) and adds at least one physical volume to it. Create a new volume group on each hard disk.

[root@testnode1 ~]# vgcreate vg_oradata /dev/sdc
  Volume group “vg_oradata” successfully created

[root@testnode1 ~]# vgcreate vg_orafra /dev/sdd
  Volume group “vg_orafra” successfully created

This creates a volume group descriptor at the start of each disk. When using partitions, run the vgcreate command on the partition (for example, vgcreate vg_oradata /dev/sdc1) which will create a volume group descriptor at the start of the partition.
Create Logical Volumes

With the new volume groups in place, use the lvcreate command to create the appropriate logical volumes (lv_oradata and lv_orafra). Logical volumes can be created as linear volumes, striped volumes, and mirrored volumes. For the purpose of this example, create a single linear volume within each of the volume groups that uses all of the unallocated space within the volume group.

[root@testnode1 ~]# lvcreate -l 100%FREE -n lv_oradata vg_oradata
  Logical volume “lv_oradata” created

[root@testnode1 ~]# lvcreate -l 100%FREE -n lv_orafra vg_orafra
  Logical volume “lv_orafra” created

Verify LVM Configuration

Use the vgscan and lvscan commands to verify the new volume groups and logical volumes respectively.

[root@testnode1 ~]# vgscan
  Reading all physical volumes.  This may take a while…
  Found volume group “vg_orafra” using metadata type lvm2
  Found volume group “vg_oradata” using metadata type lvm2
  Found volume group “vg_orasoftware” using metadata type lvm2
  Found volume group “VolGroup00” using metadata type lvm2

[root@testnode1 ~]# lvscan
  ACTIVE            ‘/dev/vg_orafra/lv_orafra’ [36.00 GB] inherit
  ACTIVE            ‘/dev/vg_oradata/lv_oradata’ [36.00 GB] inherit
  ACTIVE            ‘/dev/vg_orasoftware/lv_orasoftware’ [35.97 GB] inherit
  ACTIVE            ‘/dev/VolGroup00/LogVol00’ [30.97 GB] inherit
  ACTIVE            ‘/dev/VolGroup00/LogVol01’ [4.91 GB] inherit

LVM volume groups and underlying logical volumes are included in the device special file directory tree in the /dev directory with the following layout.

  /dev/vg/lv/

For example:

  /dev/vg_oradata/lv_oradata
  /dev/vg_orafra/lv_orafra

Delete Logical Volumes

Before describing how to drop physical and logical volumes, note that it is very common to only want to remove a logical volume from a volume group. For example, if users no longer need the logical volume lv_orafra. The logical volume can be removed and its physical extents placed back in the empty pool for the volume group. First, the file system will need to be unmounted (if it is mounted). Next, deactive it with lvchange and finally delete it with lvremove. Here is an example that removes the lv_orafra.

[root@testnode1 ~]# umount /u04/oracle/fast_recovery_area

[root@testnode1 ~]# lvchange -a n /dev/vg_orafra/lv_orafra

[root@testnode1 ~]# lvremove /dev/vg_orafra/lv_orafra
lvremove — do you really want to remove “/dev/vg_orafra/lv_orafra”? [y/n]: y
lvremove — doing automatic backup of volume group “vg_orafra”
lvremove — logical volume “/dev/vg_orafra/lv_orafra” successfully removed

Use the following to delete all physical and logical volumes created in this guide.

[root@testnode1 ~]# umount /u03/oracle/oradata

[root@testnode1 ~]# lvchange -a n /dev/vg_oradata/lv_oradata
[root@testnode1 ~]# lvremove /dev/vg_oradata/lv_oradata

[root@testnode1 ~]# vgchange -a n /dev/vg_oradata
[root@testnode1 ~]# vgchange -a n /dev/vg_orafra

[root@testnode1 ~]# vgremove /dev/vg_oradata
[root@testnode1 ~]# vgremove /dev/vg_orafra

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>