How to Identify and Fix Bad Sectors on Your Hard Drive

Guide: How to Identify and Fix Bad Sectors on Your Hard Drive (for Linux systems including OpenMediaVault).

Download script

Online command to download and execute the script from online source.

curl -sSL https://docs.greenhome.stream/attachments/30 -o find_fix_bad_sectors.sh && chmod +x find_fix_bad_sectors.sh && ./find_fix_bad_sectors.sh

English Version

What are Bad Sectors? Bad sectors are parts of your hard drive that have become unreliable or unreadable due to physical damage or data corruption. If bad sectors are not dealt with, they can cause data loss or system instability.

Step 1: Identify Bad Sectors

Example command:

sudo smartctl -a /dev/sdX

Replace /dev/sdX with your actual drive (e.g., /dev/sdc).

sudo badblocks -sv /dev/sdX > badsectors.txt

This will scan your drive and save any identified bad sector numbers to badsectors.txt.

Step 2: Fix or Relocate Bad Sectors
sudo hdparm --write-sector [sector_number] --yes-i-know-what-i-am-doing /dev/sdX

Replace [sector_number] with the exact bad sector number and /dev/sdX with your drive. This command forces the drive firmware to remap the sector if faulty.

  1. Using dd to write zeros to the sector:
sudo dd if=/dev/zero of=/dev/sdX bs=512 count=1 seek=[sector_number]

Also replace [sector_number] and /dev/sdX. This will overwrite the sector and trigger reallocation if needed.

Step 3: Verify and Monitor
sudo smartctl -a /dev/sdX
Important Notes

Greek Version (Ελληνική Έκδοση)

Τι είναι οι Κακοί Τομείς (Bad Sectors); Οι κακοί τομείς είναι περιοχές στον σκληρό δίσκο που έχουν γίνει μη αξιόπιστες ή μη αναγνώσιμες λόγω φυσικής φθοράς ή καταστροφής δεδομένων. Αν δεν αντιμετωπιστούν, μπορεί να προκαλέσουν απώλεια δεδομένων ή αστάθεια συστήματος.

Βήμα 1: Εντοπισμός Κακών Τομέων

Παράδειγμα εντολής:

sudo smartctl -a /dev/sdX

Αντικαταστήστε το /dev/sdX με τη σωστή συσκευή του δίσκου σας (π.χ. /dev/sdc).

sudo badblocks -sv /dev/sdX > badsectors.txt

Αυτό σαρώνει τον δίσκο και αποθηκεύει τους αριθμούς των κακών τομέων στο αρχείο badsectors.txt.

Βήμα 2: Επιδιόρθωση ή Μεταφορά Κακών Τομέων
sudo hdparm --write-sector [αριθμός_τομέα] --yes-i-know-what-i-am-doing /dev/sdX

Αντικαταστήστε το [αριθμός_τομέα] με τον ακριβή αριθμό τομέα και το /dev/sdX με τη συσκευή του δίσκου σας. Η εντολή αυτή αναγκάζει το firmware του δίσκου να κάνει remap αν ο τομέας είναι κατεστραμμένος.

  1. Με dd για να γράψετε μηδενικά στον τομέα:
sudo dd if=/dev/zero of=/dev/sdX bs=512 count=1 seek=[αριθμός_τομέα]

Επίσης αντικαταστήστε τα κατάλληλα πεδία. Αυτό θα αναγκάσει το δίσκο να επανατοποθετήσει κακούς τομείς.

Βήμα 3: Επιβεβαίωση και Παρακολούθηση
sudo smartctl -a /dev/sdX
Σημαντικές Σημειώσεις
Download the script find_fix_bad_sectors.sh

Or create a new find_fix_bad_sectors.sh and copy/paste the below code.

#!/bin/bash

# Define device
DEVICE="/dev/sdC"

# Run badblocks to find bad sectors and save to badsectors.txt
sudo badblocks -sv "$DEVICE" > badsectors.txt

# Check if badsectors.txt is not empty
if [ -s badsectors.txt ]; then
    echo "Bad sectors found, running hdparm commands..."
else
    echo "No bad sectors found."
    exit 0
fi

# Read each sector from the file and execute hdparm command
while read -r sector; do
    # Remove any whitespace or invalid lines
    if [[ "$sector" =~ ^[0-9]+$ ]]; then
        sudo hdparm --write-sector "$sector" --yes-i-know-what-i-am-doing "$DEVICE"
    fi
done < badsectors.txt

Then make it executable by running the below command.

chmod +x find_fix_bad_sectors.sh

Ins0mniA


Revision #17
Created 2025-08-08 00:12:30 EEST by Green
Updated 2025-09-04 02:04:20 EEST by Green