Skip to content

Instantly share code, notes, and snippets.

@oBuTr471b
Created January 9, 2021 18:54
Show Gist options
  • Save oBuTr471b/d4112accc2a8681d221827219a03d5c6 to your computer and use it in GitHub Desktop.
Save oBuTr471b/d4112accc2a8681d221827219a03d5c6 to your computer and use it in GitHub Desktop.
# In The Name Of Allah .
# -- --- ---- -- ----- -
# Wed Nov 11 05:56:35 2020
# Written By : zer0err0r .
# ======= == = ========= =
# 19, Importing And Exporting VMs .
= There is the VM_live_migration, And there is the normal importing and exporting method .
= IDK about VM_live_migration yet, So, I will explain the normal importing and exporting method .
# To export :
$ virsh dumpxml centos1 > centos1.xml
$ virsh vol-dumpxml --pool my_pool --vol centos1.qcow2 > centos1.qcow2.xml
= Then copy the [.qcow2] file .
# Exporting pools :
$ virsh pool-dumpxml my_pool > my_pool.xml
# Exporting network :
$ virsh net-dumpxml NETWORK_NAME > NETWORK_NAME.xml
# Tips and Tricks :
= If you have a lot of VMs, And you want to backup all of them, Then execute :
$ vim dump_all_xml.bash
if [ ! -d ~/xml_files ]; then mkdir ~/xml_files;fi
for item in $(virsh --connect qemu:///system list --all | awk '{print $2}' | grep -v Name); do
virsh --connect qemu:///system dumpxml --domain $item > ~/xml_files/${item}.xml
done
$ bash dump_all_xml.bash
= The script will be included in the description .
# And BTW :
= You can get all your VMs [.xml] files from [/etc/libvirt/qemu/] too ,
= I just explained here what I applied before and worked with me :) .
# To import :
# In case of importing into a standalone kvm server :
# First :
= If you are not going to put the [.qcow2] files in the same place, Then modify your [.xml] files .
= i.e., I want to change from the old [/var/lib/libvirt/images] into [/home/zer0err0r/kvm], So, Execute :
$ grep -l '/var/lib/libvirt/images' *.xml | xargs sed -i 's/\/var\/lib\/libvirt\/images/\/home\/zer0err0r\/kvm/g'
= And this is the affected line :
<source file='/home/zer0err0r/kvm/...'/>
# Then :
= Then put the [.qcow2] file in the directory that you will save it at .
# Finally :
= Define the VM using the following command ,
$ virsh define centos1.xml
# Notice :
= If you have a lot of VMs, Then use the previous [grep] command to modify all of them .
= Then use the following command to [define] all of them ,
$ for file in $(ls *.xml); do virsh --connect 'qemu:///system' define $file; done
# In case of importing into a kvm with a storage pool :
= The pool here is already defined, If not, Then define a new pool or import the exported pool .
= To import use the following ,
$ virsh pool-define my_pool.xml
# Also :
= Define your network settings using the following command ,
$ virsh net-define default.xml
# Then :
= If you are not going to put the [.qcow2] files in the same place, Then modify your [.xml] files .
= i.e., I want to change from the old [/var/lib/libvirt/images] into [/home/zer0err0r/kvm], So, Execute :
$ grep -l '/var/lib/libvirt/images' *.xml | xargs sed -i 's/\/var\/lib\/libvirt\/images/\/home\/zer0err0r\/kvm/g'
= And this is the affected line :
<source file='/home/zer0err0r/kvm/...'/>
# Then :
= Modify the path of the volume [.xml] file, In this case [centos1.qcow2.xml], Then execute the following ,
$ virsh vol-create --pool my_pool centos1.qcow2.xml
# Attention :
= It wont work with you if the file [centos1.qcow2] is already inside [~/kvm/] dir .
= You need to execute the previous command before putting the file inside the [~/kvm/] dir .
# Then :
= Put the [.qcow2] file inside the pool directory .
# Finally :
$ virsh define centos1.xml
$ virsh start centos1
= Now it should work with you .
# Again :
# In case of standalone kvm server :
= Modify the VM.xml file to the new path .
= Put the [.qcow2] file inside the new path dir .
$ virsh define VM.xml
# In case of a storage pool kvm server :
= Modify the [VM.xml] file to the new path .
= Modify the [VM.qcow2.xml] file to the new path .
$ virsh vol-create --pool my_pool VM.qcow2.xml
= Put the [.qcow2] file inside the new path dir .
$ virsh define VM.xml
# ===== == ==== ======== =
# Peace Be Up0n Muhammed .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment