One-day learning of Linux SWAP creation/expansion

Source: Internet
Author: User
Tags hex code
SWAP is called a SWAP zone, which is equivalent to the virtual memory in Windows. Its function is to increase the memory through SWAP when the memory in our system is insufficient. When we install the Linux system, the root and SWAP are the most basic partitions. If we set SW...

 

SWAP is called a SWAP zone, which is equivalent to the virtual memory in Windows. Its function is to increase the memory through SWAP when the memory in our system is insufficient. When we install the Linux system, the root and SWAP are the most basic partitions. If we split the SWAP into smaller ones, you need to increase the size of the SWAP. the content of today is the creation/expansion of SWAP. There are two ways to choose from.

 

1. use the remaining disk space

The premise is that your disk still has no space to be divided, or the partitioned partitions are not used. This method can be used at this time. The following describes how to implement this method.

 

Step 1: Partition

View disk information

 

[Root @ yufei ~] # Fdisk-l/dev/sdb

N rows are omitted here.

Device Boot Start End Blocks Id System

/Dev/sdb1 1 132 1060258 + 83 Linux

/Dev/sdb2 133 183 409657 + B W95 FAT32

/Dev/sdb3 184 2610 19494877 + 5 Extended

/Dev/sdb5 184 445 2104483 + 83 Linux

/Dev/sdb6 446 511 530113 + 82 Linux swap/Solaris

Sdb6 is the partition and format we mentioned earlier. Ignore it first. For the complete demonstration, we add new partitions.

 

[Root @ yufei ~] # Fdisk/dev/sdb

Content omitted here

Command (m for help): n

Command action

L logical (5 or over)

P primary partition (1-4)

L

First cylinder (512-2610, default 512 ):

Using default value 512

Last cylinder, + cylinders or + size {K, M, G} (512-2610, default 2610): + 256 M

 

Command (m for help): p

Omitted here

/Dev/sdb7 512 545 273073 + 83 Linux

Step 2: convert the partition type

Command (m for help): t

Partition number (1-7): 7

Hex code (type L to list codes): L

Omitted here

Hex code (type L to list codes): 82

Changed system type of partition 7 to 82 (Linux swap/Solaris)

 

Command (m for help): w

The partition table has been altered!

 

Calling ioctl () to re-read partition table.

 

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.

The kernel still uses the old table. The new table will be used

The next reboot or after you run partprobe (8) or kpartx (8)

Syncing disks.

Use [root @ yufei ~] # Partprobe/dev/sdb is also the same error.

 

The error message above indicates that the device is busy and the new partition table cannot be identified. why? See the solution below.

Do you still remember the content about automatic mounting in the previous article? By the way, the partitions on our sdb have been mounted to the system. Run the following command to view

 

[Root @ yufei ~] # Df

Filesystem 1K-blocks Used Available Use % Mounted on

/Dev/sda1 15118728 6980344 7370384 49%/

Tmpfs 255784 0 255784 0%/dev/shm

/Dev/sdb1 1033560 40980 939568 5%/mnt

/Dev/sdb2 409424 0 409424 0%/test

Unmount the two of them.

 

[Root @ yufei ~] # Umount/dev/sdb1

[Root @ yufei ~] # Umount/dev/sdb2

Come back

[Root @ yufei ~] # Partprobe/dev/sdb

At this time, no error message is displayed.

 

[Root @ yufei ~] # Fdisk-l/dev/sdb

Omitted here

Device Boot Start End Blocks Id System

/Dev/sdb1 1 132 1060258 + 83 Linux

/Dev/sdb2 133 183 409657 + B W95 FAT32

/Dev/sdb3 184 2610 19494877 + 5 Extended

/Dev/sdb5 184 445 2104483 + 83 Linux

/Dev/sdb6 446 511 530113 + 82 Linux swap/Solaris

/Dev/sdb7 512 545 273073 + 82 Linux swap/Solaris

Sdb7 has been split. Format

 

Step 3: format the SWAP partition

SWAP formatting is slightly different from the previous formatting, but the command is different. SWAP uses mkswap

 

[Root @ yufei ~] # Mkswap/dev/sdb7

Setting up swapspace version 1, size = 273068 KiB

No label, UUID = edca6b3a-047b-4e26-9b28-21493f92e2b4

Because the volume label is not set, there is no volume label.

 

Step 4: activate SWAP

Before activating the created SWAP, let's take a look at the SWAP size in the system.

 

[Root @ yufei ~] # Free

Total used free shared buffers cached

Mem: 511572 154664 356908 0 17408 76236

-/+ Buffers/cache: 61020 450552

Swap: 524280 0 524280

512 M is displayed.

 

[Root @ yufei ~] # Swapon/dev/sdb7

Activate SWAP, and then display the SWAP size, up to 256 M

[Root @ yufei ~] # Free

Total used free shared buffers cached

Mem: 511572 154928 356644 0 17444 76252

-/+ Buffers/cache: 61232 450340

Swap: 797344 0 797344

Step 5: write data to fstab

Add the following content at the end of/etc/fstab:

 

UUID = edca6b3a-047b-4e26-9b28-21493f92e2b4 swap defaults 0 0

Restart the system to check the SWAP size.

[Root @ yufei ~] # Free-m

Total used free shared buffers cached

Mem: 511572 152636 358936 0 15132 75900

-/+ Buffers/cache: 61604 449968

Swap: 797344 0 797344

2. use the remaining space of the partition

Since the remaining space of the partition is used, of course we need to find a partition with a large space to perform the following operations!

 

Step 1: Create a SWAP file

[Root @ yufei ~] # Df

Filesystem 1K-blocks Used Available Use % Mounted on

/Dev/sda1 15118728 6980460 7370268 49%/

Tmpfs 255784 0 255784 0%/dev/shm

/Dev/sdb1 1033560 40980 939568 5%/mnt

/Dev/sdb2 409424 0 409424 0%/test

Because the two below are sdb disks, we don't need to use/partition directly.

Create a M file named swapfile under the root directory

 

[Root @ yufei ~] # Dd if =/dev/zero of =/swapfile bs = 1 M count = 256

256 + 0 records in

256 + 0 records out

268435456 bytes (268 MB) copied, 9.43718 s, 28.4 MB/s

Step 2: format the SWAP file

[Root @ yufei ~] # Mkswap/swapfile

Mkswap:/swapfile: warning: don't erase bootbits sectors

On whole disk. Use-f to force.

Setting up swapspace version 1, size = 262140 KiB

No label, UUID = 11ca575a-e810-4970-889a-a3f6d9af7dfe

Step 3: activate SWAP

[Root @ yufei ~] # Free

Total used free shared buffers cached

Mem: 511572 419940 91632 0 15492 338472

-/+ Buffers/cache: 65976 445596

Swap: 797344 0 797344

 

[Root @ yufei ~] # Swapon/swapfile

[Root @ yufei ~] # Free

Total used free shared buffers cached

Mem: 511572 419948 91624 0 15492 338472

-/+ Buffers/cache: 65984 445588

Swap: 1059480 0 1059480

Step 4: write data to fstab

Add the following content

 

/Swapfile swap defaults 0 0

 

Restart to query the SWAP size

[Root @ yufei ~] # Free

Total used free shared buffers cached

Mem: 511572 151156 360416 0 15228 74056

-/+ Buffers/cache: 61872 449700

Swap: 1059480 0 1059480

In this way, we have demonstrated all the two methods. there is a problem here, that is, there are three swaps in the system. which SWP is the first to use by default? The Linux kernel automatically assigns a priority to SWAP. this priority starts from-1. each time a new SWAP with no user-specified priority is added, this priority is reduced by one. So-1 has the best priority, so it is used first.

When using VPS, the system may not allocate SWAP during installation. Therefore, you must create a SWAP by yourself, but for the partition and file SWAP, the partition SWAP efficiency is higher than that of the file. So sometimes we need to adjust their priority. Continue with the operation.

 

View swap priority

[Root @ yufei ~] # Swapon-s

Filename Type Size Used Priority

/Dev/sda2 partition 524280 0-1

/Dev/sdb7 partition 273064 0-2

/Swapfile file 262136 0-3

The greater the number, the higher the priority.

 

It is also easy to change their priority. you only need to add the pri parameter in fstab to OK. Note: The value following this pri cannot be negative! /Etc/fstab.

 

UUID = c70cd6a4-09cd-4aea-ba94-f3641df4e981 swap ULTS ults, pri = 1 0

UUID = edca6b3a-047b-4e26-9b28-21493f92e2b4 swap ULTS ults, pri = 2 0

/Swapfile swap defaults, pri = 3 0 0

To make the modification take effect, we need to disable SWAP before enabling SWAP.

 

[Root @ yufei ~] # Swapoff-

[Root @ yufei ~] # Swapon-

[Root @ yufei ~] # Swapon-s

Filename Type Size Used Priority

/Dev/sda2 partition 524280 0 1

/Dev/sdb7 partition 273064 0 2

/Swapfile file 262136 0 3

Of course, it is unreasonable to adjust the SWAP file to the highest priority. here is just a demonstration to illustrate the problem. Therefore, do not change the file SWAP to the highest value. If not necessary, use the system default.

From: Yufei blog

Related Article

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.