Archive for Virtualização

Script Attach Disco USB Xenserver (Melhor Opção)

There is a very nice script for Windows dealing with attaching XenServer USB disk to a guest. It can be found here.

This script has several problems, as I see it. The first – this is a Windows batch script, which is a very limited language, and it can handle only a single VDI disk in the SR group called “Removable Storage”.

As I am a *nix guy, and can hardly handle Windows batch scripts, I have rewritten this script to run from Linux CLI (focused on running from the XenServer Domain0), and allowed it to handle multiple USB disks. My assumption is that running this script will map/unmap *all* local USB disks to the VM.

Following downloading this script, you should make sure it is executable, and run it with the arguments “attach” or “detach”, per your needs.

And here it is:

Download usbmap-to-vm.txt

——————————————————————————————-

#!/bin/bash
# This script will map USB devices to a specific VM
# Written by Ez-Aton, http://run.tournament.org.il , with the concepts
# taken from http://jamesscanlonitkb.wordpress.com/2012/03/11/xenserver-mount-usb-from-host/
# and http://support.citrix.com/article/CTX118198

# Variables
# Need to change them to match your own!
REMOVABLE_SR_UUID=d03f247d-6fc6-a396-e62b-a4e702aabcf0
VM_UUID=b69e9788-8cd2-0074-5bc1-63cf7870fa0d
DEVICE_NAMES="hdc hde" # Local disk mapping for the VM
XE=/opt/xensource/bin/xe

function attach() {
        # Here we attach the disks
        # Check if storage is attached to VBD
        VBDS=`$XE vdi-list sr-uuid=${REMOVABLE_SR_UUID} params=vbd-uuids --minimal | tr , ' '`
        if [ `echo $VBDS | wc -w` -ne 0 ]
        then
                echo "Disks are allready attached. Check VBD $VBDS for details"
                exit 1
        fi
        # Get devices!
        VDIS=`$XE vdi-list sr-uuid=${REMOVABLE_SR_UUID} --minimal | tr , ' '`
        INDEX=0
        DEVICE_NAMES=( $DEVICE_NAMES )
        for i in $VDIS
        do
                VBD=`$XE vbd-create vm-uuid=${VM_UUID} device=${DEVICE_NAMES[$INDEX]} vdi-uuid=${i}`
                if [ $? -ne 0 ]
                then
                        echo "Failed to connect $i to ${DEVICE_NAMES[$INDEX]}"
                        exit 2
                fi
                $XE vbd-plug uuid=$VBD
                if [ $? -ne 0 ]
                then
                        echo "Failed to plug $VBD"
                        exit 3
                fi
                let INDEX++
        done
}

function detach() {
        # Here we detach the disks
        VBDS=`$XE vdi-list sr-uuid=${REMOVABLE_SR_UUID} params=vbd-uuids --minimal | tr , ' '`
        for i in $VBDS
        do
                $XE vbd-unplug uuid=${i}
                $XE vbd-destroy uuid=${i}
        done
        echo "Storage Detached from VM"
}
case "$1" in
        attach) attach
                ;;
        detach) detach
                ;;
        *)      echo "Usage: $0 [attach|detach]"
                exit 1
esac

——————————————————————————————

Crédito : http://run.tournament.org.il/attach-usb-disks-to-xenserver-vm-guest/

Adicionar Disco (Storage) Xenserver

Adding new hard-drive in XenServer is a bit different from the traditional Linux process. For XenServer, you need to create a container called a ‘storage repository’ to define a particular storage target (such as a hard disk), in which Virtual Disk Images (VDIs) of VMs are stored. A VDI is nothing but an abstracted storage space which acts as the hard-disk for VMs.

Xen storage repository supports IDE, SATA, SCSI and SAS drives  when locally connected, apart from iSCSI, NFS, SAS and fiber channel in case of a remote storage.

Steps to create an SR in a XenServer.

1. SSH to the XenServer as root.
2. Find the disk ID of the new device using the following commands:

# cat /proc/partitions

You will see list of all the HDDs and partitions. Find which device is your new local disk. This is probably “sdx”(most probably sdb) or “/cciss/c0d1p0”.

# ll /dev/disk/by-id

This command will list the disk ids for all the partitions/HDDs present in the server as shown in the image below.

Find the disk ID of the “sdx” or “cciss/c0d1”disk. The “scsi-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx” or “cciss-xxxxxxxxxxxxxxxxxxxxxxxxxx”format is what you need.

3. Find out the ‘host-uuid’ in the XenSserver using the following command.

#xe host-list

The uuid (RO) is the ‘host-uuid’  you need.

4.Create a Storage Repository (SR):

# xe sr-create content-type=user device-config:device=/dev/disk/by-id/<scsi-xxxxxxxxxxxxxxxxxxxxxxxxx> host-uuid=<host-uuid> name-label=”Local Storage 2” shared=false type=lvm
– Or –
# xe sr-create content-type=user device-config:device=/dev/disk/by-id/<cciss-xxxxxxxxxxxxxxxxxxxxxxxxx> host-uuid=<host-uuid> name-label=”Local Storage 2” shared=false type=lvm
– Or –
# xe sr-create content-type=user device-config:device=/dev/<sdx> host-uuid=<host-uuid> name-label=”Local Storage 2” shared=false type=lvm

