Jump to content
  • 0

Want to unmount / disconnect a disk from a guest via powershell


mwarr

Question

3 answers to this question

Recommended Posts

  • 0

Hi mwarr!

I am guessing you're referring to "detatching" a Virtual Disk from a VM.


This operation will remove the association between the Virtual Disk and the chosen VM, while preserving the disk and its data.

If the VBDs on the VDI you're trying to detach are currently attached you'll need to unplug them first (vbd-unplug on xe). Otherwise, you'll need to destroy all VBDs on the VDI.

Wrote a little script to give you an idea. It makes use of Invoke-XenVBD Unplug  and Remove-XenVBD?

Note that if you instead want to simply "deactivate" the disk, you must call Invoke-XenVBD Unplug without Remove-XenVBD. This will then allow you you "reactivate" the disk by calling Invoke-XenVBD Plug.

 

$vdiUuid = [System.guid]::New("VDI_UUID")
$vmUuid = [System.guid]::New("VM_UUID")

## Check input
$vdi = Get-XenVDI -Uuid $vdiUuid
if ($null -eq $vdi || $vdi.Locked) {
	Write-Output "VDI not valid"
	Exit
}

$vm = Get-XenVM -Uuid $vmUuid
if ($null -eq $vm) {
	Write-Output "VM not valid"
	Exit
}

$matchingVbd = $null

foreach ($vbdRef in $vm.VBDs) {
	$vbd = Get-XenVBD -Ref $vbdRef
	if ($null -eq $vbd) {
		Write-Output "VBD $($vbdRef) not valid"
		Exit
	}
	if ($vbd.VDI.opaque_ref -eq $vdi.opaque_ref) {
		$matchingVbd = $vbd
		break
	}
}

if ($null -eq $matchingVbd) {
	Write-Output "Could not find matching VBD!"
	Exit
}


## Detatch operation

Write-Output "Detatching VDI '$($vdi.name_label)'"
Write-Output ""

Write-Output "Attempting to destroy VBD '$($vbd.uuid)'"
if ($matchingVbd.currently_attached) {
	Write-Output "VBD is attached, we need to unplug it before we can destroy it"
	$allowedOperations = $matchingVbd.allowed_operations
	if ($allowedOperations -contains "unplug") {
		Write-Output "Unplugging '$($vbd.uuid)'"
		Invoke-XenVBD $matchingVbd -XenAction Unplug
		Write-Output "Unplugged '$($vbd.uuid)'"
	}
	else {
		Write-Output "Cannot unplug attached VBD!"
		Exit;
	}
}

Write-Output "Destroying '$($vbd.uuid)'"
Remove-XenVBD $matchingVbd
Write-Output "Destroyed '$($vbd.uuid)'"
Write-Output ""

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...