linux swap分区修改的2种方式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux swap分区修改的2种方式相关的知识,希望对你有一定的参考价值。

参考网站:http://blog.csdn.net/hshl1214/article/details/6048278

原帖子写的很好,我把步骤给贴出来,测试系统:rhel6.4 x64


1.新建磁盘分区作为swap分区

[[email protected] /]# free -m  //查看当前swap信息
             total       used       free     shared    buffers     cached
Mem:          1878        348       1530          0         19        156
-/+ buffers/cache:        172       1706
Swap:         4031          0       4031
[[email protected] /]# swapoff -a      //停用swap空间
[[email protected] /]# free -m         //查看是否停用
             total       used       free     shared    buffers     cached
Mem:          1878        345       1532          0         19        156
-/+ buffers/cache:        169       1709
Swap:            0          0          0
[[email protected] /]# fdisk /dev/sdb  //进入sdb硬盘中(sdb硬盘中有多余未使用的磁盘空间)

WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to
         switch off the mode (command ‘c‘) and change display units to
         sectors (command ‘u‘).

Command (m for help): n      //新建分区
Command action
   e   extended
   p   primary partition (1-4)
p     //创建主分区
Partition number (1-4): 2   //sdb硬盘的第二个主分区
First cylinder (8193-10240, default 8193):   //从硬盘哪个柱面开始,enter键默认(可以写对应数字)
Using default value 8193
Last cylinder, +cylinders or +size{K,M,G} (8193-10240, default 10240): //从硬盘哪个柱面开始,enter键默认

Using default value 10240

Command (m for help): p      //查看改变后分区情况,P:打印分区信息

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
64 heads, 32 sectors/track, 10240 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb92c060c

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        8192     8388592   83  Linux
/dev/sdb2            8193       10240     2097152   83  Linux

Command (m for help): t      //开始修改/dev/sdb2的属性,改为swap id:82
Partition number (1-4): 2    //选择要修改的分区
Hex code (type L to list codes): 82  //swap分区Id
Changed system type of partition 2 to 82 (Linux swap / Solaris)

Command (m for help): p  //查看改变后分区情况

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
64 heads, 32 sectors/track, 10240 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb92c060c

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        8192     8388592   83  Linux
/dev/sdb2            8193       10240     2097152   82  Linux swap / Solaris   //这里被改成swap属性

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.
[[email protected] /]# mkswap /dev/sdb2
/dev/sdb2: No such file or directory   //修改后需要重启

[[email protected] /]# reboot    //重启

[[email protected] ~]# fdisk -l

[[email protected] ~]# mkswap /dev/sdb2   //格式化swap分区

Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=2d26d5ad-4ee2-4c1d-b90c-7cf4060fd9a9
[[email protected] ~]# swapon /dev/sdb2    //启用swap
[[email protected] ~]# free  -m    // 查看swap容量是否变大,判断操作是否成功
             total       used       free     shared    buffers     cached
Mem:          1878        348       1529          0         18        156
-/+ buffers/cache:        173       1704
Swap:         6079          0       6079[[email protected] ~]# vi /etc/fstab
/dev/sdb2 swap swap defaults 0 0   //使系统开机时自启用,在文件最后添加一行


###########################################################

2.用文件作为Swap分区
(紧随上面的信息做如下操作)
[[email protected] ~]# free -m   //查看初始信息
             total       used       free     shared    buffers     cached
Mem:          1878        348       1529          0         18        156
-/+ buffers/cache:        173       1704
Swap:         6079          0       6079

#创建要作为swap分区的文件:增加1GB大小的交换分区,则命令写法如下,其中的count等于想要的块的数量(bs*count=文件大小)。
[[email protected] ~]# dd if=/dev/zero of=/root/swapfile bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 12.1908 s, 88.1 MB/s
[[email protected] ~]# mkswap /root/swapfile   //格式化为交换分区文件
mkswap: /root/swapfile: warning: don‘t erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=3330e86b-ec5a-4423-bc08-07bcc3785495
[[email protected] ~]# swapon /root/swapfile     //启用交换分区文件
[[email protected] ~]# free -m   //查看修改后的信息,判定操作是否成功
             total       used       free     shared    buffers     cached
Mem:          1878       1403        474          0         19       1180
-/+ buffers/cache:        204       1674
Swap:         7103          0       7103
[[email protected] ~]# vi /etc/fstab
/root/swapfile swap swap defaults 0 0   //使系统开机时自启用,在文件最后添加一行


3.删除swap空间

[[email protected] ~]# swapoff /root/swapfile
[[email protected] ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          1878       1403        475          0         19       1180
-/+ buffers/cache:        203       1674
Swap:         6079          0       6079
[[email protected] ~]#



以上是关于linux swap分区修改的2种方式的主要内容,如果未能解决你的问题,请参考以下文章

Linux系统之armbain配置swap交换分区

LINUX新建和增加SWAP分区

Linux下修改Swap分区大小

ubuntu 创建swap分区

redhat linux swap分区扩展的方法都有哪些

保姆级swap分区详解!手把手带你创建swap分区(两种方式,建议收藏)涉及fdiskgdiskdfpartedpartprobemkswapswaponfreeddod等命令