简体   繁体   中英

How can I check the disk size of a VM with Azure Python SDK?

When I create a VM with Azure, for example an Standard A6, it comes with 2 mounted units, one with 30GB and another with 285GB. I can see this when I ssh to the VM. From the Azure portal, I can see the 30GB storage, but not the 285GB storage. However, when I go to the Azure calculator, I can see that the Standard A6 machines have 285GB temporary space.

  • What is this temporary space? What does it mean it's "temporary"?
  • How can I automatically check the disk size? I need to do this from python since I'm working on a cloud broker portal that'll show the disk size of a machine to a user.
  • How can I manually check the disk size? I can't see it in the VM resource or the storage account in the Azure portal. The only place I found this information was from the Azure calculator which is a bit of a work around.

EDIT : Although I have access to the machine via the Azure portal and the SDK, I can't ssh into the Virtual Machine created (this is a project requirement I can't change), so running bash commands in it is not an option.

I have found something that seems like it could achieve what I need, but it doesn't seem to work:

vm = cmc.virtual_machines.get(GROUP_NAME, VM_NAME)
os_disk_name = vm.storage_profile.os_disk.name
cmc.disks.get(GROUP_NAME, os_disk_name)
---------------------------------------------------------------------------
CloudError                                Traceback (most recent call last)
<ipython-input-39-818c4d1cac7d> in <module>()
----> 1 cmc.disks.get(GROUP_NAME, os_disk_name)
~/lib/python3.6/site-packages/azure/mgmt/compute/v2017_03_30/operations/disks_operations.py in get(self, resource_group_name, disk_name, custom_headers, raw, **operation_config)
    285             exp = CloudError(response)
    286             exp.request_id = response.headers.get('x-ms-request-id')
--> 287             raise exp
    288 
    289         deserialized = None
CloudError: Azure Error: ResourceNotFound
Message: The Resource 'Microsoft.Compute/disks/myosdisk' under resource group GROUP_NAME was not found.

What is this temporary space? What does it mean it's "temporary"?

You could check this official document .

By default when you create a VM, Azure provides you with an OS disk (/dev/sda) and a temporary disk (/dev/sdb). All additional disks you add show up as /dev/sdc, /dev/sdd, /dev/sde and so on. All data on your temporary disk (/dev/sdb) is not durable, and can be lost if specific events like VM Resizing, redeployment, or maintenance forces a restart of your VM. The size and type of your temporary disk is related to the VM size you chose at deployment time.

How can I automatically check the disk size? I need to do this from python since I'm working on a cloud broker portal that'll show the disk size of a machine to a user.

Azure Python SDK support check OS disk and data disk size, but it does not support check temporary disk. Temporary disk only depends on VM size. It does not supported change size. You could ssh to your VM. Execute df -h . By default, you will see below:

root@shui:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            1.7G     0  1.7G   0% /dev
tmpfs           342M  4.9M  338M   2% /run
/dev/sda1        30G  1.3G   28G   5% /
tmpfs           1.7G     0  1.7G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           1.7G     0  1.7G   0% /sys/fs/cgroup
/dev/sdb1        59G   52M   56G   1% /mnt

/dev/sdb1 is temporary space. (Note: The result depends on your Linux system, I test on Ubuntu 16.04. A2 size VM.)

How can I manually check the disk size? I can't see it in the VM resource or the storage account in the Azure portal. The only place I found this information was from the Azure calculator which is a bit of a work around.

You could check question 2.

Update:

Yes, you could use Azure Python SDK to get VM OS/Data disk size. For example:

rg = 'shuicli'
name = 'shui'
vm = compute_client.virtual_machines.get(rg,name)
##get OS disk size(GB)
print vm.storage_profile.os_disk.disk_size_gb
datadisks = vm.storage_profile.data_disks
##get data disk size(GB)
for i in datadisks:
    print i.disk_size_gb

See Azure Python SDK in this link .

1- To answer you question about temporary storage: "What is the purpose of temporary storage? This temporary storage is used to save the system paging file.You can also use this drive only to store temporary data, which you can afford to lose at any time.Like for example: If your workflow involves getting content from the blob, processing it and storing it back to the blob then you could download content to the temporary storage, process the content, then take the processed content and save it back to the blob." More details can be found here: https://blogs.msdn.microsoft.com/mast/2013/12/06/understanding-the-temporary-drive-on-windows-azure-virtual-machines/

2- Checking disk size using Python, I'd recommend checking: Find size and free space of the filesystem containing a given file The suggestion is:

import subprocess
df = subprocess.Popen(["df", "filename"], stdout=subprocess.PIPE)
output = df.communicate()[0]
device, size, used, available, percent, mountpoint = \
    output.split("\n")[1].split()

3- When you SSH to the VM, you can use one of this info depending on your distro:

a- df command – Shows the amount of disk space used and available on Linux file systems.

b- du command – Display the amount of disk space used by the specified files and for each subdirectory.

c- btrfs fi df /device/ – Show disk space usage information for a btrfs based mount point/file system. More info here: https://www.cyberciti.biz/faq/linux-check-disk-space-command/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM