Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Swap partition management (partition or file) for CentOS7

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Swap partition management (partition or file) for CentOS7

1. Swap can be created in separate partitions when installing the operating system, or the remaining hard disks can be created after the operating system is installed, and a file can be created to be used as a swap partition.

2. It is recommended to create a new partition for swap use. If you want to use a file as a swap partition, you'd better put the file on the solid state hard disk. If you use the file as swap, you can make it smaller and larger at will, and it is easy to move, but the performance is not as good as the partition.

Create a partition to partition the swap

Use free-h to view the current swap

The current system has 4G swap space

[root@centos7] # free-h

Total used free shared buff/cache available

Mem: 1.9G 180M 1.2G 19M 545M 1.6G

Swap: 4.0G 12M 4.0G

Use swapon-s to display the swap of the current system

[root@centos7] # swapon-s

Filename Type Size Used Priority

/ dev/dm-0 partition 419430012676-2

Use lsblk to view the hard disk of the current system

As you can see, sda has a total of 200 gigabytes, but now it is only divided into more than 150g, and there is still available space.

[root@centos7 ~] # lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT

Sda 8:0 0 200G 0 disk

├─ sda1 8:1 0 1G 0 part / boot

├─ sda2 8:2 0 100G 0 part /

├─ sda3 8:3 0 50G 0 part / data

├─ sda4 8:4 0 1K 0 part

└─ sda5 8:5 0 4G 0 part

└─ centos-swap

253:0 0 4G 0 lvm [SWAP]

Sdb 8:16 0 20G 0 disk

└─ sdb2 8:18 0 2G 0 part

Third, use fdisk to create partitions on / dev/sda hard disk

[root@centos7 ~] # fdisk / dev/sda

Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.

Be careful before using the write command.

Command (m for help): n

All primary partitions are in use

Adding logical partition 6

First sector (35072896-419430399, default 325072896):

Using default value 325072896

Last sector, + sectors or + size {KMagne Mpeng} (325072896-419430399, default 419430399): + 2G

Partition 6 of type Linux and of size 2 GiB is set

Command (m for help): P

Disk / dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I size (minimum/optimal): 512 bytes / 512 bytes

Disk label type: dos

Disk identifier: 0x000f14f7

Device Boot Start End Blocks Id System

/ dev/sda1 * 2048 2099199 1048576 83 Linux

/ dev/sda2 2099200 211814399 104857600 83 Linux

/ dev/sda3 211814400 316671999 52428800 83 Linux

/ dev/sda4 316672000 419430399 51379200 5 Extended

/ dev/sda5 316674048 325070847 4198400 8e Linux LVM

/ dev/sda6 325072896 329267199 2097152 83 Linux

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 at

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

Syncing disks.

4. Execute partprobe twice (CentOS6 execute part / dev/sda-a)

[root@centos7 ~] # partprobe

Warning: Unable to open / dev/sr0 read-write (Read-only file system). / dev/sr0 has been opened read-only.

Error: Invalid partition table-recursive partition on / dev/sr0.

Warning: Unable to open / dev/sr1 read-write (Read-only file system). / dev/sr1 has been opened read-only.

[root@centos7 ~] # partprobe

Warning: Unable to open / dev/sr0 read-write (Read-only file system). / dev/sr0 has been opened read-only.

Error: Invalid partition table-recursive partition on / dev/sr0.

Warning: Unable to open / dev/sr1 read-write (Read-only file system). / dev/sr1 has been opened read-only.

5. Create a file system

[root@centos7 ~] # ls / dev/sda6

/ dev/sda6

[root@centos7 ~] # mkswap / dev/sda6

Setting up swapspace version 1, size = 2097148 KiB

No label, UUID=b98996a2-a548-4266-b636-b2cb75cd0626

Write / etc/fstab and mount-a test

[root@centos7 ~] # blkid / dev/sda6

Dev/sda6: UUID= "b98996a2-a548-4266-b636-b2cb75cd0626" TYPE= "swap"

