将设为首页浏览此站
开启辅助访问 天气与日历 收藏本站联系我们切换到窄版

易陆发现论坛

 找回密码
 开始注册
查看: 823|回复: 4
收起左侧

在线扩容swap分区操作

[复制链接]
发表于 2021-6-20 22:32:22 | 显示全部楼层 |阅读模式
购买主题 本主题需向作者支付 2 金钱 才能浏览
 楼主| 发表于 2021-6-21 15:00:29 | 显示全部楼层
Here are the steps required to increase the amount of swap space in an LVM environment:
: Z1 w; q% r/ i
# ]' U( v: x" b. }  RTurn off all swap.7 [3 s1 U  y. J' V$ m* P

& O  x  S7 |; x( [  ?5 j/ fIncrease the size of the logical volume designated for swap.2 h  v3 [8 V* ^6 [. z" [- k$ \
: k/ c: E& f: ]' c) q
Configure the resized volume as swap space.
4 ^- y- ?1 S1 L- i8 c8 B6 g/ G+ o7 p0 f1 R/ a$ r7 v
Turn on swap.1 r6 B- t8 o# A% P% t: S

1 \* h+ T& X3 _6 q4 I( K9 `. aFirst, verify that swap exists and is a logical volume using the lvs command (list logical volume).
$ i1 N) J/ ?9 P# k, Q3 j% s3 v  q: N3 [
# lvs# \' {2 v' e+ z" J
  LV     VG                Attr       LSize  Pool   Origin Data%  Meta%  Move Log Cpy%Sync Convert+ u7 `- c9 u) l% |2 p9 W
  home   fedora_studentvm1 -wi-ao----  2.00g                                                      
$ @/ J, h6 o# F9 h0 a$ t! u  pool00 fedora_studentvm1 twi-aotz--  2.00g               8.17   2.93                           
* A4 m) H. o3 X: u  root   fedora_studentvm1 Vwi-aotz--  2.00g pool00        8.17                                 
& H3 w* R4 U0 c8 C  swap   fedora_studentvm1 -wi-ao----  8.00g                                                      
0 ]" x! P# ?; S8 h6 S4 o, ?( q  tmp    fedora_studentvm1 -wi-ao----  5.00g                                                      5 x( C2 I6 u1 r1 g$ J# q
  usr    fedora_studentvm1 -wi-ao---- 15.00g                                                      3 m* F- G* R' {1 m& Y( T! @
  var    fedora_studentvm1 -wi-ao---- 10.00g
, u3 ^2 m' _6 DYou can see that the current swap size is 8GB. In this case, we want to add 2GB to this swap volume. First, stop existing swap. You may have to terminate running programs if swap space is in use.
% X/ U3 _: R( o
# G' F1 ^2 W  Z6 s$ swapoff -a) s: N: e) t9 O! v  J
Now increase the size of the logical volume./ e# Y/ s' s7 ^* G; o

5 p7 s* D9 w" V" h, c, n* ]# lvextend -L +2G /dev/mapper/fedora_studentvm1-swap
0 ~. I6 q/ N; k: f! {+ C* M  Size of logical volume fedora_studentvm1/swap changed from 8.00 GiB (2048 extents) to 10.00 GiB (2560 extents).9 d- F4 B; w+ W& K) V2 D1 S) ^0 x
  Logical volume fedora_studentvm1/swap successfully resized., z# X4 g' N+ l- ~, j
Run the mkswap command to make this entire 10GB partition into swap space.
6 E5 d, ~3 [  @& C( C8 F9 H$ t. N* G4 G% w
# mkswap /dev/mapper/fedora_studentvm1-swap
: o; y7 \7 Z2 amkswap: /dev/mapper/fedora_studentvm1-swap: warning: wiping old swap signature.; g/ [% j0 U0 ?4 J& q& N  L2 G
Setting up swapspace version 1, size = 10 GiB (10737414144 bytes)
" g" p3 ~- Q5 y& b' s8 Hno label, UUID=3cc2bee0-e746-4b66-aa2d-1ea15ef1574a3 x9 w/ Q- `7 b0 y, ]/ j
Turn swap back on.
# Z! ~3 C  |0 @  ]. |7 o5 Q$ O: {! B# U! r8 t& s2 a
# swapon -a# T# |/ [5 K) v9 x
Now verify the new swap space is present with the list block devices command. Again, a reboot is not required.% W6 I* r/ x: i$ x# W

& T( r+ P2 H9 Z0 U0 r" v5 v# lsblk" h( |  J6 O: i& H" q3 t9 Q
NAME                                 MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT% m; E6 [) H* x2 e) K2 ]3 d; n
sda                                    8:0    0   60G  0 disk- ^1 ~7 t, s6 P2 W8 G3 O, k
|-sda1                                 8:1    0    1G  0 part /boot
) d. l% l' `* @`-sda2                                 8:2    0   59G  0 part+ h, K) H( v# i2 M( \2 S
  |-fedora_studentvm1-pool00_tmeta   253:0    0    4M  0 lvm  
' [8 M* }2 k$ q+ ~8 D: P  | `-fedora_studentvm1-pool00-tpool 253:2    0    2G  0 lvm  
$ ?; h1 D( E# a8 }/ k0 f' {  |   |-fedora_studentvm1-root       253:3    0    2G  0 lvm  // u# J) H- Z" X9 Q* {2 A
  |   `-fedora_studentvm1-pool00     253:6    0    2G  0 lvm  
' {: I7 @1 a- L; `9 G  |-fedora_studentvm1-pool00_tdata   253:1    0    2G  0 lvm  
* P. z+ e. @8 ?  | `-fedora_studentvm1-pool00-tpool 253:2    0    2G  0 lvm  4 A, \& c5 l7 n
  |   |-fedora_studentvm1-root       253:3    0    2G  0 lvm  /
' K3 O/ q# J  c% r5 `5 I9 x  |   `-fedora_studentvm1-pool00     253:6    0    2G  0 lvm  1 t! }$ b! y( _/ t$ f9 U
  |-fedora_studentvm1-swap           253:4    0   10G  0 lvm  [SWAP]# S: e" z/ h: N6 R
  |-fedora_studentvm1-usr            253:5    0   15G  0 lvm  /usr) B; ]2 h- c/ E+ @# U( g2 _/ i8 S
  |-fedora_studentvm1-home           253:7    0    2G  0 lvm  /home6 |- w9 T" I- D- t6 ~6 W) m/ F5 C( R# h
  |-fedora_studentvm1-var            253:8    0   10G  0 lvm  /var. Z$ H# K/ V  a5 T( X+ s: L2 K
  `-fedora_studentvm1-tmp            253:9    0    5G  0 lvm  /tmp
( u( ^- K; o) v7 Osr0
6 n3 b- D1 U( D% H8 ~You can also use the swapon -s command, or top, free, or any of several other commands to verify this.- H4 c1 ^1 J( ^' `. n6 p$ l
' X. x6 b9 k  N+ w  A
# free
3 G- Q2 V2 U& v+ {1 O/ Z4 z              total        used        free      shared  buff/cache   available
, m3 B( G3 f" T, n& C6 k/ LMem:        4038808      382404     2754072        4152      902332     3404184
9 y9 H5 n8 z% d) M6 P$ ySwap:      10485756           0    10485756
 楼主| 发表于 2021-6-21 15:00:42 | 显示全部楼层
Adding more swap space to a non-LVM disk environment2 P9 x- ~1 H( @/ Z. D. |+ H. E; I. x+ Z; `
Due to changing requirements for swap space on hosts with Linux already installed, it may become necessary to modify the amount of swap space defined for the system. This procedure can be used for any general case where the amount of swap space needs to be increased. It assumes sufficient available disk space is available. This procedure also assumes that the disks are partitioned in “raw” EXT4 and swap partitions and do not use logical volume management (LVM).
1 v9 {2 S4 m( _7 u' u# a* q' n  T& |( ^5 n3 D. F5 F
The basic steps to take are simple:$ x# C. q; U& h6 {
, K4 B( O, O$ S, j* S
Turn off the existing swap space.
8 m/ Z. o0 o5 M% T! b
6 n! }1 v" x/ ?Create a new swap partition of the desired size.% }1 D! [! {+ w( X$ }, e

) _& q4 w% [- R, C5 Z0 _" t# ]Reread the partition table.3 V  O! |+ s- h- M, q: q* t" P
- m6 v1 l' \/ S; L
Configure the partition as swap space.3 }# W, P+ ]. W* p: q

& |- [% ~. H1 M9 EAdd the new partition/etc/fstab.
( _# F" R3 ?7 y1 b" ~( T% e1 z! q# \+ h# H; S' Z
Turn on swap.
" h  \1 t8 z  Y. a$ k& l$ u& s1 M5 s3 i3 t9 L) R
A reboot should not be necessary.
. v, H: x$ P+ B( q
6 E8 I5 N! \5 JFor safety's sake, before turning off swap, at the very least you should ensure that no applications are running and that no swap space is in use. The free or top commands can tell you whether swap space is in use. To be even safer, you could revert to run level 1 or single-user mode./ K0 h: G0 \. z
2 B# `7 }; ~$ p& v) ]
Turn off the swap partition with the command which turns off all swap space:
+ |/ G% x& [3 V% ~5 P! b, q* Z) D! T1 _5 T3 ?0 @. z0 t
$ swapoff -a( S- \5 r  I- B4 ~# T; d
Now display the existing partitions on the hard drive.
. c8 A) t# B1 M" {. Y$ f2 }- ^; ^+ X
$ fdisk -l) s! \2 Y% g2 y" w, M, h* c
This displays the current partition tables on each drive. Identify the current swap partition by number.9 R' k6 h. U/ l8 U: y& G6 f

- v" L. }: @8 E8 L5 v/ n- oStart fdisk in interactive mode with the command:1 M2 G$ G- `! O: ?

) p% m) d2 j  d6 ]4 F* ?9 w0 K7 q$ I% I$ fdisk /dev/<device name>' t- g9 c, P. W7 t( T
For example:8 W9 ?3 r& F( ?9 `' |

9 o( ?. A  `) x, h$ p0 G3 N$ fdisk /dev/sda
6 X9 k2 }% \# A5 J* ~9 }0 WAt this point, fdisk is interactive and operates only on the specified disk drive.
/ d4 u2 k* R* x0 R, W
( U, m* y5 ?( B- m' lUse the fdisk p sub-command to verify that there is enough free space on the disk to create the new swap partition. The space on the hard drive is shown in terms of 512-byte blocks and starting and ending cylinder numbers, so you may have to do some math to determine the available space between and at the end of allocated partitions.$ z' }9 s6 x5 g2 U9 g
& W* y# `! }* v$ R
Use the n sub-command to create a new swap partition. fdisk will ask you the starting cylinder. By default, it chooses the lowest-numbered available cylinder. If you wish to change that, type in the number of the starting cylinder.! c* P' X! }* J5 G
4 o4 g1 o. y1 E* i! L6 }; B5 S
The fdisk command now allows you to enter the size of the partitions in a number of formats, including the last cylinder number or the size in bytes, KB or MB. Type in 4000M, which will give about 4GB of space on the new partition (for example), and press Enter.8 W' i0 z8 `" q4 Z# T

  a9 V2 R7 s( C) m0 D* R: OUse the p sub-command to verify that the partition was created as you specified it. Note that the partition will probably not be exactly what you specified unless you used the ending cylinder number. The fdisk command can only allocate disk space in increments on whole cylinders, so your partition may be a little smaller or larger than you specified. If the partition is not what you want, you can delete it and create it again.3 B# E1 q/ E' E7 M- V# u* z" H  Y# D
4 n  z7 g2 U. N8 S. A6 S) o8 a2 a
Now it is necessary to specify that the new partition is to be a swap partition. The sub-command t allows you to specify the type of partition. So enter t, specify the partition number, and when it asks for the hex code partition type, type 82, which is the Linux swap partition type, and press Enter.
! R+ S6 H# B! b- J1 n; t" H3 M' s( A, e
When you are satisfied with the partition you have created, use the w sub-command to write the new partition table to the disk. The fdisk program will exit and return you to the command prompt after it completes writing the revised partition table. You will probably receive the following message as fdisk completes writing the new partition table:1 B# X1 B' j+ L7 D% Y6 N! e9 I

