注:来自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: -/+ buffers/cache: Swap: [root@localhost ~]# fdisk -l /dev/sda Disk /dev/sda: 64.4 GB, bytes heads, sectors/track, cylinders Units = cylinders of * = bytes Device Boot Start End Blocks Id System /dev/sda1 * Linux /dev/sda2 Linux /dev/sda3 + Linux /dev/sda4 Extended /dev/sda5 Linux swap / Solaris
第二步:新建磁盘分区指定状态为82,即为swap分区格式:fdisk命令
[root@localhost ~]# fdisk /dev/sda The number of cylinders for this disk is set to . There is nothing wrong with that, but this is larger than , and could in certain setups cause problems with: ) software that runs at boot time (e.g., old versions of LILO) ) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/ FDISK) Command (m for help): n First cylinder (-, default ): Using default value Last cylinder or +size or +sizeM or +sizeK (-, default ): +1G Command (m for help): p Disk /dev/sda: 64.4 GB, bytes heads, sectors/track, cylinders Units = cylinders of * = bytes Device Boot Start End Blocks Id System /dev/sda1 * Linux /dev/sda2 Linux /dev/sda3 + Linux /dev/sda4 Extended /dev/sda5 Linux swap / Solaris /dev/sda6 Linux Command (m for help): t Partition number (-): Hex code (type L to list codes): Changed system type of partition 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 : 设备或资源忙. 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 , size = kB
第五步:手动挂载和卸载swap分区:swapon/off
[root@localhost ~]# free -m total used free shared buffers cached Mem: -/+ buffers/cache: Swap: [root@localhost ~]# swapon /dev/sda6 [root@localhost ~]# free -m total used free shared buffers cached Mem: -/+ buffers/cache: Swap: [root@localhost ~]# swapon -s //查看都有哪些交换分区挂载 Filename Type Size Used Priority /dev/sda5 partition - /dev/sda6 partition - [root@localhost ~]# swapoff /dev/sda6 //卸载swap分区 [root@localhost ~]# swapon -s Filename Type Size Used Priority /dev/sda5 partition -
第六步:设置开机自动挂载swap分区
[root@localhost ~]# cat /etc/fstab LABEL=/ / ext3 defaults LABEL=/data /data ext3 defaults LABEL=/boot /boot ext3 defaults tmpfs /dev/shm tmpfs defaults devpts /dev/pts devpts gid=,mode= sysfs /sys sysfs defaults proc /proc proc defaults LABEL=SWAP-sda5 swap swap defaults [root@localhost ~]# vim /etc/fstab //编辑/etc/fstab文件,增加下面内容 [root@localhost ~]# cat /etc/fstab | grep sda6 //将下面的信息添加到/etc/fstab文件 /dev/sda6 swap swap defaults [root@localhost ~]# swapon -s Filename Type Size Used Priority /dev/sda5 partition - [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 - /dev/sda6 partition - [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