码迷,mamicode.com
首页 > 其他好文 > 详细

交换分区以及其它外设的挂载

时间:2016-08-27 00:43:14      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:使用硬盘分区做交换分区   使用文件做交换分区   

一、交换分区创建与挂载

交换分区是系统内存的补充,为什么出来交换分区这一概念了,那就还得先从计算机的发展说起,毕竟对于早期的计算机设备来说,内存是一个比较昂贵的耗材,所以在当时内存资源比较紧张,而应用程序对内存的需求又在不断膨胀的情况下,专家们提出了一种新的思路,那就是把高速的磁盘设备划分一部分出来当作内存来使用,当然对于计算机设备工作效率有一定了解的同学们都知道,硬盘的读写速度和内存根本不在一个层面上,所以说即使把高速的硬盘分区拿来当内存用,那也只不过是一个模拟操作,计算机只会把一些不是当前活动最为频繁的进程的数据,暂时存放到交换分区中,当该进程转换为当前进程时,计算机会及时的把交换分区上的数据及时调入内存继续工作。在高速磁盘上划分的这一区域我们就把它称之为交换分区。

当然我们要想提升这个交换分区的性能,最好是在安装操作系统的过程中就直接划分好,为什么这么说了,因为磁盘最外圈的磁道的读写速度是最快的,而磁盘分区工具默认划分分区时,都是从最外圈磁道开始的,所以说,如果你是在操作系统已经安装完之后再去创建交换分区,很显然你是无法再次划分到最快的那一部分区域来做交换分区了。


(一)使用硬盘分区做交换分区

在这里由于我的机器内存是比较大的,完全没有必须在拿硬盘来模拟内存了,所以我在安装操作系统的过程中就直接没有划分交换分区。所以为了给大家演示这一操作过程,只能用剩余硬盘空间划分一个分区来做交换分区了。


第一步:划分分区,调整分区类型为SWAP(82)

示例代码:

[root@centos7 test]# free -h

             total        used        free      shared  buff/cache   available

Mem:           1.8G        299M        1.0G        9.0M        455M        1.3G

Swap:            0B          0B          0B

[root@centos7 test]# 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): p


Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000e74ad


  Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *        2048      411647      204800   83  Linux

/dev/sda2          411648    10897407     5242880   83  Linux

/dev/sda3        10897408    21383167     5242880   83  Linux

/dev/sda4        21383168    41943039    10279936    5  Extended

/dev/sda5        21385216    31870975     5242880   83  Linux


Command (m for help): n

All primary partitions are in use

Adding logical partition 6

First sector (31873024-41943039, default 31873024): 

Using default value 31873024

Last sector, +sectors or +size{K,M,G} (31873024-41943039, default 41943039): +2G

Partition 6 of type Linux and of size 2 GiB is set


Command (m for help): t

Partition number (1-6, default 6): 

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): 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.

[root@centos7 test]# 


第二步:通知内核重读硬盘分区表(否则系统无法识别划分的分区)

示例代码:

[root@centos7 test]# partprobe /dev/sda

[root@centos7 test]# lsblk

NAME   MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT

sda      8:0    0   20G  0 disk  

├─sda1   8:1    0  200M  0 part  /boot

├─sda2   8:2    0    5G  0 part  /

├─sda3   8:3    0    5G  0 part  /test

├─sda4   8:4    0  512B  0 part  

├─sda5   8:5    0    5G  0 part  /home

└─sda6   8:6    0    2G  0 part  

[root@centos7 test]# 


第三步:使用mkswap格式化交换分区(方便区分,最好添加卷标)

示例代码:

[root@centos7 test]# mkswap -L swappart /dev/sda6

Setting up swapspace version 1, size = 2097148 KiB

LABEL=swappart, UUID=082ebfdf-e8ad-4874-af8e-6a5f2a01c7ac(当前交换分区的UUID)

[root@centos7 test]# 


第四步:使用swapon [DEVICE]挂载交换分区,并用free -h检验结果

示例代码:

[root@centos7 test]# swapon /dev/sda6

[root@centos7 test]# free -h

             total        used        free      shared  buff/cache   available

Mem:           1.8G        302M        1.0G        9.0M        459M        1.3G

Swap:          2.0G          0B        2.0G

[root@centos7 test]# 


如果是临时使用,至此就可以正常使用刚才划分出来的2G的交换分区了。如果想长期使用,就必须写入磁盘挂载配置文件。


