Pages

Tuesday, August 2, 2011

SWAP USING FILE

Share it Please

Creating a file for swap is much easier than creating a partition. We can use the “dd” command available in Linux to create a empty file and make that as swap. First check the available swap size using “free” like,

[root@localhost ~]# free
                     total       used       free     shared    buffers     cached
Mem:       2042720     498204    1544516          0      33200     308624
-/+ buffers/cache:     156380    1886340
Swap:      3052268          0    3052268

So we have around 3GB as swap.

Let’s start by creating a empty file like,

[root@localhost ~]# dd if=/dev/zero of=/newswap bs=1024 count=128000
128000+0 records in
128000+0 records out
131072000 bytes (131 MB) copied, 0.661045 seconds, 198 MB/s

The “dd” command copies a amount of data block by block. In this
If=xx is the source. Read from file instead of standard input.
Of=yy is the target. Write to a file instead of standard output.
Bs=zz is both read and write and zz is bytes at a time.

So now the file is created, we need to set the file as swap using “mkswap” like

[root@localhost ~]# mkswap /newswap
Setting up swapspace version 1, size = 131067 kB

Now activate the swap using “swapon” like

[root@localhost ~]# swapon  /newswap

Execute the “free” command to see how much swap is present now,

[root@localhost ~]# free
             total       used       free     shared    buffers     cached
Mem:          2042720     628076    1414644          0      33548     436620
-/+ buffers/cache:     157908    1884812
Swap:      3180260          0    3180260

Finally we need to add the line in the /etc/fstab like,

/newswap      swap   swap   defaults         0 0

Save the file so this swap will automatically gets up during system initialization.

REMOVE SWAP FILE
For removing the Swap file ,just stop the swap using “swapoff” and then remove the file like,

[root@localhost ~]# swapoff -v /newswap
swapoff on /newswap

Finally remove the file using rm –r /newswap and also entry in the /etc/fstab file.

So by this we are now aware of swap partition and swap file.

More articles to come .Happy CodingJ

No comments :

Post a Comment