SWAP交换分区的建立及删除

建立SWAP交换分区
[root@localhost ~]# swapon -s
[root@localhost ~]# fdisk /dev/vdb ##划分分区标识符为82
[root@localhost ~]# mkswap /dev/vdb1 ##准备好将分区用作交换区
[root@localhost ~]# blkid ##确定UUID
[root@localhost ~]# swapon -a /dev/vdb1 ##激活新交换区
[root@localhost ~]# swapon -s ##显示当前交换区的状态
设置开机挂载
[root@localhost ~]# vim /etc/fstab

/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vdb1    swap swap defaults        0 0

改变磁盘优先

改变磁盘优先级两种方法
[root@localhost ~]# swapoff /dev/vdb2 ##停用特定交换区
[root@localhost ~]# swapon -a /dev/vdb2 -p 1
[root@localhost ~]# swapon -s
第二种:
[root@localhost ~]# swapoff /dev/vdb{1,2}
[root@localhost ~]# swapon -s
[root@localhost ~]# vim /etc/fstab

[root@localhost ~]# cat /etc/fstab

c/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vdb1    swap swap defaults        0 0
/dev/vdb2    swap swap defaults,pri=2  0 0
                                       不检测  不备份

[root@localhost ~]# swapon -a
[root@localhost ~]# swapon -s
删除分区
[root@localhost ~]# swapon -s
[root@localhost ~]# swapoff /dev/vdb{1,2}
将/etc/fstab里边之前加的去掉,再删除分区(去掉开机挂载)
[root@localhost ~]# fdisk /dev/vdb
[root@localhost ~]# partprobe
[root@localhost ~]# cat /proc/partitions

具体代码块如下:

[root@localhost ~]# swapon -s    ##显示当前交换区的状态
[root@localhost ~]# fdisk /dev/vdb    分区划分
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
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-20971519, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +4G
Partition 1 of type Linux and of size 4 GiB is set

Command (m for help): t   ##修改分区类型id
Selected partition 1
Hex code (type L to list all codes): L    列出分区类型id

 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): wq
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe  ##同步分区表
[root@localhost ~]# fdisk -l

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x9d727787

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     8390655     4194304   82  Linux swap / Solaris
[root@localhost ~]# fdisk /dev/vdb
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
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): 
Using default response p
Partition number (2-4, default 2): 
First sector (8390656-20971519, default 8390656): 
Using default value 8390656
Last sector, +sectors or +size{K,M,G} (8390656-20971519, default 20971519): +1G
Partition 2 of type Linux and of size 1 GiB is set

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

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe 

[root@localhost ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x9d727787

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     8390655     4194304   82  Linux swap / Solaris
/dev/vdb2         8390656    10487807     1048576   82  Linux swap / Solaris



[root@localhost ~]# swapon -s    ##显示当前交换区的状态
[root@localhost ~]# mkswap /dev/vdb1  ##将分区/dev/vdb1用作交换区
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=3f5107c8-44c7-4c80-b489-9bbb0fc70ce7
[root@localhost ~]# blkid   ##确定UUID
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" 
/dev/vdb1: UUID="3f5107c8-44c7-4c80-b489-9bbb0fc70ce7" TYPE="swap" 

[root@localhost ~]# swapon -a /dev/vdb1   ##激活新交换区
[root@localhost ~]# swapon -s         ##显示当前交换区的状态
Filename                Type        Size    Used    Priority
/dev/vdb1                               partition   4194300 0   -1
[root@localhost ~]# mkswap /dev/vdb2   ##格式化/dev/vdb2
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=c6afb493-8857-4940-92c3-ab4b0e23679d
[root@localhost ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" 
/dev/vdb1: UUID="3f5107c8-44c7-4c80-b489-9bbb0fc70ce7" TYPE="swap" 
/dev/vdb2: UUID="c6afb493-8857-4940-92c3-ab4b0e23679d" TYPE="swap" 
[root@localhost ~]# swapon -a /dev/vdb2     ##激活新交换区
[root@localhost ~]# swapon -s              ##显示当前交换区的状态
Filename                Type        Size    Used    Priority
/dev/vdb1                               partition   4194300 0   -1
/dev/vdb2                               partition   1048572 0   -2

[root@localhost ~]# swapoff /dev/vdb2     ##停用特定交换区
[root@localhost ~]# swapon -a /dev/vdb2 -p 1    ##改变特定交换区的优先级
[root@localhost ~]# swapon -s
Filename                Type        Size    Used    Priority
/dev/vdb1                               partition   4194300 0   -1
/dev/vdb2                               partition   1048572 0   1
[root@localhost ~]# swapoff /dev/vdb{1,2}   ##停用特定交换区
[root@localhost ~]# swapon -s
设置开机自动挂载:
[root@localhost ~]# vim /etc/fstab 
[root@localhost ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vdb1    swap swap defaults        0 0
/dev/vdb2    swap swap defaults,pri=2  0 0
                                       不检测  不备份

[root@localhost ~]# swapon -a        ##激活新交换区
[root@localhost ~]# swapon -s        ##显示当前交换区的状态
Filename                Type        Size    Used    Priority
/dev/vdb1                               partition   4194300 0   -1
/dev/vdb2                               partition   1048572 0   2
删除swap交换区
[root@localhost ~]# swapon -s
Filename                Type        Size    Used    Priority
/dev/vdb1                               partition   4194300 0   -1
/dev/vdb2                               partition   1048572 0   2
[root@localhost ~]# swapoff /dev/vdb{1,2}       ##停用特定交换区 
[root@localhost ~]# vim /etc/fstab   ##将之前写的清空
[root@localhost ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1

[root@localhost ~]# fdisk /dev/vdb
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): d     ##删除
Partition number (1,2, default 2): 1
Partition 1 is deleted

Command (m for help): d 
Selected partition 2
Partition 2 is deleted

Command (m for help): 2
2: unknown command
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): d 
No partition is defined yet!

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe    ##同步分区表
[root@localhost ~]# cat /proc/partitions 
major minor  #blocks  name

 253        0   10485760 vda
 253        1   10484142 vda1
 253       16   10485760 vdb

你可能感兴趣的:(linux)