第五步:在/etc/fstab文件中添加自动挂载条目

UUID="当前交换分区的UUID" swap swap defaults 0 0


挂载交换分区

swapon [OPTION]... [DEVICE]

-a:激活所有的交换分区;

-p PRIORITY:指定优先级

当使用多个交换分时,可以为其指定优先级

长期生效写入fstab文件中的defaults字段:pri=value(值越大,优先级越高)

-s:查看交换分区使用情况(包括优先级)


示例代码:

[root@centos7 test]# free -h

             total        used        free      shared  buff/cache   available

Mem:           1.8G        302M        1.0G        9.0M        459M        1.3G

Swap:          2.0G          0B        2.0G

[root@centos7 test]# swapon -a

[root@centos7 test]# free -h

             total        used        free      shared  buff/cache   available

Mem:           1.8G        303M        1.0G        9.0M        464M        1.3G

Swap:          3.0G          0B        3.0G

[root@centos7 test]# swapon -s

Filename Type Size Used Priority

/dev/sda6               partition 2097148 0 -1

/dev/sda7               partition 1048572 0 -2

[root@centos7 test]# 


卸载交换分区

swapoff [OPTION]... [DEVICE]

/dev/sda* 卸载某个交换分区

-a 卸载所有交换分区

示例代码:

[root@centos7 test]# free -h

             total        used        free      shared  buff/cache   available

Mem:           1.8G        304M        1.0G        9.0M        463M        1.3G

Swap:          3.0G          0B        3.0G

[root@centos7 test]# swapoff /dev/sda7

[root@centos7 test]# free -h

             total        used        free      shared  buff/cache   available

Mem:           1.8G        304M        1.0G        9.0M        463M        1.3G

Swap:          2.0G          0B        2.0G

[root@centos7 test]# swapoff -a

[root@centos7 test]# free -h

             total        used        free      shared  buff/cache   available

Mem:           1.8G        302M        1.0G        9.0M        463M        1.3G

Swap:            0B          0B          0B

[root@centos7 test]# 


SWAP的优先级

用户可以给某个swap指定一个0到32767的优先级

如果用户没有指定,那么核心会自动给swap指定一个优先级,这个优先级从-1开始,每加入一个新的没有用户指定优先级的swap,会给这个优先级减一

先添加的swap的缺省优先级比较高,除非用户自己指定一个优先级,而用户指定的优先级(是正数)永远高于核心缺省指定的优先级(是负数)


(二)特殊情况,如遇没有单独的分区用作交换分区时,可用文件制作交换分区

第一步:使用dd命令创建一个交换文件

    注意:文件不可使用跳过方式创建,否则格式化时会报错:分区有漏洞

    示例代码:

[root@centos7 test]# dd if=/dev/zero of=/test/swapfile bs=1M count=1024

1024+0 records in

1024+0 records out

1073741824 bytes (1.1 GB) copied, 34.1887 s, 31.4 MB/s

[root@centos7 test]# 


第二步:使用mkswap命令格式化交换文件

示例代码:

[root@centos7 test]# mkswap -L swapfile /test/swapfile

Setting up swapspace version 1, size = 1048572 KiB

LABEL=swapfile, UUID=4618be9e-f44d-4962-b1ce-7e04317e3e5e

[root@centos7 test]# 


第三步:使用swapon [DEVICE]挂载交换分区,并用free -h检验结果

示例代码:

[root@centos7 test]# free -h

             total        used        free      shared  buff/cache   available

Mem:           1.8G        303M         80M        9.0M        1.4G        1.3G

Swap:            0B          0B          0B

[root@centos7 test]# swapon /test/swapfile

swapon: /test/swapfile: insecure permissions 0644, 0600 suggested.

[root@centos7 test]# free -h

             total        used        free      shared  buff/cache   available

Mem:           1.8G        303M         79M        9.0M        1.4G        1.3G

Swap:          1.0G          0B        1.0G

[root@centos7 test]# 


第四步:在/etc/fstab文件中添加自动挂载条目

/test/swapfile  swap    swap    defaults    0 0(注意这里和普通分区的挂载有所不同)


待续……

本文出自 “爱情防火墙” 博客,请务必保留此出处http://183530300.blog.51cto.com/894387/1843130

交换分区以及其它外设的挂载

标签:使用硬盘分区做交换分区   使用文件做交换分区   

原文地址:http://183530300.blog.51cto.com/894387/1843130

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!