背景:
Linux系统(Ubuntu)在运行时,断电等非正常关机操作,会导致ext4文件系统数据损坏。严重时会导致系统崩溃。如下log就是系统数据损坏。
- [ 7.878756] EXT4-fs error (device mmcblk0p2): ext4_mb_generate_buddy:742: group 0, 14845 clusters in bitmap, 14822 in gd
- [ 8.484660] init: samba-ad-dc main process (995) terminated with status 1
- [ 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,如下红色字体部分就是系统错误的信息。
- ~# tune2fs -l /dev/mmcblk0p2
- tune2fs 1.42.9 (4-Feb-2014)
- Filesystem volume name: <none>
- Last mounted on: /
- Filesystem UUID: ab013911-6048-465f-8a1a-cf1420c7bb01
- Filesystem magic number: 0xEF53
- Filesystem revision #: 1 (dynamic)
- Filesystem features: ext_attr resize_inode dir_index filetype extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
- Filesystem flags: unsigned_directory_hash
- Default mount options: user_xattr acl
- Filesystem state: not clean with errors
- Errors behavior: Continue
- Filesystem OS type: Linux
- Inode count: 393216
- Block count: 1572864
- Reserved block count: 78643
- Free blocks: 289870
- Free inodes: 169990
- First block: 0
- Block size: 4096
- Fragment size: 4096
- Reserved GDT blocks: 383
- Blocks per group: 32768
- Fragments per group: 32768
- Inodes per group: 8192
- Inode blocks per group: 512
- Flex block group size: 16
- Filesystem created: Sat Sep 12 11:55:02 2015
- Last mount time: Mon Oct 5 00:56:20 2015
- Last write time: Mon Oct 5 00:56:32 2015
- Mount count: 49
- Maximum mount count: -1
- Last checked: Sat Sep 12 11:55:02 2015
- Check interval: 0 (<none>)
- Lifetime writes: 5936 MB
- Reserved blocks uid: 0 (user root)
- Reserved blocks gid: 0 (group root)
- First inode: 11
- Inode size: 256
- Required extra isize: 28
- Desired extra isize: 28
- Default directory hash: half_md4
- Directory Hash Seed: 37b6421d-4697-4ff5-a68e-4a1e1ea81c0e
- Journal backup: inode blocks
- <span style="color:#ff0000;">FS Error count: 5
- First error time: Mon Oct 5 00:52:47 2015
- First error function: ext4_mb_generate_buddy
- First error line #: 742
- First error inode #: 0
- First error block #: 0
- Last error time: Mon Oct 5 00:56:32 2015
- Last error function: ext4_mb_generate_buddy
- Last error line #: 742
- Last error inode #: 0
- Last error block #: 0</span>
修复方法:
1、手动修复:借助其他完整系统启动,对所在磁盘分区卸载,比如要修复/dev/mmcblk0p2,
执行命令 fsck.ext4 /dev/mmcblk0p2 可检查修复系统;
2、自动修复:
条件:
(1)、 自动修复要保证,bootloader参数bootargs 生命挂载以制度方式挂载根文件系统
console=tty1 console=ttySAC2,115200n8 root=UUID=e139ce78-9841-40fe-8823-96a304a09859 rootwait ro
如果最后ro是rw,将不能完成自动修复。
(2)、 编辑/etc/fstab 挂载最后一个选项设置为1,标明启动时自动检测文件系统,如下:
UUID=e139ce78-9841-40fe-8823-96a304a09859 / ext4 errors=remount-ro,noatime,nodiratime 01
(3)、 编辑 /etc/default/rcS 最后一个选项(其他linux系统有区别)
# automatically repair filesystems with inconsistencies during boot
FSCKFIX=yes
然后,可以参考/etc/init/mountall.conf
- description "Mount filesystems on boot"
- start on startup
- stop on starting rcS
- expect daemon
- task
- emits virtual-filesystems
- emits local-filesystems
- emits remote-filesystems
- emits all-swaps
- emits filesystem
- emits mounting
- emits mounted
- script
- . /etc/default/rcS || true
- [ -f /forcefsck ] && force_fsck="--force-fsck"
- [ "$FSCKFIX" = "yes" ] && fsck_fix="--fsck-fix"
- # Doesn't work so well if mountall is responsible for mounting /proc, heh.
- if [ -e /proc/cmdline ]; then
- read line < /proc/cmdline
- for arg in $line; do
- case $arg in
- -q|--quiet|-v|--verbose|--debug)
- debug_arg=$arg
- ;;
- esac
- done < /proc/cmdline
- fi
- # set $LANG so that messages appearing in plymouth are translated
- if [ -r /etc/default/locale ]; then
- . /etc/default/locale || true
- export LANG LANGUAGE LC_MESSAGES LC_ALL
- fi
- exec mountall --daemon $force_fsck $fsck_fix $debug_arg
- end script
- post-stop script
- rm -f /forcefsck 2>dev/null || true
- end script
Log:
系统启动检查修复过程的log,不在/var/log/fsck/目录下,而是在/var/log/upstart/目录下,文件为 mountall.log,如下:
- # cat mountall.log
- mount: mount point /media/boot does not exist
- mountall: mount /media/boot [382] terminated with status 32
- mountall: Filesystem could not be mounted: /media/boot
- Skipping /media/boot at user request
- Skipping /media/boot at user request
- Skipping /media/boot at user request
- fsck from util-linux 2.20.1
- e2fsck 1.42.9 (4-Feb-2014)
- /dev/mmcblk0p2: clean, 223220/393216 files, 1282976/1572864 blocks
其他:
也可以通过设置 系统挂载的次数来自动检查修复文件系统
比如:
tune2fs -c 30 /dev/mmcblk0p2 系统每启动30次,就会检查修复一次。
修复完成后,通过 tune2fs -l /dev/mmcblk0p2看到没有错误信息,如下:
- ~# tune2fs -l /dev/mmcblk0p2
- tune2fs 1.42.9 (4-Feb-2014)
- Filesystem volume name: <none>
- Last mounted on: /
- Filesystem UUID: ab013911-6048-465f-8a1a-cf1420c7bb01
- Filesystem magic number: 0xEF53
- Filesystem revision #: 1 (dynamic)
- Filesystem features: ext_attr resize_inode dir_index filetype extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
- Filesystem flags: unsigned_directory_hash
- Default mount options: user_xattr acl
- Filesystem state: not clean
- Errors behavior: Continue
- Filesystem OS type: Linux
- Inode count: 393216
- Block count: 1572864
- Reserved block count: 78643
- Free blocks: 289888
- Free inodes: 169996
- First block: 0
- Block size: 4096
- Fragment size: 4096
- Reserved GDT blocks: 383
- Blocks per group: 32768
- Fragments per group: 32768
- Inodes per group: 8192
- Inode blocks per group: 512
- Flex block group size: 16
- Filesystem created: Sat Sep 12 11:55:02 2015
- Last mount time: Mon Oct 5 00:58:56 2015
- Last write time: Mon Oct 5 00:58:41 2015
- Mount count: 1
- Maximum mount count: -1
- Last checked: Mon Oct 5 00:58:41 2015
- Check interval: 0 (<none>)
- Lifetime writes: 5938 MB
- Reserved blocks uid: 0 (user root)
- Reserved blocks gid: 0 (group root)
- First inode: 11
- Inode size: 256
- Required extra isize: 28
- Desired extra isize: 28
- Default directory hash: half_md4
- Directory Hash Seed: 37b6421d-4697-4ff5-a68e-4a1e1ea81c0e
- Journal backup: inode blocks