Avoid write on a mounted loopback device to be propagated

Solution 1:

Sorry I missed this question in the past... Try using a "Fanout" filesystem which will allow you to mount an "image" and then overlay a writable file-system on-top of the non-writable image. All writes to the drive will result in the "modifications" being done to the overlaying filesystem instead of the base-iamge. mini-fo is one such filesystem which will allow you to do this.

Solution 2:

If you are willing to reconsider a change in your architecture, you should be able to get something like that using a logical volume instead of a loopback file.

If you want to give it a try, first convert your file to a logical volume:

lvcreate -n original -L 20G vg0 # assumes 20G size and a valid 'vg0' volume group
dd if=/root/original.loopback of=/dev/vg0/original

Then create a snapshot:

lvcreate -s -n volatilecopy -L 2G /dev/vg0/original

Now you can mount /dev/vg0/volatilecopy and "write" up to 2GB on it without running into problems, yet /dev/vg0/original will remain unchanged.

Afterwards:

lvremove /dev/vg0/volatilecopy
lvcreate -s -n volatilecopy -L 2G /dev/vg0/original

Gives you a clean state again.

Advantages over copying the file over and over again: this uses less space (you only need extra space for the writes) and is quicker.