1 D1 q5 p4 `& x. J$ h' L4 y/ |The partition table has been altered!
7 \" l, F; p# D7 mCalling ioctl() to re-read partition table.
, i$ v" z1 M: _4 a9 g6 x' {3 xWARNING: Re-reading the partition table failed with error 16: Device or resource busy.
; e0 T* v! x2 y) ~2 N( e; eThe kernel still uses the old table.
: S" S- p/ D9 G) t; WThe new table will be used at the next reboot.
% @6 R0 h7 m' H6 D  w, WSyncing disks.
* n& p7 i! P) C- rAt this point, you use the partprobe command to force the kernel to re-read the partition table so that it is not necessary to perform a reboot.' }. _. Z; c; ~4 Q) h! E& t
/ N2 S1 H( ]& C' }6 t
$ partprobe, O" y9 y' C7 m5 j
Now use the command fdisk -l to list the partitions and the new swap partition should be among those listed. Be sure that the new partition type is “Linux swap”.
7 q) C- f" V& R  U  j- w( h
. X+ K3 K6 W. V& |8 Q9 u& cIt will be necessary to modify the /etc/fstab file to point to the new swap partition. The existing line may look like this:# V5 n: o1 ?: J( ?+ j6 L
2 P7 K5 f' d1 t8 ?* A, Q, H
LABEL=SWAP-sdaX   swap        swap    defaults        0 0
4 C% o! j8 o& ^where X is the partition number. Add a new line that looks similar this, depending upon the location of your new swap partition:
9 o3 `1 @$ }8 Q" a% R( ^' v5 L' b6 ^6 T- k0 C0 p# D' p9 U
/dev/sdaY         swap        swap    defaults        0 0  A5 v8 V4 A, G, b+ i. ]! g& O
Be sure to use the correct partition number. Now you can perform the final step in creating the swap partition. Use the mkswap command to define the partition as a swap partition.$ G  R* s2 p. u0 P; W( y5 q9 k- m

" d/ t4 g0 j5 j$ mkswap /dev/sdaY) b  T9 L% r" ?9 l7 [
The final step is to turn swap on using the command:8 E0 s. r  L! C7 X+ H9 b; |) F, \

( z( ^9 w) N" h$ @) B$ swapon -a6 K0 r$ P( r8 a. [6 k, Z6 Q
Your new swap partition is now online along with the previously existing swap partition. You can use the free or top commands to verify this.
 楼主| 发表于 2021-6-21 15:02:38 | 显示全部楼层
查看修改swap使用比例2 A# b/ L1 w8 l0 ]: a+ G1 G
1.查看! H$ y/ k4 B; A& Q
9 c4 e' ~. b, @- P$ E4 Z
cat /proc/sys/vm/swappiness  //0不使用  100极力使用
5 O  Y" \: ~. u" i* p' F# ?- r# i- X2.临时修改使用比例2 o( H5 w' b7 e( b& i5 V

) x6 }  A" ]* {sysctl vm.swappiness=60
" `9 s$ N8 b- k8 M( O3.永久修改使用比例
$ V4 K. E, k* w- Q  K' D6 A- d" u3 X. ?4 @& l; g* \
vi  /etc/sysctl.conf# j$ x# ~7 h0 j  F) L: v
在这个文档的最后加上这样一行
% R7 z. H# w' H+ q6 I6 e
) U8 X/ o  B8 j. k) Lvm.swappiness=60
 楼主| 发表于 2021-6-23 09:34:14 | 显示全部楼层
系统默认配置为30或者60   centos8中设置默认30,centos7中默认设置60
( W* b- P' {' h( u1 t( i可以通过4 L2 e5 j+ g8 [2 ^/ i! N
cat /proc/sys/vm/swappiness  - [9 B: S* i7 M
查看默认配置大小。
. H5 u* l, R, S# T0 K; G3 a* S# cat /proc/sys/vm/swappiness  . o- ^! z3 u1 U& m
604 d, j: g1 s" K9 V

5 D* C/ @  [! ^
您需要登录后才可以回帖 登录 | 开始注册

本版积分规则

关闭

站长推荐上一条 /4 下一条

如有购买积分卡请联系497906712

QQ|返回首页|Archiver|手机版|小黑屋|易陆发现 点击这里给我发消息

GMT+8, 2021-11-28 19:56 , Processed in 0.045187 second(s), 25 queries .

Powered by LR.LINUX.cloud bbs168x X3.2 Licensed

© 2012-2022 Comsenz Inc.

快速回复 返回顶部 返回列表