How to Allocate More Space to Swap and Increase its Size Greater than Ram?

11,685

Solution 1

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

Solution 2

You question, "How to Allocate More Space to Swap and Increase its Size Greater than Ram?", does not say anything about changing the way your system is set up.

Your fdisk and free output tells us:

  • You have a a partition (/dev/sda3) dedicated to swap. If you just resize that partition you should not need to change anything in your system to use the extra space. (/etc/fstab).
  • You have a partition (/dev/sda2) that has the OS on it. This partition has 26G free space. If you shrink this partition by 20G the OS will have 5G left to grow and use.

The swap partition is not a extended partition, this makes it a little bit easier to increase the size.

Tools required:

  • Bootable OS with gparted

Look here for one location of the last free version of Parted Magic

Instruction for resizing partitions with gparted

  1. boot into another OS that has gparted tool
  2. In gparted; resize OS partition (/dev/sda2). Shrink by amount to donate to swap space. This could take a lot of time because all of the data that is in the space being freed up will need to be moved by gparted.
  3. In gparted; resize swap partition (/dev/sda3). Move and extend to include all free space.
  4. reboot into sda2 OS.
  5. verify swap partition (/dev/sda3) is being used.

Remember that you are changing you disk and partitions on a low level and that any errors or crashes could damage you data.

Backup you data first.

Share:
11,685

Related videos on Youtube

Léo Léopold Hertz 준영
Author by

Léo Léopold Hertz 준영

Updated on September 18, 2022

Comments

  • Léo Léopold Hertz 준영
    Léo Léopold Hertz 준영 almost 2 years

    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

    • Admin
      Admin almost 8 years
      Output of fdisk -lu /dev/sda?
    • Admin
      Admin almost 8 years
      Options are shrinking fs on /dev/sda2 and repartition (but that can't be done from the live system as you can't unmount /dev/sda2 as that's the root filesystem: boot on some livecd is the easiest), or use a swap file as opposed to block device, or use compressed swap or compressed ram
    • Admin
      Admin almost 8 years
      Few of those placement considerations apply to SSD:s, as they don't have heads or tracks like spinning disks. Striping over multiple drives might apply, if you have a huge I/O load.
  • Lmwangi
    Lmwangi almost 8 years
    Yep, you should be able to compute. Swapper should see a large virtual memory.
  • roaima
    roaima almost 8 years
    @masi if you add an entry in /etc/fstab you can avoid the entries in rc.local. Put the swap file somewhere like /mnt/.swap and you'll not need to worry about it too much any more
  • Admin
    Admin almost 8 years
    @Masi no, fallocate is just easire, no need to put zeros and set count via dd since you might do it wrong if you don't know what you are doing, dd is a super-powerfull command. And AFAIK Swap space in Linux is used when the amount of physical memory (RAM) is full, and be aware that is has no mount point. So I think it doesn't matter where it is, just chmod it to 600.
  • Admin
    Admin almost 8 years
    @Masi my pleasure. BTW, you can read about fields in fstab by running man 5 fstab. Good Luck mate.
  • Léo Léopold Hertz 준영
    Léo Léopold Hertz 준영 almost 8 years
    I think you do not need gparted here because you can put swab in the same disk where your OS is.
  • jc__
    jc__ almost 8 years
    True, but then you would have 2 swap. File on /sda2 and partition on /sda3. This answers the part of the question that asked "Allocate more disk space for Swap in /dev/sda3. How to take it from /dev/sda2?".
  • jc__
    jc__ almost 8 years
    In reference to the statement: "However, not sure because I think /dev/sda2 and /dev/sad3 are on the same disk partition". /dev/sda2 and /dev/sda3 are separate partitions.
  • jc__
    jc__ almost 8 years
    So you are adding another device that has space on it you want to use for swap? If yes. You could create a new partition of type swap on it and when plugged in and ready to use do a swapon or similar. On device removal do a swapoff or similar. Device swap status will not be remembered on OS reboot.
  • jc__
    jc__ almost 8 years
    Or create an ext type partition on new device, create the swap file and a script to turn on swap file with required settings and a script to turn off swap file. This would allow you to only use the extra swap space when needed.
  • jc__
    jc__ almost 8 years
    After you create the partition you can format it with mkfs. After that mount it on the running OS.
  • Léo Léopold Hertz 준영
    Léo Léopold Hertz 준영 almost 8 years
    How is mkfs different from chattr?
  • jc__
    jc__ almost 8 years
    mkfs will format a partition with a file system. (vfat, ext2, ext4, ntfs). chattr will change the file attributes of a file on an ext file system.
  • Admin
    Admin almost 8 years
    @Masi yes of course. You can list file attributes by lsattr, and as I mentioned you can set them by chattr. For instance, if you wanna make it immutable, you can use chattr +i /mnt/.swapfile, or in BSD or OS X you can use chflags uchg yourFile. You have many attribute options, for instance, when a file with s attribute set is deleted, its blocks will be zeroed and written back to the disk. But I recommend you read its manual page and surf the net about. It's not as easy as it seems to use chattr, and you should be aware of what you are really going to do!
  • jc__
    jc__ almost 8 years
    NOTE: when creating a swap type partition it does not need to be formated, but most others must be formated with the file system. Gparted will format at the same time it creates the partition so no further action required.
  • Admin
    Admin almost 8 years
    @Masi AFAIK setting attribute is up to your needs, so the answer is no for the majority systems. But you can find some Unix-Like operating systems having some files or directories with immutable or restricted attributes set by default, for Instance, MacOS X.
  • jc__
    jc__ almost 8 years
    It is true that you do not need to use mkfs or chattr if you use the same partition and resize it as described in this answer. If you create a swap file on the same partition as your OS, No mkfs is not needed because there is already a file system. chattr; I am not familiar with.
  • Admin
    Admin almost 8 years
    @Masi in fact, there is no need of doing it by default, so I think the answer is no, since Swapfiles are most likely used temporarily not permanently. If you are using LVM partitions it's better to increase your swap partition, instead of making a swapfile. But if you cannot do that and swapfiles are the only option, there is no problem, but it will affect your system and does slow its performance. There is no rule-book, everything depends on your needs, and what you can do for it, and how secure you want your system to be.