10+ losetup command examples in Linux [Cheat Sheet]


Written By - Rohan Timalsina
Advertisement

Introduction to losetup command

The loop device is a block device that maps its data to the blocks of a regular file or another block device. Loop devices are mounted as file systems on Unix-like operating systems.

losetup is a handy command-line utility to set up and control loop devices in Linux. It also allows you to detach loop devices and display the information of used loop devices.

 

How to use losetup command

The syntax to use losetup command is as follows:

$ losetup [option] [loopdev]

 

Different examples to use losetup command

1. Create a loop device with losetup

You can use the losetup command to create a new loop device in Linux.

First, you need to create a file that will be used as a block device. The following command creates a file named loopfile which is 1GB in size.

$ sudo dd if=/dev/zero of=loopfile bs=100M count=10

Sample Output:

golinux@ubuntu-PC:~$ sudo dd if=/dev/zero of=loopfile bs=100M count=10
10+0 records in
10+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 1.40495 s, 746 MB/s

Next, run the losetup command specifying the loop device and file you want to map it to.

$ sudo losetup /dev/loop13 loopfile

If the loop device is already in use, you will get the following error.

Advertisement
losetup: loopfile: failed to set up loop device: Device or resource busy

You can use the -f option to avoid errors. The available loop device will be automatically used.

$ sudo losetup -f loopfile

To verify whether the loop device is created with a file, run this command.

$ losetup -j loopfile

Sample Output:

golinux@ubuntu-PC:~$ losetup -j loopfile
/dev/loop13: []: (/home/golinux/loopfile)

 

2. Display information about all loop devices

The losetup command without any arguments prints information about all used loop devices.

$ losetup

Sample Output:

losetup command to display information about loop devices

Output columns:

  • NAME: loop device name
  • SIZELIMIT: size limit of the file in bytes
  • OFFSET: offset from the beginning
  • AUTOCLEAR: autoclear flag set
  • RO: read-only device
  • BACK-FILE: device backing file
  • DIO: access backing file with direct-io
  • LOG-SEC: logical sector size in bytes

 

3. List all used loop devices

The losetup command with -a or --alloption gets the list of all used loop devices and associated files. It does not display columns in the output.

$ losetup -a 

OR

$ losetup --all 

Sample Output:

losetup command to show status of all loop devices

 

4. Detach loop devices

The -d or --detach option is used to detach the file or device associated with the loop device. You can specify one or more loop devices.

$ losetup -d loopdev

OR

$ losetup --detach loopdev

These are the available loop devices on my server:

server:~ # losetup -a
/dev/loop0: [64772]:46 (/Images/CentOS-8.2.2004-x86_64-dvd1.iso)
/dev/loop1: [64772]:38 (/Images/OneNDS-XX-IS-20.0.0.05_dvd.iso)
/dev/loop2: [64773]:131302 (//opt/secure/loopback.img)

I will detach /dev/loop2 as the other 2 devices are basically image mounted on the system so we cannot detach them using losetup command:

server:~ # losetup -d /dev/loop2 

server:~ # losetup -a
/dev/loop0: [64772]:46 (/Images/CentOS-8.2.2004-x86_64-dvd1.iso)
/dev/loop1: [64772]:38 (/Images/Dummy-XX-IS-20.0.0.05_dvd.iso)

 

5. Detach all used devices

If you want to detach all loop devices, you can run the losetup command with -D or --detach-all option.

$ losetup -D

OR

$ losetup --detach-all

 

6. Find first unused device

The -f or --find option helps to find the first unused loop device.

$ losetup -f

OR

$ losetup --find

Sample Output:

golinux@ubuntu-PC:~$ losetup -f
/dev/loop13

7. List all devices associated with a file

The -j or --associated option instructs losetup to display the list of all loop devices associated with a given file.

$ losetup -j file

OR

$ losetup --associated file

Sample Output:

The following command prints the loop device associated with a file named blockfile in the current directory

golinux@ubuntu-PC:~$ losetup -j blockfile
/dev/loop14: []: (/home/golinux/blockfile)

 

8. Print the list in JSON format

The -J or --json option uses the JSON output format to display all devices.

$ losetup -J

OR

$ losetup --json

Sample Output:

print the list of loop devices in json format

 

9. Set up a read-only loop device

To set up a read-only loop device, you have to use the losetup command with -r or --read-only option.

The following command creates a read-only loop device using the file named loopfile.

$ sudo losetup -r /dev/loop13 loopfile

Sample Output:

set up a read only loop device with losetup command

 

10. Create a loop device with the size limit

You can set the size limit in the loop device with --sizelimit option. The following command creates a new loop device with a size limit of 200M.

$ sudo losetup --sizelimit <num> loopdev loopfile

Sample Output:

create a loop device with size limit in linux

 

11. Set offset value in loop device

The -o or --offset option helps to create a loop device with a specified offset value.

$ sudo losetup -o <num> loopdev loopfile

OR

$ sudo losetup --offset <num> loopdev loopfile

Sample Output:

create a loop device with offset value in linux

 

12. Create a partitioned loop device

The -P or --partscan can be used to create a partitioned loop device.

$ sudo losetup -Pf loopfile

OR

$ sudo losetup --partscan -f loopfile

 

Conclusion

Now you know how to set up and manage loop devices in Linux. You have also learned to delete loop devices and get detailed information about used loop devices in the system.

We hope you find this article helpful. If you have any confusion, please let us know in the comment section.

 

References

man page for losetup

 

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment