Earlier this week, I had a pressing matter of disk space unaccountably running out on my Vagrant box. Vagrant machines are commonly quite small. Normally, that would be no big problem, since LAMP stacks generally do not need much disk space. However, suddenly I had an unusable vagrant box. Damn.
I love vagrant. I use it in conjunction with VirtualBox and it enables me to have a development environment ready in mere minutes. Upon first provisioning a Vagrant box, a Virtualbox disk image of your choice is downloaded and installed and a number of things are configured out of the box. Furthermore, I can share its config file with my team, which ensures that our development environments match. Since the data can be stored on the host machine, disk space demands are normally modest.
But... Mysql has a 'feature' that causes the innodb log file to grow. And grow. And grow. And it is not possible to shrink it again without borking the entire mysql installation. That really sucks.
I suddenly had the challenge of resizing my Virtualbox disk without losing data. After some searching and trial and error, I managed to get rid of the problems. Here's how:
Step 1: Convert VMDK image to VDI
cd ~/Virtualbox VMs/<your_vagrant_box>/ VBoxManage clonehd box-disk1.vmdk box-disk1.vdi –format VDI # Clone from VMDK to VDI format; VBoxManage modifyhd box-disk1.vdi –resize 20000 # Resize to 20000 MB
Now launch the VirtualBox console and open the settings of your box. Go to
Storage > Hard Disks and connect the VDI image to the SATA interface instead of the VMDK one. Save your settings and try to boot the Virtualbox image. Does it work? Good!
Step 2: Resize the partition(s)
There are apparently several ways to achieve this, but the one described here worked quite well for me. You will need the Gparted boot CD image. Stop your VirtualBox machine. Open the settings again and go to Storage. Under 'Optical Drives', you can connect ISO files. Select the downloaded ISO file and connect it to your virtual CD drive. Then, make sure that the boot order of your virtual machine tries to use the CD before the HDD. One other setting that I needed, is EFI support. Go to
System >> Advanced and enable EFI support.
Boot your virtual box and ensure that GParted is started. You see a nice little X screen with a shortcut to GParted. Open it, resize your partitions (or create a new one alongside your old partition), save your partition table and shutdown your virtual machine.
Now go to your Vagrant directory and make sure that your vagrant box boots. Just give the command
vagrant up. Your Vagrant box should be up in a minute. Enter your box with
vagrant ssh and use the
df command to make sure that your partitions are the right size. Then, do a little happy dance.
Step 3: Make mysql behave
Of course, you'll have to make sure that your problem will not rear its ugly fat ass again. Your Innodb files will keep growing if you do not take any measures and you do not want to resize your partitions every few months. Fortunately, you can teach mysql how to behave better:
First, log into your vagrant box and make sure you have root access. Stop the mysql server entirely. Open
/etc/mysql/my.cnf and add the following line in the
Start mysql again, dump all databases except the system tables (mysql, information_schema, performance_schema) and stop mysql yet again. Now it's time to clean up the mess. Go to
/var/lib/mysql and remove (or backup)
ibdata1, and the ib_logfiles . Start mysql again. Import the dumped mysql databases again and you should be set. The ibdata and iblog files will reappear, but they should behave better now.
Step 4: profit
Not only did you resize your virtualbox partition successfully, you made mysql behave somewhat better.