8

I try to reallocate a bad sector. After reading this Question my understanding is, that the hard-drive tries to reallocate automatically. To trigger the process I tried to write to the sector with dd but nothing changed at all.

$ smartctl -a /dev/sda
...
5 Reallocated_Sector_Ct   ...       0
197 Current_Pending_Sector  ...       1
...

The bad sector is: 215040

$ dd if=/dev/sda of=/dev/null bs=512 count=1 skip=215040
io-error on reading

Why is the sector not allocated when I write to it?

$ dd if=/dev/zero of=/dev/sda bs=512 count=1 seek=215040
io-error on writing

How can I cause the sector to be rewritten?

user
  • 29,449
  • 11
  • 99
  • 144
testo
  • 198
  • 2
  • 7

1 Answers1

9

Try a low-level write using hdparm:

sudo hdparm --yes-i-know-what-i-am-doing --write-sector 215040 /dev/sda

… where 215040 is the sector number you want to overwrite (and possibly remap) and /dev/sda is the device to which you want to write.

From the hdparm man page:

--write-sector

Writes zeros to the specified sector number. VERY DANGEROUS. The sector number must be given (base10) after this flag. hdparm will issue a low-level write (completely bypassing the usual block layer read/write mechanisms) to the specified sector. This can be used to force a drive to repair a bad sector (media error).

Deltik
  • 19,353
  • 17
  • 73
  • 114
  • Hi. I was currently editing my question. You solution works fine. I think (I can't test it anymore) the problem with my dd command was the sector size of 512 which is logical. The physical size is 4096. – testo Sep 29 '15 at 09:10
  • 1
    @doev: Most drives emulate 512-byte sectors regardless of the physical sector size. You can confirm the sector size (in bytes) that the operating system sees using this command: `cat /sys/block/sda/queue/hw_sector_size` – Deltik Sep 29 '15 at 09:13
  • interesting, hdparm --read-sector ... can read the sector now but the dd command is still failing with io-error. – testo Sep 29 '15 at 09:16
  • 2
    @doev: You seem to have a different version of `dd` from what I'm familiar with. I've never seen `dd` output "`io-error on writing`" before, and that string doesn't show up anywhere in the [GNU coreutils](http://www.gnu.org/software/coreutils/coreutils.html) source code. – Deltik Sep 29 '15 at 09:20
  • 1
    The correct error message is: "dd: Fehler beim Lesen von »/dev/sda“: Eingabe-/Ausgabefehler" - dd (coreutils) 8.21 – testo Sep 29 '15 at 09:27
  • How can I do the same, but write the data that a successful `--read-sector` gave me? – Evi1M4chine Sep 06 '16 at 15:17
  • @Evi1M4chine: Why do you need to do this? The data was read. If your data is valuable, you should be backing up more than just individual sectors. – Deltik Sep 06 '16 at 15:28
  • @Deltik: Because it only reads about every 10th time, and the last time it did, I kept the data, as it may never read again. Why would I *not* do this? The data is not valuable, but I’m not the type of careless lazy ignorant loser undividual who would just accept it as being unable to do it. It is clearly a useful tool to have for many useful cases. – Evi1M4chine Sep 07 '16 at 16:16
  • @Evi1M4chine: You can take the hex values, convert them into a sector of data, then write them back to the same logical place with `dd if=SECTOR_DUMP of=/dev/YOUR_DISK bs=SECTOR_SIZE seek=ORIGINAL_SECTOR`. – Deltik Sep 07 '16 at 16:34
  • @Deltik: Yeah, that’s the thing…They have a weird byte order and when I use some tool like `xxd` to convert them back to binary, and then use a hex editor on them, I get different values. – Evi1M4chine Sep 08 '16 at 20:03
  • 1
    @Evi1M4chine: This command turns the `hdparm` sector output to binary: `sudo hdparm --read-sector SECTOR /dev/YOUR_DISK | grep -Eo '([0-9a-f]{4} ){7}[0-9a-f]{4}' | xxd -r -p` – Deltik Sep 08 '16 at 20:11
  • @testo, I also wonder if the original dd write would have worked (no error) if it had been aligned with the beginning of the physical sector, and bs=4096` had been used. Then dd would not need to read-modify-write, just overwrite, all 4096B so there would be no read-failure preventing the write. I had the same problem today but I also used hdparm before the idea occurred to m to try to align the wrote with the 4096 sector size. – reikred Jan 20 '22 at 19:31