Linux Swap ,add ,delete and move
2013-05-20You have two options: add a swap partition or add a swap file. It is recommended that you add a swap partition, but sometimes that is not easy if you do not have any free space available.
To add a swap partition (assuming /dev/sdb2 is the swap partition you want to add):
1. The hard drive can not be in use (partitions can not be mounted, and swap space can not be enabled). The easiest way to achieve this it to boot your system in rescue mode. Refer to Red Hat Linux Manuals Chapter 8 for instructions on booting into rescue mode. When prompted to mount the filesystem, select Skip.Alternately, if the drive does not contain any partitions in use, you can unmount them and turn off all the swap space on the hard drive with the swapoff command.
2. Create the swap partition using parted or fdisk. Using parted is easier than fdisk; thus, only parted will be explained. To create a swap partition with parted:
* At a shell prompt as root, type the command parted /dev/sdb, where /dev/sdb is the device name for the hard drive with free space.
* At the (parted) prompt, type print to view the existing partitions and the amount of free space. The start and end values are in megabytes. Determine how much free space is on the hard drive and how much you want to allocate for a new swap partition.
* At the (parted) prompt, type mkpartfs part-type linux-swap start end, where part-type is one of primary, extended, or logical, start is the starting point of the partition, and end is the end point of the partition.
Warning
Changes take place immediately; be careful when you type.
* Exit parted by typing quit.
3. Now that you have the swap partition, use the command mkswap to setup the swap partition. At a shell prompt as root, type the following:
mkswap /dev/sdb2
4. To enable the swap partition immediately, type the following command:
swapon /dev/sdb2
5. To enable it at boot time, edit /etc/fstab to include:
/dev/sdb2 swap swap defaults 0 0
6. The next time the system boots, it will enable the new swap partition.
7. After adding the new swap partition and enabling it, make sure it is enabled by viewing the output of the command cat /proc/swaps or free.
To add a swap file :
1. Determine the size of the new swap file and multiple by 1024 to determine the block size. For example, the block size of a 64 MB swap file is 65536.
2. At a shell prompt as root, type the following command with count being equal to the desired block size:
dd if=/dev/zero of=/swapfile bs=1024 count=65536
3. Setup the swap file with the command:
mkswap /swapfile
4. To enable the swap file immediately but not automatically at boot time:
swapon /swapfile
5. To enable it at boot time, edit /etc/fstab to include:
/swapfile swap swap defaults 0 0
6. The next time the system boots, it will enable the new swap file.
7. After adding the new swap file and enabling it, make sure it is enabled by
viewing the output of the command cat /proc/swaps or free.
#########################################################################################
Removing Swap Space
To remove a swap partition:
1. The hard drive can not be in use (partitions can not be mounted, and swap space can not be enabled). The easiest way to achieve this it to boot your system in rescue mode. Refer to Red Hat Linux Manuals Chapter 8 for instructions on booting into rescue mode. When prompted to mount the filesystem, select Skip.Alternately, if the drive does not contain any partitions in use, you can unmount them and turn off all the swap space on the hard drive with the swapoff command.
2. At a shell prompt as root, execute the following command to make sure the swap partition is disabled (where /dev/hdb2 is the swap partition):
swapoff /dev/sdb2
3. Remove its entry from /etc/fstab.
4. Remove the partition using parted or fdisk. Only parted will be discussed. To remove the partition with parted:
* At a shell prompt as root, type the command parted /dev/sdb, where /dev/sdb is the device name for the hard drive with free space.
* At the (parted) prompt, type print to view the existing partitions and determine the minor number of the swap partition you wish to delete.
* At the (parted) prompt, type rm MINOR, where MINOR is the minor number of the
partition you want to remove.
Warning
Changes take effect immediately; you must type the correct minor number.
* Type quit to exit parted.
To remove a swap file:
1. At a shell prompt as root, execute the following command to disable the swap
file (where /swapfile is the swap file):
swapoff /swapfile
2. Remove its entry from /etc/fstab.
3. Remove the actual file:
rm /swapfile
#########################################################################################
Moving Swap Space
To move swap space from one location to another, follow the steps for removing swap space, and then follow the steps for adding swap space.
相关日志
- nagios4.3.4升级4.4.1
- centos7配置网络nmcli无法使用
- Linux下怎么查看16进制文件
- shell中获得当前用户名
- nagios 4.08配置步骤
- awk和sed实例
- linux查看当前使用的shell
- wget同步FTP
- date -d
- CentOS配置vsftpd
- iperf3测带宽质量
- "tar --exclude"
- base64
- tar – 复制目录及文件
- shell Cli Shortcuts
- Windows快捷键
- windows sc command
- Displays the UUID of a computer
- history记录添加时间戳
- dstat
- getent
- lastb命令
- set -o vi
- Linpack and Flops
- grep几个小技巧
- Sed的常用命令语法 (6)
- linux命令行自动补齐 (6)
- wildcard"*"号的用法 (1)
- !$
- 回车符和换行符区别 (1)
- 监控工具nmon (1)
- awk to print all row to one line (8)
- reg命令给windows添加环境变量 (2)
- 统计文件行数的命令 (1)
- linux历史命令 (1)
- linux命令行TAB自动补全功能 (9)
- tar命令实例 (1)
- linux常用命令 (6)
- expr命令实例 (1)
- cut命令实例 (1)
- Linux下chkconfig命令详解 (1)
- vi或sed的批量替换 (1)
- xargs命令实例 (3)
- grep 用正则表达式实例 (2)
- linux add swap by file (1)
- sed-grep-awk (1)