创建swap

增加一个swap分区或文件

增加分区的方式 fdisk /dev/sdb t 82

增加文件的方式 dd if=/dev/zero of=/swapfile bs=1M count=2048 ----增加一个2G的文件 /swapfile

创建分区 mkswap /dev/sdb1 | /swapfile

vim /etc/fstab

磁盘增加分区的方式使用
UUID=XXXX swap swap pri=10(这是优先级 数字大 优先级高),defaults(可以用默认) 0(0不备份备份) 0(开机检查)

使用文件的方式 使用文件名
/swapfile swap swap pri=10(这是优先级 数字大 优先级高),defaults(可以用默认) 0(0不备份备份) 0(开机检查)

swapon -a 挂载命令
swapon -s 查看命令
swapoff /swapfile 关闭

[root@bogon ~]$swapon -s

Filename Type Size Used Priority
/swapfile file 2097148 0 110
/dev/sdb1 partition 4194300 0 10
/dev/sda5 partition 2097148 0 -2
[root@bogon ~]$vim /etc/fstab

[root@bogon ~]$swapoff /swapfile

[root@bogon ~]$swapon -s
Filename Type Size Used Priority
/dev/sdb1 partition 4194300 0 10
/dev/sda5 partition 2097148 0 -2
[root@bogon ~]$swapon -a
[root@bogon ~]$swapon -s
Filename Type Size Used Priority
/swapfile file 2097148 0 -3
/dev/sdb1 partition 4194300 0 10
/dev/sda5 partition 2097148 0 -2

#
9 UUID=434be986-3e4c-4efa-a27d-0c33fb24197b / xfs defaults 0 0
10 UUID=ab0df1bd-f6da-44e2-94a6-d26dc1311663 /boot xfs defaults 0 0
11 UUID=3a8488e1-cd88-43cb-960e-34b45656287e /data xfs defaults 0 0
12 UUID=3117e237-4782-47b6-ad7c-0f1cda84b9c3 swap swap defaults 0 0
13 UUID=3cb31b56-f539-4fb0-bb5d-ada65c8deb55 /mnt/sda7 ext4 defaults 0 0
14 UUID=8762268c-fac6-4a57-ab1b-8a1bf7a92c65 /mnt/sda6 xfs defaults 0 0
15 UUID=0c8df860-08b9-42eb-849b-f8a553b14083 swap swap pri=10 0 0
16 /swapfile swap swap pri=-5 0 0

17 /boot /mnt/boot/ none bind 0 0

#
9 UUID=434be986-3e4c-4efa-a27d-0c33fb24197b / xfs defaults 0 0
10 UUID=ab0df1bd-f6da-44e2-94a6-d26dc1311663 /boot xfs defaults 0 0
11 UUID=3a8488e1-cd88-43cb-960e-34b45656287e /data xfs defaults 0 0
12 UUID=3117e237-4782-47b6-ad7c-0f1cda84b9c3 swap swap defaults 0 0
13 UUID=3cb31b56-f539-4fb0-bb5d-ada65c8deb55 /mnt/sda7 ext4 defaults 0 0
14 UUID=8762268c-fac6-4a57-ab1b-8a1bf7a92c65 /mnt/sda6 xfs defaults 0 0
15 UUID=0c8df860-08b9-42eb-849b-f8a553b14083 swap swap pri=10 0 0
16 /swapfile swap swap pri=-5 0 0
17 /boot /mnt/boot/ none bind 0 0 ---文件系统的挂载方式 文件夹的挂载

[root@bogon /]$fdisk /dev/sdb
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xd2460de1.

Command (m for help): p

Disk /dev/sdb: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xd2460de1

Device Boot Start End Blocks Id System

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-419430399, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399): +4G
Partition 1 of type Linux and of size 4 GiB is set

Command (m for help): p

Disk /dev/sdb: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xd2460de1

Device Boot Start End Blocks Id System
/dev/sdb1 2048 8390655 4194304 83 Linux

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): p

Disk /dev/sdb: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xd2460de1

