Start and Stop KVM Virtual Machines from Command Line

0
404
Start and Stop KVM Virtual Machines from Command Line

This guide will take you through how to start and stop KVM virtual machines from command line. As much as KVM provides Virtual Machine Manager (virt-manager), a desktop application for managing virtual machines through libvirt, it also provides a command line utility called virsh which enables the terminal centric users to manage KVM virtual machines from command line interface.

Start and Stop KVM Virtual Machines from Command Line

To start and stop KVM virtual machines from command line, virsh utility can be used.

According to man pages (man virsh);

The virsh program is the main interface for managing virsh guest domains. The program can be used to create, pause, and shutdown domains. It can also be used to list current domains.

The basic structure of most virsh usage is:

 virsh [OPTION]... <command> <domain> [ARG]...

Refer to man virsh for more details.

Start KVM Virtual Machines from Command Line

To begin with, list the available virtual machines, either running or stopped or paused;

virsh list --all
 Id   Name                State
------------------------------------
 1    ubuntu20.04         running
 -    kolla-ansible       shut off
 -    ubuntu20.04-clone   shut off

As you can see, I currently have three KVM virtual machines; one running and two stopped.

You can start KVM virtual machine using virsh start command. The virsh start command can be used to start a domain, either from the last managedsave state, or via a fresh boot if no managedsave state is present.

The basic usage syntax of the virsh start command is;

virsh start --help
  NAME
    start - start a (previously defined) inactive domain

  SYNOPSIS
    start <domain> [--console] [--paused] [--autodestroy] [--bypass-cache] [--force-boot] [--pass-fds <string>]

  DESCRIPTION
    Start a domain, either from the last managedsave
    state, or via a fresh boot if no managedsave state
    is present.

  OPTIONS
    [--domain] <string>  name of the inactive domain
    --console        attach to console after creation
    --paused         leave the guest paused after creation
    --autodestroy    automatically destroy the guest when virsh disconnects
    --bypass-cache   avoid file system cache when loading
    --force-boot     force fresh boot by discarding any managed save
    --pass-fds <string>  pass file descriptors N,M,... to the guest

Therefore, if you want to start a virtual machine, for example, Ubuntu20.04-clone, as in my case;

virsh start ubuntu20.04-clone

You can pass other options as well, if you want.

Your virtual machine should now be running. You can list only running vms using the command below;

virsh list
 Id   Name                State
-----------------------------------
 1    ubuntu20.04         running
 3    ubuntu20.04-clone   running

Stop KVM Virtual Machines from Command Line

It is also possible to stop KVM virtual machine from command line using the virsh shutdown command.

virsh shutdown --help
  NAME
    shutdown - gracefully shutdown a domain

  SYNOPSIS
    shutdown <domain> [--mode <string>]

  DESCRIPTION
    Run shutdown in the target domain.

  OPTIONS
    [--domain] <string>  domain name, id or uuid
    --mode <string>  shutdown mode: acpi|agent|initctl|signal|paravirt

For example, to shutdown a vm called, ubuntu20.04;

virsh shutdown Ubuntu20.04

You can as well specify the vm UUID or ID instead of the name when shutting it down.

For example, when you run virsh list command to check running vms, the first column of the output shows the vm ID. To use the ID while shutting down a vm;

virsh shutdown <id>

for example;

virsh shutdown 3

To use UUID while shutting down a vm, you first need to obtain the vm UUID. To get the Universally Unique Identifier (UUID) for a guest virtual machine:

virsh domuuid {domain-id or domain-name}

For example;

virsh domuuid 4

Or

virsh domuuid ubuntu20.04

b05cdf0c-8516-4f1e-a517-f52aab45cf61

Similarly, you can use virsh dominfo to get other details including the UUID of the vm;

virsh dominfo ubuntu20.04
Id:             -
Name:           ubuntu20.04
UUID:           b05cdf0c-8516-4f1e-a517-f52aab45cf61
OS Type:        hvm
State:          shut off
CPU(s):         2
Max memory:     2097152 KiB
Used memory:    2097152 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: none
Security DOI:   0

The shutdown using the UUID;

virsh shutdown b05cdf0c-8516-4f1e-a517-f52aab45cf61

You can as well specify other power options while shutting down the vm.

That marks the end of our tutorial on how to start and stop KVM virtual machines from command line.

Other Related Tutorials

List Running and Stopped VMS on KVM

How to Clone KVM Virtual Machines

How to fix QEMU/KVM Not Connected Error on Ubuntu 20.04

Use VirtualBox VMs on KVM

How to Rename KVM Virtual Machine with virsh command

LEAVE A REPLY

Please enter your comment!
Please enter your name here