1

I have a virtual machine (hosted online) with 2Gb swap space.

free -m

             total       used       free     shared    buffers     cached
Mem:          1995        438       1557         37         22        190
-/+ buffers/cache:        225       1770
Swap:         1998          0       1998

Since the swap space was small, I tried to increase it to 4GB.

Doing swapon -s, returned this result (not sure what it means since I don't have a second hard disk sda2)

Filename                                Type            Size    Used    Priority
/dev/sda2                               partition       2046972 0       -1

df -H

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        25G   19G  4.3G  82% /
udev            1.1G  4.1k  1.1G   1% /dev
tmpfs           210M  492k  209M   1% /run
none            4.1k     0  4.1k   0% /sys/fs/cgroup
none            5.3M     0  5.3M   0% /run/lock
none            1.1G     0  1.1G   0% /run/shm
none            105M     0  105M   0% /run/user

So I did

swapoff -a                #deactivating previous swap space
fallocate -l 4G /swapfile #creating a swapfile
chmod 600 /swapfile       #giving permissions
mkswap /swapfile          #making swap the file
swapon /swapfile          #activating swap on the file
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab #stabilizying swap for it to be there after next reboot

Now I have the swap space I needed

free -m

             total       used       free     shared    buffers     cached Mem:          1995       1153        842         64        116 
455
-/+ buffers/cache:        581       1414 Swap:         4095          0       4095

But, the 2GB of the previous swap space are still considered occupied if I do a df (maybe they will be free at the next reboot?) and my fstab has two entries for swap space.

# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/sda1       /       ext4    errors=remount-ro,relatime      0       1
/dev/sda2       swap    swap    defaults        0       0
proc            /proc   proc    defaults                0       0
sysfs           /sys    sysfs   defaults                0       0
devtmpfs        /dev    devtmpfs        rw      0       0
/swapfile none swap sw 0 0

In this situation, how I may proceed to finalize the procedure? I have to remove the second line referring to swap in the fstab? (I mean /dev/sda2 swap swap defaults 0 0) It is safe to reboot?

Sasha Grievus
  • 223
  • 2
  • 11
  • Questions about running Linux should be asked on https://unix.stackexchange.com/ or, in this case, https://askubuntu.com/ – Rob Jan 09 '19 at 14:46
  • Notice that a swap partition is usually a better idea than a swap file. The swap partition is a bit more efficient. And you don't run the risk of not being able to shut down cleanly because you cannot unmount the file system due to a too high memory usage. – kasperd Jan 13 '19 at 22:16

1 Answers1

3

Well, you could remove the old swap partition (using fdisk or a different partitioning program) and resize your primary partition to use the space, but this requires shell access or booting from a rescue environment and if you don't know what you are doing you could wreck your whole VM.

Much easier: just create a 2GB swap file instead of your 4GB file and use both the file and the partition. Swap space does not have to be continuous space, it can be on different disks, or a partition and a file.

Gerald Schneider
  • 19,757
  • 8
  • 52
  • 79