2

I'm having Swap partition of 8 GB , how can i create a swap file from it ? Currently having three partitions root , home , swap . So if i create a swap file how do i create it and where do it get saved ? My RAM is about 4 GB.

  • You don't create really create a Swapfile from a swap partition. Do you mean you want to both remove the swap Partition to add the space it occupies space to another partition with regular filesystem and also create a swapfile? What do you hope to gain? – gmatht Jun 14 '18 at 13:55

1 Answers1

3

Generally, a swap partition is preferred to a swap file; however, you can use a swap file instead.
The swap file will be saved where you put it.
Assuming that your home partition has the most space it can be put there.

first make the swap file:

sudo dd if=/dev/zero of=/home/swapfile bs=4096 count=2M  
sudo mkswap /home/swapfile  
sudo swapon /home/swapfile

you now have an 8G swapfile.
to make it automatically active you need to edit /etc/fstab.
add the following liine:

/home/swapfile  none  swap  sw 0 0

if you no longer want the swap partition remove the line that looks like:

UUID=########## none  swap  sw  0  0
ravery
  • 6,874
  • So can i use that swap file with other distro ? Life swap partition can be used ? – Himanshu Verkiya Jul 10 '17 at 07:57
  • it will only be available to the system it is installed on. you will have to repeat the process for other installations – ravery Jul 10 '17 at 08:04
  • So , is swap partition a better option than swap file in case of multiple distros ? – Himanshu Verkiya Jul 10 '17 at 08:15
  • yes, then all distros can use the same swap. a note of caution. if you hibernate one distro. opening the swap with another may corrupt the hibernated one because the swap will be flushed – ravery Jul 10 '17 at 08:18
  • Your second command should spell mkswap instead of makeswap, isn't it? At least that's what worked for me on Kubuntu 18.04. – Neinstein Aug 22 '18 at 17:24
  • 1
    @Neinstein -- I thnk both were valid when i posted the answer, but I edited it to the more common command as you noted. – ravery Aug 22 '18 at 21:15
  • Technically, you can use swap file if you mount the partition where that file was created. However, a dedicated partition is better to share between multiple installations. If you want to simplify the process, I've a script for creating swap files, https://askubuntu.com/a/931170/295286 It basically automates all the same steps. – Sergiy Kolodyazhnyy Aug 22 '18 at 22:17
  • @ravery Fair enough. Maybe it's changed since, or it is valid only on certain systems. Apparently Kubuntu doesn't recognize it at the moment. – Neinstein Aug 23 '18 at 09:11