修复错误配置/etc/fstab文件导致系统无法正常启动

时间:2023-12-28 20:27:02
1.文件介绍

/etc/fstab这个文件描述系统中各种文件系统的信息,应用程序读取这个文件,然后根据其内容进行自动挂载的工作。作为系统配置文件,fstab通常都位于/etc目录下,它包括了所有分区和存储设备的信息,以及它们应该挂载到哪里,以什么样子的方式挂载。如果遇到一些类似于无法挂载你的windows分区,无法使用你的光驱,无法对某个分区进行写入操作什么的,那么基本上可以断定,你的fstab内容有问题了。也就是说,你可以通过修改它来搞定这些问题.

2.系统环境
(1)系统:centos 6.3 x86_64
(2)分区情况:

修复错误配置/etc/fstab文件导致系统无法正常启动

fstab文件:

修复错误配置/etc/fstab文件导致系统无法正常启动
附:分区卷标的查看和修改
1.查看分区的label  
e2label /dev/sda1
2.修改分区的label
e2label /dev/sda1 /

3.模拟出错
3.1 为虚拟机扩容
在本地计算机上运行:开始-运行-cmd,输入"D:\Program Files (x86)\vmvare\vmware-vdiskmanager.exe" -x 5GB “D:\vmware\centos 6.3_x86-64\CentOS 6.3 64-bit.vmdk"

修复错误配置/etc/fstab文件导致系统无法正常启动

参数”-x”指定扩容后整个空间大小(包含现在正在使用的),”D:\vmware\centos 6.3_x86-64\CentOS 6.3 64-bit.vmdk"” 指定需要
扩容的虚拟机磁盘文件。

3.2 再次查看扩容后的硬盘情况

修复错误配置/etc/fstab文件导致系统无法正常启动
可以看到,硬盘容量已经增加了。

3.3 在/dev/sda上创建新的主分区sda4
[root@localhost ~]# fdisk /dev/sda #指定硬盘

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n #添加新分区
Command action
   e   extended
   p   primary partition (1-4)
p #创建主分区
Selected partition 4
First cylinder (2611-3263, default 2611):  按Enter #盘符开始,这里我们默认
Using default value 2611
Last cylinder, +cylinders or +size{K,M,G} (2611-3263, default 3263): 按Enter #盘符结束,这里我们默认
Using default value 3263

Command (m for help): p #查看

Disk /dev/sda: 26.8 GB, 26843545600 bytes
255 heads, 63 sectors/track, 3263 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0005f043

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2354    18598912   83  Linux
/dev/sda3            2354        2611     2064384   82  Linux swap / Solaris
/dev/sda4            2611        3263     5238527+  83  Linux

Command (m for help): w #保存
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
 
3.4 格式化新分区
[root@localhost ~]# mkfs -t ext3 /dev/sda4 
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1309631 blocks
65481 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

3.5将Label设置为other
[root@localhost ~]# e2label /dev/sda4 others

修复错误配置/etc/fstab文件导致系统无法正常启动

3.6 挂载 /dev/sda4到 /others
[root@localhost ~]# mkdir /others
[root@localhost ~]# mount /dev/sda4 /others

修复错误配置/etc/fstab文件导致系统无法正常启动

3.7 修改fstab文件
(添加蓝色那一行)
[root@localhost ~]# vi /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Thu Apr 25 09:01:27 2013
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=89eb5a53-3c5c-4a5c-931d-ec8c5407ed68 /                       ext4    defaults        1 1
UUID=4cdac1ad-5ec3-4517-bd55-01441723c5fa /boot                   ext4    defaults        1 2
UUID=d208836e-e33c-420f-baef-bf19e32d60cc swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
LABEL=/others           /others                 ext3    defaults        1 2

请注意:这里用到是LABEL而不是设备名(/dev/sda4),但是这里把卷标写错,应该是others,前面没有 /。
    并且没有运行mount -a命令来验证配置是否正确。
    接着就直接重启系统了(init 6)
[root@localhost ~]# init 6

3.8 出现问题

修复错误配置/etc/fstab文件导致系统无法正常启动

系统发现fstab中的LABEL=/others卷标不存在!!!

4.故障修复

4.1进入运行级别1
    输入root用户密码,进入运行级别1

修复错误配置/etc/fstab文件导致系统无法正常启动

此时试图修改/etc/fstab文件
[root@localhost ~]# vi /etc/fstab

修复错误配置/etc/fstab文件导致系统无法正常启动

此时发现文件系统是只读的
    重新mount /

修复错误配置/etc/fstab文件导致系统无法正常启动

再次修改fstab文件   
[root@localhost ~]# vi /etc/fstab

修复错误配置/etc/fstab文件导致系统无法正常启动

修改完成,重启系统(init 6)。
    系统可以正常启动,问题解决了。

5.总结
     以上问题的出现是由于错误配置了/etc/fstab文件,在系统重启时,无法识别卷标(/others),从而导致无法正常启动。如果在修改/etc/fstab文件后,运行mount -a命令验证一下配置是否正确,则可以避免此类问题。
    问题的修复方法不仅适用于以上所描述的问题,同样可以处理由于错误修改配置导致系统无法启动的其他问题。
    问题的解决过程中,重新mount /是比较关键的一步(mount -n -o remount,rw /)。如果没有此步操作,则文件系统处于只读状态,导致不能修改配置文件并保存,修复存在的问题。