Cloner une VM CentOS et la paramétrer "automatiquement"

Préparer une VM et l'installer a minima avec un CentOS.

Copier le script ci-dessous dans un repertoire (ex. : home de root)

Arrêter la VM template.

Créer un répertoire dans la LUN ayant le nom de la VM.

Cloner le disque (en mode shell sous VMWare) :

vmkfstools -i \
  /vmfs/volumes/LUN-OT1/TPL-CENTOS-6.5-x86_64/TPL-CENTOS-6.5-x86_64.vmdk \
  /vmfs/volumes/<Nom de la LUN>/<Nom du repertoire cible>/<Nom de la VM>.vmdk -d thin

Créer une nouvelle VM et lui affecter le disque créé.

Démarrer la VM avec le disque cloné.

Lancer la VM et se logger en root.

Lancer le script :

[root@host ~]# update-tpl.sh <Nom de la VM>

Si tout s’exécute correctement, la carte réseau eth0 sera reconfigurée avec la nouvelle adresse MAC, le hostname ainsi que le VolumeGroup (LVM) auront été renommé correctement.

update-tpl.sh source :

#/bin/sh
. /etc/init.d/functions
 
UPD="-UPD"
BFR="-BFR"
 
NTWCFG="/etc/sysconfig/network"
ETHCFG="/etc/sysconfig/network-scripts/ifcfg-eth0"
UDVNET="/etc/udev/rules.d/70-persistent-net.rules"
GRBCFG="/boot/grub/grub.conf"
FSTCFG="/etc/fstab"
HSTCFG="/etc/hosts"
INITRD="/boot/`cat ${GRBCFG} | grep -v grep | grep -v '#' | grep initrd | awk -F " /" '{ print $2 }'`"
NEWHOST="$1"
OLDHOST="`hostname`"
 
function moveBackup() {
    mv "${1}" "${1}${BFR}"
    checkExec $? "Moving ${1} to ${1}${BFR}"
}
 
function moveReal() {
    mv "${1}${UPD}" "${1}"
    checkExec $? "Moving ${1}${UPD} to ${1}"
}
 
function checkExec() {
    echo
    echo -n ">> $2"
    if [ $1 -eq 0 ] ; then
        success
        return
    fi
    if [ "$3" == "" ] ; then
        MSG="ERROR $2, procedure aborted..."
    else
        MSG="$3"
    fi
    failure
    echo $MSG
    exit 1
}
 
function checkAnswer() {
    echo
    if [ "$1" == "" ] ; then
        echo -n "* Is it ok [y/N] ?"
    else
        echo -n "* $1"
    fi
    read answer
    case "${answer}" in
        "y")
        ;;
        *)
        echo -n "Aborting..."
        failure
        echo
        exit 1
        ;;
esac
}
 
if [ "${NEWHOST}" == "`hostname`" ] || [ "${1}" == "" ] ; then
    echo "* Same hostname, nothing to do... showing operations but no actions !"
    DEBUG="-DEBUG"
    NEWHOST="newhostname"
    set -x
elif [ "$2" != "" ] ; then
    echo "* Debug mode... showing operations but no actions !"
    DEBUG="-DEBUG"
    set -x
fi
 
echo -n "New hostname: ${NEWHOST}"
 
########### HOST
sed "s/${OLDHOST}/${NEWHOST}${DEBUG}/g" ${NTWCFG} > "${NTWCFG}${UPD}"
sed "s/${OLDHOST}/${NEWHOST}${DEBUG}/g" ${HSTCFG} > "${HSTCFG}${UPD}"
checkExec $? "Renaming hostname '${OLDHOST}' to '${NEWHOST}'"
 
########### ETHERNET
HWADDR=`cat ${UDVNET} | grep -v eth0 | grep "NAME=\"eth" | awk -F "ATTR{address}==\"" '{ print \$2}' |  awk -F "\"," '{ print \$1}' | egrep '([0-9a-f]{2}[:-]){5}([0-9a-f]{2})' | awk '{gsub(/^ +| +$/,"")} {print $0}'`
if [ "${HWADDR}" != "" ] ; then
    sed "s/eth1/eth0/g" ${UDVNET} > "${UDVNET}${UPD}"
    checkExec $? "Updating udev informations"
    echo "DEVICE=eth0
HWADDR=${HWADDR^^}
BOOTPROTO=dhcp
NM_CONTROLLED=yes
ONBOOT=yes
TYPE=Ethernet
#IPADDR=192.168.1.
#NETMASK=255.255.255.0
#BROADCAST=192.168.1.255
#NETWORK=192.168.1.0" > "${ETHCFG}${UPD}"
    checkExec $? "Updating NIC informations"
fi
########### LVM / FSTAB / GRUB
OLD_VGNAME=`vgdisplay | grep -v grep | grep "VG Name" | awk -F "VG Name" '{print \$2}' | awk '{gsub(/^ +| +$/,"")} {print $0}'`
VGNAME="vg_`echo "${NEWHOST}" | sed 's/[-_.]//g'`${DEBUG}"
checkAnswer "Rename '${OLD_VGNAME}' to '${VGNAME}' [y/N] ?"
sed "s/${OLD_VGNAME}/${VGNAME}/g" ${GRBCFG} > "${GRBCFG}${UPD}"
sed "s/${OLD_VGNAME}/${VGNAME}/g" ${FSTCFG} > "${FSTCFG}${UPD}"
 
########### MODIFICATIONS
echo "Point of no return :)"
checkAnswer "Do you want me to modify things [y/N] ?"
vgrename ${OLD_VGNAME} ${VGNAME}
checkExec $? "Renaming ${OLD_VGNAME} to ${VGNAME}" "ERROR executing vgrename ! (you should do something if you want the system to boot correctly... o_O')"
moveBackup ${HSTCFG}
moveReal ${HSTCFG}
moveBackup ${NTWCFG}
moveReal ${NTWCFG}
moveBackup ${UDVNET}
moveReal ${UDVNET}
moveBackup ${ETHCFG}
moveReal ${ETHCFG}
moveBackup ${GRBCFG}
moveReal ${GRBCFG}
moveBackup ${FSTCFG}
moveReal ${FSTCFG}
cp "${INITRD}" "${INITRD}${BFR}"
checkExec $? "Copying ${INITRD} to ${INITRD}${BFR}"
mkinitrd -f ${INITRD}  `uname -r`
checkExec $? "Creating initrd ${INITRD}"
echo