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 ?
cat /DietPi/dietpi/.version?trying to create a swap , but it keeps on failing.
DietPi-Swapfile: swapon /var/swap
Clean install, nothing has been installed yet.
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)?
dd if=/dev/zero of=/var/swap bs=1M count=100mkswap /var/swapchmod 600 /var/swapswapon /var/swapThen 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
Most helpful comment
๐ฏ๏ธ