Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

GNU/Linux has union mount that overlays dirs. So you can mount a writeable dir on top of a read-only dir. When the writeable dir is unmounted the read-only dir is untouched.

I am looking for the same functionality for block devices - preferably with the writeable part stored in a file. So I would like to run something like:

device-setup /dev/newdevice /dev/read-only-device overlayfile

If I write to /dev/newdevice the changes should be stored in overlayfile. If I read the sectors written to, I should get the data from overlayfile. If I read sectors not written to, I should get the data from /dev/read-only-device.

Does such a tool exist?

share|improve this question
    

1 Answer 1

up vote 6 down vote accepted

You can do that with the device mapper and its snapshot target.

Basically, you'd do the same as what LVM does when you create a writable snapshot.

dev=/dev/read-only-device
ovl=/path/to/overlay.file
newdevname=newdevice
size=$(blockdev --getsize -- "$dev")

loop=$(losetup -f --show -- "$ovl")
printf '%s\n' "0 $size snapshot $dev $loop P 8" |
  dmsetup create "$newdevname"

Then you can access the overlayed device as /dev/mapper/newdevice.

If you also need access to the original device at the same time, you can do:

printf '%s\n' "0 $size snapshot-origin $dev" |
  dmsetup create originaldevice

And access it over /dev/mapper/originaldevice.

You can write to that device, then in addition to the chunks written to the snapshot device, the overlay file will contain a copy of the chunks that have been overwritten when writing to the snapshot-origin.

The overlay file can be a sparse file. (for instance, create it as truncate -s10G the-file), and doesn't have to be as large as the original device. You can tell how full it is with dmsetup status "$newdevname".

share|improve this answer
1  
A subsequent interesting question is whether the acc.umulated changes can later be merged into the original device –  imz -- Ivan Zakharyaschev Aug 3 at 15:54
1  
The subsequent answer: yes. "Device-mapper allows you … *) To merge a snapshot of a block device back into the snapshot's origin device." kernel.org/doc/Documentation/device-mapper/snapshot.txt –  imz -- Ivan Zakharyaschev Aug 3 at 17:02
    
What does the chunksize parameter to the snapshot target (8 in your case) mean? How to choose it? –  imz -- Ivan Zakharyaschev Aug 4 at 4:21
    
Another important aspect: Will the devices setup by dmsetup (and losetup) survive a reboot (so that one could write an fstab entry for them), or one will have to run the script to set them up every time? –  imz -- Ivan Zakharyaschev Aug 4 at 10:04
    
@Ivan, all those questions are easily answered by the link you pointed to –  Stéphane Chazelas Aug 4 at 10:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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