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 u2004-vm1 \
  --image "Ubuntu 20.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 ${SERVER_ID} \
  $(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}  | cut -d "'" -f2)
openstack security group rule create --protocol tcp --dst-port 22:22 --remote-ip  ${SECURITY_GROUP_ID}
# allow HTTP(s) incomming access
PROJECT_ID=$(openstack server show ${SERVER_NAME} -c project_id -f value)
SECURITY_GROUP_ID=$(openstack security group list --project ${PROJECT_ID} -c ID -f value)
openstack security group rule create --protocol tcp --dst-port 80:80 --remote-ip  ${SECURITY_GROUP_ID}
openstack security group rule create --protocol tcp --dst-port 443:443 --remote-ip ${SECURITY_GROUP_ID}
# rename VM
openstack server set --name vm1-old ${SERVER_ID}
# connect to SSH (by floating IP)
openstack server ssh --login ubuntu --public ${SERVER_ID}

Snapshot and restore

# backup
openstack server image create --name vm1-snap1 vm1
openstack image list
openstack server rebuild --image vm1-snap1 vm1
openstack server show vm1 -c name -c status
# restore
openstack server rebuild --image ab317678-638d-481e-9249-831d68d854d5 vm1

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

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

Get instance meta data

curl | jq
curl | jq
# 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}


# list events
openstack server event list ${SERVER_ID}
# server event show
openstack server event show ${SERVER_ID} ${REQUEST_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)

Transfer VM

openstack volume list --all-projects --name docs1-dev
openstack volume transfer request create  4aef794d-9f79-4d24-a220-211dc4863424

Fix "Cannot 'start' instance while it is in vm_state resized" / "Instance has not been resized."

openstack server list --all --status VERIFY_RESIZE
# openstack server start ${SERVER_ID}
Cannot 'start' instance af46d773-841f-4774-8bdd-29face6a3757 while it is in vm_state resized (HTTP 409) (Request-ID: req-ccb5d5a8-8a1a-427e-a55a-8f72e3d9216d)
# openstack server resize confirm ${SERVER_ID}
Instance has not been resized. (HTTP 400) (Request-ID: req-ccf5b1c4-c0bd-4778-b8e4-bd17845c4355)
openstack server set --state active ${SERVER_ID}
openstack server stop ${SERVER_ID}


openstack server list --all-projects --name dummy -c ID -f value | xargs -L1 openstack server show -c project_id -f value | sort -u