CTX118198
XenServer
XenServer_all
Storage
2016-04-15
2014-05-23
This article provides a script for automating the procedure of attaching and detaching USB mass storage devices from a given Virtual Machine on ...

Objective

This article provides a script for automating the procedure of attaching and detaching USB mass storage devices from a given Virtual Machine on XenServer. This method is particularly useful in environments where USB storage such as removable hard disks is used for storing backups that are rotated on a regular basis.

Background

In some environments, USB mass storage devices are used for the purposes of backups. For this to be effective, the USB storage device is rotated and removed from the server after the backup.

Generally, this process requires you to first confirm that the disk is not in use and then manually perform the detach operation. Similarly, when the disk is re-attached to the server, the storage must be again manually attached to the virtual machine.??

Many different backup programs provide the ability to execute programs before and after a backup and using XenServer’s XE utility, it is entirely possible to automate this procedure.


Instructions

The following are the requirements:

  • XenServer Host

  • XenCenter installed on the host running the backup software

  • USB Mass-storage device recognized by XenServer

  • The usbmount.bat batch script

Complete the following procedure:

  1. Installing XenCenter

  2. Modifying the usbmount.bat Script

  3. Listing of USBMOUNT.BAT

Installing XenCenter

The batch program for handling the operation depends on the XE.EXE utility, a command-line interface client for executing commands on XenServer hosts. This utility is included as part of the XenCenter installation, hence XenCenter must be installed on the host on which you intend on running the utility.

If you choose to install XenCenter to a location other than C:\program files\citrix\XenCenter\, ensure to take note of the exact installation path as you will be required to modify the batch script accordingly.

Modifying the usbmount.bat Script

  1. Using a text editor such as Notepad, edit the usbmount.bat script according to your requirements. The following attributes must be set: Set the username, password and address (IP or hostname) of your XenServer host:
    SET XE_USERNAME=root
    SET XE_PASSWORD=CHANGEME
    SET XE_SERVER=xxx.xxx.xxx.xxx

  2. Determine the universally unique identifier (UUID) of the Storage Repository that corresponds with the Removable Storage. You can determine this by running the following command from within your XenServer host console:
    xe sr-list name-label=Removable\ Storage

  3. After you have the UUID of the Removable Storage, make the changes to the script:
    REM Removable Storage Repository UUID
    SET REMOVABLE_SR_UUID=CHANGEME

  4. Determine the UUID of the virtual machine (VM) you wish to attach the storage to. To do this, run:
    xe vm-list

  5. With the UUID of the Virtual Machine, make the change to the script: REM UUID of the Virtual Machine you wish to attach the USB storage to SET VM_UUID=CHANGEME

  6. Set the device order.
    Note: Ensure that there is no overlap and this is done by looking at the number of devices attached to the Virtual Machine.
    Example: If only one storage device is currently being used (for example: virtual hard disk), the next device name to use will be hdb.??
    REM Device name or order on the Virtual Machine (for example: hdb, hdc, hdd...)
    SET DEVICE_NAME=hdb

  7. Run or schedule the Batch Program.

  8. after making the changes to the batch program, test it by running it from a Command Prompt window.
    Note: The utility requires a parameter, otherwise it exits.

  9. To attach the USB storage device to the Virtual Machine, run:
    usbmount.bat ATTACH

  10. To detach the USB storage device from the Virtual Machine, run usbmount.bat DETACH

Listing of USBMOUNT.BAT

 @ECHO OFF setlocal REM Attach a USB flash device to a specified VM on XenServer REM ------------- EDIT THE OPTIONS BELOW TO SUIT YOUR ENVIRONMENT ------------- REM XenServer Credentials SET XE_USERNAME=root SET XE_PASSWORD=CHANGEME SET XE_SERVER=xxx.xxx.xxx.xxx REM Removable Storage Repository UUID SET REMOVABLE_SR_UUID=CHANGEME REM UUID of the VM you wish to attach the USB storage to SET VM_UUID=CHANGEME REM Device name/order on the VM (e.g. hdb, hdc, hdd...) set DEVICE_NAME=hdb REM --------------------------------------------------------------------------- REM XenCenter Path SET XE_CENTER_PATH=C:\program files\citrix\XenCenter\ REM XE Binary and Baseline Parameters SET XE_EXEC="%XE_CENTER_PATH%\xe.exe" -s %XE_SERVER% -u %XE_USERNAME% -pw %XE_PASSWORD% REM Temporary working file SET TEMP_FILE=%TEMP%/xs-usbmount.tmp REM ------------ DO NOT EDIT BEYOND THIS LINE ---------------- IF "%1"=="ATTACH" GOTO ATTACH_STORAGE IF "%1"=="DETACH" GOTO DETACH_STORAGE REM No parameters echo Usage USBMOUNT.BAT ^<ATTACH ^| DETACH^> GOTO END :DETACH_STORAGE %XE_EXEC% vdi-list sr-uuid=%REMOVABLE_SR_UUID% params=vbd-uuids --minimal > %TEMP_FILE% SET /P VBD_UUIDS= < %TEMP_FILE% IF "%VBD_UUIDS%"=="" GOTO NOT_BOUND %XE_EXEC% vbd-unplug uuid=%VBD_UUIDS% %XE_EXEC% vbd-destroy uuid=%VBD_UUIDS% echo.Storage Detached GOTO END :ATTACH_STORAGE REM See if the storage is already bound to a VBD %XE_EXEC% vdi-list sr-uuid=%REMOVABLE_SR_UUID% params=vbd-uuids --minimal > %TEMP_FILE% SET /P VBD_UUIDS= < %TEMP_FILE% IF NOT "%VBD_UUIDS%"=="" GOTO ALREADY_BOUND %XE_EXEC% vdi-list sr-uuid=%REMOVABLE_SR_UUID% params=uuid --minimal > %TEMP_FILE% SET /P VDI_UUID= < %TEMP_FILE% %XE_EXEC% vbd-create vm-uuid=%VM_UUID% device=%DEVICE_NAME% vdi-uuid=%VDI_UUID% --minimal > %TEMP_FILE% SET /P VBD_UUID= < %TEMP_FILE% %XE_EXEC% vbd-plug uuid=%VBD_UUID% echo.VBD UUID attached as: %VBD_UUID% GOTO END :ALREADY_BOUND echo.Removable storage already attached to a VM - aborting. GOTO END :NOT_BOUND echo.Storage device not bound to any VMs - aborting. GOTO END :END endlocal?? 

Applicable Products


 

Join the conversation

Citrix Discussions

Open a case

Citrix Support

特别说明


本文来源为Citrix.com所有,翻译后版权归翻译者所有.如需转载请注明出处.

文档版本


.

广告招租


最新留言


.

广告招租


.