增加,修改 Linux 交换空间
Itallhappenssometime:yourunoutofyourmainmemoryandthesystemturnstotheharddisktouseasatemporarymemory.WhatifyoucometothelimitsofyourLinuxserver’sswapspacebutthesystemdemandsmore?Therearebasicallyfouroptionshere:
1.Createanewswapfile
2.Createaswapfileonanexistinglogicalvolume
3.Extendaswapfileonanexistinglogicalvolume
4.Createanewswappartition
Ifyouareinneedofswapspacenowanddonotthinkyouwillneeditlater,thentakingthesystemdownforcreatinganewswappartitionorextendingtheswapfileonthelogicalvolumewillbethelongerwaytotake;creatinganewswapfileandmakingitavailablewillbetheeasiestandthequickestsolution.So,inourlist,creatinganewswapfileistheeasiest,extendingswapfileonanexistinglogicalvolumeisalittleharderbutrecommended,andcreatinganewswappartitionisthehardest.
CreateaNewSwapFile
Weneedtoknowthesizeoftheadditionalswapspacewewillneed.Let’sassumethatwewillneeda512Megabytesofswapspace.Todeterminethenumberofblocks,weneedtomultiplythesizewith1024.Inourexamplethisgives512x1024=524288.Nowweissue
ddif=/dev/zeroof=/home/swapfilebs=1024count=524888
toopenupourfreespace(youmayarguethatmakingaswapspaceinthe/homedirectoryisquestionableintermsofsecurity,butifyou’retightonharddiskspaceontheserver,/homeisthedirectorythatyoucanfindsomefreespace).Thensetupthefileasswapwith
mkswap/home/swapfile
Theswapspacewillnotbeenabledatboottime.Youneedtoaddthespaceto/etc/fstab.Addthefollowinglinetoyour/etc/fstab:
/home/swapfileswapswapdefaults00
CreatingaSwapFileonanExistingLogicalVolume
Inthiscase,let’sassumewewanttoaddtheswapspaceon/dev/VolGroup00and/LogVol02istheswapspacewewanttoadd.Let’salsoassumethattheswapspaceweneedis2Gigabytes.First,wecreatethelogicalvolumeofsize2GBby
lvmlvcreateVolGroup00-nLogVol02-L2G
Thenweformatitby
mkswap/dev/VolGroup00/LogVol02
andadditto/etc/fstabbyaddingthisline:
/dev/VolGroup00/LogVol02swapswapdefaults00
Nowweactivatetheswapvolumeby
swapon-va
Tocheckiftheswapisworkingproperly,youcanissuecat/proc/swapsorsimplyfree.
Readonforextendingaswapfileonanexistinglogicalvolumeandcreatinganewpartition.
ExtendingSwapFileonanExistingLogicalVolume
Forthiscase,let’sassumethattheswapspacewewillextendis/dev/VolGroup00/LogVol01andwewanttoextenditby1Gigabyte.First,wedisabletheactiveswapfileby
swapoff-v/dev/VolgGroup00/LogVol01
Thenweaddtheswapspaceby
lvmlvresize/dev/Volgroup00/LogVol01-L1G
formatit
mkswap/dev/VolGroup00/LogVol01
andfinallyenabletheextendedvolumebyswapon-va.Asalways,youcancheckthenewspacebycat/proc/swapsorfreecommands.
CreateaNewSwapPartition
Wecangowiththepartedprogramheresinceitiseasierthanfdisk.Beforeaddingaswappartition,wehavetocheckwhichharddiskhassufficientspacetobeusedasswap.Wecandothatbyissuingprintatthepartedprompt.Withtheinformationwereceive,wedeterminehowmuchspacewewilluseandatwhatpartition.Assumethatwewilladd1GBofswapspaceto/dev/hdc1.Thesyntaxthatwewilluseinpartedis
mkpartfspartition-typefilesystem-typestartend
wherestartandendarethemegabytesfromthebeginningofthedisk(partition-typecanbeprimary,extendedorlogicalbutextendedandlogicalareusedonlyforMS-DOSandMIPSlabels).Furtherassumingthatthefreespaceon/dev/hdc1startsatthe520thmegabyte,theendingwillbe520+1024(1Gigabyteofspace)=1544.Soourcommandwillbe
mkpartfsprimarylinux-swap5201024
Afterreservingthespace,weformatitwithswappartitiontype,using
mkswap/dev/hdc1
Thenweenableitbyswapon/dev/hdc1.Andfinally,toactivateitinboottime,weadditto/etc/fstabbyaddingthefollowingline:
/dev/hdc1swapswapdefaults00
Aswehaveseen,wechecktheswapbycat/proc/swapsorfree.
Conclusion
Withthefallingpricesofmainmemoriy(RAM),usuallysystemadministratorsdonotneedtheswapspaceindailynormaloperations.Buttimescanbetoughandthesystemmayrunoutofswapspace,andthentheadministratorcanhavenochancebuttoincreaseswapsizeinordertocopewiththepeakload.Thequickestwaycouldbetoaddaswapfileandthelong-termsolutionistoaddaswappartition.
RelatedReading
HowtoBuildaLinuxServerhttp://www.brighthub.com/computing/linux/articles/29541.aspx-InthisserieswelookatbuildingandconfiguringaLinuxserverfromscratch.Wewilllookatconfigurationindetailandwewilldeterminewaystokeepourserverassecureaspossible.