4

How to tell if Virtual machine is (un)managed? Meaning whether VM has only (un)managed disks since those cannot be mixed (to my knowledge).

I was originally using Azure Fluent SDK. But I am having some trouble with that and there are even some issues.

Only way to find out is to go over the disk(s) (or check only one since they cannot be mixed up) and check whether disk(s) itself is (un)managed. Is that right?

The question is how to find out whether disk is (un)managed? My first idea is to check OSDisk property (or DataDisk for that matter)

  • If it has managedDisk/id property, then it is obviously managed disk.
  • If it has vhd/uri property, then it is obviously unmanaged disk.

That seems simple enough (and I am pretty sure it at least 99% of the time works but I really need to be 100% sure) but if you check source code of Fluent SDK library (by Microsoft so what is the better source of truth?) the logic is way more complicated. Also, I have my doubts it is correct (see bug above).

To put it shortly what is in 100% cases reliable way to check whether VM is managed or not?

Jason Pan
  • 15,263
  • 1
  • 14
  • 29
robot40q
  • 101
  • 5
  • Have you looked at the Azure Resource Explorer btw? – Dai Feb 03 '21 at 17:15
  • Looking at my own Azure Resource Explorer ( https://resources.azure.com ) I can enumerate VMs under `subscriptions/{sub}/providers/Microsoft.Compute/virtualMachines` and after parsing the JSON response, determine if a VM has 100% or not-100% managed disks - is that sufficient for your purposes? If so, it's straightforward to write a client for that. – Dai Feb 03 '21 at 17:18
  • 2Dai: The question remains. You wrote "after parsing the JSON response, determine if a VM has 100% or not-100% managed disks". Is the JSON different from REST API? If yes how? If not according what properties do you determine whether it is managed disk or not? – robot40q Feb 03 '21 at 17:59
  • Have you looked at Resource Explorer? – Dai Feb 03 '21 at 18:24
  • Yes, it seems to return the same JSON as REST API. To answer your question it is not sufficient. How do you 100% determinate whether the disk is managed or not? As I wrote in original question I am not really sure... – robot40q Feb 03 '21 at 18:39

1 Answers1

1

All your cognitions about the (un)managed VM is right, I just can confirm it. And to distinguish if the VM is a managed VM, you only need to check if the OS disk is a managed disk, which means if the property managedDisk of the OS disk is not null.

As you know, managed and unmanaged disks can't mix. So if the OS disk is managed, then the VM must be a managed VM, because each VM must have one OS disk.

Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • Thanks for answer. It makes a lot of sense to check just OS disk. I guess we could check any disk, but OS disk is probably always there. Your answer is more or less what I am suggesting in my original post ("If it has managedDisk/id property, then it is obviously managed disk."). But are you sure it is 100% reliable? Do you have any source which would confirm that? The reason I am doubtful is that this answer is basically saying that the logic in MS library (mentioned in original post) is wrong which is strange because the logic is more complicated and less intuitive so there may be a reason. – robot40q Feb 04 '21 at 09:35
  • @robot40q Where you find that this logic is wrong with the MS library? – Charles Xu Feb 04 '21 at 09:40
  • @robot40q In fact, there is no doc to show you how to check if the VM is managed or not. You need to do it yourself. Maybe [this](https://stackoverflow.com/questions/43418208/how-to-find-if-a-virtual-machine-is-using-managed-unmanaged-disks-in-azure) one will be helpful. – Charles Xu Feb 04 '21 at 09:44
  • See the [source code on GitHub](https://github.com/Azure/azure-libraries-for-net/blob/9eb0f12e3289825c86af51fa2fa4fa887d3aed92/src/ResourceManagement/Compute/VirtualMachineImpl.cs#L1444). – robot40q Feb 04 '21 at 09:46
  • @robot40q You can see all the conditions and the results when the condition is true. Finally, you can find when each condition is true, the `managedDisk` will be what we expected. So there should be nothing that confused you. – Charles Xu Feb 18 '21 at 08:34
  • Thanks for reply but I am not sure I follow. Originally you said I can distinguish it as I wrote in original post (managedDisk/id or vhd/uri property). Now you are saying that I can follow the code where is different logic (and there are some open issues about it). So I am not even sure what you think is the right solution anymore. As I wrote I am looking for definitive 100% reliable solution, but you are just proposing solutions from original post without anything to back them up which at this point is not very helpful. – robot40q Feb 19 '21 at 17:51
  • @robot40q They are the same logic, not different. I think you're not good at finding the truth of the logic. – Charles Xu Feb 25 '21 at 06:18
  • Can you elaborate? I really don't understand how the logic is the same. – robot40q Feb 25 '21 at 08:54
  • @robot40q VM property `managedDisk` is the basic logic as I said. And you can check what does the logic in the Github example result in? If the conditions in Github are true, what does the VM looks like? Of course, the VM must have `managedDisk` property and it's not null. Just check it! – Charles Xu Feb 25 '21 at 08:58
  • I agree that if it is managed then there should be a managedDisk. But what you are basically saying is that the logic in GitHub example is at least unnecessary (if not wrong). Why there are multiple checks of multiple properties if there could be just check for managedDisk property? I just cannot ignore it, go for simple solution and hope it will work every single time. – robot40q Feb 25 '21 at 10:42