转至:

 

0、思路

创建一个新的逻辑分区,将新的逻辑分区格式化ext3(或其他类型)的文件系统,mount到磁盘空间不够的文件系统,就跟原来的分区/文件系统一样的使用

1、准备

1.1 注意使用VMware自带的Expand功能不能有Snapshot存在,所以在expand之前先把所有的Snapshot删掉了

1.2 为了以防万一,把VMware虚拟机copy了一份备用。

2、查看挂载点

1. 2. 文件系统 容量 已用 可用 已用% 挂载点
3. /dev/mapper/centos-root 36G 5.4G 31G 15% /
4. devtmpfs3.9G03.9G0% /dev
5. tmpfs 3.9G 0 3.9G 0% /dev/shm
6. tmpfs 3.9G 8.5M 3.9G 1% /run
7. tmpfs3.9G03.9G0% /sys/fs/cgroup
8. /dev/sda1 497M 125M 373M 26% /boot
9. tmpfs781M0781M0% /run/user/0

3、扩展VMWare硬盘空间

3.1 关闭Vmware 的 Linux系统

3.2 在VMWare菜单中设置:

VM -> Settings… -> Hardware -> Hard Disk -> Utilities -> Expand

输入你想要扩展到多少G,假设新增加了 40G 

centos7 新增磁盘给根目录扩容 centos 扩展磁盘_centos

centos7 新增磁盘给根目录扩容 centos 扩展磁盘_ci_02

4、对新增加的硬盘进行分区

执行步骤:
• 执行“fdisk /dev/sda”,进入到fdisk
• 输入p指令,查看已分区数量(有两个 /dev/sda1 /dev/sda2)
• 输入 n {new partition}指令,新增加一个分区
• 输入p {primary partition},分区类型选择为主分区
• 输入分区号 3 {partition number} ,分区号选3(上面显示我已经有2个分区了)
• 回车      默认(起始扇区)
• 回车      默认(结束扇区)
• 输入t {change partition id}指令,修改分区类型
• 按提示输入刚才的分区号3
• 输入分区类型 8e {Linux LVM partition}
• 输入w指令,将以上改动写入分区表
• 最后完成,退出fdisk命令
1. [root@vnode1 ~]# fdisk /dev/sda
2. 欢迎使用 fdisk (util-linux 2.23.2)。
3.  
4. 更改将停留在内存中,直到您决定将更改写入磁盘。
5. 使用写入命令前请三思。
6.  
7. 命令(输入 m 获取帮助):p
8. ....
9. Start         End      Blocks   Id  System
10. /dev/sda1   *        2048     1026047      512000   83  Linux
11. /dev/sda2         1026048    83886079    41430016   8e  Linux LVM
12.  
13. 命令(输入 m 获取帮助):n
14. Partition type:
15. primary (2 primary, 0 extended, 2 free)
16. extended
17. Select (default p): p
18. 分区号 (3,4,默认 3):3
19. 起始 扇区 (83886080-167772159,默认为 83886080):
20. 将使用默认值 83886080
21. Last 扇区, +扇区 or +size{K,M,G} (83886080-167772159,默认为 167772159):
22. 将使用默认值 167772159
23. 分区 3 已设置为 Linux 类型,大小设为 40 GiB
24.  
25. 命令(输入 m 获取帮助):t
26. 分区号 (1-3,默认 3):3
27. Hex 代码(输入 L 列出所有代码):8e
28. 已将分区“Linux”的类型更改为“Linux LVM”
29.  
30. 命令(输入 m 获取帮助):w
31. The partition table has been altered!
32.  
33. Calling ioctl() to re-read partition table.
34.  
35. WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
36. The kernel still uses the old table. The new table will be used at
37. the next reboot or after you run partprobe(8) or kpartx(8)
38. 正在同步磁盘。
 重启系统: 

[root@vnode1 ~]# shutdown -r now (很重要)
4、对新增加的硬盘格式化
1. [root@vnode1 ~]# mkfs.ext3 /dev/sda3
2. mke2fs 1.42.9 (28-Dec-2013)
3. 文件系统标签=
4. OS type: Linux
5. 块大小=4096 (log=2)
6. 分块大小=4096 (log=2)
7. Stride=0 blocks, Stripe width=0 blocks
8. 2621440 inodes, 10485760 blocks
9. 524288 blocks (5.00%) reserved for the super user
10. 第一个数据块=0
11. Maximum filesystem blocks=4294967296
12. 320 block groups
13. 32768 blocks per group, 32768 fragments per group
14. 8192 inodes per group
15. Superblock backups stored on blocks: 
16. 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
17. 4096000, 7962624
18.  
19. Allocating group tables: 完成                            
20. 正在写入inode表: 完成                            
21. Creating journal (32768 blocks): 完成
22. Writing superblocks and filesystem accounting information: 完成
 5、添加新LVM到已有的LVM组,实现扩容
