Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
home_server:home_server_setup:kvm [2022-10-01 Sat wk39 20:18] – [How to mount VM virtual disk on KVM hypervisor] baumkphome_server:home_server_setup:kvm [2023-07-29 Sat wk30 15:25] – [KVM Offline Backup] baumkp
Line 6: Line 6:
   *[[https://linuxhint.com/install_kvm_debian_10/|Installing KVM on Debian 10]]   *[[https://linuxhint.com/install_kvm_debian_10/|Installing KVM on Debian 10]]
   *[[https://computingforgeeks.com/how-to-install-kvm-virtualization-on-debian/|How To Install KVM Hypervisor on Debian 11|10]]   *[[https://computingforgeeks.com/how-to-install-kvm-virtualization-on-debian/|How To Install KVM Hypervisor on Debian 11|10]]
-Basically to install the KVM Hypervisor: ''sudo apt install qemu qemu-kvm qemu-system qemu-utils libvirt-clients libvirt-daemon-system virtinst bridge-utils'' bridge-utils is optional.+  *[[https://www.cyberciti.biz/faq/install-kvm-server-debian-linux-9-headless-server/|How to install KVM server on Debian 9/10 Headless Server]] 
 +Basically to install the KVM Hypervisor: ''sudo apt install qemu-kvm qemu-system qemu-utils libvirt-clients libvirt-daemon-system virtinst bridge-utils'' bridge-utils is optional. 
 + 
 +  *''genisoimage'' is a package to create iso images 
 +  *''libguestfs-tools'' is a library to access and modify VM disk images 
 +  *''libosinfo-bin'' is a library with guest operating systems information to assist VM creation
  
 Use the built-in clone facility: ''sudo virt-clone %%--%%connect=qemu:%%//%%example.com/system -o this-vm -n that-vm %%--%%auto-clone''. Use the built-in clone facility: ''sudo virt-clone %%--%%connect=qemu:%%//%%example.com/system -o this-vm -n that-vm %%--%%auto-clone''.
Line 66: Line 71:
     - Create a snapshot of the VM and direct all changes to the snapshot allowing safe backup of main VM file     - Create a snapshot of the VM and direct all changes to the snapshot allowing safe backup of main VM file
     - Active back commit the snapshot and verify back commit worked     - Active back commit the snapshot and verify back commit worked
 +
 +====KVM Offline Backup====
 +<fc #ff0000>Note this only works on VMs that are shut down</fc>
 +  -''sudo virsh list --all'' to list all KVM virtual machines.
 +  -''%%sudo virsh dumpxml VM_name | grep -i "source file"%%'' to list the VM source file location noted in the VM XML file.
 +  -''sudo virsh dumpxml vm-name > /path/to/xm_file.xml'' to archive/backup the VM XML definition file.
 +  -''sudo cp -p /working/path/VM_image.qcow2 /path/to/'' to archive/move the VM file.
 +  -''%%sudo virsh undefine vm-name --remove-all-storage%%'' to undefine the VM and remove its storage. <color #ed1c24>(Be careful with this one!)</color>
 +  -''sudo virsh define --file <path-to-xml-file>'' to import (define) a VM from an XML file.
 +
 +
 +References:
 +  *[[https://ostechnix.com/export-import-kvm-virtual-machines-linux/|How To Export And Import KVM Virtual Machines In Linux]]
 ====kvm back-up links==== ====kvm back-up links====
   *[[https://schh.medium.com/backup-and-restore-kvm-vms-21c049e707c1|Backup and Restore KVM Vms]]   *[[https://schh.medium.com/backup-and-restore-kvm-vms-21c049e707c1|Backup and Restore KVM Vms]]
Line 105: Line 123:
 How to change the amount of disk space assigned to a KVM  *[[https://fatmin.com/2016/12/20/how-to-resize-a-qcow2-image-and-filesystem-with-virt-resize/|How to Resize a qcow2 Image and Filesystem with Virt-Resize]] How to change the amount of disk space assigned to a KVM  *[[https://fatmin.com/2016/12/20/how-to-resize-a-qcow2-image-and-filesystem-with-virt-resize/|How to Resize a qcow2 Image and Filesystem with Virt-Resize]]
   *First turn off the virtual machine to be checked   *First turn off the virtual machine to be checked
-  *Next find the file location of the firtual machine to be checked+  *Next find the file location of the virtual machine to be checked
     *Next query the file: ''sudo qemu-img info /path_vm/vm_name.img''     *Next query the file: ''sudo qemu-img info /path_vm/vm_name.img''
     *Next increase the allowed size of the vm disk: ''sudo qemu-img resize /path_vm/vm_name.img +20G''     *Next increase the allowed size of the vm disk: ''sudo qemu-img resize /path_vm/vm_name.img +20G''
Line 124: Line 142:
  
 =====How to mount VM virtual disk on KVM hypervisor===== =====How to mount VM virtual disk on KVM hypervisor=====
-There seem to be a number of methods to do this.+There seem to be a number of methods to do this.  
 + 
 +**<fc #ff0000>In all cases the VM (Virtual Machine) must be in shutdown state.</fc>** 
  
 ====libguestfs method==== ====libguestfs method====
Line 140: Line 161:
 ++++ ++++
 ====Mount a qcow2 image directly==== ====Mount a qcow2 image directly====
-  * ''%%sudo virsh list --all%%'' : Virsh List all domainsthe ''%%--all%%'' option ensure inactive domains are listed +To check if already installed or not: ''%%sudo apt list --installed | grep qemu-utils%%'' 
-    * ''sudo virsh dominfo domain-name'' : List information of domain ''domain-name'' or domain ''domain-id'' +To install ''sudo apt install qemu-utils'' 
-    * ''sudo virsh domiflist domain-name'' : List network interface(s) information of domain ''domain-name'' or domain ''domain-id'' + 
-    * ''sudo virsh domblklist domain-name'' to locate the file of an existing VM of domain ''domain-name'' or domain ''domain-id'' +The nbd (network block device) kernel module needs to be loaded to mount qcow2 images.  
-  * ''sudo virsh start domain-name'' : To start a virtual machine of domain ''domain-name'' +  *''sudo modprobe nbd max_part=16'' will load it with support for 8 block devices. (If more blocks are required use 1632 or 64 as required.)  
-  * ''sudo virsh shutdown domain-name'' : To shutdown a VM of ''domain-name'' or ''domain-id'' (initiate shutdown now in VM, could take some time to actually stop)+  *Check VMs ''%%sudo virsh list --all%%''.   
 +    *If the VM to be mounted is active shutdown with ''sudo virsh shutdown <domain-name or ID>''.  
 +  *Use ''sudo virsh domblklist <domain-name>'' to get the full path and file name to the VM image file.  
 +  *Use ''ls -l /dev/nbd*'' to check if any devices named nbd have already been defined.  
 +  *Use ''sudo qemu-nbd -c /dev/nbd0 <image.qcow2>'' to create the block devices on the VM. 
 +  *''sudo fdisk /dev/nbd0 -l'' will list the available partitions in /dev/nbd0. 
 +  *Use ''sudo partprobe /dev/nbd0'' to update the kernel device list.  
 +  *Use ''ls -l /dev/nbd0*'' to see available partitions on image.  
 +  *If the image partitions are not managed by LVM they can be mounted directly.  
 +  *If a mount point does not already exist, create: ''sudo mkdir /mnt/image''.   
 +  *The device can then be mounted ''sudo mount /dev/nbd0p1 /mnt/image'' or ''sudo mount -r /dev/nbd0p1 /mnt/image'' to mount read only or ''sudo mount -rw /dev/nbd0p1 /mnt/image'' to mount explicitly read-write. 
 + 
 +When complete clean-up with the following commands.   
 +  *Unmount the block device with ''sudo umount /mnt/image'' 
 +  *Delete the network block device with ''sudo qemu-nbd -d /dev/nbd0''
 +  *If required the VM can be restarted with ''sudo virsh start <domain-name>'' 
 + 
 +===Mount qcow2 image with LVM===
  
 +Links:
 +  *[[https://docs.openstack.org/image-guide/modify-images.html|Modify images]]
 +  *[[https://medium.com/@aysadx/linux-nbd-introduction-to-linux-network-block-devices-143365f1901b|linux NBD: Introduction to Linux Network Block Devices]]
 +  *[[http://alexeytorkhov.blogspot.com/2009/09/mounting-raw-and-qcow2-vm-disk-images.html|Mounting raw and qcow2 VM disk images]]
 +  *[[https://www.xmodulo.com/mount-qcow2-disk-image-linux.html|How to mount qcow2 disk image on Linux]]
 =====KVM Guest Corrupted - Recovery Options and Related===== =====KVM Guest Corrupted - Recovery Options and Related=====