LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Embedded & Single-board computer (https://www.linuxquestions.org/questions/linux-embedded-and-single-board-computer-78/)
-   -   Raspberry Pi swapfile or swap partition (https://www.linuxquestions.org/questions/linux-embedded-and-single-board-computer-78/raspberry-pi-swapfile-or-swap-partition-4175556490/)

Duglas 10-18-2015 11:44 AM

Raspberry Pi swapfile or swap partition
 
I have connected a large harddrive to my Raspberry Pi as a file server.
Should I now allocate some space as a swapfile or swap partition, to use instead of the swapfile on the SD card?

If so, how to go about this?

berndbausch 10-18-2015 06:56 PM

You only need swap space if your virtual memory is not sufficient, that is, programs can't run because of the lack of virtual memory.

If this is what happens on your system, create some swap space on a disk partition or a normal file using the mkswap command. Then, tell the system to use that swap space by adding it to the /etc/fstab file.

Most likely, you don't need swap space though. Just adding a disk drive doesn't change your memory needs.

syg00 10-18-2015 07:09 PM

The OP asked whether the swap should be move off the SD card. The answer to that question is "probably yes".
Swap extent must be contiguous - having a partition makes this trivial. For a swap file use dd to create a file of the requisite size then issue the mkswap. Then use swapon to ensure it is ok, and swapoff the swap on the SD card. When all that works, add it to fstab and remove the entry for the SD swap.
Check your bootloader parms - especially the "resume" parameter (if any). Also the init{ramfs,rd} may reference the swap if you use one.

If all that seems too much work, and you aren't using the swap, you could just leave everything as-is. Your choice.

blue_z 10-18-2015 09:28 PM

Quote:

Originally Posted by berndbausch (Post 5436554)
You only need swap space if your virtual memory is not sufficient, that is, programs can't run because of the lack of virtual memory.

Not quite.
To be a correct comment, you need to replace both instances of "virtual" with "physical".

A Linux process can acquire (i.e. malloc()) all the virtual memory it wants up to the system limit (e.g. typically 3GB in a 32-bit system), but it's when the process tries to use it that physical memory has to be mapped and swapping might occur.

Regards

blue_z 10-18-2015 09:33 PM

Quote:

Originally Posted by syg00 (Post 5436560)
The OP asked whether the swap should be move off the SD card. The answer to that question is "probably yes".

Definitely "yes" if you don't want to wear out the SDcard.
The wear-leveling capabilities of SDcards are suspect, and reportedly not as good as SSD.

Regards

LinuxUser42 10-20-2015 12:36 PM

What points to the swap file?

I am thinking, it might be better to add another swap file to the hard drive and point to it, but save the prior, in case you don't have the hard drive on or hooked up (either redirect or if/then type statement).

rtmistler 10-21-2015 08:12 AM

My understanding is that ARM has no memory controller and hence all ARM based distributions have to employ a non-memory controller distribution. What this has to do with swap is that I believe the memory controller is what uses swap.

My take on this is that you absolutely do not want to put swap on a flash disk, for a couple of reasons:
  1. Swap will look to read/write a lot because it assumes all data is volatile (not intended to be retained across a boot or power cycle), and it will do this a lot to a finite life storage disk
  2. You'll want to designate a certain partition, and thus location on disk for this, and you'll not want to make it very large, thus you'll be reading and writing a lot to a very particular range in disk, not allowing write leveling algorithms to work effectively on a flash media. So just a poor idea.
If you really feel you need swap space, I'd try using a RAM disk for that.

blue_z 10-21-2015 03:15 PM

Quote:

Originally Posted by LinuxUser42 (Post 5437512)
What points to the swap file?

The file /etc/fstab.

Quote:

Originally Posted by LinuxUser42 (Post 5437512)
I am thinking, it might be better to add another swap file to the hard drive and point to it, but save the prior, in case you don't have the hard drive on or hooked up (either redirect or if/then type statement).

That'a a good idea when swap is on a removable drive.
But not easily implemented (i.e. fstab is a text file, and AFAIK there are no conditional operators for the mount program to recognize when it processes this file).
In the init script, after populating /dev and before mount processes fstab), a check for the drive and installing an appropriate version of fstab file would have to be added.
But if swapoff and swapon commands are installed, then the move can be performed by a shell script at root's descretion.

Regards

blue_z 10-21-2015 03:37 PM

Quote:

Originally Posted by rtmistler (Post 5438001)
If you really feel you need swap space, I'd try using a RAM disk for that.

Bad advice if all you're doing is re-purposing a portion of main memory to a RAM disk.
Besides RAM disks have been deprecated in favor of ramfs and tmpfs.

Regards

blue_z 10-23-2015 03:58 AM

Quote:

Originally Posted by rtmistler (Post 5438001)
My understanding is that ARM has no memory controller and hence all ARM based distributions have to employ a non-memory controller distribution.

Your understanding is incorrect.
A memory controller is the hardware whose primary function is to arbitrate access to memory and maintain it (e.g. refresh DRAM). Since the typical Linux system uses (some type of) dynamic RAM, a memory controller is an essential HW component.

Perhaps you are thinking of a memory management unit, MMU, which provides virtual memory and paging capabilities. Since Linux requires virtual memory, ARM systems running Linux will have a MMU. SoCs incorporating ARM9, ARM11 and ARM Cortex-A typically have MMUs. Note that Linux has been available on ARM for about 20 years.

There is a non-MMU version of Linux called the uClinux project. The mainline Linux kernel can be built as a MMU-less version, but since the typical ARM MPU SoC (I'm excluding all ARM MCUs) does have a MMU, ARM Linux does use virtual memory (just like x86).

Quote:

Originally Posted by rtmistler (Post 5438001)
What this has to do with swap is that I believe the memory controller is what uses swap.

Swapping is managed by the OS, not by any memory controller or MMU. Swapping is optional, and can be disabled in the kernel (e.g. in embedded systems).

Your statements are also contradictory. If ARM doesn't have a "memory controller", and "the memory controller is what uses swap", it would follow the ARM can't use swap. Yet you proceed to discuss swapping on ARM.

Regards

Duglas 10-24-2015 08:23 AM

Thanks Guys,
This thread is Solved, and will be marked as such, as soon as I figure out how to do it.

sygOO said "probably yes"
blue_z said "definitly yes if you don't want to wear out the SDcard", and that has to be the correct answer.

Searching thru the system I found a file "/etc/dphys-swapfile", and a script "/sbin/dphys-swapfile".
I repartitioned the harddisk to add a swap partition. Enabled it in /etc/fstab.
That was all, the script /etc/dphys-swapfile is smart enough to not start a swapfile when it finds a swap partition.

I had tried to add a swapfile on the disk, but came up with "permission errors".


All times are GMT -5. The time now is 08:50 PM.