交換分區swap:
當cpu使用的內存不足時,就會暫時占用硬盤一部分空間來存儲內存信息,這部分空間就是交換分區。
創建新的交換分區 swap:
1 新建分區,通過設置Id,建立swap分區
2 格式化swap分區 mkswap 設備名稱
3 啟動swap: swapon 設備名稱
4 查看swap:free命令
mkswap命令:通過設備或文件,設置一個交換分區swap格式
mkswap [options] device [size]
參數:
-c:創建交換分區前,檢測是否有壞塊
-f,--force:強制創建
-L,--label 'label':根據指定label創建
-U,--uuid UUID: 根據uuid創建
free命令:查看內存memory和交換分區swap的使用狀態
free [options]
-m: 以MB為單位
-g: 以GB為單位
-h: 以易讀的方式顯示容量
swapon, swapoff 命令:
啟用和禁用設備和文件作為交換分區
(enable/disable devices and files for paging and swapping)
swapon:啟用交換分區
swapon [option] [DEVICE]
-a: 激活所有交換分區
-p PRIORITY: 設定其優先級;優先使用
-v:顯示詳細信息
swapoff:禁用交換分區
swapoff [option] [DEVICE]
-a: 禁用所有交換分區
-v:顯示詳細信息
示例:
#1 創建新分區swap
[root@localhost ~]# 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 9
First sector (107507712-251658239, default 107507712):
Using default value 107507712
Last sector, +sectors or +size{K,M,G} (107507712-251658239, default 251658239): +512M
Partition 9 of type Linux and of size 512 MiB is set
Command (m for help): p
Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 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: 0x000c2314
Device Boot Start End Blocks Id System
....
/dev/sda6 82337792 86532095 2097152 82 Linux swap / Solaris
/dev/sda7 61853696 61855743 1024 83 Linux
/dev/sda8 86534144 107505663 10485760 83 Linux
/dev/sda9 107507712 108556287 524288 83 Linux
Partition table entries are not in disk order
Command (m for help): t
Partition number (1-9, default 9): 9
Hex code (type L to list all codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
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/sda: 128.8 GB, 128849018880 bytes, 251658240 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: 0x000c2314
Device Boot Start End Blocks Id System
......
/dev/sda6 82337792 86532095 2097152 82 Linux swap / Solaris
/dev/sda7 61853696 61855743 1024 83 Linux
/dev/sda8 86534144 107505663 10485760 83 Linux
/dev/sda9 107507712 108556287 524288 82 Linux swap / Solaris
Partition table entries are not in disk order
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.
#2 格式化swap分區
[root@localhost ~]# mkswap /dev/sda9
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=7362fb3d-4bda-41d4-bef5-ba238de58aa2
# 查看內存信息
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 979M 127M 704M 6.7M 147M 703M
Swap: 2.0G 0B 2.0G
# 3 加載swap分區
[root@localhost ~]# swapon /dev/sda9
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 979M 128M 704M 6.7M 147M 702M
Swap: 2.5G 0B 2.5G
#停用指定swap分區
[root@localhost ~]# swapoff -v /dev/sda9
swapoff /dev/sda9
#啟用指定swap分區
[root@localhost ~]# swapon -v /dev/sda9
swapon /dev/sda9
swapon: /dev/sda9: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/sda9: pagesize=4096, swapsize=536870912, devsize=536870912