You can verify it from XenCenter with the following steps:

1. Connect to the XenCenter.
2. Go to the ‘Storage tab’. You will find the details of all storage repositories here. You can see the storage ‘Local Storage2’ in the list.
This signifies that the disk has been added successfully. Now, you can start creating VDIs on it.

Créditos : http://knowledgelayer.softlayer.com/questions/534/Adding+a+hard+drive+to+Citrix+XenServer

Script ATTACH USB STORAGE no Xenserver

Modify 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:

2. 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

3. 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

4. Once you have the UUID of the Removable Storage, make the change to the script:

REM Removable Storage Repository UUID

SET REMOVABLE_SR_UUID=CHANGEME

5. Determine the UUID of the virtual machine (VM) you wish to attach the storage to. To do this, run:

xe vm-list

6. With the UUID of the VM, make the change to the script:

REM UUID of the VM you wish to attach the USB storage to

SET VM_UUID=CHANGEME

7. Set the device order. It is important to ensure that there is no overlap and this is done by looking at the number of devices attached to the VM. For example, if only one storage device is currently being used (example: virtual hard disk), the next device name to use will be hdb.

REM Device name/order on the VM (example: hdb, hdc, hdd…)

SET DEVICE_NAME=hdb

8. Run or Schedule the Batch Program

9. Once you have made the changes to the batch program, test it by running it from a Command Prompt window. Note that the utility requires a parameter, otherwise it exits.

10. To attach the USB storage device to the VM, run:

usbmount.bat ATTACH

11. To detach the USB storage device from the VM, run:

usbmount.bat DETACH

Listing of USBMOUNT.BAT

@ECHO OFF
setlocal
REM Attach a USB flash device to a specified VM on XenServer 4.x

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

# ——————————————————————————————- #

Script usbmount.sh

#!/bin/bash
xe vbd-create vm-uuid=c145b8f0-7c3d-9208-c219-14cbec468911 device=autodetect vdi-uuid=146a5021-77a5-44b6-bcaa-edc5a1d4c822 > /marlo/vbduuid
vbduuid=$(cat /marlo/vbduuid)
xe vbd-plug uuid=$vbduuid

# ——————————————————————————————- #

Crédito : http://support.citrix.com/article/CTX118198

Autostart VM Xenserver 6.0

VM auto start feature

I was a little puzzled on how I could set up my License Server VPX appliance to auto start on booting my XenServer, but I figured it was now set with the vApp features and settings. However the GUI did not offer a select box to enable the auto start feature at the Start Options in the General Properties of the VM. I did not configure HA as it is not necessary right now for my Homelab environment. I just made the assumption that creating and configuring the vApp would enable the auto start feature all the same. And turned off the Homelab servers to test it when I would boot my servers the next day.

 

Unfortunately for me, it wasn’t that easy this time. As Bill Carovano explains in this post on the Citrix Forums, so design decisions changed the auto start feature availability. As it interfered with the HA, DR and Pool upgrade functionality it was opted out for the XenServer 6 release.

 

Alternative configuration

For me it still is a very useful feature to ensure my Citrix License Server is automatically started when my XenServer is booted, so luckily for me Bill also gave the right commands to enable the feature with the CLI and have the perfect setup back for my Homelab

You first have to enable the auto power on feature at pool level, before being able to configure it for each VM individually.

* Find the uuid of the Pool:

[root@ ~]# xe pool-list
uuid ( RO) : [uuid-pool]
name-label ( RW): [pool-name]
name-description ( RW): [pool-desc]
master ( RO): [uuid-xs]
default-SR ( RW): [uuid-sr]

Which returns the following values:

  • uuid-pool: A unique identifier for the Pool.
  • pool-name: The name given to the Pool.
  • pool-desc: The description set for the Pool.
  • uuid-xs: The unique identifier of the XenServer that currently is the Pool Master.
  • uuid-sr: The unique identifier for the default Storage Repository configured for the Pool.

 

* Enable the auto power on feature at pool level:

[root@ ~]# xe pool-param-set uuid=[uuid-pool] other-config:auto_poweron=true

Which uses the following additional syntax:

  • uuid-pool: A unique identifier for the Pool.

 

* Find the uuid of the VM:

The quicky way to find the uuid of your VM is to run the vm-list command. This does however give you an overview of all VMs, so if you have alot of VMs defined, try to narrow it done with the name-label parameter (which is case sensitive). With the uuid known for the VM, you can easily enable the auto start feature.

[root@ ~]# xe vm-list
uuid ( RO) : [uuid-vm]
name-label ( RW): [vm-name]
power-state ( RO): [vm-power]

Which returns the following values:

  • uuid-vm: A unique identifier for the Virtual Machine.
  • vm-name: The name given to the Virtual Machine.
  • vm-power: Shows the power state the VM is currently in (running, halted).

 

* Enable the auto power on feature for the specified VM:

[root@ ~]# xe vm-param-set uuid=[uuid-vm] other-config:auto_poweron=true

Which uses the following additional syntax:

  • uuid-vm: A unique identifier for the Virtual Machine.

Crédito : http://www.virtues.it/2011/10/xenserver6-vm-auto-start-feature/