Creating a Dedicated swap hard drive

How can I create a dedicated hard drive to do "swap"on a ubuntu 12.04 LTS?


Solution 1:

You don't have to use a partition or hdd for swap.

Just create a swap file using dd or fallocate.

Create a file of filled with zeros 1GB in this case:

 dd if=/dev/zero of=/mnt/swap.img bs=1M count=1024

Preferred method: use fallocate instead of dd

NOTE: fallocate is much faster than dd because it quickly allocates blocks and mark them as uninitialized, no I/O to the blocks.

fallocate -l 1024M /mnt/swap.img

Format the file to create a swap device

mkswap /mnt/swap.img

Adding the swap to the running system:

swapon /mnt/swap.img

Make it permanent, edit /etc/fstab and add the following entry:

/mnt/swap.img  none  swap  sw  0 0

More information: https://help.ubuntu.com/community/SwapFaq