背景:

Linux系统(Ubuntu)在运行时,断电等非正常关机操作,会导致ext4文件系统数据损坏。严重时会导致系统崩溃。如下log就是系统数据损坏。


检查方法:

1. [    7.878756] EXT4-fs error (device mmcblk0p2): ext4_mb_generate_buddy:742: group 0, 14845 clusters in bitmap, 14822 in gd  
2. [ 8.484660] init: samba-ad-dc main process (995) terminated with status 1
3. [ 14.248075] EXT4-fs error (device mmcblk0p2): ext4_mb_generate_buddy:742: group 1, 1 clusters in bitmap, 2 in gd

1、开机log,如上log就是开机时,kernel监测到文件系统错误;

2、比如要检查的分区是/dev/mmcblk0p2,如下红色字体部分就是系统错误的信息。



1. ~# tune2fs -l /dev/mmcblk0p2  
2. tune2fs 1.42.9 (4-Feb-2014)
3. Filesystem volume name: <none>
4. Last mounted on: /
5. Filesystem UUID: ab013911-6048-465f-8a1a-cf1420c7bb01
6. Filesystem magic number: 0xEF53
7. Filesystem revision #: 1 (dynamic)
8. Filesystem features: ext_attr resize_inode dir_index filetype extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
9. Filesystem flags: unsigned_directory_hash
10. Default mount options: user_xattr acl
11. Filesystem state: not clean with errors
12. Errors behavior: Continue
13. Filesystem OS type: Linux
14. Inode count: 393216
15. Block count: 1572864
16. Reserved block count: 78643
17. Free blocks: 289870
18. Free inodes: 169990
19. First block: 0
20. Block size: 4096
21. Fragment size: 4096
22. Reserved GDT blocks: 383
23. Blocks per group: 32768
24. Fragments per group: 32768
25. Inodes per group: 8192
26. Inode blocks per group: 512
27. Flex block group size: 16
28. Filesystem created: Sat Sep 12 11:55:02 2015
29. Last mount time: Mon Oct 5 00:56:20 2015
30. Last write time: Mon Oct 5 00:56:32 2015
31. Mount count: 49
32. Maximum mount count: -1
33. Last checked: Sat Sep 12 11:55:02 2015
34. Check interval: 0 (<none>)
35. Lifetime writes: 5936 MB
36. Reserved blocks uid: 0 (user root)
37. Reserved blocks gid: 0 (group root)
38. First inode: 11
39. Inode size: 256
40. Required extra isize: 28
41. Desired extra isize: 28
42. Default directory hash: half_md4
43. Directory Hash Seed: 37b6421d-4697-4ff5-a68e-4a1e1ea81c0e
44. Journal backup: inode blocks
45. <span style="color:#ff0000;">FS Error count: 5
46. First error time: Mon Oct 5 00:52:47 2015
47. First error function: ext4_mb_generate_buddy
48. First error line #: 742
49. First error inode #: 0
50. First error block #: 0
51. Last error time: Mon Oct 5 00:56:32 2015
52. Last error function: ext4_mb_generate_buddy
53. Last error line #: 742
54. Last error inode #: 0
55. Last error block #: 0</span>

修复方法:

1、手动修复:借助其他完整系统启动,对所在磁盘分区卸载,比如要修复/dev/mmcblk0p2,

执行命令 fsck.ext4 /dev/mmcblk0p2 可检查修复系统;


2、自动修复:

条件:

(1)、

            console=tty1 console=ttySAC2,115200n8 root=UUID=e139ce78-9841-40fe-8823-96a304a09859 rootwait ro

  如果最后ro是rw,将不能完成自动修复。

(2)、 

UUID=e139ce78-9841-40fe-8823-96a304a09859       /       ext4    errors=remount-ro,noatime,nodiratime            01

(3)、

# automatically repair filesystems with inconsistencies during boot
FSCKFIX=yes

然后,可以参考/etc/init/mountall.conf


