0

For a OS project, I am creating a ext2 file system image and mounting it. This means that I am writing out a 1 MB file with block information and then using it as the mount target.

For example, assume there is a file called base.img:

fsck.ext2 base.img # checking my file system 
mkdir mnt
sudo mount -o loop base.img mnt

After the mount is successful, what is happening internally? From my understanding, my base.img simply initializes the image correctly. Internally, these are my questions:

  • Is there now a drive partition with this file system?
  • How are changes to the file system managed?

For the second, from my understanding, there exists a "mount table" and perhaps an ext2 module that Linux will use for further changes. Will these changes be reflected in base.img or is there a new disk partition for it now?

My underlying question here is: how does this all work?

user129393192
  • 797
  • 1
  • 8

2 Answers2

1

Linux uses block devices to hold data (most of the time those are disks with a partition table at the beginning and multiple partitions, each with a filesystem that then are mounted individually), but any block device will do.

The loop option you are using created a block device (usually in /dev/loop0) and associated it with the file you are using as an image.

For more details see loop(4)

Carlo Arenas
  • 55
  • 1
  • 4
1

Ext2 is a block-based filesystem, i.e. it needs a "block device".

A disk is a block device (e.g. /dev/sda). A disk partition within a disk is also a block device (e.g. /dev/sda1). Linux can also treat memory as block device (/dev/ram0).
Essentially anything that can be seen as contiguous bytes that are addressable by offset can be seen as a block device.

And that includes files, like base.img, which mount will map to block device such as /dev/loop0.

Ext2 doesn't care about whether the block device is a disk, a partition, memory, or a file, it just writes things in certain offsets.
(that's a simplification but it's good enough).

So to answer your questions:

Is there now a drive partition with this file system?

No. There's a new block device (/dev/loop0), but not a new partition - partition are a something specific to disks.

How are changes to the file system managed?
...
how does this all work?

  • You write to mnt, i.e. "write data A to /mnt/some/file at offset X".
  • The writes go to ext2.
  • ext2 computes what data needs to be written at which block device offset, writes to block device, i.e. "write data B to /dev/loop0 at offset Y".
  • The loopback driver receives this request and forwards them to the filesystem that hosts base.img (which may or may not be ext2), i.e. "write data B to base.img at offset Y".
  • That filesystem does its own computation and creates the request to its own block device, i.e. "write data C to /dev/sda1 at offset Z".
  • The disk driver receives this request and writes to the disk.

Will these changes be reflected in base.img or is there a new disk partition for it now?

These changes will be reflected in base.img, by means of ext2 writing to /dev/loop0.

root
  • 5,528
  • 1
  • 7
  • 15
  • Thanks for the response. I got a bit confused when the loopback driver came in. I tried to take a look [here](https://github.com/torvalds/linux/blob/master/drivers/net/loopback.c). Why does there need to be multiple writes, once to `/dev/loop0` and once to `/dev/sda1` for `base.img`? Your last line states that its reflected in `base.img` by means of writing to `/dev/loop0`. Where exactly is the connection there? – user129393192 May 20 '23 at 18:20
  • A loop device emulates a block device using a file. When you write to the loop device, your write is translated to one or more writes to base.img which is a regular file on disk. – stark May 21 '23 at 11:00