Activities

August 2014
M T W T F S S
« Jul   Sep »
 123
45678910
11121314151617
18192021222324
25262728293031

How do I create swap partition in Linux : mkswap Operation not permitted

One of my old friend is been used a Godaddy VPS for a long time and he is facing issue issue with MySQL over load due to poor RAM resource. After few attempts, I have convinced that I could not able to create swap partition or swap file to enable additional swap to this system.

Problem : Most of the Visualization software limit the instance to extend their memory by adding more swap. If the customer want to extend the RAM , they may need to up-grade the existing VPS plan (Go daddy Business). Swapfiles are only supported on some openvz setups, and disabled on purpose by some admins so you cant use up much more resources than you’ve been alocated. You probably need to upgrade your vps.

[root@ip-184-168-90-206 ~]# swapon /home/swapfile
swapon: /home/swapfile: Operation not permitted

Solution

You you need to create a fakeswap on such VPS servers. Here is a script which I taken from http://linux-problem-solver.blogspot.in host.

a. Create a script file named “create_swap.sh”
b. Add this file content in it

#!/bin/bash
SWAP="${1:-512}"
NEW="$[SWAP*1024]"; TEMP="${NEW//?/ }"; OLD="${TEMP:1}0"
umount /proc/meminfo 2> /dev/null
sed "/^Swap\(Total\|Free\):/s,$OLD,$NEW," /proc/meminfo > /etc/fake_meminfo
mount --bind /etc/fake_meminfo /proc/meminfo
free -m

c. Apply the fake swap, if u want to increase the swap to 4GB, then apply the script as shown below,

[root@ip-18-16-90-06 installation]# sh create_swap.sh  4096
             total       used       free     shared    buffers     cached
Mem:          2048       1841        206          0          0          0
-/+ buffers/cache:       1841        206
Swap:         4096          0       4096
[root@ip-18-16-90-06 installation]#

Now you can see that SWAP value is added.

Note : I’m not sure how this mechanism is going to work. So I’m watching on this server for next couple of day to get the SWAP RAM being used.

How do I create swap partition runtime.

1. Create a separate partion and set it as swap
2. Create a file to act as swap partiton

1.Using disk partition to create SWAP

How do I create swap using new partition
Create new EBS volume attached to this instance or create a new partition using fdisk utility. Here I have created the partition /dev/sda2 to use 512Mb disk

Creating swap partition

[root@web-test1 ~]# mkswap /dev/sda2
Setting up swapspace version 1, size = 526413 kB
[root@web-test1 ~]# swapon   /dev/sda2
[root@web-test1 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:           249        217         32          0         14         53
-/+ buffers/cache:        149         99
Swap:          502          0        502
[root@web-test1 ~]#

To make this SWAP partition permenently on each system boot up, you need to add the following lines in “/etc/fstab” file. Note device name will be vary depending on your disk mount point.

/dev/sda2 swap swap defaults 0 0

2.Using a file to create SWAP partition.

1. Create a swap file depending on your system RAM resource.
1.Here I’m using 512Mb swap file. Following command will create a file having 4GB size. We will be using this file for SWAP purpose.

dd if=/dev/zero of=/home/swapfile bs=1M count=512

2. Set only root can access this file
#chmod 600 /home/swapfile

3. Create swap file system on this file
mkswap /home/swapfile

4. Activate the swap partition
swapon /home/swapfile

[root@web-test1 ~]# dd if=/dev/zero of=/home/swapfile bs=1M count=512
512+0 records in
512+0 records out
536870912 bytes (537 MB) copied, 38.7724 seconds, 13.8 MB/s
[root@web-test1 ~]# mkswap /home/swapfile
Setting up swapspace version 1, size = 536866 kB
[root@web-test1 ~]# du -sh /home/swapfile
513M    /home/swapfile
[root@web-test1 ~]# swapon /home/swapfile
[root@web-test1 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:           249        243          5          0          9         84
-/+ buffers/cache:        150         99
Swap:          511          0        511
[root@web-test1 ~]#

To make it permanent, you may need to add the “/etc/fstab” entry
#cat /etc/fstab | grep “swap”
/home/swapfile swap swap defaults 0 0

How do I enable and disable the SWAP partition

To enable : swapon -a and to disable swapoff -a

[root@web-test1 ~]# swapon -a
[root@web-test1 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:           249        241          7          0          9         81
-/+ buffers/cache:        150         99
Swap:          511          0        511
[root@web-test1 ~]# swapoff -a
[root@web-test1 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:           249        241          8          0          9         81
-/+ buffers/cache:        149         99
Swap:            0          0          0
[root@web-test1 ~]#

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>