My job is to help the developers be more efficient and closer to production without actually being there to screw things up 😉 . The following article is part of a series on managing extra storage on VirtualBox to mimic some of our systems in production. The series will walk through adding a disk manually in VirtualBox and will walk through the automation of it through Vagrant, Chef and Chef provisioning. The first in the series shows how to create and attach a disk in VirtualBox through the command line – this is the foundation that we will build up on for the rest of the series.
As part of my role to drive DevOps Culture within a large enterprise, I need to find things that will simplify both the life of developer by providing them tools that allow them to get closer to production and increase their productivity.For our infrastructure developer who build Chef scripts (ChefDK / kitchen / Vagrant / VirtualBox), I had to find a solution to mimic the way our disks are set up within our production infrastructure. We use a mixture of Amazon instances with Ephemeral volumes and vSphere machines with additional attached storage. I have found a way to replicate our production setup on a local VM . Additionally, this solution also allows us to provide our application developers system as similar to production as possible but on their local machine ( Vagrant / Chef-provision ).
Virtual Box Command line
VirtualBox is a excellent tool for automation because it can be managed either via an API or the command line. This is probably not the way you will be creating disks, going through the basic will help your understanding of what is being done in the next articles. All of the commands we will use are documented in the VirtualBox commands at https://www.virtualbox.org/manual/ch08.html .
The procedure is a simple series of steps. In our case, we are creating a disk in a file and attaching it to an existing VM/Controller. This is the virtual version of “plug a disk on an IDE cable”. We will :
- Create a Disk on file (virtual disk)
- Collect information form VM
- Shutdown the VM
- Attach the disk to the VM
- Power on the VM
- Validate the disk
Creating a disk
Using the VirtualBox command line, we create a disk. Our disk will be a file based .vdi. We are using a relative name, so it will be created in the local directory – but we could have used a absolute name:
VBoxManage createhd --filename test1.vdi --size 1024
This will create a new sprarse file that will be the 1024MB disk. The disk is named test1.vdi in the local directory.
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100% Disk image created. UUID: 6372b64e-9691-46f0-934c-222eff8042a3
View the file created using “ls” – as you can see the 1 GB disk currently only takes up a few MB’s.
ls -lh test1.vdi -rw------- 1 uername staff 2.0M 5 Jan 09:47 test1.vdi
Collecting information for later
To attach the disk, we need to get information on the VM we wish to attach the disk to. If you do not have one already created, you can create it form the UI or (my preferred) using vagrant init / vagrant up. To get the list of running VMS, type :
VBoxManage list vms
The output will be similar to:
This lists the name of the machine along with an ID. The ID is required later to gather more information and when attaching the disk. We collect additional information about the machine – more specifically, the Controller name and what disks are already attached.
VBoxManage showvminfo 61277c29-b6fc-4a71-8824-b18a044b0456
This is a long output with a lot of information. Look for the section that defines the Storage Controller, similar to bellow:
==> ... Truncated output ... ==> Default Frontend: ==> Storage Controller Name (0): IDE Controller ==> Storage Controller Type (0): PIIX4 ==> Storage Controller Instance Number (0): 0 ==> Storage Controller Max Port Count (0): 2 ==> Storage Controller Port Count (0): 2 ==> Storage Controller Bootable (0): on ==> IDE Controller (0, 0): /Users/achiass1/VirtualBox VMs/vagrant_test_default_1420468594658_77074/packer-ubuntu-14.04-amd64-disk1.vmdk (UUID: 7acad982-063e-48e8-936d-b476cce3e500) ==> ... Truncated output ...
From this we can see that the Controller name is “IDE Controller”, it is the controller we will attach the new disk to. We also see that the existing disk in on Port 0 and is device 0 ( the “(0,0)” ), and that we cannot have more than 2 ports. We will attach our disk to port 1 as device 1.
Attaching the disk
Now that we have a disk, a machine to attach it to and a location on that machine – lets attach it. For this we will need to power off the virtual machine. Using the Machine ID found earlier, we power off the VM.
VBoxManage controlvm 61277c29-b6fc-4a71-8824-b18a044b0456 poweroff
Wait until the machine is powered down:
Now we attach the disk to port 1 as device 1 on the “IDE Controller”. Additionally we need the disk file we create above and the fact that this is hard disk (hdd).
VBoxManage storageattach 61277c29-b6fc-4a71-8824-b18a044b0456 --storagectl "IDE Controller" --port 1 --device 1 --medium test1.vdi --type hdd
Now that our storage is attached, we can start the machine up again. Again we will need the machine ID. The “–type headless” option is used so the VM does not start a UI when the machine starts.
VBoxManage startvm 61277c29-b6fc-4a71-8824-b18a044b0456 --type headless
And the output:
Waiting for VM "61277c29-b6fc-4a71-8824-b18a044b0456" to power on... VM "61277c29-b6fc-4a71-8824-b18a044b0456" has been successfully started.
Verifying the disk
We can validate that our disk was properly attached using the showvminfo command form up above. You can see the new line – “IDE Controller (1,1)” which is the disk that has been created.
VBoxManage showvminfo 61277c29-b6fc-4a71-8824-b18a044b0456 ... Truncated information ... Default Frontend: Storage Controller Name (0): IDE Controller Storage Controller Type (0): PIIX4 Storage Controller Instance Number (0): 0 Storage Controller Max Port Count (0): 2 Storage Controller Port Count (0): 2 Storage Controller Bootable (0): on IDE Controller (0, 0): /Users/achiass1/VirtualBox VMs/vagrant_test_default_1420468594658_77074/packer-ubuntu-14.04-amd64-disk1.vmdk (UUID: 7acad982-063e-48e8-936d-b476cce3e500) IDE Controller (1, 1): /Users/achiass1/test1.vdi (UUID: 6372b64e-9691-46f0-934c-222eff8042a3) NIC 1: MAC: 0800279114CA, Attachment: NAT, Cable connected: on, Trace: off (file: none), Type: 82540EM, Reported speed: 0 Mbps, Boot priority: 0, Promisc Policy: deny, Bandwidth group: none ... Truncated information ...
And thats it! At this point the disk is still blank and must be partitioned, formated and mounted much the same way you need to do this with a regular physical disk.
We have walked through what is required to create a new HD, and attache it to a Virtual Box instance. My next instalment will look at how to define this in a Vagrant File so that the disk is created and attached automatically on Vagrant up.
Tell me how it goes ! I would love to hear how you used this in real life ?