Skip to content

Instantly share code, notes, and snippets.

@hedinasr
Last active July 11, 2017 07:30
Show Gist options
  • Save hedinasr/26c90b3892cc9ed7341f153428c13cd9 to your computer and use it in GitHub Desktop.
Save hedinasr/26c90b3892cc9ed7341f153428c13cd9 to your computer and use it in GitHub Desktop.
Script for moving Xen virtual machine using only SSH, rsync and lvm
#!/bin/bash
#
# Auteur : Hedi Nasr, Nicolas Carel
# Version : 1.2
# Description : script de deplacement de vm
# Prérequis:
# - Hyperviseur: Xen
# - LVM
# - Nom des volumes: <nom-vm>-<disk|swap|data>
#
# Le script est à lancer depuis la machine de l'administrateur
##############################################################
# Workflow
# --------
#
# 1) L'administrateur se connecte en SSH sur l'hyperviseur
# source en transférant l'agent SSH.
# 2) Snapshot de la machine virtuelle + tar du snapshot
# 3) Copie de la sauvegarde vers l'hyperviseur destination
# 4) Création de la nouvelle machine virtuelle (xen-create-image)
# 5) Extinction de l'ancienne machine + montage des disques en
# locale.
# 6) rsync des données manquantes
# 7) Nettoyage
##############################################################
set -e # quitte le script a la moindre erreur
print_help() {
cat <<EOF
Usage: move-vm [-h] [-n] [-s] [-f] [-v] <hyp_src> <vm> <hyp_dst> [vg_data]
-h affiche l'aide
-n dry-run, affiche seulement les commandes qui vont etre executes
-s synchro supplementaire
-f finalisation (la vm source sera arretee)
-v verbose
hyp_src hyperviseur source
vm nom de la machine virtuelle a deplacer
hyp_dst hyperviseur sur lequel sera installe la machine virtuelle
vg_data nom du vg data (optionnel)
EOF
}
PRINT=
FINAL=0
VERBOSE=
while getopts ":hnsfv" opt; do
case $opt in
h)
print_help
exit 0
;;
n)
PRINT=echo
;;
s)
FINAL=1
;;
f)
FINAL=2
;;
v)
VERBOSE="-v"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
print_help
exit 1
;;
esac
done
shift "$((OPTIND - 1))"
if [ "$#" -lt 3 ]; then
print_help
exit 1
fi
HYP_SRC="$1"
HYP_DST="$3"
VM="$2"
VG_DATA="$4"
# verifier si on est pas deja sur l'hyperviseur source
MY_HOST=`hostname`
if [ "${HYP_SRC%%.*}" == "$MY_HOST" ] ; then
echo "Running move-vm on source hypervisor"
SRC_CMD=""
else
SRC_CMD="ssh -AC root@${HYP_SRC} "
fi
# verifier si on est pas deja sur l'hyperviseur destination
if [ "${HYP_DST%%.*}" == "$MY_HOST" ] ; then
echo "Running move-vm on destination hypervisor"
DEST_CMD=""
else
DEST_CMD="ssh root@${HYP_DST} "
fi
# il faut dans un premier temps recuperer les informations
# necessaires concernant les vm et les hyperviseurs
DISK_SIZE=`$SRC_CMD lvs | grep "$VM"-disk | awk '{ print $4 }' | sed 's/,/./'`
DATA_SIZE=`$SRC_CMD lvs | grep "$VM"-data | awk '{ print $4 }' | sed 's/,/./'`
SWAP_SIZE=`$SRC_CMD lvs | grep "$VM"-swap | awk '{ print $4 }' | sed 's/,/./'`
VG_SRC=`$SRC_CMD vgs | tail -1 | awk '{ print $1 }'`
VG_DST=`$DEST_CMD vgs | tail -1 | awk '{ print $1 }'`
# creation des volumes
# volume *-disk
if [ ! -z "$DISK_SIZE" ]; then
echo "size of disk: $DISK_SIZE"
if [ $FINAL == 0 ] ; then
$PRINT $DEST_CMD lvcreate -n "$VM"-disk -L "$DISK_SIZE" "$VG_DST"
$PRINT $DEST_CMD mkfs.ext4 /dev/"$VG_DST"/"$VM"-disk
fi
$PRINT $DEST_CMD mkdir /mnt/"$VM"-disk
$PRINT $DEST_CMD mount /dev/"$VG_DST"/"$VM"-disk /mnt/"$VM"-disk
fi
# volume *-data
if [ ! -z "$DATA_SIZE" ]; then
echo "size of disk: $DATA_SIZE"
if [ $FINAL == 0 ] ; then
if [ ! -z "$VG_DATA" ]
then
$PRINT $DEST_CMD lvcreate -n "$VM"-data -L "$DATA_SIZE" "$VG_DATA"
$PRINT $DEST_CMD mkfs.ext4 /dev/"$VG_DATA"/"$VM"-data
$PRINT $DEST_CMD mkdir /mnt/"$VM"-data
$PRINT $DEST_CMD mount /dev/"$VG_DATA"/"$VM"-data /mnt/"$VM"-data
else
$PRINT $DEST_CMD lvcreate -n "$VM"-data -L "$DATA_SIZE" "$VG_DST"
$PRINT $DEST_CMD mkfs.ext4 /dev/"$VG_DST"/"$VM"-data
$PRINT $DEST_CMD mkdir /mnt/"$VM"-data
$PRINT $DEST_CMD mount /dev/"$VG_DST"/"$VM"-data /mnt/"$VM"-data
fi
fi
fi
if [ ! -z "$SWAP_SIZE" ]; then
echo "size of swap: $SWAP_SIZE"
if [ $FINAL == 0 ] ; then
$PRINT $DEST_CMD lvcreate -n "$VM"-swap -L "$SWAP_SIZE" "$VG_DST"
$PRINT $DEST_CMD mkswap /dev/"$VG_DST"/"$VM"-swap
fi
fi
# operation finale : extinction de la vm sur l'hyperviseur source
if [ $FINAL == 2 ] ; then
$PRINT "shuting down $VM..."
$PRINT $SRC_CMD xm shutdown -w "$VM"
fi
# snapshot + migration
if [ ! -z "$DISK_SIZE" ]; then
$PRINT $SRC_CMD lvcreate --snapshot --size 1500m --name "$VM"-disk-snap /dev/"$VG_SRC"/"$VM"-disk
$PRINT $SRC_CMD mkdir /mnt/"$VM"-disk-snap
$PRINT $SRC_CMD mount -o ro /dev/"$VG_SRC"/"$VM"-disk-snap /mnt/"$VM"-disk-snap
if [ "$DEST_CMD" == "" ] ; then
echo " Copying /mnt/${VM}-disk from ${HYP_SRC}:/mnt/${VM}-disk-snap ..."
$PRINT $DEST_CMD rsync -a -H --numeric-ids $VERBOSE --delete root@"$HYP_SRC":/mnt/"$VM"-disk-snap/ /mnt/"$VM"-disk
else
echo " Copying ${VM}-disk-snap to ${HYP_DST}:/mnt/${VM}-disk... "
$PRINT $SRC_CMD rsync -a -H --numeric-ids $VERBOSE --delete /mnt/"$VM"-disk-snap/ root@"$HYP_DST":/mnt/"$VM"-disk
fi
fi
if [ ! -z "$DATA_SIZE" ]; then
$PRINT $SRC_CMD lvcreate --snapshot --size 1500m --name "$VM"-data-snap /dev/"$VG_SRC"/"$VM"-data
$PRINT $SRC_CMD mkdir /mnt/"$VM"-data-snap
$PRINT $SRC_CMD mount -o ro /dev/"$VG_SRC"/"$VM"-data-snap /mnt/"$VM"-data-snap
if [ "$DEST_CMD" == "" ] ; then
echo " Copying /mnt/${VM}-data from ${HYP_SRC}:/mnt/${VM}-data-snap ..."
$PRINT $DEST_CMD rsync -a -H --numeric-ids $VERBOSE --delete root@"$HYP_SRC":/mnt/"$VM"-data-snap/ /mnt/"$VM"-data
else
echo " Copying ${VM}-data-snap to ${HYP_DST}:/mnt/${VM}-data... "
$PRINT $SRC_CMD rsync -a -H --numeric-ids $VERBOSE --delete /mnt/"$VM"-data-snap/ root@"$HYP_DST":/mnt/"$VM"-data
fi
fi
# fichier de configuration
if [ $FINAL == 0 ] ; then
echo " Copying /etc/xen/${VM}.cfg"
if [ "$DEST_CMD" == "" ] ; then
$PRINT scp root@"$HYP_SRC":/etc/xen/"$VM".cfg /etc/xen
else
$PRINT $SRC_CMD scp /etc/xen/"$VM".cfg root@"$HYP_DST":/etc/xen
fi
fi
# verifier le paramétrage de noyau
# nettoyage sur la source
if [ ! -z "$DISK_SIZE" ]; then
$PRINT $SRC_CMD umount /mnt/"$VM"-disk-snap
$PRINT $SRC_CMD rmdir /mnt/"$VM"-disk-snap
$PRINT $SRC_CMD lvremove -f /dev/"$VG_SRC"/"$VM"-disk-snap
fi
if [ ! -z "$DATA_SIZE" ]; then
$PRINT $SRC_CMD umount /mnt/"$VM"-data-snap
$PRINT $SRC_CMD rmdir /mnt/"$VM"-data-snap
$PRINT $SRC_CMD lvremove -f /dev/"$VG_SRC"/"$VM"-data-snap
fi
# derniers reglages
echo " Installing kernel modules..."
$PRINT $DEST_CMD rsync -a $VERBOSE /lib/modules/ /mnt/${VM}-disk/lib/modules/
# verifier que la fstab est en ext4 pour les xvda*
# nettoyage sur la destination
if [ ! -z "$DISK_SIZE" ]; then
$PRINT $DEST_CMD umount /mnt/"$VM"-disk
$PRINT $DEST_CMD rmdir /mnt/"$VM"-disk
fi
if [ ! -z "$DATA_SIZE" ]; then
$PRINT $DEST_CMD umount /mnt/"$VM"-data
$PRINT $DEST_CMD rmdir /mnt/"$VM"-data
fi
# demarrage auto de la vm
#$PRINT $DEST_CMD ln -s /etc/xen/"$VM".cfg /etc/xen/auto/"$VM".cfg
# verifier le fichier de conf + xl create
echo "Migration (pas tout a fait) termine"
echo "- verifier la fstab destination (ext4!)"
echo "- verifier le fichier /etc/xen/$VM.cfg (kernel et bridge)"
echo "- eteindre la machine sur $HYP_SRC"
echo "- allumer la machine sur $HYP_DST"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment