Can LVM mark / avoid bad blocks

btrfslvmmdadm

Since as btrfs doesn't track bad blocks, as a work-around this btrfs mailing list post suggested using an underlying mdadm RAID0 configuration for badblocks support.

Could LVM be used instead of mdadm for this purpose?

Best Answer

In general, as has been mentioned in a comment here and in the mailing list thread you linked to, modern hard drives which are so far gone they’ve got unreplaceable bad blocks should just be discarded. (You’ve explained why you’re interested in this, but it’s worth noting for other readers.)

I don’t think there’s anything in LVM to avoid bad blocks as such; typically you’d address that below LVM, at the device layer. One way of dealing with the problem is to use device mapper: create a table giving the sector mapping required to skip all bad blocks, and build a device using that. Such a table would look something like

0 98 linear /dev/sda 0
98 98 linear /dev/sda 99

etc. (this creates a 196-sector device, using /dev/sda but skipping sector 98). You give this to dmsetup:

dmsetup create nobbsda --table mytable

and then create a PV on the resulting /dev/nobbsda device (instead of /dev/sda).

Using this method, with a little forward-planning you can even handle failing sectors in the future, in the same way as a drive’s firmware: leave some sectors at the end of the drive free (or even dotted around the drive, if you want to spread the risk), and then use them to fill holes left by failing sectors. Using the above example, if we consider sectors starting from say 200 to be spare sectors, and sector 57 becomes bad:

0 57 linear /dev/sda 0
57 1 linear /dev/sda 200
58 40 linear /dev/sda 58
98 98 linear /dev/sda 99

Creating a device-mapper table using a list of bad sectors as given by badblocks is left as an exercise for the reader.

Another solution that would work with an existing LVM setup would be use pvmove’s ability to move physical extents in order to move LVs out of bad areas. But that wouldn’t prevent those areas from being re-used whenever a new LV is created or an existing LV resized or moved.

Related Question