0

I have a computer I use for imaging disks running Ubuntu 16.04. Each disk is inserted into a USB 3.0 dock, imaged/wiped, and then disconnected. The disks don't have any mounted filesystems which need to be dismounted. They disappear from gnome-disks as expected. Eventually, using gparted and/or the gnome-disks, I am no longer able to see any new disks that get added. Sometimes, new disks show up under an old /dev/sdx device and I can access them but they show the old device's partition table and size. I assume this is because /dev/sdx is filling up and the kernel is holding onto pointers to disks which no longer exist?

Edit: I should add that a number of these disks have bad sectors or other issues, so that could be a part of the problem as well. This "block device exhaustion" happens faster when more malfunctioning drives are added/removed. Once it happens, even good drives won't appear when added to the system. But I notice this happens even if all drives I'm adding/removing are healthy and functioning.

What can I do to prevent this behaviour or tell the kernel to "forget" disconnected disks?

1

Before disconnecting, say, /dev/sdX, do first a blockdev -flushbufs /dev/sdX to ensure all the data is fully written on the disk and not waiting in a buffer, just to be sure.

Then do a echo 1 > /sys/block/sdX/device/delete. This will tell the kernel that /dev/sdXwill be going away and should be forgotten. Depending on disks/docks involved, this might even spin down the disk automatically.

  • 1
    Thanks for the answer : ). If it's impossible for the flush to complete, such as if it's trying to write to a drive with some bad sectors or malfunctions, would this still work? – Mr. T Oct 11 '18 at 7:48
  • If you don't care about the data, feel free to omit the blockdev -flushbufs. I obviously haven't tested the behavior of /sys/block/sdX/device/delete with all the disk controllers in the world, so I cannot be sure, but I think it should do exactly what you need. You might still get a blast of errors in the kernel log when actually disconnecting the device, but the deletion tells the kernel it should not hold any hopes for the device coming back once it's disconnected. – telcoM Oct 11 '18 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.