首先进去救援(rescue)模式,使用fdisk -l查看现有分区状况:
Disk /dev/vda: 1 TiB, 1099511627776 bytes, 2147483648 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
Disklabel type: dos
Disk identifier: 0x00095886
Device Boot Start End Sectors Size Id Type
/dev/vda1 * 2048 2143286911 2143284864 1022G 83 Linux
/dev/vda2 2143286912 2147481215 4194304 2G 82 Linux swap / Solaris
Disk /dev/vdb: 1.1 GiB, 1181116416 bytes, 2306868 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
Disklabel type: dos
Disk identifier: 0xb52fd1c0
Device Boot Start End Sectors Size Id Type
/dev/vdb1 * 2048 2304511 2302464 1.1G 83 Linux
可以看到有两个盘,一个是我们的硬盘,另一个是救援模式的硬盘。
开始操作自己的硬盘,在使用fdisk 缩小逻辑卷前一定要先缩小文件系统,否则在进行lv逻辑卷缩小时产生文件系统大小与物理分区不匹配的错误,这是大部分人错误的原因。
先运行 e2fsck -f /dev/vda1 检查分区完整性
rescue # e2fsck -f /dev/vda1
e2fsck 1.43.4 (31-Jan-2017)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vda1: 30832/66977792 files (0.2% non-contiguous), 4631647/267910608 blocks
再运行 resize2fs /dev/vda1 24G 缩小文件系统,这里的24G是指缩小完后的大小
rescue # resize2fs /dev/vda1 24G
resize2fs 1.43.4 (31-Jan-2017)
Resizing the filesystem on /dev/vda1 to 6291456 (4k) blocks.
The filesystem on /dev/vda1 is now 6291456 (4k) blocks long.
再进入 fdisk 直接删掉旧的分区,新建即可
rescue # fdisk /dev/vda
Welcome to fdisk (util-linux 2.29.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 has been deleted.
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (1,3,4, default 1):
First sector (2048-2147483647, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-2143286911, default 2143286911): +24G
Created a new partition 1 of type 'Linux' and of size 24 GiB.
Partition #1 contains a ext4 signature.
Do you want to remove the signature? [Y]es/[N]o: n
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
重点部分已经标记出来了,这里的First sector由于我是第一个分区,所以直接由2048开始,如果是其他磁盘,则需要进入fdisk后输入p,记录下要扩大那个分区的First sector,删除后从同样的位置开始创建。
24G要与上一步的24G保持一致
然后 e2fsck -f /dev/vda1 再次检查磁盘,resize2fs /dev/vda1再次确认:
rescue # e2fsck -f /dev/vda1
e2fsck 1.43.4 (31-Jan-2017)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vda1: 30832/1572864 files (0.2% non-contiguous), 524279/6291456 blocks
rescue # resize2fs /dev/vda1
resize2fs 1.43.4 (31-Jan-2017)
The filesystem is already 6291456 (4k) blocks long. Nothing to do!
即可退出救援模式,回到正常系统当中
[root@server ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.5M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/vda1 24G 1.6G 21G 8% /
tmpfs 379M 0 379M 0% /run/user/0
这时就可以发现已经缩小成功了
[…] CentOS下调整根目录大小 […]