Script – Grow VMFS datastores in command line

Ce message est également disponible en : French

Recently, i have worked on a script to automate these operations because it involved a lot of datastores. The script only works if the LUN hosting the datastore has previously been expanded.

For more information regarding the command line describe in this article you can check this  2002461.


Update :02/06/2015 – Add Powershell + plink

In my powershell script i used a script from  virtu-al.net @Alan Renouf that will generate a choice box (Cluster,ESX,Datastore)

So in this script, you will be able to choice in which cluster and esx the script will be launched.

The script has been tested in ESXi 5.0 U2 but should work with 5.x.

Unzip the file and run the script DatastoreExtend.ps1

– 1 : Enter the vCenter Name

Extend-1

– 2 : Provide the vCenter credentials

Extend-2

– 3 : Then provide the root password of your ESXi.Extend-3– 4 : Select your cluster

Extend-4

– 5 : Select your host

Extend-5

– 6 : Select your dastatore. The script files will be uploaded on it.

Extend-6

 

Capture

Once the script is done, you can choose to restart it on another cluster.

Extend-8

 

 


 

This is the content ofresize.sh” script that will generate a list of datastores. The script will check only vmfs datastore.


 

#!/bin/sh
# Julien VARELA Script 
# Release v1.0 02/03/2015
#Grow VMFS Datastore

#Export Datastore List
esxcli storage filesystem list > /tmp/vmfs.txt
cat /tmp/vmfs.txt | grep “VMFS” | awk ‘{print $2}’ > /tmp/datastorelist.txt # C

while read i; do /tmp/traitement.sh $i;done <  /tmp/datastorelist.txt

#Remove temporary files
rm -rf /tmp/vmfs.txt
rm -rf /tmp/datastorelist.txt

 


Content of  traitement.sh script.

#!/bin/sh
# Julien VARELA Script
# Release v1.0 02/03/2015
#Grow VMFS Datastores

# Retrieve naa.
parted=$(vmkfstools -P /vmfs/volumes/$1 | grep “naa.” | sed ‘s/..$//’)
naa=$(echo $parted  | sed ‘s/ //g’)

#Retrieve last sector available
endsecavailable=$(partedUtil getUsableSectors /vmfs/devices/disks/$naa | awk ‘{print $2}’)

#Retrieve current last sector
endsecactive=$(partedUtil get /vmfs/devices/disks/$naa | sed ‘1d’ |awk ‘{print $3}’)

#Retrieve partition number
numpartition=$(partedUtil get /vmfs/devices/disks/$naa | sed ‘1d’ |awk ‘{print $1}’)

#Retrieve first sector
firstsector=$(partedUtil get /vmfs/devices/disks/$naa | sed ‘1d’ |awk ‘{print $2}’)
echo “Nom du datastore a traiter : ” $1
echo “Identifiant du lun : ” $naa
echo “Premier secteur : ” $firstsector
echo “Dernier secteur en place : ” $endsecactive
echo “Dernier secteur disponible : “$endsecavailable #| awk ‘{print $2}’
echo “Numero de la partion : ” $numpartition

if [ $endsecactive = $endsecavailable ]
then
        echo “Le datastore est deja etendu”
else
echo “VMFS Resize”
partedUtil resize /vmfs/devices/disks/$naa $numpartition $firstsector $endsecavailable

echo “Rescan”
vmkfstools -V
echo “growfs”
vmkfstools –growfs /vmfs/devices/disks/$naa:$numpartition /vmfs/devices/disks/$naa:$numpartition
echo “Rescan”
vmkfstools -V
echo “Grow VMFS Datastore ended on : ” $1
fi


 

Will not be processed, datastore that were already expanded

 

Capture

You can download the zip file here : ExtendDatastore

Unzip the file and run the script DatastoreExtend.ps1 😉 have fun

 

The script will be translate in few days.

Leave a Reply