5

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

dwaynekdclarke
  • 150
  • 1
  • 3
  • 12

1 Answers1

6

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

Terry Wang
  • 9,775
  • So @Terrywang, therefore if im creating a 4GB swap this is what is should look like sudo fallocate -l 4096m /mnt/4096MiB.swap sudo chmod 600 /mnt/4096MiB.swap

    sudo dd if=/dev/zero of=/mnt/4096MiB.swap bs=4M count=4096 sudo chmod 600 /mnt/4096MiB.swap

    sudo mkswap /mnt/4096MiB.swap

    sudo swapon /mnt/4096MiB.swap

    gksudo gedit /etc/fstab

    /mnt/4096MiB.swap none swap sw 0 0

    – dwaynekdclarke Aug 01 '13 at 21:14
  • If you use fallocate to generate the swapfile, NO you don't need to du it using dd again, because they do the same thing, diff is fallocate is much faster. Otherwise the steps look good. – Terry Wang Aug 01 '13 at 21:24
  • Well I was successful in creating the swap file, which i most say is running quite well. @terrywang – dwaynekdclarke Aug 02 '13 at 12:43