Problems activating your account? Send notification email to: [email protected]
Host4Fun Budget VPS Hosting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Disable Swap and Regain Storage Space on Linux
#1
Disable Swap and Regain Storage Space on Linux


Hello Post4VPS Community

A few days ago I won the brand new VPS 16 in the December VPS giveaway. Due to lack of time because of my real life job I have only been able to do the basic setup of my new server. One of the first things I did was to upgrade Debian 9 to Debian 10 about which you can read here. After that I did a bit of security tweaking and as the last step so far I did what the subject of this thread is about.

VPS 16 comes with a big 200 GB storage space and KVM as its virtualization. I noticed that a big chunk was missing. What was using it? Well, it was Swap. With 12 GB of RAM the amount of automatic configured Swap was also set to 12 GB during the setup (out of my control!). With proper virtualization Swap is generated by using the disk space like on a real machine. 12 GB by todays standards is a decent amount of RAM and at this point Swap can be disabled to actually better utilize the amount of RAM instead of swapping to a disk *. The goal of this tutorial is to teach you how can disable unwanted Swap space and regain the disk space that it used as normal storage space for your system partition or any other partition.

* You could argue about whether to disable Swap or not. I will not at this point. I'm not going running anything that will fully utilize all 12 GB of RAM. Hence why I don't need any Swap. So keep in mind that you should question yourself and whether you really need Swap or not. Don't make the rash judgement that you don't "need" swap. Think about it. I have done this and made my decision and this is why I'm posting this tutorial.

Requirements
  • A server with Linux on it
  • root access or other administrative access (sudo)
  • cfdisk disk and partition management software
  • basic Linux knowledge

For the purpose of this tutorial I used a Debian "Buster" 10 Hyper-V VM hosted on my computer with a 25 GB VHD, 4 GB RAM (thus also 4 GB Swap) and 2 CPU cores.

Warning: You are doing this at your own risk! I highly recommend to take backups of your system before trying this on any production ready system. Try this on several test systems until you can do it blind (basically at the point where you no longer have to read this or any other guide). And even after that carefully plan any maintenance when you want to do this on a production ready server. I'm NOT responsible for data loss, failed systems or other issues caused/related by or to this procedure.

Let's get started!



Step 1

Login into your server as root or with a user that has administrative access through sudo. Create backups if needed and check if you meet all requirements (is cfdisk available? etc...). Keep your system up to date and fix requirements if necessary.


Step 2

