Home arrow Blog arrow Walk-Throughs arrow How to resize a Logical Volume on a Linux VM
Border States Consulting, LLC |
Home
Services
Service Plans
Sales
Blog
News Feeds
Links
Advanced Search
Contact
Jobs
Other Menu
Antivirus
Remote Support
Trouble Ticket
Administrator

Authorize.Net Certified Developer

Avast antivirus

 
 
 
How to resize a Logical Volume on a Linux VM PDF Print E-mail

This is a step-by-step guide on how to increase the size of a logical volume on a CentOS 6 virtual machine. I had created this particular virtual machine to do some Ruby on Rails application development work and quickly realized I didn't make the drive large enough to hold my project. So, these notes are mainly here to help me remember the steps, hopefully they can be useful to someone else too.

EDIT: this information is dated, please see the updated article here.

First, the standard disclaimer: Don't blame me if you follow these steps and wreck your VM. Make a backup of your VM before you start if there is anything on it that you can't afford to lose. These steps worked for me, but they may not work for your particular case. There may also be a "better" way to accomplish this, but as I said, this worked for me.

OK, so let's get started. In this example the host machine was a Fedora 14 box named pueblan and the virtual machine was a CentOS 6 installation named Ruby. A quick df on the VM will show the file structure.

[root@Ruby ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root
  17067980 10391472 5814228 65% /
tmpfs 251704 0 251704 0% /dev/shm
/dev/sda1 495844 29170 441074 7% /boot

As you can see, the lv_root logical volume is only 17GB and I'm using 65% of it. So, first we stop the VM.

[root@Ruby ~]# shutdown -h now

Now, as root on host, navigate to the directory where the virtual disk images are located, in this case it was /var/lib/libvirt/images/, and use "qemu-img resize" to increase the size of the Ruby image by 20GB.

[root@pueblan ~]# cd /var/lib/libvirt/images
[root@pueblan images]# ls
Fedora14VM.img Vector.img XP1.img
Centos6.img Ruby.img XP1-clone.img
[root@pueblan images]# qemu-img resize Ruby.img +20G
Image resized.

Now, time to boot the VM again and log in as root. Then run fdisk on the drive containing the logical volume - on this VM that was /dev/sda. If you aren't sure, you could do a pvdisplay to find out.

[root@Ruby ~]# fdisk /dev/sda
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').

Print the partition table

Command (m for help): p
Disk /dev/sda: 40.8 GB, 40802189312 bytes
255 heads, 63 sectors/track, 4960 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: 0x000b6e5f
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
/dev/sda2   64 2349 18355318+ 8e Linux LVM

Notice that our /dev/sda is now 40.8GB, however our /dev/sda2 is only using 18GB of that. So, we are going to delete that partition and create a new one using all the available space by hitting d, 2, p, n, p, 2, , , and p.

Command (m for help): d
Partition number (1-4): 2
Command (m for help): p
Disk /dev/sda: 40.8 GB, 40802189312 bytes
255 heads, 63 sectors/track, 4960 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: 0x000b6e5f
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (64-4960, default 64):
Using default value 64
Last cylinder, +cylinders or +size{K,M,G} (64-4960, default 4960):
Using default value 4960
Command (m for help): p
Disk /dev/sda: 40.8 GB, 40802189312 bytes
255 heads, 63 sectors/track, 4960 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: 0x000b6e5f
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
/dev/sda2   64 4960 39328176 83 Linux

Now we have the /dev/sda2 partition the correct size, but it is the wrong type so let's correct that, and then write the changes to disk and reboot.

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): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@Ruby ~]# shutdown -r now

After the reboot, log into the VM once again as root. The pvdisplay command will show that our physical volume is still only 17.5GB, so let's resize it to use the extra space we just created on the partition. Then we'll resize the logical volume to use up the entire physical volume, and then finally extend the file system onto the entire logical volume.

[root@Ruby ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name VolGroup
PV Size 17.50 GiB / not usable 905.50 KiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4481
Free PE 0
Allocated PE 4481
PV UUID iIpZGX-CvAG-G35Q-pfBS-PRW8-smq7-lxpuHH
[root@Ruby ~]# pvresize /dev/sda2
Physical volume "/dev/sda2" changed
1 physical volume(s) resized / 0 physical volume(s) not resized
[root@Ruby ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name VolGroup
PV Size 37.51 GiB / not usable 1.42 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 9601
Free PE 5120
Allocated PE 4481
PV UUID iIpZGX-CvAG-G35Q-pfBS-PRW8-smq7-lxpuHH
[root@Ruby ~]# lvresize /dev/VolGroup/lv_root -L +20G
Extending logical volume lv_root to 36.54 GiB
Logical volume lv_root successfully resized
[root@Ruby ~]# resize2fs /dev/VolGroup/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/VolGroup/lv_root to 9577472 (4k) blocks.

And now df will show that we have increased the size of our file system to use up the entire space.

[root@Ruby ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root
  37714320 7073516 28740648 20% /
tmpfs 251704 0 251704 0% /dev/shm
/dev/sda1 495844 29170 441074 7% /boot

To be safe, we'll now reboot with a forced fsck.

[root@Ruby ~]# shutdown -rF now
Last Updated ( May 31, 2017 at 12:25 AM )
 
   
     

 
Take the iconic LEGO MINI Cooper for a drive!

Mambo is Free Software released under the GNU/GPL License. Hosted by <Border States Consulting, LLC>