How to Increase the size of the virtual machine disk by expanding the Linux LVM

We will first confirm that our partition type is actually Linux LVM by running the below command.

Disk information showing

[10.6.73.19/maga/root@ ~]$ df –h

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/vg_maga8-lv_root

28G 27G 0 100% /

tmpfs 5.9G 0 5.9G 0% /dev/shm

/dev/sda1 485M 35M 425M 8% /boot

/dev/mapper/vg_maga8-lv_home

21G 3.2G 17G 16% /home

Increasing the virtual hard disk

clip_image001

We increase the allocated disk space on the virtual machine. This is done by right clicking the virtual machine in vSphere, selecting edit settings, and then selecting the hard disk. Once complete click OK, this is all that needs to be done in VMware for this process

Below is an image after performing this and confirming that the new space is displaying.

Partition the new disk space

we use fdisk to create a new primary partition to make use of the new expanded disk space.

fdisk /dev/sda

We are now using fdisk to create a new partition, the inputs I have entered in are shown below in bold. Note that you can press ‘m’ to get a full listing of the fdisk commands.

‘n’ was selected for adding a new partition.

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

‘p’ is then selected as we are making a primary partition.

Command action

l logical (5 or over)

p primary partition (1-4)

p

As I already have /dev/sda1 and /dev/sda2 as shown in previous images, I have gone with using ‘3’ for this new partition which will be created as /dev/sda3

Partition number (1-4): 3

We just press enter twice above as by default the first and last cylinders of the unallocated space should be correct. After this the partition is then ready.

First cylinder (2611-3916, default 2611): “enter”

Using default value 2611

Last cylinder, +cylinders or +size{K,M,G} (2611-3916, default 3916): “enter”

Using default value 3916

‘t’ is selected to change to a partition’s system ID, in this case we change to ‘3’ which is the one we just created.

Command (m for help): t

Partition number (1-5): 3

The hex code ‘8e’ was entered as this is the code for a Linux LVM which is what we want this partition to be, as we will be joining it with the original /dev/sda5 Linux LVM.

Hex code (type L to list codes): 8e

Changed system type of partition 3 to 8e (Linux LVM)

‘w’ is used to write the table to disk and exit, basically all the changes that have been done will be saved and then you will be exited from fdisk.

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.

You will see a warning which basically means in order to use the new table with the changes a system reboot is required. If you can not see the new partition using “fdisk -l” you may be able to run “partprobe -s” to rescan the partitions. In my test I did not require either of those things at this stage (I do a reboot later on), straight after pressing ‘w’ in fdisk I was able to see the new /dev/sda3 partition of my 10gb of space as displayed in the below image.

For CentOS/RHEL run a “partx -a /dev/sda3” to avoid rebooting later on.

[10.6.73.19/maga/root@ ~]$ fdisk -l

Disk /dev/sda: 85.9 GB, 85899345920 bytes

255 heads, 63 sectors/track, 10443 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: 0x000bb46c

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 7833 62401536 8e Linux LVM

/dev/sda3 7833 10443 20968837+ 8e Linux LVM

Disk /dev/dm-0: 30.5 GB, 30463229952 bytes

255 heads, 63 sectors/track, 3703 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: 10.6 GB, 10552868864 bytes

255 heads, 63 sectors/track, 1282 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/dm-2: 22.9 GB, 22879928320 bytes

255 heads, 63 sectors/track, 2781 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-2 doesn’t contain a valid partition table

That’s all for partitioning, we now have a new partition which is making use of the previously unallocated disk space from the increase in VMware.

Increasing the logical volume

We use the pvcreate command which creates a physical volume for later use by the logical volume manager (LVM). In this case the physical volume will be our new /dev/sda3 partition.

root@Maga:~# pvcreate /dev/sda3

Device /dev/sda3 not found (or ignored by filtering).

In order to get around this you can either reboot, or use partprobe/partx as previously mentioned to avoid a reboot, as in this instance the disk does not appear to be there correctly despite showing in “fdisk -l”. After a reboot or partprobe/partx use the same command which will succeed.

root@Maga:~# pvcreate /dev/sda3

Physical volume “/dev/sda3” successfully created

Next we need to confirm the name of the current volume group using the vgdisplay command. The name will vary depending on your setup, for me it is the name of my test server. vgdisplay provides lots of information on the volume group, I have only shown the name and the current size of it for this example.

[10.6.73.19/maga/root@ ~]$ vgdisplay

— Volume group —

VG Name vg_maga8

System ID

Format lvm2

Metadata Areas 1

Metadata Sequence No 4

VG Access read/write

VG Status resizable

MAX LV 0

Cur LV 3

Open LV 3

Max PV 0

Cur PV 1

Act PV 1

VG Size 59.51 GiB

