#!/usr/bin/perl
#Environment setup:
#Two machines with rdma setup.
#host1:dell-per730-36.lab.eng.pek2.redhat.com
#host2:dell-per730-37.lab.eng.pek2.redhat.com
#
#Refer to: https://mojo.redhat.com/docs/DOC-995273
#
#Command line:
#On host1:
#Ensure the guest setting match below:
# hard_limit > memory + other memory of qemu using
# swap_hard_limit >= hard_limit
#For example:
#  <memory unit='KiB'>1048576</memory>
#  <currentMemory unit='KiB'>1048576</currentMemory>
#  <memtune>
#    <hard_limit unit='KiB'>3145728</hard_limit>
#    <swap_hard_limit unit='KiB'>4194304</swap_hard_limit>
#  </memtune>
#virsh start guest
#Domain guest started
#
#Configure ssh log in without password to host2.
#perl rdma2.pl qemu:///system guest  qemu+ssh://192.10.10.1/system rdma://192.10.10.1
#
#On host2:
#
#ifconfig
#
#ib0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 65520
#        inet 192.168.100.2  netmask 255.255.240.0  broadcast 192.10.15.255
#
#        # virsh list --all
#         Id    Name                           State
#         ----------------------------------------------------
#          4     guest                         running
use strict;
use warnings;

use Sys::Virt;
use Sys::Virt::Domain;

if (int(@ARGV) < 4) {
    die "syntax: $0 URI DOMAIN DST-URI MIG-URI";
}

my $lis=0;

my $uri = shift @ARGV;

my $c = Sys::Virt->new(uri => $uri);

my $dom = $c->get_domain_by_name(shift @ARGV);

my $desturi = shift @ARGV;

my $miguri = shift @ARGV;

my %params=(Sys::Virt::Domain::MIGRATE_PARAM_LISTEN_ADDRESS=>$lis,
            Sys::Virt::Domain::MIGRATE_PARAM_URI=>$miguri);

$dom->migrate_to_uri(
                     $desturi,
                     \%params,
                     Sys::Virt::Domain::MIGRATE_LIVE | Sys::Virt::Domain::MIGRATE_PEER2PEER | Sys::Virt::Domain::MIGRATE_RDMA_PIN_ALL);

*******************************************
Run 
# ./rdma.pl qemu:///system setusertest  qemu+ssh://192.168.100.2/system rdma://192.168.100.2
#

# virsh list --all
#  Id    Name                           State
#  ----------------------------------------------------
#   9     setusertest                    running
#
