Pin CPU core(s) to specific Xen domU (guest)

It is often needed for a virtual machine to use specific cores of those available. On a multi-core system, one or more cores can be assigned for each virtual machine to improve performance. By default a virtual machine will use any available core. Assuming that there is a machine with 4 cores and a virtual machine needs to use two of them, the following entries in the guest’s configuration file will present the virtual machine with two virtual CPUs and will map them between the 3rd and the 4th physical cores.

cpus = "2,3"
vcpus = 2

A longer range, let’s say on a 8core machine, can be specified as “2-8” for example.
Which CPU is used for each VCPU can be seen with the xm vcpu-list command:

# xm vcpu-list node1
Name                              ID VCPUs   CPU State   Time(s) CPU Affinity
node1                              4     0     2   -b-       1.9 2-3
node1                              4     1     3   -b-       2.8 2-3

If there’s no cpus definition in the config file, each VCPU will use any of the available physical ones. The following host is configured to have 4 VCPUS:

# xm vcpu-list node3
Name                              ID VCPUs   CPU State   Time(s) CPU Affinity
node3                              2     0     3   -b-       2.1 any cpu
node3                              2     1     0   -b-       1.2 any cpu
node3                              2     2     2   -b-       0.9 any cpu
node3                              2     3     1   -b-       2.9 any cpu

However, I noticed that Xen will assign different CPU to each VCPU at different times, and many times the same CPU will be mapped to more than one VCPUs:

# xm vcpu-list node2
Name                              ID VCPUs   CPU State   Time(s) CPU Affinity
node2                              8     0     2   -b-       4.1 0-2
node2                              8     1     1   -b-       1.6 0-2
node2                              8     2     1   -b-       1.5 0-2

Remote MySQL database backup

Scenario: A database named db1 is hosted on host1. It needs to be backed up on a remote machine named host2 and be restored on the local MySQL instance. An overnight cronjob running the following script (or more accurate the command in sequence) gets the job done. The script runs mysqldump for backing up the database on host1. The generated .sql file is copied over ssh on the remote host host2. On host2, the /root/script.sql file is invoked to drop the existing db1 database and re-creates an empty db1 database to be ready to store the data:

#!/bin/bash
mysqldump -u root -ppassword db1 > /root/db1.mysql
scp /root/db1.mysql host1:/root/db1.mysql
ssh host2 "mysql -u root -ppassword < /root/script.sql"
ssh host2 "mysql -u root -ppassword db1 < /root/db1.mysql"

It may not be the most appropriate way, but it works. For any other ways of doing it please comment (apart from rsync’ing the /var/lib/mysql/db1 directories between the two hosts ;)).