Stuck VM in vCloud Director

Posted: November 22, 2013 in vCloud Director
Tags:

After using vCloud Director for some time I have run into the occasion when we have VMs that will get stuck in the interface. An example would be when it is stuck powering on/off, updating hardware or doing most changes when a cell dies for whatever reason. Usually it ends up being busy for the rest of the time without timing out. For whatever reason it becomes stuck I have not found much resources online to assist with resolving the problem.

It is good to know that the Director cells are stateless and most if not all operations are controlled in the database. With this being said most of the fixing that needs to be done happens at the database level. So let’s get started. In this example I will give a you a case for resolving a stuck VM that is ok to power off or is already off. You will need access to vCloud Director’s database, the name of the vApp and the name (display name not the OS/Host name) of the VM.

1. From vCenter, clone the VM (unless you do not want it or it no longer exists) use whatever name you choose

2. From vCenter, delete the original VM (you have that clone from step one right?)

3. Open SMS (SQL Management Studio) and go to the vCloud Director Database

Now the SQL script below will remove any VM from any vApp in vCloud Director. It is wise to note that using this script without deleting the VM from your infrastructure will only remove the reference of it from vCloud Director, it will not delete the VM from DISK. The reason we delete the VM from disk in step two is for two reasons, first, when we later add the clone back into vCloud Director we may have problems with the mac address on the network card. You may want to keep the same mac address in certain cases. Second it gives us a clean VM without the vCloud tags for import.

4. Open/paste the script in SMS and edit the two parameters for “vAPP Name” and for “VM Name”

5. Run the script and hang on for dear life. 🙂

When the script is complete just refresh your vApp page and that will be the end of your problem.

Any improvements to the script is welcomed. I will also create one for a complete wipe of the vApp and it’s contents in a future post.

–******************************
–Created By Neeshan Peters
–November 22 2013
–For personal Use Only
–This script will remove any VM form any vApp in vCloud Director
–It will not delete the VM from disk or remove it from vCenter inventory
–******************************

–******************************
–Declare and Set variables
–******************************
declare @vAPP VARCHAR(254)
declare @vm VARCHAR(254)
set @vAPP = ‘[vApp Name]’
set @vm = ‘[VM Name]’

–******************************
–Remove VM vCloud
–******************************

DELETE FROM computevm WHERE id IN (
SELECT vappvm.cvm_id FROM
computevm cvm INNER JOIN vapp_vm vappvm ON cvm.id = vappvm.cvm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

DELETE FROM deployed_vm WHERE vm_id IN (
SELECT dvm.vm_id FROM
deployed_vm dvm INNER JOIN vm svm ON dvm.vm_id = svm.id
INNER JOIN vapp_vm vappvm ON svm.id = vappvm.svm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

DELETE FROM vm_disk WHERE vm_id IN (
SELECT vmdisk.vm_id FROM
vm_disk vmdisk INNER JOIN vm svm ON vmdisk.vm_id = svm.id
INNER JOIN vapp_vm vappvm ON svm.id = vappvm.svm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

DELETE FROM vm_snapshot WHERE vm_id IN (
SELECT vmsn.vm_id FROM
vm_snapshot vmsn INNER JOIN vm svm ON vmsn.vm_id = svm.id
INNER JOIN vapp_vm vappvm ON svm.id = vappvm.svm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

DELETE FROM guest_personalization_info WHERE id IN (
SELECT guest_per.id FROM
guest_personalization_info guest_per
INNER JOIN vapp_vm vappvm ON guest_per.vapp_vm_id = vappvm.id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

DELETE FROM shadow_vm WHERE primary_vm_id IN (
SELECT shadowvm.primary_vm_id FROM
shadow_vm shadowvm  INNER JOIN vm svm ON shadowvm.primary_vm_id = svm.id
INNER JOIN vapp_vm vappvm ON svm.id = vappvm.svm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

DELETE FROM vm_resource WHERE vm_id IN (
SELECT vmres.vm_id FROM
vm_resource vmres INNER JOIN vm svm ON vmres.vm_id = svm.id
INNER JOIN vapp_vm vappvm ON svm.id = vappvm.svm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);
DELETE FROM vm WHERE id IN (

SELECT vappvm.svm_id FROM
vm svm INNER JOIN vapp_vm vappvm ON svm.id = vappvm.svm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);
DELETE FROM networked_vm_mac WHERE netvm_id IN (

SELECT nvm_mac.netvm_id FROM
networked_vm_mac nvm_mac INNER JOIN networked_vm nvm ON nvm_mac.netvm_id = nvm.id INNER JOIN vapp_vm vappvm ON nvm.id = vappvm.nvm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

DELETE FROM networked_vm WHERE id IN (
SELECT vappvm.nvm_id FROM
networked_vm nvm INNER JOIN vapp_vm vappvm ON nvm.id = vappvm.nvm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

DELETE FROM vapp_vm WHERE id IN (
SELECT vappvm.id FROM vapp_vm vappvm INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

Advertisements
Comments
  1. Brent Taylor says:

    This script worked for me….for the most part. I had to edit the comments syntax and correct one of the column names. Here is what my script looked like when I got it to run:

    /******************************
    This script will remove any VM form any vApp in vCloud Director
    It will not delete the VM from disk or remove it from vCenter inventory
    ******************************/

    /******************************
    Declare and Set variables
    ******************************/
    declare @vAPP VARCHAR(254)
    declare @vm VARCHAR(254)
    set @vAPP = ‘vApp-COD-RegisMgmt’
    set @vm = ‘RegisExch01’

    /******************************
    Remove VM vCloud
    ******************************/

    DELETE FROM computevm WHERE id IN (
    SELECT vappvm.cvm_id FROM
    computevm cvm INNER JOIN vapp_vm vappvm ON cvm.id = vappvm.cvm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

    DELETE FROM deployed_vm WHERE vm_id IN (
    SELECT dvm.vm_id FROM
    deployed_vm dvm INNER JOIN vm svm ON dvm.vm_id = svm.id
    INNER JOIN vapp_vm vappvm ON svm.id = vappvm.svm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

    DELETE FROM vm_disk WHERE vm_id IN (
    SELECT vmdisk.vm_id FROM
    vm_disk vmdisk INNER JOIN vm svm ON vmdisk.vm_id = svm.id
    INNER JOIN vapp_vm vappvm ON svm.id = vappvm.svm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

    DELETE FROM vm_snapshot WHERE vm_id IN (
    SELECT vmsn.vm_id FROM
    vm_snapshot vmsn INNER JOIN vm svm ON vmsn.vm_id = svm.id
    INNER JOIN vapp_vm vappvm ON svm.id = vappvm.svm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

    DELETE FROM guest_personalization_info WHERE id IN (
    SELECT guest_per.id FROM
    guest_personalization_info guest_per
    INNER JOIN vapp_vm vappvm ON guest_per.vm_id = vappvm.id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

    DELETE FROM shadow_vm WHERE primary_vm_id IN (
    SELECT shadowvm.primary_vm_id FROM
    shadow_vm shadowvm INNER JOIN vm svm ON shadowvm.primary_vm_id = svm.id
    INNER JOIN vapp_vm vappvm ON svm.id = vappvm.svm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

    DELETE FROM vm_resource WHERE vm_id IN (
    SELECT vmres.vm_id FROM
    vm_resource vmres INNER JOIN vm svm ON vmres.vm_id = svm.id
    INNER JOIN vapp_vm vappvm ON svm.id = vappvm.svm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);
    DELETE FROM vm WHERE id IN (

    SELECT vappvm.svm_id FROM
    vm svm INNER JOIN vapp_vm vappvm ON svm.id = vappvm.svm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);
    DELETE FROM networked_vm_mac WHERE netvm_id IN (

    SELECT nvm_mac.netvm_id FROM
    networked_vm_mac nvm_mac INNER JOIN networked_vm nvm ON nvm_mac.netvm_id = nvm.id INNER JOIN vapp_vm vappvm ON nvm.id = vappvm.nvm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

    DELETE FROM networked_vm WHERE id IN (
    SELECT vappvm.nvm_id FROM
    networked_vm nvm INNER JOIN vapp_vm vappvm ON nvm.id = vappvm.nvm_id INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

    DELETE FROM vapp_vm WHERE id IN (
    SELECT vappvm.id FROM vapp_vm vappvm INNER JOIN vm_container vapp ON vappvm.vapp_id = vapp.sg_id WHERE vapp.name = @vAPP and vappvm.name = @vm);

  2. John says:

    Hi Brent,

    Your script works fine for me, I just notice one error in “guest_personalization_info” script part:
    – Just replace ” guest_per.vm_id” by “guest_per.vapp_vm_id”

    • neeshanpeters says:

      That’s good to hear.
      Not a lot of resources for vCloud Director out there.
      Please post or ask me to post anything you feel will help the community

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s