1 Objectives

snf-image is an image creation plugin for Ganeti which comes from the Synnefo cloud platform.

NOTE: all the commands in this exercise are run as the root user. If you are logged in as a regular user, use sudo -s to get a root shell.

$ sudo -s
# 

2 Install snf-image - ALL NODES

You need to add the grnet repository.

First, create a file /etc/apt/sources.list.d/grnetdev.list containing exactly the following two lines:

deb http://apt.dev.grnet.gr wheezy/
deb-src http://apt.dev.grnet.gr wheezy/

Now add the repository signing key:

# wget -O - https://dev.grnet.gr/files/apt-grnetdev.pub | apt-key add -

Finally, install the package:

# apt-get update
# apt-get install snf-image

During the installation, you will be prompted for a URL prefix to download the snf-image-helper from. Paste in the following:

http://download.virt.nsrc.org/synnefo

This is so that you fetch a local copy. Otherwise it will take a very long time to download across the Internet from http://cdn.synnefo.org/FILENAME - it's over 100MB.

When asked "Update the helper image now?", say Yes.

If you make a typo in the URL prefix, you will get [ERROR] Downloading snf_image_helper_image. To fix this:

  dpkg-reconfigure --force snf-image
  dpkg --configure --pending

3 Fetch an image - ALL NODES

A number of sample images are available.

For testing we'll use one of the smaller ones, Debian Wheezy Base. This is still over 500MB so you need to fetch it from a local copy:

# cd /var/lib/snf-image
# wget http://download.virt.nsrc.org/synnefo/debian_base-7.0-x86_64.diskdump

Remember to do all of these steps on each node in your cluster before going on to the "Installing instances (creating VMs)" section below.

4 Installing instances (creating VMs)

(All groups should do this, but logged into the master node over SSH)

Creating a fully-working image is now just a one-step operation. Login to the MASTER node of your cluster, and run the following command:

# gnt-instance add -o snf-image+default \
  -t plain -s 1G -n nodeX.virt.nsrc.org \
  -O img_passwd=abc123,img_format=diskdump,img_id=debian_base-7.0-x86_64,img_properties='{"OSFAMILY":"linux"\,"ROOT_PARTITION":"1"\,"USERS":"root"}' \
  --no-name-check --no-ip-check wheezyX

(replacing X in nodeX and wheezyX with your node number)

You may need to copy and paste this command somewhere, replace "X" with your node number and then copy the command in to a terminal window connected to your MASTER node of your cluster.

The instance will be immediately started and ready to use.

5 Configuring the network service

The VM should be connected to the default network (br-lan) and have picked up an IP address via DHCP, but you still need to find out what address it has.

Log in using VNC to the console of your VM - if you're in doubt which node or port to connect to, use

# gnt-instance list -o +network_port

If you see something like this:

Instance Hypervisor OS               Primary_node        Status  Memory Network_port
wheezyX kvm        snf-image+default nodeX.virt.nsrc.org running   128M 11XXX

connect using your VNC client to:

nodeX.virt.nsrc.org:11XXX

Look closely at the "gnt-instance add" command above to see what user and password to use to log in on your new VM.

Once logged in to the guest:

# ifconfig eth0

Test that connectivity works, by pinging the class webserver:

# ping -c5 download.virt.nsrc.org

Does it work ?

Now you know its IP address, you should also be able to SSH to your instance from your laptop. Login as root with the password selected when you created the instance.

6 Additional info

6.1 Creating images

To find out how to create your own images for deployment with snf-image, see the documentation for snf-image-creator

6.2 Creating variants

To avoid having to give a long set of parameters using the -O flag every time you create an instance, snf-image from 0.15.1 onwards supports the use of OS variants.

On the master node, create /etc/ganeti/snf-image/variants/wheezy.conf containing the following:

IMG_PASSWD=abc123
IMG_FORMAT=diskdump
IMG_ID=debian_base-7.0-x86_64
IMG_PROPERTIES='{"OSFAMILY":"linux","ROOT_PARTITION":"1","USERS":"root"}'

and edit /etc/ganeti/snf-image/variants.list so it looks like this:

default
wheezy

Push this file out to all the other nodes:

# cd /etc/ganeti/snf-image
# gnt-cluster copyfile variants/wheezy.conf
# gnt-cluster copyfile variants.list

