Creating a VM from existing VHDs in Azure
A few years ago, for some masochistic reason, I decided I wanted to create an Arch Linux VM in Azure. I really don’t remember why, or how, I got started down that path - but switching now would be too high-cost to be worth it.
Sometimes I like to screw around with my Azure resources like the VM itself, networking, sizes, etc. which necessitates deleting the VM. But I don’t want to actually lose the OS - I want my data to say safe.
My handy .txt
file this is based off of told me I originally used this post, so thanks to whoever wrote that. Without further ado:
Connect-AzAccount
# RG info
$rgName = "default"
$locName = "East US 2"
$vmName = "{YOUR_VM_NAME}"
$diskBaseName = "{YOUR_DISK_BASE_NAME_MAYBE_SAME_AS_VM}"
# Networking
$vnet = Get-AzVirtualNetwork -Name "public-vnet" -ResourceGroupName $rgName
$nic = Get-AzNetworkInterface -Name "$vmName-if" -ResourceGroupName $rgName
# VM
$vm = New-AzVMConfig -VmName $vmName -VmSize "Standard_B2ms"
$vm = Add-AzVMNetworkInterface -VM $vm -Id $nic.Id
# Attach disks
$vm = Set-AzVMOSDisk -VM $vm -Name "arch_azure" -VhdUri "https://$vmName.blob.core.windows.net/os-vhd/arch_azure.vhd" -CreateOption Attach -Linux -Caching ReadWrite
$vm = Add-AzVMDataDisk -VM $vm -Name "$diskBaseName-data1" -VhdUri "https://$vmName.blob.core.windows.net/os-vhd/$diskBaseName-data1.vhd" -LUN 0 -Caching None -CreateOption Attach
$vm = Add-AzVMDataDisk -VM $vm -Name "$diskBaseName-data2" -VhdUri "https://$vmName.blob.core.windows.net/os-vhd/$diskBaseName-data2.vhd" -LUN 1 -Caching None -CreateOption Attach
$vm = Add-AzVMDataDisk -VM $vm -Name "$diskBaseName-data3" -VhdUri "https://$vmName.blob.core.windows.net/os-vhd/$diskBaseName-data3.vhd" -LUN 2 -Caching None -CreateOption Attach
$vm = Add-AzVMDataDisk -VM $vm -Name "$diskBaseName-data4" -VhdUri "https://$vmName.blob.core.windows.net/os-vhd/$diskBaseName-data4.vhd" -LUN 3 -Caching None -CreateOption Attach
# Create the VM
New-AzVM -ResourceGroupName $rgName -Location $locName -VM $vm -Verbose
In case it’s not obvious, this is using Powershell with the Azure Az module. Yes, it could probably use the fancy az CLI that exists too. No, I don’t care - it works for me 🙂
Preempting a few questions:
- This is using unmanaged disks because they are WAY cheaper per GB, and I can stripe 4 of them in RAID 0 using
btrfs
. While it wouldn’t be ideal, I don’t really really care about losing the data on there, plus I trust Azure to keep my data (relatively) safe.- Remember, I did this masochistically.
- The NIC name, as well as the storage account paths and disk names, are by convention - no real association necessary.
- I don’t remember why exactly I disabled disk caching for the “data disks” - I think some article told me to, but recommended that I keep it enabled for the OS disk. I might have perf tested it too.
- I’m using Azure because I get it for free as a work benefit. Not sure what I’d do otherwise, but dedicated servers are pretty cheap these days versus the public cloud!