How to map out LBAs on a HDD using Linux (TrueNAS SCALE)
In order to map out LBAs if you are unable to use dd or similar commands, I have found that a secure erase of the entire HDD might work. This is a last attempt type operation.
NOTE: This procedure is specific for a normal HDD, not SSD/NVMe. Also, do not use dd to try to fix SSD/NVMe drive LBAs, which may also include Hybrid HDDs as well. Digital memory operates differently.
This will not remove most errors recorded in the logs, however a SMART Long tests should pass and no new LBA error messages should occur, unless the drive has more failures. If after this repair operation, more LBAs are reported failed (could be hours later or months later), assume the drive will continue to have media issues and just throw it away.
I cannot stress enough the importance of ensuring the drive you are erasing is the correct ādrive_idā and āserial numberā. There is no shame in verifying this several times before you reach steps 10 and 11.
READ THIS ENTIRE PROCEDURE BEFORE TRYING IT OUT ON YOUR SYSTEM !
-
You are expected to have some general knowledge about Linux and how to read the drive SMART data.
-
You need to be
rootorsudo. -
You will need SSH access to the server to open an SSH session.
-
You will be using an SSH window and using a
tmuxsession. -
Do not attempt to use these instructions without using a
tmuxsession. -
Your drive data will be destroyed!
-
This will consume a lot of time, and the system with your hard drive must remain powered on or possible bricking of the HDD is possible. It is highly recommended that the NAS system is running on an UPS in case of a power failure.
-
You will likely need to unplug the drives power connection, not cycle power or reboot the system.
-
In my example below, my drive starts as drive ID sdc, then after power removal and application it becomes sde. Be certain you are aware of your drive IDs. A blunder on your part could be costly.
-
tmux usage:
- To detach the tmux session, press CTRL+B and then D keys.
- To create a session, Enter
tmux new -s tsdriveto create a session ātsdriveā. - To list your sessions
tmux ls. - To attach to a session
tmux attach tsdrivewill attach session ātsdriveā if it exists. - This is not a tutorial on how to use
tmux. - If your SSH window goes away while the erase is occurring, reopen a ssh connection to the server and use the tmux attach command to restore the tmux session.
While I could have made this a script, I feel the importance of full control is prudent. If you do not understand these commands, feel free to look them up before using them.
Steps:
-
Open an SSH window and log in as
root. -
Enter the drive ID of your suspect HDD? (sda, sdb, sdcā¦) ________ Example:
sdc -
We are using tmux to ensure the SSH session remains open, even if we lose our SSH window by accident. It is not absolutely necessary, however it is prudent to use it.
Enter
tmux new -s tsdrivewhich will open a session titled ātsdriveā. -
Enter
smartctl -x /dev/drive_id(replaced drive ID with value from step 2.Note: You can use a pipe to output to a file as well as the screen for later reference. Ensure you use a valid path. The example below will create a file called
drive_sdc_step_4.txtin the path/mnt/pool/scriptsExample:
smartctl -x /dev/sdc | tee -a /mnt/pool/scripts/drive_sdc_step_4.txt -
Gather the following data if present:
a: Drive Serial Number ____________________ (Example: ZR13JRL0)
b: The list of bad LBAs is not needed for this procedure, however it is good to have information if you ever need it. List of bad/defect LBAs. If not present, the LBA reported as failed from a SMART Short or Long test. Start _____________, End _____________
c: Time for a secure erase to complete. _________________
d: ATA Security is: ________________________ (Enabled/Disabled, Frozen/Not Frozen)
-
If security is Enabled, Frozen, you must perform these steps:
a: Access the physical drive, ensure this is the correct drive using the physical serial number.
b: Unplug the drive power. This should work if you have a removable drive bay as well.
c: Connect the drive power.
d: The Drive ID likely will have changed, use
lsblk -o SERIAL,NAMEand then locate the SERIAL NUMBER you wrote down in step 5a.Example Return:
SERIAL NAME K1JUMW4D sda āāsda1 2F4920060284 sdb āāsdb1 āāsdb2 āāsdb3 ZR13JRL0 sde āāsde1 K1JRSWLD sdd āāsdd1e: Write down the new drive ID ________
Example:smartctl -x /dev/sdef: At this time look for
ATA Security is: Disabled, Not Frozen. If this is not correct, then the drive may not be recoverable on this computer system. -
If security is now Disabled, Not Frozen then continue. If not, you need to figure out how to unlock this drive. Seek further advice, maybe run a LIVE CD.
-
While this step may not be absolutely required, I use it just to ensure the drive does not timeout early.
Enter
smartctl -l scterc,300,300 /dev/drive_id(where $drive= value from step 6d).Example:
smartctl -l scterc,300,300 /dev/sde -
Verify the drive in Disabled and Not Frozen:
a: Enter
smartctl -g security /dev/drive_idand verify:Example:
smartctl -g security /dev/sdeATA Security is: Disabled, NOT FROZEN [SEC?]b. Enter
smartctl -x /dev/drive_id > original_drive.txtso we can save the original condition of the drive. -
Set a password, in this example we are using the lower case
p. This only lives with the drive until the secure erase is completed. If the drive does not complete the secure erase, this password may still be enabled.a: Enter
hdparm --user-master u --security-set-pass p /dev/drive_idand verify return value of:security_password" "p"Example: āhdparm --user-master u --security-set-pass p /dev/sdeā
-
WARNING !!! The next step will commence a secure erse operation. The drive power MUST NOT be interrupted until after the erase operation has been completed or you may end up with a BRICK.
a: Enter
hdparm --user-master u --security-erase p /dev/drive_idExample: āhdparm --user-master u --security-erase p /dev/sdeā
b: You should see:
hdparm --user-master u --security-erase p /dev/sde security_password: "p" /dev/sde: Issuing SECURITY_ERASE command, password="p", user=user -
If you had a value from step 5c, then you will wait at least this long before the prompt returns. My 6TB drive took roughly 10 hours, YMMV.
-
After the prompt returns, you should not see any error messages from hdparm.
-
Letās look at the drive SMART data, Enter
smartctl -x /dev/drive_idand you should have zero values for ID 5, 197, and 198. -
Run a SMART Short Test, Enter
smartctl -t short /dev/drive_idfor a quick check, wait 5+ minutes, Entersmartctl -a /dev/drive_idand verify the Short test passed. -
Run a SMART Long Test, Enter
smartctl -t long /dev/drive_idand wait for it to complete. -
Examine the SMART data to ensure the Long test completed properly and without errors, Enter
smartctl -a /dev/drive_idand verify the Long/Extended test passed. -
If the tests passed, your drive has been healed, for now.
-
Before using this drive, Reboot your system to āenableā the security features again.
-
Now lets record the final result:
Entersmartctl -x /dev/drive_id > fixed_drive.txt
or if the drive was not fixedsmartctl -x /dev/drive_it > not_fixed_drive.txt
Recovery from a power outage
This is purely experimental as I have not had a power outage while performing this task, however it is possible that if the power drops while the secure erase is occurring, the drive could be BRICKED. If this happens, repeat the steps above. The security password should still be āpā.
Perform:
Steps 1 through 9
Step 10 should not be required
Step 11 will hopefully result in:
Issuing SECURITY_ERASE command, password="p", user=user
If Step 11 fails the first time, run step 10 and then step 11 again.
If Step 11 fails a second time, the drive may be bricked.
If you suspect your drive has been bricked, move it to a different computer and try to perform a secure erase of this drive. There are programs that will issue this command and maybe one of those will work but I will not discuss it here.
EDIT (2/12/2026): Updated beginning text and added steps 9b and 21 to record the starting point and ending point of the drive SMART data for later comparison.