How to Migrate LVM or Volume Group to New Linux Server

Migrate LVMThis article will describe on how to move or migrate LVM and volume group from one server to another.

If you are working in a production environment then you have to know about the software disk management and all the methods to provide flexibility in storage management.

LVM stand for logical volume management. It is a tool to manage and provides a higher-level view of the disk storage.

Below tutorial will be very useful for system administrators who have problems on the linux operating system and want to mount the mounted partition to other systems.

This should be the most simple recovery strategy if we run Linux in VMware virtual machines.

Migrating LVM or Volume Group to new server should be the fastest way to access the important linux partition that may contains user’s data or important configuration files.

How to Migrate LVM or Volume Group to New Server

1. Add vdisk to new virtual machine.

a) Click “Add” to proceed :

Migrate LVM

b) Select “Hard Disk” device type :

Migrate LVM

c) Please choose “Use and existing virtual disk” :

Migrate LVM

d) Browse to vdisk location :

Migrate LVM

e) Click Next to proceed :

Migrate LVM

f) Click finish :

Migrate LVM

g) Finally click “OK” to complete :

Migrate LVM

2. Once the vdisk as been added to new Linux server, please proceed to rescan the linux VM.

a) Identify host bus number :

# ls /sys/class/scsi_host/
host0  host1  host2

b) Rescan the SCSI Bus to Add a SCSI Devices :

# echo "- - -" > /sys/class/scsi_host/host0/scan
# echo "- - -" > /sys/class/scsi_host/host1/scan
# echo "- - -" > /sys/class/scsi_host/host2/scan

3. Check the new disk. The second vdisk by default will be recognised as /dev/sdb. Since the partition type has been formatted as LVM on the previous system, it will show Id “8e” if you run fdisk command.

# fdisk -l
..
..

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xce931872

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1305    10482381   8e  Linux LVM

There are two method to register and activate the LVM and volume group into new linux server. Proceed to steps 4 – 9 for Method 1. Go to step 10 for Method 2.

4. Use lvscan to scans all known volume groups or all supported LVM block devices in the system for defined Logical Volumes. The output consists of one line for each Logical Volume indicating whether it is active or inactive :

# lvscan
  inactive          '/dev/vg_newlvm/newvol' [9.99 GiB] inherit
  ACTIVE            '/dev/vg_centos66/lv_root' [30.00 GiB] inherit
  ACTIVE            '/dev/vg_centos66/lv_swap' [8.00 GiB] inherit

5. Check existing volume group and logical volume:

# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "vg_newlvm" using metadata type lvm2
  Found volume group "vg_centos66" using metadata type lvm2

One of the logical volume in “vg_newlvm” volume group still inactive status.

# lvscan
inactive '/dev/vg_newlvm/newvol' [9.99 GiB] inherit
ACTIVE '/dev/vg_centos66/lv_root' [17.51 GiB] inherit
ACTIVE '/dev/vg_centos66/lv_swap' [2.00 GiB] inherit

6. Export the volume group “vg_newlvm” :

# vgexport vg_newlvm
Volume group "vg_newlvm" successfully exported

7. Then import volume group “vg_newlvm”:

# vgimport vg_newlvm
Volume group "vg_newlvm" successfully imported

8. To activate volume groups “vg_newlvm”, run the following command :

# vgchange -ay vg_newlvm
1 logical volume(s) in volume group "vg_newlvm" now active

9. Check again updated logical volume and volume group status :

# lvscan
  ACTIVE            '/dev/vg_newlvm/newvol' [9.99 GiB] inherit
  ACTIVE            '/dev/vg_centos66/lv_root' [17.51 GiB] inherit
  ACTIVE            '/dev/vg_centos66/lv_swap' [2.00 GiB] inherit

Method 2 :

10. The fastest method to activate the migrated LVM or Volume Group are as below :

# lvscan
inactive '/dev/vg_newlvm/newvol' [9.99 GiB] inherit
ACTIVE '/dev/vg_centos66/lv_root' [17.51 GiB] inherit
ACTIVE '/dev/vg_centos66/lv_swap' [2.00 GiB] inherit

Use lvchange command to activate the LVM.

# lvchange -ay /dev/vg_newlvm/newvol
# lvscan
ACTIVE '/dev/vg_newlvm/newvol' [9.99 GiB] inherit
ACTIVE '/dev/vg_centos66/lv_root' [17.51 GiB] inherit
ACTIVE '/dev/vg_centos66/lv_swap' [2.00 GiB] inherit

11. Create /data folder :

# mkdir -p /data

12. Mount the migrated LVM to /data

# mount /dev/vg_newlvm/newvol /data

13. Verify /date to use migrated LVM :

# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos66-lv_root
                       18G  5.2G   12G  32% /
tmpfs                 939M     0  939M   0% /dev/shm
/dev/sda1             477M   67M  385M  15% /boot
/dev/mapper/vg_newlvm-newvol
                      9.8G   32M  9.2G   1% /data

How to Create LVM in Linux CentOS 7 / RHEL 7 / Oracle Linux 7

Storage technology plays a important role in improving the availability, performance, and ability to manage Linux servers.

One of the most useful and helpful technology to linux system administrator is Linux Logical Volume Manager(LVM), version 2 (or LVM 2).

LVM is a widely-used technique and extremely flexible disk management scheme for deploying logical rather than physical storage. With LVM, system administrator can easily resize and extend the logical drive when it is required.

Create Lvm

The following steps will describe how to create LVM in Linux CentOS 7 or RHEL 7 or Oracle Linux 7.

How to Create Lvm in Linux Step by Step

1. Add the new 20GB vdisk from the ESXi or vCenter :

Create Lvm

2. create a new Partiton using fdisk tool and select partition type LVM :

[root@centos7 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xfd3bf27d.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xfd3bf27d

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   83  Linux

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): L

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

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xfd3bf27d

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   8e  Linux LVM

