Skip to content

Instantly share code, notes, and snippets.

@fjctp
Last active March 14, 2020 22:41
Show Gist options
  • Save fjctp/f5d885b98e92b6d9879c9301eb368504 to your computer and use it in GitHub Desktop.
Save fjctp/f5d885b98e92b6d9879c9301eb368504 to your computer and use it in GitHub Desktop.
Loop device in Linux

Loop Devices

Loop devices are how entire file systems can be mounted. If you have had any experience with most virtualization (eg KVM, Virtualbox, VMware, etc) the most common way Guests store data is in a file (aka virtual disk) which can contain an entire operating system or more on a specified file system. The "virtual disk" file with its contents is mounted for use by the Guest and generally protected from other access.

In the same way, it might be useful to mount an ISO file for access without burning the file's contents to an optical disk (CD or DVD typically).

There are other scenarios where it's useful to mount file systems so that they are accessible from the currently running system.

The file containing the file system that attached to a loop device is typically called a backing file.

Usage

Use losetup to manage loop device

dd if=/dev/zero of=~/file.img bs=1MiB count=10

losetup --find --show ~/file.img
mkfs.ext4 /dev/loop0
mount /dev/loop0 /mnt

umount /dev/loop0
losetup --detach /dev/loop0

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment