Parted or fdisk to create Swap space

Source: Internet
Author: User

Swap space in Linux is used when physical memory RAM is full. If the system requires more memory resources and the physical memory is full, the inactive pages in the memory will be moved to the Swap space.

Use parted or fdisk to create Swap space. Parted is easier to use than fdisk. Therefore, only parted is described here. To use parted to create Swap space:

Run the command parted/dev/hdb as the root user at the shell prompt. Here/dev/hdb is the name of your device with a free space hard drive.

At the (parted) prompt, type print to view the number of existing partitions and free space. The start and end values are in MB. Determine the number of free space on the hard drive and the amount of space you want to allocate to the newly created Swap space.

At the (parted) prompt, type mkpartfs part-type linux-swap start end. Here, part-type is one of primary, extended, and logical, and start is the starting point of Swap partition, end is the termination point of the partition.

Warning changes occur immediately. Exercise caution when typing.

Type quit to exit parted.

Now you can create a Swap space and use the mkswap command to set the Swap space. In the shell prompt, enter the following command as the root user:
Mkswap/dev/hdb2

To enable swap partition immediately, type the following command:
Swapon/dev/hdb2

To enable it during boot, edit the/etc/fstab file to include the following lines:
/Dev/hdb2 swap defaults 0 0

The new Swap space is Enabled Next time the system boots.

After Swap space is added and enabled, check the output of cat/proc/swaps or the free command to ensure that the Swap partition is enabled.

To add a swap file: determine the size of the new swap file. Multiply the size by 1024 to determine the size of the block. For example, the block size of a 64 MB swap file is 65536.

In the shell prompt, enter the following command as the root user, where count is equal to the expected block size:

Dd if =/dev/zero of =/swapfile bs = 1024 count = 65536

Run the following command to set the file exchange:
Mkswap/swapfile

To enable the swap file immediately instead of automatically enabling it during boot, run the following command:
Swapon/swapfile

To enable it during boot, edit the/etc/fstab file to include the following lines:
/Swapfile swap defaults 0 0

The new swap file is Enabled Next time the system boots.

After Swap space is added and enabled, check the output of cat/proc/swaps or free command to ensure that the Swap partition has been started.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.