如何擴容縮減swap分區 一.swap簡單介紹 二.通過swap分區文件增加減少swap空間 三.通過LVM擴容縮減swap空間
一.swap簡單介紹
? swap交換內存主要是指當物理內存不夠用時,系統會啟用硬盤的一部分空間來充當服務器內存,而默認情況下swap內存會有一些設置標準,它與物理內存的大小也是有關系的:
Ram大小 Swap大小 激活Swap后合計大小
256MB 256MB 512MB
512MB 512MB 1GB
1GB 1GB 2GB
3GB 2GB 5GB
4GB 2GB 6GB
6GB 2GB 8GB
8GB 3GB 11GB
12GB 3GB 15GB
16GB 4GB 20GB
32GB 6GB 38GB
64GB 8GB 72GB
128GB 11GB 139GB
官方官方建議在RAM是2到4.5G時,swap是RAM的2倍;如果RAM大于等于4G則swap等于RAM即可
當預先設置的Swap分區空間不足,這個時候需要增加其大小,今天總結兩種增減swap大小的方法。
通過swap分區文件增加swap空間 通過LVM擴容縮減swap空間
話不多說,直接開始。實驗用centos7機器。
目標增減swap2G。
二.通過swap分區文件增加減少swap空間
1.擴展
先查看一下當前swap大小
[root@fei-7 ~] total used free shared buff
/ cache available
Mem: 1
. 9G 164M 1
. 3G 7
. 6M 491M 1
. 7G
Swap: 2
. 0G 0B 2
. 0G
創建一個2G的文件
[root@fei-7 mnt]
2048
+ 0 records in
2048
+ 0 records out
2147483648 bytes
( 2
. 1 GB
) copied
, 19
. 3321 s
, 111 MB
/ s
[root@fei-7 mnt]
2
. 0G
/ mnt
/ swap
將文件格式化為swap
[root@fei-7 mt]
Setting up swapspace version 1
, size = 2097148 KiB
no label
, UUID=c44df16e
- 2eb3
- 4132
- 9363
- b0d32324c6a8
掛載并查看swap分區
[root@fei-7 mnt]
swapon:
/ mnt
/ swap: insecure permissions 0644
, 0600 suggested
. [root@fei-7 mnt] total used free shared buff
/ cache available
Mem: 1
. 9G 163M 17M 7
. 5M 1
. 8G 1
. 7G
Swap: 4
. 0G 0B 4
. 0G
將分區信息寫入/etc/fstab,如果不寫入文件,則在下次啟動后恢復原樣。
[root@fei-7 mnt] [root@fei-7 mnt]
/ dev
/ mapper
/ centos
- root
/ xfs defaults 0 0
UUID=0362beab
- af6c
- 4573
- 8907
- b3eb62e36a81
/ boot xfs defaults 0 0
/ dev
/ mapper
/ centos
- swap swap swap defaults 0 0
/ dev
/ sr0
/ media iso9660 defaults 0 0
/ mnt
/ swap swap swap defaults 0 0
重啟后檢查一下swap組成
[root@fei-7 ~]
Filename
Type Size Used Priority
/ mnt
/ swap file 2097148 0
- 2
/ dev
/ dm
- 1 partition 2097148 0
- 3
所以以上是通過swap分區文件增加swap空間的具體操作步驟。
2.縮減(刪除)
卸載剛才掛載的swap文件。
[ root@fei-7 ~
]
[ root@fei-7 ~
] total used
free shared buff/cache available
Mem: 1.8G 128M 1.5G 9.4M 203M 1.5G
Swap: 2.0G 0B 2.0G
刪除/etc/fstab下添加的掛載信息
[ root@fei-7 ~
] [ root@fei-7 ~
]
/dev/mapper/centos-root / xfs defaults 0 0
UUID
= 0362beab-af6c-4573-8907-b3eb62e36a81 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/sr0 /media iso9660 defaults 0 0
最后將swap文件刪除
[ root@fei-7 ~
通過swap分區文件增加或者縮減swap空間這種方法較為簡單,建議使用這種方法。
三.通過LVM擴容縮減swap空間
查看一下當前系統swap分區情況
[ root@fei-7 ~
] total used
free shared buff/cache available
Mem: 1.8G 138M 1.4G 9.4M 229M 1.4G
Swap: 2.0G 0B 2.0G
1.擴容
如果對LVM還不熟悉,可以在此先了解一下:linux LVM邏輯卷相關命令及創建,修改過程
先查看一下邏輯卷(lvm)和邏輯卷(vg)組詳細情況
[ root@fei-7 ~
] --- Logical volume ---LV Path /dev/centos/swapLV Name swap VG Name centos LV UUID m4d0ur-Iz58-UAfd-tIPd-N5gM-Eqf8-NQvbvvLV Write Access read/writeLV Creation host,
time xiaoagiao, 2020-02-15 17:41:53 +0800LV Status availableLV Size 2.00 GiB Current LE 512Segments 1Allocation inheritRead ahead sectors auto- currently
set to 8192Block device 253:1
[ root@fei-7 ~
] --- Volume group ---VG Name centosSystem ID Format lvm2Metadata Areas 2Metadata Sequence No 5VG Access read/writeVG Status resizableMAX LV 0Cur LV 2Open LV 2Max PV 0Cur PV 2Act PV 2VG Size 108.99 GiBPE Size 4.00 MiBTotal PE 27902Alloc PE / Size 27902 / 108.99 GiBFree PE / Size 0 / 0 VG UUID VWTn40-zXfH-77PH-riAc-HC1U-5lqQ-1juay8
此時添加一塊硬盤,并查看。
[ root@fei-7 ~
]
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
/dev/sdb1 2048 188745727 94371840 83 Linux
Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 sectors
將這個硬盤空間創建為物理卷
[ root@fei-7 ~
] Physical volume
"/dev/sdc" successfully created.
[ root@fei-7 ~
] PV VG Fmt Attr PSize PFree/dev/sda2 centos lvm2 a--
< 19.00g 0 /dev/sdb1 centos lvm2 a--
< 90.00g 0 /dev/sdc lvm2 --- 5.00g 5.00g
接下來擴展centos這個卷組
[ root@fei-7 ~
] Volume group
"centos" successfully extended
[ root@fei-7 ~
] --- Volume group ---VG Name centosSystem ID Format lvm2Metadata Areas 3Metadata Sequence No 6VG Access read/writeVG Status resizableMAX LV 0Cur LV 2Open LV 2Max PV 0Cur PV 3Act PV 3VG Size
< 113.99 GiBPE Size 4.00 MiBTotal PE 29181Alloc PE / Size 27902 / 108.99 GiBFree PE / Size 1279 /
< 5.00 GiB VG UUID VWTn40-zXfH-77PH-riAc-HC1U-5lqQ-1juay8
到這一步之前分為兩種情況:
如果在第一步查看centos這個卷組時,發現有剩余空間,則直接進行橫線下操作。 若沒有剩余,和演示的一樣,則按照步驟依次進行。
接下來真正進行擴容,分為四步(卸載,擴展,格式化,掛載)
卸載swap分區
[ root
@fei - 7 ~ ] # swapoff
/ dev
/ mapper
/ centos
- swap
擴展swap邏輯卷
[ root
@fei - 7 ~ ] # vgs #先看一下centos這個卷組為
5 GVG #PV #LV #SN Attr VSize VFree centos
3 2 0 wz
-- n
- < 113.99 g
< 5.00 g
[ root
@fei - 7 ~ ] # lvextend
- L
+ 2 G
/ dev
/ mapper
/ centos
- swap #給swap這個邏輯卷添加
2 GSize of logical volume centos
/ swap changed from
2.00 GiB
( 512 extents
) to
4.00 GiB
( 1024 extents
) . Logical volume centos
/ swap successfully resized
. [ root
@fei - 7 ~ ] # vgs #現在剩余
3 GVG #PV #LV #SN Attr VSize VFree centos
3 2 0 wz
-- n
- < 113.99 g
< 3.00 g
[ root
@fei - 7 ~ ] # lvs LV VG Attr LSize Pool Origin Data
% Meta
% Move Log Cpy
% Sync Convertroot centos
- wi
- ao
-- -- 106.99 g swap centos
- wi
- a
-- -- - 4.00 g #此時邏輯卷swap共
4 G
格式化新swap分區
[ root
@fei - 7 ~ ] # mkswap
/ dev
/ mapper
/ centos
- swap
mkswap
: / dev
/ mapper
/ centos
- swap
: warning
: wiping old swap signature
.
Setting up swapspace version
1 , size
= 4194300 KiB
no label
, UUID
= 4 be30881
- 944 a
- 42 c2
- 8d 3 e
- d8fbd183b698
重新掛在swap分區
[ root
@fei - 7 ~ ] # swapon
/ dev
/ mapper
/ centos
- swap #掛載
[ root
@fei - 7 ~ ] # free
- htotal used free shared buff
/ cache available
Mem
: 1.8 G
130 M
1.5 G
9.4 M
168 M
1.5 G
Swap
: 4.0 G
0 B
4.0 G
到此處,LVM擴容swap完成
2.縮減
其實就是縮減這個名為swap的邏輯卷
卸載swap分區 縮減邏輯卷
[ root
@fei - 7 ~ ] # lvreduce
- L
2 G
/ dev
/ mapper
/ centos
- swap WARNING
: Reducing active logical volume to
2.00 GiB
. THIS MAY DESTROY YOUR DATA
( filesystem etc
. )
Do you really want to reduce centos
/ swap
? [ y
/ n
] : ySize of logical volume centos
/ swap changed from
4.00 GiB
( 1024 extents
) to
2.00 GiB
( 512 extents
) . Logical volume centos
/ swap successfully resized
. [ root
@fei - 7 ~ ] # lvsLV VG Attr LSize Pool Origin Data
% Meta
% Move Log Cpy
% Sync Convertroot centos
- wi
- ao
-- -- 106.99 g swap centos
- wi
- a
-- -- - 2.00 g #此時已經變回
2 G
[ root
@fei - 7 ~ ] # pvsPV VG Fmt Attr PSize PFree
/ dev
/ sda2 centos lvm2 a
-- < 19.00 g
0 / dev
/ sdb1 centos lvm2 a
-- < 90.00 g
0 / dev
/ sdc centos lvm2 a
-- < 5.00 g
< 5.00 g #centos卷組變回
5 G
格式化swap分區
[ root
@fei - 7 ~ ] # mkswap
/ dev
/ mapper
/ centos
- swap
mkswap
: / dev
/ mapper
/ centos
- swap
: warning
: wiping old swap signature
.
Setting up swapspace version
1 , size
= 2097148 KiB
no label
, UUID
= e6c3f988
- 8125 - 4574 - 8242 - e7f3efcff1f5
重新掛載
[ root@fei
- 7 ~ ] # swapon
/ dev
/ mapper
/ centos
- swap
[ root@fei
- 7 ~ ] # free
- htotal used free shared buff
/ cache available
Mem
: 1.8 G
128 M
1.5 G
9.4 M
169 M
1.5 G
Swap
: 2.0 G
0 B
2.0 G #重新變回
2 G
– 以上是通過LVM擴容縮減swap空間全過程 –
總結
以上是生活随笔 為你收集整理的手把手教学linux上扩容和缩减swap分区。 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。