esxcfg-rename-datastore.pl

Version 3

    Table of Contents

    • Author

    • Description

    • Category

    • Requirements

    • Version Support

    • Use Cases

    • Defaults and Options

    • Sample Execution

     

     

     

    Author

    William Lam

     

    Description

    This script was motivated by VMTN community member and blogger Jason Boche who posed a question on the VMware forums regarding a Scripted solution for changing the default datastore name to include the hostname of the ESX(i) host. Please take a look at the various use cases that is supported with this script.

     

    UPDATE2: Updated the script to handle additional use cases/features with some feedback from VMTN user cskowmh at: http://communities.vmware.com/message/1461837#1461837

     

    UPDATE1: Thanks to Jason for noticing this bug? If your ESX(i) host is being managed by vCenter and you use this script and directly connect to the host, the script will not properly rename the datastore even though there are no errors. The script has now been updated to check if the host is being managed by vCenter and provide a more friendly error message. When you connect to your vCenter to rename your host, you'll need to specify --vihost which will be the name/IP of your ESX)i) host that you would like to update. Find sample execution down below

     

    Category

    • Initial Server Set Up

     

    Requirements

     

    Version Support

    • Supports ESX(i) (licensed version only)

     

    Usage

    [vi-admin@scofield scripts]$ ./esxcfg-rename-datastore.pl
    Required command option 'txtplacement' not specified.
    
    Synopsis: ./esxcfg-rename-datastore.pl OPTIONS
    
    
    Command-specific options:
       --datastore
          Name of specific datastore to rename
       --hostlist
          Lists of ESX(i) hosts to perform operation _IF_ they're being managed by vCenter (default is ALL hosts in vCenter)
       --operation (default 'dryrun')
          Operation to perform [rename|dryrun]
       --txtformat (default 'LOCAL')
          New name for the datastore (e.g. LOCAL => LOCAL{1,2,3...}-SHORTHOSTNAME)
       --txtplacement (required)
          Format placement of the hostname string [append|prepend]
    
    Common VI options:
       --config (variable VI_CONFIG)
          Location of the VI Perl configuration file
       --credstore (variable VI_CREDSTORE)
          Name of the credential store file defaults to <HOME>/.vmware/credstore/vicredentials.xml on Linux and <APPDATA>/VMware/credstore/vicredentials.xml on Windows
       --encoding (variable VI_ENCODING, default 'utf8')
          Encoding: utf8, cp936 (Simplified Chinese), iso-8859-1 (German), shiftjis (Japanese)
       --help
          Display usage information for the script
       --passthroughauth (variable VI_PASSTHROUGHAUTH)
          Attempt to use pass-through authentication
       --passthroughauthpackage (variable VI_PASSTHROUGHAUTHPACKAGE, default 'Negotiate')
          Pass-through authentication negotiation package
       --password (variable VI_PASSWORD)
          Password
       --portnumber (variable VI_PORTNUMBER)
          Port used to connect to server
       --protocol (variable VI_PROTOCOL, default 'https')
          Protocol used to connect to server
       --savesessionfile (variable VI_SAVESESSIONFILE)
          File to save session ID/cookie to utilize
       --server (variable VI_SERVER, default 'localhost')
          VI server to connect to. Required if url is not present
       --servicepath (variable VI_SERVICEPATH, default '/sdk/webService')
          Service path used to connect to server
       --sessionfile (variable VI_SESSIONFILE)
          File containing session ID/cookie to utilize
       --url (variable VI_URL)
          VI SDK URL to connect to. Required if server is not present
       --username (variable VI_USERNAME)
          Username
       --verbose (variable VI_VERBOSE)
          Display additional debugging information
       --version
          Display version information for the script
    
    

     

    Use Cases

     

    a) Rename datastore(s) directly connected to a specific ESX(i) host NOT being managed by vCenter

     

    b) Rename datastore(s) for a specific set of ESX(i) host(s) being managed by vCenter connecting directly to vCenter

     

    c) Rename datastore(s) for ALL ESX(i) host(s) being managed by vCenter connecting directly to vCenter

     

    Defaults and Options

    --operation by default is set to dryrun which will let you preview what the changes WILL BE but WILL NOT make any changes until the flag has been specified with rename. This allows a user to mess around with the various options and unless user explicity specifies "--operation rename", nothing in the environment will change

     

    --hostlist is to be used in conjunction when conecting to vCenter, by default ALL ESX(i) hosts will be scanned unless the user specifies a specific list

     

    --datastore can be specified to only rename a specific datastore. By default, only datastores that match the following "^datastore1" will be up for rename unless a specific datastore is explicity specified.

     

    --txtformat is used to specified the custom text that will either be appeneded or prepended as the new name of the datastore rename alongside the short hostname of the host. e.g. "LOCAL"

     

    --txtplacement is used to specify whether the short hostname will be appended or prepended to the --txtformat of the host when renaming the datastore

     

    Note: y default the short hostname will be used but if for whatever reason short hostname is unresolvable via dnsInfo config, the FQDN will be used.

     

     

    Sample Execution

     

    Use Case #1 - Rename a specific datastore "datastore1" with the text "local-storage-" and prepending the the short hostname to text entry

     

    Preview:

    [vi-admin@scofield scripts]$ ./esxcfg-rename-datastore.pl --server esxi4-9.primp-industries.com --datastore datastore1 --txtplacement prepend --txtformat local-storage-
            DRYRUN - Renaming "datastore1" to "esxi4-9-local-storage-1" ...
    

     

    Commit:

    [vi-admin@scofield scripts]$ ./esxcfg-rename-datastore.pl --server esxi4-9.primp-industries.com --datastore datastore1 --txtplacement prepend --txtformat local-storage- --operation rename
            Renaming "datastore1" to "esxi4-9-local-storage-1" ...
            Sucessfully renamed datastore!
    

     

    Use case #2 - Rename ALL datastores that begin with "^datastore1" with the text "local-storage-" and prepending the the short hostname to text entry to a specific list of ESX(i) host specifying --hostlist connecting to vCenter

     

    hostlist:

    [vi-admin@scofield scripts]$ cat hosts
    esxi4-4.primp-industries.com
    esxi4-5.primp-industries.com
    

     

    Preview:

    [vi-admin@scofield scripts]$ ./esxcfg-rename-datastore.pl --server reflex.primp-industries.com --txtplacement prepend --txtformat local-storage-
            DRYRUN - Renaming "datastore1 (2)" to "esxi4-4-local-storage-1" ...
            DRYRUN - Renaming "datastore1 (3)" to "esxi4-4-local-storage-2" ...
            DRYRUN - Renaming "datastore1 (4)" to "esxi4-5-local-storage-1" ...
            DRYRUN - Renaming "datastore1 (5)" to "esxi4-5-local-storage-2" ...
            DRYRUN - Renaming "datastore1" to "esxi4-6-local-storage-1" ...
            DRYRUN - Renaming "datastore1 (1)" to "esxi4-7-local-storage-1" ...
    

     

    Note: Notice that both esxi4-4 and esxi4-5 both have 2 local VMFS volumes, the script will properly handle the number of volumes per host and increment a counter. In the commit example, we'll only rename 2 hosts (esxi4-4 and esxi4-5)

     

    Preview2:

    [vi-admin@scofield scripts]$ ./esxcfg-rename-datastore.pl --server reflex.primp-industries.com --txtplacement prepend --txtformat local-storage- --hostlist hosts
    esxi4-4.primp-industries.com
            DRYRUN - Renaming "datastore1 (2)" to "esxi4-4-local-storage-1" ...
            DRYRUN - Renaming "datastore1 (3)" to "esxi4-4-local-storage-2" ...
    esxi4-5.primp-industries.com
            DRYRUN - Renaming "datastore1 (4)" to "esxi4-5-local-storage-1" ...
            DRYRUN - Renaming "datastore1 (5)" to "esxi4-5-local-storage-2" ...
    

     

    Commit:

    [vi-admin@scofield scripts]$ ./esxcfg-rename-datastore.pl --server reflex.primp-industries.com --txtplacement prepend --txtformat local-storage- --hostlist hosts --operation rename
    esxi4-4.primp-industries.com
            Renaming "datastore1 (2)" to "esxi4-4-local-storage-1" ...
            Sucessfully renamed datastore!
    
            Renaming "datastore1 (3)" to "esxi4-4-local-storage-2" ...
            Sucessfully renamed datastore!
    
    esxi4-5.primp-industries.com
            Renaming "datastore1 (4)" to "esxi4-5-local-storage-1" ...
            Sucessfully renamed datastore!
    
            Renaming "datastore1 (5)" to "esxi4-5-local-storage-2" ...
            Sucessfully renamed datastore!
    

     

    Use case #3 - Rename the remainder qualified ESX(i) hosts that begin with "^datastore1" with the text "local-storage-" and prepending the the short hostname to text entry connecting to vCenter

     

    Preview:

    [vi-admin@scofield scripts]$ ./esxcfg-rename-datastore.pl --server reflex.primp-industries.com --txtplacement prepend --txtformat local-storage-
            DRYRUN - Renaming "datastore1" to "esxi4-6-local-storage-1" ...
            DRYRUN - Renaming "datastore1 (1)" to "esxi4-7-local-storage-1" ...
    

     

    Commit:

    [vi-admin@scofield scripts]$ ./esxcfg-rename-datastore.pl --server reflex.primp-industries.com --txtplacement prepend --txtformat local-storage- --operation rename
            Renaming "datastore1" to "esxi4-6-local-storage-1" ...
            Sucessfully renamed datastore!
    
            Renaming "datastore1 (1)" to "esxi4-7-local-storage-1" ...
            Sucessfully renamed datastore!