SWAP交换分区的建立及删除

来源:互联网 发布:人工智能 人类终结 编辑:程序博客网 时间:2024/05/19 02:19

建立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): nPartition type:   p   primary (0 primary, 0 extended, 4 free)   e   extendedSelect (default p): Using default response pPartition number (1-4, default 1): First sector (2048-20971519, default 2048): Using default value 2048Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +4GPartition 1 of type Linux and of size 4 GiB is setCommand (m for help): t   ##修改分区类型idSelected partition 1Hex 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 b11  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 auto1b  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): 82Changed type of partition 'Linux' to 'Linux swap / Solaris'Command (m for help): wqThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.[root@localhost ~]# partprobe  ##同步分区表[root@localhost ~]# fdisk -lDisk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x00013f3e   Device Boot      Start         End      Blocks   Id  System/dev/vda1   *        2048    20970332    10484142+  83  LinuxDisk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x9d727787   Device Boot      Start         End      Blocks   Id  System/dev/vdb1            2048     8390655     4194304   82  Linux swap / Solaris[root@localhost ~]# fdisk /dev/vdbWelcome 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): nPartition type:   p   primary (1 primary, 0 extended, 3 free)   e   extendedSelect (default p): Using default response pPartition number (2-4, default 2): First sector (8390656-20971519, default 8390656): Using default value 8390656Last sector, +sectors or +size{K,M,G} (8390656-20971519, default 20971519): +1GPartition 2 of type Linux and of size 1 GiB is setCommand (m for help): tPartition number (1,2, default 2): 2Hex code (type L to list all codes): 82Changed type of partition 'Linux' to 'Linux swap / Solaris'Command (m for help): wqThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.[root@localhost ~]# partprobe [root@localhost ~]# fdisk -lDisk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x00013f3e   Device Boot      Start         End      Blocks   Id  System/dev/vda1   *        2048    20970332    10484142+  83  LinuxDisk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk 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 KiBno 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/vdb2Setting up swapspace version 1, size = 1048572 KiBno 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 -sFilename                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 -sFilename                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/vdbWelcome 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): 1Partition 1 is deletedCommand (m for help): d Selected partition 2Partition 2 is deletedCommand (m for help): 22: unknown commandCommand 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): wqThe 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
0 0