Increase the available disk on a CentOS host maintained by Vagrant and VirtualBox

Filed under: Linux, — Tags: Centos, LVM, Linux, Vagrant, VirtualBox — Thomas Sundberg — 2015-09-10

Virtual servers are fantastic when you need to test something and want to make sure that you have a well know environment. A virtual server has no value. You can create it over and over again and it will only cost you some time to start and stop.

VirtualBox and Vagrant is a really easy combination to get started with. However, it may turn ut that the base box you use need to be enhanced. I will show you how you can increase the disk space available on the virtual host. Unfortunately, it isn't as easy as one could hope for. But it is possible and I will show you how. When the disk has been increased, I will repackage the virtual host so it can be reused easily.

I assume that you have VirtualBox and Vagrant installed. If not, stop here and install them. I also assume that you are able to run commands from a shell. I am on a Mac and this has been tested in my terminal. You might want to consider installing Cygwin if you are using a broken os.

I want to make it clear where each command is executed, either in my terminal och the mac or in the virtual host. I will indicate the different environments like this:
The mac terminal: mac$ ls
The virtual host terminal: virtaul$ pwd
This should be interpreted as that I did ls on the mac and pwd on the virtual host.

I would cut and paste each command to the terminal. If you do so, do not include the initial part (mac$ or virtaul$).

The problem

I need to have a test host running CentOS. I will start from a base box created by Puppet Labs. The problem, however, is that the disk is too small for the application I want to test.

This means that the first thing I want to do is to increase the available disk space. This step by step guide has been my inspiration for increasing the disk: https://gist.github.com/christopher-hopper/9755310

As far as I know, I am not doing anything that is CentOS specific. This means that I expect this guide to be usable for any Linux box that uses LVM for managing its disks.

Let's go through a scenario step by step.

Create a home for virtual boxes

My virtual boxes should have a home in ~/virtual-boxes and this CentOS test host should live in ~/virtual-boxes/centos

Get a new box and start it

Get a new Vagrant box for VirtualBox:

mac$ vagrant box add puppetlabs/centos-6.6-64-puppet --provider virtualbox

This will download the needed resources from the Internet. It may take a while, it is a large download.

Change to the home directory for this virtual box:

mac$ cd ~/virtual-boxes/centos

Initiate it:

mac$ vagrant init puppetlabs/centos-6.6-64-puppet

Start it:

mac$ vagrant up

SSH into it:

mac$ vagrant ssh

Verify that we have a LVM system

Check the physical volume:

virtaul$ sudo pvdisplay

The result is:

  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               VolGroup
  PV Size               19.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4994
  Free PE               0
  Allocated PE          4994
  PV UUID               wqDj0S-MIE8-RITx-Otf7-WsSe-5pPg-XYPCJw

This gave us something usable back, we have a LVM volume and can increase it.

Exit the host.

virtaul$ exit

Stop it.

mac$ vagrant halt

Locate the disk that should be increased

List all virtual boxes that VirtualBox is aware of:

mac$ VBoxManage list vms

I found "centos_default_1441182442099_63483" {b8bd7884-9c1a-4680-8fd6-52ac5b2d9ff3}

Locating the disk means searching for it using the box name. Let's do

mac$ VBoxManage showvminfo centos_default_1441182442099_63483

This tells me much more than I need. Let me grep for anything with the suffix 'vmdk'.

mac$ VBoxManage showvminfo centos_default_1441182442099_63483 | grep vmdk

The result looked like this:

IDE Controller (0, 0): /Users/tsu/VirtualBox VMs/centos_default_1441182442099_63483/packer-centos-6.6-x86_64-virtualbox-vagrant-puppet-1437997185-disk1.vmdk (UUID: 21a9238f-1ffe-4c53-8b0c-3168a2026f30)

I notice that the disk is stored in ~/VirtualBox VMs. That is the default location for VirtualBox files.

Clone the disk

VMDK disks can't be extended. I will therefore clone it to a vdi disk that can be extended.

mac$ cd ~/VirtualBox\ VMs/centos_default_1441182442099_63483/

mac$ VBoxManage clonehd packer-centos-6.6-x86_64-virtualbox-vagrant-puppet-1437997185-disk1.vmdk clone-disk1.vdi --format vdi

Find the current disk size

Let's see how large the current disk is

mac$ VBoxManage showhdinfo clone-disk1.vdi

I got:

UUID:           62bb925f-ea3a-4325-85ac-7bfeaeb97761
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       /Users/tsu/VirtualBox VMs/centos_default_1441182442099_63483/clone-disk1.vdi
Storage format: VDI
Format variant: dynamic default
Capacity:       20480 MBytes
Size on disk:   1854 MBytes
Encryption:     disabled

This tells me that the current size is 20 GBytes, 20480 MBytes. That is too small for stuff I want to test. I will resize it to 40 GBytes.

mac$ VBoxManage modifyhd clone-disk1.vdi --resize 40960

Find the name of the storage controller

Find out the name of the storage controller this resized disk should be attached to.

