2

The data being displayed on a loop device mount is incorrect when the data in the underlying file is changed.

Example:

$ mkdir drv
$ dd if=/dev/zero of=data.ext4 bs=1M count=10
$ mkfs.ext4 data.ext4
$ losetup /dev/loop0 data.ext4
$ mount /dev/loop0 drv
$ echo "abcdefg" > drv/test.txt
$ cat drv/test.txt
abcdefg
$ hexdump -C data.ext4 | grep abcd
00169000  61 62 63 64 65 66 67 0a  00 00 00 00 00 00 00 00  |abcdefg.........|
$ sed -i 's/abcd/zzzz/g' data.ext4
$ hexdump -C data.ext4 | grep zzzz
00169000  7a 7a 7a 7a 65 66 67 0a  00 00 00 00 00 00 00 00  |zzzzefg.........|
$ cat drv/test.txt 
abcdefg
$ hexdump -C data.ext4 | grep abcd
$

The string "abcd" is no longer in the backing data.ext4 file, yet when the test.txt file is cat, abcd is still seen. How can I sync the loop to properly display what the backing file truly has?

Note: --direct-io=on makes no difference to the above test.

3
  • In once case (echo & cat), you are changing a file on a filesystem. In the other case (sed), you are writing directly to the disk device. Maybe there's a caching problem, or you aren't writing to the same place you think you are. You can try to manually flush the disk cashes (ie "sync;sync;sync") and then check it. Commented Dec 22, 2020 at 21:13
  • the usage of hexdump is to show that I am changing with sed what I expect I am changing. dropping the loop then redoing the loop will show the expected zzzzefg when test.txt is cat. There is absolutely some sort of caching going on, thats what I need to sync. I need the cache for /dev/loop0 to have the data from data.ext4 read into it somehow. Commented Dec 22, 2020 at 21:28
  • When you drop the loop (ie umount), it is flushing the cache to that device. Did you try running "sync;sync;sync" after your sed to see if the changed data shows up? Commented Dec 22, 2020 at 23:06

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.