This blog shows you how to create a windows VM from a generalized image. This uses un-managed Azure disks.
For this example, I will be using resources deployed on Azure. i.e., generalize an Azure VM, create a image out of it and then create a new Azure VM using the image.
Below are the steps:
- Generalize the VM
- Capture a VM image from a generalized Azure VM, that we obtain from Step 1
- Create a VM from a generalized VHD image in a storage account, that we obtained from step 2
Part 1: Generalize the VM
- Remote Desktop to the Azure virtual machine
- **Important** Before running the “Sysprep.exe”. Delete the “unattend.xml” file from the “C:\Windows\Panther” folder. If you do not do this, you will encounter “OS Provision time out” exception while creating the VM from this image.
This is due to the fact that when an image is deployed the unattend.xml file must come from the ISO image that is attached to the Virtual Machine by Windows Azure as part of VM provisioning from an image.
- From the command prompt / powershell, change the directory to: “C:\Windows\System32\Sysprep”
- Run “Sysprep.exe”
- In the System Preparation Tool, select the option, “Enter System Out-of-Box Experience (OOBE)”. Select the “Generalize” option from the check-box.
- Select “Shutdown” from the drop down list, in Shutdown Options.
- Once the Sysprep process completes, the VM will shutdown.
- From the Azure portal, you can see the status of the VM as “Stopped (Shutdown)”. Use the below powershell cmdlet to fetch the VM status.
(Get-AzureRmVM -ResourceGroupName manjug_test -Name windowsmachine -Status).Statuses
Part 2: Capture a VM image from a generalized Azure Virtual Machine
- De-allocate the VM
Stop-AzureRmVM -ResourceGroupName manjug_test -Name windowsmachine
Confirm the status of the VM:
(Get-AzureRmVM -ResourceGroupName manjug_test -Name windowsmachine -Status).Statuses
- Set the status of the VM to “Generalized”
Set-AzureRmVm -ResourceGroupName manjug_test -Name windowsmachine -Generalized
Confirm the status of the VM:
(Get-AzureRmVM -ResourceGroupName manjug_test -Name windowsmachine -Status).Statuses
- Create the image by running the below command:
Save-AzureRmVMImage -ResourceGroupName manjug_test -Name windowsmachine ` -DestinationContainerName images -VHDNamePrefix windowsmachineimage ` -Path C:\Filename.json
-DestinationContainerName, is the container name where the image will be stored.
-VHDNamePrefix, is the prefix given to the image.
-Path, is the path of json file that contains the details of the image that gets created.You can get the URL of your image from the JSON file template. Go to the resources > storageProfile > osDisk > image > uri section for the complete path of your image. The URL of the image looks like:
https://<storageAccountName>.blob.core.windows.net/system/Microsoft.Compute/Images/<imagesContainer>/<templatePrefix-osDisk>.xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.vhd
You can also verify the URI in the portal. The image is copied to a container named system in your storage account.
Part 3: Create a VM from a generalized VHD image in a storage account
- Obtain the image uri, from the json file (Part 2, step 3). Or you can fetch this from the Azure portal.
From the portal:
From the JSON file:
- Set the VHD uri to a variable. Example:
- Create a Virtual Network
Create the subnet. The following sample creates a subnet named mySubnet in the resource group myResourceGroup with the address prefix of 10.0.0.0/24.
$rgName = “manjug_test”
$subnetName = “mySubnet”
$singleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix 10.0.0.0/24Create the virtual network. The following sample creates a virtual network named myVnet in the West US location with the address prefix of 10.0.0.0/16.
$location = “Southeast Asia”
$vnetName = “myVnet”
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName -Location $location `
-AddressPrefix 10.0.0.0/16 -Subnet $singleSubnet - Create a public IP address and network interface
Create a public IP address. This example creates a public IP address named myPip.
$ipName = “myPip”
$pip = New-AzureRmPublicIpAddress -Name $ipName -ResourceGroupName $rgName -Location $location `
-AllocationMethod DynamicCreate the NIC. This example creates a NIC named myNic.
$nicName = “myNic”
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $location `
-SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id - Create the network security group and an RDP rule
To be able to log in to your VM using RDP, you need to have a security rule that allows RDP access on port 3389
This example creates an NSG named myNsg that contains a rule called myRdpRule that allows RDP traffic over port 3389.
$nsgName = “myNsg”
$rdpRule = New-AzureRmNetworkSecurityRuleConfig -Name myRdpRule -Description “Allow RDP” `
-Access Allow -Protocol Tcp -Direction Inbound -Priority 110 `
-SourceAddressPrefix Internet -SourcePortRange * `
-DestinationAddressPrefix * -DestinationPortRange 3389$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $rgName -Location $location `
-Name $nsgName -SecurityRules $rdpRule - Create a variable for the virtual network
$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $rgName -Name $vnetName
- Create the Virtual Machine
The following PowerShell script shows how to set up the virtual machine configurations and use the uploaded VM image as the source for the new installation.
# Enter a new user name and password to use as the local administrator account
# for remotely accessing the VM.
$cred = Get-Credential# Name of the storage account where the VHD is located. This example sets the
# storage account name as “myStorageAccount”
$storageAccName = “manjugtestdisks”# Name of the virtual machine. This example sets the VM name as “myVM”.
$vmName = “winmachimage”# Size of the virtual machine. This example creates “Standard_D2_v2” sized VM.
# See the VM sizes documentation for more information:
# https://azure.microsoft.com/documentation/articles/virtual-machines-windows-sizes/
$vmSize = “Standard_D2_v2”# Computer name for the VM. This examples sets the computer name as “myComputer”.
$computerName = “winmachimage”# Name of the disk that holds the OS. This example sets the
# OS disk name as “myOsDisk”
$osDiskName = “myOsDisk”# Assign a SKU name. This example sets the SKU name as “Standard_LRS”
# Valid values for -SkuName are: Standard_LRS – locally redundant storage, Standard_ZRS – zone redundant
# storage, Standard_GRS – geo redundant storage, Standard_RAGRS – read access geo redundant storage,
# Premium_LRS – premium locally redundant storage.
$skuName = “Standard_LRS”# Get the storage account where the uploaded image is stored
$storageAcc = Get-AzureRmStorageAccount -ResourceGroupName $rgName -AccountName $storageAccName# Set the VM name and size
$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize#Set the Windows operating system configuration and add the NIC
$vm = Set-AzureRmVMOperatingSystem -VM $vmConfig -Windows -ComputerName $computerName `
-Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id# Create the OS disk URI
$osDiskUri = ‘{0}vhds/{1}-{2}.vhd’ `
-f $storageAcc.PrimaryEndpoints.Blob.ToString(), $vmName.ToLower(), $osDiskName# Configure the OS disk to be created from the existing VHD image (-CreateOption fromImage).
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $osDiskName -VhdUri $osDiskUri `
-CreateOption fromImage -SourceImageUri $imageURI -Windows# Create the new VM
New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vm - Verify that the Virtual Machine was created.
~ If this post helps at-least one person. The purpose is served. ~
Reblogged this on Cloud & Virtualization Complete Guide.
LikeLike