Site Tools


usb_formatting_with_swap_partition

USB formatting with swap partition

A swap partition can prevent several types of insufficient RAM symptoms, such as system instability, failing processes, and in severe cases, even reboots. All this depends largely on the network load and on FreshTomato's configuration. Some functions require much more RAM than others, and higher network traffic puts more load on the router and requires more RAM. USB storage is an excellent way to provide swap space.

This example assumes you are using an ARM or MIPS build with USB support, and USB support is already enabled. If you're not sure how to enable USB support, check the USB Support page.

For this example, we use a 16GB USB flash drive seen in the system as device sda.

Automatic method

This assumes your drive's device name is “sda” ! If it's not, remember to change these commands consistent with your drive's device name.

The echo 1000M statement below defines the size of the swap partition (in MB). You may adjust that according to your needs.


Then enter:


device=/dev/sda
partition=$device'1'
swappartition=$device'2'
nvram set usb_automount="0"
(echo o; echo n; echo p; echo 2; echo ; echo +1000M; echo t; echo 82; echo n; echo p; echo 1; echo ; echo ; echo w) | fdisk $device
# Uncomment the preferred option here below:
# mkfs.ext2 -L usb $partition
# mkfs.vfat -n usb $partition
mkswap -L swap $swappartition
swapon $swappartition
mkdir -p /mnt/usb && mount /dev/sda1 /mnt/usb
nvram set usb_automount="1"


Manual method

From the Web interface, disable the Automount checkbox in the USB Support menu.




Then, logon via SSH or Telnet and delete any existing partitions:

fdisk /dev/sda

o

At this point your USB flash drive is unpartitioned. Now let's create the necessary partition table:

n

p

2

ENTER

+1000M (Essentially 1 GB will used for swap)

t

82

n

p ENTER

ENTER

l (to verify)

w (write to save changes and exit fdisk)




At this point, you should have two unformatted partitions on your flash drive:

  • sda1 ~14GB (fs-type ext2/3/4)
  • sda2 ~1GB (fs-type swap)




Now, format the partitions. The ext2 filesystem is recommended for USB flash drives and ext3 for hard disks/SSDs.

mkfs.ext2 -L USB /dev/sda1
mkswap /dev/sda2




Next, mount the partitions:

swapon /dev/sda2
mkdir /mnt/sda1 && mount /dev/sda1 /mnt/sda1



You can now re-enable USB Automount on the USB Support menu.


Configuring Post-mount and Pre-unmount Commands

For best practice, the swap partition should be activated as swap once the USB drive/flash is mounted. Additionally, it's best to deactivate swap partitions before unmounting them. It is quite simple to configure both the post-mount and pre-unmount commands. The methods for both are as follows:


Activating Swap Partition (Post Mount Command)

To activate the swap partition you created on the drive, enter the following into the “Run after mounting” form field:


swapon /dev/sda2



The field should look like this:

1)


Deactivating the Swap Partition (Pre-unmount command)

To configure the router to automatically deactivate the swap partition (in this case, /dev/sda2) , the “Run before unmounting” field needs to be populated with the following:


swapoff /dev/sda2 



The field should appear like this:

2)

In both cases, click Save at the bottom before continuing.

Final setup


The USB flash drive is now formatted. Your USB and NAS page settings should look like this:




Under Attached Devices, you should see something similar to this:




To verify the swap partition works properly, check the Overview menu. You should see details related to swap space:




1)
You may have additional commands within the field.
2)
Again, your form field may have additional data within it.
usb_formatting_with_swap_partition.txt · Last modified: 2024/10/31 23:51 by hogwild