and check using gnt-os list that your new variant (snf-image+wheezy) is visible. If it isn't, use gnt-os diagnose for more details; maybe it is missing on some of the cluster nodes.

Now you can create an image using simply -o snf-image+wheezy without having to provide the -O parameters. So, for instance, your final sample "gnt-instance add" command would not look something like:

gnt-instance add -o snf-image+wheezy \
  -t plain -s 1G -n nodeX.virt.nsrc.org \
  --no-name-check --no-ip-check wheezyX

Which is certainly much simpler. Remember, you need to give a unique hostname (nodeX.virt.nsrc.org) and instance name (wheezyX).

6.3 Including ssh public keys

When you create an instance using snf-image, you can tell it to include your ssh public keys instead of, or as well as, setting a root password.

To do this you need to base64-encoded your authorized_keys file. You can do this by going to any node where this file is installed, and typing the following command:

$ base64 ~/.ssh/authorized_keys

(If using OSX: add flag -b 76 to get your data to wrap onto multiple lines). This will show your keys as lines of base64 data. Select and copy this into your clipboard.

Now you add an IMG_PERSONALITY section to your variants file, e.g. /etc/ganeti/snf-image/variants/wheezy.conf, so it looks like this:

IMG_FORMAT=diskdump
IMG_ID=ubuntu_server-14.04-x86_64
IMG_PROPERTIES='{"OSFAMILY":"linux","ROOT_PARTITION":"1","USERS":"root"}'
IMG_PERSONALITY='[{
"path":"/root/.ssh/authorized_keys",
"owner":"root",
"group":"root",
"mode":420,
"contents":"
... paste your base64 data here ...
"}]'

You may wish to include an IMG_PASSWD as well, so that you can still get into your machine if there's any problem with your ssh key.

Note that the mode "420" is correct: 644 in octal is 420 in decimal.

6*8*8 + 4*8 + 4 = 420

You can install multiple files this way. For example, if you also want to enable the serial console in the guest, for Ubuntu you create a file /etc/init/ttyS0.conf - The contents of the file are encoded in base64 in the example below:

IMG_PERSONALITY='[{
"path":"/root/.ssh/authorized_keys",
"owner":"root",
"group":"root",
"mode":420,
"contents":"
... paste your base64 data here ...
"},{
"path":"/etc/init/ttyS0.conf",
"owner":"root",
"group":"root",
"mode":420,
"contents":"
c3RhcnQgb24gc3RvcHBlZCByYyBSVU5MRVZFTD1bMjM0NV0Kc3RvcCBvbiBydW5sZXZlbCBb
ITIzNDVdCnJlc3Bhd24KZXhlYyAvc2Jpbi9nZXR0eSAtTCAxMTUyMDAgdHR5UzAgeHRlcm0K
"}]'

6.4 Instance export and import

You can try exporting the VM:

# gnt-backup export -n nodeY.virt.nsrc.org wheezyX

If the instance is running then it is temporarily shut down.

The node (-n) where you want the export to be written does not have to be the same as the node where the instance is.

The backup image is written under /var/lib/ganeti/export/wheezyX/ on nodeY.

When you import it's similar to creating a new instance, and you can use most of the flags you use when creating a new instance. This means you can use this as an alternative way of cloning machines. However by default ganeti will try to re-create the new machine with the same MAC address as the old one, which is rejected:

Failure: prerequisites not met for this operation:
error type: resource_not_unique, error details:
MAC address aa:00:00:41:1f:57 already in use in cluster

Therefore you have to tell it to assign a new MAC address when importing if the exported instance is still present.

# gnt-backup import \
  --src-node nodeY.virt.nsrc.org --src-dir /var/lib/ganeti/export/wheezyX \
  -t plain -s 2G -n nodeZ.virt.nsrc.org --no-name-check --no-ip-check \
  --net 0:mac=generate,link=br-lan wheezyX-copy

Explanation:

If that completes successfully, you can start it:

# gnt-instance start wheezyX-copy

Note however that even though you have created a larger disk (2G), the partition table and filesystem are still from the 1G disk, so "df" will show only 1G available.

It would be up to you to fix the partition table and grow the filesystem to fit the larger space available.