2021-05-12 14:32:11
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
本文永久更新連結地址:http://www.linuxidc.com/Linux/2015-07/120447.htm
相關文章