Linux – Allocate More Space to Swap and Increase Size Greater than RAM

linuxlinux-kernelswap

Situation: increase swap size (/dev/sda3) greater than Ram (8 GB) when HD 128 GB
Motivation: 8 GB RAM is too little; 30 GB free space in my SSD; I want to turn 20 GB to SSD swap
Characteristics of system

  • Swap non-immutable/changeable. I cannot find any evidence why /mnt/.swapfile should be immutable so you do not need the change the file attributes of the swapfile

    sudo lsattr /mnt/.swapfile 
    -------------e-- /mnt/.swapfile
    
  • Command sudo fdisk -lu /dev/sda gives

    Disk /dev/sda: 113 GiB, 121332826112 bytes, 236978176 sectors 
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 4096 bytes
    I/O size (minimum/optimal): 4096 bytes / 4096 bytes
    Disklabel type: gpt
    Disk identifier: 082F85CA-EE3E-479C-8244-858B196FA5BA
    
    Device         Start       End   Sectors   Size Type
    /dev/sda1       2048      4095      2048     1M BIOS boot
    /dev/sda2       4096 220323839 220319744 105.1G Linux filesystem
    /dev/sda3  220323840 236976127  16652288     8G Linux swap
    
  • Command df -h gives

    Filesystem      Size  Used Avail Use% Mounted on
    udev            3.9G     0  3.9G   0% /dev
    tmpfs           793M  9.4M  784M   2% /run
    /dev/sda2       104G   74G   25G  75% /
    tmpfs           3.9G   54M  3.9G   2% /dev/shm
    tmpfs           5.0M  4.0K  5.0M   1% /run/lock
    tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
    tmpfs           793M   64K  793M   1% /run/user/1000
    
  • Allocate more disk space for Swap in /dev/sda3.

My unsuccessful workflow for the task when HD and Swap on the same partition, /dev/sda3

masi@masi:~$ sudo -i

root@masi:~# swapoff /dev/sda3

root@masi:~# swapon
[blank]    

root@masi:~# dd if=/dev/zero of=/dev/sda3 bs=20480 count=1M
dd: error writing '/dev/sda3': No space left on device
416308+0 records in
416307+0 records out
8525971456 bytes (8.5 GB, 7.9 GiB) copied, 18.7633 s, 454 MB/s

root@masi:~# mkswap /dev/sda3
Setting up swapspace version 1, size = 8 GiB (8525967360 bytes)
no label, UUID=245cb42c-1d4e-4e21-b544-16b64af962d6

root@masi:~# swapon -p 99 /dev/sda3

root@masi:~# swapon
NAME       TYPE      SIZE USED PRIO
/dev/sda3  partition   8G   0B   99

root@masi:~# vi /etc/fstab 
...

HD and Swap on same Partition – Current Workflow [Ijaz, cas, FarazX]

Merging. Use fallocate at the beginning instead dd because no need to put zeros

masi@masi:~$ sudo fallocate -l 20G /mnt/.swapfile

masi@masi:~$ sudo mkswap /mnt/.swapfile 
Setting up swapspace version 1, size = 20 GiB (21474832384 bytes)
no label, UUID=45df9e48-1760-47e8-84d7-7a14f56bbd72

masi@masi:~$ sudo swapon /mnt/.swapfile
swapon: /mnt/.swapfile: insecure permissions 0644, 0600 suggested.

masi@masi:~$ sudo chmod 600 /mnt/.swapfile

masi@masi:~$ free -m
              total        used        free      shared  buff/cache   available
Mem:           7925        1494         175         196        6255        5892
Swap:         28610           0       28610

Add the following line in your /etc/fstab which is better than adding the thing to your runlevels (/etc/rc.local), where I put the swapfile to the /mnt/.swapfile to maintain Linux/Unix philosophy and maintain the integrity of my system backup scripts; If swapping to an SSD, use the discard option so that the blocks are trimmed on every reboot, so not sw

# http://unix.stackexchange.com/a/298212/16920
# http://unix.stackexchange.com/a/298543/16920

# If swap is on SSD, trim blocks each time at startup.
/mnt/.swapfile  none    swap    defaults,discard      0        0

# If swap on External HDD, just use sw.
#/media/masi/SamiWeek/.swapfile  none    swap    sw      0        0

Sources

  1. How to increase swap space? https://askubuntu.com/a/178726/25388 General discussion about increasing swap space for beginners.
  2. Linux Partition HOWTO for HDDs, not SSDs: 4. Partitioning requirements. http://www.tldp.org/HOWTO/Partition/requirements.html So do not put your swap to outer tracks on SSDs but use defaults,discard options to trim your blocks as proposed by @cas.

System: Linux Ubuntu 16.04 64 bit
Linux kernel: 4.6
Linux modules: wl
Hardware: Macbook Air 2013-mid
Ram: 8 GB
SSD: 128 GB

Best Answer

You just want to increase the swap size on your system using the space from sda2. Your sda2

/dev/sda2       104G   74G   25G  75%  / 

You can add additional swap space to your system by using swap file created on / that will utilize your sda2. Just do:

dd if=/dev/zero of=/swapfile bs=20480 count=1M

and then do:

sudo mkswap /swapfile  
sudo swapon /swapfile 

and check, you swap space will increase by that amount using free -m

and yes , to enable it at boot time add the entry in /etc/fstab

 /swapfile     none     swap     sw     0     0
Related Question