Add second swap partition on CentOS 6

Swap space is extremely important for Linux system. It acts like temporary storage for process memory image. There are many reasons to have multiple swap areas. We will take a look at the process of creating additional swap partition on already existing system.

In our example system was partiiotned with 3 primary partitions. / /boot and swap

[root@rh ~]#fdisk -l
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00018a08

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64         192     1024000   83  Linux
Partition 2 does not end on cylinder boundary.
/dev/sda3             192         217      204800   82  Linux swap / Solaris
Partition 3 does not end on cylinder boundary.
[root@rh ~]# mount
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
[root@rh ~]# free
total       used       free     shared    buffers     cached
Mem:       1020592     129264     891328          0       6768      39124
-/+ buffers/cache:      83372     937220
Swap:       204792          0     204792

Create extended partition

Since we already have 3 primary partitions created. We will start by creting extended partition which within itself will be holding logical volume with our swap partition.
First we run parted /dev/sda print free to see all free space available

[root@rh ~]# parted /dev/sda print free
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system     Flags
32.3kB  1049kB  1016kB           Free Space
1      1049kB  525MB   524MB   primary  ext4            boot
2      525MB   1574MB  1049MB  primary  ext4
3      1574MB  1784MB  210MB   primary  linux-swap(v1)
1784MB  8590MB  6806MB           Free Space

1. Create extended partition

fdisk /dev/sda
n for new
e for extended
select 4 for partition number
Use default value for number of cilinders
+6G to add 6gb extended partition
p to make sure everyhtong is correct
w apply the changes
[root@rh ~]# fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): n
Command action
e   extended
p   primary partition (1-4)
e
Selected partition 4
First cylinder (217-1044, default 217):
Using default value 217
Last cylinder, +cylinders or +size{K,M,G} (217-1044, default 1044): +6G

Command (m for help): p

Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00018a08

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64         192     1024000   83  Linux
Partition 2 does not end on cylinder boundary.
/dev/sda3             192         217      204800   82  Linux swap / Solaris
Partition 3 does not end on cylinder boundary.
/dev/sda4             217        1000     6290676    5  Extended

Command (m for help): w
[root@rh ~]# fdisk -l

Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00018a08

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64         192     1024000   83  Linux
Partition 2 does not end on cylinder boundary.
/dev/sda3             192         217      204800   82  Linux swap / Solaris
Partition 3 does not end on cylinder boundary.
/dev/sda4             217        1000     6290676    5  Extended

[label type="label" style="default" title="Create swap partition"]


[root@rh ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): p

Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00018a08

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64         192     1024000   83  Linux
Partition 2 does not end on cylinder boundary.
/dev/sda3             192         217      204800   82  Linux swap / Solaris
Partition 3 does not end on cylinder boundary.
/dev/sda4             217        1000     6290676    5  Extended

Command (m for help): n
First cylinder (217-1000, default 217):
Using default value 217
Last cylinder, +cylinders or +size{K,M,G} (217-1000, default 1000): +200M

Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 82
Changed system type of partition 5 to 82 (Linux swap / Solaris)

Command (m for help): p

Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00018a08

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64         192     1024000   83  Linux
Partition 2 does not end on cylinder boundary.
/dev/sda3             192         217      204800   82  Linux swap / Solaris
Partition 3 does not end on cylinder boundary.
/dev/sda4             217        1000     6290676    5  Extended
/dev/sda5             217         242      202009+  82  Linux swap / Solaris

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

[root@rh ~]#

[root@rh ~]# mkswap /dev/sda5
Setting up swapspace version 1, size = 105612 KiB
no label, UUID=94039809-db22-4411-91bd-95d0dfd6559f

[root@rh ~]# free
total       used       free     shared    buffers     cached
Mem:       1020592     179612     840980          0       8740      85420
-/+ buffers/cache:      85452     935140
Swap:       204792          0     204792
[root@rh ~]# swapon /dev/sda5
[root@rh ~]# free
total       used       free     shared    buffers     cached
Mem:       1020592     179612     840980          0       8744      85420
-/+ buffers/cache:      85448     935144
Swap:       310400          0     310400

Make changes permanent
To make chnages permanent the following line should be entered in /etc/fstab

/dev/sda5               swap                   swap   defaults        0 0