1. description     "Mount filesystems on boot"  
2.
3. start on startup
4. stop on starting rcS
5.
6. expect daemon
7. task
8.
9. emits virtual-filesystems
10. emits local-filesystems
11. emits remote-filesystems
12. emits all-swaps
13. emits filesystem
14. emits mounting
15. emits mounted
16.
17. script
18. default/rcS || true
19. "--force-fsck"
20. "$FSCKFIX" = "yes" ] && fsck_fix="--fsck-fix"
21.
22. # Doesn't work so well if mountall is responsible for mounting /proc, heh.
23. if [ -e /proc/cmdline ]; then
24. read line < /proc/cmdline
25. for arg in $line; do
26. case $arg in
27. -q|--quiet|-v|--verbose|--debug)
28. debug_arg=$arg
29. ;;
30. esac
31. done < /proc/cmdline
32. fi
33. # set $LANG so that messages appearing in plymouth are translated
34. if [ -r /etc/default/locale ]; then
35. default/locale || true
36. export LANG LANGUAGE LC_MESSAGES LC_ALL
37. fi




1.     exec mountall --daemon $force_fsck $fsck_fix $debug_arg  
2. end script




1. post-stop script  
2. true
3. end script

(4)、 系统检测到分区有问题时,会再根目录下创建一个空文件/forcefsck,重启后,执行mountall,自动进行修复,然后删除forcefsck,也可以手动创建/forcefsck,系统同样会在下次启动时强制检查修复文件系统;


Log:

系统启动检查修复过程的log,不在/var/log/fsck/目录下,而是在/var/log/upstart/目录下,文件为 mountall.log,如下:


1. # cat mountall.log  
2. mount: mount point /media/boot does not exist
3. mountall: mount /media/boot [382] terminated with status 32
4. mountall: Filesystem could not be mounted: /media/boot
5. Skipping /media/boot at user request
6. Skipping /media/boot at user request
7. Skipping /media/boot at user request
8. fsck from util-linux 2.20.1
9. e2fsck 1.42.9 (4-Feb-2014)
10. /dev/mmcblk0p2: clean, 223220/393216 files, 1282976/1572864 blocks

其他:

也可以通过设置 系统挂载的次数来自动检查修复文件系统

比如:

tune2fs -c 30 /dev/mmcblk0p2 系统每启动30次,就会检查修复一次。


修复完成后,通过 tune2fs -l /dev/mmcblk0p2看到没有错误信息,如下:




1. ~# tune2fs -l /dev/mmcblk0p2  
2. tune2fs 1.42.9 (4-Feb-2014)
3. Filesystem volume name: <none>
4. Last mounted on: /
5. Filesystem UUID: ab013911-6048-465f-8a1a-cf1420c7bb01
6. Filesystem magic number: 0xEF53
7. Filesystem revision #: 1 (dynamic)
8. Filesystem features: ext_attr resize_inode dir_index filetype extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
9. Filesystem flags: unsigned_directory_hash
10. Default mount options: user_xattr acl
11. Filesystem state: not clean
12. Errors behavior: Continue
13. Filesystem OS type: Linux
14. Inode count: 393216
15. Block count: 1572864
16. Reserved block count: 78643
17. Free blocks: 289888
18. Free inodes: 169996
19. First block: 0
20. Block size: 4096
21. Fragment size: 4096
22. Reserved GDT blocks: 383
23. Blocks per group: 32768
24. Fragments per group: 32768
25. Inodes per group: 8192
26. Inode blocks per group: 512
27. Flex block group size: 16
28. Filesystem created: Sat Sep 12 11:55:02 2015
29. Last mount time: Mon Oct 5 00:58:56 2015
30. Last write time: Mon Oct 5 00:58:41 2015
31. Mount count: 1
32. Maximum mount count: -1
33. Last checked: Mon Oct 5 00:58:41 2015
34. Check interval: 0 (<none>)
35. Lifetime writes: 5938 MB
36. Reserved blocks uid: 0 (user root)
37. Reserved blocks gid: 0 (group root)
38. First inode: 11
39. Inode size: 256
40. Required extra isize: 28
41. Desired extra isize: 28
42. Default directory hash: half_md4
43. Directory Hash Seed: 37b6421d-4697-4ff5-a68e-4a1e1ea81c0e
44. Journal backup: inode blocks