Saturday, May 4, 2024
 Popular · Latest · Hot · Upcoming
42
rated 0 times [  42] [ 0]  / answers: 1 / hits: 63615  / 2 Years ago, fri, august 5, 2022, 10:55:05

While I was away from my computer, my encrypted USB drive got accidentally unmounted somehow (although it was still physically connected at the time). I haven't been able to recover (haven't tried a reboot yet). I have now completely disconnected the device, but I still get "Device or resource busy" when I try to remove the dangling entry in /dev/mapper. Can I reconnect and mount the drive without a reboot?



Here's what I have tried (long name changed to "xxxxx")...



$ sudo dmsetup ls
luks-xxxxx (252:1)
luks-yyyyy (252:0)

$ sudo umount /dev/mapper/luks-xxxxx
umount: /dev/mapper/luks-xxxxx: not mounted

$ sudo fuser --kill /dev/mapper/luks-xxxxx
$ echo $?
1

$ sudo dmsetup info -c luks-xxxxx
Name Maj Min Stat Open Targ Event UUID
luks-xxxxx 252 1 L--w 1 1 0 CRYPT-LUKS1-xxxxx-luks-xxxxx

$ sudo dmsetup remove luks-xxxxx
device-mapper: remove ioctl on luks-xxxx failed: Device or resource busy
Command failed


After reconnecting the device...



$ sudo cryptsetup luksOpen "/dev/sde1" "luks-xxxxx"
Device luks-xxxxx already exists.


[EDIT] I solved the problem, this time, by closing a GUI text editor which had no open files, but had been launched from a folder on the device in question. So the question becomes more specific: How can you identify which application is holding the device open?



Bear in mind that lsof doesn't seem to present an easy solution because, once the device is disconnected, the associated names provided by lsof no longer include the name of the disconnected device.


More From » 13.10

 Answers
4

After two years of fighting with this, I think I've finally cracked it completely!


dmsetup ls gives you the data you need:


$ sudo dmsetup ls
luks-xxxxx (252:1)

then


sudo lsof |grep 252,1   ## notice translation: (252:1) => 252,1

It seems that sudo may be critical here -- at least in some cases.




This should give you the information necessary to close all open files on the device -- including names of open files and process IDs for the offending applications. You may be able to simply go to those apps and close them, but a brute force approach might be something like:


kill -9 (process ID)

Once you've closed all the files, some of the command-line tools shown in the question may be necessary in order to close down the existing mount before it can be reopened normally.


[#26679] Saturday, August 6, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
serveeel

Total Points: 347
Total Questions: 106
Total Answers: 117

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
serveeel questions
;