由于是虚拟机,之前分配的磁盘比较小,直接修改了磁盘大小。然后LVM扩容,物理机外挂磁盘也可以参考。

说明在代码注释里。

# 查看磁盘空间
[[email protected] ~]# fdisk -l
磁盘 /dev/sda:85.9 GB, 85899345920 字节,167772160 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0003c846

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    82198527    40893440   8e  Linux LVM

磁盘 /dev/mapper/cl-root:36.6 GB, 36570136576 字节,71426048 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
# 说明有空闲空间
# 从空白空间新建磁盘
[[email protected] ~]# fdisk /dev/sda
命令(输入 m 获取帮助):n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
分区号 (3,4,默认 3):
起始 扇区 (82198528-167772159,默认为 82198528):
将使用默认值 82198528
Last 扇区, +扇区 or +size{K,M,G} (82198528-167772159,默认为 167772159):
将使用默认值 167772159
分区 3 已设置为 Linux 类型,大小设为 40.8 GiB
# 修改分区类型为LVM
命令(输入 m 获取帮助):t
分区号 (1-3,默认 3):3
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”
# 写入保存
命令(输入 m 获取帮助):w
# 查看新分区
[[email protected] ~]# fdisk -l
磁盘 /dev/sda:85.9 GB, 85899345920 字节,167772160 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0003c846

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    82198527    40893440   8e  Linux LVM
/dev/sda3        82198528   167772159    42786816   8e  Linux LVM
# 看一下vg信息
[[email protected] ~]# vgdisplay
  --- Volume group ---
  VG Name               cl
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <39.00 GiB
  PE Size               4.00 MiB
  Total PE              9983
  Alloc PE / Size       9982 / 38.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               hgAhoo-TYE2-63M7-VwLK-kp0U-lqww-YzdUxT
# vg name是cl 刷新磁盘信息
[[email protected] ~]# partprobe /dev/sda
# 扩展cl空间
[[email protected] ~]# vgextend cl /dev/sda3
  Physical volume "/dev/sda3" successfully created.
  Volume group "cl" successfully extended
# 再次查看
[[email protected] ~]# vgdisplay            
  --- Volume group ---
  VG Name               cl
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               <79.80 GiB
  PE Size               4.00 MiB
  Total PE              20428
  Alloc PE / Size       9982 / 38.99 GiB
  Free  PE / Size       10446 / 40.80 GiB
  VG UUID               hgAhoo-TYE2-63M7-VwLK-kp0U-lqww-YzdUxT
# 查看lv
[[email protected]ocalhost ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/cl/root
  LV Name                root
  VG Name                cl
  LV UUID                Bm08jB-ilwr-GnOU-4zpD-Rm2q-JekB-0G7wFc
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-07-11 11:08:18 +0800
  LV Status              available
  # open                 1
  LV Size                <34.06 GiB
  Current LE             8719
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
# 扩展lv
[[email protected] ~]# lvextend -L 74G  /dev/cl/root 
  Size of logical volume cl/root changed from <34.06 GiB (8719 extents) to 74.00 GiB (18944 extents).
  Logical volume cl/root successfully resized.
# 修改保存,因为是CentOS 7,所以用xfs_growfs
[[email protected] ~]# xfs_growfs /dev/cl/root
meta-data=/dev/mapper/cl-root    isize=512    agcount=4, agsize=2232064 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=8928256, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=4359, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 8928256 to 19398656
# 查看磁盘大小
[[email protected] ~]# df -h
文件系统             容量  已用  可用 已用% 挂载点
devtmpfs             7.9G     0  7.9G    0% /dev
tmpfs                7.9G     0  7.9G    0% /dev/shm
tmpfs                7.9G   42M  7.8G    1% /run
tmpfs                7.9G     0  7.9G    0% /sys/fs/cgroup
/dev/mapper/cl-root   74G  6.7G   68G    9% /
/dev/sda1            197M  189M  8.6M   96% /boot
tmpfs                1.6G   12K  1.6G    1% /run/user/42
tmpfs                1.6G     0  1.6G    0% /run/user/0
# 可见已经扩容了