How to restore deleted KVM files under Linux

If you have accidently deleted the files of your virtual machines while they are still running, what can you do? that was my personal experience with about 15 virtual servers !
1 answer

restoring KVM Vm Ware deleted images

don't restart or stop the VM not at all, by doing this, you will lose your image files and this will lead to a disaster.
Linux mark the files as deleted, but they are still exist, I will show you how to restore the files.

1- Look for the pid of the running virtual machine you just deleted (for example a virtual windows server)

lsof | grep -i qcow | grep deleted
kvm 31127 root 9u REG 9,2 32212254720 2985316 /home/root/kvm/windows_2003/win2k3.qcow2 (deleted)

2-With the pid (31127) you can find the filediscriptor in /proc.

ls -al /proc/31127/fd | grep deleted
lrwx------ 1 root root 64 Nov 6 14:51 9 -> /home/root/kvm/windows_2003/win2k3.qcow2 (deleted)

3-Now suspend your virtual machine in order to prevent data loss

virsh suspend winserver

4-Restore the files to its original path and name.

cat /proc/31127/fd/9 > /home/root/kvm/windows_2003/win2k3.qcow2

5-Now you have to hard-reset your virtual machine.

virsh destroy winserver && virsh start winserver

Taggings: