Page 1 of 1

swap file on usb -=problems&thoughts=-

Posted: Sun Dec 16, 2018 9:42 am
by suphesiz
Hi,

I created swap file on the usb stick and all gone well. (ie /media/pi/usb_01/swap)

if usb stick is not connected Raspbery pi dont boot and stcuk due to cant find/create swap file.

What if we lost or unusable usb stick which is configured as swap destination ? Is it means we cant boot raspi ?

How we can make multiply swap location or decide to where to put at boot time ?

is there any pracitcle way to avoid such usb problem ?

if we lost the usb , is it a solution to format a new usb with the same name to use instead of the original one?

best

Re: swap file on usb -=problems&thoughts=-

Posted: Sun Dec 16, 2018 2:11 pm
by hommar
$ man man

$ man if

PS https://linux.die.net/man/1/if

Re: swap file on usb -=problems&thoughts=-

Posted: Sun Dec 16, 2018 3:59 pm
by suphesiz
where should I use this if statement ?

small example appreciated.

Re: swap file on usb -=problems&thoughts=-

Posted: Sun Dec 16, 2018 5:06 pm
by thagrol
Not sure if the "nofail" mount option is valid for swap files but it might be worth trying.

Assuming you have both the USB stick and the swap file in /etc/fstab, add ",nofail" to their mount options (without the "")

As to your other questions, that very much depends on how you're mounting the sticks (they have to be mounted before you can have a swap file on them)
What if we lost or unusable usb stick which is configured as swap destination ? Is it means we cant boot raspi ?
Probably, but it depnds on mount options.
if we lost the usb , is it a solution to format a new usb with the same name to use instead of the original one?
Probably not. It very much depends on how you're mounting it and how many USB storage devices you have connected.

Re: swap file on usb -=problems&thoughts=-

Posted: Sun Dec 16, 2018 6:01 pm
by epoch1970
Systemd barely supports the "legacy" /etc/fstab file, you could:
- see if nofail helps
- create systemd ".swap" units (e.g. https://coreos.com/os/docs/latest/adding-swap.html)
- do as hommar alluded to: remove the swap declarations from fstab, run a script at boot. In your script check the situation and run "swapon <this or that>" accordingly.

Re: swap file on usb -=problems&thoughts=-

Posted: Sun Dec 16, 2018 6:57 pm
by suphesiz
I will try both and write the results here.

Many thanks..

Re: swap file on usb -=problems&thoughts=-

Posted: Sun Dec 16, 2018 8:04 pm
by hommar
suphesiz wrote:
Sun Dec 16, 2018 3:59 pm
small example appreciated.
Example from man page

Code: Select all

if test -f /var/swap.bin
    echo swap.bin exists 
else
    echo swap.bin does not exist 
end
or

Code: Select all

if test -f /dev/sdx4
    echo device sdx4 exists 
else
    echo device sdx4 does not exist 
end
or

Code: Select all

#!/bin/bash
dir_name="/var"
if [ `mount | grep "$dir_name"` ]; then
echo "OK"
else
echo "Alarm!!! The directory $dir_name is not mounted!"
fi
or

Code: Select all

#!/bin/bash
dev_name="/dev/sdx4"
if [ `mount | grep "$dev_name"` ]; then
echo "OK"
else
echo "Alarm!!! The device $dev_name is not mounted!"
fi
PS i do not test it becouse have no time now

Re: swap file on usb -=problems&thoughts=-

Posted: Sun Dec 16, 2018 10:46 pm
by n67
Here's how I do it - I haven't actually tested what happens if you boot without the flash drive inserted, but I think it will do the right thing.

1) Disable the dphys-swapfile stuff (there's some systemctl command for that)
2) Create a swap partition (2G+ size) on your USB drive (flash drive). Figure out the UUID of it from lsblk.
3) In /etc/fstab, put:

UUID=Your-UID-String-Very_Long_goes_here none swap nofail,sw 0 0

Re: swap file on usb -=problems&thoughts=-

Posted: Sun Dec 16, 2018 11:32 pm
by thagrol
n67 wrote:
Sun Dec 16, 2018 10:46 pm
Here's how I do it - I haven't actually tested what happens if you boot without the flash drive inserted, but I think it will do the right thing.

1) Disable the dphys-swapfile stuff (there's some systemctl command for that)
2) Create a swap partition (2G+ size) on your USB drive (flash drive). Figure out the UUID of it from lsblk.
3) In /etc/fstab, put:

UUID=Your-UID-String-Very_Long_goes_here none swap nofail,sw 0 0
Which is fine, except the OP specifically mentioned a swap file, and won't help if the USB device has to be replaced.

Re: swap file on usb -=problems&thoughts=-

Posted: Mon Dec 17, 2018 11:26 am
by suphesiz
Yes there should be a practical and viable solution what if we lost or have a damaged usb stick which we mount swap file on it. ? How we replace it ?

I had a bad experience usb stick damaged and I coulnt boot raspi ...

thx

Re: swap file on usb -=problems&thoughts=-

Posted: Mon Dec 17, 2018 1:08 pm
by thagrol
suphesiz wrote:
Mon Dec 17, 2018 11:26 am
Yes there should be a practical and viable solution what if we lost or have a damaged usb stick which we mount swap file on it. ? How we replace it ?

I had a bad experience usb stick damaged and I coulnt boot raspi ...

thx
The "nofail" option should allow boot to continue if the USB stick isn't found. I don't know if it applies to swap files/partitions but the swapon command has a "--ifexists" option to "Silently skip devices that do not exist." so it may.

Try this:
Give the partition on your USB stick a unique LABEL
Mount the stick by that LABEL e.g. (in fstab):

Code: Select all

LABEL="swapstick" /path/to/mount_point auto defaults,nofail 0 0
Remove or comment out any swap entry in fstab.
Add the following to root's crontab:

Code: Select all

@reboot swapon --ifexists /pat/to/swap_file

If/when the stick fails, give the replacement the same LABEL, create a swap file on it and you should be good to go.

I have not tested this, I don't use swap on any of my Pi

Normally, I'd advise mounting by UUID or PARTUUID rather than LABEL but in your case that would complicate replacing the USB stick. Provided the LABEL you use is unique, it won't matter which USB port you plug it into nor whether you have more than one USB storage device connected.

Re: swap file on usb -=problems&thoughts=-

Posted: Tue Dec 18, 2018 12:18 am
by chwe
swap on usb disk might be a bad decision.. Swap needs to be on the fastest possible storage you have (especially in small blocks). All USB sticks I ever used performed badly when it comes to randomIO performance with small blocks (compared to good SD-cards - good means A1 rated cards). To be honest, I always go for cheap USB-sticks, not sure if expensive ones perform better. I would also assume that the USB overhead isn't perfect for swap on USB but that's more guessing I never tested it.

If you want to reduce the wear-out of your SD-Card due to heavy swapping you might also consider zram https://en.wikipedia.org/wiki/Zram. It's worth a try.