Dont have swap enough ... make a swap file

Every day solutions to every day challenges. + Brilliant stuff

Moderators: b1o, jkerr82508

Forum rules
Please feel free to post your tip it does not have to be advanced. Also ask questions directly related to the tip here. But do not start new threads with questions or ask for help here. That is what the help section is for. forum rules: http://bjoernvold.com/forum/viewtopic.php?f=8&t=568
User avatar
jemadux
Viking
Posts: 37
Joined: 13 Oct 2010, 19:07

Dont have swap enough ... make a swap file

Postby jemadux » 24 Dec 2011, 20:12

The trick is to make a file and then tell the swapon program to use it. Here’s how to create, for example, a 64
megs swap file on your root partition (of course make sure you have at least 64 megs free):

Code: Select all

dd if=/dev/zero of=/swapfile bs=1024

This will make a 64 megs (about 67 millions bytes) file on your hard drive. You now need to initialize it:

Code: Select all

mkswap /swapfile 65536
sync

And you can then add it to your swap pool:
swapon /swapfile
With that you have 64 megs of swap added. Don’t forget to add the swapon command to your startup files so
the command will be repeated at each reboot.

User avatar
dedanna1029
Sound-Berserk
Posts: 8780
Joined: 14 Mar 2010, 20:29
Contact:

Re: Dont have swap enough ... make a swap file

Postby dedanna1029 » 26 Dec 2011, 19:35

:s
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html

User avatar
jemadux
Viking
Posts: 37
Joined: 13 Oct 2010, 19:07

Re: Dont have swap enough ... make a swap file

Postby jemadux » 29 Dec 2011, 14:28

i found that at one old book ....

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Dont have swap enough ... make a swap file

Postby viking60 » 29 Dec 2011, 21:00

Keep it coming - we love old books :-D
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
jemadux
Viking
Posts: 37
Joined: 13 Oct 2010, 19:07

Re: Dont have swap enough ... make a swap file

Postby jemadux » 30 Dec 2011, 14:57

ok viking .... i will post more tips !!

User avatar
dedanna1029
Sound-Berserk
Posts: 8780
Joined: 14 Mar 2010, 20:29
Contact:

Re: Dont have swap enough ... make a swap file

Postby dedanna1029 » 31 Dec 2011, 01:19

viking60 wrote:Keep it coming - we love old books :-D

+1

jemadux wrote:ok viking .... i will post more tips !!

Please do! :D
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Making a swapfile on raspberry pi

Postby viking60 » 19 Sep 2013, 23:38

I just made a swapfile on my raspberry pi (it has 512 M standard memory -).
As we have learned we can make both a swapfile or a separate partition for swap.

What you choose will not matter in terms of speed or efficiency :snooty: so you can simply pick what you like.

BUT I chose a slightly different method than our friend above:

Code: Select all

fallocate -l 1024M /swapfile


This will create the directory swapfile and it is set to hold 1024 M in this case.

I could have done it like this:

Code: Select all

dd if=/dev/zero of=/swapfile bs=1M count=1024

..but I didn't :-D


Then I chmoded it:

Code: Select all

chmod 600 /swapfile
- yes it has to be 600 +1

Now it was time to format this to swap:

Code: Select all

mkswap /swapfile


And finally to activate the entire enchilada:

Code: Select all

swapon /swapfile


Just one tip - since you probably will boot into the "home" of root on Raspberry pi you must make sure that you get to /.

Code: Select all

cd /

If not - the last step will not work

You can check it out with Top.
Image
To make it stick after a reboot we need to edit /etc/fstab and add this line:

Code: Select all

/swapfile none swap defaults 0 0


(I will be doing a write-up of my Raspberry pi installation as soon as I understand what I am doing ....so sit tight... :-D )
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Dont have swap enough ... make a swap file

Postby viking60 » 09 Mar 2014, 23:58

Just to be clear:
If you have a swap partition and need more swap space; you can use the swapfile in addition to the existing swap partition.
I had set my swap partition to 16 MB and I have 6G of memory so I originally almost figured I did not need swap.

After having installed Steam glances reported that all the swap was constantly full so I decided to add a 2G swapfile.

Code: Select all

sudo fallocate -l 2G /swapfile


And did the rest of the above and added it to /etc/fstab beneath the swap partition:

Code: Select all

.....
# /dev/sda7
UUID=23a0dd5a-40b5-4c94-a583-8e278b0f7491       swap    swap    sw      0       0
# /swapfile This is what I added no /dev no UUID only /swapfile
/swapfile                                       none    swap    sw      0       0


That sure beats trying to drag the swap partition bigger with gparted. +1

And if you regret it you can easily remove the swap file like this (as root):

Code: Select all

swapoff -a

or

Code: Select all

swapoff /swapfile

If you use the latter then the swap partition you may have will remain.
then

Code: Select all

rm -f /swapfile
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Dont have swap enough ... make a swap file

Postby viking60 » 02 Oct 2015, 17:19

Hey we have inspired others too.
:A
https://forum.manjaro.org/index.php?topic=25541.0
:tux5:
All one great big Linux community - I tell you +1
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
dedanna1029
Sound-Berserk
Posts: 8780
Joined: 14 Mar 2010, 20:29
Contact:

Re: Dont have swap enough ... make a swap file

Postby dedanna1029 » 02 Oct 2015, 19:16

Good job! It will be inspiring this kid too, once I can finally get a wireless connection. :D
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Dont have swap enough ... make a swap file

Postby viking60 » 03 Oct 2015, 13:50

Here I have recorded a terminal sessions that shows the difference between swapoff -a and swapoff /swapfile.
It shows that I have both a swap partition of 15 MB and a swapfile of 2G. I can turn on/off both.

In addition I show that I can turn off only the swapfile leaving the 15 MB swap partition.
:A
Image
http://showterm.io/f4aa7926e0674827e86d3
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
dedanna1029
Sound-Berserk
Posts: 8780
Joined: 14 Mar 2010, 20:29
Contact:

Re: Dont have swap enough ... make a swap file

Postby dedanna1029 » 20 Oct 2015, 06:41

viking60 wrote:Here I have recorded a terminal sessions that shows the difference between swapoff -a and swapoff /swapfile.
It shows that I have both a swap partition of 15 MB and a swapfile of 2G. I can turn on/off both.

In addition I show that I can turn off only the swapfile leaving the 15 MB swap partition.
:A
Image
http://showterm.io/f4aa7926e0674827e86d3

This is SO COOL. I was going to ask how you put it in the container for it there, but I see how now in the code.
You present so much cool stuff to us. Many kudos.

Thank you!
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html


Return to “Tips & Tricks”