Virtual Development Server: Add enough ram and disk space to VirtualBox for further server components

Posted by Torsten Kleiber on August 13, 2016 Tags: Virtual-Development-Server Continous-Integration Infrastructure-as-Code Linux Oracle Vagrant VirtualBox VDI VDMK

As I want later build docker images and run docker containers I have to provide enough ram and disk space for this. Per default the Vagrantboxes have vmdk disks, these have a static size and are to small for my purposes.

Therefore I convert the disk, which comes with the box, via Vagrant VirtualBox provider from vmdk to vdi, so that it allocate only the used disk space in the host system. Additional I add a second big disk:

...
# virtualbox provider
config.vm.provider "virtualbox" do |vb|
# name in VirtualBox
vb.name = "Development Server"

    # configure 16 GB memory
    vb.customize ["modifyvm", :id, "--memory", "16384"]

    # clone the original vmdk disk into a dynamic vdi disk, which only allocate the used space on the host
    if ARGV[0] == "up" && ! File.exist?("#{ENV["HOME"]}/VirtualBox VMs/#{vb.name}/#{vb.name}.vdi")
      # configure the SATA controller for second disk port, for other box you may have another controller
      vb.customize [
        "storagectl", :id,
        "--name", "SATA",
        "--controller", "IntelAHCI",
        "--portcount", "1",
        "--hostiocache", "on"
      ]
      # clone the original disk, for other box you may have another disk name
      vb.customize [
        "clonehd", "#{ENV["HOME"]}/VirtualBox VMs/#{vb.name}/box-disk2.vmdk",
             "#{ENV["HOME"]}/VirtualBox VMs/#{vb.name}/#{vb.name}.vdi",
        "--format", "VDI"
      ]
      # attach the cloned disk to the controller
      vb.customize [
        "storageattach", :id,
        "--storagectl", "SATA",
        "--port", "0",
        "--device", "0",
        "--type", "hdd",
        "--nonrotational", "on",
        "--medium", "#{ENV["HOME"]}/VirtualBox VMs/#{vb.name}/#{vb.name}.vdi"
      ]
      # delete the original disk to release it's space
      vb.customize [
        "closemedium", "disk", "#{ENV["HOME"]}/VirtualBox VMs/#{vb.name}/box-disk2.vmdk",
        "--delete"
      ]
    end

    # create addtional big dynamic vdi disk for docker images
    if !File.exist?("#{ENV["HOME"]}/VirtualBox VMs/#{vb.name}/#{vb.name}_docker.vdi")
      # create addtional big dynamic vdi (200 GB)
      vb.customize [
        "createhd",
        "--filename", "#{ENV["HOME"]}/VirtualBox VMs/#{vb.name}/#{vb.name}_docker.vdi",
        "--format", "VDI",
        "--size", 200 * 1024
      ]
      # attach the addtional disk to the controller
      vb.customize [
        "storageattach", :id,
        "--storagectl", "SATA",
        "--port", "1",
        "--device", 0,
        "--type", "hdd",
        "--medium", "#{ENV["HOME"]}/VirtualBox VMs/#{vb.name}/#{vb.name}_docker.vdi"
      ]
    end
  end

  # shell provider
  # format the additional disk and add the free space to the box
  config.vm.provision :shell, :path => "add_disk.sh"
  ...

Lines 64..66 adds the additional disk space to a logical volume via a Vagrant shell provider, which calls the script add_disk.sh in the created VirtualBox machine.

Resizing the first disk does not work, as therefore the machine has to be booted, which cannot be handled with Vagrant.

# exit immediately if a command exits with a non-zero status.
set -e
# activate debugging from here
set -x

if [ -f /etc/disk_added_date ] ; then
echo "disk already added so exiting."
exit 0
fi

# show diskspace of the logical volume before adding the disk
df -h /dev/mapper/linux-root

# partitioning the disk
sudo fdisk -u /dev/sdb <<EOF n p t 8e w EOF

# initialize the partition for use by logical volume manager
sudo pvcreate /dev/sdb1

# add the partition to volume group linux
sudo vgextend linux /dev/sdb1

# increase the size of the logical volume /dev/mapper/linux-root
sudo lvextend --extents +51199 --resizefs /dev/mapper/linux-root

# mark that the disk was added
date > /etc/disk_added_date

# show diskspace of the logical volume after adding the disk
df -h /dev/mapper/linux-root

Now you have to recreate the VirtualBox machine via

vagrant destroy
vagrant up

In the output you can now see, how the logical volume grows:

==> default: Running provisioner: shell...
default: Running: C:/Users/torst/AppData/Local/Temp/vagrant-shell20160812-2348-1ln6wrz.sh
==> default: ++ '[' -f /etc/disk_added_date ']'
==> default: ++ df -h /dev/mapper/linux-root
==> default: Filesystem              Size  Used Avail Use% Mounted on
==> default: /dev/mapper/linux-root   16G  1.9G   14G  12% /
==> default: ++ sudo fdisk -u /dev/sdb
==> default: Welcome to fdisk (util-linux 2.23.2).
==> default:
==> default: Changes will remain in memory only, until you decide to write them.
==> default: Be careful before using the write command.
==> default:
==> default:
==> default: Command (m for help): Partition type:
==> default:    p   primary (0 primary, 0 extended, 4 free)
==> default:    e   extended
==> default: Select (default p): Partition number (1-4, default 1): First sector (2048-419430399, default 2048): Using default value 2048
==> default: Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399): Using default value 419430399
==> default: Partition 1 of type Linux and of size 200 GiB is set
==> default:
==> default: Command (m for help): Selected partition 1
==> default: Hex code (type L to list all codes): Hex code (type L to list all codes): Changed type of partition 'Linux' to 'Linux LVM'
==> default:
==> default: Command (m for help):
==> default: Device does not contain a recognized partition table
==> default: Building a new DOS disklabel with disk identifier 0xb4c74c64.
==> default: The partition table has been altered!
==> default:
==> default: Calling ioctl() to re-read partition table.
==> default: Syncing disks.
==> default: ++ sudo pvcreate /dev/sdb1
==> default:   Physical volume "/dev/sdb1" successfully created
==> default: ++ sudo vgextend linux /dev/sdb1
==> default:   Volume group "linux" successfully extended
==> default: ++ sudo lvextend --extents +51199 --resizefs /dev/mapper/linux-root
==> default:   Size of logical volume linux/root changed from 15.62 GiB (4000 extents) to 215.62 GiB (55199 extents).
==> default:   Logical volume root successfully resized.
==> default: meta-data=/dev/mapper/linux-root isize=256    agcount=4, agsize=1024000 blks
==> default:          =                       sectsz=512   attr=2, projid32bit=1
==> default:          =                       crc=0        finobt=0
==> default: data     =                       bsize=4096   blocks=4096000, imaxpct=25
==> default:          =                       sunit=0      swidth=0 blks
==> default: naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
==> default: log      =internal               bsize=4096   blocks=2560, version=2
==> default:          =                       sectsz=512   sunit=0 blks, lazy-count=1
==> default: realtime =none                   extsz=4096   blocks=0, rtextents=0
==> default: data blocks changed from 4096000 to 56523776
==> default: ++ date
==> default: ++ df -h /dev/mapper/linux-root
==> default: Filesystem              Size  Used Avail Use% Mounted on
==> default: /dev/mapper/linux-root  216G  1.9G  214G   1% /

Here you find the source code for this blog.

Here you find more about the topic "Virtual Development Server".

That’s it!

References: