OpenStack: server (VM)

Get VM data

openstack server show -c name -c id -c addresses -c OS-EXT-SRV-ATTR:host ${SERVER_ID}
nova interface-list ${SERVER_ID}
nova migration-list --instance-uuid ${SERVER_ID} | grep -v "2019-"

Create VM

openstack server create foo-vm1 \
  --image "Ubuntu 18.04" \
  --flavor m1.small \
  --key-name foo-key \
  --network foo-net \
  --security-group default


# Delete instance
openstack server delete ${SERVER_ID}
# list all servers from all projects
openstack server list --all-projects
# get IDs only from server
openstack server list -c ID -f value
# get all servers using windows images
for IMAGE in $(openstack image list --long --os-cloud=prod-admin -c ID -c Name -f value | grep -i "Windows" | cut -d" " -f 1); do
    openstack server list --long --all-projects --os-cloud=prod-admin --image ${IMAGE} 
# Add floating IP
openstack server add floating ip vm1 $(openstack floating ip create public -c floating_ip_address -f value)
# resize
openstack server resize ...
openstack server resize --confirm ${SERVER_ID}
# attach port to VM
openstack server add port VM1 NET1-PORT1
# list instances / server
openstack server list
openstack console log show foo-vm1
# get hypervisor
openstack server list
ssh compute1 ps axf | grep ${SERVER_ID}
# Show console URL
openstack console url show ${SERVER_ID}
# Reboot
openstack server reboot --hard  ${SERVER_ID}
# get server ports
nova interface-list  ${SERVER_ID}
# set VM properties
openstack server set --property "sysadmin=foo" --property "os=Ubuntu" ${SERVER_ID}
# remove VM property
openstack server unset --property os ${SERVER_ID}
# Update / fix server status
openstack server set --state active ${SEVER_ID}
openstack server show -c id -c name -c status -c OS-EXT-SRV-ATTR:hypervisor_hostname ${SEVER_ID}
# allow SSH incomming access
SECURITY_GROUP_ID=$(openstack server show -c security_groups -f value ${SERVER_ID})
openstack security group rule create --protocol tcp --dst-port 22:22 --remote-ip  ${SECURITY_GROUP_ID}

Snapshot and restore

openstack server image create --name vm1-snap1 vm1 --os-cloud=dev
openstack image list --os-cloud=dev
openstack server rebuild --image vm1-snap1 vm1 --os-cloud=dev
openstack server show vm1 -c name -c status --os-cloud=dev

Install qemu-guest-agent (run inside of a VM)

apt install -y qemu-guest-agent
# hw_qemu_guest_agent=yes

Get instance data

# get flavor
# get security-groups
# get availability-zone

List VMs

# list VMs and  tenants
nova list --all-tenants
nova list --all-tenants --fields tenant_id
openstack server list --all --long --sort-column Name -f json
openstack server list --all --long --sort-column Name --name foo
# show all deleted VMs
openstack server list --deleted --all

Fix task_state "powering-on"

for SERVER_ID in $(openstack server list --all -c ID -f value); do
    openstack server show ${SERVER_ID} -c id -c OS-EXT-STS:task_state -f value | paste - - | grep -v None
openstack server set --state active ${SERVER_ID}
openstack server reboot ${SERVER_ID} [--hard]
openstack console url show ${SERVER_ID}

search for VM on all compute nodes

for NODE in $(openstack compute service list --service nova-compute -c Host -f value); do
    echo ${NODE}
    ssh ${NODE} ps axf | grep ${SERVER_ID}
    ssh ${NODE} docker exec -i nova_libvirt virsh dominfo ${SERVER_ID}


openstack server event list ${SERVER_ID}

Get server / tanent data

for SERVER_ID in ${SERVER_IDS}; do
    SERVER_JSON=$(openstack server show ${SERVER_ID} -f json)
    SERVER_NAME=$(echo ${SERVER_JSON} | jq -r .name)
    PROJECT_ID=$(echo ${SERVER_JSON} | jq -r .project_id)
    PROJECT_JSON=$(openstack project show ${PROJECT_ID} -f json)
    PROJECT_NAME=$(echo ${PROJECT_JSON} | jq -r .name)
    DOMAIN_ID=$(echo ${PROJECT_JSON} | jq -r .domain_id)
    DOMAIN_JSON=$(openstack domain show ${DOMAIN_ID} -f json)
    DOMAIN_NAME=$(echo ${DOMAIN_JSON} | jq -r .name)