您的位置:首页 > 运维架构 > Linux

Linux磁盘和文件系统管理(5)_创建交换分区

2015-09-15 19:18 806 查看
交换分区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


如有错误,敬请指正!
谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: