To add a disk image to a KVM virtual machine with the virsh command, you can use the attach-disk command.
Here are the steps to follow to add a disk image to a KVM virtual machine with virsh:
Make sure that the disk image file is available on the host system, and that the virtual machine is powered off.
Determine the name of the virtual machine that you want to attach the disk image to. You can list the available virtual machines with the following command:
virsh list --all
attach-disk command to attach the disk image to the virtual machine. For example, to attach the disk image file disk.img to the virtual machine "vm1" as the first virtual disk, you can use the following command:virsh attach-disk vm1 disk.img vda --cache none --persistent
This will attach the disk image file to the virtual machine as the first virtual disk (vda). The --cache none option disables write caching for the disk, which can improve performance for some workloads. The --persistent option makes the change permanent, so that it will persist across reboots of the virtual machine.
start command. For example:virsh start vm1
The disk image will now be available to the virtual machine as a new virtual disk. You can access it and use it as you would any other disk in the virtual machine.
Note: The
virshcommand has many options and features available for managing KVM virtual machines. Consult the documentation and online resources for more information on the various options and features available withvirsh. It is recommended to usevirshwith caution, as improper use can cause data loss or disrupt the operation of the virtual machine.