Adding swap space on linux system

2.2. Adding Swap Space

Sometimes it is necessary to add more swap space after installation. Forexample, you may upgrade the amount of RAM in your system from 64 MB to128 MB, but there is only 128 MB of swap space. It might be advantageousto increase the amount of swap space to 256 MB if you performmemory-intense operations or run applications that require a largeamount of memory.

You have two options: add a swap partition or add a swap file. It isrecommended that you add a swap partition, but that can be difficult ifyou do not have any free space available.

To add a swap partition (assuming /dev/hdb2 is theswap partition you want to add):

  1. The hard drive can not be in use (partitions can not be mounted, and swap space can not be enabled). The partition table should not be modified while in use because the kernel may not properly recognize the changes. Data could be overwitten by writing to the wrong partition because the partition table and partitions mounted do not match. The easiest way to achieve this is to boot your system in rescue mode. Refer to Chapter 11 Basic System Recovery for instructions on booting into rescue mode. When prompted to mount the file system, select Skip.

    Alternately, if the drive does not contain any partitions in use, you can unmount them and turn off all the swap space on the hard drive with the swapoff command.

  2. Create the swap partition using parted:

    • At a shell prompt as root, type the command parted /dev/hdb, where /dev/hdb is the device name for the hard drive with free space.

    • At the (parted) prompt, type print to view the existing partitions and the amount of free space. The start and end values are in megabytes. Determine how much free space is on the hard drive and how much you want to allocate for a new swap partition.

    • At the (parted) prompt, typemkpartfs part-typelinux-swap startend, wherepart-type is one of primary,extended, or logical, start is thestarting point of the partition, andend is the end point of thepartition.

      WarningWarning
       

      Changes take place immediately; be careful when you type.

    • Exit parted by typing quit.

  3. Now that you have created the swap partition, use the command mkswap to setup the swap partition. At a shell prompt as root, type the following:

    mkswap /dev/hdb2
  4. To enable the swap partition immediately, type the following command:

    swapon /dev/hdb2
  5. To enable it at boot time, edit /etc/fstab to include:

    /dev/hdb2               swap                    swap    defaults        0 0

    The next time the system boots, it enables the new swap partition.

  6. After adding the new swap partition and enabling it, verify it is enabled by viewing the output of the command cat /proc/swaps or free.

To add a swap file:

  1. Determine the size of the new swap file in megabytes and multiple by 1024 to determine the block size. For example, the block size of a 64 MB swap file is 65536.

  2. At a shell prompt as root, type the following command with count being equal to the desired block size:

    dd if=/dev/zero of=/swapfile bs=1024 count=65536
  3. Setup the swap file with the command:

    mkswap /swapfile
  4. To enable the swap file immediately but not automatically at boot time:

    swapon /swapfile
  5. To enable it at boot time, edit /etc/fstab to include:

    /swapfile               swap                    swap    defaults        0 0

    The next time the system boots, it enables the new swap file.

  6. After adding the new swap file and enabling it, verify it is enabled by viewing the output of the command cat /proc/swaps or free.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值