0

guys i need to enable swap on my ubuntu system and i want to avoid swap on SSD or format my hard disk that doesn't contain the swap partition. So i was trying to create a file swap and use it but, following the ubuntu guide: https://help.ubuntu.com/community/SwapFaq i'm not able to figure out the problem. First of all i can't allocate the file with fallocate because it says operation not permitted, then i create a file with dd with all zeros but after setting the right permission and after calling mkswap, the command swapon fails saying: kipping - it appears to have holes. I want to use file swap to avoid create partition on a disk with already a whole partition, and i want it on the hard disk not on the SSD. Can you help me please? those are the steps that i did

The problem is that swapon got interrupted because the file appears to have holes. It is created using dd.

To resume what is the problem: my system has no swap at all. I want to add a new one on secondary hard disk, instead of primary SSD, but i want to avoid format it and create the partition. That is why i create the swap file. But when i try to active it with the command "swapon" i receive the error: it appears to have holes.

radamirez
  • 1
  • 2
  • 1
    Hi and welcome. What version of Ubuntu? Why do you want a swap file and why not on the SSD where it would be much faster? – David Mar 27 '21 at 11:52
  • Does this answer your question? [How do I increase swapfile in Ubuntu 18.04?](https://askubuntu.com/questions/1075505/how-do-i-increase-swapfile-in-ubuntu-18-04) – vanadium Mar 27 '21 at 11:57
  • 1
    Hi David, i'm using ubuntu 18.04. I want swap on hard disk because SSD will be saturated quite soon if i swap upon it because a huge amount of write op can decrease its life. – radamirez Mar 27 '21 at 12:31
  • Hi vanadium i already follow that instruction but i receive the holes error. I'm using dd command to create the swap file. – radamirez Mar 27 '21 at 12:36
  • 3
    You must `sudo fallocate ...`. This will grab all the disk blocks in one contiguous block. Creating the file with `dd` allocates disk blocks a few at a time, with no guarantee of contiguous groups of blocks, thus, the complaint about "holes". – waltinator Mar 27 '21 at 17:29
  • Hi, this is different of what the tutorial says but i tried fallocate, but it says operation not permitted – radamirez Mar 27 '21 at 18:41
  • 1
    Welcome to AskUbuntu! I believe that this is the answer you require. https://askubuntu.com/a/796997/225694 you'll note that sudo is required for all the commands in the answer that create and implement the swap. – Elder Geek Mar 27 '21 at 22:31
  • Does this answer your question? [How do I add swap after system installation?](https://askubuntu.com/questions/33697/how-do-i-add-swap-after-system-installation) – Elder Geek Mar 27 '21 at 22:32
  • No that question regard the partition swap, i want to enable a file to swap – radamirez Mar 27 '21 at 22:50

1 Answers1

1

To Enable Hibernation in 20.04 Using a Swapfile:

I used this method yesterday to increase my swapfile size and enable hibernation, (optional). Last paragraph is how I search for holes.

Increase swapfile size to match RAM size up to 8GB.

  • Check the swap that is in use:

    sudo swapon -s
    
  • If swap partition(s) are found:

    sudo swapoff -a
    sudo nano -Bw /etc/fstab
    
  • Add # before the UUID of the swap partition(s):

    # UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX   none   swap    sw     0       0
    
  • Add a line for the swapfile, if one does not exist:

    swapfile   none    swap     sw      0       0
    
  • Create the swapfile:

    sudo fallocate -l XG /swapfile*
    

    where X is swapfile's size in GB:

    sudo mkswap /swapfile
    sudo chmod 0600 /swapfile
    sudo swapon /swapfile
    
  • Reboot:

    sudo reboot
    

Add resume location and offset to grub.cfg:

  • Edit /etc/default/grub:

    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX resume_offset=XXXXX"
    
  • Use UUID from root.

  • Use offset from:

    sudo filefrag -v /swapfile |grep " 0:"| awk '{print $4}'
    
  • Update GRUB:

    sudo update-grub
    
  • Test hibernation:

    sudo systemctl hibernate
    

A hibernate button can be added using GNOME extensions.

Note that there is a slight possibility of getting holes in a swapfile when creating it with fallocate. /var/log/syslog can be searched for the phrase swapon: swapfile has holes to ensure there will be no data loss.

C.S.Cameron
  • 18,890
  • 10
  • 64
  • 105
  • Thanks but those are just the step that i did. i just want to figure out the error. – radamirez Mar 30 '21 at 10:26
  • Did I read that your system has a swap partition and a swapfile? – C.S.Cameron Mar 30 '21 at 13:30
  • No my system has no swap at all. I want to add a new one on secondary hard disk, but i want to avoid format it and create partition. That is why i create the swap file. But when i try to active it with the command swapon i receive the error: it appears to have holes. – radamirez Mar 31 '21 at 10:17
  • Are you creating it on the SSD and then moving it? I missed why you are not just adding a swap partition to the HDD? That would be easy. – C.S.Cameron Mar 31 '21 at 12:31
  • I already tried to create first on SSD and then move it on HDD but the same error happen. I know that is easy but doesn't figure out the holes problem. – radamirez Apr 01 '21 at 13:13