新建swap分区的规划、挂载和自动挂载示例

简介:


新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  





本文转自 murongqingqqq  51CTO博客,原文链接:http://blog.51cto.com/murongqingqqq/1361934





相关文章
|
6月前
|
Linux 测试技术
非LVM磁盘分区根目录扩容操作参考
在操作系统部署时,根目录使用的是非LVM分区,后期空间不足又无法扩容。 提供一个思路是新增一块硬盘,创建为逻辑卷,把根目录迁移过去,然后引导以新分区启动。 迁移完成后,旧的根目录可以再格式化为逻辑卷,合并到新磁盘分区。 本操作仅在CentOS Linux release 7.9.2009实验过,其他环境可能不支持。
505 0
|
11月前
|
Linux
Linux将已经挂载的分区取消挂载
Linux将已经挂载的分区取消挂载
|
5G Linux
10.15 Linux fdisk创建分区(主分区、扩展分区和逻辑分区)过程详解
本节我们实际建立一个主分区,看看过程是什么样子的。命令如下:
1066 0
10.15 Linux fdisk创建分区(主分区、扩展分区和逻辑分区)过程详解
|
Linux
Linux 查看分区、未挂载分区
ls /dev 然后挂载 df -T 只可以查看已经挂载的分区和文件系统类型。 fdisk -l 可以显示出所有挂载和未挂载的分区,但不显示文件系统类型。
3229 0
|
Linux
Linux创建swap分区(用文件作为Swap分区)
Linux创建swap分区(用文件作为Swap分区)
311 0