Tag Archives: how to add swap using parted

Ultimate guide: swap addition in Linux (LVM, parted)

Learn how to add swap using LVM, swap file, and parted disk management. Also, learn how to extend existing swap partition on Linux server

Add swap in Linux

We have already seen what is swap in article difference between tmpfs and swap. In this how-to article we will be checking out how to add swap using LVM disk, how to extend swap, how to add swap using file, and how to add swap using the parted disk.

How to add swap in Linux using LVM

If you have LVM disk partitioning in your server then you can go ahead with this method. You need a dedicated logical partition that can be used as a swap. So steps involved are :

I gave you the below outputs for reference. In lvcreate command -L should be followed by size in MB.

# lvcreate -L 500 -n myswaplv vg01
  Logical volume "myswaplv" created.

# mkswap /dev/vg01/myswaplv
mkswap: /dev/vg01/myswaplv: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 511996 KiB
no label, UUID=5339bdde-c734-48d3-94fd-0ab50ef38b4e

# swapon -va

# free
             total       used       free     shared    buffers     cached
Mem:       1018308     186344     831964        152      14616      65784
-/+ buffers/cache:     105944     912364
Swap:       511996          0     511996

# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/dm-0                               partition       511996  0       -1

Warning in mkswap you (may) see is a precautionary warning to make sure you are not using full disk if you meant to use the only partition of it and destroy boot sectors!

Also read : Swap addition in HPUX

You can see free and /proc/swaps shows 500MB of swap which we created. Sometimes if you don’t get your swap activated just by using swapon then try a reboot and it should work. But before reboot makes an entry described hereafter. To make this permanent i.e. at reboot setting lvol to swap again, add below entry in /etc/fstab file.

/dev/vg01/myswaplv swap swap defaults 0 0

How to extend swap

In case you want to extend the existing swap rather than creating a new volume for it then you can still do it on the fly (without reboot). Make sure your swap is not being used and turn it off on volume which you want to extend. Process is as follows –

  • Turn off swap on volume using swapoff
  • Extend lvol using lvresize
  • Define it again as a swap using mkswap
  • Turn on swap on volume using swapon

Refer below outputs on how commands work and their arguments. Here I extended 500MB swap to 600MB.

# swapoff -v /dev/vg01/myswaplv
swapoff on /dev/vg01/myswaplv

#  cat /proc/swaps
Filename                                Type            Size    Used    Priority

# lvresize /dev/vg01/myswaplv -L 600M
  Size of logical volume vg01/myswaplv changed from 500.00 MiB (125 extents) to 600.00 MiB (150 extents).
  Logical volume myswaplv successfully resized.

# mkswap -L newswap /dev/vg01/myswaplv
mkswap: /dev/vg01/myswaplv: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 614396 KiB
LABEL=newswap, UUID=dd91713f-5950-4922-b9a5-e4ea0ec4327e

# swapon -va
swapon on /dev/mapper/vg01-myswaplv
swapon: /dev/mapper/vg01-myswaplv: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/mapper/vg01-myswaplv: pagesize=4096, swapsize=629145600, devsize=629145600

# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/dm-0                               partition       614396  0       -1

You can see after swapoff command, swap volume vanished from /proc/swaps. The newly extended swap of 600MB is back visible after swapon.

How to add swap using file

You can opt to have swap on file rather than volume. This method is a bit easy, quick, and avoids all disk management commands. But along with easiness it involves risk since it’s on a simple file rather than a dedicated disk volume.

First you need to create a blank file with the size of your choice. If you want 8MB swap then 8*1024=8192 block numbers should be used in dd command (since block size used is bs=1024). Do your math accordingly and follow the process below :

  • Create a blank file with dd
  • Define file with swap using mkswap
  • Turn on swap on file using swapon
  • Check swap

Refer below outputs wherein I created 8MB swap on file.

# dd if=/dev/zero of=/myswapfile bs=1024 count=8192
8192+0 records in
8192+0 records out
8388608 bytes (8.4 MB) copied, 0.0144397 s, 581 MB/s

# mkswap /myswapfile
mkswap: /myswapfile: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 8188 KiB
no label, UUID=40a0f347-ce86-4018-9f4d-7ab76bde02ba

# swapon /myswapfile

# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/myswapfile                             file            8188    0       -1

Outputs are pretty self-explanatory. Only calculating dd block count is tricky which I already explained above.

How to add swap using parted disk

Many systems use parted disk management utility in place of LVM. If you are the one then follow the below process for adding swap.

  • Create a new swap partition (primary, extended or logical) using parted
  • Setup swap on it using mkswap
  • Turn on swap on it using swapon
  • Check swap

Parted should be invoked with the disk name which has free space. I am using a fresh disk here.

# parted /dev/xvdf
GNU Parted 3.1
Using /dev/xvdf
Welcome to GNU Parted! Type 'help' to view a list of commands.

Now, you can view current partition table using print command.

(parted) print
Error: /dev/xvdf: unrecognised disk label
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvdf: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:

(parted) mklabel msdos

Since I am using a new disk, I get an unrecognized disk label error. I define label using mklabel msdosNow create new swap partition using the parted console.

(parted) mkpart primary linux-swap 1 1024
(parted) print
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvdf: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  1024MB  1023MB  primary

(parted) quit

Command mkpart is used to create a partition. Followed by partition type (primary, extended or logical), FS type (swap in this case), the start of the partition (1), and end of partition (1024). After the successful execution of the command, verify the partition table using print again.

Once you are ready with partition, you can even confirm it using fdisk -l command.

# fdisk -l /dev/xvdf

Disk /dev/xvdf: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000e7301

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdf1            2048     2000895      999424   82  Linux swap / Solaris

You can see the new partition /dev/xvdf1 is created and it’s marked as Linux swap file system type too!

Now you have to enable and on swap on it.

# mkswap /dev/xvdf1
Setting up swapspace version 1, size = 999420 KiB
no label, UUID=1c19e97e-b757-48a0-99a9-68e6162d69c3

# swapon /dev/xvdf1

# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/xvdf1                              partition       999420  0       -1

That’s it! You have turned on the swap in a parted disk partition! To make it persistent over reboots you can add below entry in /etc/fstab.

/dev/xvdf1               swap                    swap    defaults        0 0

This concludes the swap in the Linux article. Comment your suggestions, feedback below.