【问题标题】:Create VM from existing VHD: Preview portal从现有 VHD 创建 VM:预览门户
【发布时间】:2016-10-05 17:55:00
【问题描述】:

现在有人如何从新的 Azure 门户中的现有 VHD 创建 VM 吗?

我可以在 manage.windowsazure.com 中找到很多关于如何执行此操作的信息,但在 portal.azure.com 中没有关于此功能的信息。

【问题讨论】:

    标签: azure azure-virtual-machine vhd


    【解决方案1】:

    它不能在门户中按字面意思完成。您将不得不使用 powershell。

    1. 创建一个存储帐户。例如在门户中。

    2. 将 VHD 上传到 Azure。为此,请在使用 Login-AzureRmAccount 登录后在 powershell 中运行以下行(更改 和硬盘上 vhd 的路径之间的参数):

      Add-AzurermVhd -Destination "https://<StorageAccountName>.blob.core.windows.net/<containerName>/<vhdname>.vhd" -LocalFilePath "D:\Virtual Machines\myharddisk.vhd" -ResourceGroupName "<ResourceGroupName" -Overwrite
      
    3. 创建 ARM 模板。 多种可能性,您可以做什么。 例如从Azure Quickstart templates 中选择一个模板,例如101

    我所做的是:

    • 在 Visual Studio 2015 中创建了一个新项目。
    • 选择以下项目:Cloud->Azure Resource Group
    • 选择以下模板:Windows 虚拟机

    • 更改了一些参数并删除了所有不必要的内容。 它现在所做的是:使用上传的 vhd 作为硬盘创建一个 Windows 虚拟机。 它现在使用参数 json 文件,并且必须在 WindowsVirtualMachine.json 中设置一些变量 这当然可以重构。但现在它会做需要做的事情。

    对于此示例,您必须具有以下目录结构(就像 Visual Studio 创建它一样)

    ProjectDirectory/Scripts/Deploy-AzureResourceGroup.ps1
    ProjectDirectory/Templates/WindowsVirtualMachine.json
    ProjectDirectory/Templates/WindowsVirtualMachine.parameters.json
    

    部署-AzureResourceGroup.ps1

    #Requires -Version 3.0
    #Requires -Module AzureRM.Resources
    #Requires -Module Azure.Storage
    
    Param(
        [string] [Parameter(Mandatory=$true)] $ResourceGroupLocation,
        [string] $ResourceGroupName = 'CreateImage',    
        [string] $TemplateFile = '..\Templates\WindowsVirtualMachine.json',
        [string] $TemplateParametersFile = '..\Templates\WindowsVirtualMachine.parameters.json'
    )
    
    Import-Module Azure -ErrorAction SilentlyContinue
    
    try {
        [Microsoft.Azure.Common.Authentication.AzureSession]::ClientFactory.AddUserAgent("VSAzureTools-$UI$($host.name)".replace(" ","_"), "2.8")
    } catch { }
    
    Set-StrictMode -Version 3
    
    $OptionalParameters = New-Object -TypeName Hashtable
    $TemplateFile = [System.IO.Path]::Combine($PSScriptRoot, $TemplateFile)
    $TemplateParametersFile = [System.IO.Path]::Combine($PSScriptRoot, $TemplateParametersFile)
    
    
    # Create or update the resource group using the specified template file and template parameters file
    New-AzureRmResourceGroup -Name $ResourceGroupName -Location $ResourceGroupLocation -Verbose -Force -ErrorAction Stop 
    
    New-AzureRmResourceGroupDeployment -Name ((Get-ChildItem $TemplateFile).BaseName + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')) `
                                       -ResourceGroupName $ResourceGroupName `
                                       -TemplateFile $TemplateFile `
                                       -TemplateParameterFile $TemplateParametersFile `
                                       @OptionalParameters `
                                       -Force -Verbose
    

    WindowsVirtualMachine.json

    {
      "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {    
        "dnsNameForPublicIP": {
          "type": "string",
          "minLength": 1,
          "metadata": {
            "description": "Globally unique DNS Name for the Public IP used to access the Virtual Machine."
          }
        }   
      },
      "variables": {
        "OSDiskName": "<vhdNameWithoutExtension>",
        "vhdStorageContainerName": "<containerName>",
        "storageAccountName": "<StorageAccountName>",
        "nicName": "myVMNic",
        "addressPrefix": "10.0.0.0/16",
        "subnetName": "Subnet",
        "subnetPrefix": "10.0.0.0/24",
        "vhdStorageType": "Standard_LRS",
        "publicIPAddressName": "myPublicIP",
        "publicIPAddressType": "Dynamic",
        "vhdStorageName": "[concat('vhdstorage', uniqueString(resourceGroup().id))]",
        "vmName": "MyWindowsVM",
        "vmSize": "Standard_A2",
        "virtualNetworkName": "MyVNET",
        "vnetId": "[resourceId('Microsoft.Network/virtualNetworks', variables('virtualNetworkName'))]",
        "subnetRef": "[concat(variables('vnetId'), '/subnets/', variables('subnetName'))]"
      },
      "resources": [
        {
          "type": "Microsoft.Storage/storageAccounts",
          "name": "[variables('vhdStorageName')]",
          "apiVersion": "2015-06-15",
          "location": "[resourceGroup().location]",
          "tags": {
            "displayName": "StorageAccount"
          },
          "properties": {
            "accountType": "[variables('vhdStorageType')]"
          }
        },
        {
          "apiVersion": "2015-06-15",
          "type": "Microsoft.Network/publicIPAddresses",
          "name": "[variables('publicIPAddressName')]",
          "location": "[resourceGroup().location]",
          "tags": {
            "displayName": "PublicIPAddress"
          },
          "properties": {
            "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
            "dnsSettings": {
              "domainNameLabel": "[parameters('dnsNameForPublicIP')]"
            }
          }
        },
        {
          "apiVersion": "2015-06-15",
          "type": "Microsoft.Network/virtualNetworks",
          "name": "[variables('virtualNetworkName')]",
          "location": "[resourceGroup().location]",
          "tags": {
            "displayName": "VirtualNetwork"
          },
          "properties": {
            "addressSpace": {
              "addressPrefixes": [
                "[variables('addressPrefix')]"
              ]
            },
            "subnets": [
              {
                "name": "[variables('subnetName')]",
                "properties": {
                  "addressPrefix": "[variables('subnetPrefix')]"
                }
              }
            ]
          }
        },
        {
          "apiVersion": "2015-06-15",
          "type": "Microsoft.Network/networkInterfaces",
          "name": "[variables('nicName')]",
          "location": "[resourceGroup().location]",
          "tags": {
            "displayName": "NetworkInterface"
          },
          "dependsOn": [
            "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]",
            "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
          ],
          "properties": {
            "ipConfigurations": [
              {
                "name": "ipconfig1",
                "properties": {
                  "privateIPAllocationMethod": "Dynamic",
                  "publicIPAddress": {
                    "id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]"
                  },
                  "subnet": {
                    "id": "[variables('subnetRef')]"
                  }
                }
              }
            ]
          }
        },
        {
          "apiVersion": "2015-06-15",
          "type": "Microsoft.Compute/virtualMachines",
          "name": "[variables('vmName')]",
          "location": "[resourceGroup().location]",
          "tags": {
            "displayName": "VirtualMachine"
          },
          "dependsOn": [
            "[concat('Microsoft.Storage/storageAccounts/', variables('vhdStorageName'))]",
            "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
          ],
          "properties": {
            "hardwareProfile": {
              "vmSize": "[variables('vmSize')]"
            },       
            "storageProfile": {         
              "osDisk": {
                "name": "osdisk",
                "osType": "Windows",
                "vhd": {
                  "uri": "[concat('http://', variables('storageAccountName'), '.blob.core.windows.net/', variables('vhdStorageContainerName'), '/', variables('OSDiskName'), '.vhd')]"
                },
                "caching": "ReadWrite",
                "createOption": "Attach"
              }
            },
            "networkProfile": {
              "networkInterfaces": [
                {
                  "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]"
                }
              ]
            }   
          }      
        }
      ]
    }
    

    WindowsVirtualMachine.parameters.json

    {
        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {       
            "dnsNameForPublicIP": {
                "value": "<someUniqueNameForYourDnsName>"
            }
        }
    }
    
    1. 执行powershell脚本 打开 Powershell 命令并执行 ps1 脚本。您只需传递您希望创建 vm 的位置,例如:(您应该已经使用 Login-AzureRmAccount 登录)

      在运行之前更改两个 json 文件中的参数!
      .\Deploy-AzureResourceGroup.ps1 "West Europe"

    日志记录应该告诉您虚拟机已成功创建。

    【讨论】:

    • 天哪,在visual studio中创建一个项目来在azure中部署一个vm,你真的有很多时间在你手上
    【解决方案2】:

    今天(2016 年 10 月)仍然无法在新门户中完成。

    但为了完整起见:您可以在旧门户中进行操作 (https://manage.windowsazure.com):

    单击新建 - 计算 - 虚拟机 - 从库。 在左侧选择我的图像或我的磁盘,然后选择要使用的 VHD。 照常按照说明进行操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多