What is loop device in linux

What is a «loop device» when mounting?

In man you can search for a string by typing /mystring after man starts. You can highlight all matches with just / . See man man . I see @Josh has added such a comment to the accepted answer.

/ is the standard search feature in vi, vim and almost all commands that output in pages (less, more. )

3 Answers 3

A loop device is a pseudo («fake») device (actually just a file) that acts as a block-based device. You want to mount a file disk1.iso that will act as an entire filesystem, so you use loop.

The -o is short for —options .

And the last thing, if you want to search for «-o» you need to escape the ‘-‘.

With GNU grep, grep -e -o ( -e says «next thing is the pattern no matter what it looks like) or grep — -o ( — means stop looking for switches) work too. Of course feel free to use whatever works for you.

You can also just type: man mount , and then you can use /-o to search for and highlight all instances of «-o»

Traditionally, UNIX systems have had various types of nodes in their filesystems:

  • directory
  • file
  • symlink
  • block device
  • character device
  • FIFO
  • UNIX domain socket

While there are now exceptions, generally block devices containing filesystems are mounted on directories.

Since you want to mount a file, you must first create a loop block device that is backed by the file. This can be done using losetup , but mount -o loop is a shortcut that handles that behind the scenes.

Источник

What is loop device in linux

NAME

loop, loop-control - loop devices

SYNOPSIS

DESCRIPTION

The loop device is a block device that maps its data blocks not to a physical device such as a hard disk or optical disk drive, but to the blocks of a regular file in a filesystem or to another block device. This can be useful for example to provide a block device for a filesystem image stored in a file, so that it can be mounted with the mount(8) command. You could do $ dd if=/dev/zero of=file.img bs=1MiB count=10 $ sudo losetup /dev/loop4 file.img $ sudo mkfs -t ext4 /dev/loop4 $ sudo mkdir /myloopdev $ sudo mount /dev/loop4 /myloopdev See losetup(8) for another example. A transfer function can be specified for each loop device for encryption and decryption purposes. The following ioctl(2) operations are provided by the loop block device: LOOP_SET_FD Associate the loop device with the open file whose file descriptor is passed as the (third) ioctl(2) argument. LOOP_CLR_FD Disassociate the loop device from any file descriptor. LOOP_SET_STATUS Set the status of the loop device using the (third) ioctl(2) argument. This argument is a pointer to loop_info structure, defined in  as: struct loop_info < int lo_number; /* ioctl r/o */ dev_t lo_device; /* ioctl r/o */ unsigned long lo_inode; /* ioctl r/o */ dev_t lo_rdevice; /* ioctl r/o */ int lo_offset; int lo_encrypt_type; int lo_encrypt_key_size; /* ioctl w/o */ int lo_flags; /* ioctl r/o */ char lo_name[LO_NAME_SIZE]; unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ unsigned long lo_init[2]; char reserved[4]; >; The encryption type (lo_encrypt_type) should be one of LO_CRYPT_NONE, LO_CRYPT_XOR, LO_CRYPT_DES, LO_CRYPT_FISH2, LO_CRYPT_BLOW, LO_CRYPT_CAST128, LO_CRYPT_IDEA, LO_CRYPT_DUMMY, LO_CRYPT_SKIPJACK, or (since Linux 2.6.0) LO_CRYPT_CRYPTOAPI. The lo_flags field is a bit mask that can include zero or more of the following: LO_FLAGS_READ_ONLY The loopback device is read-only. LO_FLAGS_AUTOCLEAR (since Linux 2.6.25) The loopback device will autodestruct on last close. LO_FLAGS_PARTSCAN (since Linux 3.2) Allow automatic partition scanning. LOOP_GET_STATUS Get the status of the loop device. The (third) ioctl(2) argument must be a pointer to a struct loop_info. LOOP_CHANGE_FD (since Linux 2.6.5) Switch the backing store of the loop device to the new file identified file descriptor specified in the (third) ioctl(2) argument, which is an integer. This operation is possible only if the loop device is read-only and the new backing store is the same size and type as the old backing store. LOOP_SET_CAPACITY (since Linux 2.6.30) Resize a live loop device. One can change the size of the underlying backing store and then use this operation so that the loop driver learns about the new size. This operation takes no argument. Since Linux 2.6, there are two new ioctl(2) operations: LOOP_SET_STATUS64, LOOP_GET_STATUS64 These are similar to LOOP_SET_STATUS and LOOP_GET_STATUS described above but use the loop_info64 structure, which has some additional fields and a larger range for some other fields: struct loop_info64 < uint64_t lo_device; /* ioctl r/o */ uint64_t lo_inode; /* ioctl r/o */ uint64_t lo_rdevice; /* ioctl r/o */ uint64_t lo_offset; uint64_t lo_sizelimit;/* bytes, 0 == max available */ uint32_t lo_number; /* ioctl r/o */ uint32_t lo_encrypt_type; uint32_t lo_encrypt_key_size; /* ioctl w/o */ uint32_t lo_flags; /* ioctl r/o */ uint8_t lo_file_name[LO_NAME_SIZE]; uint8_t lo_crypt_name[LO_NAME_SIZE]; uint8_t lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ uint64_t lo_init[2]; >; /dev/loop-control Since Linux 3.1, the kernel provides the /dev/loop-control device, which permits an application to dynamically find a free device, and to add and remove loop devices from the system. To perform these operations, one first opens /dev/loop-control and then employs one of the following ioctl(2) operations: LOOP_CTL_GET_FREE Allocate or find a free loop device for use. On success, the device number is returned as the result of the call. This operation takes no argument. LOOP_CTL_ADD Add the new loop device whose device number is specified as a long integer in the third ioctl(2) argument. On success, the device index is returned as the result of the call. If the device is already allocated, the call fails with the error EEXIST. LOOP_CTL_REMOVE Remove the loop device whose device number is specified as a long integer in the third ioctl(2) argument. On success, the device number is returned as the result of the call. If the device is in use, the call fails with the error EBUSY.

