четвъртък, 30 юни 2016 г.

How to resize partition in virtual machine

First, find the directory where your  *.qcow2 file resides.
$ ls -la *.qcow2
should show the file you're looking for
Then use qemu to resize the file.
$sudo qemu-img resize X*.qcow2 +10GB
will add 10GB to your file. Make sure not to add more GBs than you'd like the file to have, because then you cannot shrink it back to less. I found it out the hard way :)
All this is done with the virtual machine turned off.
Then comes the complicated part.
You need to convince you're virtual OS that it has more space. So this is what I did (following source):
So start your virtual machine, start a terminal and work there.
 First, check your current partition table:
$df -h
and make sure you're working on the correct partition. In my case, I had to work on /dev/sda.
Then delete the last partition on /dev/sda
$su
$fdisk /dev/sda
$p
to print the current partition table.
I had
/dev/sda1 -> primary, root partition
/dev/sda2 -> extended
/dev/sda5 -> SWAP.
Obviously different situation from the instructions, so I decided to delete /dev/sda2 and work from there.
So to delete /dev/sda2
$d
$2
$w
This finalizes the destruction.
Then I had the space in the end of my virtual hard empty. I created a new partition
$fdisk /dev/sda
$n
$p
all else was the default ones, just make sure the final cylinder is indeed the final of the "disk", because for me, the first time, it wasn't.
After that you finish with $w and exit.
Then you need to reboot to finalize.
$sudo reboot
In the better case, you could use
$resize2fs /dev/sda1

to expand your root hard. It didn't work for me, so I started gparted
$sudo gparted
and deleted the new partition, because resize2fs gave me some error with cylinders.
After deleting, I created a new ext4 partition and I hit "Apply". The idea was to fix the error I got previously.
Then when it was ready, I deleted the new partition, clicked on /dev/sda1 and told it to resize to the end (or almost) of the now unformatted region. I left little space in the end which I formatted as swap, just so that I have some. Then I clicked Apply and gparted did the magic.
Now I have 20GB bigger virtual hard, which is 10GB more than I planned for, but still, it all seems to work fine.
Yay!