Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create VM from existing VHD: Preview portal

Does anyone now how to create a VM from an existing VHD in the new azure portal ?

I can find lots of info on how to do this in manage.windowsazure.com, but nothing on this functionality in portal.azure.com.

like image 594
naylormat Avatar asked Feb 10 '16 22:02

naylormat


People also ask

How do I create a VM with existing VHD?

To create a VMUse the new Virtual Machine wizard to choose a location, name, and the base memory size. On the Connect Virtual Hard Disk page of the wizard, select Use existing virtual hard disk and choose your previously converted VHD file. Complete the wizard and start the virtual machine.

How do I make an Azure VM from a VHD?

You can create an Azure VM from an on-premises VHD by uploading the on-premises VHD and attaching it to a new VM. You use PowerShell or another tool to upload the VHD to a storage account, and then you create a managed disk from the VHD.

Does Azure support both VHDX and VHD?

Before you upload a Windows virtual machine (VM) from on-premises to Azure, you must prepare the virtual hard disk (VHD or VHDX). Azure supports both generation 1 and generation 2 VMs that are in VHD file format and that have a fixed-size disk.


2 Answers

It can't be done literally in the portal. You will have to use powershell.

  1. Create a storageaccount. For example in the portal.

  2. Upload the VHD to azure. To do this, run the following line in powershell after logging in with Login-AzureRmAccount (change the parameters between <> and the path to the vhd on your harddisk):

    Add-AzurermVhd -Destination "https://<StorageAccountName>.blob.core.windows.net/<containerName>/<vhdname>.vhd" -LocalFilePath "D:\Virtual Machines\myharddisk.vhd" -ResourceGroupName "<ResourceGroupName" -Overwrite
    
  3. Create an ARM template. Multiple possiblities what you can do. For example choose a template from the Azure Quickstart templates, for example 101

What I have done is:

  • Created a new project in Visual Studio 2015.
  • Choose the following project: Cloud->Azure Resource Group
  • Choose the following template: Windows Virtual Machine

  • Changed some parameters and removed all stuff that is not necessary. What it does now is: Create a Windows Virtual Machine using the uploaded vhd as harddisk. It's using a parameters json file now, and also some variables have to be set in the WindowsVirtualMachine.json This could be refactored ofcourse. but for now it will do what's needed.

For this sample you have to have the following directory structure (just like Visual Studio creates it)

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

Deploy-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. Execute the powershell script Open up a Powershell command and execute the ps1 script. You only have to pass the location where you want the vm to be created like: (you should already be logged in with Login-AzureRmAccount)

    Before running change the parameters in both json files!
    .\Deploy-AzureResourceGroup.ps1 "West Europe"

The logging should tell you that the VM is created successfully.

like image 98
Pascal Naber Avatar answered Oct 30 '22 14:10

Pascal Naber


Today (Oct 2016) it still can´t be done in the new portal.

But for completeness: You can do it in the old portal (https://manage.windowsazure.com):

Click New - Compute - Virtual Machine - From Gallery. On the left either select MY IMAGES or MY DISKS and select the VHD you want to use. Follow the instructions as usual.

like image 44
huha Avatar answered Oct 30 '22 15:10

huha