suphesiz
Posts: 5
Joined: Sun Dec 16, 2018 9:36 am

swap file on usb -=problems&thoughts=-

Sun Dec 16, 2018 9:42 am

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

hommar
Posts: 172
Joined: Sat Mar 25, 2017 1:55 pm
Location: Russia, Yekaterinburg

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

Sun Dec 16, 2018 2:11 pm

$ man man

$ man if

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

suphesiz
Posts: 5
Joined: Sun Dec 16, 2018 9:36 am

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

Sun Dec 16, 2018 3:59 pm

where should I use this if statement ?

small example appreciated.

User avatar
thagrol
Posts: 1777
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

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

Sun Dec 16, 2018 5:06 pm

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.
This space unintentionally left blank.

epoch1970
Posts: 3564
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

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

Sun Dec 16, 2018 6:01 pm

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.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

suphesiz
Posts: 5
Joined: Sun Dec 16, 2018 9:36 am

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

Sun Dec 16, 2018 6:57 pm

I will try both and write the results here.

Many thanks..

hommar
Posts: 172
Joined: Sat Mar 25, 2017 1:55 pm
Location: Russia, Yekaterinburg

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

Sun Dec 16, 2018 8:04 pm

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

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

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

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
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
thagrol
Posts: 1777
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

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

Sun Dec 16, 2018 11:32 pm

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.
This space unintentionally left blank.

suphesiz
Posts: 5
Joined: Sun Dec 16, 2018 9:36 am

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

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

User avatar
thagrol
Posts: 1777
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

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

Mon Dec 17, 2018 1:08 pm

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.
This space unintentionally left blank.

chwe
Posts: 116
Joined: Tue Jul 31, 2018 1:35 pm

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

Tue Dec 18, 2018 12:18 am

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.

Return to “General discussion”