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 4738
Free PE 0
Allocated PE 4738
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 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 18.51 GiB
PE Size 4.00 MiB
Total PE 4738
Alloc PE / Size 4738 / 18.51 GiB
Free PE / Size 0 / 0
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, 2015-07-23 03:42:14 -0400
LV Status available
# open 1
LV Size 4.05 GiB
Current LE 1036
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
--- 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, 2015-07-23 03:42:15 -0400
LV Status available
# open 1
LV Size 14.46 GiB
Current LE 3702
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
缩减和增加
减少swap分区空间前要取消挂载,使用 swapoff -a
[root@localhost ~]# free -m
total used free shared buffers cached
Mem: 1877 140 1737 0 6 39
-/+ buffers/cache: 93 1783
Swap: 4143 0 4143
[root@localhost ~]# swapoff -a
[root@localhost ~]# free -m
total used free shared buffers cached
Mem: 1877 136 1740 0 6 39
-/+ buffers/cache: 91 1786
Swap: 0 0 0
使用 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.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
32768 inodes, 131072 blocks
6553 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=134217728
4 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# resize2fs /dev/VolGroup/LogVol01
resize2fs 1.41.12 (17-May-2010)
The filesystem is already 131072 blocks long. Nothing to do!
使用 lvextend -L +1G filename 为根分区增加空闲空间,用法同上。空闲空间可以使用vgdisplay查看,增加空间不需要格式化。
[root@localhost ~]# lvextend -L +3.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.12 (17-May-2010)
Filesystem at /dev/VolGroup/LogVol00 is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/VolGroup/LogVol00 to 4719616 (4k) blocks.
The filesystem on /dev/VolGroup/LogVol00 is now 4719616 blocks long.
swap文件
新建2G的swap文件,挂载到swap,添加到开机启动项。
[root@localhost ~]# dd if=/dev/zero of=/www/swap bs=1024 count=2048000
2048000+0 records in
2048000+0 records out
2097152000 bytes (2.1 GB) copied, 12.0018 s, 175 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 1, size = 2047996 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: 1877 1796 80 0 7 1647
-/+ buffers/cache: 142 1735
Swap: 1999 0 1999
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/www/swap file 2047992 0 -1
修改启动项
编辑 /etc/fstab文件,去掉之前的swap分区挂载,在最末行增加swap文件的挂载
[root@localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jul 23 03:45:08 2015
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-LogVol00 / ext4 defaults 1 1
UUID=4221a802-6ce2-4638-a3a5-7c17b961f291 /boot ext4 defaults 1 2
UUID=b720b68f-37b7-45ce-b4a2-40deeb9fa90f /www ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/www/swap swap swap defaults 0 0
重启后可以看到,根分区空间有增加, swap使用的是swap文件
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-LogVol00
18G 716M 17G 5% /
tmpfs 939M 0 939M 0% /dev/shm
/dev/sda1 485M 32M 429M 7% /boot
/dev/sda2 61G 2.2G 55G 4% /www
[root@localhost ~]# free -m
total used free shared buffers cached
Mem: 1877 135 1741 0 7 36
-/+ buffers/cache: 91 1786
Swap: 1999 0 1999
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/www/swap file 2047992 0 -1
注:之前有实验过直接删掉swap的LV,用文件代替,但是,删除swap的LV后,重启报错如图
按网上的解决办法,开机按“e”加“enforcing=0”神马的不管用。这个问题暂时木有解决,有解决过的老师,请告诉下学生。
- 使用LVM创建弹性磁盘存储——第一部分 http://www.linuxidc.com/Linux/2014-10/107697.htm
- 在Linux中扩展/缩减LVM(第二部分) http://www.linuxidc.com/Linux/2014-10/107786.htm
- 在 LVM中 录制逻辑卷快照并恢复(第三部分) http://www.linuxidc.com/Linux/2014-11/108867.htm
- 在LVM中设置精简资源调配卷(第四部分) http://www.linuxidc.com/Linux/2014-11/109757.htm
- 使用条块化I/O管理多个LVM磁盘(第五部分) http://www.linuxidc.com/Linux/2014-12/110532.htm