Convert your disk file from raw to qcow2

Use Virtual machines, such as within KVM, then you may be running raw disk images.

Converting them to Qcow2 will give you some advantages, such as the ability to snapshot your VM. Here we will show you how to:

  •     Check your disk file's format to see if it is raw or qcow2
  •     Convert your disk file from raw to qcow2

Check Your Format

You can test your disk file with the following command:

qemu-img info [disk filename] 

If it is a raw image format, you should get output similar to below:

image: images.programster.org.img 
file format: raw 
virtual size: 30G (32217432064 bytes) 
disk size: 30G 

If it is a qcow2 image format, then it will output something similar to:

image: images.programster.org.qcow2 
file format: qcow2 
virtual size: 30G (32217432064 bytes) 
disk size: 4.8G 
cluster_size: 65536 
Format specific information: 
    compat: 1.1
    lazy refcounts: false

 

Convert Raw To Qcow2

Simply enter the following command:

qemu-img convert -p -f raw -O qcow2 [input filename] [output filename] 

After the operation completes, you may wish to delete the original input file. You will need to update any configs that utilize the new image, such as through sudo virsh edit [VM ID] for KVM. E.g. change the type from raw to qcow2 and update the path.

 

  • The convert option tells qemu-img that we want to copy an existing image file to a new image file.
  • The -p option tells the qemu-img tool that I want to know about the progress of the copy operation.
  • The -f option specifies the format of the original input file.
  • The [input filename] parameter is the disk name of the input file.
  • The -O option specifies the format that we want to use for the output file.
  • The [output filename] parameter is the disk name of the output

 

Credits|Credits

KVM (virt-manager) take a snapshot of qcow2 disk.

Take A Snapshot

 To list a kvm vm on Linux based server [VM ID], more info

virsh list 

If your KVM guests are now running with Qcow2 disk images, you can take an internal snapshot of the guest with the following command:

virsh snapshot-create [VM ID]  

Alternatively, you can create a file with the following contents

<domainsnapshot>
    <name>Name for the snapshot</name>
    <description>Description for the snapshot</description>
</domainsnapshot>

Then pass it to the snapshotting command.

virsh snapshot-create [VM ID] [filepath]  

You can take snapshots of guests whilst they are running. Whilst the snapshot is being taken, the guest will be "paused". The "state" of the guest is also saved.

List Snapshots
One can list a virtual machine's snapshots with

sudo virsh snapshot-list [VM ID]  

Which will output something like:

 Name                 Creation Time             State
————————————————————
 1422179589           2015-01-25 09:53:09 +0000 running

Restore Snapshot

If you want to "revert", "go to", or "restore" a previous snapshot, then execute the following command:

virsh snapshot-revert [VM ID] [Snapshot Name]  

Delete Snapshot

virsh snapshot-delete [VM ID] [Snapshot Name] 

Credits | Tips

Virtualizando uma máquina física

Virtualizar máquinas fisicas podem resolver uma série de problemas, como: migrar servidores, ter mais controlo sobre backups e restaurar máquina após incidente.

Para tal é necessária alguma preparação:

  • Uma unidade externa grande ou maior do que a unidade a ser virtualizada (formatada em NTFS para evitar  limites 4GB em partições FAT32 4GB)
  • A ferramenta dd
  • Uma distro linux ou Hiren's BootCD

Deve excluir ou mover todos os arquivos desnecessários (como imagens, filmes, música, etc.) da unidade física, bem como certificar de ter todas as actualizações estão instaladas.

1. Ligar unidade externa ao computador que se pretende converter
2. Iniciar o computador através do Hiren's BootCD (CD / Pen)
2.a. No terminal:
3.

lsblk

(listar as unidades)
4. Montar a unidade externa (x – a letra que identifica a unidade externa)

 mount -t ntfs-3g /dev/sdx /media/media/EXTERNAL_DRIVE 

5. Criar o arquivo de imagem. O comando para criar isso é:

dd if=/dev/sdx of=/media/EXTERNAL_DRIVE/sdx.img bs=1024

6. Converter o arquivo *.img numa imagem pronta após remover a unidade externa
6.a. Qemu-ready image:

qemu-img convert -f raw sda.img -O qcow sda-qcow.img

6.b. VirtualBox image:

VBoxManage convertdd input.img output.vdi

7. Criar a maquina virtual e apontar para a imagem criada anteriormente