5

I have a 500Gib hard disk and 2Gib of RAM. I have ubuntu 12.04

When I originally particioned the disk, I had unallocated space because I had another disk with Windows in it. This disk died, so I had to install Windows in this disk and had to erase Swap Partition.

Now, as I work with lot of pictures, videos and so on, computer is turning slow and I think it is because I haven't swapfile.

A friend of mine gave me the command below, but the count number give me 2Gib of swapfile and I want to have 4 Gib of swapfile.

Please can you tell me which number to put in count?

sudo dd if=/dev/zero of=/swapfile.swap bs=4096 count=1048576
kenorb
  • 9,995
  • 2
  • 78
  • 90
  • If 1048576 blocks with a size of 4086kb each0 = 2GB then, 2097152 blockswith a size of 4086kb each = 4GB – amanthethy Dec 19 '14 at 20:49
  • 2
    possible duplicate of [what is SWAP and how large a swap partition should I create?](http://askubuntu.com/questions/114566/what-is-swap-and-how-large-a-swap-partition-should-i-create) – Richard Dec 19 '14 at 20:49
  • 3
    You should rather [use a swap partition](/q/33697/175814). – David Foerster Dec 19 '14 at 21:35
  • If you run out of ram with swap enabled your computer will also be slow, because it needs to move contents from RAM to the much slower harddisk. (IIRC: If your RAM is full and you don't have swap the OS has to free up RAM by killing processes.) The only option to actually improve performance is to buy more RAM. – LiveWireBT Dec 20 '14 at 05:44
  • The DD command your friend gave you only makes a file of specific size, but doesn't turn on that file as swap. I'll post an answer on that if you want – Sergiy Kolodyazhnyy Jul 11 '16 at 02:11
  • Posted a scripting answer. Please let me know what you think or if you need any explanation on how it works – Sergiy Kolodyazhnyy Jul 11 '16 at 03:10

3 Answers3

5

To create 4GB of swapfile, you can run:

sudo dd if=/dev/zero of=swapfile bs=1K count=4M

so by using multiplicative suffixes it's easier to count (1K * 4M = 4 GiB).

Then you need to convert that file into swap space, and enable it, so:

sudo mkswap swapfile
sudo swapon swapfile

For more details, check: How do I add a swap?

kenorb
  • 9,995
  • 2
  • 78
  • 90
4

The basic steps of how to create a swap file are described in Arch Wiki article on swap. I took the liberty of condensing all those steps into a script。 Basic usage:

Usage is very simple:

sudo ./addswap.sh INTEGER LETTER

For adding 1 gigabyte you would do sudo ./addswap.sh 1 G. For adding 1 megabyte do sudo ./addswap.sh 1 M.

Script

This script is also available on my personal GitHub repository.

#!/bin/bash
set -e  # bail if anything goes wrong

is_root(){
   if [ "$( id -u )" -ne 0  ] ; then
      return 1
   fi
   return 0
}

get_swap_amount(){
    # Obtain amount of swap in Gigabytes
    awk '/SwapTotal/{printf "%.2f",($2/1048576)}' /proc/meminfo
}

make_swap_file(){ 
   # This is the function that does the job of creatig swap file
   # and enabling it.  All files are timestamped
   printf "Current swap ammount: %f\n" "$(get_swap_amount)"
   printf "Working on creating swap file\n"
   DATE=$(date +%s) # append date of creation to filename
   filename="/swapfile.""$DATE" # File will be /swapfile.$DATE
   dd if=/dev/zero  of="$filename" bs=1"$2" count="$1"
   chmod 600 "$filename"
   mkswap "$filename" && 
   swapon "$filename" && 
   printf "\nCreated and turned on %s\n"  "$filename"
   printf "Current swap ammount: %f" "$(get_swap_amount)"
}


ask_to_enable_on_boot(){
   # Prompt user to enable this new swap file on boot. If user
   # enters y, the swap file will be added to /etc/fstab
   printf "Do you want to turn on this file at boot?[y/n]\n"
   read ANSWER
   case "$ANSWER" in
    [Yy]) printf "\n%s none swap defaults 0 0\n" "$filename"  >> /etc/fstab &&
       printf "\n %s added to /etc/fstab successfuly\n" "$filename"
       exit 0 ;;
    [Nn]) printf "Exiting\n" && exit 0 ;;
    *) printf "Wrong input: %s . Exiting. /etc/fstab not altered\n" "$ANSWER" && exit 1 ;;
   esac

}

