How to run Fedora 22 cloud images with KVM

I’m going to assume you already have a working KVM host, with virsh and a networking bridge setup. If not this is a great how-to to get KVM and virsh setup: http://www.charleslabri.com/installing-and-working-with-centos-7-x64-and-kvm/

First, download the KVM cloud image, go to getfedora.org for the latest image link.

# cd /opt/guests/disk
# wget http://download.fedoraproject.org/pub/fedora/linux/releases/test/22_Beta/Cloud/x86_64/Images/Fedora-Cloud-Base-22_Beta-20150415.x86_64.qcow2

I keep my images in /opt/guests/disk and isos in /opt/guests/iso/, you may keep them elsewhere.

Its a good idea to make a copy of this image for each VM because the VM will read and write to this image.

# cp Fedora-Cloud-Base-22_Beta-20150415.x86_64.qcow2 myf22.qcow2

The image is only 3GB by default, you probably want to expand the disk, so lets do that now (you can skip this step if you’re OK with 3GB):

# qemu-img create -f qcow2 -o preallocation=metadata f22tmp.qcow2 20G
# virt-resize –expand /dev/sda1 myf22.qcow2 f22tmp.qcow2
# mv f22tmp.qcow2 myf22.qcow2

Now you’ll need to create an iso for cloud-init. cloud-init will setup your ssh user and networking.

First create the user-data and meta-data files for the iso, update the configuration accordingly.

# cat meta-data
instance-id: myfedora22
local-hostname: myfedora22.domain.tld
network-interfaces: |
iface eth0 inet static
address 192.168.1.100
network 192.168.1.0
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.1

# cat user-data
#cloud-config
users:
– name: travis
shell: /bin/bash
sudo: [‘ALL=(ALL) NOPASSWD:ALL’]
ssh-authorized-keys:
–   (insert ssh public key here)

Now generate the iso:

# genisoimage -output /opt/guests/iso/myf22.iso -volid cidata -joliet -rock user-data meta-data
# chown qemu:qemu /opt/guests/iso/myf22.iso

And create the VM:

virt-install –name MyF22 \
–ram 2048 \
–disk /opt/guests/disk/myf22.qcow2 \
–vcpus 2 \
–os-variant fedora21 \
–connect qemu:///system \
–network bridge:br0,model=virtio \
–cdrom /opt/guests/iso/myf22.iso \
–boot hd

After the VM boots you can press ^] to exit the console.

Now you can log into the VM:

ssh travis@192.168.1.100

Now the only issue is that on reboot, the cloud-init iso won’t be mounted and your networking settings won’t be provided to the guest. You will need to launch virsh and edit the XML description for the vm and add the source manually.

# virsh

virsh # edit MyF22

Find the disk section that describes the cdrom , it will look like this:

<disk type=’block’ device=’cdrom’>
<driver name=’qemu’ type=’raw’/>
<target dev=’hda’ bus=’ide’/>
<readonly/>
<address type=’drive’ controller=’0′ bus=’0′ target=’0′ unit=’0’/>
</disk>

Change the disk type to “file” and add a source tag with your cloud-init iso as the source:

<disk type=’file‘ device=’cdrom’>
<driver name=’qemu’ type=’raw’/>
  <source file=’/opt/guests/iso/myf22.iso’/>
<target dev=’hda’ bus=’ide’/>
<readonly/>
<address type=’drive’ controller=’0′ bus=’0′ target=’0′ unit=’0’/>
</disk>

This information was gathered from the following sources:

Advertisements
1 comment
  1. Ian said:

    You can avoid the need to change the cdrom definition after creating the VM if you use –import instead of –cdrom as the ‘installation method’, and provide the CDROM as a second disk instead:

    virt-install –name MyF22 \
    –ram 2048 \
    –disk /opt/guests/disk/myf22.qcow2 \
    –disk /opt/guests/iso/myf22.iso,device=cdrom \
    –vcpus 2 \
    –os-variant fedora21 \
    –connect qemu:///system \
    –network bridge:br0,model=virtio \
    –import \
    –boot hd

    The –import option is designed for using an existing disk image, rather than running an OS installer, and that fits the use case here quite well. It is described in the man page:

    –import
    Skip the OS installation process, and build a guest around an existing disk image. The device used for booting is the first device specified via “–disk” or
    “–filesystem”.

    This also seems to cause virt-install to not complain that the installation has not yet completed when you exit it. Now, on reboot of the domain, it still has access to the CDROM disk image, and can get its network information correctly.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: