2012年9月28日

[Azure] There is currently a lease on the blob and no lease ID was specified in the request.

在使用Windows Azure 3 month Free trial到達免費限制之後, 系統會自動刪除已經發布的服務及VM, 之後當重新啟用帳號並想要建立VM時, 有時會發現VHD被指向不存在的VM並且無法刪除. 當嘗試刪除時會遇到這個錯誤.

準備工作

1. 到這裡下載並安裝Windows Azure SDK

2. 下載並安裝Windows Azure Power Shell

3. 下載並安裝CloudXplorer

備份VHD

1. 啟動Cloud Xplorer並新增Storage Account

image

2. 瀏覽到Storage Accoung, 打開vhds container後, 右鍵點選需要備份的VHD檔案, 點Rename, 並給予檔案一個新的名稱

3. 因為這個VHD的lease未被刪除, 所以會跳出一個錯誤訊息, 請按Yes繼續

image

4. 這邊會看到新的檔案被建立

image

5. 回到Azure Management Portal, 選Virtual Machines, Disk點選下方Create Disk

6. 在這裡填上正確的資料, VHD URL就是剛剛我們建立出來的新的VHD的位置

image

刪除Lease

1. 以系統管理員身分執行Windows Azure Powershell

2. 在Powershell中輸入cd C:\temp以切換目前工作目錄到C:\temp下

3. 在Powershell中執行下列命令, 並按Y確定

set-executionpolicy remotesigned

image

4. 在Powershell中執行

get-azurepublishsettingsfile

這會打開瀏覽器下載publish setting, 請將檔案儲存到C:\Temp以避免後續產生權限問題

5. 如果看到以下這個錯誤訊息, 請到舊版Management Portal刪除不必要的憑證

You have reached your quota of 10 management certificates and cannot add any more. Visit the Management Portal to delete any unneeded certificates.

image

8. 在Powershell中執行

Import-AzurePublishSettingsFile

並輸入剛剛下載的檔案位置

9. 在Powershell中執行

Get-AzureSubscription並複製您的Subscription Name

image

10. 在Powershell中執行

Set-AzureSubscription –DefaultSubscription [剛剛複製的Subscription Name]

11. 在Powershell中執行這一段powershell script

並輸入您的VHD的URL

如果沒有看到錯誤訊息, 表示lease已經被刪除, 可以回到Azure Management Portal刪除這個Blob

Param([string]$Uri = $(Read-Host -prompt "Please specify a blob URL"))

$ProgressPreference = 'SilentlyContinue'


echo "Processing $Uri"

echo "Reading storage account information..."
$acct = Get-AzureStorageAccount | ? { (new-object System.Uri($_.Endpoints[0])).Host -eq (new-object System.Uri($Uri)).Host }
if(-not $acct) {
write-host "The supplied URL does not appear to correspond to a storage account associated with the current subscription." -foregroundcolor "red"
break
}

$acctKey = Get-AzureStorageKey ($acct.StorageAccountName)
$creds = "DefaultEndpointsProtocol=http;AccountName=$($acctKey.StorageAccountName);AccountKey=$($acctKey.Primary)"
$acctobj = [Microsoft.WindowsAzure.CloudStorageAccount]::Parse($creds)
$uri = $acctobj.Credentials.TransformUri($uri)
echo "Confirmed - storage account '$($acct.StorageAccountName)'."


$blobclient = New-Object Microsoft.WindowsAzure.StorageClient.CloudBlobClient($acctobj.BlobEndpoint, $acctobj.Credentials)
$blobclient.Timeout = (New-TimeSpan -Minutes 1)
$blob = New-Object Microsoft.WindowsAzure.StorageClient.CloudPageBlob($uri, $blobclient)


echo "Checking whether the blob is currently registered as a disk or image..."
$disk = Get-AzureDisk | ? { (new-object System.Uri($_.MediaLink)) -eq $blob.Uri }
if($disk) {
write-host "The blob is still registered as a disk with name '$($disk.DiskName)'. Please delete the disk first." -foregroundcolor "red"
break
}
$image = Get-AzureVMImage | ? { $_.MediaLink -eq $blob.Uri.AbsoluteUri }
if($image) {
write-host "The blob is still registered as an OS image with name '$($image.ImageName)'. Please delete the OS image first." -foregroundcolor "red"
break
}
echo "Confirmed - the blob is not in use by the Windows Azure platform."




echo "Inspecting the blob's lease status..."
try {
$blob.FetchAttributes()
} catch [System.Management.Automation.MethodInvocationException] {
write-host $_.Exception.InnerException.Message -foregroundcolor "red"
break
}

echo "Current lease status: $($blob.Properties.LeaseStatus)"

if($blob.Properties.LeaseStatus -ne [Microsoft.WindowsAzure.StorageClient.LeaseStatus]::Locked) {
write-host "Success - the blob is unlocked." -foregroundcolor "green"
break
}



echo "Unlocking the blob..."
$request = [Microsoft.WindowsAzure.StorageClient.Protocol.BlobRequest]::Lease($uri, 0, [Microsoft.WindowsAzure.StorageClient.Protocol.LeaseAction]::Break, $null)
$request.Timeout = $blobclient.Timeout.TotalMilliseconds
$acctobj.Credentials.SignRequest($request)
try {
$response = $request.GetResponse()
$response.Close()
}
catch {
write-host "The blob could not be unlocked:" -foregroundcolor "red"
write-host $_.Exception.InnerException.Message -foregroundcolor "red"
break
}

$blob.FetchAttributes()
echo "Current lease status: $($blob.Properties.LeaseStatus)"

沒有留言:

Blog Archive

About Me