Determine the size of the new swap file in megabytes and multiple by 1024 to determine the block size. For example, the block size of a 2 GB swap file is 2*1024*1024 .
Check the swap size :
[root@node2 ~]# free -m
total used free shared buffers cached
Mem: 2006 1940 66 0 8 650
-/+ buffers/cache: 1282 724
Swap: 1799 173 1626
At a shell prompt as root, type the following command with count being equal to the desired block size:
[root@node2 ~]# dd if=/dev/zero of=/swapfile bs=1024 count=2097152
2097152+0 records in
2097152+0 records out
2147483648 bytes (2.1 GB) copied, 154.49 s, 13.9 MB/s
Setup the swap file with the command:
[root@node2 ~]# mkswap /swapfile
mkswap: /swapfile: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=be934b65-5665-440d-b761-d8b4674d9161
To enable the swap file immediately but not automatically at boot time:
[root@node2 ~]# swapon /swapfile
[root@node2 ~]# free -m
total used free shared buffers cached
Mem: 2006 1936 70 0 9 641
-/+ buffers/cache: 1285 721
Swap: 3847 172 3675
[root@node2 ~]#
To enable it at boot time, edit /etc/fstab to include:
/swapfile swap swap defaults 0 0
The next time the system boots, it enables the new swap file.
[root@node2 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Dec 15 18:13:22 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_node1-lv_root / ext4 defaults 1 1
UUID=aa1f9eb9-7a79-43fc-80e0-fe2b62650201 /boot ext4 defaults 1 2
/dev/mapper/vg_node1-lv_swap swap swap defaults 0 0
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
/swapfile swap swap defaults 0 0
After adding the new swap file and enabling it, verify it is enabled by viewing the output of the command cat /proc/swaps or free.
root@node2 ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/dm-1 partition 1843196 173280 -1
/swapfile file 2097148 0 -2
Check the swap size :
[root@node2 ~]# free -m
total used free shared buffers cached
Mem: 2006 1940 66 0 8 650
-/+ buffers/cache: 1282 724
Swap: 1799 173 1626
At a shell prompt as root, type the following command with count being equal to the desired block size:
[root@node2 ~]# dd if=/dev/zero of=/swapfile bs=1024 count=2097152
2097152+0 records in
2097152+0 records out
2147483648 bytes (2.1 GB) copied, 154.49 s, 13.9 MB/s
Setup the swap file with the command:
[root@node2 ~]# mkswap /swapfile
mkswap: /swapfile: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=be934b65-5665-440d-b761-d8b4674d9161
To enable the swap file immediately but not automatically at boot time:
[root@node2 ~]# swapon /swapfile
[root@node2 ~]# free -m
total used free shared buffers cached
Mem: 2006 1936 70 0 9 641
-/+ buffers/cache: 1285 721
Swap: 3847 172 3675
[root@node2 ~]#
To enable it at boot time, edit /etc/fstab to include:
/swapfile swap swap defaults 0 0
The next time the system boots, it enables the new swap file.
[root@node2 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Dec 15 18:13:22 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_node1-lv_root / ext4 defaults 1 1
UUID=aa1f9eb9-7a79-43fc-80e0-fe2b62650201 /boot ext4 defaults 1 2
/dev/mapper/vg_node1-lv_swap swap swap defaults 0 0
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
/swapfile swap swap defaults 0 0
root@node2 ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/dm-1 partition 1843196 173280 -1
/swapfile file 2097148 0 -2