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 7 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:

    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"

Where add_new_disk.sh 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 -L +5G /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.

share|improve this answer

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.