Odroid: Swap on USB-stick

Swap is used when the kernel runs out of memory and need to swap less used information to the disc to make space for information currently in use. In a good setup the swap will never be used but if it’s needed and not existing it will make processes crash.

I have just started out testing BitTorrent Sync (btsync) on my Odroid-C1 and noticed that all the memory was allocated more or less straight away after starting the btsync service. So just to be safe during my test, to avoid crashes, I added a 8Gb USB stick to use for swap space. You can of course use the SD-card but that will take up space needed for the system as well as shorten the life of your SD-card.

First we check how much swap we currently have.

[bash]#free -m
total used free shared buffers cached
Mem: 836 772 64 14 24 525
-/+ buffers/cache: 222 614
Swap: 0 0 0[/bash]

Then we plug in the usb stick and run dmesg to find the device.

[bash][16573.684715@3] usb-storage 1-1.1:1.0: USB Mass Storage device detected
[16573.686215@3] scsi0 : usb-storage 1-1.1:1.0
[16574.907717@3] scsi 0:0:0:0: Direct-Access PNY USB 2.0 FD 1100 PQ: 0 ANSI: 4
[16574.909157@0] sd 0:0:0:0: [sda] 15826944 512-byte logical blocks: (8.10 GB/7.54 GiB)
[16574.909858@0] sd 0:0:0:0: [sda] Write Protect is off
[16574.909871@0] sd 0:0:0:0: [sda] Mode Sense: 43 00 00 00
[16574.910478@0] sd 0:0:0:0: [sda] No Caching mode page found
[16574.910518@0] sd 0:0:0:0: [sda] Assuming drive cache: write through
[16574.923530@0] sd 0:0:0:0: [sda] No Caching mode page found
[16574.923581@0] sd 0:0:0:0: [sda] Assuming drive cache: write through
[16574.933317@1] sda: sda1
[16574.937732@1] sd 0:0:0:0: [sda] No Caching mode page found
[16574.937776@1] sd 0:0:0:0: [sda] Assuming drive cache: write through
[16574.944985@2] sd 0:0:0:0: [sda] Attached SCSI removable disk[/bash]

So here we can see the 8GB usb-stick plugged in and picked up. So now we need to partition it to use as swap. We see that it’s recognized as /dev/sda.

[bash]root@BTSYNCOD:/# fdisk /dev/sda

Command (m for help): d
Selected partition 1

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-15826943, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-15826943, default 15826943):
Using default value 15826943

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.[/bash]

As you can see we use the default answer on all questions. Then we create the swap on the partition.

[bash]root@BTSYNCOD:~# mkswap /dev/sda1
Setting up swapspace version 1, size = 7912444 KiB
no label, UUID=81a92281-cc1d-4687-a0c9-13dc234d1657[/bash]

Here we make a note of, or copy, the UUID that we need to make the swap mount on boot in fstab. We then use nano /etc/fstab to edit the mount table and add:

[bash]UUID</span><span class="crayon-o">=</span><span class="crayon-v">b69cf517</span><span class="crayon-o">-</span><span class="crayon-cn">5d6e</span><span class="crayon-o">-</span><span class="crayon-cn">4388</span><span class="crayon-o">-</span><span class="crayon-v">bb69</span><span class="crayon-o">-</span><span class="crayon-e">e24d5cb1b338   </span><span class="crayon-e">none    </span><span class="crayon-e">swap    </span><span class="crayon-i">sw</span><span class="crayon-h">    </span><span class="crayon-cn">0</span><span class="crayon-h">   </span><span class="crayon-cn">0[/bash]

Of course you need to replace UUID=b69cf5175d6e4388bb69e24d5cb1b338 with your own UUID. Then we can enable the swap.

[bash]swapon /dev/sda1[/bash]

As I wrote in the intro this is just a fail safe. If the system is forced to use the swap it will decrease performance and my application should make use of a machine with more memory. This has been running for more then 24h now and no swap space is used so far.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: