How to install a list of packages with Ansible

I’ve been developing Ansible playbooks to help manage our Ubuntu servers. I wanted to install a list of package on a server using Ansible:

The old syntax:

 - name: Install list of packages
   action: apt pkg={{item}} state=installed
   with_items:
        - package1
        - package2
        - package3
        - etc

The new syntax:

 - name: Install list of packages
   apt: name={{item}} state=installed
   with_items:
        - package1
        - package2
        - package3
        - etc

How to recover a qcow2 file using fsck

I just upgrade Ubuntu on one of my virtual machines and for some reason the file system was corrupted after I rebooted the server. I got errors like:

ext4-fs error ext4_lookup deleted inode referenced

Great. Here is how I fixed the problem.

I found the solution was to attach the qcow2 file as a device on the host and then use fsck. Obviously, you will need to shutdown the virtual machine before doing this.

On the host, enable the nbd (network block device) kernel module. This should be available on all Ubuntu servers after Intrepid.

sudo modprobe nbd max_part=8

You can this use qemu-nbd to connect your qcow2 file as a network block device.

sudo qemu-nbd --connect=/dev/nbd0 /mnt/kvm/wordpress-sites.qcow2

You can then find the partitions using

sudo fdisk /dev/nbd0

On the virtual machine, partition 1 was corrupted, so here is the command I ran:

sudo fsck /dev/nbd0p1

Then disconnect the disk:

sudo qemu-nbd --disconnect /dev/nbd0

Everything seemed to be fine and so I started the domain up again.

KVM: Host CPU does not provide required features

I just exported / copied a virtual machine from one of our servers (a HP Proliant) to my laptop so that I could to mess around with it offline. I used the following commands to export the domain.

On the server:

virsh dumpxml wordpress > /tmp/wordpress.xml

On the laptop:

virsh define wordpress.xml
virsh wordpress start

However, I got this error on the laptop:

error: unsupported configuration: guest and host CPU are not compatible: Host CPU does not provide required features: rdtscp, popcnt, sse4.2, dca

The HP server has xeon processors and my laptop has a little core two duo. Obviously the CPU’s had different capabilities. You can see the cpu definitions in the wordpress.xml file were:

less wordpress.xml
<cpu match='exact'>
    <model>Nehalem</model>
    <vendor>Intel</vendor>
    <feature policy='require' name='tm2'/>
    <feature policy='require' name='est'/>
    <feature policy='require' name='monitor'/>
    <feature policy='require' name='ds'/>
    <feature policy='require' name='ss'/>
    <feature policy='require' name='vme'/>
    <feature policy='require' name='rdtscp'/>
    <feature policy='require' name='ht'/>
    <feature policy='require' name='dca'/>
    <feature policy='require' name='pbe'/>
    <feature policy='require' name='tm'/>
    <feature policy='require' name='vmx'/>
    <feature policy='require' name='ds_cpl'/>
    <feature policy='require' name='xtpr'/>
    <feature policy='require' name='acpi'/>
</cpu>

You can also view the same information using virsh capabilities command. It is worth piping it through less because the command produces alot of output and the host cpu information is at the top.

For the laptop:

virsh capabilities | less
<cpu>
    <arch>x86_64</arch>
    <model>Penryn</model>
    <vendor>Intel</vendor>
    <topology sockets='1' cores='2' threads='1'/>
    <feature name='pdcm'/>
    <feature name='xtpr'/>
    <feature name='tm2'/>
    <feature name='est'/>
    <feature name='smx'/>
    <feature name='vmx'/>
    <feature name='ds_cpl'/>
    <feature name='monitor'/>
    <feature name='dtes64'/>
    <feature name='pbe'/>
    <feature name='tm'/>
    <feature name='ht'/>
    <feature name='ss'/>
    <feature name='acpi'/>
    <feature name='ds'/>
    <feature name='vme'/>
</cpu>

So, to fix the issue, I just edited the domains xml definition and put in the correct features.

virsh edit wordpress

Note – if you are using ubuntu on your desktop and have virt-manager installed (which is an awesome gui application which runs on Linux and Macs). You can run virt-manager, open the virtual machine, go to the information tab, select the processor and then “copy host cpu configuration”. I’ve attached a screenshot of how to do this.

virt_manager_copy_configuration_from_host

I hope this helps someone.

Ubuntu 13.04 – kvm binary is deprecated?

I was trying to setup a KVM host with some KVM virtual machines on Ubuntu 13.04 Server.  It was a fresh install on a HP DL360.  However, there seemed to be a fundamental problem with KVM / virsh / virt-install.

When trying to install a virtual machine using virt-install with this command.

virt-install --accelerate -v -w bridge:br0 -m DE:AD:BE:EF:04:7B -n test1 --vcpus=2 -r 4096 --os-variant=ubuntuprecise --vnc --import --disk path=/home/me/vm.qcow2

I get this error:

ERROR internal error process exited while connecting to monitor: W: kvm binary is deprecated, please use qemu-system-x86_64 instead

It seems like a serious problem but it isn’t. I just should have put sudo in the front.

<strong>sudo</strong> virt-install --accelerate -v -w bridge:br0 -m DE:AD:BE:EF:04:7B -n test1 --vcpus=2 -r 4096 --os-variant=ubuntuprecise --vnc --import --disk path=/home/me/vm.qcow2

I hope this helps you because I wasted quite some time reading Ubuntu bug reports. Why can’t the system designers detect that the user didn’t have permissions and throw sensible errors?