2

I like the ability of VirtualBox to instantly create and restore snapshots and I was wondering, if it's possible to do similar things on the host machine.

First I looked at lvm snapshots, but some digging showed that they have too many disadvantages compared to dynamically allocated images. For example:

  • They're space-consuming. If you write 10% to a logical volume and then take a snapshot, the other 90% of that volume become unusable.
  • Taking a snapshot is instant, but reverting to that snapshot is a lengthy operation.
  • A snapshot cannot be protected from automatic invalidation, if you write too much.

On Windows 7 I successfully combined the ability to attach differencing .vhd images to host with the VirtualBox GUI to manage snapshots and am looking for a similar option for linux.

1
  • 1
    A lot of the LVM snapshot issues are fixed if you use thin pool volumes, supposedly.
    – derobert
    Mar 27, 2015 at 15:57

5 Answers 5

1

To complete @Lambert's answer, there is also Btrfs (a linux file system) which allows to create and restore snapshots of subvolume of the file system. For differences between the two read this post on reddit.

Btrfs is included into the linux kernel, and you can use it a you root filesystem. It seems possible to boot on it.

Regarding to its "experimental" status, I'm using it since more than 2 years without any troubles.

1

They're space-consuming. If you write 10% to a logical volume and then take a snapshot, the other 90% of that volume become unusable.

Uh, no it isn't. The LVM snapshot presents two block devices, the origin LV (what you're snapshotting) and the snapshot LV. Only data that changes on either LV is actually written to the snapshot LV. You can mount the either LV as a regular filesystem and use 100% of either one. The only difference from the user space is that the snapshot LV doesn't appear to change (which is, of course, the point).

Taking a snapshot is instant, but reverting to that snapshot is a lengthy operation.

Depends on what operation you're performing when you say you "revert" You create snapshots like any regular LV (with the lvcreate command). You can keep a series of snapshot LV's and tell the VM to boot from one a few weeks ago which is just a matter of giving it a different path to the block device.

If you make changes to the snapshot and want to commit those changes to the origin LV you can merge the snapshot

A snapshot cannot be protected from automatic invalidation, if you write too much.

Not sure what you mean by this. Do you mean if the snapshot LV fills up because the origin or the snapshot LV was written to too much? The only problem with writing that I'm aware of is that it's possible to need to record more data to the snapshot LV than is available but that's only an issue if you made the snapshot LV smaller than its origin in an effort to save space.

4
  • "Uh, no it isn't." after snapshot created, all writes go to snapshot volume. Free space in origin remains untouched, doesn't it?
    – basin
    Mar 27, 2015 at 13:34
  • Any writes to the snapshot volume go to the snapshot LV, any writes to the origin LV causes the current extent in the origin LV to be copied into the snapshot LV. You can still write as much as you want to the origin just like you could before. The only possible downside is if you made the snapshot smaller than its origin and then wrote a bunch of stuff to the origin which would make the snapshot unusable (since it had no space for it to write the data that was changed) but even then only because you wanted to conserve disk space.
    – Bratchley
    Mar 27, 2015 at 13:41
  • "any writes to the origin LV causes the current extent in the origin LV to be copied into the snapshot LV" but that's even slower!
    – basin
    Mar 27, 2015 at 14:04
  • We seem to have lost thread here somehow, are we talking about performance now? Your original question was about space efficiency. Either way the copy does have a little bit of overhead but most snapshot mechanisms are going to have some kind of overhead and I've booted into snapshot volumes and logged into the desktop without noticing all that much of a performance hit.
    – Bratchley
    Mar 27, 2015 at 14:25
0

You might have a look at the ZFS Fuse or ZFS on Linux project. The ZFS filesystem is capable of making snapshots and clones. I don't think it is possible to boot from ZFS for Linux but you might store your data/work within a ZFS pool.

See also: http://zfsonlinux.org/ or search the Net for ZFS Fuse

0

If you don't mind rebooting, you can try Acronis True Image. It does cost money though, and I've had several experiences where it failed to create an image.

You can use dd and gzip to create an image. It's actually quite fast, and gzip usually does a good job of keeping the image relatively small.

dd if=/dev/sdX conv=sync,noerror bs=64K | pv | gzip -c  > /path/to/backup.img.gz

You can improve compression by zeroing out the unused portion of your filesystem.

sudo dd if=/dev/zero of=/path/to/filesystem/zerofill
sudo rm /path/to/filesystem/zerofill

This creates a file and fills it with zeros until it runs out of space, then deletes it.

If you want to create a full disk image, including swap, you can zero the swap like this:

sudo swapoff /dev/sda3
sudo dd if=/dev/zero of=/dev/sda3
sudo mkswap /dev/sda3   # swap must be recreated

See this for more info.

You can write a script that does all this for you. It's best to do this from a live CD, rather than running system, to avoid inconsistency.

If you're using XFS, you can use xfsdump to create incremental backups on a live system. The disadvantage is that you can only do nine incremental levels before having to start over with a full backup. It's a good choice for tape backups, though. You can also pipe it to gzip, for a smaller image.

And, of course, there's rsync. It's slower than dd, but it can do incremental backups.

I don't know of any tool that's as convenient as VBox snapshots, though.

0

As derobert suggested, I'm now using LVM's thin snapshots:

# create pool
lvcreate -L 10000M -T vg_oktestapp1/pool

# create thin vol
lvcreate -V 10000M -T vg_oktestapp1/pool -n lv_ibm

# mount
mkfs.ext4 /dev/vg_oktestapp1/lv_ibm
mount /dev/vg_oktestapp1/lv_ibm /media/ibm

# do stuff ...

# create snapshot
lvcreate -s vg_oktestapp1/lv_ibm --name lv_ibm_20150411090827

# do stuff ...

# instant restore snapshot
lvcreate -s vg_oktestapp1/lv_ibm_20150411003856 --name lv_ibm_new --setactivationskip n
umount /media/ibm
mount /dev/vg_oktestapp1/lv_ibm_new /media/ibm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.