Create a Good Image for OpenStack

This post will describe how to construct a good OpenStack image. After VM started, cloud-init will overwrite VM’s settings (Network, Service, User and so on), so we can configure our image file for better user experience.

Environment Preparation

We use Ubuntu 16.04 for demo, you can decide use which Linux distribution by yourself. Install following package in your Image build machine.

  • qemu-utils
  • virtinst
  • virt-manager
  • libguestfs-tools

Create Image disk

Here is my custom create script, take a look or use following command:

1
2
3
4
5
6
virt-install --virt-type kvm --name mynewvm --ram 1024 \
--cdrom=base/xenial.iso \
--disk=mynewdisk.qcow2,format=qcow2 \
--network network=default \
--graphics vnc,listen=0.0.0.0 --noautoconsole \
--os-type=linux --os-variant=ubuntu16.04

Warning: If your image build machine is Ubuntu 14.04, then os-variant=ubuntu16.04 is not supported, use ubuntu14.04 instead.

And you can find Ubuntu base image on the Internet.

Install and operate VM by virt-viewer

Go to Application and search virt-viewer to start virt manager console.

If you were stuck in /dev/vda1: clean, 60739/589824 files, 367171/2359040 blocks, you can select the different video simulated options, it might work.

Install Prerequisite Package

  • cloud-init(Install when your vm is done)
  • vim
  • dhcpcd5
  • openssh-server

VM Configuration

dhcpcd configuration

Delete network interface dhcp config from:

1
2
3
# The primary network interface
auto ens3
iface ens3 inet dhcp

to

1
2
# The primary network interface
auto ens3

and start service: sudo service dhcpcd start

sudo without password

Use sudo visudo to edit sudoer file as following:

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

ubuntu  ALL=(ALL) NOPASSWD:ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

ssh with no delay

Add UseDNS no in /etc/ssh/sshd_config.

Clean & Compress VM image

1
2
sudo virt-sysprep -d mynewvm
sudo qemu-img convert -O qcow2 -c mynewimage.qcow2 mynewimage_compress.qcow2