BiX
BiX

Reputation: 647

Cannot delete device /dev/loop0

I unsafely removed a USB device that was attached to loop0 with losetup and could not delete nor detach loop0 afterwards.

losetup -a shows /dev/loop0: [0005]:145606719 (/dev/sdb1)

When I remount the device to /dev/sdb1, umount it and try losetup -d /dev/loop0, I will still get the same error loop: can't delete device /dev/loop0: Device or resource busy. Is there a way to get rid of the loop device?

Upvotes: 41

Views: 158868

Answers (12)

MolbOrg
MolbOrg

Reputation: 105

Same as by @frntn.

Loops were occupied by RAID:

# cat /proc/mdstat

Personalities : [raid0]

md126 : active raid0 loop6p5[1] loop5p3[0]
  209584128 blocks super 1.2 512k chunks

md127 : active raid0 loop6p2[1] loop5p2[0]
  31440896 blocks super 1.2 512k chunks

I removed them and everything obeys the orders again.

Upvotes: 0

osexp2000
osexp2000

Reputation: 3165

Run

mount | grep /dev/loopN

to check if the loop device is being mounted somewhere, then run

umount /dev/loopN 

to unmount it.

If still have problem, then run

lsof | grep loopN

to check which process is using it then kill it.

Upvotes: 5

LinuxBaseUser
LinuxBaseUser

Reputation: 31

THX for this article BiX and all: I'm not a Linux administrator only a user but can contribute so much to a had loop device problem:

When a virtual mounted image file (.iso) is not really cleanly removed from the file explorer (Dolphin/Nautilus).

You can unmount this virtual partition using the Prg "Disks" (under Service Prg's the disk management tool). However, the ISO file will still remain as a loop in the device management list. For example, if you want to delete this ISO file reel from the disk, the deletion process will only take place virtually, and the corresponding storage space in MB or GB will not be free at all. The ISO file is no longer visible in Dolphin/Nautilus after the deletion, but the corresponding storage space requirement is not specified as free again; i.e. the information of the free and occupied storage space on the data carrier where this ISO file was is unchanged before and after the deletion.

A simple but unconventional solution is to reboot/start the computer. But that can‘t not be the real solution!

The Linux shell losetup, which is described in the man page, provides a remedy here:

losetup is used to associate loop devices with regular files or block devices, to detach loop devices and to query the status of a loop device. If only the loopdev argument is given, the status of the corresponding loop device is shown.

So you can use losetup -d /dev/loop0 to delete a file associated with a loop on dev/loop0(! "losetup -d" is just usable as admin., so you need to type sudo losetup -d or wath the most LinuxGurus not fink fin, u have a so-terminal static open).

In the man page of losetup you can see that the expression Delete is not used here, but Solve. Nevertheless, according to my example, that a "file is not deleted reel, despite clean unmount" after using the option -d, the file is only now deleted reel. (I'm not a Linux administrator but:) This is rather associated with an assignment of the loopNumber which is virtual, but becomes a real condition when mounting the iso-image (image).

Here the complete syntax to the shell losetup:

https://linux.die.net/man/8/losetup

(I work on a bit corrupted linuxMint-18)

Translated from German with www.DeepL.com/Translator

Upvotes: 2

dwb.kelmark
dwb.kelmark

Reputation: 81

I chased this loop solution to the end of the Internet and found the solution is uninstall snapd and purge all associated files: (In my case this was 167 Gb)

sudo apt purge snapd

Upvotes: 8

frntn
frntn

Reputation: 831

I had the same issue today and none of the previous answers fixed it (I didn't try to reload the loop kernel module, because I wanted to understand the actual issue).

It turns out the image file associated with the loop device contained an "Linux LVM" partition, which was automatically set as shown by pvscan:

$ pvscan
  PV /dev/sda1    VG server-vg   lvm2 [417,76 GiB / 0  free]
  PV /dev/loop0   VG vbox-vg     lvm2 [7,81 GiB / 0    free]
  Total: 2 [425,57 GiB] / in use: 2 [425,57 GiB] / in no VG: 0 [0   ]

So I had de deactivate all the logical volumes inside the volume group:

$ vgchange --activate n vbox-vg
  0 logical volume(s) in volume group "vbox-vg" now active

Eventually I was able to nicely detach the loop device:

$ losetup -d /dev/loop0
$ losetup -a

Upvotes: 10

tamasd
tamasd

Reputation: 5913

Try reloading the loop kernel module. If it does not help then reboot.

Upvotes: 4

The Bndr
The Bndr

Reputation: 13394

Are you sure that the device isn't busy? Have you ever tried fuser to determine an possible PID?

Precautionary get all possible information:

fuser -c /dev/loop0
fuser -d /dev/loop0
fuser -f /dev/loop0

Try to stop the process, which could use /dev/loop0. If necessary use kill -9 or try fuser -k to send the kill signal -> look at man fuser.

Upvotes: 18

I just right click the loop device in dolphin and unmount it.

Upvotes: -5

user3578181
user3578181

Reputation: 1979

Definitely there is a process using it. If you couldn't determine which process is preventing it from deleting, then restart your server if you can.

Upvotes: 0

choad rocker
choad rocker

Reputation: 191

If you're using Luks, you need to luksClose first

cryptsetup luksClose $whatever
losetup -D

Upvotes: 2

Aaron Flin
Aaron Flin

Reputation: 589

You may have to also use dmsetup to remove the device mapping. Easiest way, if it doesn't interfere with any other mappings is to use a dmsetup remove_all.

Upvotes: 58

Mike.P
Mike.P

Reputation: 121

I had a similar issue with an SD card and Aaoron Flin's suggestion to use dmsetup worked for me.

Specifically, you should be able to ls /dev/mapper to see if any loop0pX partitions exist.

If so, you can use dmsetup remove /dev/mapper/loop0p2 to get rid of any unnecessary partitions. This can be useful if you want to keep some mappings attached.

Upvotes: 12

Related Questions