Basic KVM operations: Difference between revisions

From Newroco Tech Docs
Jump to navigationJump to search
No edit summary
Line 91: Line 91:
</pre>
</pre>
ssh to VM and follow the steps from here: [[changing the hostname]]
ssh to VM and follow the steps from here: [[changing the hostname]]
   
   
==VM-disk deleted - resolution==
<pre>
# lsof | grep 101
...
kvm      3649      root  18u      REG              253,2 2147483648    524290  (deleted)/var/lib/vz/images/101/vm-101-disk-1.raw
...
</pre>
Important is the pid (3649) and the filediscriptor (18)
do an copy (before that, it's a good idea to stop important services with open files inside the VM, like databases)
<code.
cp /proc/3649/fd/18 /var/lib/vz/images/101/vm-101-disk-1.raw.sav
</pre>
Voila, the disk-file (of an open-VM!) is copied back.
Test the copied file with an dummy-VM before shutdown the original VM!




[[Category:KVM]]
[[Category:KVM]]

Revision as of 09:08, 2 August 2017

SEE ALSO!

List all the VMs, and see which ones are running

# sudo virsh list --all

Start a VM

You can list the VMs first, then:

# sudo virsh start <name of your vm>

Shutdown a VM properly

Note: this require the VM to have support for ACPI - at the moment it doesn't seem to work with windows VMs (seems OK to me- Jo).

# sudo virsh shutdown <name of the vm>

Shutdown a VM

Same thing as taking the plug off. Use with care.

# sudo virsh destroy <name of the vm>

Delete a VM

THIS REMOVES A VM. NO RESTORE POSSIBLE. PLEASE USE WITH SPECIAL CARE.

# sudo virsh shutdown <name of the vm>
# sudo virsh undefine <name of the vm>
# sudo rm -rf /var/lib/kvm/<name of the vm>

Back up a VM

After shutting down:

# cd /var/lib/kvm
# cp -R <name of the vm> /to/safe/place

Copy or move a VM to another host

  • Using scp, we find that a qcow2 disk will expand to its full allocated size when moved or copied to another host (eg a 20gb VM may only take up 5gb of actual disk space, but when copied it takes up the full 20gb)
  • Eduardo found a solution using rsync. Example:
 rsync /var/lib/kvm/vanilla_templates/w2k3-vanilla3/w2k3-vanilla3.qcow2 ecoria@leibniz.goo.thehumanjourney.net:/home/ecoria/test/

Second method

In case you have some permission problem you could use this method. Copy the VM image to remote server in /tmp dir:

rsync /var/lib/kvm/<vm-name>/<vm-image> <remote-server-ip>:/tmp/

On the remote server create a dir for the VM and copy it from /tmp to that dir:

mkdir /var/lib/kvm/<vm-name>
mv /tmp/<vm-image> /var/lib/kvm/<vm-name>/

Now you also need the xml file for that VM. Since it is just text you can copy it, create a file on remote server and paste it there. The xml file is found in this dir:

/etc/libvirt/qemu

After you copied the VM image and xml file you can define and start it:

virsh define /etc/libvirt/qemu/<vm-name>.xml
virsh start <vm-name>

Rename a VM

Shutdown the VM, dump the xml file and edit it.

virsh shutdown oldname
virsh dumpxml oldname > /etc/libvirt/qemu/newname.xml
vi /etc/libvirt/qemu/newname.xml

In newname.xml change "<name>oldname</name>" to "<name>newname</name>" and "

" to "

". Undefine the VM and define the new one.

virsh undefine oldname
mv /var/lib/kvm/oldname /var/lib/kvm/newname
virsh define /etc/libvirt/qemu/newname.xml
virsh start newname

ssh to VM and follow the steps from here: changing the hostname


VM-disk deleted - resolution

# lsof | grep 101
...
kvm       3649       root   18u      REG              253,2 2147483648     524290  (deleted)/var/lib/vz/images/101/vm-101-disk-1.raw
...

Important is the pid (3649) and the filediscriptor (18) do an copy (before that, it's a good idea to stop important services with open files inside the VM, like databases) <code. cp /proc/3649/fd/18 /var/lib/vz/images/101/vm-101-disk-1.raw.sav

Voila, the disk-file (of an open-VM!) is copied back.

Test the copied file with an dummy-VM before shutdown the original VM!