【命令星级】 ★★★★☆
【功能说明】
resize2fs命令用于扩容或收集未挂载的ext2/ext3/ext4文件系统。在Linux 2.6或更高版本的内核中,该命令还支持在线扩容已经挂载的文件系统,该命令常用来针对LVM扩容后的分区使用。
【语法格式】
resize2fs [option] [device]
resize2fs [选项] [设备名]
**说明:**在resize2fs命令及后面的选项和设备名里,每个元素之间都至少要有一个空格。
【选项说明】
表8-8针对该命令的参数选项进行了说明。
表8-8 resize2fs命令的参数选项及说明
**范例8-16:**动态修改分区大小的例子。
出现这种情况一般是当初对系统分区的时候没有规划好,限制想要重新调整分区的大小。假设是要对/dev/sdb上的分区进行操作,将/mnt/data1扩容。
[root@centos7 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-2097151, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +200M
Partition 1 of type Linux and of size 200 MiB is set
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (411648-2097151, default 411648):
Using default value 411648
Last sector, +sectors or +size{K,M,G} (411648-2097151, default 2097151): +200M
Partition 2 of type Linux and of size 200 MiB is set
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xe8dce880
Device Boot Start End Blocks Id System
/dev/sdb1 2048 411647 204800 83 Linux
/dev/sdb2 411648 821247 204800 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos7 ~]# partprobe /dev/sdb
[root@centos7 ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33816576
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
[root@centos7 ~]# mkfs.ext4 /dev/sdb2
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
52752 inodes, 210688 blocks
10534 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=216006656
7 block groups
32768 blocks per group, 32768 fragments per group
7536 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
[root@centos7 ~]# ls /mnt
hgfs
[root@centos7 ~]# mkdir /mnt/data1
[root@centos7 ~]# mkdir /mnt/data2
[root@centos7 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 17G 0 part /
sdb 8:16 0 1G 0 disk
├─sdb1 8:17 0 200M 0 part /mnt/data1
└─sdb2 8:18 0 200M 0 part /mnt/data2
sr0 11:0 1 9.6G 0 rom
[root@centos7 ~]# mount /dev/sdb1 /mnt/data1
[root@centos7 ~]# mount /dev/sdb2 /mnt/data2
[root@centos7 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 476M 0 476M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.6M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda3 17G 2.4G 15G 14% /
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb1 190M 1.6M 175M 1% /mnt/data1
/dev/sdb2 190M 1.6M 175M 1% /mnt/data2
[root@centos7 ~]# touch /mnt/data1/{1..5}.html
[root@centos7 ~]# touch /mnt/data2/{a..e}.html
[root@centos7 ~]# ls /mnt/data1
1.html 2.html 3.html 4.html 5.html
[root@centos7 ~]# ls /mnt/data2
a.html b.html c.html d.html e.html
提示:/dev/sdb是一块1GB的磁盘,限制分了两个主分区sdb1、sdb2,分别是200MB,剩余800MB未分区。限制需要把Sdb2分区和Sdb1分区合并,以实现对sdb1的扩容,注意,此种情况不要在生产场景操作,仅作为演示用,生产场景一般是事先规划好不会出现扩容需求,非I/0密集应用可以采用LVM实现规范动态扩容。
以下是扩容实战步骤。
1)记录分区的扇区起始信息:
[root@centos7 ~]# fdisk -l /dev/sdb #查看分区信息。
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xe8dce880
Device Boot Start End Blocks Id System
/dev/sdb1 2048 411647 204800 83 Linux #记录开始扇区号2048。
/dev/sdb2 411648 821247 204800 83 Linux #记录结束扇区号821247。
2)卸载分区:
[root@centos7 ~]# umount /mnt/data1 #卸载/mnt/data1目录。
[root@centos7 ~]# umount /mnt/data2 #卸载/mnt/data2目录。
[root@centos7 ~]# df -h #确认成功卸载目录。
Filesystem Size Used Avail Use% Mounted on
devtmpfs 476M 0 476M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.6M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda3 17G 2.4G 15G 14% /
tmpfs 98M 0 98M 0% /run/user/0
3)重新分区:
[root@centos7 ~]# fdisk /dev/sdb #对/dev/sdb分区。
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): d #删除分区,重新建立新的分区。
Partition number (1,2, default 2): 1 #删掉第一个分区。
Partition 1 is deleted
Command (m for help): d #删掉第2个分区。
Selected partition 2
Partition 2 is deleted
Command (m for help): n #创建新的分区。
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p #创建主分区。
Partition number (1-4, default 1): 1 #设置分区编号为1。
First sector (2048-2097151, default 2048): #起始值一定要和原来的sdb1的一样,否则数据会受损。
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): 821247 #结束扇区号和sdb2结束的一样,因为有未分区的空间。
Partition 1 of type Linux and of size 400 MiB is set
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xe8dce880
Device Boot Start End Blocks Id System
/dev/sdb1 2048 821247 409600 83 Linux
Command (m for help): w #保存退出。
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos7 ~]# partprobe /dev/sdb #使得修改生效。
[root@centos7 ~]# mount /dev/sdb1 /mnt/data1 #重新挂载。
[root@centos7 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 476M 0 476M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.6M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda3 17G 2.4G 15G 14% /
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb1 190M 1.6M 175M 1% /mnt/data1 #此时分区大小并没有变化。
[root@centos7 ~]# ls /mnt/data1
1.html 2.html 3.html 4.html 5.html lost+found #内容也还是原来sdb1里的,没有sdb2的。
[root@centos7 ~]# resize2fs /dev/sdb1 #在线调整磁盘大小,本节的主人公命令。
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/sdb1 is mounted on /mnt/data1; on-line resizing required #提示没有卸载分区。
old_desc_blocks = 2, new_desc_blocks = 4
The filesystem on /dev/sdb1 is now 409600 blocks long. #扩容成功。
[root@centos7 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 476M 0 476M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.6M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda3 17G 2.4G 15G 14% /
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb1 384M 2.3M 360M 1% /mnt/data1 #容量变大了。
[root@centos7 ~]# ls /mnt/data1
1.html 2.html 3.html 4.html 5.html lost+found
#但是数据还是只有sdb1里的了,sdb2的数据丢失了。此种方法不适合生产场景扩容,比较规范方法是通过LVM逻辑卷进行扩容,扩容后也需要reseze2fs进行最终实现扩容。
【命令星级】 ★★★★☆
【功能说明】
fsck命令用于检查并修复文件系统中的错误,即针对有问题的系统或磁盘进行修复,类似的命令还有e2fsck命令。有关fsck的使用需要特别注意的是:1)文件系统必须是卸载状态,否则可能会出现故障。2)不要对正常的分区使用fsck,在不加参数的情况下,fsck会根据/etc/fstab进行文件系统检查,这相当于fsck -As参数的功能。
【语法格式】
fsck [option] [filesys]
fsck [选项] [文件系统]
说明:
1)在fsck命令及后面的选项和文件系统里,每个元素之间都至少要有一个空格。
2)filesys可以是一个设备名(例如:/dev/sda1、/dev/sda2)、一个挂载点(例如:/、/usr、/home)或一个文件系统的硬盘标签,也可以是UUID指定符(例如:UUID="06760253-ff21-4177-8956-55c96aad8f2c"或LABEL=root)。
【选项说明】
表8-9针对该命令的参数选项进行了说明。
表8-9 fsck命令的参数选项及说明
**说明:**必须卸载文件系统后才能对其进行检查,否则可能会出现错误。平时没有必要使用这个命令检查磁盘,只有当系统开机显示磁盘错误时,才需要执行。
**范例8-17:**系统开机通过fsck自检。
Linux在开机过程中系统会自动调用fsck命令对需要自检的磁盘进行自检(如图8-3所示)。
图8-3 Linux开机自检磁盘图示
系统开机之所以会通过fsck自检,就是因为系统开机过程中会优先读取/etc/fstab文件,当最后一列设置为1或2时,这个磁盘在开机时就会调用fsck进行自检, fstab的文件(man fstab看帮助)信息如下:
[root@centos6 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Sep 14 02:25:17 2020
#
# 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=bbbdb908-d86c-466b-ada6-0dbf80f1419d / ext4 defaults 1 1 #根分区最后一列一般为1.
UUID=57f61bfb-4cc4-43a0-a715-b23fe0762ee9 /boot ext4 defaults 1 2 #/boot分区一般默认为2,其他默认为0.
UUID=bbf9afb5-260d-4042-b296-91badba278f0 /home ext4 defaults 1 2
UUID=8a73de09-5531-4ea6-b90a-745dc71a55c9 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
[root@centos7 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Oct 20 16:11:51 2020
#
# 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=95c7620f-87ac-46eb-90ef-bbf800bd84ed / xfs defaults 0 0
UUID=06760253-ff21-4177-8956-55c96aad8f2c /boot xfs defaults 0 0
UUID=f8cb776f-c648-43d7-8dd7-89d2134f7130 swap swap defaults 0 0
#CentOS 7分区都是0.
**提示:**管理员增加磁盘规划分区,一般最后一列都设置为0,即开机过程中不对磁盘检查,如果真有问题,可以在启动系统后人为进行检查。
**范例8-18:**Linux断电后重启故障修复案例。
当Linux系统遭遇突然断电等非正常关机操作时,很容易导致文件系统数据损坏,造成系统不能重新启动,此时,屏幕出现的提示可能是如下内容:
此时根据系统提示输入root用户的密码,注意而不是直接按Control-D继续,会再重启。
当输入正确的密码之后,正常会出现下面的提示:
此时就可以输入fsck或者fsck -A对磁盘进行修复检查,执行后可能出现一堆询问,按yes即可。
提示:
1)除了按照开机的提示进行修复外,也可以利用系统盘进入救援模式或单用户模式对系统故障进行修复。
2)千万不要在开机正常工作的情况下执行fsck来检查磁盘,因为这样有可能会导致正常的磁盘发生故障。
3)/etc/fstab中的最后一列数字为1或2时,当系统开机时就会读取fsck对这些系统磁盘进行自检。
4)不要在已经挂载的文件系统上执行fsck等磁盘修复命令,因为这样可能会导致故障。