bad_arguments(){
     # check if second argument is a character 
     case "$2" in 
         [A-Z]) return 1;;
         *) return 0;;
     esac

    # Check if first argument is a digit. 
    # https://stackoverflow.com/a/3951175/3701431
    case "$1" in
        ''|*[!0-9]*) return 0;;
        *) return 1 ;;
    esac 

}

main(){

    # Check if we're root and if args are OK. If everything is ok, do stuff

    if is_root 
    then
        if [ $# -ne 2   ] ||  bad_arguments "$@"
        then
            printf "%s\n" ">>> ERR: $0: bad or insufficient arguments" > /dev/stderr
            printf "%s\n" ">>> Usage: $0 INTEGER LETTER" > /dev/stderr
            exit 2
        fi
        make_swap_file "$@" && ask_to_enable_on_boot
    else
        printf ">>> ERR: $0 must run as root\n" > /dev/stderr
        exit 1
    fi
}

main "$@"

Test run

$ sudo ./addswap.sh 1 G                                                                                                                                                                 
[sudo] password for xieerqi: 
Current swap ammount: 4.000000
Working on creating swap file
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 6.83322 s, 157 MB/s
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=8bf1d78d-0a8a-478b-a783-38c5935c362f

Created and turned on /swapfile.1498976162
Current swap ammount: 5.000000Do you want to turn on this file at boot?[y/n]
Y

 /swapfile.1498976162 added to /etc/fstab successfuly
 $ 
Sergiy Kolodyazhnyy
  • 103,293
  • 19
  • 273
  • 492
1

Multiply your count by 2.

If
  1048576 blocks with a size of 4086bytes each = 2GB
then,
  2097152 blocks with a size of 4086bytes each = 4GB

For anyone else reading this, the above values are not accurate. They're based off the values given by the OP

Pablo Bianchi
  • 14,308
  • 4
  • 74
  • 117
amanthethy
  • 1,231
  • 7
  • 12
  • With 2097152 I got 8368124. Are they 8 Gib then? – user299596- Verónica B. Dec 19 '14 at 21:39
  • The first command should have given you 4GBS to start with. Think about it, 4086bytes = ~4KBs * 1048576 = 4194304KBs = ~4.19GB. Either way, this isn't the best way to be making a swap file. – amanthethy Dec 19 '14 at 21:50
  • 1
    Whats up with all the crazy calculations? `bs=1G count=4` please read the [manpage](http://manpages.ubuntu.com/manpages/trusty/en/man1/dd.1.html) – LiveWireBT Dec 20 '14 at 05:48
  • LiveWireBT, I wanted to erase my swapfile but Terminal says: operation prohibited. I don't understand why because I've erased swapfile before without any problem. I put on Terminal: sudo rm /swapfile.swap. I wanted to put your values: bs=1G count=4 . Can you tell me what to do please? – user299596- Verónica B. Dec 20 '14 at 21:54
  • LiveWireBT, don't worry I fixed already. Now I will put what you wrote above : bs=1G count=4, and tell you. Thanks. – user299596- Verónica B. Dec 21 '14 at 09:24
  • It seems I continue having problems. When I put the command, Terminal says: dd= memoria agotada x buffer de entrada de 1073741824 bytes (1,0 Gib). Using Google translator: dd exhausted memory x buffer of entry of 1073741824 bytes (1,0 Gib). And now what will I do? – user299596- Verónica B. Dec 21 '14 at 10:23
  • Well yeah, that's expected. You don't have enough memory to store/create 1GB blocks in the buffer. I don't know why @LiveWireBT expects dd to be able to spit out 1GB blocks with ease. You will have to use a smaller blocksize. – amanthethy Dec 21 '14 at 19:57
  • Hello again. At last I could solve the problem. I read somewhere on the Internet to shut down and re-start, then I was able to create the swapfile. I used bs=1G count=4 and it worked perfectly. I have now 4194300k in swapfile. Thank you very much. – user299596- Verónica B. Dec 22 '14 at 11:19