41

Is there a way to set the storage size for the VM on creating it? I will be using Vagrant, but not sure if this is something that needs to be done in VirtualBox or a setting I can include in the Vagrantfile (I checked docs but there doesn't seem to be any indication)

  • Google for "Vagrant Disk Size Virtualbox" and find this post on AskUbuntu. – eyoung100 Dec 30 '14 at 21:46
  • 2
    @eyoung100 ah thanks, I guess that confirms that it can't be done prior to creating the vm. – a7omiton Dec 30 '14 at 21:51
  • Well, it can if you create the VirtualDisk without Vagrant, but then you'd not need to use vagrant if you wanted that. – eyoung100 Dec 30 '14 at 21:52
  • @eyoung100 it just seemed like an extra step, having to resize the VM after creating it – a7omiton Dec 30 '14 at 21:58
  • Vagrant destroys the VM after you shut down vagrant anyway IIRC. – eyoung100 Dec 30 '14 at 22:01
23

The vagrant-disksize plugin makes this easy. Create a debian-9 vm with a 20gb hard drive.

minimally:

Vagrant.configure("2") do |config|
    config.vm.box = "debian/stretch64" 
    config.disksize.size = "20GB"
end

or, using auto-install logic for the plugin:

Vagrant.configure("2") do |config|

    required_plugins = %w( vagrant-vbguest vagrant-disksize )
    _retry = false
    required_plugins.each do |plugin|
        unless Vagrant.has_plugin? plugin
            system "vagrant plugin install #{plugin}"
            _retry=true
        end
    end

    if (_retry)
        exec "vagrant " + ARGV.join(' ')
    end

    config.vm.box = "debian/stretch64" 
    config.disksize.size = "20GB"
end
  • Doesn't work with Ubuntu 17.10 (Artful Aardvark) #10 : github.com/sprotheroe/vagrant-disksize/issues/10 – David Dec 24 '17 at 21:40
  • 2
    It worked for Ubuntu 16.04 LTS. As you mention, just install the plugin with the command: vagrant plugin install vagrant-disksize Thanks! – Edenshaw Mar 28 '18 at 16:37
  • 1
    Seems to only work to extend the disk, not to shrink it. – xenoid Jun 25 '18 at 15:01
  • Worked like a charm. – wny May 4 at 1:52
8

It's not possible unless you know the UUID of the disk in question. If you did, you would need to have the following in your Vagrantfile:

Vagrant.configure("2") do |config|
  # ... (other config)

  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyhd", "disk id", "--resize", "size in megabytes"]
  end
end

where "size in megabytes" is the HD size in megabytes, and "disk id" is the UUID of the disk (not the VM)

Alternatively, you could do this via VBoxManage:

$ VBoxManage list hdds
[get the UUID of the disk in question from the output]
$ VBoxManage modifyhd [UUID] --resize [size in MiB]
  • Doing the alternate solution would not create the extra storage space in the actual virtual machine, or will it? – a7omiton Dec 30 '14 at 22:48
  • True, it will just resize the hard drive, you would then have to grow the partition in the VM – felixphew Dec 31 '14 at 1:01
  • 7
    As mentioned in the other threads, this usually doesn't work since most Vagrant boxes are created with .vmdk virtual disk files instead of the .VDI format preferred by VBoxManage. – Mark Hudson May 22 '15 at 23:06
  • @MarkHudson Sorry, I wasn't aware of this (I just made this answer based on my knowledge of VirtualBox and the Vagrant documentation, I don't really use Vagrant much). Thanks for pointing this out! Can VirtualBox not resize VMDKs? I've never tried it. Will update my answer. – felixphew May 22 '15 at 23:10
6

I think Resolving some common issues of this may help you.


And this is the essential part

Increase Disk space

Normally (sadly), cloud base box comes with VMDK disks formats. If you are lucky and your disk format is VDI, you can solve this directly like this. But, VMDK can't be expanded, so you need to add another disk to your VM. If making this manually is hard, can you imaging doing this from Vagrant? Well, now that I solved is not that difficult:

Create an script called "bootstrap.sh" on your working directory, and add these lines:

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

VolGroup and lv_root can change on different distributions. But it works for me on Ubuntu also.

And then add this code to your Vagrantfile:

...
  config.vm.provider :virtualbox do |vb|
  ...
  # Validate this should be run it once
  if ARGV[0] == "up" && ! File.exist?("./disk1.vdi")
    vb.customize [
      'createhd',
      '--filename', "./disk1.vdi",
      '--format', 'VDI',
      # 100GB
      '--size', 100 * 1024
    ]

    vb.customize [
      'storageattach', :id,
      '--storagectl', 'SATA Controller',
      '--port', 1, '--device', 0,
      '--type', 'hdd', '--medium',
      file_to_disk
    ]
  end

  if ARGV[0] == "up" && ! File.exist?("./disk1.vdi")
    # Run script to map new disk
    config.vm.provision "bash", path: "bootstrap.sh"
    # Run script to increase swap memory
    config.vm.provision "bash", path: "increase_swap.sh"
  end
