Azure: Create a custom image of an Azure VM using the CLI and Create a VM from the image

Azure: Create a custom image of an Azure VM using the CLI and Create a VM from the image

Deprovision the VM

De-provisioning generalizes the VM by removing machine-specific information. During deprovisioning the host name is reset to localhost.localdomain.

Specifies setting are deleted, like

  • SSH host keys,
  • nameserver configurations,
  • root password
  • cached DHCP leases

SSH to the VM.

Connect to your VM using SSH and run the command to deprovision the VM.

1
ssh azureuser@FQDN
1
sudo waagent -deprovision+user -force

You will get these messages

1
2
3
4
5
WARNING! The waagent service will be stopped.
WARNING! Cached DHCP leases will be deleted.
WARNING! root password will be disabled. You will not be able to login as root.
WARNING! /etc/resolvconf/resolv.conf.d/tail and /etc/resolvconf/resolv.conf.d/original will be deleted.
WARNING! ned account and entire home directory will be deleted.

Exit form the VM

1
exit

Deallocate the VM

1
2
3
az vm deallocate \
--resource-group myResourceGroup \
--name myVM

Mark the VM as generalized

1
2
3
az vm generalize \
--resource-group myResourceGroup \
--name myVM

Create the image

1
2
3
4
az image create \
--resource-group myResourceGroup \
--name myImage \
--source myVM

Create VMs from the image

1
2
3
4
5
6
az vm create \
--resource-group myResourceGroup \
--name myVMfromImage \
--image myImage \
--admin-username azureuser \
--generate-ssh-keys

Image management

List

1
2
3
az image list \
--resource-group myResourceGroup \
-o table

Delete

1
2
3
az image delete \
--name myOldImage \
--resource-group myResourceGroup