OpenStack is an industry-standard open-source cloud management platform. Using OpenStack, one can build public, private or hybrid clouds easily. Due to the purely open nature of the platform, major IT vendors including Red Hat, Rackspace, IBM and HP are betting on its future, actively contributing to OpenStack development.

In OpenStack, there are two different interfaces for managing cloud resources. One is via Horizon, web-based OpenStack dashboard, and the other is via OpenStack command line interface (CLI).

In this tutorial, I am going to demonstrate how to create or terminate VMs on OpenStack from the command line. This was tested in the Havana release of OpenStack. For earlier versions of OpenStack such as OpenStack Folsom, you can simply replace "neutron" with "quantum" in the command line used throughout the tutorial.

I assume that there is an OpenStack deployment already up and running somewhere. I am going to use OpenStack CLI clients to manage VMs on the existing OpenStack setup.

Install OpenStack CLI Clients

The first step is to install necessary OpenStack command line clients.

In the above, OS_USERNAME/OS_PASSWORD are OpenStack user name and its password. OS_TENANT_NAME is the name of the project created. OS_AUTH_URL is the URL of the Keystone endpoint. Keystone is an OpenStack service responsible for authentication among different OpenStack components. You should replace 192.168.10.10 with the IP address of the host where OpenStack Keystone is running in your deployment.

Now, set the environment variables by running openrc.sh as follows.

$ source openrc.sh

At this point, you are ready to run OpenStack command line clients. Verify that you do not encounter any error when running:

$ nova list

It shows an empty result, it means everything is okay.

Create a Network

Now you are ready to create a network to connect your VMs to.

Create a new network named "xmodulo".

$ neutron net-create xmodulo

Create a new subnet named "xmodulo_subnet", and add it to the network just created.

$ neutron subnet-create xmodulo 10.0.0.0/24 --name xmodulo_subnet

Check a list of available networks to verify that the network has been created successfully.

$ neutron net-list

In the output, make a note of the "id" of the network you created. This id will be used when you create a VM later.

Launch a VM

Before creating and launch a VM, you need to know a few pieces of information first.

Find out the type of VM that you want to create. For that, run the following command.

$ nova flavor-list

In this example, I am going to choose the smallest VM type "m1.nano", which has 64 MB, 1 vCPU and no disk. Make a note of this type name.

Next, choose the VM image to use for your VM. To get a list of all available VM images, use this command:

$ nova image-list

Make a note of the ID of the image that you want to use for your VM.

Next, choose the type of security group to use for your VM. A security group determines inbound access rules for your VM. To find out available security groups:

$ nova secgroup-list

To check the access rules of the "default" security group, use this command:

$ nova secgroup-list-rules default

In this example, I am going to choose a security group named "default" for the VM. This security group happens to have no rule in it.

Finally, let's create a VM using the information obtained thus far. Specify the VM type (--flavor), ID of VM image (--image), ID of a network (net-id=) that you have found out. Replace [vm-name] with the name of your VM, which needs to be unique.

To verify that the VM has been successfully created and launched, run this command:

$ nova list

Stop, Suspend or Purge a VM

When you stop a running VM, it gets shutdown completely. On the other hand, when you suspend a VM, it is temporarily frozen, and can be restarted from the suspende state any time. In both cases, the VM image still remains in OpenStack.

To stop a VM:

$ nova stop [vm-name]

To suspend a VM:

$ nova suspend [vm-name]

If you want to purge the image of either stopped or suspended VM from OpenStack, use the command:

$ nova delete [vm-name]

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.