﻿{"id":179,"date":"2012-05-17T17:12:56","date_gmt":"2012-05-17T20:12:56","guid":{"rendered":"http:\/\/binsfeld.com.br\/wp\/?p=179"},"modified":"2012-05-17T17:18:01","modified_gmt":"2012-05-17T20:18:01","slug":"script-attach-disco-usb-xenserver-melhor-opcao","status":"publish","type":"post","link":"http:\/\/binsfeld.com.br\/wp\/?p=179","title":{"rendered":"Script Attach Disco USB Xenserver (Melhor Op\u00e7\u00e3o)"},"content":{"rendered":"<p>There is a very nice script for Windows dealing with attaching XenServer USB disk to a guest. It can be found\u00a0<a title=\"XenServer Mount USB from HOST\" href=\"http:\/\/jamesscanlonitkb.wordpress.com\/2012\/03\/11\/xenserver-mount-usb-from-host\/\" target=\"_blank\">here<\/a>.<\/p>\n<p>This script has several problems, as I see it. The first \u2013 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 \u201cRemovable Storage\u201d.<\/p>\n<p>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.<\/p>\n<p>Following downloading this script, you should make sure it is executable, and run it with the arguments \u201cattach\u201d or \u201cdetach\u201d, per your needs.<\/p>\n<p>And here it is:<\/p>\n<div>\n<p>Download\u00a0<a href=\"http:\/\/binsfeld.com.br\/wp\/wp-content\/uploads\/2012\/05\/usbmap-to-vm.txt\">usbmap-to-vm.txt<\/a><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<pre>#!\/bin\/bash\r\n# This script will map USB devices to a specific VM\r\n# Written by Ez-Aton, http:\/\/run.tournament.org.il , with the concepts\r\n# taken from http:\/\/jamesscanlonitkb.wordpress.com\/2012\/03\/11\/xenserver-mount-usb-from-host\/\r\n# and http:\/\/support.citrix.com\/article\/CTX118198\r\n\r\n# Variables\r\n# Need to change them to match your own!\r\nREMOVABLE_SR_UUID=d03f247d-6fc6-a396-e62b-a4e702aabcf0\r\nVM_UUID=b69e9788-8cd2-0074-5bc1-63cf7870fa0d\r\nDEVICE_NAMES=\"hdc hde\" # Local disk mapping for the VM\r\nXE=\/opt\/xensource\/bin\/xe\r\n\r\nfunction attach() {\r\n        # Here we attach the disks\r\n        # Check if storage is attached to VBD\r\n        VBDS=`$XE vdi-list sr-uuid=${REMOVABLE_SR_UUID} params=vbd-uuids --minimal | tr , ' '`\r\n        if [ `echo $VBDS | wc -w` -ne 0 ]\r\n        then\r\n                echo \"Disks are allready attached. Check VBD $VBDS for details\"\r\n                exit 1\r\n        fi\r\n        # Get devices!\r\n        VDIS=`$XE vdi-list sr-uuid=${REMOVABLE_SR_UUID} --minimal | tr , ' '`\r\n        INDEX=0\r\n        DEVICE_NAMES=( $DEVICE_NAMES )\r\n        for i in $VDIS\r\n        do\r\n                VBD=`$XE vbd-create vm-uuid=${VM_UUID} device=${DEVICE_NAMES[$INDEX]} vdi-uuid=${i}`\r\n                if [ $? -ne 0 ]\r\n                then\r\n                        echo \"Failed to connect $i to ${DEVICE_NAMES[$INDEX]}\"\r\n                        exit 2\r\n                fi\r\n                $XE vbd-plug uuid=$VBD\r\n                if [ $? -ne 0 ]\r\n                then\r\n                        echo \"Failed to plug $VBD\"\r\n                        exit 3\r\n                fi\r\n                let INDEX++\r\n        done\r\n}\r\n\r\nfunction detach() {\r\n        # Here we detach the disks\r\n        VBDS=`$XE vdi-list sr-uuid=${REMOVABLE_SR_UUID} params=vbd-uuids --minimal | tr , ' '`\r\n        for i in $VBDS\r\n        do\r\n                $XE vbd-unplug uuid=${i}\r\n                $XE vbd-destroy uuid=${i}\r\n        done\r\n        echo \"Storage Detached from VM\"\r\n}\r\ncase \"$1\" in\r\n        attach) attach\r\n                ;;\r\n        detach) detach\r\n                ;;\r\n        *)      echo \"Usage: $0 [attach|detach]\"\r\n                exit 1\r\nesac<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<\/div>\n<p>Cr\u00e9dito :\u00a0<a href=\"http:\/\/run.tournament.org.il\/attach-usb-disks-to-xenserver-vm-guest\/\">http:\/\/run.tournament.org.il\/attach-usb-disks-to-xenserver-vm-guest\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a very nice script for Windows dealing with attaching XenServer USB disk to a guest. It can be found\u00a0here. This script has several problems, as I see it.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-179","post","type-post","status-publish","format-standard","hentry","category-virtualizacao"],"_links":{"self":[{"href":"http:\/\/binsfeld.com.br\/wp\/index.php?rest_route=\/wp\/v2\/posts\/179"}],"collection":[{"href":"http:\/\/binsfeld.com.br\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/binsfeld.com.br\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/binsfeld.com.br\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/binsfeld.com.br\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=179"}],"version-history":[{"count":3,"href":"http:\/\/binsfeld.com.br\/wp\/index.php?rest_route=\/wp\/v2\/posts\/179\/revisions"}],"predecessor-version":[{"id":182,"href":"http:\/\/binsfeld.com.br\/wp\/index.php?rest_route=\/wp\/v2\/posts\/179\/revisions\/182"}],"wp:attachment":[{"href":"http:\/\/binsfeld.com.br\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/binsfeld.com.br\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/binsfeld.com.br\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}