he's dead jim
Posts: 27
Joined: Wed Dec 09, 2015 3:35 am

How Do I Create A Swap File On A Flash Drive?

Thu Jun 08, 2017 2:24 pm

First off I would like to say that I have googled this question and I came up with several hundred answers, all centered around the drawbacks of such a thing and trying to convince the user not to do this.

Ok, we get it. it can shorten the live of the flash and all that.

So please let's not turn this thread into one of those. After all, this is a board to experiment on, and if some of us choose to use swap files as part of those experiments, I think we should have that right.

So if anyone can provide a step by step explanation of how to do it, I would greatly appreciate it.

There was a very nice example that i found among the trash talk, but it was a few years old and does not work anymore.

I have already turned off the swap on the SD card. I wiped and reformatted the flash drive from the PI.

I just need to turn that flash into one big happy swap and i can get on with my life.

Thanks :)

Ernst
Posts: 1215
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: How Do I Create A Swap File On A Flash Drive?

Thu Jun 08, 2017 2:42 pm

To be honest: I don't know because I have not tried it yet.
But the answer could be very simple, this is the contents of /etc/dphys-swapfile from a Pi using an SSD instead of SD.

Code: Select all

# /etc/dphys-swapfile - user settings for dphys-swapfile package
# author Neil Franklin, last modification 2010.05.05
# copyright ETH Zuerich Physics Departement
#   use under either modified/non-advertising BSD or GPL license

# this file is sourced with . so full normal sh syntax applies

# the default settings are added as commented out CONF_*=* lines


# where we want the swapfile to be, this is the default
#CONF_SWAPFILE=/var/swap

# set size to absolute value, leaving empty (default) then uses computed value
#   you most likely don't want this, unless you have an special disk situation
CONF_SWAPSIZE=2048

# set size to computed value, this times RAM size, dynamically adapts,
#   guarantees that there is enough swap without wasting disk space on excess
#CONF_SWAPFACTOR=2

# restrict size (computed and absolute!) to maximally this limit
#   can be set to empty for no limit, but beware of filled partitions!
#   this is/was a (outdated?) 32bit kernel limit (in MBytes), do not overrun it
#   but is also sensible on 64bit to prevent filling /var or even / partition
#CONF_MAXSWAP=2048
It could be as simple as changing the line:

Code: Select all

#CONF_SWAPFILE=/var/swap
to the location where the flash drive is mounted.
The road to insanity is paved with static ip addresses

User avatar
rpdom
Posts: 14994
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How Do I Create A Swap File On A Flash Drive?

Thu Jun 08, 2017 6:50 pm

Ernst wrote: It could be as simple as changing the line:

Code: Select all

#CONF_SWAPFILE=/var/swap
to the location where the flash drive is mounted.
and removing the #

Ernst
Posts: 1215
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: How Do I Create A Swap File On A Flash Drive?

Thu Jun 08, 2017 6:59 pm

rpdom wrote: and removing the #
You are spoiling all the fun, I mean learning effect ;-)
The road to insanity is paved with static ip addresses

he's dead jim
Posts: 27
Joined: Wed Dec 09, 2015 3:35 am

Re: How Do I Create A Swap File On A Flash Drive?

Fri Jun 09, 2017 12:36 am

didn't work, but thanks anyway.

anyone else?

jahboater
Posts: 4604
Joined: Wed Feb 04, 2015 6:38 pm

Re: How Do I Create A Swap File On A Flash Drive?

Fri Jun 09, 2017 12:58 am

The other method is:

Code: Select all

sudo dd if=/dev/zero of=/var/swap bs=1M count=1024
sudo chmod 0600 /var/swap
sudo mkswap /var/swap
sudo swapon /var/swap
Last edited by jahboater on Fri Jun 09, 2017 7:41 am, edited 1 time in total.

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

Re: How Do I Create A Swap File On A Flash Drive?

Fri Jun 09, 2017 5:14 am

he's dead jim wrote:Ok, we get it. it can shorten the live of the flash and all that.
So if anyone can provide a step by step explanation of how to do it, I would greatly appreciate it.
I use zswap->loopdev->sparefile for minimaze I/O and maximaze free space on flash
1) compile kernel with zswap/z3fold, enable zswap/z3fold on /boot/cmdline.txt
2) every boot do

Code: Select all

dd if=/dev/zero of=/var/swap conv=sparse bs=1M count=2k 2> /dev/null
chmod 600 /var/swap
losetup /dev/loop7 /var/swap
mkswap /dev/loop7
swapon -d /dev/loop7
PS I use f2fs for minimaze I/O, and maximaze the live of the flash

he's dead jim
Posts: 27
Joined: Wed Dec 09, 2015 3:35 am

Re: How Do I Create A Swap File On A Flash Drive?

Sat Jun 10, 2017 8:41 pm

jahboater wrote:The other method is:

Code: Select all

sudo dd if=/dev/zero of=/var/swap bs=1M count=1024
sudo chmod 0600 /var/swap
sudo mkswap /var/swap
sudo swapon /var/swap
this worked nicely, thanks :)

I did not use the first line as i did not find is necessary, so mine looks like this.

Code: Select all

sudo chmod 0600 /dev/sda1/swap
sudo mkswap /dev/sda1/swap
sudo swapon /dev/sda1/swap
i have to type sudo swapon /dev/sda1 every time i reboot, and the cpu overheats, but those are easy fixes.

i have the heatsinks. thanks for the help :)

Return to “Beginners”