> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Linux as a Virtualization Guest VIrtual Machines

> Learn to manage virtual machines on Linux using QEMU-KVM and Libvirt’s virsh CLI for virtualization and cloud platform support.

In this guide, you’ll learn how to manage virtual machines (VMs) on Linux using QEMU-KVM and Libvirt’s `virsh` CLI. By the end, you will be able to install dependencies, define VM domains via XML, control VM lifecycle, adjust resources, and clean up definitions—all from the command line. This workflow is fundamental for self-hosted virtualization and supports major cloud platforms like [AWS](https://aws.amazon.com), [DigitalOcean](https://www.digitalocean.com), and [Google Cloud](https://cloud.google.com).

Example: A single Linux server with 64 CPU cores and 1 TiB RAM can host dozens of isolated VMs, each with its own dedicated vCPU and memory allocation.

***

## 1. Installing Dependencies

Install QEMU, KVM, and Libvirt tools:

```bash theme={null}
sudo dnf install -y qemu-kvm libvirt
```

* **qemu-kvm**: Hardware-accelerated virtualization
* **libvirt**: API and utilities for managing VMs

<Callout icon="lightbulb" color="#1CB2FE">
  You may also need to start and enable the libvirtd service:

  ```bash theme={null}
  sudo systemctl enable --now libvirtd
  ```
</Callout>

***

## 2. Defining a Virtual Machine Domain

Libvirt uses XML to describe VM configurations (called *domains*). Create a file named `testmachine.xml`:

```bash theme={null}
vim testmachine.xml
```

Paste this minimal configuration:

```xml theme={null}
<domain type="kvm">
  <name>TestMachine</name>
  <memory unit="GiB">1</memory>
  <vcpu>1</vcpu>
  <os>
    <type arch="x86_64">hvm</type>
  </os>
</domain>
```

This sets:

* **Name**: TestMachine
* **Memory**: 1 GiB
* **vCPUs**: 1
* **OS**: Hardware VM on x86\_64

Save and exit.

***

## 3. Defining and Listing Domains

Register the domain with Libvirt:

```bash theme={null}
virsh define testmachine.xml
```

Expected output:

```text theme={null}
Domain 'TestMachine' defined from testmachine.xml
```

List all domains, including inactive:

```bash theme={null}
virsh list --all
```

Sample:

| Id | Name        | State    |
| -- | ----------- | -------- |
| -  | TestMachine | shut off |

<Callout icon="lightbulb" color="#1CB2FE">
  Omit `--all` to list only running domains.
</Callout>

***

## 4. VM Lifecycle Management

Use the following commands to control the VM. See the quick reference table below.

| Action            | Command                      |
| ----------------- | ---------------------------- |
| Start             | `virsh start TestMachine`    |
| Graceful reboot   | `virsh reboot TestMachine`   |
| Forced reset      | `virsh reset TestMachine`    |
| Graceful shutdown | `virsh shutdown TestMachine` |
| Forced power-off  | `virsh destroy TestMachine`  |

```bash theme={null}
# Example: Start the VM
virsh start TestMachine

# Verify running VMs
virsh list
```

> `reset` simulates pressing the reset button.\
> `destroy` cuts power immediately—like unplugging the VM.

***

## 5. Deleting a Domain

Remove the domain definition only:

```bash theme={null}
virsh undefine TestMachine
```

<Callout icon="triangle-alert" color="#FF6B6B">
  `--remove-all-storage` will also delete associated disk images:

  ```bash theme={null}
  virsh undefine TestMachine --remove-all-storage
  ```
</Callout>

***

## 6. Autostart Configuration

Enable the VM to start on host boot:

```bash theme={null}
virsh autostart TestMachine
```

Disable autostart:

```bash theme={null}
virsh autostart --disable TestMachine
```

***

## 7. Inspecting VM Details

Retrieve detailed VM information:

```bash theme={null}
virsh dominfo TestMachine
```

Sample output:

```text theme={null}
Id:             -
Name:           TestMachine
UUID:           01a57937-3fbb-4191-9cec-a73383456fa8
OS Type:        hvm
State:          shut off
CPU(s):         1
Max memory:     1048576 KiB
Used memory:    1048576 KiB
Persistent:     yes
Autostart:      disable
```

***

## 8. Modifying VM Resources

### Adjusting vCPUs

Increase maximum vCPUs:

```bash theme={null}
virsh setvcpus TestMachine 2 --config --maximum
```

Set vCPUs for next boot:

```bash theme={null}
virsh setvcpus TestMachine 2 --config
```

### Adjusting Memory

Set maximum memory:

```bash theme={null}
virsh setmaxmem TestMachine 2048M --config
```

Verify changes:

```bash theme={null}
virsh dominfo TestMachine
```

***

## 9. Starting and Verifying Changes

Start the VM with updated resources:

```bash theme={null}
virsh start TestMachine
```

Confirm the running state:

```bash theme={null}
virsh dominfo TestMachine
```

Expected fields:

```text theme={null}
State:        running
CPU(s):       2
Max memory:   2097152 KiB
Used memory:  2097152 KiB
```

***

## Quick Reference: Common `virsh` Commands

| Command                                                    | Description                |
| ---------------------------------------------------------- | -------------------------- |
| virsh define \<file>.xml                                   | Define a new VM domain     |
| virsh start \<domain>                                      | Start a VM                 |
| virsh shutdown \<domain>                                   | Graceful shutdown          |
| virsh destroy \<domain>                                    | Forced power off           |
| virsh reboot \<domain>                                     | Reboot VM                  |
| virsh reset \<domain>                                      | Forced reset               |
| virsh list \[--all]                                        | List VMs (active/inactive) |
| virsh undefine \<domain> \[--remove-all-storage]           | Remove VM definition       |
| virsh autostart \<domain> \[--disable]                     | Enable/disable autostart   |
| virsh dominfo \<domain>                                    | Show domain information    |
| virsh setvcpus \<domain> \<count> \[--config] \[--maximum] | Adjust vCPUs               |
| virsh setmaxmem \<domain> \<size> \[--config]              | Adjust max memory          |

***

## References

* [Libvirt Documentation](https://libvirt.org/docs.html)
* [QEMU Official Website](https://www.qemu.org/)
* [KVM on Linux](https://www.linux-kvm.org/)
* [Cloud Providers Comparison](https://aws.amazon.com/compare/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/linux-professional-institute-lpic-1-exam-101/module/78ca0fa8-2083-408a-bf8a-2775b09fbf1d/lesson/6cd15bbf-73f8-4fee-9bfb-c6923d53698e" />
</CardGroup>
