Can mmap be used to create a file which references memory subset of another file?

I’m interested in writing a program that can create two files, second file would be a "view" of first file and if modified, the first file would also be modified. Is this possible to do with mmap or at all?

I know that using mmap i can have shared memory in RAM, but I need shared memory in non-volatile memory aka hard drive. I cannot copy the first file or load it fully into RAM since I assume the file can be of very large size (GBs).

After I find how to have the second file showing memory subset of first file I plan to make 3 files, first roleplaing as container and second and third showing different subsets of the first file. Second and third file are to be formatted with filesystem so that first file container holds in it’s memory two filesystems accessible via second and third file. This I plan to accomplish by attaching the second and third files as loopback devices and mounting them.

Is this doable of am I not seeing something?

Asked By: trickingLethargy

||

You can’t do this with mmap, but you can go directly to the end of your plan and use loopback devices: losetup has an --offset option to specify where a loopback starts in the containing file, and a --sizelimit option to specify where it ends (relative to the start).

Thus with a 10GiB container, you could “map” a 2GiB file system 1GiB from the start with losetup --offset 1G --sizelimit 2G /path/to/container.

Answered By: Stephen Kitt