Last active
May 3, 2018 18:19
-
-
Save cinek810/df99794a9c0035d99e36e8d36ae99d47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# $1 - VM name | |
# $2 - resoure group | |
# Overcomplicated solution for https://funinit.wordpress.com/2018/05/03/resizing-vms-in-azure-sequel/ | |
# Basically change the VM disk from Premium to Standard SKU to allow resize to Basic tier | |
# Assume one VM in Resource group | |
# Assume VM has one disk | |
NAME=$1 | |
GROUP=$2 | |
#Gather information from OLD VM | |
echo "Get vmDiskSize" | |
diskId=$(az vm show -g $GROUP --name $NAME --query storageProfile.osDisk.managedDisk.id -o tsv) | |
echo "diskId=$diskId" | |
vmDiskSize=$(az disk show --id $diskId --query diskSizeGb -o tsv) | |
echo "vmDiskSize=$vmDiskSize" | |
echo "Get osType" | |
osType=$(az vm show -g $GROUP -n $NAME --query storageProfile.osDisk.osType -o tsv) | |
echo "osType=$osType" | |
echo "Get vmDisk" | |
vmDisk=$(az disk show --id $diskId --query "name" -o tsv ) | |
echo "vmDisk=$vmDisk" | |
echo "Get oldIpAddress" | |
oldIpAddress=$(az vm list-ip-addresses -n $NAME -g $GROUP --query [].virtualMachine.network.privateIpAddresses -o tsv) | |
snapName=${1}-snap | |
#Create a VM disk snapshot | |
echo "Create snapshot" | |
az snapshot create -g $GROUP -n $snapName --source $vmDisk | |
if [ $? -ne 0 ] | |
then | |
exit 1 | |
fi | |
snapshotId=$(az snapshot show --name $snapName --resource-group $GROUP --query [id] -o tsv) | |
#Create new DISK | |
newDiskName=${1}-StandardDisk | |
az disk create --resource-group $GROUP --name $newDiskName --sku Standard_LRS --source $snapshotId --size-gb $vmDiskSize | |
#Stop the VM to reuse IP address | |
echo "Stop old VM" | |
az vm stop -n $NAME -g $GROUP | |
nicId=$(az vm nic list -g $GROUP --vm-name $1 --query [].id -o tsv) | |
echo "nicId=$nicId" | |
#It's not possible because VM must have at least one nic | |
#echo "Remove oldVM nic" | |
#az vm nic remove --vm-name $1 -g $GROUP --nics $nicId | |
az vm delete -n $NAME -g $GROUP --yes | |
#Create new VM | |
newVMNAME=${1}-Standard | |
az vm create --name $newVMNAME --resource-group $GROUP --attach-os-disk $newDiskName --os-type $osType --nics $nicId | |
#Cleanup - remove snapshot | |
az snapshot delete --resource-group $GROUP --name $snamName | |
az vm deallocate --name $newVMName --resource-group $GROUP | |
az vm resize --name $newVMNAME --resource-group $GROUP --size Basic_A1 | |
az vm start --name $enwVMNAME -g $GROUP | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment