{"componentChunkName":"component---src-templates-page-js","path":"/2012/10/10/Windows-Azure-錯誤-當使用現有的Disk來建立新VM時產生A-lease-conflict-occurred-with-the-blob/","result":{"data":{"markdownRemark":{"frontmatter":{"title":"Windows Azure - 錯誤!! 當使用現有的Disk來建立新VM時產生A lease conflict occurred with the blob","date":"10 October 2012","author":"Sky Chang","excerpt":null,"tags":["Azure"],"coverImage":null},"id":"943341ce-52cf-55c5-84a3-aca317c8008e","html":"<p>其實這篇，是昨天在將做VM轉Image的練習，當作完刪除Disk後，準備要刪除Blob裡面的VHD，卻出現錯誤，後來查了一下<a href=\"http://social.msdn.microsoft.com/Forums/pl-PL/WAVirtualMachinesforWindows/thread/cc9bacfc-2c11-4010-b535-f1b0a141d317\">MSDN</a>，後來有找到一篇類似的解決方式 (雖然案例和我的狀況完全不同)，但感覺滿好玩的，就順便邊做邊翻譯。</p>\n<p>根據原文，主要是在寫當，我們要使用VHD來創建不同的VM時，會收到這個錯誤</p>\n<blockquote>\n<p>A lease conflict occurred with the blob &#x3C;blob url> </p>\n</blockquote>\n<p>當然，一般人應該不會無聊到把一個VHD拿去建立很多個VM，所以根據官方的說法，這個問題是出現在，當合約超過額度，時間等等，而造成系統自動把服務停止；當我們恢復合約( 繼續繳錢 )後，要使用之前的VHD來重建VM，卻建立不起來的情況。</p>\n<p>所以官方，就使用了傳說中的命令列模式!! ( 老實說，真不喜歡用… )，也就是使用Windows Azure PowerShell…來處理，根據原文文件指出，可以使用 <a href=\"http://msdn.microsoft.com/en-us/library/windowsazure/ee691972.aspx\">Lease Blob API</a> 來把契約關連給斷開，至於要怎麼用呢?，就是使用Windows Azure PowerShell來載入Microsoft.WindowsAzure.StorageClient.dll，並使用<a href=\"http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storageclient.protocol.leaseaction\">LeaseAction Enumeration</a>. </p>\n<p>神奇吧!?沒錯，我也是覺得很神奇，要不是找到官方這篇文章XDD，誰知道可以這樣搞呢~~</p>\n<h4>開始!!</h4>\n<p>我們先要<a href=\"https://www.windowsazure.com/en-us/manage/downloads/\">下載Windows Azure管理工具</a>，因為小弟是Windows環境，就使用Windows的管理工具。</p>\n<p><a href=\"http://lh5.ggpht.com/-FuL40gP-0Ew/UHRKyJ1IL4I/AAAAAAAAFB4/W-0mf54DN5Y/s1600-h/image%25255B16%25255D.png\"><img src=\"http://lh6.ggpht.com/-Kl2q7GlARZk/UHRKzcDw-jI/AAAAAAAAFCA/J7m6JT-Hl44/image_thumb%25255B8%25255D.png?imgmax=800\" alt=\"image\" title=\"image\"></a></p>\n<p>接下來，安裝程式會呼叫Web Platform Installer 4.0，當然，也沒甚麼好猶豫的…反正也只能安裝了= =…</p>\n<p><a href=\"http://lh3.ggpht.com/-XGHERe8xs5Q/UHRK0XPpa-I/AAAAAAAAFCE/fpcd152unUc/s1600-h/image%25255B21%25255D.png\"><img src=\"http://lh5.ggpht.com/-yZyLdLCRn5w/UHRK1a1LqcI/AAAAAAAAFCM/-5EL4iz53QE/image_thumb%25255B11%25255D.png?imgmax=800\" alt=\"image\" title=\"image\"></a></p>\n<p>安裝完後，就可以找到Windows Azure PowerShell。( 找不到的人，可以利用搜尋功能，搜尋Windows Azure PowerShell，通常裝完後，就可以從開始那邊找到。 )，另外，記得用administrator權限開啟!!</p>\n<p><a href=\"http://lh4.ggpht.com/-U0xOLlG-Hbg/UHRK2QQONVI/AAAAAAAAFCU/WoHEJQtPrII/s1600-h/image%25255B26%25255D.png\"><img src=\"http://lh6.ggpht.com/-9YtLATtSoKA/UHRK3p8_eRI/AAAAAAAAFCg/K6W0t_WuzqY/image_thumb%25255B14%25255D.png?imgmax=800\" alt=\"image\" title=\"image\"></a></p>\n<p>接下來，在管理工具裡面輸入<strong>Get-AzurePublishSettingsFile</strong>，他就會自動地開啟瀏覽器，並且提供publishsetting檔案給我們下載，我們當然就是下載啦!!( 建議存到預設的下載目錄，這樣等下就不需要再打路徑了。 )</p>\n<p><a href=\"http://lh5.ggpht.com/-J5AfDxtGAlY/UHRK4oteEqI/AAAAAAAAFCo/m61QRrJnBVA/s1600-h/image%25255B32%25255D.png\"><img src=\"http://lh4.ggpht.com/-QnJxyDIWc5Y/UHRK6GAnqvI/AAAAAAAAFCw/CDC9NB2rCbc/image_thumb%25255B18%25255D.png?imgmax=800\" alt=\"image\" title=\"image\"></a></p>\n<p>那剛剛下載的Publishsetting是做甚麼的呢，當然就是給Windows Azure PowerShell用的，如果沒有這個檔案，Windows Azure PowerShell自然就沒有權限…，所以下一步，我們要把這個檔案給Windows Azure PowerShell來使用；我們要在Windows Azure PowerShell裡面下這個指令*<em>Import-AzurePublishSettingsfile $env:userprofile\\downloads\\</em>.publishsettings **很簡單吧!!所以剛剛才建議大家直接丟到下載目錄，這樣這時候就可以直接複製上面那個指令到Windows Azure PowserShell裡面了，如果不是放到下載目錄，而是放在其他目錄，則後面的路徑，就要記得自己改喔!!</p>\n<p><a href=\"http://lh4.ggpht.com/-3_3_ab6Wl8A/UHRK7ZgC-8I/AAAAAAAAFC4/dZPwTfuhZtI/s1600-h/image%25255B36%25255D.png\"><img src=\"http://lh4.ggpht.com/-4cb1YEI-Mww/UHRK8bdOCFI/AAAAAAAAFC8/wazGWNz-8DE/image_thumb%25255B20%25255D.png?imgmax=800\" alt=\"image\" title=\"image\"></a></p>\n<p>接下來，我們要把下面這烙烙長的程式碼貼到記事本上面去!! ( 就不要問小弟我內容了= =|| )，並且取名為BreakLease.ps1 ( 副檔名是ps1阿!不是txt喔，所以要用另存新檔的方式喔!! )</p>\n<pre class=\"brush: csharp;\">Param([string]$Uri = $(Read-Host -prompt \"Please specify a blob URL\"))\n\n$ProgressPreference = 'SilentlyContinue'\n\necho \"Processing $Uri\"\n\necho \"Reading storage account information...\"\n$acct = Get-AzureStorageAccount | ? { (new-object System.Uri($_.Endpoints[0])).Host -eq (new-object System.Uri($Uri)).Host }\nif(-not $acct) {\n    write-host \"The supplied URL does not appear to correspond to a storage account associated with the current subscription.\" -foregroundcolor \"red\"\n    break\n}\n\n$acctKey = Get-AzureStorageKey ($acct.StorageAccountName)\n$creds = \"DefaultEndpointsProtocol=http;AccountName=$($acctKey.StorageAccountName);AccountKey=$($acctKey.Primary)\"\n$acctobj = [Microsoft.WindowsAzure.CloudStorageAccount]::Parse($creds)\n$uri = $acctobj.Credentials.TransformUri($uri)\necho \"Confirmed - storage account '$($acct.StorageAccountName)'.\"\n\n$blobclient = New-Object Microsoft.WindowsAzure.StorageClient.CloudBlobClient($acctobj.BlobEndpoint, $acctobj.Credentials)\n$blobclient.Timeout = (New-TimeSpan -Minutes 1)\n$blob = New-Object Microsoft.WindowsAzure.StorageClient.CloudPageBlob($uri, $blobclient)\n\necho \"Checking whether the blob is currently registered as a disk or image...\"\n$disk = Get-AzureDisk | ? { (new-object System.Uri($_.MediaLink)) -eq $blob.Uri }\nif($disk) {\n    write-host \"The blob is still registered as a disk with name '$($disk.DiskName)'.  Please delete the disk first.\" -foregroundcolor \"red\"\n    break\n}\n$image = Get-AzureVMImage | ? { $_.MediaLink -eq $blob.Uri.AbsoluteUri }\nif($image) {\n    write-host \"The blob is still registered as an OS image with name '$($image.ImageName)'.  Please delete the OS image first.\" -foregroundcolor \"red\"\n    break\n}\necho \"Confirmed - the blob is not in use by the Windows Azure platform.\"\n\necho \"Inspecting the blob's lease status...\"\ntry {\n    $blob.FetchAttributes()\n} catch [System.Management.Automation.MethodInvocationException] {\n    write-host $_.Exception.InnerException.Message -foregroundcolor \"red\"\n    break\n}\n\necho \"Current lease status: $($blob.Properties.LeaseStatus)\"\n\nif($blob.Properties.LeaseStatus -ne [Microsoft.WindowsAzure.StorageClient.LeaseStatus]::Locked) {\n    write-host \"Success - the blob is unlocked.\" -foregroundcolor \"green\"\n    break\n}\n\necho \"Unlocking the blob...\"\n$request = [Microsoft.WindowsAzure.StorageClient.Protocol.BlobRequest]::Lease($uri, 0, [Microsoft.WindowsAzure.StorageClient.Protocol.LeaseAction]::Break, $null)\n$request.Timeout = $blobclient.Timeout.TotalMilliseconds\n$acctobj.Credentials.SignRequest($request)\ntry {\n    $response = $request.GetResponse()\n    $response.Close()\n}\ncatch {\n    write-host \"The blob could not be unlocked:\" -foregroundcolor \"red\"\n    write-host $_.Exception.InnerException.Message -foregroundcolor \"red\"\n    break\n}\n\n$blob.FetchAttributes()\necho \"Current lease status: $($blob.Properties.LeaseStatus)\"\n\nwrite-host \"Success - the blob is unlocked.\" -foregroundcolor \"green\"\n</pre>\n<p>如下圖，存完後的檔案。</p>\n<p><a href=\"http://lh3.ggpht.com/-1ILJq7lcYbw/UHRK9UoZg-I/AAAAAAAAFDI/2ltSSyBEbI0/s1600-h/image%25255B41%25255D.png\"><img src=\"http://lh6.ggpht.com/-YTkhy-Pl_ww/UHRK-kgjSgI/AAAAAAAAFDQ/3S5pSvxKNJM/image_thumb%25255B23%25255D.png?imgmax=800\" alt=\"image\" title=\"image\"></a></p>\n<p>接下來，因為要執行的shell，會牽涉到安全性，所以我們要先把Windows Azure PowserShell的安全性原則解除，輸入<strong>Set-ExecutionPolicy unrestricted</strong>，然後按下Y。</p>\n<p><a href=\"http://lh6.ggpht.com/-mXAiv-qf9e0/UHRK_niPWmI/AAAAAAAAFDY/Hx6p3Y8XxOg/s1600-h/image%25255B45%25255D.png\"><img src=\"http://lh5.ggpht.com/-AzXrrkCCUzI/UHRLA92pAfI/AAAAAAAAFDg/u4mB7wUTGKg/image_thumb%25255B25%25255D.png?imgmax=800\" alt=\"image\" title=\"image\"></a></p>\n<p>接下來，我們就要Run剛剛建立的Shell之後，就會要求我們填入一些資訊，第一個要填的是Blob的URL，我們可以回到Windows Azure的管理介面，Blob那邊找到 ( 也就是要刪除的那頁 )，我們把URL全部框起來，利用左鍵，並按下複製，其實就可以將全部的URL複製下來了喔!!如下圖，貼上去就可以了。</p>\n<p><a href=\"http://lh5.ggpht.com/-kNEbUtbUWoU/UHRLCIDMcgI/AAAAAAAAFDo/yFIVtwh7hDI/s1600-h/image%25255B49%25255D.png\"><img src=\"http://lh3.ggpht.com/-zd5DbyTh93w/UHRLDUJN9HI/AAAAAAAAFDw/tJOO243csj0/image_thumb%25255B27%25255D.png?imgmax=800\" alt=\"image\" title=\"image\"></a></p>\n<p>然後根據官方文件，就會吐出這些東西。</p>\n<p>BreakLease.ps1 -Uri '<a href=\"http://clstorage.blob.core.windows.net/vhds/testvm1-testvm1-2012-06-26.vhd\">http://clstorage.blob.core.windows.net/vhds/testvm1-testvm1-2012-06-26.vhd</a>'\nProcessing <a href=\"http://clstorage.blob.core.windows.net/vhds/testvm1-testvm1-2012-06-26.vhd\">http://clstorage.blob.core.windows.net/vhds/testvm1-testvm1-2012-06-26.vhd</a>\nReading storage account information...\nConfirmed - storage account 'clstorage'.\nChecking whether the blob is currently registered as a disk or image...\nConfirmed - the blob is not in use by the Windows Azure platform.\nInspecting the blob's lease status...\nCurrent lease status: Locked\nUnlocking the blob...\nCurrent lease status: Unlocked\nSuccess - the blob is unlocked.</p>\n<p>到這邊，就算完成了!!</p>\n<h4>後記</h4>\n<p>而官方也提供了另外一個方法，是利用<a href=\"http://clumsyleaf.com/products/cloudxplorer\">CloudXplorer</a>工具，利用此工具的Rename功能來複製VHD ( 沒錯，就是Rename功能- -.. )，然後再用新的VHD來建立Disk；其實這個方法也滿簡單的，至於要用哪個做法就看看官如何選擇啦~~</p>\n<h4>參考網址</h4>\n<ul>\n<li><a href=\"http://social.msdn.microsoft.com/Forums/pl-PL/WAVirtualMachinesforWindows/thread/cc9bacfc-2c11-4010-b535-f1b0a141d317\" title=\"http://social.msdn.microsoft.com/Forums/pl-PL/WAVirtualMachinesforWindows/thread/cc9bacfc-2c11-4010-b535-f1b0a141d317\">http://social.msdn.microsoft.com/Forums/pl-PL/WAVirtualMachinesforWindows/thread/cc9bacfc-2c11-4010-b535-f1b0a141d317</a></li>\n</ul>","excerpt":"其實這篇，是昨天在將做VM轉Image的練習，當作完刪除Disk後，準備要刪除Blob裡面的VHD，卻出現錯誤，後來查了一下MSDN，後來有找到一篇類似的解決方式 (雖然案例和我的狀況完全不同)，但感覺滿好玩的，就順便邊做邊翻譯。 根據原文，主要是在寫當，我們要使用VHD…"}},"pageContext":{"type":"posts","next":{"frontmatter":{"path":null,"title":"Windows Azure - 刪除Windows Azure上的VM (10月新功能)","tags":["Azure"]},"fields":{"slug":"/2012/10/09/Windows-Azure-刪除Windows-Azure上的VM-10月新功能/"},"fileAbsolutePath":"/home/runner/work/blog.sanc.idv.tw/blog.sanc.idv.tw/src/posts/blog.sanc.idv.tw/Windows-Azure-刪除Windows-Azure上的VM-10月新功能.md"},"previous":{"frontmatter":{"path":null,"title":"Windows Azure - 刪除VHD時，產生There is currently a lease on the blob and no lease ID was specified in the request 錯誤","tags":["Azure"]},"fields":{"slug":"/2012/10/10/Windows-Azure-刪除VHD時，產生There-is-currently-a-lease-on-the-blob-and-no-lease-ID-was-specified-in-the-request-錯誤/"},"fileAbsolutePath":"/home/runner/work/blog.sanc.idv.tw/blog.sanc.idv.tw/src/posts/blog.sanc.idv.tw/Windows-Azure-刪除VHD時，產生There-is-currently-a-lease-on-the-blob-and-no-lease-ID-was-specified-in-the-request-錯誤.md"}}}}