-1

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.

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
  • 19,519
  • 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