72

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?

1

9 Answers 9

56

I found this simplest way to resolve this problem:

  • Install this plugin: vagrant plugin install vagrant-disksize

  • Edit the Vagrantfile:

    Vagrant.configure('2') do |config|
      ...
      config.vm.box = 'ubuntu/xenial64'
      config.disksize.size = '50GB'
      ...
    end
    
  • vagrant halt && vagrant up

    • Note: this will not work with vagrant reload
6
  • 3
    Worked like a charm. Seems to be the easiest solution I've come across. Thank you, sir.
    – Rico
    Mar 14, 2019 at 21:28
  • 1
    For a Debian9 box this increases the raw disk size, but not the actual partition used by the Debian installation. The increased disk size is not directly usable.
    – Læti
    Mar 22, 2019 at 12:26
  • @Tonin did you manage to find a solution?
    – Arjun
    Jun 17, 2019 at 16:41
  • 10
    I used sudo cfdisk /dev/sda tool to resize my /dev/sda1 partition to all newly available space. Then I told my filesystem to take use of that space with sudo resize2fs -p -F /dev/sda1
    – tutuDajuju
    Aug 12, 2019 at 10:02
  • 1
    Thank you. This works on my particular configuration too. Host OS: Windows 10, Virtual Box: 6.0, VM OS: Ubuntu Xenial Aug 15, 2020 at 1:56
27

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 purposes.

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 convert 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 increases the capacity to 15GB.

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

3
  • 3
    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) Jul 10, 2014 at 4:24
  • 1
    For Debian users, I suggest also looking at this post: blog.lenss.nl/2012/09/resize-a-vagrant-vmdk-drive. When I got to step 3 it was not quite as simple as running resize2fs. Good luck! Feb 1, 2015 at 21:18
  • 2
    I just wrote a consolidated guide on this: medium.com/@phirschybar/… Feb 1, 2015 at 21:47
15

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
10
  • 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 Sep 16, 2014 at 11:48
  • @RudiStrydom I've update the answer - hope it makes more sense now.
    – Chris Snow
    Sep 16, 2014 at 17:38
  • 2
    This should add in the config.vm.provider :virtualbox do |vb| block to make things more clear
    – B T
    Oct 5, 2014 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, 2014 at 12:19
  • 1
    Your example would create a 5TB disk (the --size unit is MB, see virtualbox.org/manual/ch08.html#vboxmanage-createvdi).
    – backflip
    Jan 31, 2015 at 10:00
8

Install Vagrant plugin vagrant-disksize

vagrant plugin install vagrant-disksize

If you want to add make sure user has the plugin installed when starting vagrant you can add this in the beginning of Vagrantfile

# Install vagrant-disksize to allow resizing the vagrant box disk.
unless Vagrant.has_plugin?("vagrant-disksize")
    raise  Vagrant::Errors::VagrantError.new, "vagrant-disksize plugin is missing. Please install it using 'vagrant plugin install vagrant-disksize' and rerun 'vagrant up'"
end

Set desired disk size in Vagrantfile

vagrant.configure('2') do |config|
    config.disksize.size = '50GB'
end

Updating existing vagrant box

  1. Do all of the above
  2. Run vagrant halt & vagrant up (You should see something like "Resized disk: old 32768 MB, req 51200 MB, new 51200 MB")
  3. SSH to vagrant box
  4. Run sudo cfdisk /dev/sda
  5. Use arrows to select your disk probably sdaX. Mine was sda3.
  6. Then select resize using arrow keys. Accept the suggested disk size.
  7. Then select write. And answer yes.
  8. You can select quit now.
  9. Run sudo resize2fs -p -F /dev/sdaX You should see something like: "Filesystem at /dev/sda3 is mounted on /; on-line resizing required old_desc_blocks = 4, new_desc_blocks = 6 The filesystem on /dev/sda3 is now 11933952 (4k) blocks long. "
  10. Run df and see that your disk size has increased.
7

You can use the plugin, which will enable you to specify the disk size https://github.com/sprotheroe/vagrant-disksize

1
  • This resized my disk and expanded the partition in only one line of configuration. Very slick! Mar 3, 2019 at 15:01
0

disclaimer: answer most likely applicable only for distros using logical volume management like rhel for which the following use case applies:

I have been using the official Red Hat Enterpise 7.2 vagrant box officially provisioned by red hat.

(you will need a rhel developer account which you can create for free)

Upon installation I was troubled by the fact that only 8GB were also available:

sudo df -h
[vagrant@rhel-cdk ~]$ sudo df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-root   8G  2,5G   6,5G  28% /
devtmpfs                     234M     0  234M   0% /dev
tmpfs                        245M     0  245M   0% /dev/shm
tmpfs                        245M  4,3M  241M   2% /run
tmpfs                        245M     0  245M   0% /sys/fs/cgroup
/dev/sda2                    297M  134M  164M  45% /boot
tmpfs                         49M     0   49M   0% /run/user/1000

And as you can see most of the space had already been consumed by the rhel installation.

However I discovered that the volume group corresponding to the /dev/mapper/VolGroup00-root fileystem did have extra space (which I suppose in terms of virtual box was dynamicaly allocated)

sudo vgdisplay VolGroup00
  --- Volume group ---
  VG Name               VolGroup00
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  11
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               39,70 GiB
  PE Size               4,00 MiB
  Total PE              10164
  Alloc PE / Size       7872 / 30,75 GiB
  Free  PE / Size       2292 / 8,95 GiB
  VG UUID               JBVwpl-13KX-HbQw-FqUa-CA9w-swpF-dF6glm

So the only things remaining to be done were to:

a) Increase the size of our logical volume by say 10G

