Basic KVM operations: Difference between revisions

From Newroco Tech Docs
Jump to navigationJump to search
Line 169: Line 169:


If the disk will be used for data storage then reduce the space reserved for root operations
If the disk will be used for data storage then reduce the space reserved for root operations
</pre>tune2fs -m 1 /dev/vda1</pre>
<pre>tune2fs -m 1 /dev/vda1</pre>


Find the UUID of the partition (the groups of numbers/letters before the arrow -> )
Find the UUID of the partition (the groups of numbers/letters before the arrow -> )

Revision as of 07:40, 4 January 2018

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

Solution 1

# 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)

cp /proc/3649/fd/18 /var/lib/vz/images/101/vm-101-disk-1.raw

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

Solution 2

To conver the image from raw to qcow2

qemu-img convert -p -f qcow2 -O raw /proc/2850/fd/19 vm-100-disk-1.raw

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


List all the VMs with the autostart option activated

# sudo virsh list --autostart

Snapshoot operations

Create snapshoot

virsh shutdown vm_name
virsh snapshot-create-as --domain vm_name \
--name "snapshot_name" \
--description "Snapshpot"

Restore snapshoot

virsh snapshot-list --domain vm_name
virsh snapshot-revert --domain vm_name snapshot_name

Delete snapshoot

virsh snapshot-delete --domain vm_name --snapshotname snapshot_name_to_be_deleted

Adding a disk to VM

On the host, create an image

qemu-img create -f qcow2 /path/to/image.qcow2 10G

Attach the disk to the VM (check before if vda is used; if yes use vdb, vdc, etc.)

virsh shutdown vm-name
virsh attach-disk vm-name --source /path/to/image.qcow2 --target vda --persistent
virsh start vm-name

On the VM, check disk vda has been attached

# ls -l /dev/disk/by-path/
lrwxrwxrwx 1 root root  9 Oct 23 13:44 virtio-pci-0000:00:05.0 -> ../../vda

Create a partition with parted or other tool

parted /dev/vda

If it shows that the disk size is only a few KB, it's an error and it can be resolved by converting the image, detach the image and attach the converted one

qemu-img convert -O qcow2 input-image.qcow2 output-image.qcow2

Format it

mkfs.ext4 /dev/vda1

If the disk will be used for data storage then reduce the space reserved for root operations

tune2fs -m 1 /dev/vda1

Find the UUID of the partition (the groups of numbers/letters before the arrow -> )

#ls -l /dev/disk/by-uuid/
lrwxrwxrwx 1 root root 10 Oct 23 13:45 936de7fd-084f-4456-898f-483341df1cf1 -> ../../vda1

Mount the partition using the UUID

mount /dev/disk/by-uuid/youruuid /path/to/mymountpoint

Append a line to /etc/fstab so your mount can survive a reboot

UUID=youruuid       /path/to/mymountpoint ext4    defaults        0       0

Detach disk from a VM

virsh detach-disk vm-name vda --config

Converting

Info about image

qemu-img info image.vmdk 

From .vdi to .qcow2

qemu-img convert -f vdi -O qcow2 from.vdi to.qcow2

From qcow2 to .vdi

qemu-img convert -O vdi from.qcow2 to.vdi

From .img to .qcow2

qemu-img convert -f raw from.img -O qcow2 to.qcow2

From .qcow2 to .raw

qemu-img convert -O raw from.qcow2 to.raw