- 积分
- 11080
在线时间 小时
最后登录1970-1-1
|

楼主 |
发表于 2021-6-21 15:00:42
|
显示全部楼层
Adding more swap space to a non-LVM disk environment+ }( ?; d9 W4 N. Z) [' B
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).
* B/ Q' D* @, I) r% X2 w) Z3 @$ t, p$ [ v8 x: m
The basic steps to take are simple:
7 Y, t) Z% G: @- p0 v2 d7 q& R) i& N( x: U6 Z
Turn off the existing swap space.9 w( f1 k6 l1 }! O7 a+ A8 B
* s7 ^# Q* z- s4 }( o; O; QCreate a new swap partition of the desired size.
' X7 {) O% p1 g) d" A6 X* ?( G9 S3 d3 _$ J: l, a; Z% J
Reread the partition table.
& I6 ?3 L' Q7 m0 P1 q
! a0 |) R, V8 Z. U% |$ X9 `Configure the partition as swap space.
2 r$ m& n/ k. p. }8 r
0 \ b, ?4 ]5 Z$ e1 A: F1 MAdd the new partition/etc/fstab.# ~: l* Z* j, b) H: ~; w( d6 m
2 L9 _- e% C9 ]: Y l! V$ K
Turn on swap.2 I, E% b/ `: y+ W
9 R8 p9 ~' O7 [: X
A reboot should not be necessary., ~$ Y0 ^7 T4 m a; H3 u
; r, F. }+ U9 V
For 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.
: x8 `4 v( b5 k( H, a
5 p6 B5 i& j' l+ t" t" x) xTurn off the swap partition with the command which turns off all swap space:. W( m! e. G+ J7 ]
9 Q2 m! W7 Q- c; ?6 l# H. G' H$ swapoff -a
+ k/ K* r7 c9 [% pNow display the existing partitions on the hard drive.
$ q& L& I4 m' B3 @8 J2 n
j* y7 \% k7 `. b @' R$ fdisk -l
# ?! e! Z/ _: k% e7 J' s! wThis displays the current partition tables on each drive. Identify the current swap partition by number.1 O3 j" Z( i$ d4 U- S# ]: V
$ I+ f& _" Z/ c- PStart fdisk in interactive mode with the command:
& r& c" O& Y: _. Q5 r/ E% p6 \" O; J% h6 s$ M+ k2 i S
$ fdisk /dev/<device name>
% l5 P& y- `. H0 J! NFor example:, V! V2 n! _! R2 g
* ]( v$ E. s7 C2 ?) A) ]. x5 g y$ fdisk /dev/sda
2 g1 c& ~% ]2 g. T2 ZAt this point, fdisk is interactive and operates only on the specified disk drive. * Z$ W: ^+ W! x0 p3 E
+ L: ]; _( q8 L+ D8 @Use 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.4 ?& t& s& ?" ?( ~, ]
3 g1 g6 c6 D/ S& ~. j8 L" V
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.0 P+ L! ?0 n% v3 C
]# y0 o5 q3 H8 g" B, u& cThe 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.
+ }! f1 F' }. w
" y9 H, A! Y# R$ G. ], fUse 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.! G$ A: [7 x, P+ ]& \+ ^
* U$ \- q; U$ Z" r: J7 q
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.
# K. L/ K% ?4 ?$ h! y3 `5 S0 L1 V1 C+ f4 i% @" W
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:
0 K( z3 ~7 w7 i/ K+ F! v3 x
3 e: n' y3 Q( q! RThe partition table has been altered!
" F$ ^0 s. _$ L$ `% G3 i, i; E9 NCalling ioctl() to re-read partition table.
6 s; L5 w- W$ `WARNING: Re-reading the partition table failed with error 16: Device or resource busy.' t& N2 l' U/ C1 |
The kernel still uses the old table.. S% S5 l& R: z- y+ E
The new table will be used at the next reboot." h; h$ A& F7 R9 x' [' }
Syncing disks.
! I Q0 Q) k& r( \4 T5 W& \: S. uAt 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.' f7 t2 P( L1 A& @/ h5 ?7 i4 l
4 ~1 W+ \; z" J/ X3 e+ N. Q$ partprobe" _& G" l n, N5 D' T
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”.) {* ` Q: ?: z2 P
4 t) l# @" x' \9 l" TIt will be necessary to modify the /etc/fstab file to point to the new swap partition. The existing line may look like this:
( ]0 K( B# d! S0 H/ \3 t
" j' o: h' `4 N- G3 U7 cLABEL=SWAP-sdaX swap swap defaults 0 0+ M% v/ `5 m9 I/ [$ j# g1 A3 G
where X is the partition number. Add a new line that looks similar this, depending upon the location of your new swap partition:! n" x; X- |) z
9 T+ ^( u- J! k* u/dev/sdaY swap swap defaults 0 0. U+ H1 N- r! B3 I$ M& l) K; S
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.
4 j. L3 S, o7 W G$ I! E. O% t! g' u1 f) M! [
$ mkswap /dev/sdaY0 O% _+ J3 D9 {/ ~2 I3 i# V
The final step is to turn swap on using the command:0 W2 f% `7 P% z5 F
# e" I' V$ p3 L4 P- y0 V7 y1 e- S
$ swapon -a% z- ~7 A' G$ y" i' ~$ y1 K+ s
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. |
|