Friday, March 29, 2013

Extending LVM using pvcreate, vgcreate, lvcreate, lvextend and resize2fs

 

components of LVM are as follows:
1. VG – Volume Group: high level container that holds one or more physical or logical volumes.
2. PV – Physical Volume: represents a storage device such as a disk drive or storage media.
3. PE – Physical Extent: each physical volume is divided into equal size blocks called physical extents.
4. LV – Logical Volume: eqivalent to a disk partition and can contain a filesystem.
5. LE – Logical Extent: Each logical volume (LV) is divided into equal size blocks called logical extents.
Creating new partition
First you need to add a new disk, it does not matter whether it is a virtual or physical. In my case I have added a Disk /dev/vdb.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@mytestbox ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.3 (Santiago)
[root@mytestbox ~]# fdisk -l
Disk /dev/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007c501
   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3         409      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2             409       41611    20765696   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
Disk /dev/mapper/vg_01-swap_vol: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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
<p>Disk /dev/mapper/vg_01-root_vol: 12.7 GB, 12650020864 bytes
255 heads, 63 sectors/track, 1537 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
<p>Disk /dev/vdb: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Next you need to create a partition on this new disk. For that we will use fdisk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@mytestbox ~]# fdisk /dev/vdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x3467714a.
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): p
Disk /dev/vdb: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x3467714a
Device Boot      Start         End      Blocks   Id  System
We will create a new primary partition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Command (m for help): n
Command action<br />
   e   extended<br />
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-41610, default 1):press enter
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-41610, default 41610):press enter
Using default value 41610
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): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
And check with fdisk if it has been recognized
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@mytestbox ~]# fdisk -l
Disk /dev/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007c501
Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3         409      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2             409       41611    20765696   8e  Linux LVM
Partition 2 does not end on cylinder boundary.

Disk /dev/vdb: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x3467714a
Device Boot      Start         End      Blocks   Id  System
/dev/vdb1               1       41610    20971408+  8e  Linux LVM
[root@mytestbox ~]#
Creating physical volume
First let’s check the size of existing physical volume
1
2
3
4
<br />
[root@mytestbox ~]# pvs
  PV         VG    Fmt  Attr PSize  PFree
  /dev/vda2  vg_01 lvm2 a--  19.78g    0
And create a new physical volume and check if it worked
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@mytestbox ~]# pvcreate /dev/vdb1
  Writing physical volume data to disk &quot;/dev/vdb1&quot;
  Physical volume &quot;/dev/vdb1&quot; successfully created
[root@mytestbox ~]# pvs
  PV         VG    Fmt  Attr PSize  PFree
  /dev/vda2  vg_01 lvm2 a--  19.78g     0
  /dev/vdb1        lvm2 a--  20.00g 20.00g
[root@mytestbox ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               vg_01
  PV Size               19.80 GiB / not usable 23.00 MiB
  Allocatable           yes (but full)
  PE Size               32.00 MiB
  Total PE              633
  Free PE               0
  Allocated PE          633
  PV UUID               vp2QRP-Ufi4-2f3v-afSI-o4pM-2hYb-H4YRoh
&quot;/dev/vdb1&quot; is a new physical volume of &quot;20.00 GiB&quot;
  --- NEW Physical volume ---
  PV Name               /dev/vdb
  VG Name<br />
  PV Size               20.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               7X10kE-GF36-iuhE-1ja6-9LLa-q2CI-hWRiao
[root@mytestbox ~]#
Extending volume group
Now it’s time to extend our volume group, Please note the values Free PE / Size before and after the extend.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[root@mytestbox ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_01
  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               19.78 GiB
  PE Size               32.00 MiB
  Total PE              633
  Alloc PE / Size       633 / 19.78 GiB
  Free  PE / Size       0 / 0
  VG UUID               qHhhfc-HvnQ-bS2t-dUqX-buOP-MWCf-THdQQP

[root@mytestbox ~]# vgextend vg_01 /dev/vdb1
  Volume group &quot;vg_01&quot; successfully extended
[root@mytestbox ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_01
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               39.75 GiB
  PE Size               32.00 MiB
  Total PE              1272
  Alloc PE / Size       633 / 19.78 GiB
  Free  PE / Size       639 / 19.97 GiB
  VG UUID               qHhhfc-HvnQ-bS2t-dUqX-buOP-MWCf-THdQQP
[root@mytestbox ~]#
Extending logical volume
Now, as we got a volume group extended, we can extend a desired logical volume. In my case it will be a root partition.
1
2
3
4
[root@mytestbox ~]# lvextend -L +19G /dev/vg_01/root_vol (e.g, /dev/mapper/VolGroup00-LogVo100)
  Extending logical volume root_vol to 30.78 GiB
  Logical volume root_vol successfully resized
And resize file system so now size can now be visible to the system
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
root@mytestbox ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_01-root_vol
                       12G  1.3G  9.8G  12%
tmpfs                 246M     0  246M   0% /dev/shm
/dev/vda1             194M   78M  106M  43% /boot
[root@mytestbox ~]# resize2fs /dev/vg_01/root_vol
resize2fs 1.41.12 (17-May-2010
Filesystem at /dev/vg_01/root_vol is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/vg_01/root_vol to 8069120 (4k) blocks.
The filesystem on /dev/vg_01/root_vol is now 8069120 blocks long.
[root@mytestbox ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_01-root_vol
                       31G  1.3G   28G   5% /
tmpfs                 246M     0  246M   0% /dev/shm
/dev/vda1             194M   78M  106M  43% /boot
[root@mytestbox ~]#