Expand partition on Linux KVM Client (ext4 / raw)

I've scoured the internet for this but it's new territory and daunting me thus far.

I have a 30GB VM created and working fine.  I use Virt-manager to login from a client.
I have succesfully increased the drive size from 30GB to 113GB using steps 1 - 4 shown here  

I then tried using 'parted' following this site's instructions to increase my boot partition to say 110GB of the available 113GB


But I get an error returned from parted that says:
The location is outside the device /dev/vda1

I am uncertain of the solution.  I need expert help ;-)  The screen shots on http://wiki.linuxmatter.com/index.php/Virtual_Servers:resize_disk are in fact of my partitions as they exist right now.

What sayeth the group ?

Jay LeporeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Seth SimmonsSr. Systems AdministratorCommented:
boot on ubuntu live cd and use gparted to extend
just tried in a test environment with the partition layout the way you have it and it worked fine
Daniel HelgenbergerCommented:
The Howto in your post is fairly outdated. Since qemu v1.3 resize is supported. Please check your qemu version for the the resize command (run qemu-img without parameters). In the howto, parted is only used to illustrate the changes.

Can you please clarify some things:
1. You want to extend a virtual disk in a client vm
2. You assigned a qemu image (raw/qcow) as boot disk to the VM
3. The VM's FS is ext4

If the former is true, you have no need to use parted at all. You should:
1. Stop the guest
2. Use qemu-img to resize the the image file in your Hypervisor
3. Use resize2fs to extent the partition inside the running VM:
# Stop the guest
$ virsh shutdown <yourvmname>
# resize the image; add 20GB for instance
$ qemu-img resize /path/to/boot.img +20GB
#start VM
$ virsh start <yourvmname>
# Switch to your guest
$ ssh <yourvmname>
# resize the partition; this works on active mount points only with extend
$ resize2fs /

Open in new window

The last command will claim all available space on the root partition. Check this with
$ df -h

Open in new window

Jay LeporeAuthor Commented:
With regard to seth2740's thoughts:
When you boot to gparted, it only shows mounted physical drives via the dropdown menu in the top right corner. I use gparted often at our shop.

It doesn't even see the VM image and I am uncertain how to point it to that image file for expanding the partition.

Your thoughts on this ?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Jay LeporeAuthor Commented:
With regard to helge000's comments:

When I try to ssh into my VM using the method you have provided, it fails with:

ssh: Could not resolve hostname VM-Staging: Name or service not known

no route to host
I happen to know the IP of this VM is however, if I type this I receive
ssh: connect to host port 22: No route to host

I also cannot ping it.

So I suppose something is not quite right with your instruction.

Your thoughts ?
Jay LeporeAuthor Commented:
With regard to helge000's comments:

I would enjoy your answer to my previous comment.  However I also have a follow up observation.

If i log into my VM using virt-manager I am able to go to the command line and run resize2fs as you suggested.  Howver, this also yields an error as follows:

jay@staging:~$ resize2fs /
resize2fs 1.42 (29-Nov-2011)
open: Is a directory while opening /

Your thoughts ?

Daniel HelgenbergerCommented:
Hello compu523,
the SSH command is only an example, since I have no knowledge how you generally access your VM; I just assumed you access it with ssh because this is the most common way. If you use virt-manager; this will work the same. You can also use virt-manager to start and shutdown machines. I used virsh because you need to do the resize of the image via command line anyway and can provide an easy step by step operation; there is nothing wrong - but of course you need to adapt it for your needs.

The important part is, you need to do the final resize operation inside the VM. Because this for ext4 and your VM knowing the filesystem size. Please keep in mind you not only need to extend the device but the file system as well.

So, first: Did you manage to extend your image with qemu-img? Please confirm this with:
$ qemu-img info /path/to/boot.img

Open in new window

Remember to shutdown the VM prior to resizing.

Second: If resize2fs with mount points does not work, then just substitute it with a device. Back in your running / restarted VM terminal do the filesystem resize:
$ resize2fs /dev/vda1

Open in new window

Attention! This is an example - like in my previous sippets. I cannot know your boot device; I assumed the first partition of vda because of your post. So, please find your root dev from the output of mount or df, you need the device mounted at /.
Jay LeporeAuthor Commented:

Yes, that extension of the image went fast and smooth.  This is excellent of course.

Regarding your 2nd portion.  You mention 'remember to shutdown the VM prior to resizing'.   I assume that the resize2fs needs to be run from inside that same VM so that is confusing.  I believe you just wanted me to to be sure to close it before reopening with the larger image size.  Please clarify on this one line.

Beyond that, assuming you want me to reopen the VM I have done so and from inside I have run resize2fs /dev/vda1 (you are correct as this is my device) I receive the message "The filesystem is already 7339776 blocks long, Nothing to do!"

I think you are trying to extend the filesystem before you extend the partition.  The partition must first be extended isn't that correct ?

Your thoughts ?

