Migrating from file to lvm storage

Steps to take for migrating from file to lvm storage. This tutorial will make use of virsh from libvirt. Tested on Ubuntu 14.04 (Trusty Tahr).

Before we begin

I assume you already have an LVM storage pool created and active. You can list your storage pools with this command.

root@flipflop /home/data # virsh pool-list
 Name                 State      Autostart 
 mylvmpool            active     yes 

On my system, only 1 storage book is started. It’s called ‘mylvmpool’. Click here for steps how to create an LVM storage pool. Use this command to display more detailed information about the storage pool.

root@flipflop /home/data # virsh pool-info mylvmpool
Name:           mylvmpool
UUID:           cf2438fa-eeb7-457d-ae48-7c05f3cc8dc8
State:          running
Persistent:     yes
Autostart:      yes
Capacity:       2.73 TiB
Allocation:     569.81 GiB
Available:      2.17 TiB

Migrating from file to lvm storage

File format and size

The file format should be “raw”. Please check that before continuing with the next steps. If different, first convert using ‘qemu-img’. You can check the file format like this.

root@flipflop /home/data # qemu-img info dns-cache.img 
image: dns-cache.img
file format: raw
virtual size: 7.8G (8388608000 bytes)
disk size: 7.8G

This command also return the file size in bytes. We need that size for the next step.

Creating the lvm storage volume

root@flipflop /home/data # virsh vol-create-as mylvmpool dns-cache 8388608000
Vol dns-cache created

The new volume is now created. We can use virsh to get info about it.

root@flipflop /home/data # virsh vol-info dns-cache --pool mylvmpool
Name:           dns-cache
Type:           block
Capacity:       7.81 GiB
Allocation:     7.81 GiB

Or simply use lvdisplay.

root@flipflop /home/data # lvdisplay /dev/vg0/dns-cache 
  --- Logical volume ---
  LV Path                /dev/vg0/dns-cache
  LV Name                dns-cache
  VG Name                vg0
  LV UUID                fyDAEF-uZIh-fSgh-lJOK-TyW9-x1Jp-IVto1H
  LV Write Access        read/write
  LV Creation host, time flipflop, 2015-03-02 16:48:28 +0100
  LV Status              available
  # open                 0
  LV Size                7.81 GiB
  Current LE             2000
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:4

Copying all data

Next step will copy all data from the old file to the new block device. Before copying the data, it’s best to shutdown the virtual machine to avoid loss of data.

root@flipflop /home/data # dd if=dns-cache.img of=/dev/vg0/dns-cache
16384000+0 records in
16384000+0 records out
8388608000 bytes (8.4 GB) copied, 138.953 s, 60.4 MB/s
root@flipflop /home/data #

Please note that this step takes a while and no progress indicator is displayed. Doublecheck if no errors are printed at when the command finishes.

Adapting the configuration file

Editing the configuration file can be done with this command.

virsh edit <machinename>

Now you have to look for the disk section.
Old situation:

    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/images/dns-cache.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>

New situation:

    <disk type='block' device='disk'>
      <driver name='qemu' type='raw'/>
      <source dev='/dev/vg0/dns-cache'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>

After this step, you should be able to start your virtual machine again. If the machine boots successfully, you can remove the old disk file.


If you have any question after reading this article, please use the contact form below to reach me.

2 thoughts on “Migrating from file to lvm storage

  1. Der Osterhase

    Many thanks for this excellent description. Exaclty what I looked for.

    Since you mentioned no progress indicator exists for dd, there is one however: dd if=… of=… status=progress


Leave a Reply

Your email address will not be published. Required fields are marked *