fuser -km /dev/sdX1 # kill processes using the mount umount -l /dev/sdX1 # lazy unmount (detach now, clean up later) swapoff /dev/sdX2 # if a swap partition exists 4.3 Deactivate LVM or RAID components lvchange -an /dev/vg_name/lv_name # deactivate LVM mdadm --stop /dev/mdX # stop RAID array 4.4 Stop automount services (temporarily) systemctl stop udisks2 systemctl stop autofs 4.5 Force release (last resort – data unsafe) echo 1 > /sys/block/sdX/device/delete # SCSI device removal (re-scan required) hdparm --user-master u --security-unlock "" /dev/sdX # (not recommended) ⚠️ Force methods may cause data loss or system instability. 5. Prevention & Best Practices | Practice | Reason | |----------|--------| | Always unmount before partitioning/formatting | Prevents locks | | Use partprobe or blockdev --rereadpt after partitioning | Informs kernel without full unlock | | Avoid using -f (force) flags on mount commands arbitrarily | Masks underlying busy issues | | Check lsof / fuser habitually | Quick diagnosis | | Use wipefs carefully | Removes signatures without locking full device | 6. Example Scenario Symptom: User runs fdisk /dev/sdb and sees the error, even though mount | grep sdb shows nothing.
kill -9 <PID> fdisk /dev/sdb # now works | Similar error | Context | |---------------|---------| | Device or resource busy | Generic – often same cause | | Can't open /dev/sdX: Resource temporarily unavailable | Lock held by another process | | Failed to open device: Device or resource busy | Same, from mkfs or dd | 8. Conclusion The "unable to lock device" error is a protective mechanism preventing concurrent destructive access to a storage device. The solution is almost always to identify and release the holding process or mount, not to forcibly override the lock. Systematic checking with lsof , fuser , and mount will resolve nearly all cases safely. fuser -km /dev/sdX1 # kill processes using the
lsof /dev/sdb reveals a stale NFS lock or a process like gvfsd-trash holding the device. Example Scenario Symptom: User runs fdisk /dev/sdb and
lsblk | grep sdX mount | grep sdX lsof | grep '/dev/sdX' fuser -v /dev/sdX For a specific partition: The solution is almost always to identify and