centos硬盘分区及文件系统扩容
[TOC]
硬盘分区
2T以下容量分区
[root@VM-16-2-centos ~]# fdisk -l #查看分区详情
Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x0009ac89
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 104857566 52427759+ 83 Linux
Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 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
[root@VM-16-2-centos ~]# fdisk /dev/vdb #进入fdisk进行分区
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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x4a2d1cd4.
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-41943039, default 2048): #直接回车表示从第2048分区块开始
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): #直接回车表示到最大分区块结束
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set
Command (m for help): w #保存并退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@VM-16-2-centos ~]# fdisk -l #查看硬盘详情
Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x0009ac89
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 104857566 52427759+ 83 Linux
Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x4a2d1cd4
Device Boot Start End Blocks Id System
/dev/vdb1 2048 41943039 20970496 83 Linux
[root@VM-16-2-centos ~]# mkfs.ext4 /dev/vdb1 #把分区格式化成ext4文件系统
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
1310720 inodes, 5242624 blocks
262131 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2153775104
160 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, 1605632, 2654208,
4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@VM-16-2-centos ~]# mount /dev/vdb1 /data/ #挂载硬盘
[root@VM-16-2-centos ~]# df -Th #查看硬盘挂载详情
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 484M 0 484M 0% /dev
tmpfs tmpfs 496M 24K 496M 1% /dev/shm
tmpfs tmpfs 496M 472K 495M 1% /run
tmpfs tmpfs 496M 0 496M 0% /sys/fs/cgroup
/dev/vda1 ext4 50G 2.9G 45G 7% /
tmpfs tmpfs 100M 0 100M 0% /run/user/0
/dev/vdb1 ext4 20G 45M 19G 1% /data
[root@VM-16-2-centos ~]#
2T以上容量分区
[root@VM-16-2-centos ~]# parted /dev/vdb #进入parted进行分区
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt #把当前硬盘转换成gpt格式
Warning: The existing disk label on /dev/vdb will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? yes
(parted) mkpart primary 0% 100% #创建一个分区容量从0%到100%
(parted) p #查看分区
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 21.5GB 21.5GB ext4 primary
(parted) q
Information: You may need to update /etc/fstab.
[root@VM-16-2-centos ~]# fdisk -l #查看硬盘详情
Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x0009ac89
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 104857566 52427759+ 83 Linux
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 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: gpt
Disk identifier: CE874854-3D3C-402B-B46D-9383CD4E0280
# Start End Size Type Name
1 2048 41940991 20G Microsoft basic primary
[root@VM-16-2-centos ~]# mkfs.ext4 /dev/vdb1 #把分区格式化成ext4文件系统
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
1310720 inodes, 5242368 blocks
262118 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2153775104
160 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, 1605632, 2654208,
4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@VM-16-2-centos ~]# mount /dev/vdb1 /data/ #把分区挂载到指定路径
[root@VM-16-2-centos ~]# df -Th #查看硬盘挂载
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 484M 0 484M 0% /dev
tmpfs tmpfs 496M 24K 496M 1% /dev/shm
tmpfs tmpfs 496M 472K 495M 1% /run
tmpfs tmpfs 496M 0 496M 0% /sys/fs/cgroup
/dev/vda1 ext4 50G 3.0G 45G 7% /
tmpfs tmpfs 100M 0 100M 0% /run/user/0
/dev/vdb1 ext4 20G 45M 19G 1% /data
[root@VM-16-2-centos ~]#
分区扩容
- 硬盘初始化分区后,MBR( fdisk 分区工具)分区最大支持容量2T以下扩容,GPT(使用parted 分区工具)分区支持2T以上扩容
MBR分区扩容
[root@VM-16-2-centos ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 203.6M 0 rom
vda 253:0 0 50G 0 disk
└─vda1 253:1 0 50G 0 part /
vdb 253:16 0 30G 0 disk
└─vdb1 253:17 0 20G 0 part
[root@VM-16-2-centos ~]# yum -y install gdisk #安装硬盘扩容工具
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
os | 3.6 kB 00:00:00
updates | 2.9 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package gdisk.x86_64 0:0.8.10-3.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================================================================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================================================================================================================================================
Installing:
gdisk x86_64 0.8.10-3.el7 os 190 k
Transaction Summary
==============================================================================================================================================================================================================================================================================
Install 1 Package
Total download size: 190 k
Installed size: 660 k
Downloading packages:
gdisk-0.8.10-3.el7.x86_64.rpm | 190 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : gdisk-0.8.10-3.el7.x86_64 1/1
Verifying : gdisk-0.8.10-3.el7.x86_64 1/1
Installed:
gdisk.x86_64 0:0.8.10-3.el7
Complete!
[root@VM-16-2-centos ~]# growpart /dev/vdb 1 #分区扩容
CHANGED: partition=1 start=2048 old: size=41938944 end=41940992 new: size=62912478 end=62914526
[root@VM-16-2-centos ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 203.6M 0 rom
vda 253:0 0 50G 0 disk
└─vda1 253:1 0 50G 0 part /
vdb 253:16 0 30G 0 disk
└─vdb1 253:17 0 30G 0 part /data
[root@VM-16-2-centos ~]# resize2fs /dev/vdb1 #文件系统扩容
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vdb1 is mounted on /data; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 4
The filesystem on /dev/vdb1 is now 7864059 blocks long.
GPT分区扩容
- 建议扩容前先找一块临时硬盘进行备份
#查看当前硬盘及分区详情,可以看到vdb数据盘总大小2201.2GB,只有一个20G的分区
[root@VM-16-3-centos ~]# fdisk -l
Disk /dev/vda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x0009ac89
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 62914526 31456239+ 83 Linux
Disk /dev/vdb: 2201.2 GB, 2201170739200 bytes, 4299161600 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: 0x00000000
Device Boot Start End Blocks Id System
/dev/vdb1 1 41943039 20971519+ ee GPT
#检查硬盘容量及分区大小
[root@VM-16-3-centos ~]# parted /dev/vdb print
Error: The backup GPT table is not at the end of the disk, as it should be. This might mean that another operating system believes the disk is smaller. Fix, by moving the backup to the end (and removing the old backup)?
Fix/Ignore/Cancel? FIX
Warning: Not all of the space available to /dev/vdb appears to be used, you can fix the GPT to use all of the space (an extra 4257218560 blocks) or continue with the current setting?
Fix/Ignore? FIX
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 2201GB #硬盘分区总大小
Sector size (logical/physical): 512B/512B
Partition Table: gpt #分区格式
Disk Flags:
#分区详情
Number Start End Size File system Name Flags
1 1049kB 21.5GB 21.5GB ext4 primary
#进入分区挂载目录
[root@VM-16-3-centos ~]# cd /data/
#查看当前目录下的所有内容(不包含隐藏文件)
[root@VM-16-3-centos data]# ls
aaa aaaa lost+found
#退出分区挂载路径到当前用户家目录下
[root@VM-16-3-centos data]# cd
#把需要扩容的分区取消挂载
[root@VM-16-3-centos ~]# umount /dev/vdb1
#进入parted分区工具
[root@VM-16-3-centos ~]# parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print #查看当前硬盘的大小及分区
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 4299161600s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 2048s 41940991s 41938944s ext4 primary
(parted) rm 1 #删除原有分区
(parted) print #查看分区是否删除成功
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 4299161600s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
(parted) mkpart primary 2048s 100% #新建一个分区并把硬盘所有大小写入新分区
(parted) print #查看是否分区成功
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 4299161600s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 2048s 4299159551s 4299157504s ext4 primary
(parted) quit #退出分区工具
Information: You may need to update /etc/fstab.
#查看硬盘及分区详情
[root@VM-16-3-centos ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 203.6M 0 rom
vda 253:0 0 30G 0 disk
└─vda1 253:1 0 30G 0 part /
vdb 253:16 0 2T 0 disk
└─vdb1 253:17 0 2T 0 part
#检查分区
[root@VM-16-3-centos ~]# e2fsck -f /dev/vdb1
e2fsck 1.42.9 (28-Dec-2013)
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/vdb1: 16/1310720 files (0.0% non-contiguous), 126327/5242368 blocks
#扩容分区
[root@VM-16-3-centos ~]# resize2fs /dev/vdb1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vdb1 to 537394688 (4k) blocks.
The filesystem on /dev/vdb1 is now 537394688 blocks long.
#挂载扩容后的分区到系统目录
[root@VM-16-3-centos ~]# mount /dev/vdb1 /data
#进入挂载点
[root@VM-16-3-centos ~]# cd /data/
#查看文件及文件夹是否有丢失
[root@VM-16-3-centos data]# ls
aaa aaaa lost+found
[root@VM-16-3-centos data]# cd aaa/
[root@VM-16-3-centos aaa]# ls
bbb
[root@VM-16-3-centos aaa]# cd bbb/
[root@VM-16-3-centos bbb]# ls
ccc
[root@VM-16-3-centos bbb]# cd /data/
[root@VM-16-3-centos data]# ls
aaa aaaa lost+found
[root@VM-16-3-centos data]# cat aaaa
aaaa
扫描二维码,在手机上阅读