sudo lvextend -L+10G /dev/VolGroup00/root

and

b) Update your root filesystem so that it becomes aware of this change

sudo xfs_growfs /dev/mapper/VolGroup00-root

(pls note that at least for rhel 7.2 resize2fs will not work for the this purpose).

After that, additional space was available to my filesystem:

sudo df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-root   18G  2,5G   16G  14% /
devtmpfs                     234M     0  234M   0% /dev
tmpfs                        245M     0  245M   0% /dev/shm
tmpfs                        245M  4,3M  241M   2% /run
tmpfs                        245M     0  245M   0% /sys/fs/cgroup
/dev/sda2                    297M  134M  164M  45% /boot
tmpfs                         49M     0   49M   0% /run/user/1000

... no intervention on virtualbox level

0

increase the disk size of VM centos 7

  1. On the host install the plugin:

    vagrant plugin install vagrant-disksize

  2. Edit vagrant file to set the disk size:

    vagrant.configure('2') do |config| 
        config.vm.box = 'centos/7' 
        config.disksize.size = '50GB'
    end

3: Boot up the VM 4: Install the growpart and grow the disk size to full

yum install epel-release -y yum install cloud-utils-growpart.x86_64 -y growpart /dev/sda 1

5: Download gparted live iso image and attach it as a cdrom to the VM and boot into gpartedlive using the Virtualbox GUI reboot with gparted live

6: With gparted check partition to clear the error flag

0

The following Vagrantfile (placed in the root of my cookbook's folder) worked for me with Chef's test-kitchen:

###########################################################################################################
# The in-line script that will be used to grow our disk partition to the size specified in our Vagrantfile.
###########################################################################################################
$script = <<-SCRIPT
#!/bin/bash
ROOT_DISK_DEVICE="/dev/sda"
ROOT_DISK_DEVICE_PART="/dev/sda5"
LV_PATH=`sudo lvdisplay -c | sed -n 1p | awk -F ":" '{print $1;}'`
FS_PATH=`df / | sed -n 2p | awk '{print $1;}'`
ROOT_FS_SIZE=`df -h / | sed -n 2p | awk '{print $2;}'`
echo "The root file system (/) has a size of $ROOT_FS_SIZE"
echo "> Increasing disk size of $ROOT_DISK_DEVICE to available maximum"
sudo parted /dev/sda resizepart 2 100%
sudo parted /dev/sda resizepart 5 100%
sudo pvresize $ROOT_DISK_DEVICE_PART
sudo lvextend -l +100%FREE $LV_PATH
sudo resize2fs -p $FS_PATH
ROOT_FS_SIZE=`df -h / | sed -n 2p | awk '{print $2;}'`
echo "The root file system (/) has a size of $ROOT_FS_SIZE"
exit 0
SCRIPT

# Fail if the vagrant-disksize plugin is not installed
unless Vagrant.has_plugin?("vagrant-disksize")
  raise 'vagrant-disksize is not installed!'
end

Vagrant.configure("2") do |config|
  config.disksize.size = "150GB"
  config.vm.provision "shell", inline: $script
end

Along with the following little snippet added to my kitchen.yml:

  vagrantfiles:
    - Vagrantfile
  provision: True

Place that directly beneath:

driver:
  name: vagrant

Replace the script value with whatever you want... this one was used to extend the filesystem of an Ubuntu 20.04 virtualbox image.

0

Vagrant has recently added experimental support for custom disk sizes (including disk resizing) with the VirtualBox provider. Some common usage scenarios are documented here and here.

To activate this feature, modify your Vagrantfile (see the example below), set the VAGRANT_EXPERIMENTAL="disks" environment variable in your shell and then run vagrant up.

Here is an example Vagrantfile tested on Vagrant 2.2.19, using the bento/ubuntu-20.04 base box (which is based on LVM):


Vagrant.configure("2") do |config|
  ...
  config.vm.disk :disk, size: "150GB", primary: true
  config.vm.box = "bento/ubuntu-20.04"
  config.vm.box_version = "202005.21.0"
  config.vm.box_check_update = false
  ...
end

You should see the following output (more or less) during vagrant up:

==> vagrant: You have requested to enabled the experimental flag with the following features:
==> vagrant:
==> vagrant: Features:  disks
==> vagrant:
==> vagrant: Please use with caution, as some of the features may not be fully
==> vagrant: functional yet.
Bringing machine 'server' up with 'virtualbox' provider...
==> server: Clearing any previously set forwarded ports...
==> server: Clearing any previously set network interfaces...
==> server: Preparing network interfaces based on configuration...
    server: Adapter 1: nat
==> server: Forwarding ports...
    server: 22 (guest) => 49222 (host) (adapter 1)
    server: 4200 (guest) => 49280 (host) (adapter 1)
    server: 4243 (guest) => 49243 (host) (adapter 1)
==> server: Configuring storage mediums...
    server: Disk 'vagrant_primary' needs to be resized. Resizing disk...

This process can take a while to complete (in my case, between 1 and 2 hours).

Use vagrant ssh to enter your virtual machine, and then use the following commands (based on this answer) to gather information about your filesystem and then to expand the LVM volume. If your base box does not use LVM, then you shouldn't use these commands.

# Understand your partitions.
> lsblk
> df -lhT /
> sudo fdisk -l /dev/sda
> sudo vgdisplay
> sudo parted /dev/sda print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 161GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 
Number  Start   End     Size    Type      File system  Flags
1      1049kB  538MB   537MB   primary   fat32        boot
2      539MB   68.7GB  68.2GB  extended
5      539MB   68.7GB  68.2GB  logical                lvm

# Resize your partitions.
> sudo parted /dev/sda resizepart 2 100%
> sudo parted /dev/sda resizepart 5 100%
> sudo parted /dev/sda print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 161GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 
Number  Start   End    Size   Type      File system  Flags
1      1049kB  538MB  537MB  primary   fat32        boot
2      539MB   161GB  161GB  extended
5      539MB   161GB  161GB  logical                lvm

# Resize your LVM physical volume to match the new partition extents.
> sudo pvresize /dev/sda5
> sudo vgdisplay
Alloc PE / Size       16255 / <63.50 GiB
Free  PE / Size       22016 / 86.00 GiB

# Resize your LVM logical volume to fill the available Free space.
> sudo lvdisplay
--- Logical volume ---
LV Path                /dev/vgvagrant/root
> sudo lvextend -l+100%FREE /dev/vgvagrant/root
Size of logical volume vgvagrant/root changed from <62.54 GiB (16010 extents) to <148.54 GiB (38026 extents).
Logical volume vgvagrant/root successfully resized.

# Apply the changes to the filesystem.
> sudo resize2fs /dev/vgvagrant/root
> df -lhT /
Filesystem                 Type  Size  Used Avail Use% Mounted on
/dev/mapper/vgvagrant-root ext4  146G   55G   85G  40% /

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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