Extend swap file linux

How to Increase Swap Size on Ubuntu Linux

In this quick tip, you’ll learn to increase the swap size on Ubuntu and other Linux distributions.

The recent releases of Ubuntu use swap file instead of the traditional swap partition. The swap file is simply a file under the root which is used as swap to share the burden on the RAM.

The biggest advantage of using a swap file is that you can easily resize it. That’s not always the case when you use a dedicated swap partition.

Let’s see how to resize the swap space on Ubuntu.

Increase swap size on Ubuntu

If you are using swap partition and want to increase the swap size, you can create swap file. Your Linux system can use multiple swap spaces as needed. This way, you won’t have to touch the partition.

This tutorial assumes that you are using swap file on your system, not a swap partition.

Now, let’s see how to increase the swap file. First thing first, make sure that you have a swap file in your system.

It will show the current swap available. If you see the type file, it indicates that you are using a swap file.

swapon --show NAME TYPE SIZE USED PRIO /swapfile file 2G 0B -2

Now before you resize the swap file, you should turn the swap off. You should also make sure that you have enough free RAM available to take the data from swap file. Otherwise, create a temporary swap file.

You can disable a given swap file using this command. The command doesn’t produce any output and it may take a few minutes to complete:

Now use the fallocate command in Linux to change the size of the swap file.

sudo fallocate -l 4G /swapfile

Make sure that you mark this file as swap file:

You should see an output like this where it warns you that old swap signature is being wiped out.

sudo mkswap /swapfile mkswap: /swapfile: warning: wiping old swap signature. Setting up swapspace version 1, size = 4 GiB (4294967296 bytes) no label, UUID=c50b27b0-a530-4dd0-9377-aa28eabf3957

Once you do that, enable the swap file:

Читайте также:  Livecd do linux ubuntu

That’s it. You just increased the swap size in Ubuntu from 2 GB to 4 GB. You can check swap size using the free command or the swapon —show command.

free -h total used free shared buff/cache available Mem: 7.7G 873M 5.8G 265M 1.0G 6.3G Swap: 4.0G 0B 4.0G

You see how easy it is to resize swap size thanks to the swap files. You didn’t touch the partition, you didn’t reboot the system. Everything was done on the fly. How cool is that!

Similarly, resizing LVM is also a painless task. I like how these things have improved the Linux experience.

I hope you found this quick tutorial helpful in resizing the swap space on Ubuntu as well as other Linux distributions. If you have questions or suggestions, please leave a comment below.

Источник

How to Extend Swap Space using Swap file in Linux

Swap space is a dedicated storage space in the disk either in the form of partition or a file. Swap space comes into the picture when Linux kernel wants to allocate more memory to processes by moving infrequently used processes or tasks into the swap space.

There are some scenarios where our Linux box is running out of swap space, so in that case we can extend the swap space either via swap partition or via a swap file. In this post we will learn how to extend swap space using a swap file in Linux.

Steps to extend Swap Space using Swap File in Linux

Let’s first check the size of existing swap space / partition using the command like ‘ free -m ‘ and ‘ swapon -s ‘

free-output-with-swap

In my case, swap partition is of 2 GB. So we will be extending swap space by 1 GB.

Step 1) Create a swap file of size 1 GB

Run following dd command to create a file of size 1GB,

[[email protected] ~]# dd if=/dev/zero of=/swap_file bs=1G count=1 1+0 records in 1+0 records out 1073741824 bytes (1.1 GB) copied, 414.898 s, 2.6 MB/s [[email protected] ~]#

Replace the value of ‘ bs ‘ and ‘ count ‘ according your requirement.

We can also use fallocate command to create a file, example is show below.

$ sudo fallocate -l 1G /swap_file

Step 2) Secure the swap file

Secure the swap file by setting the permissions as ‘600’, run beneath chmod command ,

Step 3) Enable the Swap Area on Swap File

Use mkswap command to enable swap area on the file, run

[[email protected] ~]# mkswap /swap_file Setting up swapspace version 1, size = 1048572 KiB no label, UUID=f7b3ae59-c09a-4dc2-ba4d-c02abb7db33b [[email protected] ~]#

Step 4) Add the swap file entry in fstab file

Add the below entry in the fstab file so that swap file become persistent across every reboot. To add below entry either use echo command or vi editor to edit fstab file.

# echo "swap_file swap swap defaults 0 0" >> /etc/fstab or $ vi /etc/fstab/swap_file swap swap defaults 0 0

swap-file-fstab-entry

Step 5) Extend Swap Space