Device Boot Start End Blocks Id System
/dev/sdb1 2048 8390655 4194304 82 Linux swap / Solaris

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@bogon /]$
[root@bogon /]$mkswap /dev/sdb1
mkswap: /dev/sdb1: warning: wiping old ext3 signature.
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=0c8df860-08b9-42eb-849b-f8a553b14083
[root@bogon /]$blkid
/dev/sr0: UUID="2018-06-29-11-20-20-00" LABEL="CentOS_6.10_Final" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="ab0df1bd-f6da-44e2-94a6-d26dc1311663" TYPE="xfs"
/dev/sda2: UUID="434be986-3e4c-4efa-a27d-0c33fb24197b" TYPE="xfs"
/dev/sda3: UUID="3a8488e1-cd88-43cb-960e-34b45656287e" TYPE="xfs"
/dev/sda5: UUID="3117e237-4782-47b6-ad7c-0f1cda84b9c3" TYPE="swap"
/dev/sda6: UUID="8762268c-fac6-4a57-ab1b-8a1bf7a92c65" TYPE="xfs"
/dev/sda7: UUID="3cb31b56-f539-4fb0-bb5d-ada65c8deb55" TYPE="ext4"
/dev/sr1: UUID="2018-11-26-14-22-58-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sdb1: UUID="0c8df860-08b9-42eb-849b-f8a553b14083" TYPE="swap"
[root@bogon /]$blkid /dev/sdb1
/dev/sdb1: UUID="0c8df860-08b9-42eb-849b-f8a553b14083" TYPE="swap"
[root@bogon /]$fdisk -l /dev/sdb1

Disk /dev/sdb1: 4294 MB, 4294967296 bytes, 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@bogon /]$fdisk -l /dev/sdb

Disk /dev/sdb: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xd2460de1

Device Boot Start End Blocks Id System
/dev/sdb1 2048 8390655 4194304 82 Linux swap / Solaris
[root@bogon /]$echo 2048512|bc
1048576
[root@bogon /]$hexdump -C -n 1049576 /dev/sdb
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|

000001b0 00 00 00 00 00 00 00 00 e1 0d 46 d2 00 00 00 20 |..........F.... |
000001c0 21 00 82 4b 81 0a 00 08 00 00 00 00 80 00 00 00 |!..K............|
000001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|

000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
00000200 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|

