Saturday 31 December 2011

Creating Swap space in Linux / RHEL / CENTOS / Fedora

Creating Swap space

As mentioned before, a swap space can be a partition or a file with pre-allocated space.


Setting up a Swap file


Suppose your system RAM is 2GB. Then you want to allocate 2 GB for swap space. So create a file anywhere for example in / as /swapfile.


Step 1: Open a shell, login as root

Step 2: Use dd command to create the file called /swapfile. We can use dd many ways to create the file.

[root@server ~]# dd if=/dev/zero of=/swapfile bs=1k count=2M
This will dump 2 million blocks of 1KB each into the /swapfile.

or

[root@server ~]# dd if=/dev/zero of=/swapfile bs=2k count=1M
This will dump 1 million blocks of 2KB each into the /swapfile.

or
[root@server ~]# dd if=/dev/zero of=/swapfile bs=1024 count=2097152
This will dump 2097152 blocks of 1024 each into the /swapfile.

Use any of these as you.

Step 3: Create the signature using mkswap.

[root@server ~]# mkswap /swapfile

Step 4: Add the swap space to the /etc/fstab file to make it persistent at system reboots.

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


Add the following line

/swapfile     swap     swap     defaults     0 0


Step 5: Activate the new swap space using swapon -a.

[root@server ~]# swapon -a 


Step 6: Check the swap partition's status using swapon -s or free -m.


[root@server ~]# swapon -s
[root@server ~]# free -m


Setting  up a Swap partition

Use fdisk or some other partitioning program to add a partition. Set the partition id type to 0x82 (Linux Swap / Solaris). Create the signature on the partition using mkswap and set a signature label to identify it consistently if disk device pathnames change.

Step 1: Open a shell, login as root

Step 2: Open fdisk to create the partition and change the system id.

[root@server ~]# fdisk -cu /dev/sda

Press n to add a new partition.

Now change the new partition's system id by pressing the t option.

Now save the table and exit. Restart for it it to work properly.


Step 3: Create the signature using mkswap.

[root@server ~]# mkswap -L SWAP2 /dev/sda2
mkswap: /dev/sda2: warning: don't erase bootbits sectors
        (dos partition table detected). Use -f to force.
Setting up swapspace version 1, size = 10485756 KiB
LABEL=SWAP2, UUID=986049e1-c454-484e-866c-22b38cb16e7b



Step 4: Add the swap space to the /etc/fstab file to make it persistent at system reboots.

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

Add the following line

LABEL=SWAP2     swap     swap     defaults     0 0



Step 5: Activate the new swap space using swapon -a.


[root@server ~]# swapon -a 



Step 6: Check the swap partition's status using swapon -s or free -m.


[root@server ~]# swapon -s
[root@server ~]# free -m



Thats all...

No comments:

Post a Comment

Navigation by WebRing.