Ubuntu 磁盘修复
在 ubuntu 文件系统变成只读模式,该处理呢?
文件系统内部的错误,如索引错误、元数据损坏等,也可能导致系统进入只读状态。磁盘坏道或硬件故障也可能引发文件系统只读的问题。/etc/fstab配置错误,可能导致文件系统被错误地挂载为只读模式。接下来跟着文章逐步排查、修复问题。
作者:炭烤毛蛋 ,点击博主了解更多。
提示:数据无价,操作需谨慎。
文章目录
- Ubuntu 磁盘修复
- 1. 硬盘错误日志
- 2. 硬盘错误分析
- 3. 硬盘数据恢复
- 3.1 借助 fsck 命令修复文件系统
- 3.2 借助 smartctl 检查硬盘健康状态
- 结语
1. 硬盘错误日志
Ubuntu 系统在正常使用过程突然出现文件系统变为只读,没过多久系统重启。
重启后系统无法正常开机,而是停留在启动的命令终端。
- 启动日志
Ubuntu 22.04.2 LTS Linux tty1
Cloud-init[1327] v.23.1.2-OubuntuO~22.04.1 running 'moudles:config' at Mon Aug 5 11:16:56 PM CST 2019 +0000. Up 199.40 seconds.
Cloud-init[1334] v.23.1.2-OubuntuO~22.04.1 running 'moudles:final' at Mon Aug 5 11:16:56 PM CST 2019 +0000. Up 199.92 seconds.
Cloud-init[1334] v.23.1.2-OubuntuO~22.04.1 finished at Mon Aug 5 11:16:57 PM CST 2019 +0000. Datasource DataSourceNone. Up 200.06 seconds.
Cloud-init[1334] Mon Aug 5 11:16:57 PM CST 2019 - cc_final_message.py[WWARNING]: Used failback datasource
blk_update_request: I/O error, dev sda, sector 24460536 op 0x0:(READ) flag 0x80700 phys_seg 2 prio class 0
blk_update_request: I/O error, dev sda, sector 24460576 op 0x0:(READ) flag 0x80700 phys_seg 1 prio class 0
blk_update_request: I/O error, dev sda, sector 24460624 op 0x0:(READ) flag 0x80700 phys_seg 1 prio class 0
blk_update_request: I/O error, dev sda, sector 24460576 op 0x0:(READ) flag 0x0 phys_seg 1 prio class 0
blk_update_request: I/O error, dev sda, sector 24460576 op 0x0:(READ) flag 0x0 phys_seg 1 prio class 0
blk_update_request: I/O error, dev sda, sector 1835808720 op 0x0:(READ) flag 0x80700 phys_seg 4 prio class 0
blk_update_request: I/O error, dev sda, sector 1835808720 op 0x0:(READ) flag 0x0 phys_seg 1 prio class 0
blk_update_request: I/O error, dev sda, sector 24466768 op 0x0:(READ) flag 0x80700 phys_seg 2 prio class 0
blk_update_request: I/O error, dev sda, sector 24466808 op 0x0:(READ) flag 0x80700 phys_seg 1 prio class 0
blk_update_request: I/O error, dev sda, sector 24466768 op 0x0:(READ) flag 0x0 phys_seg 1 prio class 0
从启动日志分析磁盘可能出现坏道,或者坏块影响正常使用。
- 尝试修复启动异常
尝试重新挂载文件系统,发现仍然为只读模式。
mount -o remount,rw /
尝试显示文件系统的元数据,如果看到检测异常信息,很大概率是磁盘问题。
tune2fs -l /dev/sda | grep checked
2. 硬盘错误分析
磁盘损坏可能导致文件系统不稳定或丢失数据,影响系统性能甚至导致系统无法启动。如果磁盘出现严重的物理故障,可能需要更换硬盘或备份数据后进行恢复。
-
bios 查看磁盘状态
重新进入 BIOS 查看硬盘,发现硬盘已经掉线,使用机械硬盘从现象确认磁盘存在坏道问题。
-
重新挂载磁盘
raid0 阵列硬盘出现硬盘损坏情况,系统默认记录当前状态。想要重新挂载该磁盘需要先清除磁盘标识,再尝试挂载硬盘。
磁盘挂载成功,然后仍然无法进入系统。好消息确认磁盘已损坏,坏消息磁盘有大量数据。
抱着能找回尽量找回的原则,可以尝试从 U 盘系统修复磁盘。 -
U盘引导进入系统
借助带有 Ubuntu 安装 U盘,然后进入到 Ubuntu 尝试安装的临时系统,然后尝试修复损坏磁盘。
制作启动U盘和U盘启动,此处省略 1万字,需要的小伙伴自行百度。进入U盘系统按照如下方法修复磁盘。
3. 硬盘数据恢复
在 Ubuntu 系统中,修复磁盘通常是为了修复文件系统的损坏或修复磁盘上的错误。修复 Ubuntu 系统中的磁盘问题,通常包括使用 fsck 修复文件系统错误、使用 smartctl 检查硬盘健康状态,或者通过图形界面工具进行管理。
3.1 借助 fsck 命令修复文件系统
fsck(文件系统检查器)是 Linux 系统中用于检查和修复文件系统错误的工具。使用此命令可以扫描文件系统并自动修复文件系统中的错误。
- 检查磁盘
fsck /dev/sda
/dev/sda 可以替换为需要修复的磁盘设备分区,第一个磁盘分区,则使用 /dev/sda1。
- 尝试自动修复磁盘
使用 fsck 尝试自动修复磁盘,该命可以自动修复错误。
fsck -y /dev/sda
提示:磁盘正在被使用(比如挂载了某个分区),你需要先卸载该分区再执行恢复磁盘数据。
- 强制修复
fsck -f /dev/sda
好在磁盘损坏较轻,经过简单修复可以读到原来数据。接下来备份磁盘内容就不必多讲。
- fsck 命令详细注释
Usage:fsck [options] -- [fs-options] [<filesystem> ...]Check and repair a Linux filesystem.Options:-A check all filesystems-C [<fd>] display progress bar; file descriptor is for GUIs-l lock the device to guarantee exclusive access-M do not check mounted filesystems-N do not execute, just show what would be done-P check filesystems in parallel, including root-R skip root filesystem; useful only with '-A'-r [<fd>] report statistics for each device checked;file descriptor is for GUIs-s serialize the checking operations-T do not show the title on startup-t <type> specify filesystem types to be checked;<type> is allowed to be a comma-separated list-V explain what is being done-?, --help display this help--version display versionSee the specific fsck.* commands for available fs-options.
For more details see fsck(8).
3.2 借助 smartctl 检查硬盘健康状态
如果怀疑磁盘存在物理问题,可以使用 smartctl 工具来检查硬盘的健康状况,特别是对于固态硬盘 (SSD) 和机械硬盘 (HDD) 都非常有用。
- 安装 stmartmontools
sudo apt update
sudo apt install smartmontools
- 检查硬盘状态
smartctl -a /dev/sda
将输出硬盘的健康状态报告,包括错误日志和一些详细的健康信息。看到“Reallocated Sector Count”或“Pending Sector”,可能硬盘正在出现物理故障。
- smartctl 命令详细注释
smartctl 7.2 2020-12-30 r5155 [x86_64-linux-5.15.167.4-microsoft-standard-WSL2] (local build)
Copyright (C) 2002-20, Bruce Allen, Christian Franke, www.smartmontools.orgUsage: smartctl [options] device============================================ SHOW INFORMATION OPTIONS =====-h, --help, --usageDisplay this help and exit-V, --version, --copyright, --licensePrint license, copyright, and version information and exit-i, --infoShow identity information for device--identify[=[w][nvb]]Show words and bits from IDENTIFY DEVICE data (ATA)-g NAME, --get=NAMEGet device setting: all, aam, apm, dsn, lookahead, security,wcache, rcache, wcreorder, wcache-sct-a, --allShow all SMART information for device-x, --xallShow all information for device--scanScan for devices--scan-openScan for devices and try to open each device================================== SMARTCTL RUN-TIME BEHAVIOR OPTIONS =====-j, --json[=cgiosuvy]Print output in JSON or YAML format-q TYPE, --quietmode=TYPE (ATA)Set smartctl quiet mode to one of: errorsonly, silent, noserial-d TYPE, --device=TYPESpecify device type to one of:ata, scsi[+TYPE], nvme[,NSID], sat[,auto][,N][+TYPE], usbcypress[,X], usbjmicron[,p][,x][,N], usbprolific, usbsunplus, sntjmicron[,NSID], sntrealtek, intelliprop,N[+TYPE], jmb39x[-q],N[,sLBA][,force][+TYPE], jms56x,N[,sLBA][,force][+TYPE], marvell, areca,N/E, 3ware,N, hpt,L/M/N, megaraid,N, aacraid,H,L,ID, cciss,N, auto, test-T TYPE, --tolerance=TYPE (ATA)Tolerance: normal, conservative, permissive, verypermissive-b TYPE, --badsum=TYPE (ATA)Set action on bad checksum to one of: warn, exit, ignore-r TYPE, --report=TYPEReport transactions (see man page)-n MODE[,STATUS], --nocheck=MODE[,STATUS] (ATA, SCSI)No check if: never, sleep, standby, idle (see man page)============================== DEVICE FEATURE ENABLE/DISABLE COMMANDS =====-s VALUE, --smart=VALUEEnable/disable SMART on device (on/off)-o VALUE, --offlineauto=VALUE (ATA)Enable/disable automatic offline testing on device (on/off)-S VALUE, --saveauto=VALUE (ATA)Enable/disable Attribute autosave on device (on/off)-s NAME[,VALUE], --set=NAME[,VALUE]Enable/disable/change device setting: aam,[N|off], apm,[N|off],dsn,[on|off], lookahead,[on|off], security-freeze,standby,[N|off|now], wcache,[on|off], rcache,[on|off],wcreorder,[on|off[,p]], wcache-sct,[ata|on|off[,p]]======================================= READ AND DISPLAY DATA OPTIONS =====-H, --healthShow device SMART health status-c, --capabilities (ATA, NVMe)Show device SMART capabilities-A, --attributesShow device SMART vendor-specific Attributes and values-f FORMAT, --format=FORMAT (ATA)Set output format for attributes: old, brief, hex[,id|val]-l TYPE, --log=TYPEShow device log. TYPE: error, selftest, selective, directory[,g|s],xerror[,N][,error], xselftest[,N][,selftest], background,sasphy[,reset], sataphy[,reset], scttemp[sts,hist],scttempint,N[,p], scterc[,N,M], devstat[,N], defects[,N], ssd,gplog,N[,RANGE], smartlog,N[,RANGE], nvmelog,N,SIZE-v N,OPTION , --vendorattribute=N,OPTION (ATA)Set display OPTION for vendor Attribute N (see man page)-F TYPE, --firmwarebug=TYPE (ATA)Use firmware bug workaround:none, nologdir, samsung, samsung2, samsung3, xerrorlba, swapid-P TYPE, --presets=TYPE (ATA)Drive-specific presets: use, ignore, show, showall-B [+]FILE, --drivedb=[+]FILE (ATA)Read and replace [add] drive database from FILE[default is +/etc/smart_drivedb.hand then /var/lib/smartmontools/drivedb/drivedb.h]============================================ DEVICE SELF-TEST OPTIONS =====-t TEST, --test=TESTRun test. TEST: offline, short, long, conveyance, force, vendor,N,select,M-N, pending,N, afterselect,[on|off]-C, --captiveDo test in captive mode (along with -t)-X, --abortAbort any non-captive test on device=================================================== SMARTCTL EXAMPLES =====smartctl --all /dev/sda (Prints all SMART information)smartctl --smart=on --offlineauto=on --saveauto=on /dev/sda(Enables SMART on first disk)smartctl --test=long /dev/sda (Executes extended disk self-test)smartctl --attributes --log=selftest --quietmode=errorsonly /dev/sda(Prints Self-Test & Attribute errors)smartctl --all --device=3ware,2 /dev/sdasmartctl --all --device=3ware,2 /dev/twe0smartctl --all --device=3ware,2 /dev/twa0smartctl --all --device=3ware,2 /dev/twl0(Prints all SMART info for 3rd ATA disk on 3ware RAID controller)smartctl --all --device=hpt,1/1/3 /dev/sda(Prints all SMART info for the SATA disk attached to the 3rd PMPortof the 1st channel on the 1st HighPoint RAID controller)smartctl --all --device=areca,3/1 /dev/sg2(Prints all SMART info for 3rd ATA disk of the 1st enclosureon Areca RAID controller)
结语
不枉博主详细讲解,欢迎订阅博主–炭烤毛蛋 。