OVMCLI: Standardize the Naming of Virtual Disks

February 6th, 2013 | Tags: , , , , , ,

Today we are going over a script I wrote to utilize the ovmcli to rename all virtual disks to follow a understandable standard, in this case we will take the vmname and append a disk identifier to it.  The first disk (slot 0) will be appended with “_system.img” with all subsequent being appended with “_dataX.img” where X is the slot number.  This is a pretty simple script, but depending on the size of your environment it could take a significant amount of time to do a run in your environment due to the number of calls that you need to make to the OVM Manager.

I have built in a few parameters.  We need to provide the OVM 3.x Manager server name, additionally we can provide the port, and username.  Both of which have default values.  Additionally I have added a dry run parameter which will give you the chance to see what the output would be.  Finally there is a verbose parameter which is self explanatory.

Please keep in mind that in OVM 3.x vdisk names are simple metadata.  This doesn’t change any names on disk.  But this does help when you have taken the time to name your vdisks when creating the VMs but have had to re-manage a hypervisor, as in that scenario you end up being able to rediscover the VMs themselves, and their names, but the vdisks will come up as named their UUID.

Please keep in mind that this script will make multiple SSH connections to the ovmcli, as such you will want to use keys to streamline this authentication.  I have an article covering that here.

Name       : rename-vdisks.sh
Version   :  1.2.1
MD5        :  c1a0cf9935a4cdcfd12f3eda71bfdd5d
SHA256  :  bd96192e20e5371b0124eec1c8372c97dbf8469b2c5538400461b82d7f43f5e2
URL         :  http://source.allanglesit.net/pub/rename-vdisks.sh

#!/bin/bash
# chkconfig:
# description:
#
#: Script Name    : rename-vdisks.sh
#: Version    : 1.2.1
#: Author    : Matthew Mattoon - http://blog.allanglesit.com
#: Date Created    : January 26, 2013
#: Date Updated    : February 20, 2013
#: Description    : Renames all OVM 3.x virtual disks to a standard naming convention.
#: Examples    : rename-vdisks.sh -m OVMMANAGER -p PORT -u USER -v
#:         : rename-vdisks.sh -m ovmserver.localdomain -v

usage()
{
cat << EOF
usage: $0 options

This script allows you to rename all OVM 3.x virtual disks to follow a consistent standard.

Standard:    vmname_system.img (slot 0)
vmname_dataX.img (slot 1-x where X is the slot number)
vmname1_vmname2_dataX.img (where a disk is shared between 2 VMs)

OPTIONS:
-h    Show this message
-m    OVM 3.x Manager Server (required).
-p    OVM 3.x Manager Port (default: 10000).
-u    OVM 3.x Manager User (default: admin).
-d   Dry Run.  Script will make no changes, but instead provide the output of an actual run.
-v   Verbose flag.
EOF
}

while getopts "hm:pudv" OPTION
do
case $OPTION in
h) usage; exit 1;;
m) ovmmgr=$OPTARG;;
p) ovmport=$OPTARG;;
u) ovmuser=$OPTARG;;
d) dryrun=1;;
v) verbose=1;;
?) usage; exit 1;;
esac
done

if [[ -z $ovmmgr ]]
then
usage
exit 1
fi

echo ""

if [[ -z $ovmuser ]]
then
ovmuser=admin
echo "Using default OVM User [ $ovmuser ]"
fi

if [[ -z $ovmport ]]
then
ovmport=10000
echo "Using default OVM Port [ $ovmport ]"
fi

if [ "$dryrun" = "1" ]
then
echo "Using Dry Run Option"
fi

if [ "$verbose" = "1" ]
then
echo "Using Verbose Option"
fi

echo ""
echo ""
echo `sed -n 3p $0 | sed 's/#://'`
echo `sed -n 5p $0 | sed 's/#://'`
echo `sed -n 4p $0 | sed 's/#://'`
echo ""
echo ""

for i in `ssh -p $ovmport $ovmuser@$ovmmgr "list vmdiskmapping" | grep -v 'OVM>\|Command:\|Status:\|Time:\|Data:' | sed 's/  /:/g'`
do
id=`echo $i | cut -d ":" -f 3`
echo "Examining $id  "
read name id slot emdev vdisk vm <<<$(ssh -p $ovmport $ovmuser@$ovmmgr "show vmdiskmapping id=$id" | grep -v 'OVM>\|Command:\|Status:\|Time:\|Data:' | sed 's/Emulated Block Device/EmulatedBlockDevice/' | sed 's/Virtual Disk Id/VirtualDiskId/' | sed 's/Vm Id/VmId/' | sed 's/  //' | sed 's/ = /=/g' | sed 's/  \[/:\[/g')
name=`echo $name | cut -d = -f 2`
id=`echo $id | cut -d = -f 2`
slot=`echo $slot | cut -d = -f 2`
emdev=`echo $emdev | cut -d = -f 2`
vdiskid=`echo $vdisk | cut -d = -f 2 | cut -d ":" -f 1`
vdiskname=`echo $vdisk | cut -d = -f 2 | cut -d ":" -f 2 | sed 's/\[\|\]//g'`
vm=`echo $vm | cut -d = -f 2 | cut -d ":" -f 2 | sed 's/\[\|\]//g'`
if [ "$slot" != "0" ]
then
slotname=data"$slot".img
else
slotname=system.img
fi
newvdiskname="$vm"_"$slotname"

imginfo=( `ssh -p $ovmport $ovmuser@$ovmmgr "show virtualdisk id=$vdiskid" | grep -v 'OVM>\|Command:\|Status:\|Time:\|Data:' | sed 's/Max (GiB)/Max(GiB)/' | sed 's/Used (GiB)/Used(GiB)/' | sed 's/Repository Id/RepositoryId/' | sed 's/Vm /Vm/' | sed 's/  //' | sed 's/ = /=/g' | sed 's/  \[/:\[/g'` )
imgname=`printf "%s\n" "${imginfo[0]}" | cut -d = -f 2`
imgid=`printf "%s\n" "${imginfo[1]}" | cut -d = -f 2`
imgmaxsize=`printf "%s\n" "${imginfo[2]}" | cut -d = -f 2`
imgusedsize=`printf "%s\n" "${imginfo[3]}" | cut -d = -f 2`
imgshareable=`printf "%s\n" "${imginfo[4]}" | cut -d = -f 2`
imgrepoid=`printf "%s\n" "${imginfo[5]}" | cut -d = -f 2 | cut -d : -f 1`
imgreponame=`printf "%s\n" "${imginfo[5]}" | cut -d = -f 2 | cut -d : -f 2 | sed 's/\[\|\]//g'`
if [ "$verbose" = "1" ]
then
echo "  VM Name         :  $vm"
echo "  Disk Id         :  $vdiskid"
echo "  Disk Name       :  $vdiskname"
echo "  Slot Number     :  $slot"
echo "  Shareable       :  $imgshareable"
fi
if [ -n "$vdiskid" -o  "$vdiskid" != *".iso" ]
then
if [ "$imgshareable" = "Yes"  ]
then
for item in `printf "%s\n" "${imginfo[@]}" | grep Vm | cut -d : -f 2 | sed 's/\[\|\]//g'`
do
shareddiskvmnames+="$item"_
done
newvdiskname="${shareddiskvmnames}${slotname}"
unset shareddiskvmnames
fi
if [ "$vdiskname" != "$newvdiskname" ]
then
if [ "$verbose" = "1" ]
then
echo "  Disk Name       :  $vdiskname"
echo "  New Disk Name   :  $newvdiskname"
fi
echo "Renaming [ $vdiskname ] to follow Standard [ $newvdiskname ]  "
if [ "$dryrun" = "1" ]
then
echo ""
else
ssh -p $ovmport $ovmuser@$ovmmgr "edit virtualdisk id=$vdiskid name=$newvdiskname" | grep "Status:"
fi
echo ""
else
echo "Name [ $vdiskname ] follows Standard [ $newvdiskname ]"
echo ""
fi
else
echo "Virtual CDROM Detected.  Rename not Required."
echo ""
fi
done
Comments are closed.