...

This creates a VDI disk file with 100GB of capacity. And is attached to your OS.

This problem is solved when you create cloud Vagrant boxes, like AWS EC2 instances with Vagrant :D

Increase Swap Memory

As I install Oracle Fusion Middleware products, they require some amount of swap memory, but base box comes with a small amount of swap.

To resolve this, add this script called "increase_swap.sh" on your working directory:

#!/bin/sh

# size of swapfile in megabytes
swapsize=8000

# does the swap file already exist?
grep -q "swapfile" /etc/fstab

# if not then create it
if [ $? -ne 0 ]; then
  echo 'swapfile not found. Adding swapfile.'
  fallocate -l ${swapsize}M /swapfile
  chmod 600 /swapfile
  mkswap /swapfile
  swapon /swapfile
  echo '/swapfile none swap defaults 0 0' >> /etc/fstab
else
  echo 'swapfile found. No changes made.'
fi

# output results to terminal
df -h
cat /proc/swaps
cat /proc/meminfo | grep Swap

If you destroy and up your box now, you will have a new box with 8GB of swap memory added and 100GB of additional disk space.

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Tejas Dec 21 '15 at 10:34
  • Got it.Had changed, – Forelax Dec 21 '15 at 10:45
4

How I did this in Vagrantfile. For people searching ...

ubuntu.vm.provider "virtualbox" do |virtualbox|
  virtualbox.name = "Ubuntu_1510_#{Time.now.getutc.to_i}"
  virtualbox.customize [
      "storagectl", :id, 
      "--name", "SATAController", 
      "--controller", "IntelAHCI", 
      "--portcount", "1", 
      "--hostiocache", "on"
  ]
  virtualbox.customize [
      "clonehd", "#{ENV["HOME"]}/VirtualBox VMs/Vagrant Test Boxes/#{virtualbox.name}/box-disk1.vmdk", 
                 "#{ENV["HOME"]}/VirtualBox VMs/Vagrant Test Boxes/#{virtualbox.name}/ubuntu.vdi", 
      "--format", "VDI"
  ]
  virtualbox.customize [
      "modifyhd", "#{ENV["HOME"]}/VirtualBox VMs/Vagrant Test Boxes/#{virtualbox.name}/ubuntu.vdi",
      "--resize", 100 * 1024
  ]
  virtualbox.customize [
      "storageattach", :id, 
      "--storagectl", "SATAController", 
      "--port", "0", 
      "--device", "0", 
      "--type", "hdd",
      "--nonrotational", "on",
      "--medium", "#{ENV["HOME"]}/VirtualBox VMs/Vagrant Test Boxes/#{virtualbox.name}/ubuntu.vdi" 
  ]
end
  • I've done this but the new disk have unassigned free space, how can I assign the available memory to the active primary partition? – eifersucht Mar 27 '17 at 7:34
  • This is a working solution. Although you have to edit quite a few params depending on which image you're using. – servermanfail Oct 16 '17 at 4:39
2

@Anders had the answer that worked best for me (initializing the box with more memory) but i did have to make a few small tweaks that actually made it simpler.

ubuntu.vm.provider "virtualbox" do |virtualbox|
virtualbox.name = "Ubuntu_ProjectName"

virtualbox.customize [
  "clonehd", "#{ENV["HOME"]}/VirtualBox VMs/#{virtualbox.name}/box-disk1.vmdk",   # Note that this may not be the correct file name
             "#{ENV["HOME"]}/VirtualBox VMs/#{virtualbox.name}/ubuntu.vdi", 
  "--format", "VDI"
]
virtualbox.customize [
  "modifyhd", "#{ENV["HOME"]}/VirtualBox VMs/#{virtualbox.name}/ubuntu.vdi",
  "--resize", 100 * 1024
]
virtualbox.customize [
  "storageattach", :id, 
  "--storagectl", "SCSI",  # Instead of the first customize section i just referenced the existing SCSI controller as I am on a mac which uses SCSI by default 
  "--port", "0", 
  "--device", "0", 
  "--type", "hdd",
  "--nonrotational", "on",
  "--medium", "#{ENV["HOME"]}/VirtualBox VMs/#{virtualbox.name}/ubuntu.vdi" 
]
end
1

For Centos7 I used Yujie's answer but with a couple of changes. First use an IDE controller, editing the --storagectl:

    v.customize [
          'storageattach', :id,
          '--storagectl', 'IDE',
          '--port', 1, 
          '--device', 0,
          '--type', 'hdd', 
          '--medium',  ".vagrant/machines/target/virtualbox/disk1.vdi"
        ]

Then in the bootstrap.sh the volume names are different and the file system is XFS, so this becomes

sudo pvcreate /dev/sdb
sudo vgextend VolGroup00 /dev/sdb
sudo lvextend /dev/VolGroup00/LogVol00 /dev/sdb
sudo xfs_growfs /

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.