1. [root@vnode1 ~]# lvm
2. lvm> pvcreate /dev/sda3
3. WARNING: ext3 signature detected on /dev/sda3 at offset 1080. Wipe it? [y/n]: y
4. on /dev/sda3.
5. "/dev/sda3" successfully created
6. lvm> vgextend centos /dev/sda3 
7. "centos" successfully extended
8. lvm> lvextend -L +39.9G /dev/mapper/centos-root
9. to boundary between physical extents: 39.90 GiB
10. of logical volume centos/root changed from 35.47 GiB (9080 extents) to 75.37 GiB (19295 extents).
11.   Logical volume root successfully resized.
12. lvm> pvdisplay
13. --- Physical volume ---
14.   PV Name               /dev/sda2
15.   VG Name               centos
16. 39.51 GiB / not usable 3.00 MiB
17. but full)
18. 4.00 MiB
19. 10114
20. 0
21. 10114
22.   PV UUID               vtNvX3-b1yw-ePoh-YGHQ-tDhL-x0ru-mfts3k
23.  
24. --- Physical volume ---
25.   PV Name               /dev/sda3
26.   VG Name               centos
27. 40.00 GiB / not usable 4.00 MiB
28.   Allocatable           yes 
29. 4.00 MiB
30. 10239
31. 34
32. 10205
33. 51VP-yHNZ1g
34.  
35. lvm> quit
36.   Exiting.
37. [root@vnode1 ~]# 
备注:
1. lvm                                      进入lvm管理  
2. lvm> pvcreate /dev/sda3                          这是初始化刚才的分区,必须的  
3. lvm>vgextend centos /dev/sda3                    将初始化过的分区加入到虚拟卷组vg_dc01
4. lvm>lvextend -L +39.9G /dev/mapper/centos-root  扩展已有卷的容量(注意容量大小)
5. lvm>pvdisplay                           查看卷容量,这时你会看到一个很大的卷了
6. lvm>quit                              退出
以上只是卷扩容了,下面是文件系统的真正扩容,输入以下命令:
1. [root@vnode1 ~]# resize2fs /dev/mapper/centos-root
2. resize2fs 1.42.9 (28-Dec-2013)
3. resize2fs: Bad magic number in super-block 当尝试打开 /dev/mapper/centos-root 时
4. 找不到有效的文件系统超级块.
报错:当尝试打开 /dev/mapper/centos-root 时 找不到有效的文件系统超级块
因为我的centos7的某些分区用的是xfs的文件系统(使用df -T查看即可知道)
1. [root@vnode1 ~]# dfT
2. 文件系统                                          类型                 1K-块          已用            可用          已用%  挂载点
3. /dev/mapper/centos-root   xfs                37173520 5574340  31599180    15%      /
4. devtmpfsdevtmpfs3987400039874000%       /dev
5. tmpfs                                                   tmpfs        3997856        0               3997856       0%      /dev/shm
6. tmpfs                                                   tmpfs            3997856     8628        3989228       1%     /run
7. tmpfs                                tmpfs3997856039978560%    /sys/fs/cgroup
8. /dev/sda1                                        xfs                   508588       127152      381436         26%  /boot
9. tmpfs                               tmpfs799572     07995720%   run/user/0
10. [root@vnode1 ~]# 
将resize2fs替换为xfs_growfs,重新执行一遍即可,如下:
1. [root@vnode1 ~]# xfs_growfs /dev/mapper/centos-root
2. meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=2324480 blks
3. 512   attr=2, projid32bit=1
4. 0        finobt=0
5. data     =                       bsize=4096   blocks=9297920, imaxpct=25
6. 0      swidth=0 blks
7. naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
8. log      =internal               bsize=4096   blocks=4540, version=2
9. 512   sunit=0 blks, lazy-count=1
10. realtime =none                   extsz=4096   blocks=0, rtextents=0
11. data blocks changed from 9297920 to 19758080
 最后再运行下:df -h 

即可看到扩容后的磁盘空间1. [root@vnode1 ~]# dfh
2. 文件系统                 容量  已用  可用 已用% 挂载点
3. /dev/mapper/centos-root   76G  5.4G   71G    8% /
4. devtmpfs3.9G03.9G0% /dev
5. tmpfs                    3.9G     0  3.9G    0% /dev/shm
6. tmpfs                    3.9G  8.5M  3.9G    1% /run
7. tmpfs3.9G03.9G0% /sys/fs/cgroup
8. /dev/sda1                497M  125M  373M   26% /boot
9. tmpfs781M0781M0% /run/user/0
ps:修复 unknown 区  vgreduce --removemissing centos


  --- Physical volume ---
  PV Name               [unknown]
  VG Name               centos
  PV Size               10.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              2559
  Free PE               2559
  Allocated PE          0
  PV UUID               VCwaaP-hhX8-YcSg-Cqji-ctob-AAod-wRnOLO