Linux系统添加Swap分区完整指南
首先需要在磁盘上创建一个标准的Linux分区,该分区既可以是主分区也可以是逻辑分区。
fdisk /dev/sdb
Command (m for help): p
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders, total 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x740e36e3
Device Boot Start End Blocks Id System
/dev/sdb1 63 4194367 2097152+ 83 Linux
/dev/sdb2 4194368 16777215 6291424 5 Extended
/dev/sdb5 4194431 8388735 2097152+ 83 Linux
/dev/sdb6 8388799 16777215 4194208+ 83 Linux
以设备/dev/sdb6为例,通过查看分区表可知其当前的十六进制标识符为83,这代表它是一个常规的Linux文件系统分区。
接下来需要将该分区的类型标识符修改为交换分区专用的代码。
//t命令:改写分区十六进制编码
Command (m for help): t
Partition number (1-6): 6
//L命令可以查看不同分区的十六进制编码,交换分区为82
Hex code (type L to list codes): 82
Changed system type of partition 6 to 82 (Linux swap / Solaris)
Command (m for help): p
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders, total 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x740e36e3
Device Boot Start End Blocks Id System
/dev/sdb1 63 4194367 2097152+ 83 Linux
/dev/sdb2 4194368 16777215 6291424 5 Extended
/dev/sdb5 4194431 8388735 2097152+ 83 Linux
/dev/sdb6 8388799 16777215 4194208+ 82 Linux swap / Solaris
//分区表写入磁盘并退出
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
分区类型修改完成后,需对该分区进行格式化,使其具备交换空间的结构。
mkswap /dev/sdb6
Setting up swapspace version 1, size = 4194204 KiB
no label, UUID=40eff2c0-3f6b-440e-bc2a-f71d3d5581a7
使用swapon命令激活该交换分区,使其立即生效。
swapon /dev/sdb6
执行free命令可以检查当前系统的内存及交换分区的使用状态,确认加载是否成功。
free
total used free shared buffers cached
Mem: 1030108 160768 869340 264 12040 56188
-/+ buffers/cache: 92540 937568
Swap: 6291352 0 6291352
若需移除交换分区,可使用swapoff命令停止使用该分区。
swapoff /dev/sdb6
注意事项:
Swap分区不同于普通文件系统,它没有挂载点,因此不能使用mount命令进行挂载。
为了让系统在重启后自动加载交换分区,需要编辑/etc/fstab文件,添加相应的配置条目。
/dev/sdb6 swap swap defaults 0 0
//查看交换分区
swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 2097148 0 -1
/dev/sdb6 partition 4194204 0 -2
总结
以上介绍了在Linux中添加Swap分区的具体操作步骤,希望能对读者有所帮助。如有任何疑问,欢迎留言讨论,作者将及时回复。