Creating a SWAP partiton in Standard HDD layout.

Creating and activating a SWAP partition

Check for any free space that is available in your HDD.

[root@sunny ~]# parted
GNU Parted 2.1
Using /dev/vda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print free                                                      
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 21.5GB
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   4720MB  4194MB  primary   ext4
 3      4720MB  8914MB  4194MB  primary   ext4
 4      8914MB  21.5GB  12.6GB  extended
 5      8916MB  11.0GB  2097MB  logical   ext4
        11.0GB  11.0GB  456kB             Free Space
 6      11.0GB  12.1GB  1049MB  logical   ext4
 7      12.1GB  13.1GB  1049MB  logical   ext4
 8      13.1GB  14.0GB  839MB   logical   ext4
 9      14.0GB  14.5GB  537MB   logical   linux-swap(v1)
        14.5GB  21.5GB  6985MB            Free Space

(parted) q                                                              
[root@sunny ~]#

If there is any free space in your HDD then fire a command like given below, here your device name may vary like sda, sdb, etc...

[root@sunny ~]# fdisk /dev/vda

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/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007586f

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018        9145     4096000   83  Linux
Partition 2 does not end on cylinder boundary.
/dev/vda3            9145       17272     4096000   83  Linux
Partition 3 does not end on cylinder boundary.
/dev/vda4           17272       41611    12266496    5  Extended
Partition 4 does not end on cylinder boundary.
/dev/vda5           17276       21340     2048000   83  Linux
/dev/vda6           21341       21341         315   83  Linux
/dev/vda7           17272       17275        1544+  83  Linux
/dev/vda8           21340       21340         256+  83  Linux
/dev/vda9           21342       21444       51880+  83  Linux

Partition table entries are not in disk order

Command (m for help):

Now create a new partition by firing a command as n on fdisk's prompt for whole list of commands which you can fire on fdisk prompt you can fire m.

n: For new partition.
First cylinder: As you can see above for the partition /dev/vda9 the last cylinder is 21444 (/dev/vda9           21342       21444       51880+  83  Linux).... So for this new partition the First cylinder would be 21444 or 21445 if the default value is different then your partition table entries are not in disk order.
Last cylinder: It should be number of cylinders or the size of the partition in KB,MB,GB, If you just pressed enter it will consider all your free space of the HDD.

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

Now we need to toggle the partition type to SWAP, because when we create a partition it will create a default partition type of Linux.

t: For toggle.
Partition number: Which you just created.
Hex. Code: For SWAP its 82.. you can check this by typing L.

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

Now you need to write the changes to the disk which you made just type in w and enter.

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@sunny ~]# 

Now to make the kernel aware that you have made a partition reboot your machine or just fire partx -a /dev/vda two times.  
Note: vda may vary according to your device type.

[root@sunny ~]# partx -a /dev/vda

Now format the the partition which you created as swap.

[root@sunny ~]# mkswap /dev/vda10
Setting up swapspace version 1, size = 244800 KiB
no label, UUID=5b57a637-278a-460f-8925-75782063f846
[root@sunny ~]#

Make a entry in fstab file so that when you next reboot your machine the partition is still accepted.

[root@sunny ~]# vim /etc/fstab 
Entry which you need to make is: /dev/vda10 swap swap defaults 0 0

Now check your free SWAP space it will show the SWAP space size when you did installed your machine in my case it is 511MB.

[root@sunny ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          1959        522       1436          0         22        188
-/+ buffers/cache:        311       1647
Swap:          511          0        511
[root@sunny ~]# 

Now switch on the SWAP partition which you just created.

[root@sunny ~]# swapon /dev/vda10

Now fire the same command free -m to check the SWAP space available now.

[root@sunny ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          1959        523       1436          0         22        188
-/+ buffers/cache:        311       1647
Swap:          711         0        711

Deactivating and deleting the SWAP partition

Firstly switch off the device.

[root@sunny ~]# swapoff /dev/vda10

If you want then you can check now what is your free SWAP space in your machine now.

[root@sunny ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          1959        537       1421          0         23        188
-/+ buffers/cache:        325       1633
Swap:          511          0        511
[root@sunny ~]#

Remove the entry which you did from the fstab file.

[root@sunny ~]# vim /etc/fstab 

Open fdisk prompt so that you can delete the partition which you made.

[root@sunny ~]# fdisk /dev/vda

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').

p is used for checking the current partitions in your machine.

Command (m for help): p

Disk /dev/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007586f

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018        9145     4096000   83  Linux
Partition 2 does not end on cylinder boundary.
/dev/vda3            9145       17272     4096000   83  Linux
Partition 3 does not end on cylinder boundary.
/dev/vda4           17272       41611    12266496    5  Extended
Partition 4 does not end on cylinder boundary.
/dev/vda5           17276       21340     2048000   83  Linux
/dev/vda6           21341       21341         315   83  Linux
/dev/vda7           17272       17275        1544+  83  Linux
/dev/vda8           21340       21340         256+  83  Linux
/dev/vda9           21342       21444       51880+  83  Linux
/dev/vda10          21445       21851      205096+  82  Linux swap / Solaris

This was the partition which you created /dev/vda10          21445       21851      205096+  82  Linux swap / Solaris.

For deleting a partition just enter d and enter then the partition number which you want to delete.

Command (m for help): d
Partition number (1-10): 10

To check whether the partition has been deleted or not.

Command (m for help): p

Disk /dev/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007586f

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018        9145     4096000   83  Linux
Partition 2 does not end on cylinder boundary.
/dev/vda3            9145       17272     4096000   83  Linux
Partition 3 does not end on cylinder boundary.
/dev/vda4           17272       41611    12266496    5  Extended
Partition 4 does not end on cylinder boundary.
/dev/vda5           17276       21340     2048000   83  Linux
/dev/vda6           21341       21341         315   83  Linux
/dev/vda7           17272       17275        1544+  83  Linux
/dev/vda8           21340       21340         256+  83  Linux
/dev/vda9           21342       21444       51880+  83  Linux

Now write the changes to the HDD.

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.

Now you can reboot your machine or fire partx -a /dev/vda two times so that the kernel is aware that you have deleted one partition from your HDD.

[root@sunny ~]# partx -a /dev/vda

Comments

Popular posts from this blog

Installing Tomcat8 on RHEL6

Manage existing resources via Helm

Configuration and setting-up of AIDE on RHEL6