Loop devices are used frequently, though not many people know precisely what they are. In this video, learn how to create a loop device—a file that contains a file system.
- [Instructor] In this episode, I want to take a few minutes to talk about loop devices. A loop device is basically a file that contains a file system. So that means that you can move the file around the system or to other systems and take the file system along with it. We use these pretty widely, sometimes even without realizing it. Linux installations, for example, the ISO images that represent an optical disc are loop devices because they're a file that contains a file system. And Snaps, the portable applications that are becoming more popular lately, run from their own small loop devices containing their initial read-only file system.
Loop devices are also used where portable encrypted file systems are needed, like in TrueCrypt, which was popular a while ago, and others. Other systems make use of loop app devices as well. macOS, for example, makes extensive use of what it calls disk images, which are files with a file system inside them, or loop devices. We can create a loop device of our own by generating a file of a particular size with the dd or fallocate commands. I like fallocate because it's really quick, but you can use dd as well and write zeros to a file.
I'll make a one-gigabyte file here. I'll write fallocate -l 1G myimage. And there it is. Now, I can create a file system within that image with mkfs.ext4 and the name of the image. I'll use fallocate again to make this image sparse, that is, to make it so that it doesn't actually take up all the space on the disk right now that it can contain.
I'll write fallocate -d myimage. A sparse image will take up more space as data is added to it rather than being a monolithic block of a fixed maximum size right from the beginning. This is handy for keeping a portable file system to a reasonable size. Once that's done I can create a place to mount the file and then mount it. I'll write mkdir /mnt/myfiles, and then I'll write mount myimage to my files, and then I can take a look over there.
Let's make this writeable by everyone with chmod 777 and period, and then I'll create some highly valuable files with touch and we'll expand one to a thousand. This device is presented to the system just like a regular disk. So I could manage files on it with a regular GUI application. Because it's a loop device, it has its own entry on the device's folder name with loop and a number.
I can see that these are blocked devices because they start with a B here and with the losetup tool, I can explore them a bit more. Check out the main page for losetup if you're curious about it. Okay, I'm done using this loop device for now. I'll make sure I move out of the folder for the mount point and then I'll unmount it with umount and the path to the mount. The files that I created are now within a file system within this loop device, or now, I suppose, we can start going in the disk image.
I can move this file around to other systems and mount it there and have my own little file system. I could zip it or encrypt it as it suits my needs. Before we end, I want to show you the effect of this being a sparse file. If I list the folder with ls - lh, I see that my file is indicated to take up a gigabyte as I told it to be. The file system expects this file to be a gigabyte based on the metadata for the file that was written when I created it.
But because we made it sparse, I mentioned it won't actually take up that space. If I use the du - h command on myimage to see how much it's actually taking, I can see that it's much less than a gigabyte. Loop devices are great for distributing software or other files and they're commonly used across the Linux world so they're helpful to know about.
Note: Because this is an ongoing series, viewers will not receive a certificate of completion.