linux swap分區修改的2種方式

參考網站:http://blog.csdn.net/hshl1214/article/details/6048278

原帖子寫的很好,我把步驟給貼出來,測試系統:rhel6.4 x64


1.新建磁盤分區作爲swap分區

[root@localhost /]# free -m  //查看當前swap信息
             total       used       free     shared    buffers     cached
Mem:          1878        348       1530          0         19        156
-/+ buffers/cache:        172       1706
Swap:         4031          0       4031
[root@localhost /]# swapoff -a      //停用swap空間
[root@localhost /]# free -m         //查看是否停用
             total       used       free     shared    buffers     cached
Mem:          1878        345       1532          0         19        156
-/+ buffers/cache:        169       1709
Swap:            0          0          0
[root@localhost /]# 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.
[root@localhost /]# mkswap /dev/sdb2
/dev/sdb2: No such file or directory   //修改後需要重啓

[root@localhost /]# reboot    //重啓

[root@localhost ~]# fdisk -l

[root@localhost ~]# mkswap /dev/sdb2   //格式化swap分區

Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=2d26d5ad-4ee2-4c1d-b90c-7cf4060fd9a9
[root@localhost ~]# swapon /dev/sdb2    //啓用swap
[root@localhost ~]# free  -m    // 查看swap容量是否變大,判斷操作是否成功
             total       used       free     shared    buffers     cached
Mem:          1878        348       1529          0         18        156
-/+ buffers/cache:        173       1704
Swap:         6079          0       6079[root@localhost ~]# vi /etc/fstab
/dev/sdb2 swap swap defaults 0 0   //使系統開機時自啓用,在文件最後添加一行


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

2.用文件作爲Swap分區
(緊隨上面的信息做如下操作)
[root@localhost ~]# 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=文件大小)。
[root@localhost ~]# 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
[root@localhost ~]# 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
[root@localhost ~]# swapon /root/swapfile     //啓用交換分區文件
[root@localhost ~]# free -m   //查看修改後的信息,判定操作是否成功
             total       used       free     shared    buffers     cached
Mem:          1878       1403        474          0         19       1180
-/+ buffers/cache:        204       1674
Swap:         7103          0       7103
[root@localhost ~]# vi /etc/fstab
/root/swapfile swap swap defaults 0 0   //使系統開機時自啓用,在文件最後添加一行


3.刪除swap空間

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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章