Pre-Requisites

Install Azure PowerShell Module or Azure CLI

In order to create a custom image using this guide, you will need to have either the Azure PowerShell Module or Azure CLI tools available on your workstation.

Login to Azure Account

Once the proper Azure tools are installed, you need to set them up to work with your Azure Resource Manager subscription:

  1. Login to your Azure account and display the available subscriptions:

    Login-AzureRmAccount
    Get-AzureRmSubscription
    
    azure config mode arm
    azure login
    azure account list
    
  2. Select the Azure Subscription we will be working with:

    Select-AzureRmSubscription -SubscriptionName <Subscription Name>
    
    azure account set <Name>
    

Image Creation

Windows

  1. Launch a VM using the base image you wish to customize. At this stage you must decide if you wish to use Managed Disks or not.
  2. Connect to the operational VM and make any necessary modifications:

    • Install RightLink 10

      $wc = New-Object System.Net.Webclient
      $wc.DownloadFile("https://rightlink.rightscale.com/rll/10/rightlink.install.ps1", `
          "$pwd\rightlink.install.ps1")
      Powershell -ExecutionPolicy Unrestricted -File rightlink.install.ps1
      
    • Install updates

    • Install applications

    • Etc...

  3. From the VM: Prepare the VM for image capture by running SYSPREP from an elevated command prompt: C:\windows\system32\sysprep\sysprep.exe /oobe /generalize /shutdown

  4. Wait for the VM to be in a Stopped state...

  5. From your Workstation: Deallocate the VMs resources:

    Stop-AzureRmVM -Name <VM Name> -ResourceGroupName <Resource Group Name> -Force
    
    azure vm deallocate --resource-group <Resource Group Name> --name <VM Name>
    
  6. From your Workstation: Generalize the VM:

    Set-AzureRmVM -Name <VM Name> -ResourceGroupName <Resource Group Name> -Generalized
    
    azure vm generalize --resource-group <Resource Group Name> --name <VM Name>
    
  7. From your Workstation: Capture the image:
    Managed Image:

    $vm = Get-AzureRmVM -Name <VM Name> -ResourceGroupName <Resource Group Name>
    $image = New-AzureRmImageConfig -Location <Region> -SourceVirtualMachineId $vm.ID 
    New-AzureRmImage -Image $image -ImageName <Name of Image> -ResourceGroupName <Resource Group Name>
    
    az image create --resource-group <Resource Group Name> --name <Name of Image> --source <VM Name>
    

    Unmanaged Image:

    Save-AzureRmVMImage -VMName <VM Name> -ResourceGroupName <Resource Group Name> -DestinationContainerName "vhds" -VHDNamePrefix <choose unique prefix string>
    
    azure vm capture --resource-group <Resource Group Name> --name <VM Name> --vhd-name-prefix <choose unique prefix string>
    

    The unmanaged image will be captured as a virtual hard drive (VHD), with the prefix you defined and in the same Storage Account as the source VM, at the following path:
    <Storage Account>/system/Microsoft.Compute/Images/vhds/

  8. The source VM is no longer usable once it has been sysprepped and marked as generalized. Run the following command once the image has been successfully captured to delete the source VM:

    Remove-AzureRmVM -Name <VM Name> -ResourceGroupName <Resource Group Name> -Force
    
    azure vm delete --resource-group <Resource Group Name> --name <VM Name> --quiet
    
  9. Wait for RightScale Cloud Management to discover the newly captured image. This takes approximately 5-10 minutes.

  10. Once RightScale Cloud Management has discovered the image, you can add it to an existing MultiCloud Image or create a new MultiCloud Image.

Linux

  1. Launch a VM using the base image you wish to customize. At this stage you must decide if you wish to use Managed Disks or not.
  2. SSH to the operational VM and make any necessary modifications:

    • Install RightLink 10

      curl -s https://rightlink.rightscale.com/rll/10/rightlink.install.sh |
          sudo bash -s
      
    • Install updates

    • Install applications

    • Etc...

  3. From the VM: Run the following commands to sanitize the VM and prepare it for capture:

    sudo rm -rf /var/lib/cloud/ /tmp/*
    sudo rm -f /var/log/cloud-init* /etc/udev/rules.d/70-persistent-net.rules
    sudo find /root -name authorized_keys -type f -exec rm -f {} \;
    sudo find /home -name authorized_keys -type f -exec rm -f {} \;
    sudo find /var/log -type f -exec cp /dev/null {} \;
    history -c
    sync
    sudo waagent -deprovision+user -force
    
  4. Close the SSH session by typing: exit

  5. From your Workstation: Stop and Deallocate the VMs resources:

    Stop-AzureRmVM -Name <VM Name> -ResourceGroupName <Resource Group Name> -Force
    
    azure vm deallocate --resource-group <Resource Group Name> --name <VM Name>
    
  6. From your Workstation: Generalize the VM:

    Set-AzureRmVM -Name <VM Name> -ResourceGroupName <Resource Group Name> -Generalized
    
    azure vm generalize --resource-group <Resource Group Name> --name <VM Name>
    
  7. From your Workstation: Capture the image:
    Managed Image:

    $vm = Get-AzureRmVM -Name <VM Name> -ResourceGroupName <Resource Group Name>
    $image = New-AzureRmImageConfig -Location <Region> -SourceVirtualMachineId $vm.ID 
    New-AzureRmImage -Image $image -ImageName <Name of Image> -ResourceGroupName <Resource Group Name>
    
    az image create --resource-group <Resource Group Name> --name <Name of Image> --source <VM Name>
    

    Unmanaged Image:

    Save-AzureRmVMImage -VMName <VM Name> -ResourceGroupName <Resource Group Name> -DestinationContainerName "vhds" -VHDNamePrefix <choose unique prefix string>
    
    azure vm capture --resource-group <Resource Group Name> --name <VM Name> --vhd-name-prefix <choose unique prefix string>
    

    The unmanaged image will be captured as a VHD, with the prefix you defined and in the same Storage Account as the source VM, at the following path:
    <Storage Account>/system/Microsoft.Compute/Images/vhds/

  8. The source VM is no longer usable once it has been marked as generalized. Run the following command once the image has been successfully captured to delete the source VM:

    Remove-AzureRmVM -Name <VM Name> -ResourceGroupName <Resource Group Name> -Force
    
    azure vm delete --resource-group <Resource Group Name> --name <VM Name> --quiet
    
  9. Wait for RightScale Cloud Management to discover the newly captured image. This takes approximately 5-10 minutes.

  10. Once RightScale Cloud Management has discovered the image, you can add it to an existing MultiCloud Image or create a new MultiCloud Image.