Creating swap with dd command

[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/sda3 partition 1048572 0 -1

[root@localhost ~]# free -m
total used free shared buffers cached
Mem: 981 748 233 3 33 386
-/+ buffers/cache: 328 652
Swap: 1023 0 1023

Determine the size of the new swap file in megabytes and multiply by 1024 to determine the number of blocks. For example, the block size of a 64 MB swap file is 65536

[root@localhost ~]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty’.
64*1024
65536
[root@localhost ~]# dd if=/dev/zero of=/swapfile bs=1024 count=65536
65536+0 records in
65536+0 records out
67108864 bytes (67 MB) copied, 2.0382 s, 32.9 MB/s

[root@localhost ~]# mkswap /swapfile
mkswap: /swapfile: warning: don’t erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 65532 KiB
no label, UUID=37ac34ec-ed4b-4e58-a4f6-ffe717d5e927

[root@localhost ~]# swapon /swapfile

 

[root@localhost ~]# cat >> /etc/fstab
/swapfile swap swap defaults 0 0

[root@localhost ~]# tail -n1 /etc/fstab

/swapfile swap swap defaults 0 0

[root@localhost ~]# swapon -s

Filename Type Size Used Priority

/dev/sda3 partition 1048572 0 -1

/swapfile file 65532 0 -2

[root@localhost ~]# free -m
total used free shared buffers cached
Mem: 981 748 232 3 33 386
-/+ buffers/cache: 329 652
Swap: 1087 0 1087

[root@localhost ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/sda3 partition 1048572 0 -1
/swapfile file 65532 0 -2

if=/dev/zero (if=/dev/input.file) : The name of the input file you want dd the read from.

of=/tmp/test1.img (of=/path/to/output.file) : The name of the output file you want dd write the input.file to.

bs=1G (bs=block-size) : Set the size of the block you want dd to use. 1 gigabyte was written for the test.

count=1 (count=number-of-blocks): The number of blocks you want dd to read.

oflag=dsync (oflag=dsync) : Use synchronized I/O for data. Do not skip this option. This option get rid of caching and gives you good and accurate results

conv=fdatasyn: Again, this tells dd to require a complete “sync” once, right before it exits. This option is equivalent to oflag=dsync.

ex:2

[root@localhost ~]# dd if=/dev/zero of=/swapfile2 bs=1G count=4
4+0 records in
4+0 records out
4294967296 bytes (4.3 GB) copied, 78.6588 s, 54.6 MB/s

[root@localhost ~]# mkswap /swapfile2
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=bcadc64e-0bbe-44bb-8ff9-721ca3937769

[root@localhost ~]# swapon /swapfile2

[root@localhost ~]# cat >> /etc/fstab
/swapfile2 swap swap defaults 0 0
[root@localhost ~]# free -m
total used free shared buffers cached
Mem: 981 144 836 0 2 21
-/+ buffers/cache: 121 859
Swap: 5183 75 5108

[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/sda3 partition 1048572 77132 -1
/swapfile file 65532 0 -2
/swapfile2 file 4194300 0 -3

[root@localhost ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/sda3 partition 1048572 77132 -1
/swapfile file 65532 0 -2
/swapfile2 file 4194300 0 -3

[root@localhost ~]# tail -n1 /etc/fstab
/swapfile2 swap swap defaults 0 0

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s