[root@centos7 ~] # vim / etc/fstab

[root@centos7] # tail-1 / etc/fstab

UUID=b98996a2-a548-4266-b636-b2cb75cd0626 swap swap defaults 0 0

Mounted device name mount point file system type mount option dump frequency system self-test option

[root@centos7] # mount-a

7. Swapon-a

At this point, execute free-hre swap partition before it is shown, execute swapon-a, enable all swap

[root@centos7] # free-h

Total used free shared buff/cache available

Mem: 1.9G 176M 1.2G 19M 547M 1.6G

Swap: 4.0G 12M 4.0G

Execute swapon-a

[root@centos7] # swapon-a

[root@centos7] # free-h

Total used free shared buff/cache available

Mem: 1.9G 178M 1.2G 19M 547M 1.6G

Swap: 6.0G 12M 6.0G

Uninstall this swap

Use swapoff to close / dev/sda6 the swap partition

[root@centos7 ~] # swapoff / dev/sda6

Use sed to delete the last line added by fstab

[root@centos7 ~] # sed-ri'$d' / etc/fstab

Mount-a

[root@centos7] # mount-a

At this time, swap has become the original 4G

Create a file to act as a swap partition

Use the dd command to create a file of 100m, when swap uses the

[root@centos7 data] # dd if=/dev/zero of=/data/swapfile bs=1M count=100

100 minutes 0 records in

100 minutes 0 records out

104857600 bytes (105 MB) copied, 2.74665 s, 38.2 MB/s

The permission of swap file is changed to 600. it is safe.

[root@centos7 data] # chmod 600 swapfile

Second, create a file system for the file

[root@centos7 data] # mkswap swapfile

Setting up swapspace version 1, size = 102396 KiB

No label, UUID=a16538e7-685e-4141-aaeb-e1b7896cffdf

3. Write / etc/fstab and mount (note here that the mount device cannot write UUID, only the file name)

[root@centos7 data] # tail-1 / etc/fstab

/ data/swapfile swap swap defaults 0 0

[root@centos7 data] # mount-a

4. Execute swapon-a to open all swap

[root@centos7 data] # swapon-a

[root@centos7 data] # swapon-s

Filename Type Size Used Priority

/ dev/dm-0 partition 4194300 12672-2

/ data/swapfile file 102396 0-3

[root@centos7 data] # free-h

Total used free shared buff/cache available

Mem: 1.9G 176M 1.1G 19M 650M 1.6G

Swap: 4.1G 12M 4.1G

5. To increase swap, you need to unmount it first.

You must uninstall it first, or you will make a mistake.

[root@centos7 data] # dd if=/dev/zero of=/data/swapfile bs=1M count=200

Dd: failed to open'/ data/swapfile': Text file busy

Unloading

[root@centos7 data] # swapoff / data/swapfile

[root@centos7 data] # dd if=/dev/zero of=/data/swapfile bs=1M count=200

20000 records in

20000 records out

209715200 bytes (210MB) copied, 3.23377 s, 64.9 MB/s

Create a file system

[root@centos7 data] # mkswap swapfile

Setting up swapspace version 1, size = 204796 KiB

No label, UUID=79d0f287-c998-42d3-aabc-b3b2d60b9cb2

Execute swapon-a to enable all swap

[root@centos7 data] # swapon-a

[root@centos7 data] # free-h

Total used free shared buff/cache available

Mem: 1.9G 176M 1.0G 19M 752M 1.6G

Swap: 4.2G 12M 4.2G

VI. Unloading

Unmount

[root@centos7 data] # swapoff / data/swapfile

[root@centos7 data] # sed-ri'$d' / etc/fstab

[root@centos7 data] # mount-a

[root@centos7 data] # rm-f swapfile

[root@centos7 data] # free-h

Total used free shared buff/cache available

Mem: 1.9G 176M 1.2G 19M 547M 1.6G

Swap: 4.0G 12M 4.0G

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report