In our previous article of Linux KVM series, we explained how to Install Linux KVM and create a Guest VM.
But, once you’ve created a Guest VM, you need to know how to perform some of the routine maintenance activities on the VM.
This tutorial will explain how to perform the following Linux KVM VM activities:
First, shutdown the VM using virsh shutdown as shown below:
You can now dynamically modify the VM memory upto the 4G max limit.
Create the Domain XML file using virsh create
In this example, let us increase the memory of myRHELVM1′s VM from 2GB to 4GB.
First, shutdown the VM using virsh shutdown as shown below:
In the following example, we are creating a virtual disk image with 7GB of size. The disk images are typically located under /var/lib/libvirt/images/ directory.
Similarly to detach a disk from the guest VM, you can use the below command. But be careful to specify the correct vd* otherwise you may end-up removing wrong device.
Use the virsh dumpxml file to take a backup and save the configuration information of your VM as shown below.
Now you can remove the disk img file under /var/lib/libvirt/images
But, once you’ve created a Guest VM, you need to know how to perform some of the routine maintenance activities on the VM.
This tutorial will explain how to perform the following Linux KVM VM activities:
- Add Memory to VM
- Add vCPU to VM
- Add Disk to VM
- Save VM Configuration
- Delete a VM
1. Add Memory to Virtual Machine
To add additional memory to your VM, you should do the following:- Shutdown your VM
- Edit the VM file and increase the value of maximum memory allocated to this VM
- Restart the VM
- Use virsh setmem to set the memory upto the maximum memory allocated for this VM.
First, shutdown the VM using virsh shutdown as shown below:
# virsh shutdown myRHELVM1 Domain myRHELVM1 is being shutdownNext, edit the VM using virsh edit:
# virsh edit myRHELVM1Look for the below line and change the value for memory to the following. In my example, earlier it was 2097152:
<memory unit='KiB'>4194304</memory>Please note that the above value is in KB. After making the change, save and exit:
# virsh edit myRHELVM1 Domain myRHELVM1 XML configuration edited.Restart the VM with the updated configuration file. Now you will see the max memory increased from 2G to 4G.
You can now dynamically modify the VM memory upto the 4G max limit.
Create the Domain XML file using virsh create
# virsh create /etc/libvirt/qemu/myRHELVM1.xml Domain myRHELVM1 created from /etc/libvirt/qemu/myRHELVM1.xmlView the available Memory for this domain. As you see below, even though the maximum available memory is 4GB, this domain only has 2GB (Used memory).
# virsh dominfo myRHELVM1 | grep memory Max memory: 4194304 KiB Used memory: 2097152 KiBSet the memory for this domain to 4GB using virsh setmem as shown below:
# virsh setmem myRHELVM1 4194304Now, the following indicates that we’ve allocated 4GB (Used memory) to this domain.
# virsh dominfo myRHELVM1 | grep memory Max memory: 4194304 KiB Used memory: 4194304 KiB
2. Add VCPU to VM
To increase the virtual CPU that is allocated to the VM, do virsh edit, and change the vcpu parameter as explained below.In this example, let us increase the memory of myRHELVM1′s VM from 2GB to 4GB.
First, shutdown the VM using virsh shutdown as shown below:
# virsh shutdown myRHELVM1 Domain myRHELVM1 is being shutdownNext, edit the VM using virsh edit:
# virsh edit myRHELVM1Look for the below line and change the value for vcpu to the following. In my example, earlier it was 2.
<vcpu placement='static'>4</vcpu>Create the Domain XML file using virsh create
# virsh create /etc/libvirt/qemu/myRHELVM1.xml Domain myRHELVM1 created from /etc/libvirt/qemu/myRHELVM1.xmlView the virtual CPUs allocated to this domain as shown below. This indicates that we’ve increased the vCPU from 2 to 4.
# virsh dominfo myRHELVM1 | grep -i cpu CPU(s): 4 CPU time: 21.0s
3. Add Disk to VM
In this example, we have only two virtual disks (vda1 and vda2) on this VM.# fdisk -l | grep vd Disk /dev/vda: 10.7 GB, 10737418240 bytes /dev/vda1 * 3 1018 512000 83 Linux /dev/vda2 1018 20806 9972736 8e Linux LVMThere are two steps involved in creating and attaching a new storage device to Linux KVM guest VM:
- First, create a virtual disk image
- Attach the virtual disk image to the VM
In the following example, we are creating a virtual disk image with 7GB of size. The disk images are typically located under /var/lib/libvirt/images/ directory.
# cd /var/lib/libvirt/images/ # qemu-img create -f raw myRHELVM1-disk2.img 7G Formatting 'myRHELVM1-disk2.img', fmt=raw size=7516192768To attach the newly created disk image, use the virsh attach-disk command as shown below:
# virsh attach-disk myRHELVM1 --source /var/lib/libvirt/images/myRHELVM1-disk2.img --target vdb --persistent Disk attached successfullyThe above virsh attach-disk command has the following parameters:
- myRHELVM1 The name of the VM
- –source The full path of the source disk image. This is the one that we created using qemu-image command above. i.e: myRHELVM1-disk2.img
- –target This is the device mount point. In this example, we want to attach the given disk image as /dev/vdb. Please note that we don’t really need to specify /dev. It is enough if you just specify vdb.
- –persistent indicates that the disk that attached to the VM will be persistent.
# fdisk -l | grep vd Disk /dev/vda: 10.7 GB, 10737418240 bytes /dev/vda1 * 3 1018 512000 83 Linux /dev/vda2 1018 20806 9972736 8e Linux LVM Disk /dev/vdb: 7516 MB, 7516192768 bytesNow, you can partition the /dev/vdb device, and create multiple partitions /dev/vdb1, /dev/vdb2, etc, and mount it to the VM. Use fdisk to create the partitions as we explained earlier.
Similarly to detach a disk from the guest VM, you can use the below command. But be careful to specify the correct vd* otherwise you may end-up removing wrong device.
# virsh detach-disk myRHELVM1 vdb Disk detached successfully
4. Save Virtual Machine Configuration
If you make lot of changes to your VM, it is recommended that you save the configurations.Use the virsh dumpxml file to take a backup and save the configuration information of your VM as shown below.
# virsh dumpxml myRHELVM1 > myrhelvm1.xml # ls myrhelvm1.xml myrhelvm1.xmlOnce you have the configuration file in the XML format, you can always recreate your guest VM from this XML file, using virsh create command as shown below:
virsh create myrhelvm1.xml
5. Delete KVM Virtual Machine
If you’ve created multiple VMs for testing purpose, and like to delete them, you should do the following three steps:- Shutdown the VM
- Destroy the VM (and undefine it)
- Remove the Disk Image File
# virsh shutdown myRHELVM2 Domain myRHELVM2 is being shutdownNext, destory this VM as shown below:
# virsh destroy myRHELVM2 Domain myRHELVM2 destroyedApart from destroying it, you should also undefine the VM as shown below:
# virsh undefine myRHELVM2 Domain myRHELVM2 has been undefinedFinally, remove any disk image file that you’ve created for this VM from the /var/lib/libvirt/images directory:
Now you can remove the disk img file under /var/lib/libvirt/images
rm /var/lib/libvirt/images/myRHELVM2-disk1.img rm /var/lib/libvirt/images/myRHELVM2-disk2.img
0 comments:
Post a Comment