Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New-AzVM: Cannot attach an existing OS disk if the VM is created from a platform, user or a shared gallery image #24884

Open
cadumi opened this issue May 10, 2024 · 2 comments
Assignees
Labels
bug This issue requires a change to an existing behavior in the product in order to be resolved. Compute - VM customer-reported Service Attention This issue is responsible by Azure service team.

Comments

@cadumi
Copy link

cadumi commented May 10, 2024

Description

Trying to delete an unmanaged disk VM and export it's properties as JSON and then recreate it, recreation fails with:

New-AzVM: Cannot attach an existing OS disk if the VM is created from a platform, user or a shared gallery image.
ErrorCode: InvalidParameter
ErrorMessage: Cannot attach an existing OS disk if the VM is created from a platform, user or a shared gallery image.
ErrorTarget: osDisk
StatusCode: 400
ReasonPhrase:
OperationID : 0de7d270-15e4-4650-9f1c-3d5fc1abd618

Issue script & Debug output

$rgname = "ReproRG" 
$vmname = "ReproRG"

Get-AzVM -ResourceGroupName $rgname -Name $vmname |ConvertTo-Json -depth 100|Out-file -FilePath .\$vmname.json
 
Remove-AzVM -ResourceGroupName $rgname -Name $vmname
 
    #Import from json
    $json = "$vmname.json"
    $import = gc $json -Raw|ConvertFrom-Json
    #Create variables for redeployment 
    $rgname = $import.ResourceGroupName
    $loc = $import.Location
    $vmsize = $import.HardwareProfile.VmSize
    $vmname = $import.Name
    #Create the vm config
    $vm = New-AzVMConfig -VMName $vmname -VMSize $vmsize
    #Network card info
    $importnicid = $import.NetworkProfile.NetworkInterfaces.Id
    $nicname = $importnicid.split("/")[-1]
    $nic = Get-AzNetworkInterface -Name $nicname -ResourceGroupName $rgname
    $nicId = $nic.Id
    $vm = Add-AzVMNetworkInterface -VM $vm -Id $nicId
    #OS Disk info
    $osDiskName = $import.StorageProfile.OsDisk.Name
    $osDiskVhdUri = $import.StorageProfile.OsDisk.Vhd.Uri
    $vm = Set-AzVMOSDisk -VM $vm -VhdUri $osDiskVhdUri -name $osDiskName -CreateOption attach -Linux -DeleteOption $import.StorageProfile.OsDisk.DeleteOption
    #$vm = Set-AzVMOSDisk -VM $vm -ManagedDiskId $import.StorageProfile.OsDisk.ManagedDisk.Id -StorageAccountType $import.StorageProfile.OsDisk.ManagedDisk.StorageAccountType -Name $osDiskName -CreateOption attach -Linux -DeleteOption $import.StorageProfile.OsDisk.DeleteOption
 
     
    #Create the VM
    New-AzVM -ResourceGroupName $rgname -Location $loc -VM $vm -Verbose

Environment data

$PSVersionTable     (Cloudshell)                                                                                                                                                              
                                                                                               
Name                           Value
----                           -----
PSVersion                      7.4.2
PSEdition                      Core
GitCommitId                    7.4.2
OS                             CBL-Mariner/Linux
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS C:\Windows\system32> $PSVersionTable (Local)

Name                           Value                                                                                                                                             
----                           -----                                                                                                                                             
PSVersion                      5.1.22621.2506                                                                                                                                    
PSEdition                      Desktop                                                                                                                                           
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                           
BuildVersion                   10.0.22621.2506                                                                                                                                   
CLRVersion                     4.0.30319.42000                                                                                                                                   
WSManStackVersion              3.0                                                                                                                                               
PSRemotingProtocolVersion      2.3                                                                                                                                               
SerializationVersion           1.1.0.1

Module versions

Get-Module Az* -Listavailable

7.3.0                 Az.Compute

Reproducible from Cloud Shell as well as local Powershell with Az Module installed.
No repro under old Az.Compute version 7.1.1.

Error output

Resolve-AzError -Last

   HistoryId: 26

RequestId      : 
Message        : Cannot attach an existing OS disk if the VM is created from a platform, user or a shared gallery image.
                 ErrorCode: InvalidParameter
                 ErrorMessage: Cannot attach an existing OS disk if the VM is created from a platform, user or a shared gallery image.
                 ErrorTarget: osDisk
                 StatusCode: 400
                 ReasonPhrase: 
                 OperationID : 0de7d270-15e4-4650-9f1c-3d5fc1abd618
ServerMessage  : 
ServerResponse : 
RequestMessage : 
InvocationInfo : {New-AzVM}
Line           :      New-AzVM -ResourceGroupName $rgname -Location $loc -VM $vm -Verbose
Position       : At line:1 char:6
                 +      New-AzVM -ResourceGroupName $rgname -Location $loc -VM $vm -Verb …
                 +      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
StackTrace     :    at Microsoft.Azure.Commands.Compute.ComputeClientBaseCmdlet.ExecuteClientAction(Action action)
                    at Microsoft.Azure.Commands.Compute.NewAzureVMCommand.DefaultExecuteCmdlet()
                    at Microsoft.Azure.Commands.Compute.NewAzureVMCommand.ExecuteCmdlet()
                    at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.<>c__3`1.<ExecuteSynchronouslyOrAsJob>b__3_0(T c)
                    at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet, Action`1 executor)
                    at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet)
                    at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()
HistoryId      : 26
@cadumi cadumi added bug This issue requires a change to an existing behavior in the product in order to be resolved. needs-triage This is a new issue that needs to be triaged to the appropriate team. labels May 10, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added customer-reported needs-triage This is a new issue that needs to be triaged to the appropriate team. and removed needs-triage This is a new issue that needs to be triaged to the appropriate team. labels May 10, 2024
@isra-fel isra-fel added Compute - VM Service Attention This issue is responsible by Azure service team. labels May 17, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot removed the needs-triage This is a new issue that needs to be triaged to the appropriate team. label May 17, 2024
@isra-fel isra-fel removed the needs-triage This is a new issue that needs to be triaged to the appropriate team. label May 17, 2024
@TravisCragg-MSFT TravisCragg-MSFT self-assigned this May 23, 2024
@TravisCragg-MSFT
Copy link

@cadumi can I ask why you are doing this?

Unmanaged disks in Azure are on the path to deprecation and I would highly advise moving to manage disks.

I would also recommend recreating your VM from scratch and not by capturing the JSON. There are objects that might not exist.

@cadumi
Copy link
Author

cadumi commented May 24, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue requires a change to an existing behavior in the product in order to be resolved. Compute - VM customer-reported Service Attention This issue is responsible by Azure service team.
Projects
None yet
Development

No branches or pull requests

3 participants