001003e0 00 00 00 00 00 00 00 00 |........|
001003e8
[root@bogon /]$
[root@bogon /]$vim /etc/fstab
[root@bogon /]$free -h
total used free shared buff/cache available
Mem: 1.8G 410M 882M 12M 527M 1.2G
Swap: 2.0G 0B 2.0G
[root@bogon /]$cat /proc/swap
cat: /proc/swap: No such file or directory
[root@bogon /]$cat /proc/swaps
Filename Type Size Used Priority
/dev/sda5 partition 2097148 0 -2
[root@bogon /]$swapon -a
[root@bogon /]$cat /proc/swaps
Filename Type Size Used Priority
/dev/sda5 partition 2097148 0 -2
/dev/sdb1 partition 4194300 0 -3
[root@bogon /]$free
total used free shared buff/cache available
Mem: 1863252 423648 899916 12948 539688 1217844
Swap: 6291448 0 6291448
[root@bogon /]$swap -s
bash: swap: command not found...
Similar command is: 'swapon'
[root@bogon /]$swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 2097148 0 -2
/dev/sdb1 partition 4194300 0 -3
[root@bogon /]$dd if=/dev/zero of=/dev/null bs=2G count=1
0+1 records in
0+1 records out
2147479552 bytes (2.1 GB) copied, 27.5861 s, 77.8 MB/s
[root@bogon /]$swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 2097148 267016 -2
/dev/sdb1 partition 4194300 0 -3
[root@bogon /]$free -h
total used free shared buff/cache available
Mem: 1.8G 117M 1.6G 5.1M 101M 1.5G
Swap: 6.0G 260M 5.7G
[root@bogon /]$dd if=/dev/blkid /dev/sdb1^C
[root@bogon /]$vim /etc/fstab
[root@bogon /]$swapon -a
[root@bogon /]$dd if=/dev/zero of=/dev/null bs=4G count=1
0+1 records in
0+1 records out
2147479552 bytes (2.1 GB) copied, 4.26626 s, 503 MB/s
[root@bogon /]$free -h
total used free shared buff/cache available
Mem: 1.8G 120M 1.5G 432K 138M 1.5G
Swap: 6.0G 264M 5.7G
[root@bogon /]$swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 2097148 271072 -2
/dev/sdb1 partition 4194300 0 -3
[root@bogon /]$
[root@bogon /]$swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 2097148 271072 -2
/dev/sdb1 partition 4194300 0 -3
[root@bogon /]$swapoff /dev/sdb1
[root@bogon /]$swapon -a
[root@bogon /]$swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 2097148 268448 -2
/dev/sdb1 partition 4194300 0 10
[root@bogon /]$free
total used free shared buff/cache available
Mem: 1863252 135576 1575968 656 151708 1555832
Swap: 6291448 268448 6023000
[root@bogon /]$dd if=/dev/zero of=/dev/null bs=3G count=1
0+1 records in
0+1 records out
2147479552 bytes (2.1 GB) copied, 4.99203 s, 430 MB/s
[root@bogon /]$free
total used free shared buff/cache available
Mem: 1863252 109628 1664756 640 88868 1613224
Swap: 6291448 277000 6014448
[root@bogon /]$swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 2097148 268256 -2
/dev/sdb1 partition 4194300 8488 10
[root@bogon /]$dd if=/dev/zero of=/dev/null bs=10G count=1
dd: memory exhausted by input buffer of size 10737418240 bytes (10 GiB)
[root@bogon /]$dd if=/dev/zero of=/dev/null bs=5G count=1
0+1 records in
0+1 records out
2147479552 bytes (2.1 GB) copied, 4.43574 s, 484 MB/s
[root@bogon /]$dd if=/dev/dd if=/dev/zero of=/dev/null bs=3G count=1^C
[root@bogon /]$free
total used free shared buff/cache available
Mem: 1863252 123468 1651208 620 88576 1599424
Swap: 6291448 268948 6022500
[root@bogon /]$swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 2097148 263492 -2
/dev/sdb1 partition 4194300 5456 10
[root@bogon /]$dd if=/dev/zero of=/swapfile bs=1M count=2048
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 9.64536 s, 223 MB/s
[root@bogon /]$
[root@bogon /]$ls /swapfile -h
/swapfile
[root@bogon /]$ls /swapfile -H
/swapfile
[root@bogon /]$ll /swapfile -H
-rw-r--r-- 1 root root 2147483648 Mar 7 22:44 /swapfile
[root@bogon /]$ll /swapfile -h
-rw-r--r-- 1 root root 2.0G Mar 7 22:44 /swapfile
[root@bogon /]$mkswap /swapfile
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=3ba030a9-adee-4dc7-8046-8f452ebf5e0e
[root@bogon /]$vim /etc/fstab
[root@bogon /]$
[root@bogon /]$
[root@bogon /]$
[root@bogon /]$swapoff /swapfile
swapoff: /swapfile: swapoff failed: Invalid argument
[root@bogon /]$swapon /swapfile
swapon: /swapfile: insecure permissions 0644, 0600 suggested.
[root@bogon /]$chmod 600 /swapfile
[root@bogon /]$free -h
total used free shared buff/cache available
Mem: 1.8G 134M 65M 1.1M 1.6G 1.4G
Swap: 8.0G 258M 7.7G
[root@bogon /]$swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 2097148 262864 -2
/dev/sdb1 partition 4194300 1988 10
/swapfile file 2097148 0 -3

上一篇:wxWidgets 在 Windows 下开发环境配置


下一篇:搭建本地和网络yum源、源码编译安装软件及磁盘分区管理