 What are loop devices? That's the topic for today, because this question comes up pretty frequently, especially from new to Linux users, novice Linux users, especially those of you that are running Ubuntu, because Ubuntu out of the box now, ships a lot of programs as snap packages already installed as snap packages. And when you run your standard list block command, LSBLK, you have all of these loop devices that are listed among your block devices. What are those loop devices? Well, let's start with what a standard block device is. That's typically a hard drive or a solid state drive or an optical drive, like a DVD drive, a Blu-ray drive, right? And then these loop devices, they're essentially pseudo devices. They're not really devices, they're virtual devices. You can think of it as a virtual device that allows you to mount a virtual file system. Essentially, a loop device is a file that can be mounted as a quasi block device. So a good example of this would be an ISO file, an ISO. You typically you'll mount an ISO and you can mount that thing as a device essentially, but really it's just a file. An ISO, all it is is an image file. And that's also what's going on with snap packages in Ubuntu, right? These snap packages, these are files, right? They get mounted as devices. They get mounted as a file system, essentially a virtual file system. So let me switch over to my desktop here and I'm gonna go ahead and launch your terminal and just to show you guys what most people complain about with this loopback device problem. It's not really a problem. When I run a LSB okay, I have a lot of snaps installed on my computer here. Well, not a lot, but I've got, I don't know, two dozen or so snap packages and they're all mounted as loop devices, right? And these are the loops and it actually specifies in the LSB okay command what is a loop device. And then down here, of course, these are the actual block devices. So these are legitimate drives. So I've got a bunch of SSD drives. I got a NVME drive. You can see I've got some partitions created on two of the drives. Those are partitions that are currently mounted. You can see on my computer, I've got of course the root partition. I also have a separate partition for my music directory because it's very large. I have that on its own disk mounted as its own separate partition. I actually did a video about that, how you can actually put a, like your home folder or music folder or video folder on its own separate drive and then mount that thing. I'll link to that video in the show description in case you guys wanna check that out. If I had any optical drives, they would also be listed here. I did fire up a virtual machine here. So this is a virtual machine of Ubuntu just running off an ISO. So if I do Control Alt T in this VM and zoom in here and I run LSBLK, I may have to zoom back out a little bit. You can see running the live ISO here. There's not much here. There's really no, you don't see any hard drives, right? Because all of this is running off the CD-ROM, right? That's the CD-ROM drive. It's mounted and then we have this read-only file system mounted as a loop and that read-only file system. Of course, it's gonna be the ISO and then we have all these snaps that are also part of the live environment that's already installed and they are all mounted as loop devices. Now, one of the complaints that people make with snaps being mounted as loop devices is if you have a lot of snaps installed, you've got a lot of output in this LSBLK command and a lot of people claim that, you know, snaps pollute the output in LSBLK because it's, you know, there's so much stuff in the output and they only wanna see their physical drives, right? Their physical block devices. They don't wanna see any of the loops. Well, this is rather easy to accomplish because honestly, there is a flag you can use. You can do LSBLK-E7 and it will remove all the loop devices and only return your legitimate block devices. And there's also a command you can run if you want to just list your loop devices. There's actually a command. Most desktop users are never gonna run but it's a LO setup, so that's a loop setup. And if you do loop setup space-L for list, I believe, yeah, that will list all the loop devices I'm zoomed way in so the output from that looks a little weird. Let me run that again and zoom out. There we go. So that is LO setup-L for list. You could also do LO setup-A for all. And it gives you a little bit different format but it gives you a similar list of information. They're not sorted in any kind of like numerical or alphabetic way, so I like to typically run that command with sort, pipe that into sort, that way you kind of get them at least numerically sorted. Now, I don't think I've ever created my own loop device but if you wanted to, it's rather easy to do. The first thing you need to do is find a loop device that's not in use and you could do LO setup-F for find and if you do it with no other arguments, what this will do, this will find the first unused loop. So in the output from the LO setup-A, you can see loop 17 is not here, right? We went 15, 16, 17 is not here, 18, 19. So the first unused loop that I could use for a new loop device would be loop 17. So let's go ahead and create a loop. So what I'm gonna do is first thing, we're gonna have to mount a file to the loop, right? Because again, these loop devices, they're essentially files that are mounted. So I'm gonna make a directory, I'll call this make a dear test loop will be the name of the directory and then I'm gonna touch a file, I'm gonna touch test loop and I'll call this my loop will be the name of the file. Now the byte size for this new file I just created, of course it's gonna be zero bytes because it's an empty file but you can't mount something, right? Unless you give it a specific size, it's gotta have a block size, right? So what I could do is I could use this F allocate command. What this does is allocates a specific amount of space to a file and I could do dash L, I could make the size anything, I could make it 10 gigs. I've got space on the drive for this so I'll make this a 10 gig file here and then what I'm gonna do is test loop slash my loop and then with sudo privileges because anytime you create a loop device or delete a loop device, you need sudo privileges. So I'm gonna do sudo lo setup and what was the first unused loop? It was a slash dev slash loop 17 and then we need the file that we're gonna use and of course that is test loop slash my loop. Then it's gonna ask for my sudo password and if I did that correctly, if I did it LSBLK, we should now see loop 17 listed, yes and there is loop 17 and you can see the mount point is empty because it is not mounted. You see all the snap packages, they are mounted. If they weren't mounted, you actually couldn't launch these snaps. So if I did a U mount for unmount and I unmounted one of these loops for the snaps, I actually could not use that program anymore. Now what I could have done for this new loop device, I mean, I could mount it. I'd have to make a file system, you know, like if you've ever run through a standard like Arch Linux installation or Gen2 installation, anything where you've had to manually create a file system. Of course you've got the MKFS command, right? Make file system dot and then whatever file system you're creating so I could do extend for for the file system. I could do slash div slash loop 17. If I wanted to go ahead and create that file system, I'm not gonna do that though because this would, it's not something I plan on keeping but after making the file system, then of course, you know, I'd mount slash div slash loop 17. And of course I would have to mount that to a specific spot, you know, I'd have to create a directory or I could mount it to temp or whatever it is. Of course that's assuming I only am using this temporarily, if I wanted to make this a permanent like a mount point, obviously, then you would go into your file system table. I wasn't actually planning on going over most of this. I really was just gonna explain what these loop devices are for your snaps but you know, I could go into the file system table and I could, you know, add the mount point for this new loop device and then on a reboot, it should permanently always be mounted. Now I didn't actually add anything to any of that but had I gone through that just for sake of completeness, then you know, if I later wanted to delete this new loop device and mount point, what I would do is, I would do a sudo lo setup dash D for delete and delete slash dev slash loop 17 and it would destroy that loop. As a matter of fact, I do need to do that and now when I do LSB okay, I should no longer see loop 17 in the output. But I diverged quite a bit from what I originally wanted to talk about. So these loop devices that you're seeing for the snap packages, they're essentially, it's a file that is mounted. You can think of it as a virtual file system and these snaps have to be mounted. If they weren't mounted, you couldn't run the program. So if I did a U mount for example and I unmounted one of the loops, you know, pick one, I don't know. The leave very office one right here. You know, if I unmounted slash dev slash loop 11, right, I would no longer be able to run leave very office until of course I mount that back. And of course some people are gonna ask the question, you know, why would Ubuntu choose to mount these things as loop devices, these snaps? What's the benefit of that? And one of the benefits actually of using these loop devices is that you can use your loop device as a way to sandbox applications. So they contain all the necessary dependencies and they are sandboxed from the rest of the system. That's why they do these things as loop devices. It has like a real world benefit. Now before I go, I need to thank a few special people. I need to thank the producers of this episode. And of course I'm talking about Brian Gabe James, Matt Maxim, and Mitchell Paul West, why you bald homie, Alex Harmer, Dragon Chuck, Commander Rangri, Dioke George, Lee Marsstrom, Nate Erion, Alexander Paul, Pete Sortion, Vador Polytech, Realities for a Less Red Profit, Roland Steven, Tools Devler, and Willie, these guys. They're my highest tiered patrons over on Patreon without these guys. This quick little chat about loop devices would not have been possible. The show's also brought to you by each and every one of these fine ladies and gentlemen. All these names you're seeing on the screen right now. These are all my supporters over on Patreon because I don't have any corporate sponsors. I'm sponsored by you guys, the community. If you like my work, want to see more videos about Linux and free and open source software. Subscribe to DistroTube over on Patreon. Peace. And also Happy Hanukkah and Merry Christmas. Happy New Year.