EXAMPLE

The program below uses the /dev/loop-control device to find a free loop device, opens the loop device, opens a file to be used as the underlying storage for the device, and then associates the loop device with the backing store. The following shell session demonstrates the use of the program: $ dd if=/dev/zero of=file.img bs=1MiB count=10 10+0 records in 10+0 records out 10485760 bytes (10 MB) copied, 0.00609385 s, 1.7 GB/s $ sudo ./mnt_loop file.img loopname = /dev/loop5 Program source #include fcntl.h> #include linux/loop.h> #include #include stdio.h> #include stdlib.h> #include unistd.h> #define errExit(msg) do < perror(msg); exit(EXIT_FAILURE); \ > while (0) int main(int argc, char *argv[]) < int loopctlfd, loopfd, backingfile; long devnr; char loopname[4096]; if (argc != 2) < fprintf(stderr, "Usage: %s backing-file\n", argv[0]); exit(EXIT_FAILURE); >loopctlfd = open("/dev/loop-control", O_RDWR); if (loopctlfd == -1) errExit("open: /dev/loop-control"); devnr = ioctl(loopctlfd, LOOP_CTL_GET_FREE); if (devnr == -1) errExit("ioctl-LOOP_CTL_GET_FREE"); sprintf(loopname, "/dev/loop%ld", devnr); printf("loopname = %s\n", loopname); loopfd = open(loopname, O_RDWR); if (loopfd == -1) errExit("open: loopname"); backingfile = open(argv[1], O_RDWR); if (backingfile == -1) errExit("open: backing-file"); if (ioctl(loopfd, LOOP_SET_FD, backingfile) == -1) errExit("ioctl-LOOP_SET_FD"); exit(EXIT_SUCCESS); >

FILES

/dev/loop*: the loop block special device files

SEE ALSO

Источник

Читайте также:  Linux вывод открытых портов

What is a Loop device in Linux?

While listing mounted drives through the terminal, you must have encountered drive names starting with loop:

list drives in ubuntu

If you are an Ubuntu user, then you’ll get a long list of loop devices as shown in the screenshot above.

It is because of snaps, the universal package management system developed by Canonical. The snap applications are mounted as loop devices.

Now, this raises another set of questions such as what is a loop device and why snaps applications are mounted as a disk partition.

Let me shed some light on the topic

Loop devices: Regular Files that are mounted as File System

Linux allows users to create a special block device by which they can map a normal file to a virtual block device.

Seems too complicated right? Let me break it down for you.

In simple terms, a loop device can behave as a virtual file system which is quite helpful while working with isolated programs such as snaps.

So basically you get an isolated file system mounted at a specific mounting point. By which a developer/advanced user packs a bunch of files in one place. So it can be accessed by an operating system and that behavior is known as loop mounts.

But working with isolated systems using a loop device is one of the many reasons why loop devices are utilized and if you’re interested, here are more use cases of loop devices.

Reasons for using loop Devices

While being a virtual file system, there are endless possibilities; here are some widely known use cases of loop devices:

  1. It can be used to install an operating system over a file system without going through repartitioning the drive.
  2. A convenient way to configure system images (after mounting them).
  3. Provides permanent segregation of data.
  4. It can be used for sandboxed applications that contain all the necessary dependencies.
Читайте также:  Understanding kernel linux pdf

And the developers can do wonders when given isolated file systems.

The loop devices can be easily managed through losetup utility. Let me show you how.

Manage loop devices

So let’s start with listing available loop devices.

To list them, all you need to do is pair losetup with -a option:

losetup a

Unmount Loop device

The process for unmounting any loop device is pretty straightforward. For that, I’ll be using umount command.

lsblk

The loop9 block was brave browser installed as snap, and you can clearly see, it is no longer mounted and can’t be launched.

Delete Loop device

This is for demonstration purposes only. Don’t go and randomly delete loop devices.

Make sure to unmount the loop device before you proceed further in deleting a specific loop device.

Your first step will be detaching files to any loop device using -d option. For demonstration, I’ll be using loop9 :

And now, you can remove the loop9 device by the same old the rm command that is used to remove files and directory:

And loop9 was no longer listed in available loop devices:

delete loop device

Final Words

The guide was intended to cover the basics of loop devices, and I kept it simple enough so even new users could benefit from this guide.

Have a point to add? The comment section is all yours.

Источник

Оцените статью
Adblock
detector