查看syslog日志发现有硬盘提示扇区错误:
Dec 30 07:08:27 ubuntu-20 kernel: [34767.158065] blk_update_request: critical medium error,
dev sda, sector 1729898440 op 0x0:(READ) flags 0x0 phys_seg 1 prio class 0
badblocks查看出错的扇区数量,-b指定每次扫描的区块大小,设置为和扇区大小一致,1729898440为日志提示出错的扇区位置,1729898499指定扫描扇区的终止位置,扫描后可以看到共有8个扇区出错。
badblocks -b 512 /dev/sda 1729898499 1729898440
1729898440
1729898441
1729898442
1729898443
1729898444
1729898445
1729898446
1729898447
写入修复坏掉的扇区,注意会丢失之前的数据,如果有坏道硬盘会自动重映射坏掉的扇区。
badblocks -b 512 -w /dev/sda 1729898447 1729898440
扫描整盘,-b指定每次扫描的块大小,如果用于扇区写入为512,文件系统隔离为4096,-s显示进度,-v详细输出。
badblocks -b 512 -s -v -o badblocks.log /dev/sdf
读取生成的坏块列表使用badblocks写测试能否修复或重映射扇区。
<?php
$list = file('badblocks.log');
$arr = array();
$i = 0;
$last = 0;
foreach ($list as $v) {
$v = trim($v);
if (!$v) {
continue;
}
if (!$last) {
$arr[$i][] = $v;
$last = $v;
continue;
}
if (($v - $last) == 1) {
$arr[$i][] = $v;
} else {
$i++;
$arr[$i][] = $v;
}
$last = $v;
}
foreach ($arr as $vv) {
if (count($vv) > 1) {
$start = reset($vv);
$end = end($vv);
} else {
$start = reset($vv);
$end = reset($vv);
}
echo '/usr/sbin/badblocks -w -s -v /dev/sdf '."$end $start".PHP_EOL;
shell_exec('/usr/sbin/badblocks -w -s -v /dev/sdf '."$end $start");
}
也可使用hdparm和dd写扇区:
hdparm --write-sector 77610960 --yes-i-know-what-i-am-doing /dev/sdf
dd if=/dev/zero of=/dev/sdf bs=512 seek=77610960 count=1
或重写整个硬盘:
dd iflag=fullblock oflag=direct conv=noerror,notrunc if=/dev/zero of=/dev/sdf bs=1M
对于不能修复的坏块让文件系统隔离,未经验证。
e2fsck -l badblocks.log /dev/sdf
mkfs -l badblocks.log /dev/sdf
参考:
https://zhuanlan.zhihu.com/p/31899347
https://www.linuxcool.com/badblocks
https://lug.ustc.edu.cn/wiki/linux_digest/badblocks/
https://tomjamescn.github.io/post/2019-06-16-repair-hard-disk-with-bad-sectors/
https://unix.stackexchange.com/questions/229354/how-to-ignore-write-errors-while-zeroing-a-disk/229362
https://superuser.com/questions/1322780/fix-or-reallocate-a-bad-sector
https://askubuntu.com/questions/751134/problems-restoring-damaged-sectors-on-hdd