# 13.2 Swap Space Swap space is an area on a hard disk. It is part of your machine's Virtual Memory, which is a combination of accessible physical memory (RAM) and the swap space. Swap holds memory pages that are temporarily inactive. Swap space is used when your operating system decides that it needs physical memory for active processes and the amount of available (unused) physical memory is insufficient. When this happens, inactive pages from the physical memory are then moved into the swap space, freeing up that physical memory for other uses. Note that the access time for swap is slower, depending on the speed of the hard drive. #### Making a partition for swap Like the last assignment we will need to make a partition for the swap to use: ```bash fdisk /dev/vdb ``` As a reminder to use with the ```fdisk``` command: ``` n - new partition p - primairy partion [enter] - select logical next number [enter] - select logical partition start sector +1G - 1 Gigabyte in size w. - write partition ``` Do not yet write the partition, we need to give the swap the correct label. Set the newly created partition to type Linux swap press ```t``` and select the newly made partition ```2``` and you can now select ```L``` to see all the types: ``` Command (m for help): t Partition number (1-2, default 3): 2 Hex code (type L to list all codes): L 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx 5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data 6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility 8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt 9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary 16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT 1e Hidden W95 FAT1 80 Old Minix ``` We want the partition to be labeled as ```Linux swap``` which is entry ```82```: ``` Hex code (type L to list all codes): 82 Changed type of partition 'Linux' to 'Linux swap / Solaris' ``` This will have changed the partition label, we can check the end result with ```p```: ``` Command (m for help): p Disk /dev/vdb: 10 GiB, 10737418240 bytes, 20971520 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 Disklabel type: dos Disk identifier: 0x7af272f8 Device Boot Start End Sectors Size Id Type /dev/vdb1 2048 2099199 2097152 1G 83 Linux /dev/vdb2 2099200 4196351 2097152 1G 82 Linux swap / Solaris ``` Save the partition table changes: ``` Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks. ``` Run partprobe to make the kernel aware of the partition table change. Now we must add the swap filesystem to ```vdb2```, we will use the ```mkswap``` command for this. ```bash mkswap /dev/vdb2 ``` ```bash [root@rhcsa ~]# mkswap /dev/vdb2 Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes) no label, UUID=e5d1e61d-f73e-4bd0-98a9-80d2c2fc6ae1 ``` Either do blkid and copy the ```UUID``` or let's do a trick and add the swap filesystem blockid: ```bash cp /etc/fstab /etc/fstab.bak; blkid | grep vdb2 >> /etc/fstab ``` We can now edit the ```/etc/fstab``` finish the swap mount (Note the UUID to keep the UUID of your swap): ``` UUID=e5d1e61d-f73e-4bd0-98a9-80d2c2fc6ae1 swap swap defaults 0 0 ``` or ``` /dev/vdb2 swap swap defaults 0 0 ``` Now that the swap has been added to the ```/etc/fstab```, file we can assume it will come up on a reboot. However, with swap - if we want to test and check it - we will have to run a command to turn the swap on. Let's check our available swap first with the free command: ```bash [root@rhcsa ~]# free -h total used free shared buff/cache available Mem: 754Mi 288Mi 156Mi 4.0Mi 308Mi 331Mi Swap: 923Mi 28Mi 895Mi ``` This shows it has 900M available right now. ``` swapon /dev/vdb2 ``` This will turn the swap on. Let's check: ``` free -h ``` ```bash [root@rhcsa ~]# free -h total used free shared buff/cache available Mem: 754Mi 289Mi 155Mi 4.0Mi 308Mi 330Mi Swap: 1.9Gi 29Mi 1.9Gi ``` And we can check it with ```swapon -s```: ``` [root@rhcsa ~]# swapon -s Filename Type Size Used Priority /dev/dm-1 partition 946172 30088 -2 /dev/vdb2 partition 1048572 0 -3 ``` This shows that the swap on ```vdb2``` is added to the pool. You can disable the swap space with: ``` swapoff /dev/vdb2 ``` Verify that the swap space is disabled ``` [root@rhcsa ~]# swapon -s Filename Type Size Used Priority /dev/dm-1 partition 946172 29376 -2 ``` As you can see, it is out of the pool!