===============
Swap Files Partition
===============
As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether.
This may be especially desirable if disk space is at a premium (e.g. a modestly-sized SSD).
Note: The BTRFS filesystem does not currently support swapfiles.
Swap file creation
=============
As root use fallocate to create a swap file the size of your choosing (M = Megabytes, G = Gigabytes)
(dd can also be used but will take longer). For example, creating a 512 MB swap file:
cd /u02/
# fallocate -l 4096M /swapfile // optional
# dd if=/dev/zero of=swapfile bs=1M count=4096
Set the right permissions (a world-readable swap file is a huge local vulnerability)
# chmod 600 swapfile
After creating the correctly-sized file, format it to swap:
# mkswap swapfile
Activate the swapfile:
# swapon swapfile
Edit /etc/fstab and add an entry for the swap file:
/u02/swapfile none swap defaults 0 0
This invocation creates a file called my_swap in /var. It is 128 Mb long (128 x 1024 = 131072). Initially, it is filled with zeros.
However, mkswap marks it as swap space and swapon tells the kernel to start using it as swap space. When you are done with it,
swapoff /u02/swapfile
rm /u02/swapfile
==============
Remove swap file
==============
To remove a swap file, the current swap file must be turned off.
As root:
# swapoff -a
Remove swapfile:
# rm -rf /swapfile
========================================================================
================
Swap with USB device
================
Thanks to modularity offered by Linux, we can have multiple swap partitions spread over different devices. If you have a very full hard disk,
USB device can be used as partition temporally. But this method has some severe disadvantage
* USB device is slower than hard disk.
* flash memories have limited write cycles. Using it as swap partition will kill it quickly.
* when another device is attached to the computer, no swap can be used.
To add a a USB device to SWAP, first take a USB flash and partition it with a swap partition.You can use graphical tools such as
Gparted or console tools like fdisk. Make sure to label the partition as SWAP before writing the partition table.
Make sure you are writing the partition to the correct disk!
Next edit the fstab
# nano /etc/fstab
Now add a new entry, just under the current swap entry, which take the current swap partition over the new USB one
UUID=... none swap defaults,pri=10 0 0
where UUID is taken from the output of the command
ls -l /dev/disk/by-uuid/ | grep /dev/sdc1
Just replace sdc1 with your new USB swap partition. sdb1
We use UUID because when you attach other devices to the computer it could modify the device order
Last, add
pri=0
in the original swap entry for teaching fstab to use HD swap only when USB is full
This guide will work for other memory such as SD cards, etc.
========================================================================
============
Swap partition
============
A swap partition can be created with most GNU/Linux partitioning tools (e.g. fdisk, cfdisk). Swap partitions are designated as type 82.
NOTE : Only Primary Partition Use
To set up a Linux swap area, the mkswap command is used. For example:
fdisk /dev/sdb
p
n
p
+4096M
t
82
w
# mkswap /dev/sda2
Warning: All data on the specified partition will be lost.
To enable the device for paging:
# swapon /dev/sda2 // swapoff
To enable this swap partition on boot, add an entry to fstab:
/dev/sda2 none swap defaults 0 0
No comments:
Post a Comment