Fundamentals 7 min read

Understanding Linux Loop Device: Mechanism, Usage, and Implementation Details

The Linux loop device creates a virtual block device from a regular file, allowing the file’s filesystem to be mounted like a real disk; using losetup or ioctl you can attach, configure (including encryption or direct I/O), and the kernel forwards I/O through a concise, high‑performance stack ensuring compatibility and extensibility.

OPPO Kernel Craftsman
OPPO Kernel Craftsman
OPPO Kernel Craftsman
Understanding Linux Loop Device: Mechanism, Usage, and Implementation Details

In Unix-like operating systems, there is a special block device called a loop device, which is a virtual device file created from an existing regular file. If the file contains a usable filesystem, the virtual block device can be mounted like a normal disk, so loop devices are often used to mount disk image files.

After mounting a disk image that contains a filesystem, the files inside the image can be accessed through the generic filesystem interface of the operating system without any special software. This convenient method has many scenarios, such as installing an operating system into an image while preserving disk partitions, isolating data, or wrapping an encrypted filesystem on a virtual block device.

Using the losetup command, you can associate and detach loop devices with files, or query the status of loop devices. The typical steps to initialize a loop device are:

Create a disk image that contains a filesystem:

Obtain an available loop device and associate it with the file:

Besides using losetup , you can also configure the loop device via ioctl; the relevant flags are defined in .

When configuring a loop device, the loop_add interface creates a virtual block device, whose blk_mq_ops are illustrated below:

When the block layer submits a request to the device, the queue_rq method is invoked. It initializes a loop_cmd based on the request, forwards the I/O, and finally submits it to the loop device’s workqueue for processing:

After some pre‑condition checks, the kernel calls do_req_filebacked to operate on the associated file:

For a flush operation, the kernel invokes fsync to flush the entire file to disk:

For discard operations, only REQ_OP_WRITE_ZEROES and REQ_OP_DISCARD are supported. WRITE_ZEROES calls fallocate to zero the specified range; DISCARD calls fallocate to discard the range, and subsequent reads depend on the filesystem’s handling of discarded blocks:

If the loop device is configured with encryption or direct I/O, the handling differs. When an encryption algorithm is configured, the corresponding implementation encrypts/decrypts the file data:

If direct I/O is used, the kernel forwards I/O using non‑blocking operations to avoid context switches and achieve higher throughput:

In buffered reads, random I/O benefits from concurrency, while sequential I/O often hits the page cache. Non‑blocking I/O can avoid unnecessary context switches without harming random read performance.

Conclusion

This article briefly introduced the Linux loop device mechanism and analyzed the details of its I/O forwarding. Overall, the loop device’s I/O forwarding stack is short, ensuring good performance, and its block‑device interface offers strong compatibility and extensibility.

References

https://en.wikipedia.org/wiki/Loop_device https://man7.org/linux/man-pages/man8/losetup.8.html https://man7.org/linux/man-pages/man4/loop.4.html https://lwn.net/Articles/612483/

KernellinuxBlock DeviceIO ForwardingLoop Devicelosetup
OPPO Kernel Craftsman
Written by

OPPO Kernel Craftsman

Sharing Linux kernel-related cutting-edge technology, technical articles, technical news, and curated tutorials

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.