Jay Lepore
Daniel HelgenbergerCommented:
Sorry, but I think I need to go more deeply into the whole resize/VM thing - I hope I do you not wrong:
Resizing a partition, no matter if you are in a VM or not, breaks down in two steps:
1. Extend the physical volume (Eg. add hard disk space, in a VM with qemu-img). In this step you need to change/modify your hardware. In a Hypervisor, you need to modify the VM parameters.
2. After changing the physical size of your hardware, you need to adjust the file system length. You need to to this with software on your OS.

So, if I mean shutdown your VM, I mean you do changes on hardware level. Like a real world computer, you would shut it down when installing RAM and HDD's ect. If you working with qemu tools, this is like changing the hardware. In your case, the HDD.

After you did the qemu-img resize and the virtual disk has the correct size, you would start your computer and change the filesystem specs there. This is the resize2fs. You do not need to extend the partition! This is already done by qemu-img.

If resize2fs tells you it cannot extend any more, then there are two possible causes:
(1) - you reached the maximum size
(2) - you have another partition blocking the resizing - you there may be no partitions 'in between'.

Please verify all this with parted in comparison with df (in your VM of course):
parted -l /dev/vda1
df -B 512

Open in new window

Jay LeporeAuthor Commented:
Up till now we are on the same page.  I have understood clearly your statements.  
I am guessing we have an 'in between' problem I have here the results of your suggested exercise.  I would guess the 'in between' is the problem but impassible for me !

Here is the screen shot for the first parted -l /dev/vda1 command
Parted -l /dev/vda1 Command results
Here are the results of the suggested df -B 512 command
 results of the suggested df -B 512 command
I look forward to hearing your further thoughts.

Jay Lepore
Daniel HelgenbergerCommented:
As I said: You have a swap partition after your root partition you wish to extend.

This is why you cannot extend it.
But, and this is the good part, your disk is 86GB in size. You could easily extend swap!
Note: If extending a disk, space is added in the end of the disk. Space to extend a partition needs to be adjacent the the partition you want to extend.

To fix this:
1. switch off swap,
2. delete swap logical / extended partition and verify you have only one left
3, recreate it with parted, using a primary partition in the end of the disk
4. create swap space on the new partition
5. switch swap back on
6. resize your /dev/vda1 with resize2fs - which will now be possible
7. Edit /etc/fstab, modify your swap mount entry to match the new vda2

The relevant commands for swap are:
mkswap /dev/vda2

Hint: Do not run the commands above just this way! You will need to use parted to delete the swap partition and creating it again. For the "start", end parameters use something like about 97% and 100%. This will effectively create it at the end of the HD:
parted /dev/vda mkpart primary 83GB 100%

Open in new window

If you want exactly 2GB, then you need to calculate the start poinnt. Possible units are MiB,GiB,MB,GB,%,s (sectors).  For more, see man parted.
Jay LeporeAuthor Commented:

You gave me good information however you assumed a level of command knowledge which I did not have.  

After your comments, I also reviewed the following good resources on this subject:
* http://hexeract.wordpress.com/2012/04/30/how-to-expand-the-root-filesystem-of-a-11-10-ubuntu-running-inside-vmware-player/
* http://luke.bowbak.org/2013/01/24/partitions-thing-of-the-past-1.html
* and many more sites

Together, your simple review, and all of the other sites researched allowed me to create a step by step solution for others to follow which can be seen at


I will select you as the solution but truly it was a partial solution as your detail was not clear as it could have been and much research had to follow.


Jay Lepore
Daniel HelgenbergerCommented:
Hello Jay,
I'am glad you could extend your partition finally!

Just some explanation:
The problems with the solutions here (esp. for linux) are, however: I do know nothing about your setup. For instance, I suspect you have Ubuntu, I do not know if you have any graphical desktop installed, and then, which one. For instance, you provided none. I could have asked about this in three or four questions myself - but, you provided me with most of the clues to present a solution anyway and asking myself takes a lot of time and gets your problem not solved.
And lastly, I cannot know what level of expertise you have. In this case, you started with bash stuff - from your howtos.
As you can imagine, the permutations are huge. The most reliable way I can provide solutions independently from host OS (mostly) are via command line.  For instance, the simple line with parted above are about 10 steps with graphical parted, gparted - which you may or may not have installed.

As for your expertise; I think it is not my job to google the 10 howtos where only one applies (see above). I can only point you in the right direction. In your case, googling "ubuntu gparted move swap partition" may have been the right thing (and, you never even asked).

If you start with Linux seriously (I suspected this; virtualization with with libvirt; resizing partitions, this task could easily been circumvented with for instance another mounted virtual disk) - do look at the command line stuff and get familiar with this.
If you ask questions here, at least the answers from me will always involve bash and cli tools (reasons also above).
I hope this clarifies some things - please keep in mind, everyone here is a volunteer and - except for the points - our "payment" is your problem solved :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jay LeporeAuthor Commented:
My apologies.  Of course.  I am grateful for your efforts and my problem is certainly solved. You've earned your points and I am happy.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.