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.– eyoung100Commented Dec 30, 2014 at 21:46
-
2@eyoung100 ah thanks, I guess that confirms that it can't be done prior to creating the vm.– a7omitonCommented Dec 30, 2014 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.– eyoung100Commented Dec 30, 2014 at 21:52
-
@eyoung100 it just seemed like an extra step, having to resize the VM after creating it– a7omitonCommented Dec 30, 2014 at 21:58
-
Vagrant destroys the VM after you shut down vagrant anyway IIRC.– eyoung100Commented Dec 30, 2014 at 22:01
8 Answers
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
-
1Doesn't work with Ubuntu 17.10 (Artful Aardvark) #10 : github.com/sprotheroe/vagrant-disksize/issues/10– DavidCommented Dec 24, 2017 at 21:40
-
3It worked for Ubuntu 16.04 LTS. As you mention, just install the plugin with the command:
vagrant plugin install vagrant-disksize
Thanks!– EdenshawCommented Mar 28, 2018 at 16:37 -
3
-
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?– a7omitonCommented Dec 30, 2014 at 22:48
-
True, it will just resize the hard drive, you would then have to grow the partition in the VM Commented Dec 31, 2014 at 1:01
-
8As 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. Commented May 22, 2015 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. Commented May 22, 2015 at 23:10
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.– TPSCommented Dec 21, 2015 at 10:34
-
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? Commented Mar 27, 2017 at 7:34
-
This is a working solution. Although you have to edit quite a few params depending on which image you're using. Commented Oct 16, 2017 at 4:39
@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
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 /
short answer; go to the box file in your ~/.vagrant.d/boxes/<yourbox>/box
file and edit lines:
<DiskSection>
<Info>Virtual disk information</Info>
<Disk ovf:capacity="42949672960" ovf:capacityAllocationUnits="byte" ovf:diskId="vmdisk1" ovf:fileRef="file1" ovf:format="http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized" ovf:populatedSize="0"/>
<Disk ovf:capacity="10485760" ovf:capacityAllocationUnits="byte" ovf:diskId="vmdisk2" ovf:fileRef="file2" ovf:format="http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized" ovf:populatedSize="0"/>
</DiskSection>
That will edit the initial disk size more on the matter (short read) here
How can I add more space to my vagrant file virtual box when it says config.vm.provider "virtualbox" do |v| v.memory = 4096 v.cpus = 4
end
end
~
~
~
~
~
~
"Vagrantfile" [noeol] 17L, 359B
-
-
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review– AdminBeeCommented Nov 7, 2022 at 10:39