Dietpi: Unable to generate a working swap

Created on 3 Apr 2018  ยท  7Comments  ยท  Source: MichaIng/DietPi

Creating a bug report/issue:

I cant seem to create a swap , i have tried manually creating one using fallocate and then mkswap and swapon, but when i enter the swapon /var/swap command it gives me an error saying no such file. I have already made the swap and used chmod 600 to make it r/w . The swapfile is there , but the system will not allow me to use it as a swap.. What am I doing wrong ?

Required Information:

  • DietPi Version | cat /DietPi/dietpi/.version?
  • RPI 3b+
    -3.5 amp usb (Dell branded)
  • SanDisk Extreme Plus
  • Stretch

Expected behaviour:

trying to create a swap , but it keeps on failing.

Actual behaviour:

DietPi-Swapfile: swapon /var/swap

Extra details:

Clean install, nothing has been installed yet.

Bug Solution available

Most helpful comment

๐Ÿˆฏ๏ธ

[ INFO ] DietPi-Swapfile | Setting /tmp tmpfs size: 1990M
[  OK  ] DietPi-Swapfile | mount -o remount,size=1990M tmpfs /tmp
[  OK  ] DietPi-Swapfile | swapoff -a
[ INFO ] DietPi-Swapfile | Deleting existing swapfile (/mnt/63864066-5558-408d-be95-1af911c94bb9/.swapfile)
[ SUB1 ] DietPi-Swapfile > Generating new swapfile
[ INFO ] DietPi-Swapfile | Size     = 99 MB
[ INFO ] DietPi-Swapfile | Location = /mnt/63864066-5558-408d-be95-1af911c94bb9/.swapfile
[  OK  ] DietPi-Swapfile | dd if=/dev/zero of=/mnt/63864066-5558-408d-be95-1af911c94bb9/.swapfile bs=4K count=25344
         DietPi-Swapfile | mkswap /mnt/63864066-5558-408d-be95-1af911c94bb9/.swa[  OK  ] DietPi-Swapfile | mkswap /mnt/63864066-5558-408d-be95-1af911c94bb9/.swapfile
         DietPi-Swapfile | swapon /mnt/63864066-5558-408d-be95-1af911c94bb9/.swa[  OK  ] DietPi-Swapfile | swapon /mnt/63864066-5558-408d-be95-1af911c94bb9/.swapfile
[ INFO ] DietPi-Swapfile | Setting /tmp tmpfs size: 1990M
[  OK  ] DietPi-Swapfile | mount -o remount,size=1990M tmpfs /tmp

All 7 comments

Nvm , i figured out it was because i formatted mmcblk0p2 to f2fs and that was causing the issue . Is there a reason why f2fs wont work with swap ?

@mgherghi
Just a quick search: https://wiki.archlinux.org/index.php/swap

# fallocate -l 512M /swapfile
Note: fallocate may cause problems with some file systems such as F2FS or XFS.[1] As an alternative, using dd is more reliable, but slower:
# dd if=/dev/zero of=/swapfile bs=1M count=512

We use fallocate within our script: https://github.com/Fourdee/DietPi/blob/master/dietpi/func/dietpi-set_dphys-swapfile#L114

@Fourdee
Do we want to provide wider filesystem support by adding fs check and use dd in case, as suggested within link above?

Just to be sure, we would need to test this again, if it is really (just) the fallocate that causes this issue, as lack of log/output so far ๐Ÿ˜‰.


Manual test on f2fs USB stick:

root@DietPi:~# l /mnt/sdb
total 45108
-rw------- 1 root root 46137344 Apr  3 10:41 .swapfile
root@DietPi:~# fallocate -l 100M /mnt/sdb/.swapfile
root@DietPi:~# mkswap /mnt/sdb/.swapfile
mkswap: /mnt/sdb/.swapfile: warning: wiping old swap signature.
Setting up swapspace version 1, size = 100 MiB (104853504 bytes)
no label, UUID=f90071bc-b8b2-47c8-a292-263d20eaa9cf
root@DietPi:~# chmod 600 /mnt/sdb/.swapfile
root@DietPi:~# swapon /mnt/sdb/.swapfile
swapon: /mnt/sdb/.swapfile: swapon failed: Invalid argument
root@DietPi:~# l /mnt/sdb
total 102508
-rw------- 1 root root 104857600 Apr  3 11:05 .swapfile
root@DietPi:~# swapon -v /mnt/sdb/.swapfile
swapon: /mnt/sdb/.swapfile: found signature [pagesize=4096, signature=swap]
swapon: /mnt/sdb/.swapfile: pagesize=4096, swapsize=104857600, devsize=104857600
swapon /mnt/sdb/.swapfile
swapon: /mnt/sdb/.swapfile: swapon failed: Invalid argument

Apr 03 11:09:50 DietPi kernel: swapon: swapfile has holes

root@DietPi:~# dd if=/dev/zero of=/mnt/sdb/.swapfile bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.353495 s, 297 MB/s
root@DietPi:~# mkswap /mnt/sdb/.swapfile
Setting up swapspace version 1, size = 100 MiB (104853504 bytes)
no label, UUID=d3448ba6-448d-4f7b-bdf2-12486b1f5f6e
root@DietPi:~# chmod 600 /mnt/sdb/.swapfile
root@DietPi:~# swapon /mnt/sdb/.swapfile
root@DietPi:~# swapon -s
Filename                                Type            Size    Used    Priority
/mnt/sdb/.swapfile                      file            102396  0       -1

๐Ÿˆฏ๏ธ Indeed fallocate is the problem, even that it does not show any error. Replacing with dd works fine.
โ‚ฌ: Ah just see the commend with this already in place ๐Ÿ˜†: https://github.com/Fourdee/DietPi/blob/master/dietpi/func/dietpi-set_dphys-swapfile#L113

@mgherghi
Could you try the steps as well, just to be sure (here 100M swapfile just for testing, adjust to your desired value)?

  1. dd if=/dev/zero of=/var/swap bs=1M count=100
  2. mkswap /var/swap
  3. chmod 600 /var/swap
  4. swapon /var/swap

Then use swapon -s or htop to verify that your swapfile is in use.
To make this permanent, check if the entry is already inside /etc/fstab, otherwise add:
/var/swap none swap sw 0 0

@MichaIng

Do we want to provide wider filesystem support by adding fs check and use dd in case, as suggested within link above?

Yep ๐Ÿ‘

We can use following to detect filesystem type for the target swap location:

root@DietPi:~# stat --file-system --format=%T /tmp
tmpfs
root@DietPi:~# stat --file-system --format=%T /var
ext2/ext3

Or this seems better:

root@DietPi:~# findmnt -n -o FSTYPE -T /var/tmp
ext4

https://github.com/Fourdee/DietPi/commit/1c4b7d94fa5b7d95f331ca875ba237b6274b5aed should do it. Once i've setup a test system i'll verify.

๐Ÿˆฏ๏ธ

[ INFO ] DietPi-Swapfile | Setting /tmp tmpfs size: 1990M
[  OK  ] DietPi-Swapfile | mount -o remount,size=1990M tmpfs /tmp
[  OK  ] DietPi-Swapfile | swapoff -a
[ INFO ] DietPi-Swapfile | Deleting existing swapfile (/mnt/63864066-5558-408d-be95-1af911c94bb9/.swapfile)
[ SUB1 ] DietPi-Swapfile > Generating new swapfile
[ INFO ] DietPi-Swapfile | Size     = 99 MB
[ INFO ] DietPi-Swapfile | Location = /mnt/63864066-5558-408d-be95-1af911c94bb9/.swapfile
[  OK  ] DietPi-Swapfile | dd if=/dev/zero of=/mnt/63864066-5558-408d-be95-1af911c94bb9/.swapfile bs=4K count=25344
         DietPi-Swapfile | mkswap /mnt/63864066-5558-408d-be95-1af911c94bb9/.swa[  OK  ] DietPi-Swapfile | mkswap /mnt/63864066-5558-408d-be95-1af911c94bb9/.swapfile
         DietPi-Swapfile | swapon /mnt/63864066-5558-408d-be95-1af911c94bb9/.swa[  OK  ] DietPi-Swapfile | swapon /mnt/63864066-5558-408d-be95-1af911c94bb9/.swapfile
[ INFO ] DietPi-Swapfile | Setting /tmp tmpfs size: 1990M
[  OK  ] DietPi-Swapfile | mount -o remount,size=1990M tmpfs /tmp

Resolved for v6.7.

@MichaIng Great job on finding the cause ๐Ÿ‘

I have the same issue with an ntfs filessystem

DietPi-Swapfile: fallocate -l 1072M /mnt/2C..CC/dietpi_userdate/.swapfile
Exit code: 1
DietPi version: v6.7 | HW_MODEL:3 | HW_ARCH:2 | DISTRO: 4
fallocate: fallocate failed: Operations not supported

root@DietPi:/# findmnt -n -o FSTYPE -T /mnt/2C..CC/
fuseblk
root@DietPi:/#

un-commenting the if clause for fallocate and just use dd in dietpi-set_dphys-swapfile
appears to work fine

#Pre-allocate for filesystems which do no support quick-allocate with fallocate
#if [ "$fs_type" = 'f2fs' ] || [ "$fs_type" = 'xfs' ]; then
G_RUN_CMD dd if=/dev/zero of=$SWAP_LOCATION bs=4K count=$(( $SWAP_SIZE * 1024 / 4 ))
#else
#G_RUN_CMD fallocate -l $SWAP_SIZE'M' $SWAP_LOCATION
#fi

Was this page helpful?
0 / 5 - 0 ratings

Related issues

and09 picture and09  ยท  3Comments

oshank picture oshank  ยท  3Comments

k-plan picture k-plan  ยท  3Comments

pfeerick picture pfeerick  ยท  3Comments

Fourdee picture Fourdee  ยท  3Comments