The idea is to create the virtual machine on a LV (Logical Volume) and afterwards, by using a feature called snapshot LV, to get an exact copy of that volume.
For my tests, I will utilize Kubuntu 11.04 (64 bits). First of all, I am going to set a LV so as to make up a virtual machine on it. Remember that if you want to set up a LV on a partition, this one must be marked as Linux LVM.
javi@kubuntu:~$ sudo fdisk -l
...
/dev/sdb1 7945 8992 8417280 8e Linux LVM
In order to create a LV on a partition, we can follow the next steps. At the end, we will format the volume as ext4 (or another kind of format that you prefer).
javi@kubuntu:~$ pvcreate /dev/sdb1
javi@kubuntu:~$ vgcreate VolGroup00 /dev/sdb1
javi@kubuntu:~$ sudo lvcreate -n LogVol00 -L 2G VolGroup00
javi@kubuntu:~$ sudo mkfs.ext4 /dev/VolGroup00/LogVol00
The following figure shows a stage of the Virtual Machine Manager wizard, specifically the part where you have to pick the storage out. I have choosen the previous LV created. On this LV, I will make a new virtual machine (UbuntuServer_10.10, 2 GB virtual hard disk) which will be used before for the snapshotting.
At this point, we are ready to take a snapshot through LVM. Really easy.
javi@kubuntu:~$ sudo lvcreate -n UbuntuServer_10.10-22052011 -L 512M -s /dev/VolGroup00/LogVol00
By taking the snapshot with the preceding command, a new LV is created on the same VG (Volume Group) that LogVol01, that is to say, VolGroup00 in my case. For this reason, we must make sure that there is enough free space on the VG.
You have to take into account how a snapshot works. When you take a snapshot, the original virtual disk (LogVol01) is frozen and all changes are stored into the snapshot (UbuntuServer_10.10-22052011). So as to demonstrate it, we are going to display the LVs state once the snapshot has been taken.
javi@kubuntu:~$ sudo lvdisplay
--- Logical volume ---
LV Name /dev/VolGroup00/LogVol00
VG Name VolGroup00
LV UUID Ag34Yq-990o-eyei-ClnF-OjCA-QltD-BrI39w
LV Write Access read/write
LV snapshot status source of
/dev/VolGroup00/UbuntuServer_10.10-22052011 [active]
LV Status available
# open 0
LV Size 2,00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0
--- Logical volume ---
LV Name /dev/VolGroup00/UbuntuServer_10.10-22052011
VG Name VolGroup00
LV UUID PSpJH0-ANEu-HW4W-YnLj-00Ta-g2Gz-WedRqi
LV Write Access read/write
LV snapshot status active destination for /dev/VolGroup00/LogVol00
LV Status available
# open 0
LV Size 2,00 GiB
Current LE 512
COW-table size 512,00 MiB
COW-table LE 128
Allocated to snapshot 0,00%
Snapshot chunk size 4,00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:1
The previous output indicates us that we can save up to 512 MB of data for our snapshot (COW-table size), and 0% of that space is being used (Allocated to snapshot). Then we are going to make a little change inside the virtual machine, for example to create a new file of 256 MB.
javi@ubuntu-server:~$ dd if=/dev/zero of=file bs=1M count=256
Now if we take a look at the LV state again, we can see that the 50% of the snapshot has already been allocated.
javi@kubuntu:~$ sudo lvdisplay /dev/VolGroup00/UbuntuServer_10.10-22052011
--- Logical volume ---
LV Name /dev/VolGroup00/UbuntuServer_10.10-22052011
VG Name VolGroup00
LV UUID PSpJH0-ANEu-HW4W-YnLj-00Ta-g2Gz-WedRqi
LV Write Access read/write
LV snapshot status active destination for /dev/VolGroup00/LogVol00
LV Status available
# open 0
LV Size 2,00 GiB
Current LE 512
COW-table size 512,00 MiB
COW-table LE 128
Allocated to snapshot 50,45%
Snapshot chunk size 4,00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:1
And finally, if we want to bring back a specific snapshot, we must execute the following sentence.
javi@kubuntu:~$ sudo lvconvert --merge VolGroup00/UbuntuServer_10.10-22052011