User:Tsu2/loop devices
This Guide
Creating this article on loop devices because I found that almost all guides I found on the Internet are obsolete and will not work.
This information is based on the losetup man pages which I found is the only reliable working reference
The information in this guide should apply to all known current versions of openSUSE and likely other distros as well on all hardware platforms.
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.
Losetup
Although loop devices (sometimes called loopback devices) can be created elsewhere, this guide describes using losetup which is the utility to create, manage and destroy loop devices in a fully running *NIX system. It's also possible to create loop devices in GRUB so that loop devices can be available during pre-boot but is not discussed here.
Main Example
The following is lifted directly from the losetup man pages, although IMO is self-explanatory, is followed by descriptive comments
EXAMPLE
The following commands can be used as an example of using the loop device.
# dd if=/dev/zero of=~/file.img bs=1MiB count=10 # losetup --find --show ~/file.img /dev/loop0 # mkfs -t ext2 /dev/loop0 # mount /dev/loop0 /mnt ... # umount /dev/loop0 # losetup --detach /dev/loop0
- The first line is a standard way of creating an empty backing file of a specific size (10 megabytes)
- By default, there are 8 loop devices (loop0-7), the second command verifies loop0 is unused and available, and sets up loop0
- The third command formats the inside of the backing file with a specific file system
- The fourth command mounts the loop device so it can be accessed at /mnt
- The last two commands describe unmounting the loop device and then removing the loop0 device configuration so that it can be used again later for a different setup. If you don't remove the loop device, the configuration will persist although unmounted.
Additional Info
Alternate create file system commands
Although the example uses the command "mkfs" to create the file backing file fs and accepts flags to create EXT3 and EXT4 (EXT2 is default) and DOS, the following also creates the fs without the fs type flag
mke2fs
mke3fs
mke4fs
mkdosfs
Create CD or DVD ISO file
You can create an ISO file with
mkisofs
Supported File System formats
All file systems supported by mkfs
mkfs -t bfs | ext2 | ext3 | ext4 | minix | msdos | vfat | xfs | xiafs