🌐 🇵🇱 Polski · 🇬🇧 EN
In Linux, SWAP space is used as additional memory capacity that activates when the current physical memory is insufficient. It is not a complete replacement for physical RAM because it is significantly slower, but it is useful in critical situations where memory is exhausted. There are two types of SWAP systems: a SWAP file and a SWAP partition. Let's look at the commands for SWAP.
mkswap - Create a SWAP device
swapon - Enable SWAP space
swapoff - Disable SWAP space
dd - Reserve disk space
First, we will create a SWAP partition in the remaining space of the hdc2 partition (remember that a swap partition is different from an ext partition). We use the "mkswap" command to create the SWAP space.
Syntax for swapon : # swapon [options] device
Options :
- -a - Enable all swap spaces
- -e - Silently skip non-existent devices
- -s - Check if SWAP is running
Step 1 - Start with the "swapon" command to check where we can place the SWAP space
Filename Type Size Used Priority
/dev/dm-1 partition 4128760 0 -1
The output shows the existing SWAP partition created during system installation.
Syntax for the mkswap command: # mkswap [options] device
Options for the mkswap command:
- -c - check for bad blocks on the device before creating SWAP
Step 2 - Create SWAP space on /dev/hdc2:
# mkswap /dev/hdc2
Setting up swapspace version 1, size = 4184928 KiB
no label, UUID=aaf9b7ec-4e88-462b-a369-d63cf84ec626
After creating the SWAP space, you must enable it to start using it
Step 3 - Enable the swap partition
# swapon /dev/hdc2
Step 4 - Verify that the SWAP partition is working correctly
# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 4128760 0 -1
/dev/hdc2 partition 4184924 0 -2
The SWAP partitions have been configured. You can also use the option to reserve SWAP space in a file. Let's use the "dd" command to reserve additional swap space on the /dev/hdb1 disk.
Step 5 - Reserve 1GB of space for SWAP
# dd if=/dev/zero of=/mnt/file_swap bs=1024 count=1000000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 19.2931 seconds, 53.1 MB/s
Step 6 - Similar to swap on a partition, you can use swap space by specifying a file device
# mkswap /mnt/file_swap
Setting up swapspace version 1, size = 999996 KiB
no label, UUID=e08a3e4d-8712-413d-9db7-c221cf5087a2
Step 7 - Enable swap
# swapon /mnt/file_swap
Step 8 - Verify that the SWAP exchange space is working.
# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 4128760 0 -1
/dev/hdc2 partition 4184924 0 -2
/mnt/file_swap file 999992 0 -3
If you need to disable the swap space, use the command: "swapoff"
RESIZING SWAP
Step 1 - Disable SWAP
swapoff -v /dev/VolGroup00/LogVol01
Step 2 - Resize
lvm lvresize /dev/VolGroup00/LogVol01 -L +256M
Step 3 - Add entry to /etc/fstab
/dev/VolGroup00/LogVol02 swap swap defaults 0 0
Step 4 - Enable the extended volume
# swapon -va
RESIZING SWAP
Step 1 - Disable SWAP
swapoff -v /dev/VolGroup00/LogVol01
Step 2 - Resize
lvm lvresize /dev/VolGroup00/LogVol01 -L +256M
Step 3 - Add entry to /etc/fstab
/dev/VolGroup00/LogVol02 swap swap defaults 0 0
Step 4 - Enable the extended volume
# swapon -va
Step 5 - Test
cat /proc/swaps # free
Comments