`
qqdwll
  • 浏览: 131621 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

增加,修改 Linux 交换空间

阅读更多
It all happens some time: you run out of your main memory and the system turns to the hard disk to use as a temporary memory. What if you come to the limits of your Linux server’s swap space but the system demands more? There are basically four options here:

   1. Create a new swap file
   2. Create a swap file on an existing logical volume
   3. Extend a swap file on an existing logical volume
   4. Create a new swap partition

If you are in need of swap space now and do not think you will need it later, then taking the system down for creating a new swap partition or extending the swap file on the logical volume will be the longer way to take; creating a new swap file and making it available will be the easiest and the quickest solution. So, in our list, creating a new swap file is the easiest, extending swap file on an existing logical volume is a little harder but recommended, and creating a new swap partition is the hardest.

Create a New Swap File

We need to know the size of the additional swap space we will need. Let’s assume that we will need a 512 Megabytes of swap space. To determine the number of blocks, we need to multiply the size with 1024. In our example this gives 512 x 1024 = 524288. Now we issue

dd if=/dev/zero of=/home/swapfile bs=1024 count=524888

to open up our free space (you may argue that making a swap space in the /home directory is questionable in terms of security, but if you’re tight on hard disk space on the server, /home is the directory that you can find some free space). Then setup the file as swap with

mkswap /home/swapfile

The swap space will not be enabled at boot time. You need to add the space to /etc/fstab. Add the following line to your /etc/fstab:

/home/swapfile swap swap defaults 0 0

Creating a Swap File on an Existing Logical Volume

In this case, let’s assume we want to add the swap space on /dev/VolGroup00 and /LogVol02 is the swap space we want to add. Let’s also assume that the swap space we need is 2 Gigabytes. First, we create the logical volume of size 2 GB by

lvm lvcreate VolGroup00 -n LogVol02 -L 2 G

Then we format it by

mkswap /dev/VolGroup00/LogVol02

and add it to /etc/fstab by adding this line:

/dev/VolGroup00/LogVol02 swap swap defaults 0 0

Now we activate the swap volume by

swapon -va

To check if the swap is working properly, you can issue cat /proc/swaps or simply free.

Read on for extending a swap file on an existing logical volume and creating a new partition.
Extending Swap File on an Existing Logical Volume

For this case, let’s assume that the swap space we will extend is /dev/VolGroup00/LogVol01 and we want to extend it by 1 Gigabyte. First, we disable the active swap file by

swapoff -v /dev/VolgGroup00/LogVol01

Then we add the swap space by

lvm lvresize /dev/Volgroup00/LogVol01 -L 1G

format it

mkswap /dev/VolGroup00/LogVol01

and finally enable the extended volume by swapon -va. As always, you can check the new space by cat /proc/swaps or free commands.

Create a New Swap Partition

We can go with the parted program here since it is easier than fdisk. Before adding a swap partition, we have to check which hard disk has sufficient space to be used as swap. We can do that by issuing print at the parted prompt. With the information we receive, we determine how much space we will use and at what partition. Assume that we will add 1 GB of swap space to /dev/hdc1. The syntax that we will use in parted is

mkpartfs partition-type filesystem-type start end

where start and end are the megabytes from the beginning of the disk (partition-type can be primary, extended or logical but extended and logical are used only for MS-DOS and MIPS labels). Further assuming that the free space on /dev/hdc1 starts at the 520th megabyte, the ending will be 520 + 1024 (1 Gigabyte of space) = 1544. So our command will be

mkpartfs primary linux-swap 520 1024

After reserving the space, we format it with swap partition type, using

mkswap /dev/hdc1

Then we enable it by swapon /dev/hdc1. And finally, to activate it in boot time, we add it to /etc/fstab by adding the following line:

/dev/hdc1 swap swap defaults 0 0

As we have seen, we check the swap by cat /proc/swaps or free.

Conclusion

With the falling prices of main memoriy (RAM), usually system administrators do not need the swap space in daily normal operations. But times can be tough and the system may run out of swap space, and then the administrator can have no chance but to increase swap size in order to cope with the peak load. The quickest way could be to add a swap file and the long-term solution is to add a swap partition.


Related Reading

How to Build a Linux Serverhttp://www.brighthub.com/computing/linux/articles/29541.aspx - In this series we look at building and configuring a Linux server from scratch. We will look at configuration in detail and we will determine ways to keep our server as secure as possible.


分享到:
评论

相关推荐

    LINUX操作系统(电子教案,参考答案)

    其中包括交换空间、/proc的信息以及各种日志文件和进程记账等。 第12章:图形界面。本章主要介绍了X Window以及它的配置,并且介绍了在KDE环境下汉化的基本思想,用实例来讲解了Linux汉化的基础知识。 第13章:网络...

    Linux从入门到精通

    4.4 初试化交换空间 4.5 仅对FTP安装... 4.6 仅对SMB安装... 4.7 仅对硬盘安装... 4.8 分区格式化 4.9 选择和安装包 4.9.1 选择部件 4.9.2 选择单独的包 4.9.2.1 取得关于包的信息 4.9.3 包的相关性 4.9.4...

    linux操作系统教程

    其中包括交换空间、/proc的信息以及各种日志文件和进程记账等。第12章:图形界面。本章主要介绍了X Window以及它的配置,并且介绍了在KDE环境下汉化的基本思想,用实例来讲解了Linux汉化的基础知识。第13章:网络的...

    《Linux从入门到精通》

    4.4 初试化交换空间 4.5 仅对FTP安装... 4.6 仅对SMB安装... 4.7 仅对硬盘安装... 4.8 分区格式化 4.9 选择和安装包 4.9.1 选择部件 4.9.2 选择单独的包 4.9.2.1 取得关于包的信息 4.9.3 包的相关性 4.9.4...

    RED HAT LINUX 6大全

    本书全面系统地介绍了Red Hat Linux 6。全书共分为五个部分,包括35章和四个附录。第一部分为Red Hat Linux的介绍和安装;第二部分为服务配置;第三部分为系统管理;第四部分为Linux编程;第五部分为附录。本书内容...

    linux从入门到精通.chm

    4.4 初试化交换空间 4.5 仅对FTP安装... 4.6 仅对SMB安装... 4.7 仅对硬盘安装... 4.8 分区格式化 4.9 选择和安装包 4.9.1 选择部件 4.9.2 选择单独的包 4.9.2.1 取得关于包的信息 4.9.3 包的相关性 4.9.4...

    2005详细介绍Linux从入门到精通

    4.4 初试化交换空间 4.5 仅对FTP安装... 4.6 仅对SMB安装... 4.7 仅对硬盘安装... 4.8 分区格式化 4.9 选择和安装包 4.9.1 选择部件 4.9.2 选择单独的包 4.9.2.1 取得关于包的信息 4.9.3 包的相关性 4.9.4...

    Linux操作系统基础教程

    Linux 操作系统基础教程 清华大学信息学院计算机系 目 录 前言..........................................................................................................................................

    Linux指令一周通 (技术图书大系).azw3

    1.73 touch指令:更改文件/目录时间 1.74 umask指令:指定在建立文件时权限掩码 1.75 umount指令:卸除文件系统 1.76 unarj指令:解压缩文件 1.77 uncompress指令:解压缩.z文件 1.78 unzip指令:解压缩.zip文件 ...

    入门学习Linux常用必会60个命令实例详解doc/txt

    chsh命令的作用是更改使用者shell设定,它的使用权限是所有使用者。 2.格式 chsh [ -s ] [ -list] [ --help ] [ -v ] [ username ] 3.主要参数 -l:显示系统所有Shell类型。 -v:显示Shell版本号。 4....

    详解Linux服务器集群.docx

    整个服务器集群的结构对客户是透明的,而且无需修改客户端和服务器端的程序。 详解Linux服务器集群全文共34页,当前为第1页。 图:集群的体系结构 为此,在设计时需要考虑系统的透明性、可伸缩性、高可用性和易管理...

    飞飞影视导航系统 v4.1.190209.zip

    4:多终端跨平台(windows、Linux均可安装/PC、手机、平板同步观看) 5:建站资源丰富(SEO优化模板免费下载/实用插件工具免费下载/完善的教程学习) 6:网站管理轻松(支持定时采集/定时生成/做最懒最赚钱的站长) ...

    Foxpro 开发答疑160问

    69. 如何进行DOS/Windows文本文件与UNIX/Linux文本文件的相互转换 257 70. 如何遍历磁盘中的所有目录及文件 258 71. 如何使用低级函数读写文件 263 72. 如何为Visual FoxPro应用程序增加文件压缩功能 271 73. ...

    超级有影响力霸气的Java面试题大全文档

    派生类可以从它的基类那里继承方法和实例变量,并且类可以修改或增加新的方法使之更适合特殊的需要。 3.封装:  封装是把过程和数据包围起来,对数据的访问只能通过已定义的界面。面向对象计算始于这个基本概念,...

    java 面试题 总结

    派生类可以从它的基类那里继承方法和实例变量,并且类可以修改或增加新的方法使之更适合特殊的需要。 3.封装: 封装是把过程和数据包围起来,对数据的访问只能通过已定义的界面。面向对象计算始于这个基本概念,即...

    JAVA上百实例源码以及开源项目

    原理是初始化颜色选择按钮,然后为颜色选择按钮增加事件处理事件,最后实例化颜色选择器。 Java二进制IO类与文件复制操作实例 16个目标文件 内容索引:Java源码,初学实例,二进制,文件复制  Java二进制IO类与文件...

    JAVA上百实例源码以及开源项目源代码

    发送消息,同时对文本进行少量修改,发送end-of-messages消息,最后关闭连接。 Tcp服务端与客户端的JAVA实例源代码 2个目标文件 摘要:Java源码,文件操作,TCP,服务器 Tcp服务端与客户端的JAVA实例源代码,一个简单的...

    java开源包1

    容易维护扩展(不需要修改主类就可以添加新的API支持) 注入型解释器(依据不同的返回格式注入相应的解释器) 集中管理请求参数与参数映射 以运行时异常的方式来管理错误的响应 使用泛型来做强类型编程 多协议扩展...

    java开源包11

    容易维护扩展(不需要修改主类就可以添加新的API支持) 注入型解释器(依据不同的返回格式注入相应的解释器) 集中管理请求参数与参数映射 以运行时异常的方式来管理错误的响应 使用泛型来做强类型编程 多协议扩展...

Global site tag (gtag.js) - Google Analytics