Run swapon command to enable swap space on the file and which will ultimately extend the swap space,

Читайте также:  About linux command line

Step 6) Now verify the swap space

Run ‘ free -m ‘ or ‘ swapon -s ‘ commands to verify swap space.

swap-space-after-extension

Note: To disable the swap file for any troubleshooting point of view, use swapoff command as shown below and to re-enable swap file then use swapon command as shown in step 5.

[[email protected] ~]# swapoff /swap_file [[email protected] ~]#

That’s all from this post, I hope you have found it informative. Please do not hesitate to share your valuable feedback and queries in below comments section.

2 thoughts on “How to Extend Swap Space using Swap file in Linux”

Use fallocate, not dd, for creating /swap_file . It’s faster and doesn’t require an enormous memory buffer.
sudo fallocate -l 4G /swap_file Reply

The mkswap manpage has a warning NOT to use fallocate on most systems: “Note that a swap file must not contain any holes. Using cp(1) to create the file is not acceptable. Neither is use of fallocate(1) on file systems that support preallocated files, such as XFS or ext4, or on copy-on-write filesystems like btrfs. It is recommended to use dd(1) and /dev/zero in these cases. “ Reply

Leave a Comment Cancel reply

Recent Posts

WP-Subscribe-MailChimp

  • Top 10 Things to Do After Installing Debian 12 (Bookworm)
  • How to Install Debian 12 (Bookworm) Step-by-Step
  • How to Upgrade Debian 11 to Debian 12 (Bookworm) via CLI
  • How to Setup Dynamic NFS Provisioning in Kubernetes Cluster
  • How to Install Nagios on Rocky Linux 9 / Alma Linux 9
  • How to Install Ansible AWX on Kubernetes Cluster
  • How to Install Docker on Fedora 38/37 Step-by-Step
  • How to Setup High Availability Apache (HTTP) Cluster on RHEL 9/8
  • How to Install FreeIPA Client on RHEL | Rocky Linux | AlmaLinux
  • How to Configure DHCP Server on RHEL 9 / Rocky Linux 9

Pages

Источник

How to extend an LVM swap partition in Linux

Question: Given an LVM partition, how can we extend it to give some addtional space?

Swap filesystem

Swap is used if there is not enough memory available for your application. It’s normal and can be a good thing for Linux systems to use swap, even if there is still available RAM. But, it’s not just used if there is not enough memory.

Читайте также:  Install source file linux

How to extend LVM based swap filesystem

To increase the space for LVM Swap partition follow the steps given below:

1. Verify availability of the new space.

# fdisk -l /dev/sda Disk /dev/sda: 4294 MB, 4294967296 bytes, 8388608 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 byte

2. Create additional partition for the new swap partition.

# fdisk /dev/sda Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): First sector (2048-8388607, default 2048): Using default value 2048 Last sector, +sectors or +size (2048-8388607, default 8388607): 4056 Partition 1 of type Linux and of size 1004.5 KiB is set Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): First sector (2048-8388607, default 2048): Using default value 2048 Last sector, +sectors or +size (2048-8388607, default 8388607): 4056 Partition 1 of type Linux and of size 1004.5 KiB is set Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.

3. Activate the new partition.

4. Verify the new partition is available.

# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 12G 0 disk ├─sda1 8:1 0 500M 0 part /boot └─sda2 8:2 0 11.5G 0 part ├─vg_lv_root (dm-0) 251:0 0 10.3G 0 lvm / └─vg_lv_swap (dm-1) 251:1 0 1.2G 0 lvm [SWAP] └─sda3 8:3 0 1G 0 part sr0 11:0 1 1024M 0 rom

Note: a reboot may be needed if the change does not show at this point.

5. Create a new physical volume on the LUN.

6. Add the new volume to the volume group for the swap volume. Our examples use SwapVG and /dev/sda3; replace with the volume names and devices as appropriate to your deployment.

7. Disable swapping for the associated physical volume.

# swapoff -v /dev/SwapVG/SwapLV

8. Resize the logical volume to the desired size.

# lvresize /dev/SwapVG/SwapLV -L +8G

9. Format the extended swap volume.

10. Enable the logical volume.

11. View the new swap size.

# cat /proc/swaps Filename Type Size Used Priority /dev/dm-1 partition 1257468 0 -1
# free total used free shared buffers cached Mem: 1784432 196920 1587512 516 12624 77268 -/+ buffers/cache: 107028 1677404 Swap: 1257468 0 1257468

Источник

Оцените статью
Adblock
detector