Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a VM managed by Vagrant using VirtualBox on a Ubuntu host server. My Vagrant box uses the Puppetlabs Debian 6.0.7 basebox, which uses LVM for its root partition.

By default the disk is 8GB which is too small for my purposes. I would like to:

  1. Increase the size of the existing disk and the file system on it without destroying and recreating my VM.
  2. Configure Vagrant so that in future it will create a larger disk for this project.

Can anyone explain how to do this?

share|improve this question

2 Answers 2

up vote 8 down vote accepted

Personally I think it'll be easier to attach an extra virtual HDD and mount it to a proper mount point, for example /opt and move your stuff over using rsync to work around this issue, after all puppet vagrant boxes are for testing purpose.

Reason why: VBoxManage modifyhd only works with native VDI image. However, vagrant base boxes are basically exported OVF/OVA using VMDK format.

See VirtualBox Docs

The --resize x option (where x is the desired new total space in megabytes) allows you to change the capacity of an existing image; this adjusts the logical size of a virtual disk without affecting the physical size much.[37] This currently works only for VDI and VHD formats, and only for the dynamically allocated variants, and can only be used to expand (not shrink) the capacity.

To increase the capacity of disk for Vagrant Base Box

Steps are

  1. To be able to resize the HDD, you'll have to covert it to VDI first, e.g. VBoxManage clonehd in.vmdk out.vdi --format VDI and then re-attached it (using the GUI is easier).

  2. Resize it using VBoxManage modifyhd box.vdi --resize 15360 which increase the capacity to 15GB.

  3. However this only changes the drive capacity, you will have to grow the file system for the guest afterwards. For example, use resize2fs -p -F DEVICE for ext{3,4}.

share|improve this answer
    
Note that after resizing and before starting up the vagrant box again, you also need to update the VirtualBox storage configuration so it uses the new VDI and not the original VMDK: In VirtualBox > Storage >Remove existing hard disk > Add hard disk (select existing and point to the new VDI image) –  Steve Swinsburg Jul 10 at 4:24

I've automated adding the disk in my Vagrantfile:

Vagrant.configure("2") do |config|
    ...
    file_to_disk = File.realpath( "." ).to_s + "/disk.vdi"

    if ARGV[0] == "up" && ! File.exist?(file_to_disk) 
       puts "Creating 5GB disk #{file_to_disk}."
       vb.customize [
            'createhd', 
            '--filename', file_to_disk, 
            '--format', 'VDI', 
            '--size', 5000 * 1024 # 5 GB
            ] 
       vb.customize [
            'storageattach', :id, 
            '--storagectl', 'SATA Controller', 
            '--port', 1, '--device', 0, 
            '--type', 'hdd', '--medium', 
            file_to_disk
            ]
   ...
   config.vm.provision "shell", path: "scripts/add_new_disk.sh"
   ...
end

Where the add_new_disk.sh shell script looks like this:

set -e
set -x

if [ -f /etc/disk_added_date ]
then
   echo "disk already added so exiting."
   exit 0
fi


sudo fdisk -u /dev/sdb <<EOF
n
p
1


t
8e
w
EOF

pvcreate /dev/sdb1
vgextend VolGroup /dev/sdb1
lvextend /dev/VolGroup/lv_root
resize2fs /dev/VolGroup/lv_root

date > /etc/disk_added_date

This script is for a centos 6.4 box, but could easily be adapted to ubuntu.

Instead of adding a disk, other options include:

  • using a box with a bigger disk such as opscode bento which have 40Gb disks
  • build your own box using packer. You can use the opscode boxes packer definitions as a starting point
share|improve this answer
    
Hi There, I would like to try your solution out. Can you confirm where in the Vagrant File this section of code was added in? Kind Regards –  Rudi Strydom Sep 16 at 11:48
    
@RudiStrydom I've update the answer - hope it makes more sense now. –  SHC Sep 16 at 17:38
    
I did figure it out thank you for getting back with a response, I am sure it will help others find the result –  Rudi Strydom Sep 25 at 10:54
1  
This should add in the config.vm.provider :virtualbox do |vb| block to make things more clear –  B T Oct 5 at 2:17
1  
If you get an "specify either size or extents" error for lvextend, try lvextend -l +100%FREE /dev/VolGroup/lv_root instead –  Andomar Oct 8 at 12:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.