最近发现接手的服务器磁盘空间颇有浪费,大部分空间闲置未分配,而系统刚好使用的lvm故可以方便拓展。


简单说下LVM增加的时候先创建磁盘,然后创建pv,扩展vg,扩展lv,最后文件系统



使用fdisk -l 查看已使用空间,有大部分空间未使用,xvda空85.9G 而仅分配了约20G空间。

[root@localhost ~]# fdisk -l

Disk /dev/xvda: 85.9 GB, 85899345920 bytes, 167772160 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: 0x00060da6

Device Boot      Start         End      Blocks   Id  System

/dev/xvda1   *        2048     1026047      512000   83  Linux

/dev/xvda2         1026048    41943039    20458496   8e  Linux LVM

Disk /dev/mapper/centos-root: 18.8 GB, 18756927488 bytes, 36634624 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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



用fdisk新建磁盘分区并更改分区类型为Linux LVM以供扩容。

[root@localhost ~]# fdisk /dev/xvda
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 (2 primary, 0 extended, 2 free)
e   extended
Select (default p): p
Partition number (3,4, default 3): 
First sector (41943040-167772159, default 41943040): 
Using default value 41943040
Last sector, +sectors or +size{K,M,G} (41943040-167772159, default 167772159): 
Using default value 167772159
Partition 3 of type Linux and of size 60 GiB is set
Command (m for help): t
Partition number (1-3, default 3): 
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.


以上解释

fdisk /dev/sda

n   #新建分区

3   #分区编号3

t   #修改分区类型

L   #查看分区类型编号

8e  #8e linux LVM

w   #保持并退出


保存若提示设备正忙使用partprobe 或者 kpartx重读分配表


创建pv

[root@localhost ~]# pvcreate /dev/xvda3

Physical volume "/dev/xvda3" successfully created


查看vg名字

[root@localhost ~]# vgs

VG     #PV #LV #SN Attr   VSize  VFree 

centos   1   2   0 wz--n- 19.51g 40.00m


拓展vg


[root@localhost ~]# vgextend centos /dev/xvda3

Volume group "centos" successfully extended


vgs 查看拓展的vg空间

[root@localhost ~]# vgs

VG     #PV #LV #SN Attr   VSize  VFree 

centos   2   2   0 wz--n- 79.50g 60.04g


可见剩余空间已将新建的xvda3添加进来


拓展lv

lvextend -L +30G /dev/mapper/centos-root

拓展至全部空闲空间

lvextend -l +100%FREE /dev/mapper/centos-root

拓展所有剩余空间

[root@localhost ~]# lvextend -l +100%FREE /dev/mapper/centos-root

Size of logical volume centos/root changed from 17.47 GiB (4472 extents) to 77.50 GiB (19841 extents).

Logical volume root successfully resized.


再次查看vg

[root@localhost ~]# vgs

VG     #PV #LV #SN Attr   VSize  VFree

centos   2   2   0 wz--n- 79.50g    0


最后拓展文件系统fs

ext

resize2fs -p /dev/mapper/centos-root

xfs

xfs_growfs /dev/mapper/centos-root


[root@localhost ~]# xfs_growfs /dev/mapper/centos-root 
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=1144832 blks
=                       sectsz=512   attr=2, projid32bit=1
=                       crc=0        finobt=0
data     =                       bsize=4096   blocks=4579328, imaxpct=25
=                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
=                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 4579328 to 20317184


[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   78G  2.1G   76G   3% /
devtmpfs                 3.6G     0  3.6G   0% /dev
tmpfs                    3.5G     0  3.5G   0% /dev/shm
tmpfs                    3.5G  121M  3.4G   4% /run
tmpfs                    3.5G     0  3.5G   0% /sys/fs/cgroup
/dev/xvda1               497M  141M  357M  29% /boot
tmpfs                    707M     0  707M   0% /run/user/1000


增加的时候先创建磁盘,然后创建pv,扩展vg,扩展lv,最后文件系统

缩减的时候刚好相反,先缩减文件系统然后在lv vg pv最后磁盘