LVM 管理减少swap分区空间增加到根分区

简介

LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现。

物理存储介质(PhysicalStorageMedia)

指系统的物理存储设备:磁盘,如:/dev/hda、/dev/sda等,是存储系统最底层的存储单元。

物理卷(Physical Volume,PV)

指磁盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。

卷组(Volume Group,VG)

类似于非LVM系统中的物理磁盘,其由一个或多个物理卷PV组成。可以在卷组上创建一个或多个LV(逻辑卷)。

逻辑卷(Logical Volume,LV)

类似于非LVM系统中的磁盘分区,逻辑卷建立在卷组VG之上。在逻辑卷LV之上可以建立文件系统(比如/home或者/usr等)。

查看信息命令

pvdisplay   #查看PV信息

[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda3
VG Name VolGroup
PV Size 18.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE
Free PE
Allocated PE
PV UUID ARNJxr-gfcH-rW5H-X6I1-c2da-QhM1-BJ7ljo

vgdisplay   #查看VG信息

[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name VolGroup
System ID
Format lvm2
Metadata Areas
Metadata Sequence No
VG Access read/write
VG Status resizable
MAX LV
Cur LV
Open LV
Max PV
Cur PV
Act PV
VG Size 18.51 GiB
PE Size 4.00 MiB
Total PE
Alloc PE / Size / 18.51 GiB
Free PE / Size /
VG UUID gFkWoE-8AVZ-kbd5-Clge-6Tq3-gcFh-4iVr1x

lvdisplay   #查看LV信息

[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/VolGroup/LogVol01
LV Name LogVol01
VG Name VolGroup
LV UUID UeAMMJ-G6Ou-yMlJ-9Igz-pVBU-1HSy-jt6Trt
LV Write Access read/write
LV Creation host, time localhost.localdomain, -- :: -
LV Status available
# open
LV Size 4.05 GiB
Current LE
Segments
Allocation inherit
Read ahead sectors auto
- currently set to
Block device : --- Logical volume ---
LV Path /dev/VolGroup/LogVol00
LV Name LogVol00
VG Name VolGroup
LV UUID OF62jU-eVGO-4Rtd-TycU-aoGC-0Rs0-nDRRhE
LV Write Access read/write
LV Creation host, time localhost.localdomain, -- :: -
LV Status available
# open
LV Size 14.46 GiB
Current LE
Segments
Allocation inherit
Read ahead sectors auto
- currently set to
Block device :1

缩减和增加

减少swap分区空间前要取消挂载,使用 swapoff -a

[root@localhost ~]# free -m
total used free shared buffers cached
Mem:
-/+ buffers/cache:
Swap:
[root@localhost ~]# swapoff -a
[root@localhost ~]# free -m
total used free shared buffers cached
Mem:
-/+ buffers/cache:
Swap:

使用 lvreduce -L -xxx filename 或 lvreduce -L xxx filename 来减少swap分区空间,“-”代表减多少,不加“-”代表减剩多少。下图表示给swap分区留512M。

使用 mke2fs filename 格式化分区

使用 resize2fs filename 重写分区空间

[root@localhost ~]# lvreduce -L 512M /dev/VolGroup/LogVol01
WARNING: Reducing active and open logical volume to 512.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce LogVol01? [y/n]: y
Reducing logical volume LogVol01 to 512.00 MiB
Logical volume LogVol01 successfully resized
[root@localhost ~]# mke2fs /dev/VolGroup/LogVol01
mke2fs 1.41. (-May-)
Filesystem label=
OS type: Linux
Block size= (log=)
Fragment size= (log=)
Stride= blocks, Stripe width= blocks
inodes, blocks
blocks (5.00%) reserved for the super user
First data block=
Maximum filesystem blocks=
block groups
blocks per group, fragments per group
inodes per group
Superblock backups stored on blocks:
, Writing inode tables: done
Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every mounts or
days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# resize2fs /dev/VolGroup/LogVol01
resize2fs 1.41. (-May-)
The filesystem is already blocks long. Nothing to do!

使用 lvextend -L +1G filename 为根分区增加空闲空间,用法同上。空闲空间可以使用vgdisplay查看,增加空间不需要格式化。

[root@localhost ~]# lvextend -L +.54G /dev/VolGroup/LogVol00
Rounding size to boundary between physical extents: 3.54 GiB
Extending logical volume LogVol00 to 18.00 GiB
Logical volume LogVol00 successfully resized
[root@localhost ~]# resize2fs /dev/VolGroup/LogVol00
resize2fs 1.41. (-May-)
Filesystem at /dev/VolGroup/LogVol00 is mounted on /; on-line resizing required
old desc_blocks = , new_desc_blocks =
Performing an on-line resize of /dev/VolGroup/LogVol00 to (4k) blocks.
The filesystem on /dev/VolGroup/LogVol00 is now blocks long.

swap文件

新建2G的swap文件,挂载到swap,添加到开机启动项。

[root@localhost ~]# dd if=/dev/zero of=/www/swap bs= count=
+ records in
+ records out
bytes (2.1 GB) copied, 12.0018 s, MB/s
[root@localhost ~]# mkswap /www/swap
mkswap: /www/swap: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version , size = KiB
no label, UUID=92ab4ed5-f301-4ba4-970f-9e5600610522
[root@localhost ~]# swapon /www/swap
[root@localhost ~]# free -m
total used free shared buffers cached
Mem:
-/+ buffers/cache:
Swap:
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/www/swap file -

修改启动项

编辑 /etc/fstab文件,去掉之前的swap分区挂载,在最末行增加swap文件的挂载

[root@localhost ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Jul ::
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(), findfs(), mount() and/or blkid() for more info
#
/dev/mapper/VolGroup-LogVol00 / ext4 defaults
UUID=4221a802-6ce2--a3a5-7c17b961f291 /boot ext4 defaults
UUID=b720b68f-37b7-45ce-b4a2-40deeb9fa90f /www ext3 defaults
tmpfs /dev/shm tmpfs defaults
devpts /dev/pts devpts gid=,mode=
sysfs /sys sysfs defaults
proc /proc proc defaults
/www/swap swap swap defaults

重启后可以看到,根分区空间有增加, swap使用的是swap文件

[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-LogVol00
18G 716M 17G % /
tmpfs 939M 939M % /dev/shm
/dev/sda1 485M 32M 429M % /boot
/dev/sda2 61G .2G 55G % /www
[root@localhost ~]# free -m
total used free shared buffers cached
Mem:
-/+ buffers/cache:
Swap:
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/www/swap file -

注:之前有实验过直接删掉swap的LV,用文件代替,但是,删除swap的LV后,重启报错如图

LVM 管理减少swap分区空间增加到根分区

按网上的解决办法,开机按“e”加“enforcing=0”神马的不管用。这个问题暂时木有解决,有解决过的老师,请告诉下学生。

上一篇:LeetCode(77):组合


下一篇:【JNI】锅炉压力监测器