首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

Linux中swap交换分区的创建和容量调整的教程(2)

Linux中swap交换分区的创建和容量调整的教程(2)

三、用文件作为Swap分区
1.创建要作为swap分区的文件:增加1GB大小的交换分区,则命令写法如下,其中的count等于想要的块的数量(bs*count=文件大小)。
复制代码
代码如下:
# dd if=/dev/zero of=/root/swapfile bs=1M count=1024
2.格式化为交换分区文件:
复制代码
代码如下:
# mkswap /root/swapfile #建立swap的文件系统
3.启用交换分区文件:
复制代码
代码如下:
# swapon /root/swapfile #启用swap文件
4.使系统开机时自启用,在文件/etc/fstab中添加一行:
复制代码
代码如下:
/root/swapfile swap swap defaults 0 0
新建和增加交换分区用到的命令为:mkswap、swapon等,而想关闭掉某个交换分区则用“swapon /dev/sdb2”这样的命令即可。
下面的操作命令演示了用文件作为Swap的操作过程(包括前后的一些检查)。
复制代码
代码如下:
[root@jay-linux ~]# free -m
total used free shared buffers cached
Mem: 3979 3179 800 0 266 1873
-/+ buffers/cache: 1039 2940
Swap: 4095 0 4095

复制代码
代码如下:
[root@jay-linux ~]# cat /etc/fstab
# 已省略与主题无关的部分输出
/dev/sda3 swap swap defaults 0 0

复制代码
代码如下:
[root@jay-linux ~]# 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, 4.09017 s, 263 MB/s
[root@jay-linux ~]# mkswap /root/swapfile
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=48a7ffd5-392f-4963-904c-3e67f0e5dfbe

复制代码
代码如下:
[root@jay-linux ~]# swapon /root/swapfile
[root@jay-linux ~]# free -m
total used free shared buffers cached
Mem: 3979 3945 34 0 261 2628
-/+ buffers/cache: 1054 2924
Swap: 5119 0 5119

复制代码
代码如下:
[root@jay-linux ~]# swapoff /dev/sda3
[root@jay-linux ~]# free -m
total used free shared buffers cached
Mem: 3979 3945 34 0 261 2629
-/+ buffers/cache: 1053 2925
Swap: 1023 0 1023

复制代码
代码如下:
[root@jay-linux ~]# swapoff /root/swapfile
[root@jay-linux ~]# free -m
total used free shared buffers cached
Mem: 3979 3945 34 0 261 2629
-/+ buffers/cache: 1053 2926
Swap: 0 0 0

复制代码
代码如下:
[root@jay-linux ~]# swapon /dev/sda3
[root@jay-linux ~]# free -m
total used free shared buffers cached
Mem: 3979 3946 33 0 261 2629
-/+ buffers/cache: 1054 2924
Swap: 4095 0 4095

复制代码
代码如下:
[root@jay-linux ~]# free -m
total used free shared buffers cached
Mem: 3979 3179 800 0 266 1873
-/+ buffers/cache: 1039 2940
Swap: 4095 0 4095

复制代码
代码如下:
[root@jay-linux ~]# cat /etc/fstab
# 已省略与主题无关的部分输出
/dev/sda3 swap swap defaults 0 0

复制代码
代码如下:
[root@jay-linux ~]# 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, 4.09017 s, 263 MB/s
[root@jay-linux ~]# mkswap /root/swapfile
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=48a7ffd5-392f-4963-904c-3e67f0e5dfbe

复制代码
代码如下:
[root@jay-linux ~]# swapon /root/swapfile
[root@jay-linux ~]# free -m
total used free shared buffers cached
Mem: 3979 3945 34 0 261 2628
-/+ buffers/cache: 1054 2924
Swap: 5119 0 5119

复制代码
代码如下:
[root@jay-linux ~]# swapoff /dev/sda3
[root@jay-linux ~]# free -m
total used free shared buffers cached
Mem: 3979 3945 34 0 261 2629
-/+ buffers/cache: 1053 2925
Swap: 1023 0 1023

复制代码
代码如下:
[root@jay-linux ~]# swapoff /root/swapfile
[root@jay-linux ~]# free -m
total used free shared buffers cached
Mem: 3979 3945 34 0 261 2629
-/+ buffers/cache: 1053 2926
Swap: 0 0 0

复制代码
代码如下:
[root@jay-linux ~]# swapon /dev/sda3
[root@jay-linux ~]# free -m
total used free shared buffers cached
Mem: 3979 3946 33 0 261 2629
-/+ buffers/cache: 1054 2924
Swap: 4095 0 4095
继承事业,薪火相传
返回列表