Difference between revisions of "Scan and check USB drives"

From wiki.N4VX.net
Jump to navigation Jump to search
(Created page with "Bad sectors or bad blocks are damaged portion of your mechanical hard disk drive which can not be used at all for data storing purposes. However, the operating system still ca...")
 
m
Line 5: Line 5:
 
Hence, you should periodically scan your hard drive (especially aging ones) for bad sectors if you feel your system is slowing down, or, disk IO is increasing. In Linux (Ubuntu, Fedora, and other distributions), you can easily do this via below terminal commands.  
 
Hence, you should periodically scan your hard drive (especially aging ones) for bad sectors if you feel your system is slowing down, or, disk IO is increasing. In Linux (Ubuntu, Fedora, and other distributions), you can easily do this via below terminal commands.  
  
*How to Manage Disk Bad Sectors in Linux
+
==How to Manage Disk Bad Sectors in Linux==
 
It is better to run below commands when your disk is not mounted with the operating system. Hence I would suggest, you try this using LIVE operating system boot from a USB stick. You can create a LIVE USB using this guide with any Linux operating system of your choice (recommended: Ubuntu).
 
It is better to run below commands when your disk is not mounted with the operating system. Hence I would suggest, you try this using LIVE operating system boot from a USB stick. You can create a LIVE USB using this guide with any Linux operating system of your choice (recommended: Ubuntu).
  
 
However, you can still run these commands in your installed Linux distribution but you should not scan or mark the mounted “/” root filesystem.
 
However, you can still run these commands in your installed Linux distribution but you should not scan or mark the mounted “/” root filesystem.
  
*Scan for Bad Sectors
+
==Scan for Bad Sectors==
 
As a first step, identify the disk partition which you want to scan for bad sectors. If you have GParted installed, it is easy to find out. Otherwise, you can run below command (lsblk – List block devices) to view your disk partitions.  
 
As a first step, identify the disk partition which you want to scan for bad sectors. If you have GParted installed, it is easy to find out. Otherwise, you can run below command (lsblk – List block devices) to view your disk partitions.  
  
Line 20: Line 20:
 
  sudo badblocks -v /dev/sda1 > ~/bad_sectors.txt
 
  sudo badblocks -v /dev/sda1 > ~/bad_sectors.txt
  
*Repair Bad Sectors
+
==Repair Bad Sectors==
 
For ext2, ext3, and ext4 file systems, you can use e2fsck utility to check and repair bad sectors. In the terminal run below command with admin privilege to check and repair.  
 
For ext2, ext3, and ext4 file systems, you can use e2fsck utility to check and repair bad sectors. In the terminal run below command with admin privilege to check and repair.  
 
  sudo e2fsck -cfpv /dev/sda1
 
  sudo e2fsck -cfpv /dev/sda1

Revision as of 12:04, 21 October 2021

Bad sectors or bad blocks are damaged portion of your mechanical hard disk drive which can not be used at all for data storing purposes. However, the operating system still can write to those sectors unless you specifically mark them as ‘bad’ or unusable. Also if you have data stored in those areas of the hard drive, it is very difficult to recover those as well.

Although, the latest computer storage technology such as SSD, etc almost eliminates this problem. However, there is still a huge number of hard disk drives in use today which is aging and might slowly start having bad sectors.

Hence, you should periodically scan your hard drive (especially aging ones) for bad sectors if you feel your system is slowing down, or, disk IO is increasing. In Linux (Ubuntu, Fedora, and other distributions), you can easily do this via below terminal commands.

How to Manage Disk Bad Sectors in Linux

It is better to run below commands when your disk is not mounted with the operating system. Hence I would suggest, you try this using LIVE operating system boot from a USB stick. You can create a LIVE USB using this guide with any Linux operating system of your choice (recommended: Ubuntu).

However, you can still run these commands in your installed Linux distribution but you should not scan or mark the mounted “/” root filesystem.

Scan for Bad Sectors

As a first step, identify the disk partition which you want to scan for bad sectors. If you have GParted installed, it is easy to find out. Otherwise, you can run below command (lsblk – List block devices) to view your disk partitions.

sudo lsblk -o name,mountpoint,label,size,uuid

If you are running above command via LIVE USB, make sure you can identify your HDD and USB stick. Typically HDD should be defined as /dev/sda.

Then you can run badblocks command as below with the verbose (-v) switch. And save the output to a text file for further investigation. This is just a verification whether you have bad sectors in the hard drive or not.

sudo badblocks -v /dev/sda1 > ~/bad_sectors.txt

Repair Bad Sectors

For ext2, ext3, and ext4 file systems, you can use e2fsck utility to check and repair bad sectors. In the terminal run below command with admin privilege to check and repair.

sudo e2fsck -cfpv /dev/sda1

Make sure to replace sda1 with the proper device identifier. The parameters “c” searches for bad blocks and add it to a list, “f” does a check on the file system. The “p” parameter repairs anything if possible and “v” is the verbose mode which gives you the terminal output of the command progress.

You can also specify the bad_sectors.txt file created in the earlier steps as well to force e2fsck to repair those in the file only via the below command.

sudo e2fsck -l bad_sectors.txt /dev/sda1

For other file systems (such as FAT32), you can use fsck.

sudo fsck -l bad_sectors.txt /dev/sda1

However, the above command execution might take several hours to run depending on your disk partition size and health of your disk. So be ready before you start the command. Try not to terminate the command via CTRL+C or CTRL+Z while it is in progress.

I hope this tutorial helped you to identify issues in your hard drive and mark them as bad to prevent further data loss. Remember that even if you mark the sectors as ‘bad’, the disk is still physically damaged already. It is merely a software fix to mark those tracks as bad to tell the operating system not to access. If your hard disk started having bad sectors, in the long run, it would become worse. Hence is it recommended that you should start backing up your data and replace your hard drive with a new one or go for the latest SSD drives.

This writeup was taken from https://www.debugpoint.com/2020/07/scan-repair-bad-sector-disk-linux/