Use custom VMDK images on Gandi Server

You can use custom system images for your Servers instead of an official Gandi image. Your Virtual Machine image can be a vanilla system or it can be fully customised to contain the latest build of your current web application, for example. The idea is that you'll then be able to spawn servers that are ready to run in a minute on Gandi Cloud, using a copy of your image.

This document will walk you through the creation a virtual machine image on your own computer using VirtualBox with VMDK and setting it up for usage on Gandi Cloud Servers.

Overview

VPS and Cloud Servers are really Virtual Machines, just like the ones you can run on your own computer. They are just operated at a large scale. While many different virtualization technologies exist, there are enough standard formats, architectures and tools to ensure portability.

This is exactly what you'll be taking advantage of in this tutorial. You'll learn how to take a Virtual Machine that you created on your computer and put it on Gandi Cloud. Once it's there, you'll be able to use it as a source image to create new servers, instead of using one of the official Gandi images and configuring servers after they're created on the cloud.

We'll start by creating an image from scratch using VirtualBox, as it is a popular cross-platform tool. However, you can choose to create your image with different tools and formats – we'll use VMDK in this example. You can simply skip or adapt the sections of the article that concern VirtualBox or VMDK specifically to best fit your own use case.

By the end of this tutorial, you'll be able to spawn Gandi Cloud Servers using system disks that are clones of your own image in a minute and with a single command.

Requirements

To complete this tutorial, you'll need:

  • Credits on your Gandi Hosting account
  • VirtualBox installed on your computer (or a pre-made .vmdk image)
  • An SSH client installed on your computer
  • Gandi CLI installed on your computer (optional, but recommended)

You can complete this tutorial without using Gandi CLI. All server-management related tasks can be done from the website as well.

Step 1: Download a system image

The Cloud platform can run any system that includes virtio libraries and support ACPI events.

In this tutorial, we'll use an Ubuntu 16.04 LTS Server. You can download the ISO image that we'll use for installation from the official website:

http://www.ubuntu.com/download/server

Step 2: Create a VM with the system image

In VirtualBox, create a new 64bit Linux Ubuntu VM. Choose the VMDK (Virtual Machine Disk) type for the hard disk. Give your disk the following properties:

  1. dynamic hard drive
  2. 3 GB of size can be enough if you don't install stuff beyond what's listed in this document

You'll need to upload the VMDK file to Gandi, so keep it as small as you can :)

Step 3: Launch the VM and install the system

Once the disk is created, start the VM and use the Ubuntu Server ISO image you downloaded in the previous step to install the system on your Virtual Machine's disk.

It's very important that you make specific choices for partitioning the disk and installating the GRUB boot loader:

  1. when prompted to partition the disk, select the Guided install without LVM and opt to use the whole disk without creating any partitions.
  2. When prompted about installing the GRUB boot loader, say “Yes” to install GRUB on the master boot record.

