新建swap分區的規划、掛載和自動掛載示例


注:來自Linux系統管理_磁盤分區和格式化的擴展

Linux系統管理_磁盤分區和格式化:http://murongqingqqq.blog.51cto.com/2902694/1361918

 

思路:

第一步:首先查看當前swap分區的大小:free -m

第二步:新建磁盤分區指定狀態為82,即為swap分區格式:fdisk命令

第三步:重讀磁盤分區:partprobe命令

第四步:格式化swap分區:mkswap命令

第五步:手動掛載和卸載swap分區:swapon/off

第六步:設置開機自動掛載swap分區:swapon -a

 

具體操作:

第一步:首先查看當前swap分區的大小:free -m

[root@localhost ~]# free -m

            total       used       free     shared    buffers     cached

Mem:           495        285        209          0         18        167

-/+ buffers/cache:         99        395

Swap:         2047          0       2047

 

[root@localhost ~]# fdisk -l /dev/sda

 

Disk /dev/sda: 64.4 GB, 64424509440 bytes

255 heads, 63 sectors/track, 7832 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

 

  Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          25      200781   83  Linux

/dev/sda2              26        2575    20482875   83  Linux

/dev/sda3            2576        3850    10241437+  83  Linux

/dev/sda4            3851        7832    31985415    5  Extended

/dev/sda5            3851        4111     2096451   82  Linux swap / Solaris

 

 

第二步:新建磁盤分區指定狀態為82,即為swap分區格式:fdisk命令

 

[root@localhost ~]# fdisk /dev/sda

 

The number of cylinders for this disk is set to 7832.

There is nothing wrong with that, but this is larger than 1024,

and could in certain setups cause problems with:

1) software that runs at boot time (e.g., old versions of LILO)

2) booting and partitioning software from other OSs

  (e.g., DOS FDISK, OS/2 FDISK)

 

Command (m for help): n

First cylinder (4112-7832, default 4112):

Using default value 4112

Last cylinder or +size or +sizeM or +sizeK (4112-7832, default 7832): +1G

 

Command (m for help): p

 

Disk /dev/sda: 64.4 GB, 64424509440 bytes

255 heads, 63 sectors/track, 7832 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

 

  Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          25      200781   83  Linux

/dev/sda2              26        2575    20482875   83  Linux

/dev/sda3            2576        3850    10241437+  83  Linux

/dev/sda4            3851        7832    31985415    5  Extended

/dev/sda5            3851        4111     2096451   82  Linux swap / Solaris

/dev/sda6            4112        4234      987966   83  Linux

 

Command (m for help): t

Partition number (1-6): 6

Hex code (type L to list codes): 82

Changed system type of partition 6 to 82 (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: 設備或資源忙.

The kernel still uses the old table.

The new table will be used at the next reboot.

Syncing disks.

 

[root@localhost ~]# ls /dev/sd*

/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sda4  /dev/sda5

 

 

第三步:重讀磁盤分區:partprobe命令

[root@localhost ~]# partprobe

[root@localhost ~]# ls /dev/sd*

/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sda4  /dev/sda5  /dev/sda6

 

 

 

第四步:格式化swap分區:mkswap命令

[root@localhost ~]# mkswap /dev/sda6

Setting up swapspace version 1, size = 1011671 kB

 

 

第五步:手動掛載和卸載swap分區:swapon/off

[root@localhost ~]# free -m

            total       used       free     shared    buffers     cached

Mem:           495        287        208          0         18        168

-/+ buffers/cache:         99        395

Swap:         2047          0       2047

[root@localhost ~]# swapon /dev/sda6

[root@localhost ~]# free -m

            total       used       free     shared    buffers     cached

Mem:           495        287        207          0         18        168

-/+ buffers/cache:        100        395

Swap:         3012          0       3012

[root@localhost ~]# swapon -s        //查看都有哪些交換分區掛載

Filename                                Type            Size    Used    Priority

/dev/sda5                               partition       2096440 0       -1

/dev/sda6                               partition       987956  0       -3

[root@localhost ~]# swapoff /dev/sda6        //卸載swap分區

[root@localhost ~]# swapon -s

Filename                                Type            Size    Used    Priority

/dev/sda5                               partition       2096440 0       -1

 

 

第六步:設置開機自動掛載swap分區

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

LABEL=/                 /                       ext3    defaults        1 1

LABEL=/data             /data                   ext3    defaults        1 2

LABEL=/boot             /boot                   ext3    defaults        1 2

tmpfs                   /dev/shm                tmpfs   defaults        0 0

devpts                  /dev/pts                devpts  gid=5,mode=620  0 0

sysfs                   /sys                    sysfs   defaults        0 0

proc                    /proc                   proc    defaults        0 0

LABEL=SWAP-sda5         swap                    swap    defaults        0 0

[root@localhost ~]# vim /etc/fstab        //編輯/etc/fstab文件,增加下面內容

[root@localhost ~]# cat /etc/fstab | grep sda6    //將下面的信息添加到/etc/fstab文件

/dev/sda6               swap                    swap   defaults        0 0

 

 

[root@localhost ~]# swapon -s

Filename                                Type            Size    Used    Priority

/dev/sda5                               partition       2096440 0       -1

[root@localhost ~]# swapon -a        //用swapon -a來重讀/etc/fstab文件,使新swap分區掛載

[root@localhost ~]# swapon -s       //再次用swapon -s查看的時候,新的swap分區sda6成功掛載

Filename                                Type            Size    Used    Priority

/dev/sda5                               partition       2096440 0       -1

/dev/sda6                               partition       1959888 0       -5

[root@localhost ~]#

 

 

 

 

 

擴展:swap分區開機自動掛載的第二種方式:

第一步:修改/etc/rc.d/rc.local文件

第二步:將swapon /dev/sda6寫入這個腳本當中,那么開機就可以自動掛載交換分區/dev/sda6了!!!

 

 

 

注:用swapon -a和reboot命令來實現重讀/etc/fstab文件,實現開機自動掛載。

普通分區重讀/etc/fstab文件的時候用mount -a,swap分區重讀/etc/fstab文件的時候,

使用swapon -a  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM