Citrix - Xenserver - Migrate MAC address
This post provides two scripts based on the shell of linux to export basic network information of your XenServer's virtual machines (VMs), this will include the VM's name, the device number, the MAC address and the network name of all the virtual interfaces (VIFs) of every VM of your pool, it has to be known that this software only works fine when the name of you VMs and the name of your networks have no spaces in between neither leading spaces, so rename this paramenters before using the scripts.
Why should you like to save this information? In the following scenario:
There are 2 sites in your company, one of them are the principal one, there you have provisioned VM's with DHCP reserved to their MAC addresses, the alternate site is a recovery site, where you have to make work your VMs if the main site fails, as a requirement you have to have:
- Repplication LUNs from one site to the other, the VM's disks have to be in a shared Storage Repository (SR) , the one with the replication feature.
- There has to be a scheduled VM metadata backup pointing to one of the replicated LUNs.
- The network VLANs to whom XenServer's Physical Interfaces (PIFs) of the main site have to be the same to whom should be connected the recovery site PIFs.
After the failure has occured, the repplication between LUNs has to be stopped so you be able to write on the recovery LUNs disks, then execute the Restore Metadata Backup from the Xenserver console and the VMs will be repopulated in the recovery site.
But, after you do this, the VM's MAC addresses will be different and you should reconfigure the DHCP server before any provisioned VM could start properly.
So, there is where this software could be applied, the GetMac script gives you a file where you have information of the VM's name, and the information described in the first lines of this article. The SetMac script reconfigure the VIFs of the pre-populated VMs in the recovery site with the same MAC address that they have in the main site.
Of course, the SetMac script allows you to reconfigure the MAC addresses from a custom file with the same format as the output of the GetMac script.
Try it and enjoy, i'm open to any constructive comments.
###########################GetMac Start#####################
But, after you do this, the VM's MAC addresses will be different and you should reconfigure the DHCP server before any provisioned VM could start properly.
So, there is where this software could be applied, the GetMac script gives you a file where you have information of the VM's name, and the information described in the first lines of this article. The SetMac script reconfigure the VIFs of the pre-populated VMs in the recovery site with the same MAC address that they have in the main site.
Of course, the SetMac script allows you to reconfigure the MAC addresses from a custom file with the same format as the output of the GetMac script.
Try it and enjoy, i'm open to any constructive comments.
###########################GetMac Start#####################
#GetMac, this software creates a file of the network cards of your XenServer's VMs
#Copyright (C) 2013 Bryan Saldivar
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>
#Feel free to contact me at bsaldivar.emc2@gmail.com
#!/bin/bash
testd ()
{
dirname=${1}
if [ ! -d $dirname ]
then
mkdir ${dirname}
fi
}
testf ()
{
filename=${1}
if [ ! -f $filename ]
then
touch ${filename}
fi
}
validaerror ()
{
if [ $? != 0 ]
then
echo "Error en el proceso ${1}"
exit 1
fi
}
temp=""
echo "This tool will create a mac export file with the name of virtual machines, the MAC address per interface and the name of the network associated to it"
echo "Please set all the names of the networks and virtual machines with no blanks or spaces between words or at the beggining of it"
echo "The software works only with no spaces names, otherwise this will result in machine corruption"
separator=","
fecha=$(date +"%Y-%m-%d-%H-%M-%S")
export_file="mac-exportfile-${fecha}"
testf ${export_file}
eraseblank ()
{
outvar=""
tempfile=${1}
templines=$(wc -l ${tempfile} | cut -d ' ' -f1 )
for (( i=1 ; i< ${templines} +1 ; i++ ))
do
b=${outvar}
c=$(sed -n "${i}p" ${tempfile} | sed 's/^ *//g' | sed 's/ *$//g' | awk '{printf $NF}' )
outvar="${b} ${c}"
done
temp=${outvar}
}
tempfile ()
{
tempdir=bsaldivartempfiles
testd ${tempdir}
fe1=$(date +"%Y-%m-%d-%H-%M-%S")
tempfile1=${tempdir}/t1-${fe1}.tmp
testf ${tempfile1}
}
##Start vm-list
tempfile
xe vm-list params=name-label | grep -v '^$' | grep -v 'Control domain' > ${tempfile1}
eraseblank ${tempfile1}
vm_list=${temp}
##End vm-list
for vmx in ${vm_list}
do
a=${vmx}${separator}
##Start vif-list
tempfile
xe vif-list vm-name-label=${vmx} params=uuid | grep -v '^$' > ${tempfile1}
validaerror VIFList
eraseblank ${tempfile1}
vif_uuid_list=${temp}
##End vif-list
for vif_uuidx in ${vif_uuid_list}
do
##Start device-list
tempfile #prepare file for process blanks
xe vif-list uuid=${vif_uuidx} params=device | grep -v '^$' > ${tempfile1}
validaerror "GettingDevice"
eraseblank ${tempfile1}
devicex=${temp}
##End device-list
##Start MAC-list
tempfile #prepare file for process blanks
xe vif-list uuid=${vif_uuidx} params=MAC | grep -v '^$' > ${tempfile1}
validaerror "GettingMAC"
eraseblank ${tempfile1}
macx=${temp}
##End MAC-list
##Start network-list
tempfile #prepare file for process blanks
#xe vif-list uuid=${vif_uuidx} params=network-uuid | grep -v '^$' > ${tempfile1}
xe vif-list uuid=${vif_uuidx} params=network-name-label | grep -v '^$' > ${tempfile1}
validaerror "GettingNetwork"
eraseblank ${tempfile1}
network_uuidx=${temp}
##End network-list
b=${a}
a=${b}${devicex}" "${macx}" "${network_uuidx}" "${separator}
done
echo ${a} >> ${export_file}
validaerror "ExportingFile"
done
rm -rf ${tempdir}
########################GetMac End#######################################
#SetMac, this software creates a file of the network cards of your XenServer's VMs
#Copyright (C) 2013 Bryan Saldivar
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>
#Feel free to contact me at bsaldivar.emc2@gmail.com
#!/bin/bash
testd ()
{
dirname=${1}
if [ ! -d $dirname ]
then
mkdir ${dirname}
fi
}
testf ()
{
filename=${1}
if [ ! -f $filename ]
then
echo "No such file ${1}"
fi
}
validaerror ()
{
if [ $? != 0 ]
then
echo "Error en el proceso"
exit 1
fi
}
eraseblank ()
{
outvar=""
tempfile=${1}
templines=$(wc -l ${tempfile} | cut -d ' ' -f1 )
for (( k=1 ; k< ${templines} +1 ; k++ ))
do
b=${outvar}
c=$(sed -n "${k}p" ${tempfile} | sed 's/^ *//g' | sed 's/ *$//g' | awk '{printf $NF}' )
outvar="${b} ${c}"
done
temp=$(echo ${outvar} | sed 's/^ *//g' | sed 's/ *$//g' )
}
tempfile ()
{
tempdir=bsaldivartempfiles
testd ${tempdir}
fe1=$(date +"%Y-%m-%d-%H-%M-%S")
tempfile1=${tempdir}/t1-${fe1}.tmp
testf ${tempfile1}
}
#set -x
echo "Tool for Xenserver mac setup - Developed by Bryan Saldivar 2013"
echo "This tool will reconfigure the MAC address per interface from an input file you will specify related to Virtual machines associated to it"
echo "Please set all the names of the networks and virtual machines with no blanks or spaces between words or at the beggining of it"
echo "The software works only with no spaces between names, otherwise this will result in machine corruption"
read -p "Colocar el nombre del archivo de origen : " import_file
testf ${import_file}
separator=","
tline=$(wc -l ${import_file} | cut -d ' ' -f1)
for (( i=1; i<= ${tline} ; i++ ))
do
#List the number of fields, count the number of delimeters, the f1 is name, the others are the amount of devices
fieldn=$(sed -n "${i}p" ${import_file} | grep -o ${separator} | wc -l )
vmx=$(sed -n "${i}p" ${import_file} | cut -d ',' -f1)
##Start vmx_uuid
tempfile
xe vm-list name-label=${vmx} params=uuid | grep -v '^$' > ${tempfile1}
validaerror VM_List
eraseblank ${tempfile1}
vm_uuidx=${temp}
##End vmx_uuid
for (( j=2; j<=${fieldn} ; j++))
do
dev_mac_netx=$(sed -n "${i}p" ${import_file} | cut -d ',' -f${j} | sed 's/^ *//g' | sed 's/ *$//g')
devx=$(echo ${dev_mac_netx} | cut -d ' ' -f1 )
macx=$(echo ${dev_mac_netx} | cut -d ' ' -f2 )
netx=$(echo ${dev_mac_netx} | cut -d ' ' -f3 )
##Start network_uuidx
tempfile
xe network-list params=uuid name-label=${netx} | grep -v '^$' > ${tempfile1}
validaerror NetworkuuidList
eraseblank ${tempfile1}
net_uuidx=${temp}
##End network_uuidx
##Start vif_uuidx
tempfile
xe vif-list vm-name-label=${vmx} network-name-label=${netx} device=${devx} params=uuid | grep -v '^$' > ${tempfile1}
validaerror VIFuuidList
eraseblank ${tempfile1}
vif_uuidx=${temp}
##End vif_uuidx
xe vif-destroy uuid=${vif_uuidx}
xe vif-create vm-uuid=${vm_uuidx} network-uuid=${net_uuidx} mac=${macx} device=${devx}
done
done
rm -rf ${tempdir}
##############################SetMac End#####################################

Citrix XenServer MAC Migration by Bryan Percy Saldivar Espinoza (bsaldivar.emc2@gmail.com) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
¿Desea donar?/Do you wish to give some money to the author?
Comentarios
Publicar un comentario