Figure out the disk on which the Swap partition is located by using the command "fdisk -l" in administrative context (without administrative context fdisk won't work properly). The Swap partition will basically be easy to detect as its filesystem/type is usually always "Linux Swap".

Screenshot:
[Image: UmkeF9e.png]

As you can see the Swap partition is located on the /dev/sda3 partition and has a size of 4 GB. In my case as describe before this Swap has been created by the Debian 10 installer through the guided partition installation process.

Write down the following information somewhere: device name (/dev/sda3) and partition size (4GCool of the Swap partition to have this info on quick access in the next step.


Step 3

The next necessary step is to disable Swap and verify that it is disabled. To disable Swap run the following command in administrative context:

Command: swapoff -a

Screenshot
[Image: 5cEZMjz.png]

Check whether Swap has really been disable with the "free -h" command. The Swap value should be 0.

Screenshot
[Image: qD0m6Ie.png]

As you can see Swap is now disabled. However this is only temporary! Swap is a partition and thus it gets mounted everytime your server starts. In the next step we will disable it permanently by preventing the mounting of the Swap partition.


Step 4

To prevent the mount process of the Swap partition you have to modify the fstab file where all automatic mount points are configured. In order to do so you simply have to open fstab with a text editor like nano, vi or vim. Remember to do this within administrative context.

Screenshot
[Image: 1DHK9q7.png]

Once the file is open you have to identify the configuration line at which the Swap partition is mounted. It is also fairly easy to identify the Swap partition as its declared type is "swap". You might also see a comment that tells you which partition is the Swap partition.

Screenshot
[Image: VNZP0od.png]

Move the cursor to the beginning of this line and add a hashtag at the beginning to comment this line. By commenting this line you disable the mount of the Swap partition because comments are simply ignored and not executed.

Screenshot
[Image: vltYHKw.png]

Save the file and close the text editor. You can verify whether what you've done is working by rebooting your server. After the reboot there should be no Swap when running the "free -h" command. Everything good so far? Great. Let's continue.


Step 5

Now it's time to delete the disabled Swap partition with the command line tool to manage disks and partitions called "cfdisk". Start cfdisk in administrative context and specify the drive you want to edit. The drive you want to edit is the device name from step two without the partition number. In this case it's simply "/dev/sda".

You would open this drive in cfdisk with this command: cfdisk /dev/sda.

Screenshot
[Image: fkIaO6g.png]

Now select the Swap partition by using the up/down arrows. After that select "DELETE" at the bottom by using the left/right arrows. Press ENTER and type in yes to confirm. The partition will be deleted and you will see free space available at the point where the partition was.

Screenshots
[Image: k1KXkTu.png]
[Image: bBJZP5B.png]

The Swap partition is gone now.


Step 6

At this point you are ready to expand your system partition or a different partition. Select your desired partition (I will select /dev/sda2 which is my system partition). Select the "Resize" option at the bottom and hit ENTER. Set the desired size (hint: if you leave it as shown it will simply add the free space to the selected partition) and hit ENTER again.

Screenshots
[Image: zkzAj3j.png]
[Image: sqpXFeJ.png]

Now you see that the selected partition has grown and there is no more available free space on the disk.

To finalize this change you have to select "Write" at the bottom options and hit ENTER. This will actually write all changes to the disk that you have performed! If asked confirm that you want to write the changes by writing yes. After that exit cfdisk by selecting "Quit" and hitting ENTER again.

Screenshot
[Image: uzW894s.png]


Step 7

If you run "fdisk -l" again now you will see the change. The Swap partition will be gone and the system partition or your desired partition will be bigger than before by the amount of free space that you got from removing the Swap space.

Execute the "df -h" command and you will notice that the root partition / is still at the old size. The reason for this is that the filesystem inside the partition has not been resized, yet. You can resize the filesystem to use all its space with the "resize2fs" command and the device name of the partition.

Command: resize2fs /dev/sda2

Screenshot
[Image: UZHrVpr.png]

Run "df -h" again and you will see that the filesystem is now bigger.

Screenshot
[Image: t7WHJZn.png]

That's all of it and about it. The Swap is gone and you have regained its disk space as storage space of your desired partition. The goal of this tutorial has been achieved. If you have any questions or need help feel free to post below.



I would like to thank everyone for taking their time and reading this tutorial. Hopefully you can make use of it, learn new things and adopt your new knowledge for further projects and advancements in your journey through the server world and Linux Smile .

P.S.: Sorry for the screenshot format. I just used the Windows snipping tool and only it. The only useful feature to highlight things was the text marker which is kind of hard to use with a mouse. I didn't have much time to fiddle around with Paint.NET. I basically used my only free time (the evening) to write this guide including installing Hyper-V and creating the Debian 10 VM. Also I can't post more than 10 images.

You can use this guide to learn how to generally modify partitions using cfdisk. This can and should be a part of your basic knowledge to manage servers using CLI tools.
Reply
 


Possibly Related Threads...
Thread Author Replies Views Last Post
  How to Remotely Increase Your VPS Disk Space fChk 8 467 01-30-2020, 04:29 PM
Last Post: Honey
  Convert PuTTY SSH Key Files to Linux Format Hidden Refuge 0 514 09-25-2019, 06:37 PM
Last Post: Hidden Refuge
  Setting Up MyBB Forum On a Linux Server ( Ubuntu / Debian ) Rehan 4 559 09-03-2019, 12:55 PM
Last Post: sohamb03
  install ogp agent in linux ubuntu os Khadeer143 2 631 07-10-2019, 04:02 PM
Last Post: Rehan
  Join split files (.001, .002, ...) easily on Linux Hidden Refuge 15 2,464 03-15-2019, 12:04 PM
Last Post: rudra

Forum Jump:


Users browsing this thread: 1 Guest(s)

Sponsors: VirMach - Host4Fun - CubeData - Evolution-Host - HostDare - Hyper Expert - Shadow Hosting - Bladenode - Hostlease - RackNerd - ReadyDedis


About Post4VPS

Post4VPS is a forum/destiny where you can Delploy Your Free VPSs just by the Power of Posts.

We Provide VPSs of many locations like Germany,US,Canada,France,London,etc.

We also Provide VPSs of Both Linux and Windows OS.