PE Size 4.00 MiB

Total PE 15234

Alloc PE / Size 15234 / 59.51 GiB

Free PE / Size 0 / 0

VG UUID 13k4PP-TNJL-GfsX-uBmY-o4SU-oeoA-s75eHQ

Now we extend the ‘Maga’ volume group by adding in the physical volume of /dev/sda3 which we created using the pvcreate command earlier.

[10.6.73.19/maga/root@ ~]$ vgextend vg_maga8 /dev/sda3

Volume group “vg_maga8” successfully extended

[10.6.73.19/maga/root@ ~]$ vgdisplay

— Volume group —

VG Name vg_maga8

System ID

Format lvm2

Metadata Areas 2

Metadata Sequence No 5

VG Access read/write

VG Status resizable

MAX LV 0

Cur LV 3

Open LV 3

Max PV 0

Cur PV 2

Act PV 2

VG Size 79.50 GiB

PE Size 4.00 MiB

Total PE 20353

Alloc PE / Size 15234 / 59.51 GiB

Free PE / Size 5119 / 20.00 GiB

VG UUID 13k4PP-TNJL-GfsX-uBmY-o4SU-oeoA-s75eHQ

Using the pvscan command we scan all disks for physical volumes

[10.6.73.19/maga/root@ ~]$ pvscan

PV /dev/sda2 VG vg_maga8 lvm2 [59.51 GiB / 0 free]

PV /dev/sda3 VG vg_maga8 lvm2 [20.00 GiB / 20.00 GiB free]

Total: 2 [79.50 GiB] / in use: 2 [79.50 GiB] / in no VG: 0 [0 ]

Firstly confirm the path of the logical volume using lvdisplay. This path name will vary depending on your setup.

[10.6.73.19/maga/root@ ~]$ lvdisplay

— Logical volume —

LV Name /dev/vg_maga8/lv_root

VG Name vg_maga8

LV UUID ieRE1N-I7Qf-cNKe-qW05-ipuB-rQnI-JUhmiT

LV Write Access read/write

LV Status available

# open 1

LV Size 48.37 GiB

Current LE 12382

Segments 2

Allocation inherit

Read ahead sectors auto

– currently set to 256

Block device 253:0

— Logical volume —

LV Name /dev/vg_maga8/lv_home

VG Name vg_maga8

LV UUID jgfW98-hDcn-hf1y-B6Q7-X0Yr-fAiH-FpBCea

LV Write Access read/write

LV Status available

# open 1

LV Size 21.31 GiB

Current LE 5455

Segments 1

Allocation inherit

Read ahead sectors auto

– currently set to 256

Block device 253:2

— Logical volume —

LV Name /dev/vg_maga8/lv_swap

VG Name vg_maga8

LV UUID OPe04Y-W5t3-hRTX-Q0K9-bzVT-y3j1-hl58I2

LV Write Access read/write

LV Status available

# open 1

LV Size 9.83 GiB

Current LE 2516

Segments 1

Allocation inherit

Read ahead sectors auto

– currently set to 256

Block device 253:1

The logical volume is then extended using the lvextend command.

[10.6.73.19/maga/root@ ~]$ lvextend /dev/vg_was8/lv_root /dev/sda3

Extending logical volume lv_root to 48.37 GiB

Logical volume lv_root successfully resized

There is then one final step which is to resize the file system.

Note that this may take some time to complete, it took about 30 seconds

[10.6.73.19/maga/root@ ~]$ resize2fs /dev/vg_maga8/lv_root

resize2fs 1.41.12 (17-May-2010)

Filesystem at /dev/vg_maga8/lv_root is mounted on /; on-line resizing required

old desc_blocks = 2, new_desc_blocks = 4

Performing an on-line resize of /dev/vg_maga8/lv_root to 12679168 (4k) blocks.

The filesystem on /dev/vg_maga8/lv_root is now 12679168 blocks long.

That’s it, now with the ‘df’ command we can see that the total available disk space has been increased.

[10.6.73.19/maga/root@ ~]$ df -h

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/vg_maga8-lv_root

48G 27G 19G 60% /

tmpfs 5.9G 0 5.9G 0% /dev/shm

/dev/sda1 485M 35M 425M 8% /boot

/dev/mapper/vg_maga8-lv_home

21G 3.2G 17G 16% /home

[10.6.73.19/maga/root@ ~]$

We have increased the virtual disk drive through VMware, created a new partition out of this newly unallocated space within the guest OS, turned it into a physical volume, extended the volume group, and then finally extended the original logical volume over the newer physical volume resulting in overall disk space being increased successfully.

Click to rate this post!
[Total: 0 Average: 0]

Share and Enjoy !

Shares
Get Weekly KBV Tips !

Learn and improve day to day Knowledge & lifestyles.