Add and size disks in Azure virtual machines

C.J. Shields
4 min readJul 17, 2020

--

My goal here is to create a Linux VM and attach a virual hard disk to store /uploads directory

  1. First I chose the default location I’d like to use. In this case I chose westus2

az configure — defaults location=eastus

2. Next I set the resource group name(which was already preconfigured). In this case you would choose YOUR resource group to attach your new VHD

az configure — defaults group=”learn-1e168d69–5574–4a4c-870c-81fa33988891"

3. Now I create my Virtual Machine using the az vm create command

az vm create \
— name support-web-vm01 \
— image UbuntuLTS \
— size Standard_DS1_v2 \
— admin-username azureuser \
— generate-ssh-keys

Adding the disk

Now we need to add the disk using the az vm disk attach command. This command adds a new empty disk to our VM

az vm disk attach \
— vm-name support-web-vm01 \
— name uploadDataDisk1 \
— size-gb 64 \
— sku Premium_LRS \
— new

Note: A quick rundown of this command

the name, names the disk uploadDataDisk1

the size, sets the size to 64 GB

and the sku, specifies to use premium storage(SSD) with local redundancy(LRS)

Initialize and format disk

My next step would be to run the az vm extension set command to attaches the disk to the mount point /uploads

az vm extension set \
— vm-name support-web-vm01 \
— name customScript \
— publisher Microsoft.Azure.Extensions \
— settings ‘{“fileUris”:[“https://raw.githubusercontent.com/MicrosoftDocs/mslearn-add-and-size-disks-in-azure-virtual-machines/master/add-data-disk.sh"]}' \
— protected-settings ‘{“commandToExecute”: “./add-data-disk.sh”}’

After using SSH to remote into my VM I see the sdc is partitioned and mounted to the uploads directoy

Resizing your disk

There are times were servers may need additional storage or you simply underestimated how large some files are. With these next commands you can easily provision additional space to a disk.

In this case we already know the name of our disk since we’ve created it. But lets say for instance someone else created it and you need to find the name. You would use the az disk list command. To resize a disk you either need the ID or the name of the disk.

az disk list \
— query ‘[*].{Name:name,Gb:diskSizeGb,Tier:sku.tier}’ \
— output table

Next you need to run the az vm deallocate command. This stops the vm and releases computing resources that it’s associated with so that we can make changes to the virtualized hardware.

az vm deallocate — name support-web-vm01

Run the az disk update to resize the disk

az disk update — name uploadDataDisk1 — size-gb 128

Run the az vm start to restart the vm

az vm start — name support-web-vm01

You VM has allocated your new space BUT you need to initialize the disk. This tells the OS the newly available space. The command you would use is az vm extension set. Here is a pre-made script to assist

az vm extension set \
— vm-name support-web-vm01 \
— name customScript \
— publisher Microsoft.Azure.Extensions \
— settings ‘{“fileUris”:[“https://raw.githubusercontent.com/MicrosoftDocs/mslearn-add-and-size-disks-in-azure-virtual-machines/master/resize-data-disk.sh"]}' \
— protected-settings ‘{“commandToExecute”: “./resize-data-disk.sh”}’

How we can run our command to see our changes

ssh azureuser@$ipaddress lsblk

--

--