Fredrik Wendt
Fredrik Wendt

Reputation: 1128

How do I resize a VDI and it's contained filesystem from the host OS?

There a lots of descriptions out there on how to resize a Virtualbox Disk Image (VDI), and then boot a live CD to resize it's filesystem(s), reboot from the now resized disk and voila: you've extended the size of the filesystems in the VDI.

I would like to do this using the host OS, which is Linux (Ubuntu 12.04 LTS) only. The point of this is to be able to automate it and also have the process take less time. How can I do this?

Upvotes: 1

Views: 1879

Answers (3)

ldav1s
ldav1s

Reputation: 16305

I tried various suggestions on how to do this from the net but on an embedded distro (e.g. OpenWrt) the gparted trick from the guest side isn't going to work (though admittedly parted might) because you just don't have all the prerequisites installed. So you have to do it from outside the guest.

What I ended up doing was going back to the image file. If you don't have the original image around you can get it from the .vdi:

VBoxManage internalcommands  converttoraw file.vdi disk.img

Now you can do something like:

qemu-img resize disk.img +10G

to add 10 GiB to the disk for example. Then:

kpartx -av disk.img

kpartx should output which loopback device it's using for the block device (e.g. loop0, AKA /dev/loop0). Then do this:

ls -la /dev/mapper/loop0p*

to see the devices kpartx has inserted under the /dev tree (e.g. /dev/dm-8) for each partition in the disk image. You'll need to know these, to fake out gparted which likes it's partitions called something else like /dev/loop0_partx. So create these fake links for gparted

ln -s /dev/dm-8 /dev/loop0_part1
...

So you can finally do the resize, etc. in gparted

gparted /dev/loop0

Don't forget to clean up after the image is all how you want it:

kpartx -d disk.img
rm /dev/loop0_part1
...

and reimport disk.img as a .vdi.

VBoxManage convertfromraw --format VDI disk.img disk.vdi

BTW, I did try the accepted answer, but gparted just wouldn't resize.

Upvotes: 0

Mohammad Nimer
Mohammad Nimer

Reputation: 1

Simply you have to follow the following steps:

  1. Power off your machine.
  2. Right click on virtual machine name > Settings > Storage
  3. Click on Controller : SATA > Add Hard Disk.
  4. Choose the new hard disk drive type size and hit create.
  5. Discard the machine state.
  6. Insert Ubuntu Live CD.
  7. Boot from ubuntu live cd.
  8. Open "gparted" (It's installed, not need to installation).
  9. Check if the system see your new created hard disk.
  10. Open Terminal.
  11. Type the following code.
  12. sudo dd if=/dev/sda of=/dev/sdb (The first is the old partition path, the second is the new partition path).
  13. Wait until its finish copying data (This step may take some time according to your host specs).
  14. After finish copying, return to gparted and select refresh devices.
  15. Select the new partition /dev/sdb it must be typical to the old one after doing dd command.
  16. It'll show the expanded space as unlocated data.
  17. Delete Swap partition/Extended partition.
  18. Right click on root partition /dev/sdb > Resize
  19. Allocate the whole space without swap allocation.
  20. Create new extended partition > Choose extended > Create
  21. Create new linux-swap partition > choose linux-swap > Create
  22. Now turn off your running machine.
  23. Right click on machine > settings > Storage.
  24. Eject ubuntu live cd.
  25. Right click on the old hard disk > remove attachment.
  26. Now you can start your vm from the newly created hard disk.
  27. Check the storage by enter df -kh command.
  28. It must show you the new size.

Congratulation, enjoy your free space.
This video will help you: https://youtu.be/ikSIDI535L0

Upvotes: -1

Fredrik Wendt
Fredrik Wendt

Reputation: 1128

Looks like this can be achieved (not always with predictable/stable/reliable result) using Qemu's NBD tool(s), as is described by Jeff Waugh in http://bethesignal.org/blog/2011/01/05/how-to-mount-virtualbox-vdi-image/ which essentially goes like this:

sudo aptitude install qemu-utils
sudo modprobe nbd
VBoxManage modifyhd <vdi-file> --resize <new_size>
sudo qemu-nbd -c /dev/nbd0 <vdi-file>
sudo gparted /dev/nbd0
sudo qemu-nbd -d /dev/nbd0

Upvotes: 2

Related Questions