mac$ VBoxManage showvminfo centos_default_1441182442099_63483 | grep Storage

I got

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

Attach the disk to the storage controller

mac$ VBoxManage storageattach centos_default_1441182442099_63483 --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium clone-disk1.vdi

Increase the disk size on the host

Bring the box up again

mac$ cd ~/virtual-boxes/centos/

mac$ vagrant up

Ssh back into it and become root

mac$ vagrant ssh

virtaul$ sudo su -

Find the name of the logical volume for the file system

virtaul$ df

I got

Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      19003260   1185320  16845960   7% /
tmpfs                   251080         0    251080   0% /dev/shm
/dev/sda1               487652     28352    433700   7% /boot
vagrant              487358464 263884424 223474040  55% /vagrant

The name I am looking for is

/dev/mapper/VolGroup-lv_root

Find the name of the physical volume using disk.

virtaul$ fdisk -l

This gave me

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000906bd

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        2611    20458496   8e  Linux LVM

Disk /dev/mapper/VolGroup-lv_root: 19.9 GB, 19906166784 bytes
255 heads, 63 sectors/track, 2420 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/VolGroup-lv_swap: 1040 MB, 1040187392 bytes
255 heads, 63 sectors/track, 126 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

I am interested in /dev/sda

Create a new primary partition

Let's use /dev/sda to create a new primary partition.

virtaul$ fdisk /dev/sda

Follow these steps:

A new partition was created.

Reboot the machine, ssh back into it and become root.

virtaul$ reboot

mac$ vagrant ssh

virtaul$ sudo su -

Create a new physical volume

virtaul$ pvcreate /dev/sda3

Find the name of the logical volume group

virtaul$ vgdisplay

I got

  --- Volume group ---
  VG Name               VolGroup
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               19.51 GiB
  PE Size               4.00 MiB
  Total PE              4994
  Alloc PE / Size       4994 / 19.51 GiB
  Free  PE / Size       0 / 0
  VG UUID               kolyjB-epDi-uOQt-pp9k-3WOe-twe8-JJb9pz

Extend the volume group to use the new physical volume

virtaul$ vgextend VolGroup /dev/sda3

Find the name of the logical volume

virtaul$ df

I got

Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      19003260 1185428  16845852   7% /
tmpfs                   251080       0    251080   0% /dev/shm
/dev/sda1               487652   28352    433700   7% /boot

Extend the logical volume to use all available space

virtaul$ lvextend -l +100%FREE /dev/mapper/VolGroup-lv_root

Resize the file system

virtaul$ resize2fs /dev/mapper/VolGroup-lv_root

Verify that there are more space available

virtaul$ df -h

I got

Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       38G  1.2G   35G   4% /
tmpfs                 246M     0  246M   0% /dev/shm
/dev/sda1             477M   28M  424M   7% /boot

The disk is increased! We are almost done.

Now we want to do some cleaning and then repackage this as a new base box.

Clean and repackage

Remove the command history and last login. As root, do this:

virtaul$ cat /dev/null > ~/.bash_history && cat /dev/null > /var/log/lastlog && history -c && exit

and then, as vagrant, do

virtaul$ cat /dev/null > ~/.bash_history && history -c && exit

Now there won't be any history available when you login again.

Time to re-package the new box.

mac$ vagrant package --output centos-6.6-64-puppet-40Gb.box

Move the new box somewhere where we can refer to it when a new box is created from it. Create a home for base boxes.

mac$ mkdir ~/virtual-boxes/base-boxes/

Then, move the newly created box there:

mac$ mv centos-6.6-64-puppet-40Gb.box ~/virtual-boxes/base-boxes/

Add this new box to Vagrant for later usage.

mac$ vagrant box add centos-6.6-64-puppet-40Gb ~/virtual-boxes/base-boxes/centos-6.6-64-puppet-40Gb.box

Destroy the current CentOS box

mac$ vagrant destroy -f

and finally delete the Vagrant file

mac$ rm Vagrantfile

Initiate the new box:

mac$ vagrant init centos-6.6-64-puppet-40Gb

Start it

mac$ vagrant up

Use the new box with the increased disk.

mac$ vagrant ssh

Conclusion

This was a step by step guide that initiated a small CentOS box, increased the disk and created a new base box.

I hope you have use for it.

Acknowledgements

I would like to thank Johan Helmfrid for proof reading.

Resources



(less...)

Pages

About
Events
Why

Categories

Agile
Automation
BDD
Clean code
Continuous delivery
Continuous deployment
Continuous integration
Cucumber
Culture
Design
Executable specification
Gradle
Guice
J2EE
JUnit
Java
Linux
Load testing
Maven
Mockito
Pair programming
PicoContainer
Programming
Public speaking
Quality
Recruiting
Requirements
Scala
Selenium
Software craftsmanship
Software development
Spring
TDD
Teaching
Technical debt
Test automation
Tools
Web
Windows
eXtreme Programming

Authors

Thomas Sundberg
Adrian Bolboaca

Archives

Meta

rss RSS