Configure Swap Space and Hibernation in Linux

Configure Swap Space and Hibernation in Linux

Typically, swap space is something that is taken care of during the server deployment, however, there are situations in which it has to be done afterwards. I got into a situation where swap space had to be increased and no linux admin was available. And, since Linux is not my job, at the time, I had no clue what to do. So, that made me deploy a linux server in my virtualbox lab and try it out myself.

I have seen this many times in the past that I would request a Linux administrator to increase the size of the swap file on a server to properly configure the applications that I deploy. The greatest example that comes to my mind is Cyber Sense. Cyber Sense needs a lot of memory AND a lot of swap space and rightly so, because of the scanning of data that it does. Its a very resource heavy process. Then, there are databases that may need to have access to more swap space after they have been running for a few months.

Also, creating swap space enables a linux server to hibernate. Hibernation is not enabled by default on most linux distros. It is not a very useful thing for servers, because, the servers have to serve. But, hibernation is (in my experience) very useful for home labs. You dont want to shut down your linux servers as it will basically kill all the apps that you have been running. Now, imagine, you are working on 5 Linux VMs simultaneously and you are using your physical linux host for note taking, researching etc. which requires any apps to be open at the same time. Now, if you shut this host down, you will essentially have to close all the applications, then switch the PC on, then open all these apps again. BORRRIIINGGG!!! Hibernation solves this problem.

Lets take a look at the actual process of increasing swap space. The pre-requisites are:

  1. Calculate the amount of swap space that you need. For a simple need of hibernation, you need space on disk which equal to the amount of RAM thats installed on your system. My system has 16GB, so I configured 16GB of swap space. If you are increasing swap space to be used for an application, then the application document will tell you how much swap space you need. In most cases, most application vendors recommend swap to be twice that of system RAM
  2. Provide disk(s) for swap. if the required amount of disk storage is not available on the server right now, provision another disk or partition to be used as swap

Once the pre-requisites are taken care of, open a terminal to the linux server and follow this process. I did this on a Linux Mint desktop which is based on Ubuntu, but the process is largely the same for all major Linux distros.

Here are the steps:

  1. Run free-h to see current configuration. As you can see, I currently have 2GB of swap space in use which is default. Lets increase it to 16GB

? root@alderaan  ~  free -h

?total used free shared buff/cache available

Mem: 15Gi 1.4Gi 7.3Gi 14Mi 6.9Gi 13Gi

Swap: 2.0Gi 0B 2.0Gi

?? root@alderaan  ~ 
        

2. It is assumed that the system file path /swapfile is the default location of your swap file configurations. It is not recommended to work with a manually customized swap file location.<Take a system snapshot at this point using timeshift etc.>

?? root@alderaan  ~  swapon

NAME TYPE SIZE USED PRIO
        
/swapfile file 2G 0B -2        

3. The first step is to disable your Linux Mint machine's currently active swap space. Since this operation is highly privileged, you will need to be a Sudoer user or possess Sudo privileges. This article explains how to add a user to admin group.

$ swapoff -a

?
        
( Silent output :D)        

4. With no swap file currently active, you can now create and resize it to the desired swap file size. When assigning the swap file a custom size, your input preference should be in Gigabytes or GiB. For instance, if you want a swap file of 16 GB, the following command will achieve this objective. This command immediately succeeds in switching off all active swap partitions and their memory allocations.

$ SIZE=16

        
(Silent output :D)        

5. The above command will only execute successfully if you have enough system memory storage to be allocated. The following command string confirms whether your remaining system storage can handle the newly defined swap file size. The value under avail should be MUCH greater than 16G. Do not allocate all remaing space to swap. That can potentially HALT the system.

$ df / -h

?? root@alderaan  ~  df / -h

Filesystem Size Used Avail Use% Mounted on
        
/dev/sdb3 192G 43G 140G 24% /        

6. run this command to allocate /swapfile 16 GB of space. This command took 3 minutes on my system without prompting anything. This can take significantly more time, if e.g. you are allocating 768GB of swap for applications such as CyberSense.

? root@alderaan  ~  dd if=/dev/zero of=/swapfile bs=1M count=$((SIZE*1024))

?

18432+0 records in

18432+0 records out
        
19327352832 bytes (19 GB, 18 GiB) copied, 79.7732 s, 242 MB/s        
dd - convert and copy a file
if=FILE
?read from FILE instead of stdin
of=FILE
?write to FILE instead of stdout
bs=BYTES
?read and write up to BYTES bytes at a time (default: 512); overrides
?ibs and obs
ibs=BYTES
?read up to BYTES bytes at a time (default: 512)
obs=BYTES
?write BYTES bytes at a time (default: 512)
count=N
?copy only N input blocks

7. Now, we will set up a swap area in a file on a disk using mkswap. but before that, change the permission of /swapfile using chmod. The argument to mkswap can be a disk partition such as /dev/sda2 etc. or it can be a file such as /swapfile. The size parameter can be used for legacy compatibility but is best left unused. mkswap will use the entire file if size parameter is unused which is what most people want to do.?After creating the swap area, you need the swapon command to start using it. Usually swap areas are listed in /etc/fstab so that they can be taken into use at boot time by a swapon -a command in some boot script.

? root@alderaan  ~  chmod 0600 /swapfile

?? root@alderaan  ~  ls -l / | grep swap

-rw------- 1 root root 19327352832 Apr 17 16:49 swapfile

?? root@alderaan  ~ 

? root@alderaan  ~  mkswap /swapfile

Setting up swapspace version 1, size = 18 GiB (19327348736 bytes)
        
no label, UUID=61c679e1-2d83-4b92-b942-4ac2c54ce9e6        

8. To start the swapfile, run:

? root@alderaan  ~  swapon /swapfile
? root@alderaan  ~  
? root@alderaan  ~  swapon --show
NAME       TYPE  SIZE  USED  PRIO
/swapfile  file  16G   0B    -2         

9. Add /swapfile to /etc/fstab file

? root@alderaan  ~  vim /etc/fstab

# /etc/fstab: static file system information.

#

# Use 'blkid' to print the universally unique identifier for a

# device; this may be used with UUID= as a more robust way to name devices

# that works even if disks are added and removed. See fstab(5).

#

# <file system> <mount point> <type> <options> <dump> <pass>

# / was on /dev/sda3 during installation

UUID=10588dfd-f6cb-4b55-b5d9-a90e3bfd1357 / ext4 errors=remount-ro 0 1
        
/swapfile none swap sw 0 0        

10. At this point, configuration of swap space is complete. If your application needs it, you can now change the swappiness value in /etc/sysctl.conf file (vm.swappiness=60).

11. Now, lets configure the linux desktop to use hibernation

? root@alderaan  ~  RESUME_PARAMS="resume=UUID=$(findmnt / -o UUID -n) resume_offset=$(sudo filefrag -v /swapfile|awk 'NR==4{gsub(/\./,"");print $4;}') "
? root@alderaan  ~  
? root@alderaan  ~  
? root@alderaan  ~  
? root@alderaan  ~  if grep resume /etc/default/grub>/dev/null; then echo -e "\nERROR: Hibernation already configured. Remove the existing configuration from /etc/default/grub and add these parameters instead:\n$RESUME_PARAMS";else sudo sed -i "s/GRUB_CMDLINE_LINUX_DEFAULT=\"/GRUB_CMDLINE_LINUX_DEFAULT=\"$RESUME_PARAMS/" /etc/default/grub;fi
? root@alderaan  ~  
? root@alderaan  ~  
? root@alderaan  ~  
? root@alderaan  ~  update-grub
? root@alderaan  ~ 
? root@alderaan  ~ 
? root@alderaan  ~         
? root@alderaan  ~  tee /etc/polkit-1/localauthority/50-local.d/com.ubuntu.enable-hibernate.pkla <<'EOB' [Enable hibernate] Identity=unix-user:* Action=org.freedesktop.login1.hibernate;org.freedesktop.login1.handle-hibernate-key;org.freedesktop.login1;org.freedesktop.login1.hibernate-multiple-sessions ResultActive=yes EOB
        
?? ? root@alderaan  ~ ?
? root@alderaan  ~ 
? root@alderaan  ~ 
? root@alderaan  ~ 
        

12. Reboot the desktop and then run the following command:

? root@alderaan  ~  busctl call org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager CanHibernate
        
s "yes"        

13. Now, hibernation option will be available in the Linux Mint GUI as well as you can run the command systemctl hibernate to put the desktop in hibernation



要查看或添加评论,请登录

Kamal Gupta的更多文章

  • How to Install Dockers - Linux

    How to Install Dockers - Linux

    These days, the words "dockers" and "kubernetes" are very common to hear if you work in IT. Many applications are…

    2 条评论
  • Personal Cloud on Raspberry Pi using NextCloud and Ubuntu

    Personal Cloud on Raspberry Pi using NextCloud and Ubuntu

    I bought a Raspberry Pi about 8 months ago, back in december 2020. I bought it because, I wanted to learn linux and I…

  • DNS Server Config CentOS 8

    DNS Server Config CentOS 8

    1. Configure the firewall firewall-cmd --permanent --zone=public --add-service=dns firewall-cmd --reload 2.

    3 条评论

社区洞察

其他会员也浏览了