Otherwise, you can mostly setup the system to your liking, such as selecting the hostname, username, password, etc. (in the rest of the tutorial, we'll assume you chose ubuntu as your username).

Step 4: Configure the system

Once the installation is completed and you find yourself inside the system prompt, you have to add a specific media for Gandi tools. Importantly, this media will give you access to the gandi-hosting-vm2 package:

  (virtualbox)# cat << EOF  > /etc/apt/sources.list.d/gandi.list
  deb http://mirrors.gandi.net/gandi/ubuntu xenial main
  EOF

You can also add Gandi's Ubuntu mirrors to your aptitude sources. It'll make package downloads faster when your servers will be running on Gandi Cloud.

  (virtualbox)# cat << EOF  > /etc/apt/sources.list.d/ubuntu.list
  deb http://mirrors.gandi.net/ubuntu xenial main universe multiverse
  deb http://mirrors.gandi.net/ubuntu xenial-updates main universe multiverse
  deb http://mirrors.gandi.net/ubuntu xenial-security main universe multiverse
  deb http://mirrors.gandi.net/ubuntu xenial-backports main universe multiverse
  deb http://mirrors.gandi.net/ubuntu xenial-proposed main universe multiverse
  EOF

Once you have added the media sources, run apt-get update to refresh the package lists.

Now you should install Gandi's custom built package that will enable your server to fetch their Gandi Cloud configuration at boot:

  (virtualbox)# apt-get install gandi-hosting-vm2

Your network settings should be alright if left untouched. Ubuntu defaults to using DHCP on interface eth0, which will allow your servers to grab an IP address from the platform, be it an IPV4 or IPV6.

Now would also be the time to setup more things that are specific to your image. Is it going to run a web application? A mail server? A load balancer? You can setup anything you need, knowing that you'll be able to create new servers by cloning this system on Gandi Cloud.

Also, remember to install the OpenSSH server if you need it and haven't installed it yet:

  (virtualbox)# sudo apt-get install ssh

Finally, you'll need adjust grub's configuration to enable the server to boot on Gandi Cloud.

Assuming you have a root session (or using sudo with the ubuntu user), edit the default config file at ”/etc/default/grub” and set:

  GRUB_CMDLINE_LINUX_DEFAULT="console=hvc0 root=/dev/xvda1 nomce"
  GRUB_CMDLINE_LINUX=""

Then, be sure to run sudo update-grub2.

Note that once you apply the new GRUB configuration it's likely that you won't be able to reboot the system again inside VirtualBox. That's perfectly fine, because these settings are intended for Gandi Cloud.

Now you can shut down your VM in VirtualBox and proceed to the next section, where you'll setup your image on Gandi Cloud.

Step 5: Create the build server and an empty disk

To prepare the system image on Gandi Cloud, you'll create a so-called “build server”. A build server is just a regular server, but you'll only use it temporarily to transfer and install the image on a blank disk.

The build server doesn't need to be fast or powerful. You only need to make sure it has a large enough disk to hold the VMDK file and its raw version, which will be as large as you defined it in VirtualBox. If you followed the recommendations and used the dynamic disk size option, the VMDK file could be smaller than the raw image version.

Later, you'll be able to boot servers using your image with any amount of RAM and CPU cores you need, unrelated to the settings used for the build server.

In this example, we'll name the temporary server builder and we'll create it in the FR-SD3 (Paris) datacenter, where we'll want our image to be available. You can repeat the procedure for any datacenter where you want it to be available.

  $ gandi vm create --hostname builder --size 12288 --cores 1 --memory 512 --datacenter FR-SD3 --image "Ubuntu 16.04 LTS"

Once the server is created, go ahead and create the empty disk where you'll install the system. This is the disk that will serve as the “base image” that will be cloned when creating new servers.

For this example, we'll call the disk ubuntu_img and we'll attach it directly to builder. Here, you'll want to make sure you create a disk that is large enough to hold your system and the application that it's running. It should be at least as large as the disk size you used to create the original image in VirtualBox. We'll use 6 GB in this example.

  $ gandi disk create --name ubuntu_img --size 6144 --datacenter FR-SD3 --vm builder

Step 6: Upload the image file

Now you can upload the image to the build server. Start by getting its IP address:

  $ gandi vm info builder | grep ip

We'll be uploading the VMDK image file to the root user's home directory on the build server. Make sure you adapt the local image's location and replace replace <ip> with the IP address obtained with the previous command in the command below:

  $ scp /path/to/your/local/image/Ubuntu.vmdk root@<ip>:~/

If it's an IPV6 address, remember to escape it like this:

 root@\[2001:...\]:~/.

The transfer can take a while depending on the size of the VMDK file and the speed of your Internet connection.

Step 7: Install the image

Once complete, you can access the build server and install the system image. First, start an ssh connection to the server in order to run the necessary commands.

  $ gandi vm ssh builder

You'll use qemu to convert the Ubuntu.vmdk file you exported from VirtualBox to a raw image file. You'll then use pv to copy that image onto the “ubuntu_img” disk you have previously created and attached to the build server.

Note that the disk was automatically mounted onto your filesystem. However, we want to write the raw image file directly onto the block device, so you'll need to unmount the disk before you copy it.

  (vm-builder)# apt-get update
  (vm-builder)# apt-get install qemu-utils pv
  (vm-builder)# qemu-img convert Ubuntu.vmdk -O raw disk.img # this step might take a while
  (vm-builder)# mount | awk '/ubuntu_img/ { print $1 }' # will show the device 
  (vm-builder)# umount /srv/ubuntu_img
  (vm-builder)# pv < disk.img > /dev/xvdb

The device is usually located at /dev/xvdb in this scenario and mounted at /srv/ubuntu_img, but you can run mount to make sure. This last step can also be accomplished with the dd command. We used pv simply to get a progress bar, as it can take a few minutes.

You can exit the server once the process is completed. You won't be needing it again and will soon delete it.

  (vm-builder)# exit

Step 8: Convert the disk to a system disk

Congratulations, your system image is installed on the ubuntu_img disk.

Detach it from the build server and convert the disk to a system disk. Use the grub-x86_64 (xen) kernel to boot from your image's grub.

  $ gandi disk detach ubuntu_img
  $ gandi disk update ubuntu_img --kernel "grub-x86_64 (xen)" --cmdline "console=hvc0 root=/dev/xvda1 ro nosep"

Note: In case you're installing a non-Linux system, you can use the raw (xen) kernel instead of grub-x86_64 (xen). For example:

  $ gandi disk update my_image_disk --kernel "raw (xen)" --cmdline "console=hvc0 root=/dev/xvda1 ro nosep"

That's it, ubuntu_img can now be used to create new servers.

You can go ahead and delete the builder server, as you'll no longer need it.

  $ gandi vm delete builder

Step 9: Create a bunch of new servers using the new system image

You're now able to create new servers using the ubuntu_img disk as the source disk.

  $ gandi vm create --hostname ubuntu0 --image ubuntu_img --datacenter FR-SD3 [--login <user> --password]
  $ gandi vm create --hostname ubuntu1 --image ubuntu_img  --datacenter FR-SD3
  $ gandi vm create --hostname ubuntu2 --image ubuntu_img --datacenter FR-SD3

If you installed the ssh server, you'll be able to login:

  $ gandi vm ssh ubuntu0 --login ubuntu
  <password>

You can replace ubuntu with the username you chose when you installed the system.

And that's all there is to it :) Each server you create will use a clone of ubuntu_img as its system disk. You can define different disk sizes, specify the number of cores and the amount of RAM that you'll use for each server, just as you would if you were using a system image provided by Gandi.

What's next?

You can further customise the VM to make it fit better with your workflow and the Gandi platform.

  1. Attach all servers to a load balancer (Web Accelerator) with Gandi CLI
  2. Use Gandi time server to keep your VM from drifting (ntp.gandi.net)
Last modified: 06/26/2017 at 18:23 by Nicolas C. (Gandi)