3. Initializes the partition /dev/sdb1 as an LVM physical volume :

[root@centos7 ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created

4. Scanning for Block Devices

[root@centos7 ~]# lvmdiskscan
  /dev/centos/swap [       2.00 GiB]
  /dev/sda1        [     500.00 MiB]
  /dev/centos/root [      27.51 GiB]
  /dev/sda2        [      29.51 GiB] LVM physical volume
  /dev/sdb1        [      20.00 GiB] LVM physical volume
  2 disks
  1 partition
  0 LVM physical volume whole disks
  2 LVM physical volumes

5. Displaying Physical Volumes :

There are three commands you can use to display properties of LVM physical volumes: pvs,
pvdisplay, and pvscan.

The pvdisplay command provides a verbose multi-line output for each physical volume. It displays
physical properties (size, extents, volume group, etc.) in a fixed format.

[root@centos7 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               29.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              7554
  Free PE               0
  Allocated PE          7554
  PV UUID               JvDOto-KDiF-gtca-TveX-ne9M-frsB-qsP1aJ

  "/dev/sdb1" is a new physical volume of "20.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name
  PV Size               20.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               rJ8wl7-xzIN-2qqV-ov7Z-lHKe-ELge-aAV29V

The pvscan command scans all supported LVM block devices in the system for physical volumes

[root@centos7 ~]# pvscan
  PV /dev/sda2   VG centos          lvm2 [29.51 GiB / 0    free]
  PV /dev/sdb1                      lvm2 [20.00 GiB]
  Total: 2 [49.51 GiB] / in use: 1 [29.51 GiB] / in no VG: 1 [20.00 GiB]

6. Create volume group name vg_newlvm and add /dev/sdb1 partition into the group.

[root@centos7 ~]# vgcreate vg_newlvm /dev/sdb1
  Volume group "vg_newlvm" successfully created

If you have more than one partition, you can add multiple partition in single command. This command creates a local volume named vg_newlvm that contains physical volumes /dev/sdb1 and /dev/sdc1 :

[root@centos7 ~]# vgcreate vg_newlvm /dev/sdb1 /dev/sdc1

7. Creates a logical volume called centos7_newvol that uses all of the unallocated space in the volume group vg_newlvm :

[root@centos7 ~]# lvcreate --name centos7_newvol -l 100%FREE vg_newlvm
  Logical volume "centos7_newvol" created

You can see more example of lvcreate command in article “4 lvcreate Command Examples on Linux

8. Display the created logical volumes :

[root@centos7 ~]# lvdisplay
..
..
  --- Logical volume ---
  LV Path                /dev/vg_newlvm/centos7_newvol
  LV Name                centos7_newvol
  VG Name                vg_newlvm
  LV UUID                szlkNP-0lwe-f59Z-PJVU-X7pG-unBL-qN10D4
  LV Write Access        read/write
  LV Creation host, time centos7.ehowstuff.local, 2015-01-25 15:15:48 +0800
  LV Status              available
  # open                 0
  LV Size                20.00 GiB
  Current LE             5119
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

9. Use the mkfs command to format a newly created LVM :

[root@centos7 ~]# mkfs.ext4 /dev/vg_newlvm/centos7_newvol
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5241856 blocks
262092 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2153775104
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

10. Create the mount point and mount the new LVM :

[root@centos7 ~]# mkdir -p /data
[root@centos7 ~]# mount /dev/vg_newlvm/centos7_newvol /data

11. Verify thew new disk layout :

[root@centos7 ~]# df
Filesystem                           1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos-root               28260132 9191032  17610516  35% /
devtmpfs                               1935888       0   1935888   0% /dev
tmpfs                                  1941892       0   1941892   0% /dev/shm
tmpfs                                  1941892    8728   1933164   1% /run
tmpfs                                  1941892       0   1941892   0% /sys/fs/cgroup
/dev/sda1                               487634   73191    384747  16% /boot
tmpfs                                  1941892    8728   1933164   1% /var/named/chroot/run/named
/dev/mapper/vg_newlvm-centos7_newvol  20507216   45080  19397384   1% /data

How to Add a New Hard Disk Without Rebooting on CentOS 7/ RHEL 7

Normally when you have added new storage to a running Virtual machine, you probably won’t see the new storage automatically.

This is because the SCSI bus to which the storage devices are connected needs to be rescanned to make the new hardware visible.

As a system administrator, to maintain the linux server uptime is very important and it is better to add a new hard disk without reboot the server.

There is one simple command to rescan the SCSI Bus and add SCSI Devices. The command will trigger the linux rescan scsi bus device on the system.

The following steps has been tested on CentOS 7, RHEL 7 and Oracle Linux 7.

Steps to Add a New Hard Disk Without Rebooting on CentOS 7/ RHEL 7

1. Add the new 20GB vdisk from the ESXi or vCenter :

Add a New Hard Disk Without Reboot

2. Show current disk and partition :

[root@centos7 ~]# fdisk -l
Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0006b96a

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    62914559    30944256   8e  Linux LVM

Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-root: 29.5 GB, 29536288768 bytes, 57688064 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

3. Identify host bus number :

[root@centos7 ~]# ls /sys/class/scsi_host/
host0  host1  host2

4. Rescan the SCSI Bus to Add a SCSI Devices :

[root@centos7 ~]# echo "- - -" > /sys/class/scsi_host/host0/scan
[root@centos7 ~]# echo "- - -" > /sys/class/scsi_host/host1/scan
[root@centos7 ~]# echo "- - -" > /sys/class/scsi_host/host2/scan

4. Verify the disk and partiton and make sure 20GB has been added. In this case, the following line appeared “Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors” and confirmed that 20GB has been added without reboot the server :

[root@centos7 ~]# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0006b96a

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    62914559    30944256   8e  Linux LVM

Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-root: 29.5 GB, 29536288768 bytes, 57688064 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

4 lvcreate Command Examples on Linux

Logical volume management (LVM) is a widely-used technique and extremely flexible disk management scheme. It basically contain three basic command :

a. Creates the physical volumes using pvcreate
b. Create the volume group and add partition into volume group using vgcreate
c. Create a new logical volume using lvcreate

lvm-diagram1

The following examples focus on the command to create a logical volume in an existing volume group, lvcreate. lvcreate is the command do allocating logical extents from the free physical extent pool of that volume group. Normally logical volumes use up any space available on the underlying physical volumes on a next-free basis. Modifying the logical volume will frees and reallocates space in the physical volumes. The following lvcreate command has been tested on linux CentOS 5, CentOS 6, CentOS 7, RHEL 5, RHEl 6 and RHEL 7 version.

4 lvcreate Command Examples on Linux :

1. The following command creates a logical volume 15 gigabytes in size in the volume group vg_newlvm :

[root@centos7 ~]# lvcreate -L 15G vg_newlvm

2. The following command creates a 2500 MB linear logical volume named centos7_newvol in the volume group
vg_newlvm, creating the block device /dev/vg_newlvm/centos7_newvol :

[root@centos7 ~]# lvcreate -L 2500 -n centos7_newvol vg_newlvm

3. You can use the -l argument of the lvcreate command to specify the size of the logical volume in extents. You can also use this argument to specify the percentage of the volume group to use for the logical volume. The following command creates a logical volume called centos7_newvol that uses 50% of the total space in volume group vg_newlvm :

[root@centos7 ~]# lvcreate -l 50%VG -n centos7_newvol vg_newlvm

4. The following command creates a logical volume called centos7_newvol that uses all of the unallocated space in the volume group vg_newlvm :

[root@centos7 ~]# lvcreate --name centos7_newvol -l 100%FREE vg_newlvm

To see more lvcreate command options, issue the following command :

[root@centos7 ~]# lvcreate --help
  lvcreate: Create a logical volume

lvcreate
        [-A|--autobackup {y|n}]
        [-a|--activate [a|e|l]{y|n}]
        [--addtag Tag]
        [--alloc AllocationPolicy]
        [--cachemode CacheMode]
        [-C|--contiguous {y|n}]
        [-d|--debug]
        [-h|-?|--help]
        [--ignoremonitoring]
        [--monitor {y|n}]
        [-i|--stripes Stripes [-I|--stripesize StripeSize]]
        [-k|--setactivationskip {y|n}]
        [-K|--ignoreactivationskip]
        {-l|--extents LogicalExtentsNumber[%{VG|PVS|FREE}] |
         -L|--size LogicalVolumeSize[bBsSkKmMgGtTpPeE]}
        [-M|--persistent {y|n}] [--major major] [--minor minor]
        [-m|--mirrors Mirrors [--nosync] [{--mirrorlog {disk|core|mirrored}|--corelog}]]
        [-n|--name LogicalVolumeName]
        [--noudevsync]
        [-p|--permission {r|rw}]
        [--[raid]minrecoveryrate Rate]
        [--[raid]maxrecoveryrate Rate]
        [-r|--readahead ReadAheadSectors|auto|none]
        [-R|--regionsize MirrorLogRegionSize]
        [-T|--thin  [-c|--chunksize  ChunkSize]
          [--discards {ignore|nopassdown|passdown}]
          [--poolmetadatasize MetadataSize[bBsSkKmMgG]]]
          [--poolmetadataspare {y|n}]
        [--thinpool ThinPoolLogicalVolume{Name|Path}]
        [-t|--test]
        [--type VolumeType]
        [-v|--verbose]
        [-W|--wipesignatures {y|n}]
        [-Z|--zero {y|n}]
        [--version]
        VolumeGroupName [PhysicalVolumePath...]

lvcreate
        { {-s|--snapshot} OriginalLogicalVolume[Path] |
          [-s|--snapshot] VolumeGroupName[Path] -V|--virtualsize VirtualSize}
          {-T|--thin} VolumeGroupName[Path][/PoolLogicalVolume]
                      -V|--virtualsize VirtualSize}
        [-c|--chunksize]
        [-A|--autobackup {y|n}]
        [--addtag Tag]
        [--alloc AllocationPolicy]
        [-C|--contiguous {y|n}]
        [-d|--debug]
        [--discards {ignore|nopassdown|passdown}]
        [-h|-?|--help]
        [--ignoremonitoring]
        [--monitor {y|n}]
        [-i|--stripes Stripes [-I|--stripesize StripeSize]]
        [-k|--setactivationskip {y|n}]
        [-K|--ignoreactivationskip]
        {-l|--extents LogicalExtentsNumber[%{VG|FREE|ORIGIN}] |
         -L|--size LogicalVolumeSize[bBsSkKmMgGtTpPeE]}
        [--poolmetadatasize MetadataVolumeSize[bBsSkKmMgG]]
        [-M|--persistent {y|n}] [--major major] [--minor minor]
        [-n|--name LogicalVolumeName]
        [--noudevsync]
        [-p|--permission {r|rw}]
        [-r|--readahead ReadAheadSectors|auto|none]
        [-t|--test]
        [--thinpool ThinPoolLogicalVolume[Path]]
        [-v|--verbose]
        [--version]
        [PhysicalVolumePath...]

How to Remove Volume Group and Physical Volume on LVM

icon-redhatlinuxBasically, adding and removing LVM will always involves these three logical “layers” which are Physical volumes (pv), Volume groups (vg) and Logical volumes (lv). This post assumed that the Logical volumes haven’t created. In certain situation, linux administrators are required to remove the existing Physical Volume or Volume Group only. It is possible to perform the required task if that system administrator has the right skill on linux. The steps as below :

1. Display the physical volumes and Volume Group using pvdisplay command :

[root@centos63 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               vg_data
  PV Size               4.99 GiB / not usable 2.41 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              1278
  Free PE               1278
  Allocated PE          0
  PV UUID               qTmTeq-8qoL-xxEk-Dj5V-zOjl-vEag-P6Jl7L

2. Remove the Volume Group using vgremove command :

[root@centos63 ~]# vgremove vg_data
  Volume group "vg_data" successfully removed

3. Remove Physical Volume using pvremove command :

[root@centos63 ~]# pvremove /dev/sdb1
  Labels on physical volume "/dev/sdb1" successfully wiped

Additional Note :
How to Remove a Logical Volume on CentOS 6.3/RHEL6

How to Remove a Logical Volume on CentOS 6.3/RHEL6

In previous post, i have cover the steps to create additional Logical volume manager (LVM). In certain situation, linux administrator is required to remove the existing logical volume. It is possible to perform the required task if that system administrator has the right skill on linux. Otherwise, they will put themselves and the organization in the disaster or risk as this working with the system’s data. This post assumed centos63_vol has been created and will be remove later. In summary, removing a logical volume (centos63_vol) will involve the following processes:

a) Unmount the LV
b) Remove the LV
c) Update /etc/fstab

1. Begin by listing all the logical volumes on the system :

[root@centos63 ~]# lvs
  LV           VG          Attr     LSize Pool Origin Data%  Move Log Copy%  Convert
  lv_root      vg_centos63 -wi-ao-- 5.54g
  lv_swap      vg_centos63 -wi-ao-- 1.97g
  centos63_vol vg_data     -wi-ao-- 4.99g

2. Check current disk layout and find where centos63_vol is mounted :

[root@centos63 ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos63-lv_root
                      5.5G  2.2G  3.1G  41% /
tmpfs                 504M     0  504M   0% /dev/shm
/dev/sda1             485M   65M  395M  15% /boot
/dev/mapper/vg_data-centos63_vol
                      5.0G  139M  4.6G   3% /mydata

3. View current /etc/fstab value :

[root@centos63 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sun Jul 15 20:17:38 2012
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_centos63-lv_root /                       ext4    defaults        1 1
UUID=2217c7b1-4467-4c81-8596-c3ee7758e2cc /boot                   ext4    defaults        1 2
/dev/mapper/vg_centos63-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/vg_data/centos63_vol /mydata                       ext4    defaults        1 1

4. Unmount centos63_vol (mounted at /mydata) :

[root@centos63 ~]# umount /mydata

5. Verify that centos63_vol has unmounted :

[root@centos63 ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos63-lv_root
                      5.5G  2.2G  3.1G  41% /
tmpfs                 504M     0  504M   0% /dev/shm
/dev/sda1             485M   65M  395M  15% /boot

6. Remove the logical volume /dev/mapper/vg_data-centos63_vol :

[root@centos63 ~]# lvremove /dev/mapper/vg_data-centos63_vol
Do you really want to remove active logical volume centos63_vol? [y/n]: y
  Logical volume "centos63_vol" successfully removed

7. Verify the logical volume has been removed :

[root@centos63 ~]# lvs
  LV      VG          Attr     LSize Pool Origin Data%  Move Log Copy%  Convert
  lv_root vg_centos63 -wi-ao-- 5.54g
  lv_swap vg_centos63 -wi-ao-- 1.97g
[root@centos63 ~]#

8. Update /etc/fstab to reflect the removal of the file system :

[root@centos63 ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Jul 15 20:17:38 2012
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_centos63-lv_root /                       ext4    defaults        1 1
UUID=2217c7b1-4467-4c81-8596-c3ee7758e2cc /boot                   ext4    defaults        1 2
/dev/mapper/vg_centos63-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

How to Create Additional LVM on CentOS 6.3

This post will covers the steps to create additional Logical volume manager (LVM) on CentOS 6.3. This will very useful when the production system facing run out of disk space to store data. Sometimes there is another request for them to create additional LVM for new system or to store data. It is possible to perform this task if that system administrator has the right skill on linux. Otherwise, they will put themselves and the organization in the disaster or risk as working with the system’s data is on of the most critical job that must be perform with the right steps and skills. Follow below steps :

1. Assumed that second disk has been added called /dev/sdb. Create new partition for /dev/sdb using below command :

[root@centos63 ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xd29f2d59.
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)

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
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-652, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652):
Using default value 652

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd29f2d59

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         652     5237158+  83  Linux

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

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd29f2d59

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

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

Calling ioctl() to re-read partition table.
Syncing disks.

2. Convert this disk into a physical volume :

[root@centos63 ~]# pvcreate /dev/sdb1
  Writing physical volume data to disk "/dev/sdb1"
  Physical volume "/dev/sdb1" successfully created

Display the physical volume :

[root@centos63 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               vg_centos63
  PV Size               7.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              1922
  Free PE               0
  Allocated PE          1922
  PV UUID               521faS-HW2C-nUrs-yI1E-4OO9-eLmj-1e3bGW

  "/dev/sdb1" is a new physical volume of "4.99 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name
  PV Size               4.99 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               qTmTeq-8qoL-xxEk-Dj5V-zOjl-vEag-P6Jl7L

3. With the physical volume created we now need to create new volume group for it. In this case i will give the volume group name vg_data.

[root@centos63 ~]# vgcreate vg_data /dev/sdb1
  Volume group "vg_data" successfully created

Display the volume group :

[root@centos63 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_data
  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               4.99 GiB
  PE Size               4.00 MiB
  Total PE              1278
  Alloc PE / Size       0 / 0
  Free  PE / Size       1278 / 4.99 GiB
  VG UUID               YV3IYN-3CF9-3Yd1-69ue-wcq4-0UKk-Zk49Vk

  --- Volume group ---
  VG Name               vg_centos63
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               7.51 GiB
  PE Size               4.00 MiB
  Total PE              1922
  Alloc PE / Size       1922 / 7.51 GiB
  Free  PE / Size       0 / 0
  VG UUID               G3H7s2-0yfo-vU9W-5g9g-fq9K-tpNO-U2Y7BQ

4. Create logical volumes with the name of centos63_vol into vg_data volume group :

[root@centos63 ~]# lvcreate --name centos63_vol -l 100%FREE vg_data
  Logical volume "centos63_vol" created

Display logical volume :

[root@centos63 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg_data/centos63_vol
  LV Name                centos63_vol
  VG Name                vg_data
  LV UUID                bVRJwM-5CuR-mLf4-tqHV-j5e4-cs3o-ffKL41
  LV Write Access        read/write
  LV Creation host, time centos63.ehowstuff.local, 2012-09-28 22:55:13 +0800
  LV Status              available
  # open                 0
  LV Size                4.99 GiB
  Current LE             1278
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

  --- Logical volume ---
  LV Path                /dev/vg_centos63/lv_root
  LV Name                lv_root
  VG Name                vg_centos63
  LV UUID                3Se9Zl-RmAu-f707-1Fv2-wMvX-oH3z-maExU0
  LV Write Access        read/write
  LV Creation host, time centos63.ehowstuff.local, 2012-07-15 20:17:31 +0800
  LV Status              available
  # open                 1
  LV Size                5.54 GiB
  Current LE             1418
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/vg_centos63/lv_swap
  LV Name                lv_swap
  VG Name                vg_centos63
  LV UUID                KijzOf-uPjy-JbJd-dcjw-u0XO-aqPA-2GKScq
  LV Write Access        read/write
  LV Creation host, time centos63.ehowstuff.local, 2012-07-15 20:17:33 +0800
  LV Status              available
  # open                 1
  LV Size                1.97 GiB
  Current LE             504
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

5. Format the LVM using mke2fs with -j switch command :

[root@centos63 ~]# mke2fs -j /dev/vg_data/centos63_vol
[root@centos63 ~]# mke2fs -j /dev/vg_data/centos63_vol
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1308672 blocks
65433 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736

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

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

6. Create a mount point for the newly created logical volume :

[root@centos63 ~]# mkdir /mydata

7. Define mount point to use this partitions :

[root@centos63 ~]# e2label /dev/vg_data/centos63_vol /mydata

8. Modify /etc/fstab and add the following:

[root@centos63 ~]# vi /etc/fstab
/dev/vg_data/centos63_vol /mydata                       ext4    defaults        1 1
#
# /etc/fstab
# Created by anaconda on Sun Jul 15 20:17:38 2012
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_centos63-lv_root /                       ext4    defaults        1 1
UUID=2217c7b1-4467-4c81-8596-c3ee7758e2cc /boot                   ext4    defaults        1 2
/dev/mapper/vg_centos63-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/vg_data/centos63_vol /mydata                       ext4    defaults        1 1

9. Mount the newly created logical volume :

[root@centos63 ~]# mount /mydata

10. The newly created logical volume will now mount automatically each time the system is booted.

[root@centos63 ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos63-lv_root
                      5.5G  2.0G  3.3G  39% /
tmpfs                 504M     0  504M   0% /dev/shm
/dev/sda1             485M   65M  395M  15% /boot
/dev/mapper/vg_data-centos63_vol
                      5.0G  139M  4.6G   3% /mydata

How to Create Additional LVM on RHEL 6/CentOS 6

Most of the linux administrator facing run out of disk space to store data and sometimes there is another request for them to create additional logical volume manager(LVM) for new system or to store data. It is possible to perform this task if that system administrator has the right skill on linux. Otherwise, they will put themselves and the organization in the disaster or risk as working with the system’s data is on of the most critical job that must be perform with the right steps and skills. In this post, i will show how to create additional LVM on Red Hat Enterprise Linux 6 (RHEL 6) system. This step may apply to CentOS 6.0. CentOS 6.1 and CentOS 6.2.

1. By typing the mount command with no arguments shows us all currently mounted filesystems

[root@rhel6 ~]# mount
/dev/mapper/VolGroup-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

2. Print the current partition table of the hard disk :

[root@rhel6 ~]# fdisk -l

Disk /dev/sda: 12.9 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0000690f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1567    12069888   8e  Linux LVM

Disk /dev/dm-0: 10.2 GB, 10242490368 bytes
255 heads, 63 sectors/track, 1245 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/dm-0 doesn't contain a valid partition table

Disk /dev/dm-1: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/dm-1 doesn't contain a valid partition table

Disk /dev/sdb: 12.9 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table

3. Create new partition for /dev/sdb using below command :

[root@rhel6 ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x1adc0629.
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)

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): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1566, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1566, default 1566):
Using default value 1566

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

Calling ioctl() to re-read partition table.
Syncing disks.

4. Convert this disk into a physical volume :

[root@rhel6 ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created

Display the physical volume :

[root@rhel6 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               VolGroup
  PV Size               11.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              2946
  Free PE               0
  Allocated PE          2946
  PV UUID               tzxpC7-70n1-OuUQ-kMfT-H627-VHAA-kroHvm

  "/dev/sdb1" is a new physical volume of "12.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name
  PV Size               12.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               SoWQ5U-CmmA-tjIY-KDh0-H3gU-qVpa-tx4n6n

5. With the physical volume created we now need to create new volume group for it. In this case i will give the volume group name VolGroup_rhel6 :

[root@rhel6 ~]# vgcreate VolGroup_rhel6 /dev/sdb1
  Volume group "VolGroup_rhel6" successfully created

Display the volume group

[root@rhel6 ~]# vgdisplay
  --- Volume group ---
  VG Name               VolGroup_rhel6
  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               11.99 GiB
  PE Size               4.00 MiB
  Total PE              3070
  Alloc PE / Size       0 / 0
  Free  PE / Size       3070 / 11.99 GiB
  VG UUID               mg2I2s-8u5l-Mjm7-oKrQ-yYJ3-yzaO-mq2u9v

  --- Volume group ---
  VG Name               VolGroup
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               11.51 GiB
  PE Size               4.00 MiB
  Total PE              2946
  Alloc PE / Size       2946 / 11.51 GiB
  Free  PE / Size       0 / 0
  VG UUID               uROlLN-3aef-Cb7G-rJ8W-EyY0-aDaf-TyeE5T

6. Create logical volumes with the name of rhelvol into VolGroup_rhel6 volume group :

[root@rhel6 ~]# lvcreate --name rhelvol -l 100%FREE VolGroup_rhel6
  Logical volume "rhelvol" created

Display logical volume :

[root@rhel6 ~]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/VolGroup_rhel6/rhelvol
  VG Name                VolGroup_rhel6
  LV UUID                o3RQMB-O3Lu-TB2h-7pSm-A2kI-sh1W-rXb1Ce
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                11.99 GiB
  Current LE             3070
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

  --- Logical volume ---
  LV Name                /dev/VolGroup/lv_root
  VG Name                VolGroup
  LV UUID                1f4tl8-rCgZ-yI9J-xCsI-Hrh2-5DUS-DJF5N2
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                9.54 GiB
  Current LE             2442
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Name                /dev/VolGroup/lv_swap
  VG Name                VolGroup
  LV UUID                Kpw601-Wcvj-EM8b-FJnh-v2f6-5CqT-rV2JdT
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                1.97 GiB
  Current LE             504
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

7. Creating a file with the mke2fs command :

[root@rhel6 ~]# mke2fs -j /dev/VolGroup_rhel6/rhelvol
[root@rhel6 ~]# mke2fs -j /dev/VolGroup_rhel6/rhelvol
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
786432 inodes, 3143680 blocks
157184 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=3221225472
96 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208

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

This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@rhel6 ~]# e2label /dev/VolGroup_rhel6/rhelvol /opt
[root@rhel6 ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Jan  4 03:41:29 2012
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
/dev/VolGroup_rhel6/rhelvol /opt                       ext4    defaults        1 1
UUID=0e03e7c9-6971-4783-8fb9-fc251febf486 /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@rhel6 /]# mount /opt
[root@rhel6 /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      9.4G  2.1G  6.9G  23% /
tmpfs                 504M     0  504M   0% /dev/shm
/dev/sda1             485M   29M  432M   7% /boot
/dev/mapper/VolGroup_rhel6-rhelvol
                       12G  159M   12G   2% /opt

Type the mount command with no arguments to shows the updated mounted filesystems. /dev/mapper/VolGroup_rhel6-rhelvol now exist at the bottom :

[root@rhel6 ~]# mount
/dev/mapper/VolGroup-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/mapper/VolGroup_rhel6-rhelvol on /opt type ext4 (rw)

How to Increase the Size of Logical Volume (LVM) on Linux

There are eight(8) functional steps to resize the logical volume in linux.

1. Increase A VMware Disk Size (VMDK) Formatted As Linux LVM
2. In VM OS, Create Partition
3. Reboot the VM
4. Format the new partition
5. Creates the physical volumes using pvcreate
6. Add physical volumes to a volume group using vgextend. vgextend allows you to add one or more initialized physical volumes to an existing volume group to extend it in size.
7. Extend the size of a logical volume using lvextend
8. Resize the logical volume size using resize2fs. The resize2fs program will resize ext2, ext3, or ext4 file systems. It can be used to enlarge or shrink an unmounted file system located on device.

    [root@server ~]# df -lh
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda3             5.8G  4.3G  1.2G  79% /
    /dev/sda1              99M   12M   83M  13% /boot
    tmpfs                 506M     0  506M   0% /dev/shm
    /dev/mapper/VolGroup00-datavol
                           13G  855M   12G   7% /data
    /dev/mapper/VolGroup01-optvol
                           20G  173M   19G   1% /opt
    
    [root@server ~]# fdisk -lu
    
    Disk /dev/sda: 21.4 GB, 21474836480 bytes
    255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *          63      208844      104391   83  Linux
    /dev/sda2          208845     2313359     1052257+  82  Linux swap / Solaris
    /dev/sda3         2313360    14667344     6176992+  8e  Linux LVM
    /dev/sda4        14667345    41929649    13631152+  8e  Linux LVM
    
    Disk /dev/sdb: 26.8 GB, 26843545600 bytes
    255 heads, 63 sectors/track, 3263 cylinders, total 52428800 sectors
    Units = sectors of 1 * 512 = 512 bytes
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1              63    41929649    20964793+  8e  Linux LVM
    
    [root@server ~]# fdisk /dev/sdb
    
    The number of cylinders for this disk is set to 3263.
    There is nothing wrong with that, but this is larger than 1024,
    and could in certain setups cause problems with:
    1) software that runs at boot time (e.g., old versions of LILO)
    2) booting and partitioning software from other OSs
       (e.g., DOS FDISK, OS/2 FDISK)
    
    Command (m for help): n
    Command action
       e   extended
       p   primary partition (1-4)
    p
    Partition number (1-4): 2
    First cylinder (2611-3263, default 2611):
    Using default value 2611
    Last cylinder or +size or +sizeM or +sizeK (2611-3263, default 3263):
    Using default value 3263
    
    Command (m for help): t
    Partition number (1-4): 2
    Hex code (type L to list codes): 8e
    Changed system type of partition 2 to 8e (Linux LVM)
    
    Command (m for help): p
    
    Disk /dev/sdb: 26.8 GB, 26843545600 bytes
    255 heads, 63 sectors/track, 3263 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1        2610    20964793+  8e  Linux LVM
    /dev/sdb2            2611        3263     5245222+  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.
    Syncing disks.
    [root@server ~]# reboot
    
    [root@server ~]# df -lh
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda3             5.8G  4.3G  1.2G  79% /
    /dev/sda1              99M   12M   83M  13% /boot
    tmpfs                 506M     0  506M   0% /dev/shm
    /dev/mapper/VolGroup00-datavol
                           13G  855M   12G   7% /data
    /dev/mapper/VolGroup01-optvol
                           20G  173M   19G   1% /opt
    

After the new partition table has been written, format the new partition.

    [root@server ~]# mke2fs -j /dev/sdb2
    mke2fs 1.39 (29-May-2006)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    656000 inodes, 1311305 blocks
    65565 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=1346371584
    41 block groups
    32768 blocks per group, 32768 fragments per group
    16000 inodes per group
    Superblock backups stored on blocks:
            32768, 98304, 163840, 229376, 294912, 819200, 884736
    
    Writing inode tables: done
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done
    
    This filesystem will be automatically checked every 20 mounts or
    180 days, whichever comes first.  Use tune2fs -c or -i to override.
    

Creates the physical volumes using command below:

    [root@server ~]# pvcreate /dev/sdb2
      Physical volume "/dev/sdb2" successfully created
    

Scan all disks for physical volumes.

    [root@server ~]# pvscan
      PV /dev/sdb1   VG VolGroup01      lvm2 [19.99 GB / 0    free]
      PV /dev/sda4   VG VolGroup00      lvm2 [13.00 GB / 0    free]
      PV /dev/sdb2                      lvm2 [5.00 GB]
      Total: 3 [37.99 GB] / in use: 2 [32.99 GB] / in no VG: 1 [5.00 GB]
    

Add physical volumes to a volume group. This example add /dev/sdb2 to VolGroup01

    [root@server ~]# vgextend VolGroup01 /dev/sdb2
      Volume group "VolGroup01" successfully extended
    
    [root@server ~]# df -lh
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda3             5.8G  4.3G  1.2G  79% /
    /dev/sda1              99M   12M   83M  13% /boot
    tmpfs                 506M     0  506M   0% /dev/shm
    /dev/mapper/VolGroup00-datavol
                           13G  855M   12G   7% /data
    /dev/mapper/VolGroup01-optvol
                           20G  173M   19G   1% /opt
    

Extend the size of a logical volume using lvextend.

    [root@server ~]# lvextend -l +100%FREE /dev/VolGroup01/optvol
      Extending logical volume optvol to 24.99 GB
      Logical volume optvol successfully resized
    

Resize the logical volume size using resize2fs.

    [root@server ~]# resize2fs /dev/mapper/VolGroup01-optvol
    resize2fs 1.39 (29-May-2006)
    Filesystem at /dev/mapper/VolGroup01-optvol is mounted on /opt; on-line resizing required
    Performing an on-line resize of /dev/mapper/VolGroup01-optvol to 6551552 (4k) blocks.
    The filesystem on /dev/mapper/VolGroup01-optvol is now 6551552 blocks long.
    
    [root@server ~]# df -lh
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda3             5.8G  4.3G  1.2G  79% /
    /dev/sda1              99M   12M   83M  13% /boot
    tmpfs                 506M     0  506M   0% /dev/shm
    /dev/mapper/VolGroup00-datavol
                           13G  855M   12G   7% /data
    /dev/mapper/VolGroup01-optvol
                           25G  173M   24G   1% /opt
    

How to Setup Additional LVM in CentOS 5.5

In this post, i will share on how to setup additional LVM in CentOS 5.5.

1. Assumed that new hard disk 2 (sdb) has been added in this CentOS 5.5 server.
2. Print the current partition table of the hard disk :

    [root@server ~]# fdisk -lu
    
    Disk /dev/sda: 21.4 GB, 21474836480 bytes
    255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *          63      208844      104391   83  Linux
    /dev/sda2          208845     2313359     1052257+  82  Linux swap / Solaris
    /dev/sda3         2313360    14667344     6176992+  8e  Linux LVM
    /dev/sda4        14667345    41929649    13631152+  8e  Linux LVM
    
    Disk /dev/sdb: 21.4 GB, 21474836480 bytes
    255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    
    Disk /dev/sdb doesn't contain a valid partition table
    

3. Create new partition for /dev/sdb using below command.

    [root@server ~]# 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.
    
    
    The number of cylinders for this disk is set to 2610.
    There is nothing wrong with that, but this is larger than 1024,
    and could in certain setups cause problems with:
    1) software that runs at boot time (e.g., old versions of LILO)
    2) booting and partitioning software from other OSs
       (e.g., DOS FDISK, OS/2 FDISK)
    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)
    p
    Partition number (1-4): 1
    First cylinder (1-2610, default 1):
    Using default value 1
    Last cylinder or +size or +sizeM or +sizeK (1-2610, default 2610):
    Using default value 2610
    
    Command (m for help): t
    Selected partition 1
    Hex code (type L to list codes): 8e
    Changed system type of partition 1 to 8e (Linux LVM)
    
    Command (m for help): p
    
    Disk /dev/sdb: 21.4 GB, 21474836480 bytes
    255 heads, 63 sectors/track, 2610 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1        2610    20964793+  8e  Linux LVM
    
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
    
    [root@server ~]# mke2fs -j /dev/sdb1
    mke2fs 1.39 (29-May-2006)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    2621440 inodes, 5241198 blocks
    262059 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=0
    160 block groups
    32768 blocks per group, 32768 fragments per group
    16384 inodes per group
    Superblock backups stored on blocks:
            32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
            4096000
    
    Writing inode tables: done
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done
    
    This filesystem will be automatically checked every 24 mounts or
    180 days, whichever comes first.  Use tune2fs -c or -i to override.
    

4. Next create the new partitions for this LVM.

    [root@server ~]# pvcreate /dev/sdb1
      Physical volume "/dev/sdb1" successfully created
    

5. Display the Physical Volume using command below:

    [root@server ~]# pvdisplay
      --- Physical volume ---
      PV Name               /dev/sda4
      VG Name               VolGroup00
      PV Size               13.00 GB / not usable 3.67 MB
      Allocatable           yes (but full)
      PE Size (KByte)       4096
      Total PE              3327
      Free PE               0
      Allocated PE          3327
      PV UUID               JgYuxF-UJiB-t148-g5WQ-Lx05-Ozk1-13UwAR
    
      "/dev/sdb1" is a new physical volume of "19.99 GB"
      --- NEW Physical volume ---
      PV Name               /dev/sdb1
      VG Name
      PV Size               19.99 GB
      Allocatable           NO
      PE Size (KByte)       0
      Total PE              0
      Free PE               0
      Allocated PE          0
      PV UUID               246F8V-FMTR-WNK0-9305-q2sf-PKtC-VQXOQr
    

6. Create new volume group for /dev/sdb1. Assumed that previous volume goup is VolGroup00. So next volume group will be VolGroup01.

    [root@server ~]# vgcreate VolGroup01 /dev/sdb1
      Volume group "VolGroup01" successfully created
    

7. Display the existing Volume Group.

    [root@server ~]# vgdisplay
      --- Volume group ---
      VG Name               VolGroup01
      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               19.99 GB
      PE Size               4.00 MB
      Total PE              5118
      Alloc PE / Size       0 / 0
      Free  PE / Size       5118 / 19.99 GB
      VG UUID               tvLVhu-6Nzo-jB0b-oZQ4-VY30-9TP4-Dhxvvm
    
      --- Volume group ---
      VG Name               VolGroup00
      System ID
      Format                lvm2
      Metadata Areas        1
      Metadata Sequence No  2
      VG Access             read/write
      VG Status             resizable
      MAX LV                0
      Cur LV                1
      Open LV               1
      Max PV                0
      Cur PV                1
      Act PV                1
      VG Size               13.00 GB
      PE Size               4.00 MB
      Total PE              3327
      Alloc PE / Size       3327 / 13.00 GB
      Free  PE / Size       0 / 0
      VG UUID               3XozzT-3A3w-6eXs-XoEb-yRN1-v9FM-N0YheL
    

8. Create logical volumes with the name of optvol into VolGroup01 volume group.

    [root@server ~]# lvcreate --name optvol -l 100%FREE VolGroup01
      Logical volume "optvol" created
    

9. Display the logical volumes.

    [root@server ~]# lvdisplay
      --- Logical volume ---
      LV Name                /dev/VolGroup01/optvol
      VG Name                VolGroup01
      LV UUID                YXkMnR-l7gH-tj0r-dCUt-Ncl7-aTmb-Vas4Tp
      LV Write Access        read/write
      LV Status              available
      # open                 0
      LV Size                19.99 GB
      Current LE             5118
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     256
      Block device           253:1
    
      --- Logical volume ---
      LV Name                /dev/VolGroup00/datavol
      VG Name                VolGroup00
      LV UUID                KowzAO-WWMj-O0xw-IY31-Gfqv-sjaE-nYvdhk
      LV Write Access        read/write
      LV Status              available
      # open                 1
      LV Size                13.00 GB
      Current LE             3327
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     256
      Block device           253:0
    

10. creating a file with the mke2fs command

    [root@server ~]# mke2fs -j /dev/VolGroup01/optvol
    mke2fs 1.39 (29-May-2006)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    2621440 inodes, 5240832 blocks
    262041 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=0
    160 block groups
    32768 blocks per group, 32768 fragments per group
    16384 inodes per group
    Superblock backups stored on blocks:
            32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
            4096000
    
    Writing inode tables: done
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done
    
    This filesystem will be automatically checked every 38 mounts or
    180 days, whichever comes first.  Use tune2fs -c or -i to override.
    
    [root@server ~]# e2label /dev/VolGroup01/optvol /opt
    
    [root@server ~]# vi /etc/fstab
    
    [root@server ~]# mount /opt
    
    [root@server ~]# df -lh
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda3             5.8G  4.3G  1.2G  79% /
    /dev/sda1              99M   12M   83M  13% /boot
    tmpfs                 506M     0  506M   0% /dev/shm
    /dev/mapper/VolGroup00-datavol
                           13G  855M   12G   7% /data
    /dev/mapper/VolGroup01-optvol
                           20G  173M   19G   1% /opt
    
    [root@server ~]# fdisk -lu
    
    Disk /dev/sda: 21.4 GB, 21474836480 bytes
    255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *          63      208844      104391   83  Linux
    /dev/sda2          208845     2313359     1052257+  82  Linux swap / Solaris
    /dev/sda3         2313360    14667344     6176992+  8e  Linux LVM
    /dev/sda4        14667345    41929649    13631152+  8e  Linux LVM
    
    Disk /dev/sdb: 21.4 GB, 21474836480 bytes
    255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1              63    41929649    20964793+  8e  Linux LVM
    

References:

    https://wiki.archlinux.org/index.php/LVM
    http://vr.org/docs/centos/5/html/Cluster_Logical_Volume_Manager/LV_create.html
    http://serverfault.com/questions/41904/volume-group-out-of-space-how-do-i-reclaim-it
    http://www.idevelopment.info/data/Unix/Linux/LINUX_ManagingPhysicalLogicalVolumes.shtml
    http://en.gentoo-wiki.com/wiki/Root_filesystem_over_LVM2,_DM-Crypt_and_RAID
    http://wingloon.com/2009/01/16/setup-additional-lvm-in-centos-52/
    http://rute.2038bug.com/node22.html.gz