How to Attach Disk to KVM VM with Virsh Command or Virt-manager?

If you have a virtual disk, you can use it on an existing VM. In KVM environment, IT administrators can use virsh command or virt-manager to attach virtual disk.

download-icon
Free Download
for VM, OS, DB, File, NAS, etc.
nick-zhao

Updated by Nick Zhao on 2023/01/16

Table of contents
  • How to attach virtual disk to KVM VM with virsh command?

  • How to attach virtual disk to VM with virt-manager?

  • How to protect and manage KVM VM with Vinchin Backup & Recovery?

  • Sum Up

Hard disk is used to contain most of data of computer and virtual machine also has virtual disk for data storing. On different hypervisors, virtual disks often require different management solutions.

For example, if companies use KVM as the hypervisor to deploy virtual environment, virtual disks are usually saved as qcow2 or raw format files and the default management tool is virsh command. There is also the graphical management tool for IT administrators, virt-mananger.

Virtual disks can be created or extracted from an existing VM. When the data in certain virtual disk is needed by another VM, IT administrator can attach it to this VM for data migration.

How to attach virtual disk to KVM VM with virsh command?

Virsh command utility is included in libvirt client package. To make sure you can successfully use the command later, you need to install it first.

The first step is having a virtual disk to attach. You can create an empty virtual disk with qemu-img, a virtual disk management tool, which can be also used to convert virtual disk.

Use the following command to create a qcow2 image with a maximum size of 10GB:

qemu-img create -f qcow2 virtual_disk_directory.img 10G

If you wish to get the virtual disk from an existing VM, you just need to find it directory by:

virsh domblklist guest_name

The next step is attaching the virtual disk.

# virsh attach-disk nebula1 /data/nebula1-add.qcow2 vdb --subdriver qcow2

--subdriver qcow2 means attaching qcow2 virtual disk.

--config means adding virtual disk to this machine forever, or you will find it’s gone the next time you boot this virtual machine.

Now you can check whether the virtual disk has been attached to the target VM.

If you want to remove the virtual disk temporarily, you can also use virsh command to do this:

# virsh detach-disk nebula1 vdb

If you don’t need the virtual disk anymore and want to remove it forever, you need to add –config in the end of this command.

How to attach virtual disk to VM with virt-manager?

Like VMware has vCenter, Hyper-V has Hyper-V Manager, KVM also has virt-manager for VM management. The graphical interface allows users to easily create, delete, export VM, etc., and users can also monitor the VM performance in real time.

Attaching virtual disk to KVM by editing VM in virt-manager is simpler than using virsh command because there is no additional tool required and users don’t have to understand any options and parameters.

1. In virt-manager, select the VM you would like to attach virtual disk > click the light bulb icon to edit VM

2. Click Add Hardware at the bottom and then you will be shown with a list which presents which hardware you can add > select Storage

3. You can directly create a new virtual disk here or select a custom storage. Click Finish then.

How to protect and manage KVM VM with Vinchin Backup & Recovery?

To protect VM data and optimize disaster recovery plan, you can select the professional backup and disaster recovery solution Vinchin Backup & Recovery.

Vinchin Backup & Recovery can help protect KVM environment like Red Hat Virtualization, oVirt, OpenStack, Oracle Linux KVM, etc. There is a user-friendly web console to help you easily add VMs to the backup system agentlessly and configure backup jobs for different VMs.

Vinchin Backup & Recovery

You can flexibly configure your backup job with abundant backup strategies. If you just use virsh command, you might only backup KVM VM by exporting and importing VM but in Vinchin Backup & Recovery, you can backup VM automatically by configuring the schedule, save backup storage and time by configuring incremental backup, protect backup data by encrypting data and data transmission, etc.

Vinchin Instant Recovery technology will help you quickly recover VM in the event of downtime from its backup in 15 seconds and Vinchin Virtual Machine Convert Engine will help you easily move VM to another hypervisor like VMware ESXi and Xen.

Vinchin Backup & Recovery has been selected by thousands of companies and you can also start to use this powerful system with a 60-day full-featured free trial. Just click the button to get the installation package and take a few minutes to deploy it.

Sum Up

The default management tool for KVM environment is virsh command. You can use it to attach or detach virtual disk temporarily or perpetually. You can also use the graphical management tool virt-manager to attach virtual disk step by step.

It is recommended to use Vinchin Backup & Recovery to protect your virtual environment. Don’t miss the free trial.

Share on:

Categories: VM Tips
Free Trial Now close-icon