Linux 磁盘扩容

一、查看系统磁盘
当使用df -h 命令,可以看到系统的磁盘使用情况,如下所示。

[root@localhost home]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       51G   24G   25G  49% /
tmpfs                 935M     0  935M   0% /dev/shm
/dev/xvda1            477M   93M  360M  21% /boot
/dev/mapper/VolGroup-lv_home
                      199G   60M  189G   1% /home

二、Linux 磁盘扩容
当LVM分区空间不足的时候,可以进行扩容。主要的扩容方法有两种:

通过空余的磁盘进行扩容,这个方法比较简单,不会对原有数据有影响。
将其他LVM分区空间取出一部分给需要扩容的LVM分区。
下面就分别具体介绍这两种方法。

三、利用空余磁盘扩容

  1. 首先使用命令fdisk -l 查看磁盘情况,此系统有两块硬盘,/dev/xvda 274.9G,/dev/xvdb 10.7G
[root@localhost home]# fdisk -l

Disk /dev/xvda: 274.9 GB, 274877906944 bytes
255 heads, 63 sectors/track, 33418 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000c5cd2

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/xvda2              64       33419   267922432   8e  Linux LVM

Disk /dev/xvdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
  1. 创建pv ,通过pvcreate命令将磁盘/dev/xvdb创建为一个系统PV
[root@localhost home]# pvcreate /dev/xvdb
  Physical volume "/dev/xvdb" successfully created
  1. 将 PV /dev/xvdb添加到卷组VolGroup 中。
[root@localhost home]# vgextend  VolGroup  /dev/xvdb
  Volume group "VolGroup" successfully extended

若提示卷组不存在,需要手动创建

[root@localhost ~]# vgcreate VolGroup  /dev/xvdb

查看卷组的详情

[root@localhost home]# vgs
  VG       #PV #LV #SN Attr   VSize   VFree
  VolGroup   2   3   0 wz--n- 265.50g    0 
  1. 为/ 添加10G的空间。使用命令lvextend -L +10G /dev/mapper/VolGroup-lv_root
[root@localhost home]# lvextend -L +10G  /dev/mapper/VolGroup-lv_root
  Size of logical volume VolGroup/lv_root changed from 50.98 GiB (13050 extents) to 55.98 GiB (14330 extents).
  Logical volume lv_root successfully resized
  1. 逻辑卷扩展后并不会马上生效,需要使用“resize2fs”命令重新加载逻辑卷的大小。使用命令 resize2fs /dev/VolGroup/lv_root。再使用命令df -h查看发现/已经多了10G。
[root@localhost home]# resize2fs /dev/VolGroup/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
old desc_blocks = 4, new_desc_blocks = 4
Performing an on-line resize of /dev/VolGroup/lv_root to 14673920 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 14673920 blocks long.


[root@localhost home]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       60G   24G   34G  41% /
tmpfs                 935M     0  935M   0% /dev/shm
/dev/xvda1            477M   93M  360M  21% /boot
/dev/mapper/VolGroup-lv_home
                      199G   60M  189G   1% /home

四、利用其他LVM分区空余空间进行扩容

  1. 使用df -h查看每个分区的使用情况。如下,发现/dev/mapper/VolGroup-lv_home容量很充裕,本次扩容通过减少/dev/mapper/VolGroup-lv_home的空间给/dev/mapper/VolGroup-lv_root。
[root@localhost home]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                     60G   24G   34G  41% /
tmpfs                 935M     0  935M   0% /dev/shm
/dev/xvda1            477M   93M  360M  21% /boot
/dev/mapper/VolGroup-lv_home
                    199G   60M  189G   1% /home
  1. 卸载/home
[root@localhost home]# umount /home
umount: /home: device is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))

umount /home 如果提示无法卸载,因为有进程占用/home,使用如下命令来终止占用进程:

[root@localhost home]# fuser -m /home  
/home:                1463c

如果依然无法卸载,使用以下命令:

[root@localhost home]# umount -l /home
  1. 调整/dev/mapper/VolGroup-lv_home分区大小

需要先进行磁盘检测 ,输入命令e2fsck -f /dev/mapper/VolGroup-lv_home。
注意:遇到Abort< y >? 这边输入的是n,才能继续进行。

[root@localhost ~]# e2fsck -f /dev/mapper/VolGroup-lv_home
e2fsck 1.41.12 (17-May-2010)
The filesystem size (according to the superblock) is 52848640 blocks
The physical size of the device is 52592640 blocks
Either the superblock or the partition table is likely to be corrupt!
Abort? no

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/mapper/VolGroup-lv_home: 26/13213696 files (0.0% non-contiguous), 877250/52848640 blocks

然后输入命令resize2fs -p /dev/mapper/VolGroup-lv_home 100G,进行磁盘重订大小。

[root@localhost ~]# resize2fs -p /dev/mapper/VolGroup-lv_home 100G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/VolGroup-lv_home to 26214400 (4k) blocks.
Begin pass 2 (max = 32768)
Relocating blocks             XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 1613)
Scanning inode table          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/mapper/VolGroup-lv_home is now 26214400 blocks long.
  1. 重新挂载/home

重新挂载后,输入df -h,发现/dev/mapper/VolGroup-lv_home已经改变。

[root@localhost ~]# mount /home

[root@localhost ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       60G   24G   34G  41% /
tmpfs                 935M     0  935M   0% /dev/shm
/dev/xvda1            477M   93M  360M  21% /boot
/dev/mapper/VolGroup-lv_home
                       99G   60M   94G   1% /home
  1. 设置空闲空间
[root@localhost ~]#  lvreduce -L 100G /dev/mapper/VolGroup-lv_home 
  WARNING: Reducing active and open logical volume to 100.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_home? [y/n]: y
  Size of logical volume VolGroup/lv_home changed from 200.62 GiB (51360 extents) to 100.00 GiB (25600 extents).
  Logical volume lv_home successfully resized

[root@localhost ~]# vgdisplay
  --- Volume group ---
  VG Name               VolGroup
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  11
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               265.50 GiB
  PE Size               4.00 MiB
  Total PE              67969
  Alloc PE / Size       42209 / 164.88 GiB
  Free  PE / Size       25760 / 100.62 GiB
  VG UUID               hQoxGl-H4T9-Lu9W-sITN-hO2B-aos7-T5TmKC
  1. 把闲置空间挂在到根目录下
[root@localhost ~]# lvextend -L +100.62G /dev/mapper/VolGroup-lv_root 
  Rounding size to boundary between physical extents: 100.62 GiB
  Size of logical volume VolGroup/lv_root changed from 60.97 GiB (15609 extents) to 161.59 GiB (41368 extents).
  Logical volume lv_root successfully resized

使用命令resize2fs -p /dev/mapper/VolGroup-lv_root, 可以不用重启,就显示最新的磁盘空间。

[root@localhost ~]# resize2fs -p /dev/mapper/VolGroup-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/VolGroup-lv_root is mounted on /; on-line resizing required
old desc_blocks = 4, new_desc_blocks = 11
Performing an on-line resize of /dev/mapper/VolGroup-lv_root to 42360832 (4k) blocks.
The filesystem on /dev/mapper/VolGroup-lv_root is now 42360832 blocks long.

  1. 最后结果
[root@localhost ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      159G   24G  128G  16% /
tmpfs                 935M     0  935M   0% /dev/shm
/dev/xvda1            477M   93M  360M  21% /boot
/dev/mapper/VolGroup-lv_home
                       99G   60M   94G   1% /home

你可能感兴趣的:(Linux 磁盘扩容)