adding swap volume
To create a fifth partition, you would have to remove one of your four existing primary partitions, create an extended partition (which is a container for logical partitions, so you can effectively have more than four partitions on a system with an MS-DOS type partition table), and recreate the partition you'd deleted as a logical partition inside the extended partition. You'd have to have somewhere to back up the partition you remove (or at least its contents). This is a big hassle, in some instances prohibitively so.
Therefore, unless you need your Ubuntu system to support hibernation (which requires a swap partition), you should set it up to use a swap file instead. This performs just as well (provided your swap file is stored on an ext2, ext3, or ext4 partition, which it would be).
The following instructions are taken from the Swap FAQ in Ubuntu's community documentation. It is not an exact quote, since I have changed the formatting for internal consistency and for compatibility with AskUbuntu; I have also made a few tiny edits for clarity. This documentation is licensed under CC-BY-SA, which permits inclusion (with or without modification) into articles like this (which, as AskUbuntu.com content, is also made available under that license). It might make sense to edit this further for clarity (or other purposes), but if significant further modifications are made, it would be good to explicitly offer them for consideration "upstream" (so as to improve the original source also).
Four-step Process to Add Swap File
- Creating a file the size you want.
- Formatting that file to create a swapping device.
- Adding the swap to the running system.
- Making the change permanent.
INFO: This will not work on btrfs-filesystems at the moment. See man swapon
.
Instructions For Adding a 512 MiB Swap
-
Create a file 512 MiB size (or replace what with whatever size you want):
We will create a
/mnt/512MiB.swap
swap file and set the permissions so that users cannot read it directly.sudo fallocate -l 512m /mnt/512MiB.swap sudo chmod 600 /mnt/512MiB.swap
fallocate
length suffixes are: k, m, g, t, p, e (Seeman fallocate
.)By default your swap file may be created world readable. We set the 600 mode permissions in order to prevent users from being able to read potentially sensitive information from the swap file.
If fallocate fails with
fallocate failed: Operation not supported
as it currently does on my Maverick machine, you can do this the old way, again 512 MiB:sudo dd if=/dev/zero of=/mnt/512MiB.swap bs=1024 count=524288 sudo chmod 600 /mnt/512MiB.swap
-
Format that file to create a swapping device:
sudo mkswap /mnt/512MiB.swap
-
Add the swap to the running system:
sudo swapon /mnt/512MiB.swap
The additional swap is now available and can be seen by
cat /proc/meminfo
. -
Making the change permanent:
Edit
/etc/fstab
:gksudo gedit /etc/fstab
Add this line at the end of the file:
/mnt/512MiB.swap none swap sw 0 0
Save. After the next reboot the swap will be used automatically.
Example of making a swap file
This is an example of making and using a swap file on a computer with no swap partition.
user@computer:~$ sudo fallocate -l 512m /mnt/512MiB.swap
Password:
user@computer:~$ sudo mkswap /mnt/512MiB.swap
Setting up swapspace version 1, size = 536866 kB
no label, UUID=dd6a01c8-93f0-41e0-9b7a-306956d8821b
user@computer:~$ sudo swapon /mnt/512MiB.swap
user@computer:~$ cat /proc/meminfo
MemTotal: 499496 kB
MemFree: 9156 kB
Buffers: 4748 kB
Cached: 233140 kB
SwapCached: 724 kB
Active: 254432 kB
Inactive: 157920 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 499496 kB
LowFree: 9156 kB
SwapTotal: 524280 kB
SwapFree: 523556 kB
Dirty: 128 kB
Writeback: 0 kB
Mapped: 243420 kB
Slab: 20672 kB
CommitLimit: 774028 kB
Committed_AS: 648680 kB
PageTables: 2224 kB
VmallocTotal: 524280 kB
VmallocUsed: 5708 kB
VmallocChunk: 518176 kB
user@computer:~$ gksudo gedit /etc/fstab
user@computer:~$ free
total used free shared buffers cached
Mem: 499496 479488 20008 0 8256 215892
-/+ buffers/cache: 255340 244156
Swap: 524280 3856 520424
#####Then, after running a few more programs...
user@computer:~$ free
total used free shared buffers cached
Mem: 499496 492768 6728 0 1240 142336
-/+ buffers/cache: 349192 150304
Swap: 524280 53384 470896
#####Next, reboot to make sure it will work consistently.
user@computer:~$ free
total used free shared buffers cached
Mem: 499496 493136 6360 0 7528 174700
-/+ buffers/cache: 310908 188588
Swap: 524280 17148 507132
Undoing Your Changes
Undoing basically follows the same process in reverse.
gksudo gedit /etc/fstab
Remove the line:
/mnt/512MiB.swap none swap sw 0 0
Remove the swap from the running system and remove the swap file:
sudo swapoff /mnt/512MiB.swap && sudo rm /mnt/512MiB.swap
No need to reboot.
Source: As described above in detail, this is a derivative work of SwapFaq in the Ubuntu community-authored documentation. That page is written by Contributors to the Ubuntu documentation wiki
. (Maintainers of this AskUbuntu answer should carefully read this legal information before editing or removing this citation, to ensure that the requirement to give credit to the original authors continues to be met.)
Related videos on Youtube
Mhmd Korhani
Updated on September 18, 2022Comments
-
Mhmd Korhani almost 2 years
I have recently installed ubuntu 12.04 alongside my windows 7. But i did not created swap volume for ubuntu. There are already 4 partitions on my hard drive ( one windows 7 , one system tools ( windows 7), one for ubuntu and one for common media storage(ntfs)). Therefore Gparted didn't allow me to create any further partition for swap volume.All it said to create an extended partition, but i do not know to do this. I want to create a swap volume out of common media storage. How can i accomplish this?
And I'm completely new at Ubuntu , so can you suggest some good getting started tutorial for it?
-
Mhmd Korhani about 12 yearsthnx for your help!
-
CMCDragonkai about 10 yearsWhy would this
fallocate failed: Operation not supported
happen? Is it a bug? I know that fallocate is available, but what causes such an error?