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://code.allanglesit.net/bash/rename-vdisks.sh

[sourcecode language=”bash” gutter=”true” collapse=”true” firstline=”1″ title=”Expand 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 |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 |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[/sourcecode]